libcyberradio 22.01.24
Configurable.cpp
1/***************************************************************************
2 * \file Configurable.cpp
3 * \brief Defines the basic interface for an object configurable through
4 * dictionaries.
5 * \author DA
6 * \author NH
7 * \author MN
8 * \copyright (c) 2017 CyberRadio Solutions, Inc. All rights reserved.
9 *
10 ***************************************************************************/
11
12#include "LibCyberRadio/Driver/Configurable.h"
13#include <sstream>
14#include <algorithm>
15#include <cstdio>
16#include <cstdarg>
17#include <ctype.h>
18
19
20namespace LibCyberRadio
21{
22 namespace Driver
23 {
24
26 BASIC_DICT_CONTAINER<std::string, ConfigString>()
27 {
28 }
29
34
35 std::string ConfigurationDict::toString() const
36 {
37 std::ostringstream oss;
38 oss << "Configuration(";
39 ConfigurationDict::const_iterator it;
40 for ( it = this->begin(); it != this->end(); it++)
41 {
42 if ( it != this->begin() )
43 oss << ", ";
44 oss << it->first.c_str()
45 << "="
46 << it->second.c_str();
47 }
48 oss << ")";
49 return oss.str();
50 }
51
52 bool ConfigurationDict::hasKey(const std::string& key) const
53 {
54 return ( this->find(key) != this->end() );
55 }
56
57 Configurable::Configurable(const std::string& name, bool debug) :
58 Debuggable(debug, name),
59 _name(name)
60 {
61 }
62
66
68 _name(other._name),
69 _config(other._config)
70 {
71 }
72
74 {
75 if ( this != &other )
76 {
77 _name = other._name;
78 _config = other._config;
79 }
80 return *this;
81 }
82
83 std::string Configurable::getName() const
84 {
85 return _name;
86 }
87
88 void Configurable::setName(const std::string &name)
89 {
90 _name = name;
91 }
92
94 {
95 return _config;
96 }
97
99 {
100 ConfigString ret = "";
101 ConfigurationDict::const_iterator it = _config.find(key);
102 if ( it != _config.end() )
103 {
104 ret = it->second;
105 }
106 return ret;
107 }
108
109 bool Configurable::getConfigurationValueAsBool(const std::string& key) const
110 {
111 bool ret = false;
112 try
113 {
114 ret = ( getConfigurationValue(key).asBool() );
115 }
116 catch(std::exception& ex)
117 {
118 }
119 return ret;
120 }
121
122 int Configurable::getConfigurationValueAsInt(const std::string& key) const
123 {
124 int ret = 0;
125 try
126 {
127 ret = ( getConfigurationValue(key).asInt() );
128 }
129 catch(std::exception& ex)
130 {
131 }
132 return ret;
133 }
134
135 unsigned int Configurable::getConfigurationValueAsUInt(const std::string& key) const
136 {
137 unsigned int ret = 0;
138 try
139 {
140 ret = ( getConfigurationValue(key).asUInt() );
141 }
142 catch(std::exception& ex)
143 {
144 }
145 return ret;
146 }
147
148 double Configurable::getConfigurationValueAsDbl(const std::string& key) const
149 {
150 double ret = 0.0;
151 try
152 {
153 ret = ( getConfigurationValue(key).asDouble() );
154 }
155 catch(std::exception& ex)
156 {
157 }
158 return ret;
159 }
160
162 {
163 // Replace elements in the configuration dictionary with
164 // normalized elements in the incoming dictionary that have the
165 // same key
167 ConfigurationDict::iterator nit;
168 for (ConfigurationDict::iterator it = _config.begin(); it != _config.end(); it++)
169 {
170 nit = normCfg.find(it->first);
171 if ( nit != normCfg.end() )
172 it->second = nit->second;
173 }
174 return true;
175 }
176
177 bool Configurable::setConfigurationValue(const std::string& key,
178 const std::string& value)
179 {
180 bool ret = false;
181 ConfigurationDict::iterator it = _config.find(key);
182 if ( it != _config.end() )
183 {
184 _config[key] = normalizedBool(value);
185 ret = true;
186 }
187 else
188 {
189 }
190 return ret;
191 }
192
193 bool Configurable::setConfigurationValueToBool(const std::string& key,
194 const bool value)
195 {
196 return setConfigurationValue(key, ConfigString(value));
197 }
198
199 bool Configurable::setConfigurationValueToInt(const std::string& key,
200 const int value)
201 {
202 return setConfigurationValue(key, ConfigString(value));
203 }
204
205 bool Configurable::setConfigurationValueToUInt(const std::string& key,
206 const unsigned int value)
207 {
208 return setConfigurationValue(key, ConfigString(value));
209 }
210
211 bool Configurable::setConfigurationValueToDbl(const std::string& key,
212 const double value)
213 {
214 return setConfigurationValue(key, ConfigString(value));
215 }
216
220
224
228
229 // Normalization converts strings representing Boolean values to
230 // standard "0" or "1"
231
233 const ConfigurationDict& cfg)
234 {
235 ConfigurationDict adjCfg = cfg;
236 for (ConfigurationDict::iterator it = adjCfg.begin(); it != adjCfg.end(); it++)
237 it->second = normalizedBool(it->second);
238 return adjCfg;
239 }
240
241 std::string Configurable::normalizedBool(const std::string& val)
242 {
243 std::string ret = "";
244 std::string adjVal = val;
245 std::transform(val.begin(), val.end(), adjVal.begin(), tolower);
246 if ( (adjVal == "1") || (adjVal == "yes") || (adjVal == "on") || (adjVal == "true") )
247 ret = "1";
248 else if ( (adjVal == "0") || (adjVal == "no") || (adjVal == "off") || (adjVal == "false") )
249 ret = "0";
250 else
251 ret = val;
252 return ret;
253 }
254
256 {
257 this->debug("Configuration:\n");
258 ConfigurationDict::const_iterator it;
259 for ( it = _config.begin(); it != _config.end(); it++)
260 {
261 this->debug("-- %s = %s\n", it->first.c_str(), it->second.c_str());
262 }
263 this->debug("[end configuration]\n");
264 }
265
266 } // namespace Driver
267
268} // namespace LibCyberRadio
269
270
271std::ostream& operator<<(std::ostream& os, const LibCyberRadio::Driver::ConfigurationDict& obj)
272{
273 os << obj.toString();
274 return os;
275}
276
virtual int debug(const char *format,...)
Outputs debug information.
Debuggable(bool debug=false, const std::string &debug_name="", FILE *debug_fp=DEBUG_FP, const std::string &debug_timefmt=DEBUG_TIME_FMT)
Constructs a Debuggable object.
Configuration value string class.
bool asBool() const
Retrieves a value from this object.
int asInt() const
Retrieves a value from this object.
double asDouble() const
Retrieves a value from this object.
unsigned int asUInt() const
Retrieves a value from this object.
virtual bool setConfiguration(ConfigurationDict &cfg)
Sets the configuration dictionary for this object.
virtual int getConfigurationValueAsInt(const std::string &key) const
Gets a named configuration value as an integer value.
virtual ConfigurationDict normalizedConfigurationDict(const ConfigurationDict &cfg)
Normalizes an incoming configuration dictionary.
virtual ~Configurable()
Destroys a Configurable object.
virtual double getConfigurationValueAsDbl(const std::string &key) const
Gets a named configuration value as a double value.
virtual bool getConfigurationValueAsBool(const std::string &key) const
Gets a named configuration value as a Boolean.
virtual Configurable & operator=(const Configurable &other)
Assignment operator for Configurable objects.
virtual ConfigurationDict getConfiguration() const
Gets the configuration dictionary for this object.
virtual unsigned int getConfigurationValueAsUInt(const std::string &key) const
Gets a named configuration value as an unsigned integer value.
virtual std::string getName() const
Gets the name of the configurable object.
virtual bool setConfigurationValue(const std::string &key, const std::string &value)
Sets a named configuration value to a string.
virtual void queryConfiguration()
Tells the object to create its configuration dictionary.
virtual bool setConfigurationValueToUInt(const std::string &key, const unsigned int value)
Sets a named configuration value to an unsigned integer value.
virtual std::string normalizedBool(const std::string &val)
Normalizes a Boolean string value.
virtual void dumpConfiguration()
Dumps this object's configuration dictionary to debug output.
virtual void setName(const std::string &name)
Sets the name of the configurable object.
virtual void updateConfigurationDict()
Updates the configuration dictionary from object settings.
Configurable(const std::string &name="<unknown>", bool debug=false)
Constructs a Configurable object.
virtual bool setConfigurationValueToInt(const std::string &key, const int value)
Sets a named configuration value to an integer value.
virtual bool setConfigurationValueToDbl(const std::string &key, const double value)
Sets a named configuration value to a double value.
virtual ConfigString getConfigurationValue(const std::string &key) const
Gets a named configuration value as a string.
virtual void initConfigurationDict()
Initializes the configuration dictionary, defining the allowed keys.
virtual bool setConfigurationValueToBool(const std::string &key, const bool value)
Sets a named configuration value to a Boolean.
A configuration dictionary.
virtual std::string toString() const
Construct a string representation of this object.
ConfigurationDict()
Constructs a ConfigurationDict object.
virtual bool hasKey(const std::string &key) const
Determines if the dictionary has the given key.
virtual ~ConfigurationDict()
Destroys a ConfigurationDict object.
Provides programming elements for driving CRS NDR-class radios.
Defines functionality for LibCyberRadio applications.
Definition App.h:24