AGX Dynamics 2.41.2.0
Loading...
Searching...
No Matches
Value.h
Go to the documentation of this file.
1/*
2Copyright 2007-2025. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx 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
31namespace agxNet { class Device; }
32
33namespace agxData
34{
35 class Type;
36
39
49 {
50 public:
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
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
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
176
177 void print() 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
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);
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
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 */
328 AGX_FORCE_INLINE const void *Value::ptr() const { return m_value.ptr(); }
329
330 template <typename T>
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
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>
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>
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_ */
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
#define AGXCORE_EXPORT
#define AGX_DECLARE_VECTOR_TYPES(type)
Definition: agx/Vector.h:34
Abstract representation of a scalar variable.
Definition: Scalar.h:30
Generic data.
Definition: Data.h:40
void * ptr()
Definition: Data.h:84
A format is an implementation of a agxData::Type.
Definition: Format.h:64
Abstracted type.
Definition: Type.h:98
Only use as member allocated variable.
Definition: Value.h:285
virtual ~Val()
Definition: Value.h:302
Val(const agx::Name &name, const T &value)
Definition: Value.h:292
Val(const agx::Name &name)
Definition: Value.h:287
Val< T > & operator=(const Val< T > &value)
Definition: Value.h:309
Val(const T &value=T())
Definition: Value.h:297
const T * operator->() const
Definition: Value.h:308
Templated value-reference.
Definition: Value.h:263
agx::ref_ptr< ValueT< T > > RefT
Definition: Value.h:265
ValueT< T > * operator->() const
Definition: Value.h:273
ValueT< T > & operator*() const
Definition: Value.h:272
ValueRefT(ValueT< T > *val)
Definition: Value.h:269
Templated value.
Definition: Value.h:237
static bool ValidateCast(const agx::Referenced *object)
Definition: Value.h:406
ValueT(const T &value=T())
Definition: Value.h:426
void signal(const T &value)
Definition: Value.h:447
ValueT(const agx::Name &name, const T &value)
Definition: Value.h:420
ValueT< T > & operator=(const ValueT< T > &value)
Definition: Value.h:461
ValueT(const agx::Name &name)
Definition: Value.h:414
void set(const T &value)
Definition: Value.h:441
const T & get() const
Definition: Value.h:454
virtual ~ValueT()
Definition: Value.h:432
Abstract representation of a value.
Definition: Value.h:49
void setFormat(agxData::Format *format)
void unbind(bool removeAutoBindPath=true)
Unbind, also disables auto-binding.
void bind(Value *binding)
Explicit binding, also disables auto-binding.
virtual void rebind() override
Rebind with the current auto-bind path.
bool m_restartIteration
Definition: Value.h:223
static agx::Model * ClassModel()
ValueRef m_binding
Definition: Value.h:218
Value(const agx::Name &name, const agxData::Format *format)
AbstractScalar m_value
Definition: Value.h:220
virtual void buildNavigationTree(agxJson::Value &eNode) const override
bool isValid() const
Definition: Value.h:392
Value * getRootBinding()
Event updateEvent
Triggered when the value is updated.
Definition: Value.h:63
void setDefaultValue()
Set the default value of the format.
void print() const
const void * ptr() const
Definition: Value.h:328
static void copy(Value *target, const Value *source)
virtual ~Value()
EventListener m_bindingListener
Definition: Value.h:225
Value(const agxData::Type *type)
agx::UInt32 m_allocationIndex
Definition: Value.h:222
Value * clone() const
virtual void configure(agx::TiXmlElement *eValue) override
agxData::Format * getFormat()
Definition: Value.h:387
virtual void snapshot(agx::TiXmlNode *eParent, const agx::String &directory) const override
virtual agxData::Data * getData()
agxData::Type * getType()
Definition: Value.h:377
virtual bool bind(Object *object)
virtual void save(agx::TiXmlElement *eParent) const override
ValuePtrVector m_children
Definition: Value.h:219
FormatRef m_format
Definition: Value.h:221
void setExpression(const agx::String &expression)
Configure value using an expression.
Value(const agxData::Format *format)
const Value * getRootBinding() const
void addListener(EventListener *listener)
T transform() const
Definition: Value.h:339
void signal(const T &value)
Wrapper for this->getRootBinding()->set()
Definition: Value.h:372
const T & get() const
Definition: Value.h:331
static Value * load(agx::TiXmlElement *eValue, agx::Device *device)
EventDispatch m_eventDispatch
Definition: Value.h:224
void forceRemove()
void set(const Value *other)
Explicitly set the value, removes active binding and disables autobinding.
const agx::Path & getBindPath() const
Definition: Value.h:397
Value * getBinding()
Definition: Value.h:389
agx::Path m_bindPath
Definition: Value.h:217
void removeListener(EventListener *listener)
bool hasListener(EventListener *listener)
agx::String toString() const
agx::Event1< Value * > Event
Definition: Value.h:60
void bind(const agx::Path &path)
Set bind path which will auto-bind when resolved.
Value(const agx::Name &name, const agxData::Type *type)
bool isTypeCompatible(const Value *other)
An agx::Device is an abstract representation of a device on which data can be stored and processed.
Definition: Device.h:58
An event with one argument.
Definition: Event.h:103
Inheritance with partial specialization due to bug with ref_ptr containers.
A model is an abstract representation of the class of an agx::Object.
Definition: Model.h:41
Representation of a name string.
Definition: Name.h:33
agx::Object is a refcounted object with a name.
Definition: Object.h:59
const agx::Name & getName() const
Definition: Object.h:382
agx::Object * getContext()
Definition: Object.h:386
Representation of a path, a list of name components.
Definition: Path.h:33
Base class providing referencing counted objects.
Definition: Referenced.h:120
void reference(void *ptr=nullptr) const
Explicitly increment the reference count by one, indicating that this object has another pointer whic...
ValueT< T > * get() const
Definition: ref_ptr.h:256
#define agxAssertN(expr, format,...)
Definition: debug.h:145
#define agxAssert(expr)
Definition: debug.h:143
#define AGX_FORCE_INLINE
Definition: macros.h:58
bool AGXCORE_EXPORT isShutdown()
Contains classes for low level data storage for AGX.
Definition: Container.h:23
Format * getFormat()
Definition: Type.h:269
AGXCORE_EXPORT Format * getFormat(const agx::String &typeFormatName)
AGXCORE_EXPORT Type * getType(const agx::String &name)
std::ostream & operator<<(std::ostream &output, const Attribute &attribute)
agx::HashTable< agx::String, ValueRef > ValueTable
Definition: Value.h:228
void AGXCORE_EXPORT transform(void *target, const Format *targetFormat, const void *source, const Format *sourceFormat, size_t numElements)
Transform data between two buffers.
Containins classes for sending/reading data over sockets as well as compression functionality.
Definition: MacUtil.h:23
uint32_t UInt32
Definition: Integer.h:32