Open Lighting Architecture  0.9.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 <stdlib.h>
35 
36 #include <functional>
37 #include <map>
38 #include <sstream>
39 #include <string>
40 #include <vector>
41 
42 namespace ola {
43 
50 class BaseVariable {
51  public:
56  explicit BaseVariable(const std::string &name): m_name(name) {}
57 
61  virtual ~BaseVariable() {}
62 
67  const std::string Name() const { return m_name; }
68 
73  virtual const std::string Value() const = 0;
74 
75  private:
76  std::string m_name;
77 };
78 
79 struct VariableLessThan: public std::binary_function<BaseVariable*,
80  BaseVariable*, bool> {
81  bool operator()(BaseVariable *x, BaseVariable *y) {
82  return x->Name() < y->Name();
83  }
84 };
85 
86 
91 class BoolVariable: public BaseVariable {
92  public:
97  explicit BoolVariable(const std::string &name)
98  : BaseVariable(name),
99  m_value(false) {}
100  ~BoolVariable() {}
101 
106  void Set(bool value) { m_value = value; }
107 
112  bool Get() const { return m_value; }
113 
120  const std::string Value() const { return m_value ? "1" : "0"; }
121 
122  private:
123  bool m_value;
124 };
125 
126 
127 /*
128  * Represents a string variable
129  */
131  public:
132  explicit StringVariable(const std::string &name)
133  : BaseVariable(name),
134  m_value("") {}
135  ~StringVariable() {}
136 
137  void Set(const std::string &value) { m_value = value; }
138  const std::string Get() const { return m_value; }
139  const std::string Value() const { return m_value; }
140 
141  private:
142  std::string m_value;
143 };
144 
145 
146 /*
147  * Represents a integer variable
148  */
150  public:
151  explicit IntegerVariable(const std::string &name)
152  : BaseVariable(name),
153  m_value(0) {}
154  ~IntegerVariable() {}
155 
156  void Set(int value) { m_value = value; }
157  void operator++(int) { m_value++; }
158  void operator--(int) { m_value--; }
159  void Reset() { m_value = 0; }
160  int Get() const { return m_value; }
161  const std::string Value() const {
162  std::ostringstream out;
163  out << m_value;
164  return out.str();
165  }
166 
167  private:
168  int m_value;
169 };
170 
171 
172 /*
173  * Represents a counter which can only be added to.
174  */
176  public:
177  explicit CounterVariable(const std::string &name)
178  : BaseVariable(name),
179  m_value(0) {}
180  ~CounterVariable() {}
181 
182  void operator++(int) { m_value++; }
183  void operator+=(unsigned int value) { m_value += value; }
184  void Reset() { m_value = 0; }
185  unsigned int Get() const { return m_value; }
186  const std::string Value() const {
187  std::ostringstream out;
188  out << m_value;
189  return out.str();
190  }
191 
192  private:
193  unsigned int m_value;
194 };
195 
196 
197 /*
198  * A Map variable holds string -> type mappings
199  */
200 template<typename Type>
201 class MapVariable: public BaseVariable {
202  public:
203  MapVariable(const std::string &name, const std::string &label)
204  : BaseVariable(name),
205  m_label(label) {}
206  ~MapVariable() {}
207 
208  void Remove(const std::string &key);
209  void Set(const std::string &key, Type value);
210  Type &operator[](const std::string &key);
211  const std::string Value() const;
212  const std::string Label() const { return m_label; }
213 
214  protected:
215  std::map<std::string, Type> m_variables;
216 
217  private:
218  std::string m_label;
219 };
220 
222 
223 
227 class IntMap: public MapVariable<int> {
228  public:
229  IntMap(const std::string &name, const std::string &label)
230  : MapVariable<int>(name, label) {}
231 
232  void Increment(const std::string &key) {
233  m_variables[key]++;
234  }
235 };
236 
237 
241 class UIntMap: public MapVariable<unsigned int> {
242  public:
243  UIntMap(const std::string &name, const std::string &label)
244  : MapVariable<unsigned int>(name, label) {}
245 
246  void Increment(const std::string &key) {
247  m_variables[key]++;
248  }
249 };
250 
251 
252 /*
253  * Return a value from the Map Variable, this will create an entry in the map
254  * if the variable doesn't exist.
255  */
256 template<typename Type>
257 Type &MapVariable<Type>::operator[](const std::string &key) {
258  return m_variables[key];
259 }
260 
261 
262 /*
263  * Set a value in the Map variable.
264  */
265 template<typename Type>
266 void MapVariable<Type>::Set(const std::string &key, Type value) {
267  m_variables[key] = value;
268 }
269 
270 
275 template<typename Type>
276 void MapVariable<Type>::Remove(const std::string &key) {
277  typename std::map<std::string, Type>::iterator iter = m_variables.find(key);
278 
279  if (iter != m_variables.end())
280  m_variables.erase(iter);
281 }
282 
283 
288 class ExportMap {
289  public:
290  ExportMap() {}
291  ~ExportMap();
292 
301  BoolVariable *GetBoolVar(const std::string &name);
302 
311  IntegerVariable *GetIntegerVar(const std::string &name);
312 
321  CounterVariable *GetCounterVar(const std::string &name);
322 
331  StringVariable *GetStringVar(const std::string &name);
332 
333  StringMap *GetStringMapVar(const std::string &name,
334  const std::string &label="");
335  IntMap *GetIntMapVar(const std::string &name, const std::string &label="");
336  UIntMap *GetUIntMapVar(const std::string &name,
337  const std::string &label="");
338 
343  std::vector<BaseVariable*> AllVariables() const;
344 
345  private :
346  template<typename Type>
347  Type *GetVar(std::map<std::string, Type*> *var_map,
348  const std::string &name);
349 
350  template<typename Type>
351  Type *GetMapVar(std::map<std::string, Type*> *var_map,
352  const std::string &name,
353  const std::string &label);
354 
355  std::map<std::string, BoolVariable*> m_bool_variables;
356  std::map<std::string, CounterVariable*> m_counter_variables;
357  std::map<std::string, IntegerVariable*> m_int_variables;
358  std::map<std::string, StringVariable*> m_string_variables;
359 
360  std::map<std::string, StringMap*> m_str_map_variables;
361  std::map<std::string, IntMap*> m_int_map_variables;
362  std::map<std::string, UIntMap*> m_uint_map_variables;
363 
364  DISALLOW_COPY_AND_ASSIGN(ExportMap);
365 };
366 } // namespace ola
367 #endif // INCLUDE_OLA_EXPORTMAP_H_