AGX Dynamics 2.42.1.1
Loading...
Searching...
No Matches
EntityRef.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_ENTITYREF_H
18#define AGXDATA_ENTITYREF_H
19
20#include <agx/AtomicValue.h>
21#include <agxData/EntityPtr.h>
22
23namespace agxData
24{
33 template <typename PtrT>
34 class EntityRef : public PtrT
35 {
36 public:
42
45
46 private:
47 void retain();
48 void release();
49 };
50
51
52
53
54
55 /* Implementation */
56 template <typename PtrT>
58
59 template <typename PtrT>
61 {
62 this->retain();
63 }
64
65 template <typename PtrT>
67 {
68 this->retain();
69 }
70
71 template <typename PtrT>
73 {
74 this->retain();
75 }
76
77 template <typename PtrT>
79 {
80 this->release();
81 }
82
83 template <typename PtrT>
85 {
86 if (this->isValid())
87 {
88 this->getStorage()->reference();
89
90 typename PtrT::InstanceType instance = this->instance();
91 agx::ScopeLock<agx::SpinMutex> scopeLock(instance.observerMutex());
92 instance.referenceCount()++;
93 instance.observers().push_back(this);
94 }
95 }
96
97 template <typename PtrT>
98 AGX_FORCE_INLINE void EntityRef<PtrT>::release()
99 {
100 if (this->isValid())
101 {
102 typename PtrT::InstanceType instance = this->instance();
103
104 agxData::EntityStorage* storage = nullptr;
105 agx::UInt32 refCounter = 0;
106 {
107 agx::ScopeLock<agx::SpinMutex> scopeLock(instance.observerMutex());
108
109 agx::UInt32& refCount = instance.referenceCount();
110 refCount--;
111
112 size_t index = instance.observers().find(this);
113 agxAssert(index != instance.observers().size());
114 instance.observers().eraseFast(index);
115
116 storage = this->getStorage();
117 refCounter = refCount;
118 }
119
120 if (refCounter == 0)
121 this->destroy();
122
123 storage->unreference();
124 }
125 }
126
127
128 template <typename PtrT>
130 {
131 this->release();
132 PtrT::init(storage, id);
133 this->retain();
134 }
135
136
137 template <typename PtrT>
139 {
140 this->release();
141 PtrT::operator=(other);
142 this->retain();
143 return* this;
144 }
145
146}
147
148
149#endif /* AGXDATA_ENTITYREF_H */
Accessor to a entity instance stored in a EntityStorage, to allow SOA (structure of arrays) data to b...
Definition: EntityPtr.h:52
An entity reference is a safe/referencing handle to an entity instance whose model is descendant from...
Definition: EntityRef.h:35
EntityRef(const agxData::EntityRef< PtrT > &other)
Definition: EntityRef.h:66
EntityRef(agxData::EntityStorage *storage, agx::Index id)
Definition: EntityRef.h:60
EntityRef< PtrT > & operator=(const EntityRef< PtrT > &other)
Definition: EntityRef.h:138
void init(agxData::EntityStorage *storage, agx::Index id)
Definition: EntityRef.h:129
EntityRef(const agxData::EntityPtr &other)
Definition: EntityRef.h:72
Data storage for a collection of entity instances of a specified EntityModel.
Definition: EntityStorage.h:73
void unreference(void *ptr=nullptr) const
Decrement the reference count by one, indicating that a pointer to this object is referencing it.
#define agxAssert(expr)
Definition: debug.h:143
#define AGX_FORCE_INLINE
Definition: macros.h:58
Contains classes for low level data storage for AGX.
Definition: Container.h:23
AGXTERRAIN_EXPORT TimingStorage & getStorage()
uint32_t UInt32
Definition: Integer.h:32
std::lock_guard< T > ScopeLock
agx::ScopeLock is an alias for std::lock_guard
Definition: ScopeLock.h:31
UInt32 Index
Definition: Integer.h:44