OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
generic_property.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the OpenStructure project <www.openstructure.org>
3 //
4 // Copyright (C) 2008-2011 by the OpenStructure authors
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 3.0 of the License, or (at your option)
9 // any later version.
10 // This library is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 //------------------------------------------------------------------------------
19 #ifndef OST_GENETRIC_PROPERTY_HH
20 #define OST_GENETRIC_PROPERTY_HH
21 
22 /*
23  usage:
24 
25  the impl is derived from GenericPropContainerImpl
26 
27  the handle is derived from GenericPropContainer, and then has the
28  setter and getter methods for String, Real, int and bool mapping
29 */
30 
31 #include <sstream>
32 #include <map>
33 #include <vector>
34 #include <boost/variant.hpp>
35 
36 #include <ost/module_config.hh>
37 #include <ost/invalid_handle.hh>
38 #include <ost/message.hh>
39 #include <ost/geom/vec3.hh>
40 
41 namespace ost {
42 
43 struct DLLEXPORT GenericPropError: public Error
44 {
46  Error(m)
47  {}
48 };
49 
50 typedef boost::variant<String, Real, int, bool, geom::Vec3> GenericPropValue;
51 
53 class TEMPLATE_EXPORT GenericPropContainerImpl
54 {
55  typedef std::map<String,GenericPropValue> PropertyMap;
56 
57 public:
58  GenericPropContainerImpl(): map_(NULL) {}
60  {
61  if (map_) {
62  delete map_;
63  }
64  }
66  map_(rhs.map_ ? new PropertyMap(*rhs.map_) : NULL)
67  { }
68 
70  {
71  this->Assign(r);
72  return *this;
73  }
74 
76  {
77  if (!map_) {
78  map_=new PropertyMap;
79  }
80  return (*map_)[key];
81  }
82 
83  const GenericPropValue& GenericProp(const String& key) const
84  {
85  if (!map_) {
86  map_=new PropertyMap;
87  }
88  return (*map_)[key];
89  }
90 
91  bool HasProp(const String& key) const
92  {
93  return map_ && map_->find(key) != map_->end();
94  }
95 
96  void ClearProps()
97  {
98  if (map_) {
99  map_->clear();
100  }
101  }
102 
103  void RemoveProp(const String& key)
104  {
105  if (map_) {
106  map_->erase(key);
107  }
108  }
109 
111  {
112  if (impl.map_) {
113  if (!map_) {
114  map_=new PropertyMap(*impl.map_);
115  } else {
116  *map_=*impl.map_;
117  }
118  } else {
119  this->ClearProps();
120  }
121  }
122 
123  PropertyMap GetPropMap() const
124  {
125  if (!map_) {
126  map_=new PropertyMap;
127  }
128  return *map_;
129  }
130 
131  std::vector<String> GetPropList() const
132  {
133  std::vector<String> prop_list;
134  if (map_) {
135  PropertyMap::const_iterator i;
136  for (i=map_->begin(); i!=map_->end(); ++i) {
137  prop_list.push_back(i->first);
138  }
139  }
140  return prop_list;
141  }
142 
143 private:
144  mutable PropertyMap* map_;
145 };
146 
147 template <typename H>
148 class TEMPLATE_EXPORT ConstGenericPropContainer {
149 protected:
150 
151  template<typename T>
152  T gp_get(const String& key) const {
153  if(HasProp(key)) {
154  return boost::get<T>(GetImpl()->GenericProp(key));
155  } else {
156  std::ostringstream m("");
157  m << "unknown property " << key;
158  throw GenericPropError(m.str());
159  }
160  }
161 
162  template<typename T>
163  T gp_get(const String& key, const T& def) const {
164  if(HasProp(key)) {
165  return boost::get<T>(GetImpl()->GenericProp(key));
166  }
167  return def;
168  }
170  {
171  return static_cast<H*>(this)->GpImpl();
172  }
173 
175  {
176  return static_cast<const H*>(this)->GpImpl();
177  }
178 
179 public:
181  bool HasProp(const String& key) const {
182  CheckHandleValidity(*static_cast<const H*>(this));
183  return this->GetImpl()->HasProp(key);
184  }
185 
193  String GetPropAsString(const String& key) const
194  {
195  CheckHandleValidity(*static_cast<const H*>(this));
196  if(!HasProp(key)) return "";
197  std::ostringstream rep("");
198  rep << this->GetImpl()->GenericProp(key);
199  return rep.str();
200  }
201 
203  String GetStringProp(const String& key) const
204  {
205  CheckHandleValidity(*static_cast<const H*>(this));
206  return this->gp_get<String>(key);
207  }
208 
211  Real GetFloatProp(const String& key) const
212  {
213  CheckHandleValidity(*static_cast<const H*>(this));
214  if(HasProp(key)) {
215  GenericPropValue value=this->GetImpl()->GenericProp(key);
216  switch (value.which()) {
217  case 1:
218  return boost::get<Real>(value);
219  case 2:
220  return static_cast<Real>(boost::get<int>(value));
221  case 3:
222  return static_cast<Real>(boost::get<bool>(value));
223  }
224  std::ostringstream m("");
225  m << "property '" << key << "' is not numeric";
226  throw GenericPropError(m.str());
227  }
228  std::ostringstream m("");
229  m << "unknown property " << key;
230  throw GenericPropError(m.str());
231  }
233  int GetIntProp(const String& key) const
234  {
235  CheckHandleValidity(*static_cast<const H*>(this));
236  if (HasProp(key)){
237  GenericPropValue value=this->GetImpl()->GenericProp(key);
238  switch (value.which()) {
239  case 2:
240  return boost::get<int>(value);
241  case 3:
242  return boost::get<bool>(value);
243  }
244  std::ostringstream m("");
245  m << "property '" << key << "' is not integral";
246  throw GenericPropError(m.str());
247  }
248  std::ostringstream m("");
249  m << "unknown property " << key;
250  throw GenericPropError(m.str());
251  }
252 
254  bool GetBoolProp(const String& key) const
255  {
256  CheckHandleValidity(*static_cast<const H*>(this));
257  return this->gp_get<bool>(key);
258  }
259 
261  geom::Vec3 GetVec3Prop(const String& key) const
262  {
263  CheckHandleValidity(*static_cast<const H*>(this));
264  return this->gp_get<geom::Vec3>(key);
265  }
266 
268  String GetStringProp(const String& key, const String& def) const
269  {
270  CheckHandleValidity(*static_cast<const H*>(this));
271  return this->gp_get<String>(key,def);
272  }
273 
276  Real GetFloatProp(const String& key, Real def) const
277  {
278  CheckHandleValidity(*static_cast<const H*>(this));
279  if(this->HasProp(key)) {
280  GenericPropValue value=GetImpl()->GenericProp(key);
281  switch (value.which()) {
282  case 1:
283  return boost::get<Real>(value);
284  case 2:
285  return static_cast<Real>(boost::get<int>(value));
286  case 3:
287  return static_cast<Real>(boost::get<bool>(value));
288  }
289  std::ostringstream m("");
290  m << "property '" << key << "' is not numeric";
291  throw GenericPropError(m.str());
292  }
293  return def;
294  }
295 
297  int GetIntProp(const String& key, int def) const
298  {
299  CheckHandleValidity(*static_cast<const H*>(this));
300  if(this->HasProp(key)) {
301  GenericPropValue value=GetImpl()->GenericProp(key);
302  switch (value.which()) {
303  case 2:
304  return boost::get<int>(value);
305  case 3:
306  return static_cast<int>(boost::get<bool>(value));
307  }
308  std::ostringstream m("");
309  m << "property '" << key << "' is not integral";
310  throw GenericPropError(m.str());
311  }
312  return def;
313  }
314 
316  bool GetBoolProp(const String& key, bool def) const
317  {
318  CheckHandleValidity(*static_cast<const H*>(this));
319  return this->gp_get<bool>(key, def);
320  }
321 
322  std::map<String,GenericPropValue> GetPropMap() const
323  {
324  CheckHandleValidity(*static_cast<const H*>(this));
325  return this->GetImpl()->GetPropMap();
326  }
327 
328  std::vector<String> GetPropList() const
329  {
330  CheckHandleValidity(*static_cast<const H*>(this));
331  return this->GetImpl()->GetPropList();
332  }
333 };
334 
336 template <typename H>
337 class TEMPLATE_EXPORT GenericPropContainer :
338  public ConstGenericPropContainer<H>
339 {
340 public:
341  void ClearProps()
342  {
343  CheckHandleValidity(*static_cast<const H*>(this));
344  this->GetImpl()->ClearProps();
345  }
346 
348  void SetStringProp(const String& key, const String& value)
349  {
350  CheckHandleValidity(*static_cast<const H*>(this));
351  this->GetImpl()->GenericProp(key)=value;
352  }
353 
355  void SetFloatProp(const String& key, Real value)
356  {
357  CheckHandleValidity(*static_cast<const H*>(this));
358  this->GetImpl()->GenericProp(key)=value;
359  }
360 
362  void SetIntProp(const String& key, int value)
363  {
364  CheckHandleValidity(*static_cast<const H*>(this));
365  this->GetImpl()->GenericProp(key)=value;
366  }
367 
369  void SetBoolProp(const String& key, bool value)
370  {
371  CheckHandleValidity(*static_cast<const H*>(this));
372  this->GetImpl()->GenericProp(key)=value;
373  }
374 
376  void SetVec3Prop(const String& key, geom::Vec3 value)
377  {
378  CheckHandleValidity(*static_cast<const H*>(this));
379  this->GetImpl()->GenericProp(key)=value;
380  }
381 
382  void RemoveProp(const String& key)
383  {
384  CheckHandleValidity(*static_cast<const H*>(this));
385  this->GetImpl()->RemoveProp(key);
386  }
387 };
388 
389 
390 } // ns
391 
392 #endif
int GetIntProp(const String &key) const
returns integer property, raises an exception if it does not exist
const GenericPropContainerImpl * GetImpl() const
void RemoveProp(const String &key)
void CheckHandleValidity(const H &handle)
void SetBoolProp(const String &key, bool value)
\ brief sets boolean property
std::string String
Definition: base.hh:54
int GetIntProp(const String &key, int def) const
returns integer property, or the given default if it does not exist
float Real
Definition: base.hh:44
GenericPropContainerImpl(const GenericPropContainerImpl &rhs)
bool HasProp(const String &key) const
checks existence of property
void Assign(const GenericPropContainerImpl &impl)
void SetVec3Prop(const String &key, geom::Vec3 value)
\ brief sets Vec3 property
void RemoveProp(const String &key)
void SetIntProp(const String &key, int value)
sets integer property
T gp_get(const String &key) const
void SetFloatProp(const String &key, Real value)
sets floating point property
bool GetBoolProp(const String &key) const
returns boolean property, raises an exception if it does not exist
String GetStringProp(const String &key) const
returns String property, raises an exception if it does not exist
std::vector< String > GetPropList() const
void SetStringProp(const String &key, const String &value)
sets String property
std::vector< String > GetPropList() const
Real GetFloatProp(const String &key, Real def) const
returns floating point property, or the given default if it does not exist
GenericPropError(const String &m)
Three dimensional vector class, using Real precision.
Definition: vec3.hh:42
GenericPropValue & GenericProp(const String &key)
PropertyMap GetPropMap() const
String GetStringProp(const String &key, const String &def) const
returns String property, or the given default if it does not exist
bool HasProp(const String &key) const
const GenericPropValue & GenericProp(const String &key) const
std::map< String, String > PropertyMap
Definition: settings.hh:93
Real GetFloatProp(const String &key) const
returns floating point property, raises an exception if it does not exist
geom::Vec3 GetVec3Prop(const String &key) const
returns Vec3 property, raises an exception if it does not exist
T gp_get(const String &key, const T &def) const
base class for the implementation
GenericPropContainerImpl & operator=(const GenericPropContainerImpl &r)
base class for the handler classes
std::map< String, GenericPropValue > GetPropMap() const
GenericPropContainerImpl * GetImpl()
String GetPropAsString(const String &key) const
returns a String representation of stored value
bool GetBoolProp(const String &key, bool def) const
returns boolean property, or the given default if it does not exist
boost::variant< String, Real, int, bool, geom::Vec3 > GenericPropValue