AGXDynamics  2.31.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Value.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2021. 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:
213  ValuePtrVector m_children;
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 
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_ */
Representation of a path, a list of name components.
Definition: Path.h:33
agxData::Format * getFormat()
Definition: Value.h:381
AGXCORE_EXPORT Type * getType(const agx::String &name)
void setFormat(agxData::Format *format)
const T * operator->() const
Definition: Value.h:302
AbstractScalar m_value
Definition: Value.h:214
bool isValid() const
Definition: Value.h:386
EventListener m_bindingListener
Definition: Value.h:219
Val< T > & operator=(const Val< T > &value)
Definition: Value.h:303
bool m_restartIteration
Definition: Value.h:217
A model is an abstract representation of the class of an agx::Object.
Definition: Model.h:40
Abstract representation of a value.
Definition: Value.h:42
AGXCORE_EXPORT Format * getFormat(const agx::String &typeFormatName)
#define agxAssertN(expr, format,...)
Definition: debug.h:145
void AGXCORE_EXPORT transform(void *target, const Format *targetFormat, const void *source, const Format *sourceFormat, size_t numElements)
Transform data between two buffers.
agxData::Type * getType()
Definition: Value.h:371
Generic data.
Definition: Data.h:39
void forceRemove()
A format is an implementation of a agxData::Type.
Definition: Format.h:63
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
Representation of a name string.
Definition: Name.h:32
ValueT(const agx::Name &name)
Definition: Value.h:408
#define AGX_FORCE_INLINE
Definition: Object.h:62
Templated value-reference.
Definition: Value.h:256
agx::HashTable< agx::String, ValueRef > ValueTable
Definition: Value.h:222
const agx::Name & getName() const
Definition: Object.h:369
Abstract representation of a scalar variable.
Definition: Scalar.h:29
void * ptr()
Definition: Data.h:84
agx::Path m_bindPath
Definition: Value.h:211
virtual ~Val()
Definition: Value.h:296
ValueT< T > & operator=(const ValueT< T > &value)
Definition: Value.h:455
FormatRef m_format
Definition: Value.h:215
ValueRef m_binding
Definition: Value.h:212
Base class providing referencing counted objects.
Definition: Referenced.h:113
Only use as member allocated variable.
Definition: Value.h:278
void signal(const T &value)
Definition: Value.h:441
const void * ptr() const
Definition: Value.h:322
Val(const agx::Name &name, const T &value)
Definition: Value.h:286
Abstracted type.
Definition: Type.h:97
AGX_DECLARE_VECTOR_TYPES(BinaryData)
agx::ref_ptr< ValueT< T > > RefT
Definition: Value.h:259
ValueRefT(ValueT< T > *val)
Definition: Value.h:263
Representation of a session on disk created by the FileFrameWriter.
Definition: FileJournal.h:118
T transform() const
Definition: Value.h:333
const agx::Path & getBindPath() const
Definition: Value.h:391
Event updateEvent
Triggered when the value is updated.
Definition: Value.h:57
ValueT< T > & operator*() const
Definition: Value.h:266
const T & get() const
Definition: Value.h:448
virtual ~ValueT()
Definition: Value.h:426
agx::UInt32 m_allocationIndex
Definition: Value.h:216
#define agxAssert(expr)
Definition: debug.h:143
static bool ValidateCast(const agx::Referenced *object)
Definition: Value.h:400
EventDispatch m_eventDispatch
Definition: Value.h:218
T * get() const
Definition: ref_ptr.h:256
Val(const agx::Name &name)
Definition: Value.h:281
Value * getBinding()
Definition: Value.h:383
Value * getRootBinding()
std::ostream & operator<<(std::ostream &output, const Attribute &attribute)
Val(const T &value=T())
Definition: Value.h:291
ValuePtrVector m_children
Definition: Value.h:213
agx::Event1< Value * > Event
Definition: Value.h:54
void signal(const T &value)
Wrapper for this-&gt;getRootBinding()-&gt;set()
Definition: Value.h:366
bool AGXCORE_EXPORT isShutdown()
Inheritance with partial specialization due to bug with ref_ptr containers.
const T & get() const
Definition: Value.h:325
void reference(void *ptr=nullptr) const
Explicitly increment the reference count by one, indicating that this object has another pointer whic...
An agx::Device is an abstract representation of a device on which data can be stored and processed...
Definition: Device.h:57
void set(const T &value)
Definition: Value.h:435
uint32_t UInt32
Definition: Integer.h:40
agx::Object * getContext()
Definition: Object.h:373
ValueT< T > * operator->() const
Definition: Value.h:267
#define AGXCORE_EXPORT
void set(const Value *other)
Explicitly set the value, removes active binding and disables autobinding.
Templated value.
Definition: Value.h:230
agx::Object is a refcounted object with a name.
Definition: Object.h:59
GLenum type
Definition: gl.h:5539