OpenStructure
Loading...
Searching...
No Matches
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-2020 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
41namespace ost {
42
43struct DLLEXPORT GenericPropError: public Error
44{
46 Error(m)
47 {}
48};
49
50typedef boost::variant<String, Real, int, bool, geom::Vec3> GenericPropValue;
51
53class TEMPLATE_EXPORT GenericPropContainerImpl
54{
55 typedef std::map<String,GenericPropValue> PropertyMap;
56
57public:
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
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
143private:
144 mutable PropertyMap* map_;
145};
146
147template <typename H>
148class TEMPLATE_EXPORT ConstGenericPropContainer {
149protected:
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
179public:
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
336template <typename H>
337class TEMPLATE_EXPORT GenericPropContainer :
339{
340public:
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
Three dimensional vector class, using Real precision.
Definition vec3.hh:48
String GetPropAsString(const String &key) const
returns a String representation of stored value
bool HasProp(const String &key) const
checks existence of property
String GetStringProp(const String &key) const
returns String property, raises an exception if it does not exist
int GetIntProp(const String &key) const
returns integer property, raises an exception if it does not exist
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
std::map< String, GenericPropValue > GetPropMap() const
std::vector< String > GetPropList() const
bool GetBoolProp(const String &key) const
returns boolean property, raises an exception if it does not exist
const GenericPropContainerImpl * GetImpl() const
String GetStringProp(const String &key, const String &def) const
returns String property, or the given default if it does not exist
Real GetFloatProp(const String &key, Real def) const
returns floating point property, or the given default if it does not exist
T gp_get(const String &key, const T &def) const
int GetIntProp(const String &key, int def) const
returns integer property, or the given default if it does not exist
GenericPropContainerImpl * GetImpl()
bool GetBoolProp(const String &key, bool def) const
returns boolean property, or the given default if it does not exist
T gp_get(const String &key) const
base class for the handler classes
void SetBoolProp(const String &key, bool value)
\ brief sets boolean property
void SetVec3Prop(const String &key, geom::Vec3 value)
\ brief sets Vec3 property
void SetStringProp(const String &key, const String &value)
sets String property
void SetFloatProp(const String &key, Real value)
sets floating point property
void SetIntProp(const String &key, int value)
sets integer property
void RemoveProp(const String &key)
base class for the implementation
GenericPropContainerImpl(const GenericPropContainerImpl &rhs)
bool HasProp(const String &key) const
GenericPropValue & GenericProp(const String &key)
const GenericPropValue & GenericProp(const String &key) const
void Assign(const GenericPropContainerImpl &impl)
std::vector< String > GetPropList() const
void RemoveProp(const String &key)
GenericPropContainerImpl & operator=(const GenericPropContainerImpl &r)
float Real
Definition base.hh:44
std::string String
Definition base.hh:54
Definition base.dox:1
boost::variant< String, Real, int, bool, geom::Vec3 > GenericPropValue
void CheckHandleValidity(const H &handle)
GenericPropError(const String &m)