Open Lighting Architecture
 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * ExportMap.h
17  * Interface the ExportMap and ExportedVariables
18  * Copyright (C) 2005-2008 Simon Newton
19  */
20 
30 #ifndef INCLUDE_OLA_EXPORTMAP_H_
31 #define INCLUDE_OLA_EXPORTMAP_H_
32 
33 #include <stdlib.h>
34 #include <functional>
35 #include <map>
36 #include <sstream>
37 #include <string>
38 #include <vector>
39 
40 namespace ola {
41 
42 using std::string;
43 using std::stringstream;
44 using std::map;
45 using std::vector;
46 
53 class BaseVariable {
54  public:
59  explicit BaseVariable(const string &name): m_name(name) {}
60 
64  virtual ~BaseVariable() {}
65 
70  const string Name() const { return m_name; }
71 
76  virtual const string Value() const = 0;
77 
78  private:
79  string m_name;
80 };
81 
82 struct VariableLessThan: public std::binary_function<BaseVariable*,
83  BaseVariable*, bool> {
84  bool operator()(BaseVariable *x, BaseVariable *y) {
85  return x->Name() < y->Name();
86  }
87 };
88 
89 
94 class BoolVariable: public BaseVariable {
95  public:
100  explicit BoolVariable(const string &name)
101  : BaseVariable(name),
102  m_value(false) {}
103  ~BoolVariable() {}
104 
109  void Set(bool value) { m_value = value; }
110 
115  bool Get() const { return m_value; }
116 
123  const string Value() const { return m_value ? "1" : "0"; }
124 
125  private:
126  bool m_value;
127 };
128 
129 
130 /*
131  * Represents a string variable
132  */
134  public:
135  explicit StringVariable(const string &name)
136  : BaseVariable(name),
137  m_value("") {}
138  ~StringVariable() {}
139 
140  void Set(const string &value) { m_value = value; }
141  const string Get() const { return m_value; }
142  const string Value() const { return m_value; }
143 
144  private:
145  string m_value;
146 };
147 
148 
149 /*
150  * Represents a integer variable
151  */
153  public:
154  explicit IntegerVariable(const string &name)
155  : BaseVariable(name),
156  m_value(0) {}
157  ~IntegerVariable() {}
158 
159  void Set(int value) { m_value = value; }
160  void operator++(int) { m_value++; }
161  void operator--(int) { m_value--; }
162  void Reset() { m_value = 0; }
163  int Get() const { return m_value; }
164  const string Value() const {
165  stringstream out;
166  out << m_value;
167  return out.str();
168  }
169 
170  private:
171  int m_value;
172 };
173 
174 
175 /*
176  * Represents a counter which can only be added to.
177  */
179  public:
180  explicit CounterVariable(const string &name)
181  : BaseVariable(name),
182  m_value(0) {}
183  ~CounterVariable() {}
184 
185  void operator++(int) { m_value++; }
186  void operator+=(unsigned int value) { m_value += value; }
187  void Reset() { m_value = 0; }
188  unsigned int Get() const { return m_value; }
189  const string Value() const {
190  stringstream out;
191  out << m_value;
192  return out.str();
193  }
194 
195  private:
196  unsigned int m_value;
197 };
198 
199 
200 /*
201  * A Map variable holds string -> type mappings
202  */
203 template<typename Type>
204 class MapVariable: public BaseVariable {
205  public:
206  MapVariable(const string &name, const string &label):
207  BaseVariable(name),
208  m_label(label) {}
209  ~MapVariable() {}
210 
211  void Remove(const string &key);
212  void Set(const string &key, Type value);
213  Type &operator[](const string &key);
214  const string Value() const;
215  const string Label() const { return m_label; }
216 
217  protected:
218  map<string, Type> m_variables;
219 
220  private:
221  string m_label;
222 };
223 
225 
226 
230 class IntMap: public MapVariable<int> {
231  public:
232  IntMap(const string &name, const string &label)
233  : MapVariable<int>(name, label) {
234  }
235 
236  void Increment(const string &key) {
237  m_variables[key]++;
238  }
239 };
240 
241 
245 class UIntMap: public MapVariable<unsigned int> {
246  public:
247  UIntMap(const string &name, const string &label)
248  : MapVariable<unsigned int>(name, label) {
249  }
250 
251  void Increment(const string &key) {
252  m_variables[key]++;
253  }
254 };
255 
256 
257 /*
258  * Return a value from the Map Variable, this will create an entry in the map
259  * if the variable doesn't exist.
260  */
261 template<typename Type>
262 Type &MapVariable<Type>::operator[](const string &key) {
263  return m_variables[key];
264 }
265 
266 
267 /*
268  * Set a value in the Map variable.
269  */
270 template<typename Type>
271 void MapVariable<Type>::Set(const string &key, Type value) {
272  m_variables[key] = value;
273 }
274 
275 
280 template<typename Type>
281 void MapVariable<Type>::Remove(const string &key) {
282  typename map<string, Type>::iterator iter = m_variables.find(key);
283 
284  if (iter != m_variables.end())
285  m_variables.erase(iter);
286 }
287 
288 
293 class ExportMap {
294  public:
295  ExportMap() {}
296  ~ExportMap();
297 
306  BoolVariable *GetBoolVar(const string &name);
307 
316  IntegerVariable *GetIntegerVar(const string &name);
317 
326  CounterVariable *GetCounterVar(const string &name);
327 
336  StringVariable *GetStringVar(const string &name);
337 
338  StringMap *GetStringMapVar(const string &name, const string &label="");
339  IntMap *GetIntMapVar(const string &name, const string &label="");
340  UIntMap *GetUIntMapVar(const string &name, const string &label="");
341 
346  vector<BaseVariable*> AllVariables() const;
347 
348  private :
349  ExportMap(const ExportMap&);
350  ExportMap& operator=(const ExportMap&);
351 
352  template<typename Type>
353  Type *GetVar(map<string, Type*> *var_map, const string &name);
354 
355  template<typename Type>
356  Type *GetMapVar(map<string, Type*> *var_map,
357  const string &name,
358  const string &label);
359 
360  map<string, BoolVariable*> m_bool_variables;
361  map<string, CounterVariable*> m_counter_variables;
362  map<string, IntegerVariable*> m_int_variables;
363  map<string, StringVariable*> m_string_variables;
364 
365  map<string, StringMap*> m_str_map_variables;
366  map<string, IntMap*> m_int_map_variables;
367  map<string, UIntMap*> m_uint_map_variables;
368 };
369 } // namespace ola
370 #endif // INCLUDE_OLA_EXPORTMAP_H_