GetSet  .9
A c++ library for load/saving typed and named properties and automatic GUI.
GetSetDictionary.h
Go to the documentation of this file.
1 //
2 // Library: GetSet
3 // c++ library for load/saving *typed* and *named* properties and automatic GUI.
4 //
5 // Copyright (c) by André Aichert (aaichert@gmail.com)
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 
20 #ifndef __GetSetDictionary_h
21 #define __GetSetDictionary_h
22 
23 #include <string>
24 #include <vector>
25 #include <map>
26 #include <set>
27 
28 #include "StringUtil.hxx"
29 
30 namespace GetSetInternal {
31 
34  public:
35  virtual std::string getType() const = 0;
36  virtual void setString(const std::string& new_value) = 0;
37  virtual std::string getString() const = 0;
38  std::map<std::string,std::string> attributes;
39  };
40 
42  template <typename T>
44  {
45  public:
46  T value;
47 
48  virtual std::string getType() const {
49  return getTypeName<T>();
50  }
51 
52  virtual void setString(const std::string& new_value) {
53  value=stringTo<T>(new_value);
54  }
55 
56  virtual std::string getString() const {
57  return toString(value);
58  }
59  };
60 
62  template <typename T> inline std::string getTypeName()
63  {
64  return typeid(T).name();
65  }
66 
67  // specializations
68  #define DEFINE_TYPE_NAME_STRING(X) template<> inline std::string getTypeName<X>() {return #X;}
75  DEFINE_TYPE_NAME_STRING(std::vector<std::string>)
76  DEFINE_TYPE_NAME_STRING(std::vector<double>)
77  DEFINE_TYPE_NAME_STRING(std::vector<int>)
78 
79 } // GetSetInternal
80 
81 namespace tinyxml2 {
82  class XMLElement;
83 } // namespace tinyxml2
84 
87 {
88 public:
89  typedef std::map<std::string, GetSetInternal::GetSetDataInterface*> Section;
90  typedef std::map<std::string, Section> Dictionary;
91 
92  static GetSetDictionary& globalDictionary();
93 
95 
97  void parseXML(const std::string& xml);
99  std::string getXML() const;
100 
102  void parseIni(const std::string& ini);
104  std::string getIni() const;
105 
107  void erase(const std::string& section, const std::string& key);
108 
110  void clear(const std::string& section);
111 
112  //
113  // The folling functions have to be public (design flaw) but should not be used directly.
114  //
115 
117  GetSetInternal::GetSetDataInterface* getDatainterface(const std::string& section, const std::string& key) const;
118 
120  Dictionary& get();
121 
123  void signalChange(const std::string& section,const std::string& key);
125  void signalCreate(const std::string& section,const std::string& key);
127  void signalDestroy(const std::string& section,const std::string& key="");
128 
130  class Observer {
131  protected:
133  Observer(GetSetDictionary& subject);
134  ~Observer();
135  virtual void notifyChange(const std::string& section,const std::string& key)=0x0;
136  virtual void notifyCreate(const std::string& section,const std::string& key) {};
137  virtual void notifyDestroy(const std::string& section,const std::string& key) {};
138  friend class GetSetDictionary;
139  };
140 
141 protected:
143  std::set<Observer*> registered_observers;
144 
146 
147  void loadSection(tinyxml2::XMLElement* node, const std::string& section="");
148 };
149 
152 {
153 public:
154  GetSetHandler(void (*change)(const std::string&,const std::string&));
155  GetSetHandler(GetSetDictionary& subject, void (*change)(const std::string&,const std::string&));
156 protected:
157  void (*change_handler)(const std::string&,const std::string&);
158  virtual void notifyChange(const std::string& s,const std::string& k);
159 };
160 
161 #endif // __GetSetDictionary_h