AGX Dynamics  2.38.0.2
Value.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 AGXDATA_VALUE_H
18 #define AGXDATA_VALUE_H
19 
20 #ifdef _MSC_VER
21 # pragma warning(push)
22 # pragma warning(disable: 4251) // warning C4251: class X needs to have dll-interface to be used by clients of class Y
23 #endif
24 
25 
26 #include <agx/Object.h>
27 #include <agx/Event.h>
28 #include <agx/ref_ptr.h>
29 #include <agxData/Scalar.h>
30 
31 namespace agxNet { class Device; }
32 
33 namespace agxData
34 {
35  class Type;
36 
39 
49  {
50  public:
51  static agx::Model *ClassModel();
52 
53  static Value *load(agx::TiXmlElement *eValue, agx::Device *device);
54  virtual void configure(agx::TiXmlElement *eValue) override;
55  virtual void save(agx::TiXmlElement *eParent) const override;
56  virtual void snapshot(agx::TiXmlNode *eParent, const agx::String& directory) const override;
57 
58  public:
59 
61 
63  Event updateEvent; // TODO Remove!
64 
65  // Generated event interface, defined in Value.agxEvent
67 
68 #ifndef SWIG
69  void addListener(EventListener *listener);
70  void removeListener(EventListener *listener);
71  bool hasListener(EventListener *listener);
72 #endif
73 
74  public:
75  Value(const agx::Name& name, const agxData::Type *type);
76  Value(const agx::Name& name, const agxData::Format *format);
77 
78  Value(const agxData::Type *type);
79  Value(const agxData::Format *format);
80 
84  const void *ptr() const;
85 
86  template <typename T>
87  const T& get() const;
88 
89  template <typename T>
90  T transform() const;
91 
95  void set(const Value *other);
96 
97 
98  template <typename T>
99  void set(const T& value);
100 
101 
105  template <typename T>
106  void signal(const T& value);
107 
108 
112  void setDefaultValue();
113 
118  void setExpression(const agx::String& expression);
119 
124  const agxData::Type *getType() const;
125 
130  const agxData::Format *getFormat() const;
131 
135  Value *getBinding();
136  const Value *getBinding() const;
137 
141  Value *getRootBinding();
142  const Value *getRootBinding() const;
143 
147  const agx::Path& getBindPath() const;
148 
149 
153  void bind(const agx::Path& path);
154 
158  void bind(Value *binding);
159 
163  void unbind(bool removeAutoBindPath = true);
164 
168  virtual void rebind() override;
169 
173  bool isValid() const;
174 
175  virtual agxData::Data *getData();
176 
177  void print() const;
178  agx::String toString() const;
179  bool isTypeCompatible(const Value *other);
180 
181  Value *clone() const;
182  static void copy(Value *target, const Value *source);
183 
184  // Needed so template does not capture these cases
185  void set(Value *other);
186  void set(ValueRef& other);
187  void set(const ValueRef& other);
188 
189  void *ptr();
190 
191 
192  void setFormat(agxData::Format *format);
193 
194  virtual void buildNavigationTree(agxJson::Value& eNode) const override;
195  protected:
196  virtual ~Value();
197 
198  virtual bool bind(Object *object);
199 
200  // void setType(const agxData::Type *type);
201  void forceRemove();
202  private:
203  friend class agxNet::Device;
204  // void updateCallback(Value *other);
205  void set(const void *value);
206  void setStorage(void *ptr);
207  void propagateUpdate();
208 
209  static void allocate(Value *value);
210  static void deallocate(Value *value);
211 
212  private:
213  friend class agxData::Format;
214  void forceInvalidateDefaultFormatValue();
215 
216  protected:
224  EventDispatch m_eventDispatch;
225  EventListener m_bindingListener;
226  };
227 
229 
230  AGXCORE_EXPORT std::ostream& operator << ( std::ostream& output, const Value& value );
231 
232  //----------------------------------------------------------------------------------
233 
235  template <typename T>
236  class ValueT : public Value
237  {
238  public:
239  template <typename T2>
240  static bool ValidateCast(const agx::Referenced *object);
241 
242  public:
243  ValueT(const agx::Name& name);
244  ValueT(const T& value = T());
245  ValueT(const agx::Name& name, const T& value);
246 
247 
248  virtual ~ValueT();
249 
250  operator const T& () const;
251 
252  void set(const T& value);
253  void signal(const T& value);
254  const T& get() const;
255 
256  ValueT<T>& operator= (const ValueT<T>& value);
257  ValueT<T>& operator= (const T& value);
258  };
259 
261  template <typename T>
262  class ValueRefT : public agx::ref_ptr< ValueT<T> >
263  {
264  public:
266 
267  public:
269  ValueRefT(ValueT<T>* val) : RefT(val) {}
270 
271  AGX_FORCE_INLINE operator const ValueT<T>*() const { return this->get(); }
272  AGX_FORCE_INLINE ValueT<T>& operator*() const { return *this->get(); }
273  AGX_FORCE_INLINE ValueT<T>* operator->() const { return this->get(); }
274  };
275 
276 
277  //----------------------------------------------------------------------------------
278 
279 
283  template <typename T>
284  class Val : public ValueT<T>
285  {
286  public:
287  Val(const agx::Name& name) : ValueT<T>(name)
288  {
289  this->reference();
290  }
291 
292  Val(const agx::Name& name, const T& value) : ValueT<T>(name, value)
293  {
294  this->reference();
295  }
296 
297  Val(const T& value = T()) : ValueT<T>(value)
298  {
299  this->reference();
300  }
301 
302  virtual ~Val()
303  {
304  this->forceRemove();
305  }
306 
307 
308  const T *operator-> () const { return &this->get(); }
309  Val<T>& operator= (const Val<T>& value)
310  {
311  this->operator=(value.get());
312  return *this;
313  }
314 
315  Val<T>& operator= (const T& value)
316  {
317  this->set(value);
318  return *this;
319  }
320  };
321 
322 
323  //----------------------------------------------------------------------------------
324 
325 
326  /* Implementation */
327  AGX_FORCE_INLINE void *Value::ptr() { return m_value.ptr(); }
328  AGX_FORCE_INLINE const void *Value::ptr() const { return m_value.ptr(); }
329 
330  template <typename T>
331  AGX_FORCE_INLINE const T& Value::get() const
332  {
333  agxAssert(agxCore::isShutdown() || (m_format && m_format->is(agxData::getFormat<T>())));
334  agxAssertN(this->isValid(), "Can not dereference an invalid value! Value \'%s\' in component \'%s\'", this->getName().c_str(), this->getContext() ? this->getContext()->getName().c_str() : "nullptr");
335  return *(const T *)m_value.ptr();
336  }
337 
338  template <typename T>
339  inline T Value::transform() const
340  {
341  T result;
342  agxData::transform(&result, agxData::getFormat<T>(), m_value.ptr(), m_format, 1);
343  return result;
344  }
345 
347  {
348  this->set(const_cast<const Value *>(other));
349  }
350 
352  {
353  this->set(const_cast<const Value *>(other.get()));
354  }
355 
357  {
358  this->set(const_cast<const Value *>(other.get()));
359  }
360 
361  template <typename T>
362  AGX_FORCE_INLINE void Value::set(const T& value)
363  {
364  if (!m_format)
365  this->setFormat(agxData::getFormat<T>());
366 
367  agxAssert(agxCore::isShutdown() || m_format->is(agxData::getFormat<T>()));
368  this->set((const void *)&value);
369  }
370 
371  template <typename T>
372  AGX_FORCE_INLINE void Value::signal(const T& value)
373  {
374  this->getRootBinding()->set(value);
375  }
376 
378  {
379  return m_format ? m_format->getType() : nullptr;
380  }
381 
383  {
384  return m_format ? m_format->getType() : nullptr;
385  }
386 
388  AGX_FORCE_INLINE const agxData::Format *Value::getFormat() const { return m_format; }
390  AGX_FORCE_INLINE const Value *Value::getBinding() const { return m_binding; }
391 
393  {
394  return m_value.ptr() != nullptr;
395  }
396 
398  {
399  return m_bindPath;
400  }
401 
402  //----------------------------------------------------------------------------------/////////
403 
404 
405  template <typename T> template <typename T2>
407  {
408  const Value *value = dynamic_cast<const Value *>(object);
409  return value ? value->getFormat() == agxData::getFormat<T>() : false;
410  }
411 
412 
413  template <typename T>
414  ValueT<T>::ValueT(const agx::Name& name) : Value(name, agxData::getFormat<T>())
415  {
416  }
417 
418 
419  template <typename T>
420  ValueT<T>::ValueT(const agx::Name& name, const T& value) : Value(name, agxData::getFormat<T>())
421  {
422  this->set(value);
423  }
424 
425  template <typename T>
426  ValueT<T>::ValueT(const T& value) : Value(agxData::getFormat<T>())
427  {
428  this->set(value);
429  }
430 
431  template <typename T>
433  {
434  }
435 
436 
437  template <typename T>
438  AGX_FORCE_INLINE ValueT<T>::operator const T& () const { return this->get(); }
439 
440  template <typename T>
441  AGX_FORCE_INLINE void ValueT<T>::set(const T& value)
442  {
443  Value::set(value);
444  }
445 
446  template <typename T>
447  AGX_FORCE_INLINE void ValueT<T>::signal(const T& value)
448  {
449  Value::signal(value);
450  }
451 
452 
453  template <typename T>
455  {
456  agxAssertN(this->isValid(), "Can not dereference an invalid value (%s)!", this->getPath().c_str());
457  return *(const T*)Value::ptr();
458  }
459 
460  template <typename T>
462  {
463  this->operator=(value.get());
464  return *this;
465  }
466 
467  template <typename T>
469  {
470  this->set(value);
471  return *this;
472  }
473 
474 
475 }
476 
478 
479 #ifdef _MSC_VER
480 # pragma warning(pop)
481 #endif
482 
483 
484 #endif /* _AGXDATA_VALUE_H_ */
agxData::Value::getType
agxData::Type * getType()
Definition: Value.h:377
AGXCORE_EXPORT
#define AGXCORE_EXPORT
agxData::Value::Event
agx::Event1< Value * > Event
Definition: Value.h:60
Scalar.h
agxData::Value
Abstract representation of a value.
Definition: Value.h:48
agx::Object::getContext
agx::Object * getContext()
Definition: Object.h:372
agxData::ValueRefT::RefT
agx::ref_ptr< ValueT< T > > RefT
Definition: Value.h:265
agxData::transform
void AGXCORE_EXPORT transform(void *target, const Format *targetFormat, const void *source, const Format *sourceFormat, size_t numElements)
Transform data between two buffers.
agxData::Val::Val
Val(const T &value=T())
Definition: Value.h:297
agxData::getFormat
AGXCORE_EXPORT Format * getFormat(const agx::String &typeFormatName)
agxData::ValueRefT::operator->
ValueT< T > * operator->() const
Definition: Value.h:273
agxData::ValueT::operator=
ValueT< T > & operator=(const ValueT< T > &value)
Definition: Value.h:461
agxData::Val::Val
Val(const agx::Name &name)
Definition: Value.h:287
agx::Path
Representation of a path, a list of name components.
Definition: Path.h:32
agxData::Value::m_children
ValuePtrVector m_children
Definition: Value.h:219
agxData::Value::getRootBinding
Value * getRootBinding()
agxData::Value::m_allocationIndex
agx::UInt32 m_allocationIndex
Definition: Value.h:222
agxData::getType
AGXCORE_EXPORT Type * getType(const agx::String &name)
agxData::ValueT
Templated value.
Definition: Value.h:236
agxData::Value::m_bindingListener
EventListener m_bindingListener
Definition: Value.h:225
agxData::Val::operator=
Val< T > & operator=(const Val< T > &value)
Definition: Value.h:309
agxAssert
#define agxAssert(expr)
Definition: debug.h:143
agxData::Value::m_restartIteration
bool m_restartIteration
Definition: Value.h:223
agxData::ValueRefT::ValueRefT
ValueRefT()
Definition: Value.h:268
agxData::Value::setFormat
void setFormat(agxData::Format *format)
agxData::Format
A format is an implementation of a agxData::Type.
Definition: Format.h:63
agxData::AbstractScalar
Abstract representation of a scalar variable.
Definition: Scalar.h:29
agxData::ValueT::ValidateCast
static bool ValidateCast(const agx::Referenced *object)
Definition: Value.h:406
agxData::Data
Generic data.
Definition: Data.h:39
agx::VectorPOD< Value * >
agxData::Value::m_eventDispatch
EventDispatch m_eventDispatch
Definition: Value.h:224
agxData::ValueT::ValueT
ValueT(const agx::Name &name)
Definition: Value.h:414
agxData::Data::ptr
void * ptr()
Definition: Data.h:84
agxData
Contains classes for low level data storage for AGX.
Definition: Container.h:23
agxData::ValueTable
agx::HashTable< agx::String, ValueRef > ValueTable
Definition: Value.h:228
agx::Model
A model is an abstract representation of the class of an agx::Object.
Definition: Model.h:40
agxData::Val::Val
Val(const agx::Name &name, const T &value)
Definition: Value.h:292
agxData::Value::get
const T & get() const
Definition: Value.h:331
agxData::ValueT::get
const T & get() const
Definition: Value.h:454
AGX_DECLARE_VECTOR_TYPES
#define AGX_DECLARE_VECTOR_TYPES(type)
Definition: Vector.h:34
agxData::Val::operator->
const T * operator->() const
Definition: Value.h:308
agxData::ValueT::signal
void signal(const T &value)
Definition: Value.h:447
agx::ref_ptr< Value >
agxData::Value::signal
void signal(const T &value)
Wrapper for this->getRootBinding()->set()
Definition: Value.h:372
agxData::Value::getFormat
agxData::Format * getFormat()
Definition: Value.h:387
agxData::ValueT::~ValueT
virtual ~ValueT()
Definition: Value.h:432
agxData::Type
Abstracted type.
Definition: Type.h:97
agx::LinearProbingHashTable
Inheritance with partial specialization due to bug with ref_ptr containers.
Definition: LinearProbingHashTable.h:926
Event.h
agxData::ValueRefT::ValueRefT
ValueRefT(ValueT< T > *val)
Definition: Value.h:269
agxData::Value::m_binding
ValueRef m_binding
Definition: Value.h:218
agx::Name
Representation of a name string.
Definition: Name.h:32
Object.h
agx::Object
agx::Object is a refcounted object with a name.
Definition: Object.h:58
agxData::Value::forceRemove
void forceRemove()
ref_ptr.h
agxData::Value::m_value
AbstractScalar m_value
Definition: Value.h:220
agx::Event1< Value * >
agxCore::isShutdown
bool AGXCORE_EXPORT isShutdown()
agx::Referenced
Base class providing referencing counted objects.
Definition: Referenced.h:119
AGX_DECLARE_POINTER_TYPES
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
agxData::Value::updateEvent
Event updateEvent
Triggered when the value is updated.
Definition: Value.h:63
agxData::ValueRefT
Templated value-reference.
Definition: Value.h:262
agxData::Value::getBindPath
const agx::Path & getBindPath() const
Definition: Value.h:397
agxData::Value::getBinding
Value * getBinding()
Definition: Value.h:389
agx::Device
An agx::Device is an abstract representation of a device on which data can be stored and processed.
Definition: Device.h:57
agx::AgXString< std::string >
agxAssertN
#define agxAssertN(expr, format,...)
Definition: debug.h:145
agxData::Value::ptr
const void * ptr() const
Definition: Value.h:327
agxData::Value::set
void set(const Value *other)
Explicitly set the value, removes active binding and disables autobinding.
agxData::ValueRefT::operator*
ValueT< T > & operator*() const
Definition: Value.h:272
ValueEvent.Declaration.h
agx::Referenced::reference
void reference(void *ptr=nullptr) const
Explicitly increment the reference count by one, indicating that this object has another pointer whic...
agxData::ValueT::set
void set(const T &value)
Definition: Value.h:441
agxData::Value::transform
T transform() const
Definition: Value.h:339
agx::Object::getName
const agx::Name & getName() const
Definition: Object.h:368
agxData::Val::~Val
virtual ~Val()
Definition: Value.h:302
agx::UInt32
uint32_t UInt32
Definition: Integer.h:32
agxData::Value::m_format
FormatRef m_format
Definition: Value.h:221
agxData::operator<<
std::ostream & operator<<(std::ostream &output, const Attribute &attribute)
agxStream::ElementTypes::Type
Type
Definition: Types.h:37
agxData::Val
Only use as member allocated variable.
Definition: Value.h:284
agxData::Value::m_bindPath
agx::Path m_bindPath
Definition: Value.h:217
AGX_FORCE_INLINE
#define AGX_FORCE_INLINE
Definition: macros.h:58
agxNet
Containins classes for sending/reading data over sockets as well as compression functionality.
Definition: MacUtil.h:22
agxData::Value::isValid
bool isValid() const
Definition: Value.h:392
agx::ref_ptr< ValueT< T > >::get
ValueT< T > * get() const
Definition: ref_ptr.h:256
ValueEvent.Implementation.h