AGX Dynamics  2.37.3.2
EntityPtr.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_ENTITYPTR_H
18 #define AGXDATA_ENTITYPTR_H
19 
20 #ifdef _MSC_VER
21 # pragma warning( disable: 4714 )// const' marked as __forceinline not inlined
22 #endif
23 
24 #include <agx/agx.h>
25 #include <agx/Vector.h>
26 #include <agx/IndexRange.h>
27 #include <agxData/EntityInstance.h>
28 #include <iosfwd>
29 
30 namespace agxData
31 {
32  class EntityStorage;
33  class EntityModel;
34  class EntityData;
35 
36  class Attribute;
37  template <typename T> class ScalarAttributeT;
38  template <typename T> class PointerAttributeT;
39  template <typename T> class VectorAttributeT;
40  template <typename T> class HashSetAttributeT;
41  template <typename T> class ArrayAttributeT;
42  template <typename T> class SharedAttributeT;
43 
52  {
53  public:
54  EntityPtr();
56  EntityPtr(const agxData::EntityPtr& other) = default;
57  EntityPtr(const agxData::EntityInstance& instance);
58 
59  ~EntityPtr() = default;
60 
61  EntityPtr& operator=( const EntityPtr& other ) = default;
62  EntityPtr& operator=( EntityPtr&& ) = default;
63 
64  static EntityPtr createWithIndex(EntityStorage *storage, agx::Index index);
65 
70 
74  agxData::EntityData *getData();
75  const agxData::EntityData *getData() const;
76 
77  template <typename T>
78  T& dataSet();
79 
80  template <typename T>
81  const T& dataSet() const;
82 
86  agxData::EntityInstance instance();
87  const agxData::EntityInstance instance() const;
88 
92  agx::Index getId() const;
93 
97  void init(agxData::EntityStorage *storage, agx::Index id);
98 
102  void invalidate();
103 
107  agx::Index calculateIndex() const;
108 
109 
110 
114  bool isValid() const;
115 
119  void verifyIndex() const;
120 
125  const agxData::EntityModel* getModel() const;
126 
130  bool isInstanceOf(agxData::EntityModel *model) const;
131 
135  void destroy();
136 
140  void transfer(agxData::EntityStorage *target);
141 
142 
146  agxData::EntityPtr copy();
147 
152 
156  void copyAttributes(agxData::EntityPtr source);
157 
161  void swapId(EntityPtr& other);
162 
166  void swapIndex(EntityPtr& other);
167 
171  template <typename T>
172  T& operator() (const agxData::ScalarAttributeT<T> *attribute);
173 
174  template <typename T>
175  const T& operator() (const agxData::ScalarAttributeT<T> *attribute) const;
176 
178  template <typename T>
179  T& operator() (const agxData::PointerAttributeT<T> *attribute);
180 
181  template <typename T>
182  const T& operator() (const agxData::PointerAttributeT<T> *attribute) const;
183 
185  template <typename T>
186  typename agxData::VectorAttributeT<T>::Type& operator() (const agxData::VectorAttributeT<T> *attribute);
187 
188  template <typename T>
189  const typename agxData::VectorAttributeT<T>::Type& operator() (const agxData::VectorAttributeT<T> *attribute) const;
190 
192  template <typename T>
193  typename agxData::HashSetAttributeT<T>::Type& operator() (const agxData::HashSetAttributeT<T> *attribute);
194 
195  template <typename T>
196  const typename agxData::HashSetAttributeT<T>::Type& operator() (const agxData::HashSetAttributeT<T> *attribute) const;
197 
199  template <typename T>
200  typename agxData::ArrayAttributeT<T>::Type& operator() (const agxData::ArrayAttributeT<T> *attribute);
201 
202  template <typename T>
203  const typename agxData::ArrayAttributeT<T>::Type& operator() (const agxData::ArrayAttributeT<T> *attribute) const;
204 
206  template <typename T>
207  T& operator() (const agxData::SharedAttributeT<T> *attribute);
208 
209  template <typename T>
210  const T& operator() (const agxData::SharedAttributeT<T> *attribute) const;
211 
212  // Hash function
213  agx::UInt32 hash() const;
214 
215 
216  bool operator!() const;
217  operator bool() const;
218 
219  bool operator<( const EntityPtr& rhs ) const;
220  bool operator>( const EntityPtr& rhs ) const;
221 
222  bool operator==( const EntityPtr& rhs ) const;
223  bool operator!=( const EntityPtr& rhs ) const;
224 
225  void print(std::ostream& stream) const;
226  void print() const;
227 
228  static void TransformToAscii(void *targetBuffer, const void *sourceBuffer, size_t numElements);
229 
230  private:
231  template <typename T>
232  friend class AttributePtr;
233 
234  template <typename T>
235  T& getElement(const Attribute *attribute) const;
236 
237  private:
238  EntityStorage *m_storage;
239  agx::Index m_id;
240  };
241 
243 
244  AGXCORE_EXPORT std::ostream& operator << (std::ostream& output, const EntityPtr& entity);
245 
246 
248  {
249  public:
250  EntityRange();
251  EntityRange(EntityStorage *storage, size_t start, size_t end);
252 
253  EntityPtr operator[] (size_t index);
254  EntityPtr at(size_t index);
255  EntityRange operator[] (agx::IndexRange subRange);
256 
257  private:
258  EntityStorage *m_storage;
259  };
260 
261 
262 
263  /* Implementation */
265  AGX_FORCE_INLINE EntityPtr::EntityPtr(EntityStorage *storage, agx::Index id) { this->init(storage, id); }
266 
267 
268 
270  inline const EntityInstance EntityPtr::instance() const { return EntityInstance(const_cast<EntityData *>(getData()), calculateIndex()); }
271 
272  AGX_FORCE_INLINE EntityStorage *EntityPtr::getStorage() const { return m_storage; }
274 
276  {
277  m_storage = nullptr;
278  m_id = agx::InvalidIndex;
279  }
280 
282  {
283  return agx::hash( agx::hash( m_storage ), agx::hash( m_id ) );
284  }
285 
286 
287  AGX_FORCE_INLINE bool EntityPtr::isValid() const { return m_storage != nullptr; }
288  AGX_FORCE_INLINE bool EntityPtr::operator!() const { return !this->isValid(); }
289  AGX_FORCE_INLINE EntityPtr::operator bool() const { return this->isValid(); }
290 
292  {
293  return this->getId() < rhs.getId();
294  }
295 
297  {
298  return this->getId() > rhs.getId();
299  }
300 
302  {
303  return (this->getStorage() == rhs.getStorage()) && (this->getId() == rhs.getId());
304  }
305 
307  {
308  return (this->getStorage() != rhs.getStorage()) || (this->getId() != rhs.getId());
309  }
310 
311 }
312 
313 
314 #endif /* AGXDATA_ENTITYPTR_H */
AGXCORE_EXPORT UInt32 hash(const T &key)
Definition: HashFunction.h:65
void init(agxData::EntityStorage *storage, agx::Index id)
Set the target storage and id.
void AGXPHYSICS_EXPORT init()
Initialize AGX Dynamics API including thread resources and must be executed before using the AGX API...
agxData::EntityData * getData()
Inheritance with partial specialization due to bug with ref_ptr containers.
Definition: HashSet.h:669
agxData::EntityInstance instance()
Definition: EntityPtr.h:269
Data storage for a collection of entity instances of a specified EntityModel.
Definition: EntityStorage.h:72
bool operator==(T val, InvalidIndexStruct)
Definition: agx.h:194
AGXCORE_EXPORT Model * getModel(const Path &path, const Path &_namespace=Path())
Definition: Object.h:363
Entity instance provides access to a single instance in an EntityStorage.
Accessor to a entity instance stored in a EntityStorage, to allow SOA (structure of arrays) data to b...
Definition: EntityPtr.h:51
AGXCORE_EXPORT const InvalidIndexStruct InvalidIndex
agx::Index getId() const
Definition: EntityPtr.h:273
#define AGX_FORCE_INLINE
Definition: Object.h:59
bool operator==(const EntityPtr &rhs) const
Definition: EntityPtr.h:301
Type-specific Array used for fast access into the data held by a Buffer.
Definition: agxData.h:30
bool isValid() const
Definition: EntityPtr.h:287
bool operator!() const
Definition: EntityPtr.h:288
Templated vector class.
Definition: Vector.h:52
void invalidate()
Invalidate the entity pointer.
Definition: EntityPtr.h:275
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
An abstract attribute description, part of an Entity.
Definition: Attribute.h:57
Contains classes for low level data storage for AGX.
Definition: Container.h:23
UInt32 Index
Definition: Integer.h:52
An abstract description of a data entity stored using SOA (structure of arrays) pattern in a EntitySt...
Definition: EntityModel.h:63
bool operator>(const EntityPtr &rhs) const
Definition: EntityPtr.h:296
AGXTERRAIN_EXPORT TimingStorage & getStorage()
bool operator!=(const EntityPtr &rhs) const
Definition: EntityPtr.h:306
std::ostream & operator<<(std::ostream &output, const Attribute &attribute)
LinearProbingHashSetImplementation< KeyT, HashT >::iterator end(LinearProbingHashSetImplementation< KeyT, HashT > &set)
bool operator!=(T val, InvalidIndexStruct)
Definition: agx.h:200
agx::VectorPOD< EntityPtr > EntityPtrVector
Definition: EntityPtr.h:242
agxData::EntityStorage * getStorage() const
Definition: EntityPtr.h:272
bool operator<(const EntityPtr &rhs) const
Definition: EntityPtr.h:291
uint32_t UInt32
Definition: Integer.h:40
agx::Index calculateIndex() const
Calculate the index using the indirect id.
#define AGXCORE_EXPORT
agx::UInt32 hash() const
Definition: EntityPtr.h:281