GetSet  .9
A c++ library for load/saving typed and named properties and automatic GUI.
GetSet.hxx
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 __GetSet_h
21 #define __GetSet_h
22 
23 #include "GetSetDictionary.h"
24 
26 template <typename BasicType>
27 class GetSet
28 {
29 protected:
30  std::string section;
31  std::string key;
33 
35 
36  void create()
37  {
38  using namespace GetSetInternal;
40  if (!p) {
43  }
44  }
45 
46 public:
47  GetSet(GetSetDictionary& d, const std::string& s, const std::string k)
48  : dict(d), section(s), key(k) { create(); }
49 
50  GetSet(const std::string& s, const std::string k)
51  : dict(GetSetDictionary::globalDictionary()), section(s), key(k) { create(); }
52 
54  void operator=(const BasicType& v)
55  {
56  using namespace GetSetInternal;
58  GetSetData<BasicType>* sameType=dynamic_cast<GetSetData<BasicType>*>(p);
59  if (sameType)
60  {
61  sameType->value=v;
63  }
64  else
65  p->setString(toString(v));
66  }
67 
69  operator BasicType() const
70  {
71  using namespace GetSetInternal;
73  GetSetData<BasicType>* sameType=dynamic_cast<GetSetData<BasicType>*>(p);
74  if (sameType)
75  return sameType->value;
76  else
77  return stringTo<BasicType>(p->getString());
78  }
79 
81  std::string getString() const
82  {
84  }
85 
87  void setString(const std::string& value)
88  {
91  }
92 
93 };
94 
95 
96 //
97 // Special GetSet types: Slider Enum Trigger StaticText Directory File
98 //
99 
101 #define GETSET_SPECIALIZATION(SPECIAL_TYPE,BASE_TYPE,CLASS_BODY) \
102  namespace GetSetInternal { \
103  class GetSetData##SPECIAL_TYPE : public GetSetData<BASE_TYPE> { \
104  virtual std::string getType() const { return #SPECIAL_TYPE; } \
105  }; \
106  } \
107  namespace GetSetGui { \
108  class SPECIAL_TYPE : public GetSet<BASE_TYPE> { \
109  public: \
110  SPECIAL_TYPE(GetSetDictionary& d, const std::string& s, const std::string k) \
111  : GetSet<BASE_TYPE>(d) { section=s; key=k; forceType(); } \
112  SPECIAL_TYPE(const std::string& s, const std::string k) \
113  : GetSet<BASE_TYPE>(GetSetDictionary::globalDictionary()){ section=s; key=k; forceType(); } \
114  void operator=(const BASE_TYPE& v) { setString(toString(v)); } \
115  operator BASE_TYPE() const { return stringTo<BASE_TYPE>(getString()); } \
116  CLASS_BODY \
117  protected: \
118  void forceType() { \
119  GetSetInternal::GetSetDataInterface* p=dict.getDatainterface(section,key); \
120  if (p && ! dynamic_cast<GetSetInternal::GetSetData##SPECIAL_TYPE*>(p)) { delete p; p=0x0; } \
121  if (!p) { \
122  dict.get()[section][key]=new GetSetInternal::GetSetData##SPECIAL_TYPE(); \
123  dict.signalCreate(section,key); \
124  } \
125  } \
126  }; \
127  }
128 
129 #define GETSET_TAG(TYPE,TAG) \
130  void set##TAG(const TYPE& value) { dict.get()[section][key]->attributes[#TAG]=toString(value); } \
131  TYPE get##TAG() const {return stringTo<TYPE>(dict.get()[section][key]->attributes[#TAG]);}
132 
134 GETSET_SPECIALIZATION(Slider,double, GETSET_TAG(double, Min) GETSET_TAG(double, Max) )
135 
136 
137 GETSET_SPECIALIZATION(Enum,int, GETSET_TAG(std::string, Choices) )
138 
141 
144 
147 
150 
152 GETSET_SPECIALIZATION(File,std::string, GETSET_TAG(std::string,Extensions) GETSET_TAG(bool, CreateNew) GETSET_TAG(bool, Multiple) );
153 
154 #undef GETSET_SPECIALIZATION
155 #undef GETSET_TAG
156 
157 #endif // __GetSet_h