AGXDynamics  2.35.0.2
Value.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. 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 #include <agx/Object.h>
21 #include <agx/Event.h>
22 #include <agx/ref_ptr.h>
23 #include <agxData/Scalar.h>
24 
25 namespace agxNet { class Device; }
26 
27 namespace agxData
28 {
29  class Type;
30 
33 
43  {
44  public:
45  static agx::Model *ClassModel();
46 
47  static Value *load(agx::TiXmlElement *eValue, agx::Device *device);
48  virtual void configure(agx::TiXmlElement *eValue) override;
49  virtual void save(agx::TiXmlElement *eParent) const override;
50  virtual void snapshot(agx::TiXmlNode *eParent, const agx::String& directory) const override;
51 
52  public:
53 
55 
57  Event updateEvent; // TODO Remove!
58 
59  // Generated event interface, defined in Value.agxEvent
61 
62 #ifndef SWIG
63  void addListener(EventListener *listener);
64  void removeListener(EventListener *listener);
65  bool hasListener(EventListener *listener);
66 #endif
67 
68  public:
69  Value(const agx::Name& name, const agxData::Type *type);
70  Value(const agx::Name& name, const agxData::Format *format);
71 
72  Value(const agxData::Type *type);
73  Value(const agxData::Format *format);
74 
78  const void *ptr() const;
79 
80  template <typename T>
81  const T& get() const;
82 
83  template <typename T>
84  T transform() const;
85 
89  void set(const Value *other);
90 
91 
92  template <typename T>
93  void set(const T& value);
94 
95 
99  template <typename T>
100  void signal(const T& value);
101 
102 
106  void setDefaultValue();
107 
112  void setExpression(const agx::String& expression);
113 
118  const agxData::Type *getType() const;
119 
124  const agxData::Format *getFormat() const;
125 
129  Value *getBinding();
130  const Value *getBinding() const;
131 
135  Value *getRootBinding();
136  const Value *getRootBinding() const;
137 
141  const agx::Path& getBindPath() const;
142 
143 
147  void bind(const agx::Path& path);
148 
152  void bind(Value *binding);
153 
157  void unbind(bool removeAutoBindPath = true);
158 
162  virtual void rebind() override;
163 
167  bool isValid() const;
168 
169  virtual agxData::Data *getData();
170 
171  void print() const;
172  agx::String toString() const;
173  bool isTypeCompatible(const Value *other);
174 
175  Value *clone() const;
176  static void copy(Value *target, const Value *source);
177 
178  // Needed so template does not capture these cases
179  void set(Value *other);
180  void set(ValueRef& other);
181  void set(const ValueRef& other);
182 
183  void *ptr();
184 
185 
186  void setFormat(agxData::Format *format);
187 
188  virtual void buildNavigationTree(agxJson::Value& eNode) const override;
189  protected:
190  virtual ~Value();
191 
192  virtual bool bind(Object *object);
193 
194  // void setType(const agxData::Type *type);
195  void forceRemove();
196  private:
197  friend class agxNet::Device;
198  // void updateCallback(Value *other);
199  void set(const void *value);
200  void setStorage(void *ptr);
201  void propagateUpdate();
202 
203  static void allocate(Value *value);
204  static void deallocate(Value *value);
205 
206  private:
207  friend class agxData::Format;
208  void forceInvalidateDefaultFormatValue();
209 
210  protected:
218  EventDispatch m_eventDispatch;
219  EventListener m_bindingListener;
220  };
221 
223 
224  AGXCORE_EXPORT std::ostream& operator << ( std::ostream& output, const Value& value );
225 
226  //----------------------------------------------------------------------------------
227 
229  template <typename T>
230  class ValueT : public Value
231  {
232  public:
233  template <typename T2>
234  static bool ValidateCast(const agx::Referenced *object);
235 
236  public:
237  ValueT(const agx::Name& name);
238  ValueT(const T& value = T());
239  ValueT(const agx::Name& name, const T& value);
240 
241 
242  virtual ~ValueT();
243 
244  operator const T& () const;
245 
246  void set(const T& value);
247  void signal(const T& value);
248  const T& get() const;
249 
250  ValueT<T>& operator= (const ValueT<T>& value);
251  ValueT<T>& operator= (const T& value);
252  };
253 
255  template <typename T>
256  class ValueRefT : public agx::ref_ptr< ValueT<T> >
257  {
258  public:
260 
261  public:
263  ValueRefT(ValueT<T>* val) : RefT(val) {}
264 
265  AGX_FORCE_INLINE operator const ValueT<T>*() const { return this->get(); }
266  AGX_FORCE_INLINE ValueT<T>& operator*() const { return *this->get(); }
267  AGX_FORCE_INLINE ValueT<T>* operator->() const { return this->get(); }
268  };
269 
270 
271  //----------------------------------------------------------------------------------
272 
273 
277  template <typename T>
278  class Val : public ValueT<T>
279  {
280  public:
281  Val(const agx::Name& name) : ValueT<T>(name)
282  {
283  this->reference();
284  }
285 
286  Val(const agx::Name& name, const T& value) : ValueT<T>(name, value)
287  {
288  this->reference();
289  }
290 
291  Val(const T& value = T()) : ValueT<T>(value)
292  {
293  this->reference();
294  }
295 
296  virtual ~Val()
297  {
298  this->forceRemove();
299  }
300 
301 
302  const T *operator-> () const { return &this->get(); }
303  Val<T>& operator= (const Val<T>& value)
304  {
305  this->operator=(value.get());
306  return *this;
307  }
308 
309  Val<T>& operator= (const T& value)
310  {
311  this->set(value);
312  return *this;
313  }
314  };
315 
316 
317  //----------------------------------------------------------------------------------
318 
319 
320  /* Implementation */
321  AGX_FORCE_INLINE void *Value::ptr() { return m_value.ptr(); }
322  AGX_FORCE_INLINE const void *Value::ptr() const { return m_value.ptr(); }
323 
324  template <typename T>
325  AGX_FORCE_INLINE const T& Value::get() const
326  {
327  agxAssert(agxCore::isShutdown() || (m_format && m_format->is(agxData::getFormat<T>())));
328  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");
329  return *(const T *)m_value.ptr();
330  }
331 
332  template <typename T>
333  inline T Value::transform() const
334  {
335  T result;
336  agxData::transform(&result, agxData::getFormat<T>(), m_value.ptr(), m_format, 1);
337  return result;
338  }
339 
341  {
342  this->set(const_cast<const Value *>(other));
343  }
344 
346  {
347  this->set(const_cast<const Value *>(other.get()));
348  }
349 
351  {
352  this->set(const_cast<const Value *>(other.get()));
353  }
354 
355  template <typename T>
356  AGX_FORCE_INLINE void Value::set(const T& value)
357  {
358  if (!m_format)
359  this->setFormat(agxData::getFormat<T>());
360 
361  agxAssert(agxCore::isShutdown() || m_format->is(agxData::getFormat<T>()));
362  this->set((const void *)&value);
363  }
364 
365  template <typename T>
366  AGX_FORCE_INLINE void Value::signal(const T& value)
367  {
368  this->getRootBinding()->set(value);
369  }
370 
372  {
373  return m_format ? m_format->getType() : nullptr;
374  }
375 
377  {
378  return m_format ? m_format->getType() : nullptr;
379  }
380 
382  AGX_FORCE_INLINE const agxData::Format *Value::getFormat() const { return m_format; }
384  AGX_FORCE_INLINE const Value *Value::getBinding() const { return m_binding; }
385 
387  {
388  return m_value.ptr() != nullptr;
389  }
390 
392  {
393  return m_bindPath;
394  }
395 
396  //----------------------------------------------------------------------------------/////////
397 
398 
399  template <typename T> template <typename T2>
401  {
402  const Value *value = dynamic_cast<const Value *>(object);
403  return value ? value->getFormat() == agxData::getFormat<T>() : false;
404  }
405 
406 
407  template <typename T>
408  ValueT<T>::ValueT(const agx::Name& name) : Value(name, agxData::getFormat<T>())
409  {
410  }
411 
412 
413  template <typename T>
414  ValueT<T>::ValueT(const agx::Name& name, const T& value) : Value(name, agxData::getFormat<T>())
415  {
416  this->set(value);
417  }
418 
419  template <typename T>
420  ValueT<T>::ValueT(const T& value) : Value(agxData::getFormat<T>())
421  {
422  this->set(value);
423  }
424 
425  template <typename T>
427  {
428  }
429 
430 
431  template <typename T>
432  AGX_FORCE_INLINE ValueT<T>::operator const T& () const { return this->get(); }
433 
434  template <typename T>
435  AGX_FORCE_INLINE void ValueT<T>::set(const T& value)
436  {
437  Value::set(value);
438  }
439 
440  template <typename T>
441  AGX_FORCE_INLINE void ValueT<T>::signal(const T& value)
442  {
443  Value::signal(value);
444  }
445 
446 
447  template <typename T>
449  {
450  agxAssertN(this->isValid(), "Can not dereference an invalid value (%s)!", this->getPath().c_str());
451  return *(const T*)Value::ptr();
452  }
453 
454  template <typename T>
456  {
457  this->operator=(value.get());
458  return *this;
459  }
460 
461  template <typename T>
463  {
464  this->set(value);
465  return *this;
466  }
467 
468 
469 }
470 
472 
473 #endif /* _AGXDATA_VALUE_H_ */
agxData::Value::getType
agxData::Type * getType()
Definition: Value.h:371
AGXCORE_EXPORT
#define AGXCORE_EXPORT
agxData::Value::Event
agx::Event1< Value * > Event
Definition: Value.h:54
Scalar.h
agxData::Value
Abstract representation of a value.
Definition: Value.h:42
agx::Object::getContext
agx::Object * getContext()
Definition: Object.h:373
agxData::ValueRefT::RefT
agx::ref_ptr< ValueT< T > > RefT
Definition: Value.h:259
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:291
agxData::getFormat
AGXCORE_EXPORT Format * getFormat(const agx::String &typeFormatName)
agxData::ValueRefT::operator->
ValueT< T > * operator->() const
Definition: Value.h:267
agxData::ValueT::operator=
ValueT< T > & operator=(const ValueT< T > &value)
Definition: Value.h:455
agxData::Val::Val
Val(const agx::Name &name)
Definition: Value.h:281
agx::Path
Representation of a path, a list of name components.
Definition: Path.h:33
agxData::Value::m_children
ValuePtrVector m_children
Definition: Value.h:213
agxData::Value::getRootBinding
Value * getRootBinding()
agxData::Value::m_allocationIndex
agx::UInt32 m_allocationIndex
Definition: Value.h:216
agxData::getType
AGXCORE_EXPORT Type * getType(const agx::String &name)
agxData::ValueT
Templated value.
Definition: Value.h:230
agxData::Value::m_bindingListener
EventListener m_bindingListener
Definition: Value.h:219
agxData::Val::operator=
Val< T > & operator=(const Val< T > &value)
Definition: Value.h:303
agxAssert
#define agxAssert(expr)
Definition: debug.h:143
agxData::Value::m_restartIteration
bool m_restartIteration
Definition: Value.h:217
agxData::ValueRefT::ValueRefT
ValueRefT()
Definition: Value.h:262
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:400
agxData::Data
Generic data.
Definition: Data.h:39
agx::VectorPOD< Value * >
agxData::Value::m_eventDispatch
EventDispatch m_eventDispatch
Definition: Value.h:218
agxData::ValueT::ValueT
ValueT(const agx::Name &name)
Definition: Value.h:408
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:222
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:286
agxData::Value::get
const T & get() const
Definition: Value.h:325
agxData::ValueT::get
const T & get() const
Definition: Value.h:448
AGX_DECLARE_VECTOR_TYPES
#define AGX_DECLARE_VECTOR_TYPES(type)
Definition: Vector.h:34
agxData::Val::operator->
const T * operator->() const
Definition: Value.h:302
agxData::ValueT::signal
void signal(const T &value)
Definition: Value.h:441
agx::ref_ptr< Value >
agxData::Value::signal
void signal(const T &value)
Wrapper for this->getRootBinding()->set()
Definition: Value.h:366
agxData::Value::getFormat
agxData::Format * getFormat()
Definition: Value.h:381
agxData::ValueT::~ValueT
virtual ~ValueT()
Definition: Value.h:426
agxData::Type
Abstracted type.
Definition: Type.h:97
agx::LinearProbingHashTable
Inheritance with partial specialization due to bug with ref_ptr containers.
Definition: LinearProbingHashTable.h:871
Event.h
agxData::ValueRefT::ValueRefT
ValueRefT(ValueT< T > *val)
Definition: Value.h:263
agxData::Value::m_binding
ValueRef m_binding
Definition: Value.h:212
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:59
agxData::Value::forceRemove
void forceRemove()
ref_ptr.h
agxData::Value::m_value
AbstractScalar m_value
Definition: Value.h:214
agx::Event1< Value * >
agxCore::isShutdown
bool AGXCORE_EXPORT isShutdown()
agx::Referenced
Base class providing referencing counted objects.
Definition: Referenced.h:113
AGX_DECLARE_POINTER_TYPES
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
agxData::Value::updateEvent
Event updateEvent
Triggered when the value is updated.
Definition: Value.h:57
agxData::ValueRefT
Templated value-reference.
Definition: Value.h:256
agxData::Value::getBindPath
const agx::Path & getBindPath() const
Definition: Value.h:391
agxData::Value::getBinding
Value * getBinding()
Definition: Value.h:383
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:321
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:266
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:435
agxData::Value::transform
T transform() const
Definition: Value.h:333
agx::Object::getName
const agx::Name & getName() const
Definition: Object.h:369
agxData::Val::~Val
virtual ~Val()
Definition: Value.h:296
agx::UInt32
uint32_t UInt32
Definition: Integer.h:40
agxData::Value::m_format
FormatRef m_format
Definition: Value.h:215
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:278
agxData::Value::m_bindPath
agx::Path m_bindPath
Definition: Value.h:211
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:386
agx::ref_ptr< ValueT< T > >::get
ValueT< T > * get() const
Definition: ref_ptr.h:256
ValueEvent.Implementation.h