configlet.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : configlet                                                         *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *******************************************************************************
00007 * Copyright (c) 2001-$now By Author.  This program is free software; you can  *
00008 * redistribute it and/or modify it under the terms of the GNU General Public  *
00009 * License as published by the Free Software Foundation; either version 2 of   *
00010 * the License or (at your option) any later version.  This is online at:      *
00011 *     http://www.fsf.org/copyleft/gpl.html                                    *
00012 * Please send any updates to: fred@gruntose.com                               *
00013 \*****************************************************************************/
00014 
00015 #include "configlet.h"
00016 #include "configurator.h"
00017 
00018 #include <basis/astring.h>
00019 #include <basis/functions.h>
00020 
00021 using namespace basis;
00022 
00023 namespace configuration {
00024 
00025 const astring bogus_default = "OOPS: not supposed to ever be seen.  d'oh!";
00026 
00028 
00029 configlet::configlet(const astring &section, const astring &entry)
00030 : _section(new astring(section)),
00031   _entry(new astring(entry))
00032 {}
00033 
00034 configlet::configlet(const configlet &to_copy)
00035 : _section(new astring(*to_copy._section)),
00036   _entry(new astring(*to_copy._entry))
00037 {}
00038 
00039 configlet::~configlet()
00040 {
00041   WHACK(_section);
00042   WHACK(_entry);
00043 }
00044 
00045 configlet &configlet::operator =(const configlet &to_copy)
00046 {
00047   if (this == &to_copy) return *this;
00048   *_section = *to_copy._section;
00049   *_entry = *to_copy._entry;
00050   return *this;
00051 }
00052 
00053 const astring &configlet::section() const { return *_section; }
00054 
00055 const astring &configlet::entry() const { return *_entry; }
00056 
00057 void configlet::section(const astring &new_section) const
00058 { *_section = new_section; }
00059 
00060 void configlet::entry(const astring &new_entry) const
00061 { *_entry = new_entry; }
00062 
00064 
00065 string_configlet::string_configlet(const astring &section, const astring &entry,
00066     const astring &current_value, const astring &default_value)
00067 : configlet(section, entry),
00068   _current(new astring(current_value)),
00069   _default(new astring(default_value))
00070 {}
00071 
00072 string_configlet::string_configlet(const string_configlet &to_copy)
00073 : configlet(to_copy.section(), to_copy.entry()),
00074   _current(new astring(*to_copy._current)),
00075   _default(new astring(*to_copy._default))
00076 {
00077 }
00078 
00079 string_configlet::~string_configlet()
00080 {
00081   WHACK(_current);
00082   WHACK(_default);
00083 }
00084 
00085 string_configlet &string_configlet::operator =(const string_configlet &to_copy)
00086 {
00087   if (this == &to_copy) return *this;
00088   (configlet &)*this = to_copy;
00089   *_current = *to_copy._current;
00090   *_default = *to_copy._default;
00091   return *this;
00092 }
00093 
00094 const astring &string_configlet::current_value() const { return *_current; }
00095 
00096 const astring &string_configlet::default_value() const { return *_default; }
00097 
00098 void string_configlet::current_value(const astring &new_current)
00099 { *_current = new_current; }
00100 
00101 void string_configlet::default_value(const astring &new_default)
00102 { *_default = new_default; }
00103   
00104 bool string_configlet::load(configurator &config)
00105 {
00106   if (config.get(section(), entry(), *_current)) return true;  // success.
00107   // we failed to read the value...
00108   *_current = *_default;
00109   if (config.behavior() == configurator::AUTO_STORE)
00110     config.put(section(), entry(), *_current);
00111   return false;  // don't hide that it wasn't there.
00112 }
00113 
00114 bool string_configlet::store(configurator &config) const
00115 { return config.put(section(), entry(), *_current); }
00116 
00117 configlet *string_configlet::duplicate() const
00118 { return new string_configlet(section(), entry(), *_current, *_default); }
00119 
00121 
00122 int_configlet::int_configlet(const astring &section, const astring &entry,
00123     int current_value, int default_value)
00124 : configlet(section, entry),
00125   _current(current_value),
00126   _default(default_value)
00127 {
00128 }
00129 
00130 int_configlet::~int_configlet() {}
00131 
00132 void int_configlet::current_value(int new_current)
00133 { _current = new_current; }
00134 
00135 bool int_configlet::load(configurator &config)
00136 {
00137   astring temp;
00138   bool to_return = config.get(section(), entry(), temp);
00139     // just check if it was already there.
00140   int temp_c = config.load(section(), entry(), _default);
00141   current_value(temp_c);
00142   return to_return;
00143 }
00144 
00145 bool int_configlet::store(configurator &config) const
00146 { return config.store(section(), entry(), _current); }
00147 
00148 configlet *int_configlet::duplicate() const
00149 { return new int_configlet(*this); }
00150 
00152 
00153 bounded_int_configlet::bounded_int_configlet(const astring &section,
00154     const astring &entry, int current_value, int default_value,
00155     int minimum, int maximum)
00156 : int_configlet(section, entry, current_value, default_value),
00157   _minimum(minimum),
00158   _maximum(maximum)
00159 {
00160 }
00161 
00162 bounded_int_configlet::~bounded_int_configlet() {}
00163 
00164 void bounded_int_configlet::current_value(int new_current)
00165 {
00166   if (new_current < _minimum)
00167     new_current = default_value();
00168   if (new_current > _maximum)
00169     new_current = default_value();
00170   int_configlet::current_value(new_current);
00171 }
00172 
00173 configlet *bounded_int_configlet::duplicate() const
00174 { return new bounded_int_configlet(*this); }
00175 
00176 } //namespace.
00177 
Generated on Sat Jan 28 04:22:17 2012 for hoople2 project by  doxygen 1.6.3