RTXI  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
settings.h
Go to the documentation of this file.
1 /*
2  The Real-Time eXperiment Interface (RTXI)
3  Copyright (C) 2011 Georgia Institute of Technology, University of Utah, Weill Cornell Medical College
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18 */
19 
20 #ifndef SETTINGS_H
21 #define SETTINGS_H
22 
23 #include <QString>
24 #include <debug.h>
25 #include <list>
26 #include <map>
27 #include <mutex.h>
28 #include <string>
29 
30 class QDomDocument;
31 class QDomElement;
32 
34 
37 namespace Settings
38 {
39 
43 class Object
44 {
45 
46  friend class Manager;
47 
48 public:
49 
53  typedef unsigned long ID;
57  const static ID INVALID = 0;
58 
62  class State
63  {
64 
65  friend class Manager;
66  friend class Object;
67 
68  public:
69 
70  State(void);
71  State(ID n);
72  ~State(void);
73 
77  double loadDouble(const std::string &name) const;
81  int loadInteger(const std::string &name) const;
85  std::string loadString(const std::string &name) const;
89  void saveDouble(const std::string &name,double);
93  void saveInteger(const std::string &name,int);
97  void saveString(const std::string &name,const std::string &value);
98 
102  State loadState(const std::string &name) const;
106  void saveState(const std::string &name,const State &value);
107 
111  QDomElement xml(QDomDocument &) const;
115  void xml(const QDomElement &);
116 
117  private:
118 
119  ID id;
120  std::map<std::string,std::string> paramMap;
121  std::map<std::string,State> stateMap;
122 
123  };
124 
125  Object(void);
126  virtual ~Object(void);
127 
131  ID getID(void) const
132  {
133  return id;
134  };
135 
139  State save(void) const;
143  void load(const State &);
147  void deferred(const State &);
148 
149 protected:
150 
154  virtual void doLoad(const State &) {};
158  virtual void doDeferred(const State &) {};
162  virtual void doSave(State &) const {};
163 
164 private:
165 
166  ID id;
167 
168 }; // class Object
169 
173 class Manager
174 {
175 
176  friend class Object;
177 
178 public:
179 
183  static Manager *getInstance(void);
184 
188  Object *getObject(Object::ID) const;
192  void foreachObject(void (*callback)(Object *,void *),void *param);
193 
197  int load(const std::string &);
201  int save(const std::string &);
202 
203 private:
204 
205  Manager(void) : mutex(Mutex::RECURSIVE), currentID(Object::INVALID+1) {};
206  ~Manager(void) {};
207  Manager(const Manager &) {};
209  {
210  return *getInstance();
211  };
212 
213  static Manager *instance;
214 
216  void releaseID(Object *);
217 
218  void insertObject(Object *);
219  void removeObject(Object *);
220 
221  mutable Mutex mutex;
223  std::list<Object *> objectList;
224  std::map<Object::ID,Object *> objectMap;
225 
226 }; // class Manager
227 
228 }; // namespace Settings
229 
230 #endif // SETTINGS_H
void insertObject(Object *)
Definition: settings.cpp:498
int load(const std::string &)
Definition: settings.cpp:230
std::map< Object::ID, Object * > objectMap
Definition: settings.h:224
void releaseID(Object *)
Definition: settings.cpp:486
virtual void doLoad(const State &)
Definition: settings.h:154
Manager & operator=(const Manager &)
Definition: settings.h:208
void removeObject(Object *)
Definition: settings.cpp:519
Manager(const Manager &)
Definition: settings.h:207
void deferred(const State &)
Definition: settings.cpp:207
std::map< std::string, State > stateMap
Definition: settings.h:121
std::list< Object * > objectList
Definition: settings.h:223
Definition: mutex.h:28
int save(const std::string &)
Definition: settings.cpp:358
unsigned long ID
Definition: settings.h:53
Object * getObject(Object::ID) const
Definition: settings.cpp:212
void load(const State &)
Definition: settings.cpp:195
virtual ~Object(void)
Definition: settings.cpp:50
void saveString(const std::string &name, const std::string &value)
Definition: settings.cpp:119
void foreachObject(void(*callback)(Object *, void *), void *param)
Definition: settings.cpp:223
static const ID INVALID
Definition: settings.h:57
int loadInteger(const std::string &name) const
Definition: settings.cpp:77
virtual void doDeferred(const State &)
Definition: settings.h:158
Object::ID currentID
Definition: settings.h:222
void saveDouble(const std::string &name, double)
Definition: settings.cpp:105
void saveState(const std::string &name, const State &value)
Definition: settings.cpp:136
std::map< std::string, std::string > paramMap
Definition: settings.h:120
void saveInteger(const std::string &name, int)
Definition: settings.cpp:112
ID getID(void) const
Definition: settings.h:131
QDomElement xml(QDomDocument &) const
Definition: settings.cpp:141
void acquireID(Object *, Object::ID=Object::INVALID)
Definition: settings.cpp:444
double loadDouble(const std::string &name) const
Definition: settings.cpp:61
State loadState(const std::string &name) const
Definition: settings.cpp:124
State save(void) const
Definition: settings.cpp:188
static Manager * getInstance(void)
Definition: settings.cpp:534
static Manager * instance
Definition: settings.h:211
std::string loadString(const std::string &name) const
Definition: settings.cpp:93
Objects for saving/loading setting information between sessions.
Definition: settings.h:37
virtual void doSave(State &) const
Definition: settings.h:162