AGX Dynamics  2.37.3.2
Property.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGX_PROPERTY_H
18 #define AGX_PROPERTY_H
19 
20 
21 #include <agx/agxPhysics_export.h>
22 #include <agx/Referenced.h>
23 #include <agx/ref_ptr.h>
24 
25 #include <agx/stdint.h>
26 #include <typeinfo>
27 #include <stdexcept>
28 #include <agxStream/Serializable.h>
29 #include <agx/Vec3.h>
30 #include <agx/Vec2.h>
31 
32 #define PROPERTY_THROW_IF_TRUE(X, MESSAGE) if (X) { agxThrow std::runtime_error(MESSAGE); }
33 #include <agx/HashTable.h>
34 #include <string>
35 #include <agx/HashSet.h>
36 
37 namespace agx {
38 
39 #define PROPERTY_TYPE(type, Type) \
40 public: \
41  \
42  typedef agx::HashTable<std::string,type > Type ## Map;\
43  \
44 protected: \
45  \
46  Type ## Map m_ ## Type ## _map;\
47  \
48 public:\
49  \
50 \
54  Type ## Map& getPropertyMap ## Type() { return m_ ## Type ## _map; };\
55  \
56 \
61  size_t getNumberOfProperties ## Type() const{ return m_ ## Type ## _map.size(); };\
62  \
63  void registerPropertyType ## Type()\
64  {\
65  m_types.insert( #Type );\
66 };\
67  \
68 \
73  type& addProperty ## Type( const std::string& name,\
74  type value = type() )\
75  {\
76  m_ ## Type ## _map[name] = value;\
77  return m_ ## Type ## _map[name];\
78 };\
79 \
84  bool removeProperty ## Type( const std::string& name)\
85  {\
86  Type ## Map::iterator p = m_ ## Type ## _map.find( name );\
87  if(p == m_ ## Type ## _map.end()) return false; else { m_ ## Type ## _map.erase(p); return true; } \
88 }\
89  \
90 \
95  type& setProperty ## Type( const std::string& name, type value)\
96  {\
97  Type ## Map::iterator p = m_ ## Type ## _map.find( name );\
98  PROPERTY_THROW_IF_TRUE( p == m_ ## Type ## _map.end(), "Property named "+name+" is missing" );\
99  p->second = value;\
100  return p->second;\
101 };\
102  \
103 \
107  bool hasProperty ## Type( const std::string& name) const \
108  {return m_ ## Type ## _map.find(name)!=m_ ## Type ## _map.end();};\
109  \
110 \
115  type getProperty ## Type( const std::string& name ) const \
116  {\
117  Type ## Map::const_iterator p = m_ ## Type ## _map.find( name );\
118  PROPERTY_THROW_IF_TRUE( p == m_ ## Type ## _map.end(), "Property " + name + " of type " + #Type + " is missing!" );\
119  return p->second;\
120 };\
121  type& getProperty ## Type( const std::string& name ) \
122 {\
123  Type ## Map::iterator p = m_ ## Type ## _map.find( name );\
124  PROPERTY_THROW_IF_TRUE( p == m_ ## Type ## _map.end(), "Property " + name + " of type " + #Type + " is missing!" );\
125  return p->second;\
126 };\
127 \
132  bool getProperty ## Type( const std::string& name, type & value) const \
133  { \
134  if (hasProperty ## Type( name )) { \
135  value = getProperty ## Type( name ); \
136  return true; \
137  } \
138  return false; \
139 }\
140  \
141  \
142  void copyProperties ## Type( const PropertyContainer* pc ) {\
143  if ( pc->m_ ## Type ## _map.size() == 0 ) return;\
144  for ( Type ## Map::const_iterator i = pc->m_ ## Type ## _map.begin();\
145  i != pc->m_ ## Type ## _map.end(); i++ ) {\
146  m_ ## Type ## _map[i->first] = i->second;\
147 }\
148 }\
149 
150 
151 
152 
159  {
160  public:
161 
163  registerPropertyTypeInt();
164  registerPropertyTypeFloat();
165  registerPropertyTypeDouble();
166  registerPropertyTypeReal();
167  registerPropertyTypeBool();
168  registerPropertyTypeString();
169  registerPropertyTypeLong();
170  registerPropertyTypeVec2();
171  registerPropertyTypeVec3();
172  };
173 
174  virtual ~PropertyContainer() {}
175 
176 
177  void copyProperties( const PropertyContainer* pc ) {
178  copyPropertiesInt( pc );
179  copyPropertiesFloat( pc );
180  copyPropertiesDouble( pc );
181  copyPropertiesReal( pc );
182  copyPropertiesBool( pc );
183  copyPropertiesString( pc );
184  copyPropertiesLong( pc );
185  copyPropertiesVec2( pc );
186  copyPropertiesVec3( pc );
187  };
188 
190  bool hasPropertyType( const std::string& name ) const {
191  return m_types.find( name ) != m_types.end();
192  };
193 
195  size_t size( ) const {
196  return m_types.size();
197  };
198 
199 
200  PROPERTY_TYPE( int64_t, Long )
201 
202  PROPERTY_TYPE( int, Int )
203 
204  PROPERTY_TYPE( float, Float )
205 
206  PROPERTY_TYPE( double, Double )
207 
209 
210  PROPERTY_TYPE( bool, Bool )
211 
212  PROPERTY_TYPE( std::string, String )
213 
215 
217 
218 
219 
220  void clear()
221  {
222  m_Long_map.clear();
223  m_Int_map.clear();
224  m_Float_map.clear();
225  m_Double_map.clear();
226  m_Real_map.clear();
227  m_Bool_map.clear();
228  m_String_map.clear();
229  m_Vec2_map.clear();
230  m_Vec3_map.clear();
231  }
232 
234 
235  protected:
237  TypeSet m_types;
238 
239  };
240 
242 
243 #define SAVE_PROPERTY_CONTAINER( Out, TypeName, Type ) \
244  {\
245  TypeName ## Map::const_iterator it = m_ ## TypeName ## _map.begin(); \
246  Out.beginSection("PropertyContainer");\
247  Out.addAttribute("size", m_ ## TypeName ## _map.size() );\
248  for(; it != m_ ## TypeName ## _map.end(); ++it) {\
249  Type TypeName ## value; \
250  std::string name = it->first; \
251  TypeName ## value = it->second; \
252  Out << agxStream::out( "name", name );\
253  Out << agxStream::out( "value", TypeName ## value );\
254  }\
255  Out.endSection("PropertyContainer");\
256  }
257 
258 /*
259  define SAVE_PROPERTY_CONTAINER_NON_OPERATOR( Out, TypeName, Type ) \
260  {\
261  TypeName ## Map::const_iterator it = m_ ## TypeName ## _map.begin(); \
262  Out << agxStream::out( "size", m_ ## TypeName ## _map.size() );\
263  for(; it != m_ ## TypeName ## _map.end(); ++it) {\
264  Type TypeName ## value; \
265  std::string name = it->first; \
266  TypeName ## value = it->second; \
267  Out << agxStream::out( "name", \
268  TypeName ## value.save( Out ); \
269  }\
270  }
271 */
272 
273 #define RESTORE_PROPERTY_CONTAINER( In, TypeName, Type ) \
274  {\
275  size_t size; std::string name;\
276  Type value;\
277  \
278  m_ ## TypeName ## _map.clear(); \
279  In.beginSection("PropertyContainer");\
280  In.getAttribute("size", size); \
281  for(size_t i=0; i < size ; ++i) {\
282  In >> agxStream::in( "name", name );\
283  In >> agxStream::in( "value", value );\
284  addProperty ## TypeName (name, value);\
285  }\
286  In.endSection("PropertyContainer");\
287  }
288 
289 /*
290  define RESTORE_PROPERTY_CONTAINER_NON_OPERATOR( In, TypeName, Type ) \
291  {\
292  size_t size; std::string name;\
293  Type value;\
294  \
295  m_ ## TypeName ## _map.clear(); \
296  In >> agxStream::in( "size", size );\
297  for(size_t i=0; i < size ; ++i) {\
298  In >> agxStream::in( name );\
299  value.restore( In );\
300  addProperty ## TypeName (name, value);\
301  }\
302  }
303 */
304 
305 } // namespace agx
306 
307 
308 #endif // AGX_PROPERTY_H
bool hasPropertyType(const std::string &name) const
Return true if a property of type name is present in the container.
Definition: Property.h:190
AGXTERRAIN_EXPORT void clear()
bool Bool
Definition: Integer.h:48
void copyProperties(const PropertyContainer *pc)
Definition: Property.h:177
agx::HashSet< std::string > TypeSet
Definition: Property.h:233
size_t size() const
Return the number of types that are registered.
Definition: Property.h:195
#define AGXPHYSICS_EXPORT
int32_t Int
Definition: Integer.h:35
Base class providing referencing counted objects.
Definition: Referenced.h:119
Class that is a container of named properties.
Definition: Property.h:158
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
virtual ~PropertyContainer()
Definition: Property.h:174
agx::ref_ptr< PropertyContainer > PropertyContainerRef
Definition: Property.h:241
#define PROPERTY_TYPE(type, Type)
Definition: Property.h:39
#define AGXSTREAM_DECLARE_SERIALIZABLE(T)
Use this in a Serializable class to add the required methods Important: Use full namespace in the dec...
Definition: Serializable.h:207
This class is an abstract base class for all classes that can be stored and retrieved from an Archive...
Definition: Serializable.h:44
double Real
Definition: Real.h:42