AGX Dynamics  2.37.2.0
EntityRef.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. 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_ENTITYREF_H
18 #define AGXDATA_ENTITYREF_H
19 
20 #include <agx/AtomicValue.h>
21 #include <agxData/EntityPtr.h>
22 
23 namespace agxData
24 {
33  template <typename PtrT>
34  class EntityRef : public PtrT
35  {
36  public:
37  EntityRef();
39  EntityRef(const agxData::EntityRef<PtrT>& other);
40  EntityRef(const agxData::EntityPtr& other);
41  ~EntityRef();
42 
43  void init(agxData::EntityStorage* storage, agx::Index id);
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>
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 */
void AGXPHYSICS_EXPORT init()
Initialize AGX Dynamics API including thread resources and must be executed before using the AGX API...
An entity reference is a safe/referencing handle to an entity instance whose model is descendant from...
Definition: EntityRef.h:34
Data storage for a collection of entity instances of a specified EntityModel.
Definition: EntityStorage.h:72
Accessor to a entity instance stored in a EntityStorage, to allow SOA (structure of arrays) data to b...
Definition: EntityPtr.h:51
#define AGX_FORCE_INLINE
Definition: Object.h:59
void init(agxData::EntityStorage *storage, agx::Index id)
Definition: EntityRef.h:129
std::lock_guard< T > ScopeLock
agx::ScopeLock is an alias for std::lock_guard
Definition: ScopeLock.h:31
EntityRef< PtrT > & operator=(const EntityRef< PtrT > &other)
Definition: EntityRef.h:138
Contains classes for low level data storage for AGX.
Definition: Container.h:23
UInt32 Index
Definition: Integer.h:52
#define agxAssert(expr)
Definition: debug.h:143
AGXTERRAIN_EXPORT TimingStorage & getStorage()
void unreference(void *ptr=nullptr) const
Decrement the reference count by one, indicating that a pointer to this object is referencing it...
uint32_t UInt32
Definition: Integer.h:40