Open Lighting Architecture  Latest Git
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ExportMap.h
Go to the documentation of this file.
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * ExportMap.h
17  * Interface the ExportMap and ExportedVariables
18  * Copyright (C) 2005 Simon Newton
19  */
20 
30 #ifndef INCLUDE_OLA_EXPORTMAP_H_
31 #define INCLUDE_OLA_EXPORTMAP_H_
32 
33 #include <ola/base/Macro.h>
34 #include <ola/StringUtils.h>
35 #include <stdlib.h>
36 
37 #include <functional>
38 #include <map>
39 #include <sstream>
40 #include <string>
41 #include <vector>
42 
43 namespace ola {
44 
51 class BaseVariable {
52  public:
57  explicit BaseVariable(const std::string &name): m_name(name) {}
58 
62  virtual ~BaseVariable() {}
63 
68  const std::string Name() const { return m_name; }
69 
74  virtual const std::string Value() const = 0;
75 
76  private:
77  std::string m_name;
78 };
79 
80 struct VariableLessThan: public std::binary_function<BaseVariable*,
81  BaseVariable*, bool> {
82  bool operator()(BaseVariable *x, BaseVariable *y) {
83  return x->Name() < y->Name();
84  }
85 };
86 
87 
92 class BoolVariable: public BaseVariable {
93  public:
98  explicit BoolVariable(const std::string &name)
99  : BaseVariable(name),
100  m_value(false) {}
101  ~BoolVariable() {}
102 
107  void Set(bool value) { m_value = value; }
108 
113  bool Get() const { return m_value; }
114 
121  const std::string Value() const { return m_value ? "1" : "0"; }
122 
123  private:
124  bool m_value;
125 };
126 
127 
128 /*
129  * Represents a string variable
130  */
132  public:
133  explicit StringVariable(const std::string &name)
134  : BaseVariable(name),
135  m_value("") {}
136  ~StringVariable() {}
137 
138  void Set(const std::string &value) { m_value = value; }
139  const std::string Get() const { return m_value; }
140  const std::string Value() const { return m_value; }
141 
142  private:
143  std::string m_value;
144 };
145 
146 
147 /*
148  * Represents a integer variable
149  */
151  public:
152  explicit IntegerVariable(const std::string &name)
153  : BaseVariable(name),
154  m_value(0) {}
155  ~IntegerVariable() {}
156 
157  void Set(int value) { m_value = value; }
158  void operator++(int) { m_value++; }
159  void operator--(int) { m_value--; }
160  void Reset() { m_value = 0; }
161  int Get() const { return m_value; }
162  const std::string Value() const {
163  std::ostringstream out;
164  out << m_value;
165  return out.str();
166  }
167 
168  private:
169  int m_value;
170 };
171 
172 
173 /*
174  * Represents a counter which can only be added to.
175  */
177  public:
178  explicit CounterVariable(const std::string &name)
179  : BaseVariable(name),
180  m_value(0) {}
181  ~CounterVariable() {}
182 
183  void operator++(int) { m_value++; }
184  void operator+=(unsigned int value) { m_value += value; }
185  void Reset() { m_value = 0; }
186  unsigned int Get() const { return m_value; }
187  const std::string Value() const {
188  std::ostringstream out;
189  out << m_value;
190  return out.str();
191  }
192 
193  private:
194  unsigned int m_value;
195 };
196 
197 
198 /*
199  * A Map variable holds string -> type mappings
200  */
201 template<typename Type>
202 class MapVariable: public BaseVariable {
203  public:
204  MapVariable(const std::string &name, const std::string &label)
205  : BaseVariable(name),
206  m_label(label) {}
207  ~MapVariable() {}
208 
209  void Remove(const std::string &key);
210  void Set(const std::string &key, Type value);
211  Type &operator[](const std::string &key);
212  const std::string Value() const;
213  const std::string Label() const { return m_label; }
214 
215  protected:
216  std::map<std::string, Type> m_variables;
217 
218  private:
219  std::string m_label;
220 };
221 
223 
224 
228 class IntMap: public MapVariable<int> {
229  public:
230  IntMap(const std::string &name, const std::string &label)
231  : MapVariable<int>(name, label) {}
232 
233  void Increment(const std::string &key) {
234  m_variables[key]++;
235  }
236 };
237 
238 
242 class UIntMap: public MapVariable<unsigned int> {
243  public:
244  UIntMap(const std::string &name, const std::string &label)
245  : MapVariable<unsigned int>(name, label) {}
246 
247  void Increment(const std::string &key) {
248  m_variables[key]++;
249  }
250 };
251 
252 
253 /*
254  * Return a value from the Map Variable, this will create an entry in the map
255  * if the variable doesn't exist.
256  */
257 template<typename Type>
258 Type &MapVariable<Type>::operator[](const std::string &key) {
259  return m_variables[key];
260 }
261 
262 
263 /*
264  * Set a value in the Map variable.
265  */
266 template<typename Type>
267 void MapVariable<Type>::Set(const std::string &key, Type value) {
268  m_variables[key] = value;
269 }
270 
271 
276 template<typename Type>
277 void MapVariable<Type>::Remove(const std::string &key) {
278  typename std::map<std::string, Type>::iterator iter = m_variables.find(key);
279 
280  if (iter != m_variables.end())
281  m_variables.erase(iter);
282 }
283 
284 /*
285  * Return the string representation of this map variable.
286  * The form is:
287  * var_name map:label_name key1:value1 key2:value2
288  * @return the string representation of the variable.
289  */
290 template<typename Type>
291 inline const std::string MapVariable<Type>::Value() const {
292  std::ostringstream value;
293  value << "map:" << m_label;
294  typename std::map<std::string, Type>::const_iterator iter;
295  for (iter = m_variables.begin(); iter != m_variables.end(); ++iter)
296  value << " " << iter->first << ":" << iter->second;
297  return value.str();
298 }
299 
300 
301 /*
302  * Strings need to be quoted
303  */
304 template<>
305 inline const std::string MapVariable<std::string>::Value() const {
306  std::ostringstream value;
307  value << "map:" << m_label;
308  std::map<std::string, std::string>::const_iterator iter;
309  for (iter = m_variables.begin(); iter != m_variables.end(); ++iter) {
310  std::string var = iter->second;
311  Escape(&var);
312  value << " " << iter->first << ":\"" << var << "\"";
313  }
314  return value.str();
315 }
316 
317 
318 
319 
324 class ExportMap {
325  public:
326  ExportMap() {}
327  ~ExportMap();
328 
337  BoolVariable *GetBoolVar(const std::string &name);
338 
347  IntegerVariable *GetIntegerVar(const std::string &name);
348 
357  CounterVariable *GetCounterVar(const std::string &name);
358 
367  StringVariable *GetStringVar(const std::string &name);
368 
369  StringMap *GetStringMapVar(const std::string &name,
370  const std::string &label = "");
371  IntMap *GetIntMapVar(const std::string &name, const std::string &label = "");
372  UIntMap *GetUIntMapVar(const std::string &name,
373  const std::string &label = "");
374 
379  std::vector<BaseVariable*> AllVariables() const;
380 
381  private :
382  template<typename Type>
383  Type *GetVar(std::map<std::string, Type*> *var_map,
384  const std::string &name);
385 
386  template<typename Type>
387  Type *GetMapVar(std::map<std::string, Type*> *var_map,
388  const std::string &name,
389  const std::string &label);
390 
391  std::map<std::string, BoolVariable*> m_bool_variables;
392  std::map<std::string, CounterVariable*> m_counter_variables;
393  std::map<std::string, IntegerVariable*> m_int_variables;
394  std::map<std::string, StringVariable*> m_string_variables;
395 
396  std::map<std::string, StringMap*> m_str_map_variables;
397  std::map<std::string, IntMap*> m_int_map_variables;
398  std::map<std::string, UIntMap*> m_uint_map_variables;
399 
400  DISALLOW_COPY_AND_ASSIGN(ExportMap);
401 };
402 } // namespace ola
403 #endif // INCLUDE_OLA_EXPORTMAP_H_
void Set(bool value)
Set the value of the variable.
Definition: ExportMap.h:107
A container for the exported variables.
Definition: ExportMap.h:324
BoolVariable(const std::string &name)
Create a new BoolVariable.
Definition: ExportMap.h:98
BaseVariable(const std::string &name)
Create a new BaseVariable.
Definition: ExportMap.h:57
Definition: ExportMap.h:228
const std::string Value() const
Return the value of the variable as a string.
Definition: ExportMap.h:140
const std::string Value() const
Return the value of the variable as a string.
Definition: ExportMap.h:187
Definition: ExportMap.h:176
std::vector< BaseVariable * > AllVariables() const
Fetch a list of all known variables.
Definition: ExportMap.cpp:101
Definition: ExportMap.h:202
bool Get() const
Get the value of this variable.
Definition: ExportMap.h:113
Definition: ExportMap.h:242
The base variable class.
Definition: ExportMap.h:51
Definition: ExportMap.h:131
void Remove(const std::string &key)
Definition: ExportMap.h:277
const std::string Name() const
Return the name of this variable.
Definition: ExportMap.h:68
IntegerVariable * GetIntegerVar(const std::string &name)
Lookup or create an IntegerVariable.
Definition: ExportMap.cpp:51
Definition: ExportMap.h:150
const std::string Value() const
Return the value of the variable as a string.
Definition: ExportMap.h:162
Various string utility functions.
A boolean variable.
Definition: ExportMap.h:92
StringVariable * GetStringVar(const std::string &name)
Lookup or create a StringVariable.
Definition: ExportMap.cpp:59
Helper macros.
virtual const std::string Value() const =0
Return the value of the variable as a string.
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
CounterVariable * GetCounterVar(const std::string &name)
Lookup or create a CounterVariable.
Definition: ExportMap.cpp:55
virtual ~BaseVariable()
Definition: ExportMap.h:62
const std::string Value() const
Get the value of this variable as a string.
Definition: ExportMap.h:121
const std::string Value() const
Return the value of the variable as a string.
Definition: ExportMap.h:291
BoolVariable * GetBoolVar(const std::string &name)
Lookup or create a BoolVariable.
Definition: ExportMap.cpp:47
void Escape(string *original)
Escape a string with \ .
Definition: StringUtils.cpp:249
Definition: ExportMap.h:80