19#include <unordered_map>
22#include <tbb/spin_rw_mutex.h>
65 virtual const std::string&
name()
const;
70 virtual std::string
path()
const;
104 const std::string&
type()
const;
110 template <
typename T>
140 bool await(
bool silent =
false,
bool assert =
false,
double timeout = 3.0)
const;
160 bool silent =
false,
bool assert =
false,
double timeout = 3.0)
const;
167 template <
typename T>
179 template <
typename T>
188 template <
typename T>
198 template <
typename T>
207 template <
typename T>
220 template <
typename RET_T,
typename T>
221 RET_T
read(std::function<RET_T(
const T& value)> readFunc)
const;
237 template <
typename T>
253 template <
typename T>
254 bool modify(std::function<
void(T& value)> modifyFunc);
267 template <
typename T>
284 template <
typename T>
294 template <
typename T>
314 template <
typename T>
477 using DumpItem = std::pair<DtAttributeHandle, std::list<std::string>>;
498 std::chrono::time_point<std::chrono::high_resolution_clock>
myCreateTime;
562 using Ptr = std::shared_ptr<DtAttribute>;
565 using Ref = std::weak_ptr<DtAttribute>;
568 using Map = std::map<std::string, std::pair<Ptr, Ref>>;
571 using DataPtr = std::shared_ptr<DtAttributeData>;
688 void forEach(
const std::function<
void(
const Ptr)>& forEachFun)
const;
702 Ptr find(
const std::string&
path,
bool orCreate =
false, std::string* foundAs =
nullptr,
Ptr* foundIn =
nullptr);
711 const Ptr find(
const std::string&
path,
bool orCreate =
false, std::string* foundAs =
nullptr,
Ptr* foundIn =
nullptr)
const;
741 template <
typename T>
746 template <
typename T>
771 template <
typename T>
775 template <
typename T>
984 template <
typename RET_T>
985 RET_T
read(std::function<RET_T(
const T& value)> readFunc)
const;
1001 bool modify(std::function<
void(T& value)> modifyFunc);
1011#include "attribute_data.inl"
1012#include "attribute.inl"
1013#include "attributeHandle.inl"
1014#include "attributeCallback.inl"
Defines data types for the attribute system in VREngage.
Provides a callback mechanism for attribute changes in the VREngage system.
Provides handle classes for safe access to attributes.
Abstract base class for all attribute change callbacks.
Definition attributeCallback.h:34
Mutex myCallbackMutex
Value change callback mutex for thread safety.
Definition attribute.h:878
static std::unordered_map< unsigned int, bool > TheCallbackThreadMap
Definition attribute.h:885
bool removeAlias(DtAttributeHandle alias) const
Removes an alias path for this attribute.
Ptr mySelfInit
Temporary shared pointer to self for initialization of mySelf.
Definition attribute.h:894
bool _isType(const std::string &type) const
Internal implementation of isType()
static std::atomic< bool > TheTraceQueuedFlag
Atmoic flag to avoid unnecessary mutex locks when no trace queued.
Definition attribute.h:918
void modifyFast(std::function< bool(T &value)> modifyFunc)
Modifies the attribute value in-place using a callback.
unsigned int myWriteKey
Write permission key.
Definition attribute.h:906
const void * raw() const
Gets the raw data pointer.
DtAttributeHandle nextAfter(const DtAttributeHandle &ref)
Gets a handle to the next child attribute after the reference child.
bool containedBy(const DtAttributeHandle &attr) const
Checks if this attribute is a direct or indirect child of the given attribute.
static void callbackDispatchTick()
std::string myName
The name of this attribute.
Definition attribute.h:863
bool setType()
Establish the type of the attribute if value attribute is not yet set.
void cleanTheMap()
Performs cleanup of expired child attribute entries in the map.
std::string getAsString() const
Gets string representation of the attribute value.
DtAttributeHandle parent()
Gets a handle to the parent attribute.
const Ptr addAttribute(const std::string &name, bool &aliasExists) const
Const overload of addAttribute().
DtAttribute(const std::string &name, const Ptr &parent)
Constructor with name and parent.
bool isLocked() const
Checks if the attribute is write-locked.
void decHandleCount()
Decrements the handle count for this attribute.
DtAttributeHandle attribute(const std::string &name)
Gets a handle to a child attribute.
bool unset() const
Unsets the attribute value.
bool isRoot() const
Checks if this attribute is the root attribute.
Ptr find(const std::string &path, bool orCreate=false, std::string *foundAs=nullptr, Ptr *foundIn=nullptr)
Recursive search for child attribute at path, relative to this.
const DtAttributeHandle first() const
Gets a const handle to the first child attribute.
bool setFromString(const std::string &newValue)
Sets the attribute value from a string representation.
bool _isSet() const
Internal implementation of isSet()
bool await(bool silent=false, bool assert=false, double timeout=3.0) const
Blocks execution on this thread until this attribute value is defined.
static void unregisterCallbackThread()
DtAttributeHandle handleFor(const Ptr &child, const Map::const_iterator &) const
Generate a handle to the child attribute, with alias support.
void doTraceIf(Trace which) const
Triggers a diagnostic debug break and logs trace context for this attribute.
Trace
Trace level for diagnostic attribute access logging.
Definition attribute.h:529
@ LOOKUP
Trace on the next attribute lookup.
Definition attribute.h:534
@ CHANGE
Trace on the next value change.
Definition attribute.h:532
@ OFF
Tracing disabled.
Definition attribute.h:530
@ GET
Trace on the next value get.
Definition attribute.h:533
@ SET
Trace on the next value set.
Definition attribute.h:531
Ptr pFirst()
Non-const version of pFirst()
bool myExplicitType
Flag indicating if the type was explicitly set.
Definition attribute.h:869
static bool callbacksRunningOnThread(const unsigned int threadId)
Returns whether the given thread is invoking callbacks.
const Stats & stats() const
Gets the attribute statistics.
Definition attribute.h:522
bool setIfNot(const T &defaultValue)
Sets the value of the attribute only if it is currently undefined.
std::weak_ptr< DtAttribute > Ref
Weak pointer type for attributes.
Definition attribute.h:565
static void TidyUp()
Perform any necessary cleanup of expired attribute entries in the attribute tree.
static void recursiveDumpToTable(const DtAttributeHandle attrToDump, DumpTable &output, int depth=0)
bool isType() const
Checks if the attribute is of the specified type.
static std::unordered_map< unsigned int, bool > callbackThreadIds()
Gets the list of thread IDs that are known or registered to invoke callbacks.
const std::string & _type() const
Internal implementation of type()
const DtAttributeHandle attribute(const std::string &name) const
Const version of attribute(const std::string&)
void _createData() const
Creates data storage for the specified type.
bool contains(const DtAttributeHandle &attr) const
Checks if the given attribute is a direct or indirect child of this attribute.
bool await(std::function< bool(const DtAttributeHandle)> condition, bool silent=false, bool assert=false, double timeout=3.0) const
Blocks execution on this thread until the condition is satisfied.
virtual const std::string & name() const
Returns the name associated with this attribute.
const DtAttributeHandle nextAfter(const DtAttributeHandle &ref) const
Const version of nextAfter(const DtAttributeHandle&)
const Ptr pFirst() const
Get the first child attribute, excluding aliases.
DtAttribute()
Default constructor.
Ptr _nextValid(Map::iterator &fromIt) const
Get the next valid child entry.
Map::iterator myItr
Iterator in parent's attribute map referring to this attribute.
Definition attribute.h:903
static Ptr create(const std::string &name, const Ptr &parent)
Factory method that creates and registers a named child attribute.
T getAttributeOr(const std::string &attribute, T orValue) const
Gets the value of a child attribute or a default value if undefined.
std::pair< DtAttributeHandle, std::list< std::string > > DumpItem
Recursively dumps the attribute hierarchy to a table.
Definition attribute.h:477
static void QueueTrace(DtAttributeHandle attribute, Trace trace)
Queues a one-shot diagnostic trace for the specified attribute and access type.
static Mutex TheTraceMutex
Mutex guard for trace queue.
Definition attribute.h:915
static void CancelQueuedTrace()
Cancels any currently queued diagnostic trace.
Mutex myMapMutex
Sub-attribute map mutex for thread safety.
Definition attribute.h:875
const T getAttribute(const std::string &attribute) const
Gets the value of a child attribute.
void _assertType() const
Throws an assertion if the attribute is not of type T.
virtual std::string path() const
Returns the full path of this attribute in the hierarchy.
unsigned int handleCount() const
Gets the number of handles currently assigned to this attribute.
static void needsTidying(DtAttribute::Ptr dirty)
Marks the attribute as needing tidying, adds to list to be cleaned up on the next call to TidyUp().
Ref mySelf
Weak reference to self.
Definition attribute.h:891
bool setAttribute(const std::string &attribute, const T &newValue)
Sets the value of a child attribute.
static Mutex TheTidyMutex
Mutex protecting the static tidy list and related operations.
Definition attribute.h:924
bool unlock(unsigned int key)
Unlocks write access.
void addCallback(const DtAttributeCallbackBase &callback)
Create a temporary write-lock on this attribute.
void forEach(const std::function< void(Ptr)> &forEachFun)
Calls the given function for each immediate child, excluding aliases.
static void callbacksFinishedOnThread(const unsigned int threadId)
Record that the given thread is done invoking callbacks.
void invokePendingCallbacksRecursive()
Checks and dispatches pending change callbacks for this attribute and all descendants.
void forEach(const std::function< void(const DtAttributeHandle)> &forEachFun) const
Const version of forEach(std::function<void(DtAttributeHandle)>)
CallbackThreadMap myChangeCallbacks
List of change callbacks.
Definition attribute.h:881
const Ptr find(const std::string &path, bool orCreate=false, std::string *foundAs=nullptr, Ptr *foundIn=nullptr) const
Const overload of find().
bool _isLocked() const
Internal implementation of isLocked()
DataPtr myData
The data stored in this attribute.
Definition attribute.h:866
bool modify(std::function< void(T &value)> modifyFunc)
Modifies the attribute value in-place using a callback, with change detection.
tbb::spin_rw_mutex Mutex
Mutex type used for thread-safe attribute access.
Definition attribute.h:577
Ptr addAttribute(const std::string &name, bool &aliasExists)
Creates and registers a new child attribute with the given name.
bool isSet() const
Checks if the attribute value is undefined.
std::map< unsigned int, CallbacksForThread > CallbackThreadMap
Map of per-thread callback state, keyed by thread ID.
Definition attribute.h:597
void valueChanged()
Marks the value as changed and dispatches callbacks for the current thread.
static void callbacksStartedOnThread(const unsigned int threadId)
Record that the given thread is invoking callbacks.
static std::pair< Ref, std::string > TheLastTrace
The most recent trace that was fired, stored for debugging purposes.
Definition attribute.h:912
std::map< std::string, std::pair< Ptr, Ref > > Map
Map type for storing child attributes.
Definition attribute.h:568
static void quickDump(const DtAttributeHandle, bool includeValues=false, int depth=0)
const T get() const
Gets the value of the attribute.
Ptr self()
Non-const version of self()
void forEach(const std::function< void(const Ptr)> &forEachFun) const
Const version of forEach(std::function<void(Ptr)>)
tbb::spin_rw_mutex::scoped_lock MutexLock
Definition attribute.h:578
Map myAttributes
Map of child attributes.
Definition attribute.h:900
bool _isType() const
Internal implementation of isType()
std::atomic< unsigned int > myHandleCount
Count of active attribute handles to this attribute.
Definition attribute.h:921
void forEach(const std::function< void(DtAttributeHandle)> &forEachFun)
Calls the given function for each immediate child attribute.
std::shared_ptr< DtAttribute > Ptr
Shared pointer type for attributes.
Definition attribute.h:562
static std::map< void *, Ref > TheAttributesNeedTidying
List of attributes that need tidying up due to expired child entries.
Definition attribute.h:927
bool unsetRecursive()
Unsets this attribute and all child attributes, recursively.
static unsigned int callbackThreadIdForThread(const unsigned int threadId)
Gets the thread ID on which a callback from the current thread may be invoked.
bool isType(const std::string &type) const
Checks if the attribute is of the specified type by name.
DtAttributeHandle first()
Gets a handle to the first child attribute.
bool recursiveLock(unsigned int callerKey)
Locks this attribute and all descendants recursively.
Stats myStats
Statistics for this attribute.
Definition attribute.h:888
std::shared_ptr< DtAttributeData > DataPtr
Shared pointer type for attribute data storage.
Definition attribute.h:571
const DtAttributeHandle handle() const
Returns the const handle for this attribute.
void validateData(MutexLock &lock) const
Validates that the attribute data is of the specified type.
const Ptr self() const
Gets a shared pointer to this attribute.
bool hasAttribute(const std::string &name) const
Checks if a child attribute exists.
void setPersistence(bool persistent)
Set whether this attribute exists w/o handles assigned.
static Mutex TheCallbackThreadMapMutex
Definition attribute.h:883
void invokeCallbacks(const Ptr &changed, const unsigned int threadId)
Invokes all registered change callbacks.
static DtAttributeHandle Root()
Returns a reference to the root attribute.
const Ptr pNext(const Ptr ¤t) const
Gets a shared pointer to the next sibling attribute after the current child, excluding aliases.
const T previousValue() const
Gets the previous value of the attribute before the last change.
static void registerCallbackThread()
friend class DtAttributeHandle
Definition attribute.h:559
std::list< DumpItem > DumpTable
Definition attribute.h:478
DtAttributeHandle handle()
Returns the handle for this attribute.
bool isPersistent() const
Check the persistence state of this attribute.
bool lock(unsigned int key)
Locks write access to attribute value.
virtual ~DtAttribute()
Virtual destructor.
static std::pair< Ref, Trace > TheQueuedTrace
The currently queued one-shot diagnostic trace, if any.
Definition attribute.h:909
RET_T read(std::function< RET_T(const T &value)> readFunc) const
Reads the value of the attribute using a callback.
void setPersistenceRecursive(bool persistent)
Sets the persistence of this attribute and all child attributes.
Ptr myParent
Shared pointer to parent.
Definition attribute.h:897
bool set(const T &value)
Sets the value of the attribute.
void invokePendingCallbacks()
Checks and dispatches pending change callbacks for the calling thread.
std::list< const DtAttributeCallbackBase * > CallbackList
Ordered list of raw callback pointers for a single thread.
Definition attribute.h:574
bool recursiveUnlock(unsigned int callerKey)
Unlocks this attribute and all descendants whose lock key matches callerKey.
void removeCallback(const DtAttributeCallbackBase &callback)
Removes a change callback from this attribute.
const std::string & type() const
Returns the type of this attribute.
Mutex myValueMutex
Attribute data value mutex for thread safety.
Definition attribute.h:872
static std::pair< DtAttributeHandle, Trace > QueuedTrace()
Returns the currently queued trace entry.
bool addAlias(DtAttributeHandle alias) const
Assigns an alias path for this attribute.
T getOr(T orValue) const
Gets the value of the attribute or a default value if undefined.
const DtAttributeHandle parent() const
Gets a const handle to the parent attribute.
Ptr pNext(const Ptr ¤t)
Non-const version of pNext(const Ptr&)
void incHandleCount()
Increments the handle count for this attribute.
bool _lockCheck(unsigned int callerKey=0) const
Checks if the attribute is locked against the given caller key.
Type-specific attribute class.
Definition attribute.h:937
bool verifyType() const
Verifies that the attribute is of the correct type.
bool set(const T &value)
Sets the value of the attribute.
void modifyFast(std::function< bool(T &value)> modifyFunc)
Modifies the attribute value in-place using a callback.
bool setIfNot(const T &value)
Sets the value of the attribute only if it is currently undefined.
const T get() const
Gets the value of the attribute.
RET_T read(std::function< RET_T(const T &value)> readFunc) const
Reads the value of the attribute using a callback.
T getOr(T orValue) const
Gets the value of the attribute or a default value if undefined.
const T previousValue() const
Returns the previous value of this attribute before the last change.
bool modify(std::function< void(T &value)> modifyFunc)
Modifies the attribute value in-place with change detection.
Defines export macros for the VREngage Utility library.
#define UTIL_DLL
Export/import macro for non-Windows platforms.
Definition export.h:39
Provides logging functionality with various severity levels and channels.
!
Definition glsVreMessageUtil.h:50
Callback state scoped to a single thread.
Definition attribute.h:585
bool hasExpired
True if the attribute has expired entries in the CallbackList that should be cleaned up.
Definition attribute.h:593
bool valueChanged
True if the attribute value has changed and callbacks have not yet been dispatched.
Definition attribute.h:587
CallbackList callbacks
Callbacks registered for this thread.
Definition attribute.h:590
Statistics structure for tracking attribute usage.
Definition attribute.h:485
std::atomic< unsigned int > myGets
Number of attribute value gets.
Definition attribute.h:513
std::atomic< unsigned int > myGetsUndefined
Number of attribute value gets when undefined.
Definition attribute.h:516
std::atomic< unsigned int > myCreates
Number of attribute creations.
Definition attribute.h:504
Stats()
Constructor initializing all counters to zero.
Definition attribute.h:487
std::chrono::time_point< std::chrono::high_resolution_clock > myCreateTime
Time when the attribute was created.
Definition attribute.h:498
std::atomic< unsigned int > myChanges
Number of attribute value changes.
Definition attribute.h:510
std::atomic< unsigned int > mySets
Number of attribute value sets.
Definition attribute.h:507
std::atomic< unsigned int > myLookups
Number of attribute lookups.
Definition attribute.h:501