feisty meow concerns codebase 2.140
configlet.cpp
Go to the documentation of this file.
1/*****************************************************************************\
2* *
3* Name : configlet *
4* Author : Chris Koeritz *
5* *
6*******************************************************************************
7* Copyright (c) 2001-$now By Author. This program is free software; you can *
8* redistribute it and/or modify it under the terms of the GNU General Public *
9* License as published by the Free Software Foundation; either version 2 of *
10* the License or (at your option) any later version. This is online at: *
11* http://www.fsf.org/copyleft/gpl.html *
12* Please send any updates to: fred@gruntose.com *
13\*****************************************************************************/
14
15#include "configlet.h"
16#include "configurator.h"
17
18#include <basis/astring.h>
19#include <basis/functions.h>
20
21using namespace basis;
22
23namespace configuration {
24
25const astring bogus_default = "OOPS: not supposed to ever be seen. d'oh!";
26
28
29configlet::configlet(const astring &section, const astring &entry)
30: _section(new astring(section)),
31 _entry(new astring(entry))
32{}
33
35: _section(new astring(*to_copy._section)),
36 _entry(new astring(*to_copy._entry))
37{}
38
40{
41 WHACK(_section);
42 WHACK(_entry);
43}
44
46{
47 if (this == &to_copy) return *this;
48 *_section = *to_copy._section;
49 *_entry = *to_copy._entry;
50 return *this;
51}
52
53const astring &configlet::section() const { return *_section; }
54
55const astring &configlet::entry() const { return *_entry; }
56
57void configlet::section(const astring &new_section) const
58{ *_section = new_section; }
59
60void configlet::entry(const astring &new_entry) const
61{ *_entry = new_entry; }
62
64
66 const astring &current_value, const astring &default_value)
67: configlet(section, entry),
68 _current(new astring(current_value)),
69 _default(new astring(default_value))
70{}
71
73: configlet(to_copy.section(), to_copy.entry()),
74 _current(new astring(*to_copy._current)),
75 _default(new astring(*to_copy._default))
76{
77}
78
80{
81 WHACK(_current);
82 WHACK(_default);
83}
84
86{
87 if (this == &to_copy) return *this;
88 (configlet &)*this = to_copy;
89 *_current = *to_copy._current;
90 *_default = *to_copy._default;
91 return *this;
92}
93
94const astring &string_configlet::current_value() const { return *_current; }
95
96const astring &string_configlet::default_value() const { return *_default; }
97
99{ *_current = new_current; }
100
102{ *_default = new_default; }
103
105{
106 if (config.get(section(), entry(), *_current)) return true; // success.
107 // we failed to read the value...
108 *_current = *_default;
109 if (config.behavior() == configurator::AUTO_STORE)
110 config.put(section(), entry(), *_current);
111 return false; // don't hide that it wasn't there.
112}
113
115{ return config.put(section(), entry(), *_current); }
116
118{ return new string_configlet(section(), entry(), *_current, *_default); }
119
121
122int_configlet::int_configlet(const astring &section, const astring &entry,
123 int current_value, int default_value)
124: configlet(section, entry),
125 _current(current_value),
126 _default(default_value)
127{
128}
129
131
132void int_configlet::current_value(int new_current)
133{ _current = new_current; }
134
136{
137 astring temp;
138 bool to_return = config.get(section(), entry(), temp);
139 // just check if it was already there.
140 int temp_c = config.load(section(), entry(), _default);
141 current_value(temp_c);
142 return to_return;
143}
144
146{ return config.store(section(), entry(), _current); }
147
149{ return new int_configlet(*this); }
150
152
154 const astring &entry, int current_value, int default_value,
155 int minimum, int maximum)
156: int_configlet(section, entry, current_value, default_value),
157 _minimum(minimum),
158 _maximum(maximum)
159{
160}
161
163
165{
166 if (new_current < _minimum)
167 new_current = default_value();
168 if (new_current > _maximum)
169 new_current = default_value();
170 int_configlet::current_value(new_current);
171}
172
175
176} //namespace.
177
Provides a dynamically resizable ASCII character string.
Definition astring.h:35
Stores an integer in a configuration repository with range checking.
Definition configlet.h:151
bounded_int_configlet(const basis::astring &section, const basis::astring &entry, int current_value, int default_value, int minimum, int maximum)
configlet * duplicate() const
a virtual copy constructor for configlets.
Represents an atom of configuration info.
Definition configlet.h:33
configlet & operator=(const configlet &to_copy)
Definition configlet.cpp:45
const basis::astring & section() const
observes the section of this configlet.
Definition configlet.cpp:53
configlet(const basis::astring &section, const basis::astring &entry)
creates a configlet that lives in the "section" at the "entry".
Definition configlet.cpp:29
const basis::astring & entry() const
observes the entry name of this configlet.
Definition configlet.cpp:55
Provides a base class for configuration repositories.
bool store(const basis::astring &section, const basis::astring &entry, const basis::astring &to_store)
a synonym for put.
virtual bool put(const basis::astring &section, const basis::astring &entry, const basis::astring &to_store)=0
Places an item into the configuration store.
treatment_of_defaults behavior() const
observes the behavior chosen for the load() function.
virtual bool get(const basis::astring &section, const basis::astring &entry, basis::astring &found)=0
Retrieves an item from the configuration store.
basis::astring load(const basis::astring &section, const basis::astring &entry, const basis::astring &default_value)
a synonym for get that implements the auto-store behavior.
Stores a simple integer in a configuration repository.
Definition configlet.h:116
int_configlet(const basis::astring &section, const basis::astring &entry, int current_value=0, int default_value=0)
virtual bool store(configurator &config) const
writes the configlet's information out to the "config".
configlet * duplicate() const
a virtual copy constructor for configlets.
virtual bool load(configurator &config)
retrieves the configlet's information from the "config".
a string_configlet holds onto a character string value.
Definition configlet.h:85
const basis::astring & current_value() const
Definition configlet.cpp:94
virtual bool store(configurator &config) const
writes the configlet's information out to the "config".
virtual bool load(configurator &config)
retrieves the configlet's information from the "config".
configlet * duplicate() const
a virtual copy constructor for configlets.
string_configlet & operator=(const string_configlet &to_copy)
Definition configlet.cpp:85
string_configlet(const basis::astring &section, const basis::astring &entry, const basis::astring &current_value=basis::astring::empty_string(), const basis::astring &default_value=basis::astring::empty_string())
Definition configlet.cpp:65
const basis::astring & default_value() const
Definition configlet.cpp:96
The guards collection helps in testing preconditions and reporting errors.
Definition array.h:30
void WHACK(contents *&ptr)
deletion with clearing of the pointer.
Definition functions.h:121
type maximum(type a, type b)
minimum returns the lesser of two values.
Definition functions.h:26
type minimum(type a, type b)
maximum returns the greater of two values.
Definition functions.h:29
const astring bogus_default
Definition configlet.cpp:25