AGX Dynamics  2.37.2.0
EntityModel.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_ENTITY_MODEL_H
18 #define AGXDATA_ENTITY_MODEL_H
19 
20 
21 #include <agx/Vector.h>
22 #include <agx/HashTable.h>
23 #include <agx/String.h>
24 #include <agx/Model.h>
25 #include <agx/agxCore_export.h>
26 #include <agxData/EntityPtr.h>
27 #include <agxData/EntityInstance.h>
28 #include <agxData/AttributePtr.h>
29 #include <agxData/Attribute.h>
30 
31 
32 #define AGX_ENTITY_NAMESPACE ""
33 
34 namespace agx
35 {
36  class TiXmlElement;
37  class Device;
38  class EntityParameter;
39  class NameSpace;
40 }
41 
42 namespace agxData
43 {
44  class EntityData;
45 
49 
52 
54  AGXCORE_EXPORT bool hasEntity(const agx::Path& path);
55 
57 
64  {
65  public:
66  static agx::Model *ClassModel();
68 
69  public:
70  static EntityModel *load(agx::TiXmlElement *eEntity, agx::Device *device);
71  virtual void configure(agx::TiXmlElement *eEntity);
72 
73  static bool findEntityPath(const agx::Path& entityPath, agx::Path& fullEntityPath, agx::String& filePath, agx::String& classPath, agx::TiXmlElement *relativeRoot = nullptr, agxIO::Environment::Type envType = agxIO::Environment::RUNTIME_PATH);
74 
75  static agx::Namespace *root();
76 
77 
78 
79 
80  static void setThreadSafe(bool flag);
81  static bool getThreadSafe();
82  public:
84 
85  StorageEvent storageCreationEvent;
86 
87  public:
88  EntityModel(const agx::Name& name);
89 
93  EntityModel* getSource();
94  const EntityModel* getSource() const;
95 
100  void addAttribute(Attribute *attribute);
101 
105  const AttributePtrVector& getAttributes() const;
106 
110  Attribute* getAttribute(const agx::Name& name);
111  const Attribute* getAttribute(const agx::Name& name) const;
112 
113 
114  void setUseInstanceTable(bool flag);
115  bool useInstanceTable() const;
116 
117  void setHasConstructor(bool flag);
118  bool hasConstructor() const;
119 
123  EntityStorage *getDefaultStorage();
124 
125 
129  size_t getNumBytes();
130  size_t getNumBytes(agx::Device *device);
131 
135  void print(std::ostream& stream) const;
136  void print() const;
137 
138  static EntityModel *getRootModel();
139  void setBaseEntity(EntityModel *base);
140 
141  virtual void shutdownCleanup() {}
142 
143  public:
144  virtual EntityData *createData(EntityStorage *storage);
145 
146  class Loader;
147 
148  template <typename T>
149  class LoaderT;
150 
151  virtual void initAttributeAccessors() {}
152  virtual void construct(agxData::EntityPtr) {}
153 
154  protected:
155  virtual ~EntityModel();
156 
157  // void registerStorage(EntityStorage *storage);
158  // void unregisterStorage(EntityStorage *storage);
159  void propagateNewAttribute(Attribute *attribute);
160  void addChild(EntityModel *child);
161  void removeChild(EntityModel *child);
162  EntityPtr createInstanceWrapper();
163 
164  private:
165  friend class EntityStorage;
166 
167  struct SharedBuffer
168  {
169  agx::Name name;
170  Format *format;
171  };
172 
173  struct SharedStorage
174  {
175  EntityModel *entity;
176  agx::Name name;
177  };
178 
181  const SharedBufferVector& getSharedBuffers() const;
182  const SharedStorageVector& getSharedStorages() const;
183 
184  void addSharedBuffer(SharedBuffer buffer);
185  void addSharedStorage(SharedStorage storage);
186 
187  private:
188  static void initDefaultEntity();
189 
190  static bool s_threadSafe;
191 
192  // EntityModel *m_parent;
193  EntityModelPtrVector m_children;
194  AttributePtrVector m_attributes;
195  SharedBufferVector m_sharedBuffers;
196  SharedStorageVector m_sharedStorages;
197  EntityStorage *m_defaultStorage;
198  bool m_useInstanceTable;
199  bool m_hasConstructor;
200  // EntityStoragePtrVector m_activeStorages;
201  };
202 
203  class AGXCORE_EXPORT EntityModel::Loader
204  {
205  public:
206  Loader(const char *modelPath);
207  virtual EntityModel *load() = 0;
208  virtual ~Loader();
209  };
210 
211  template <typename T>
213  {
214  public:
215  LoaderT(const char *modelPath) : EntityModel::Loader(modelPath) {}
216  virtual EntityModel *load() { return new T(); }
217  virtual ~LoaderT() {}
218  };
219 
220 
221 
222  /* Implementation */
223  AGX_FORCE_INLINE bool EntityModel::getThreadSafe() { return s_threadSafe; }
224  AGX_FORCE_INLINE bool EntityModel::hasConstructor() const { return m_hasConstructor; }
225 
226 
227  AGX_FORCE_INLINE const AttributePtrVector& EntityModel::getAttributes() const { return m_attributes; }
228  AGX_FORCE_INLINE const EntityModel::SharedBufferVector& EntityModel::getSharedBuffers() const { return m_sharedBuffers; }
229  AGX_FORCE_INLINE const EntityModel::SharedStorageVector& EntityModel::getSharedStorages() const { return m_sharedStorages; }
230 
231  AGX_FORCE_INLINE const EntityModel* EntityModel::getSource() const { return dynamic_cast<const EntityModel *>(Model::getSource()); }
232  AGX_FORCE_INLINE EntityModel* EntityModel::getSource() { return dynamic_cast<EntityModel *>(Model::getSource()); }
233 
234 
235  // Implementation of EntityPtr is located in EntityStorage.h due to include loops
236 }
237 
239 AGX_TYPE_BINDING(agxData::EntityInstance, "EntityInstance")
241 
242 #undef AGX_ENTITY_NAMESPACE
243 #endif /* _AGXDATA_ENTITY_MODEL_H_ */
virtual void initAttributeAccessors()
Definition: EntityModel.h:151
virtual void shutdownCleanup()
Definition: EntityModel.h:141
Representation of a path, a list of name components.
Definition: Path.h:32
agx::Event1< EntityStorage * > StorageEvent
Definition: EntityModel.h:83
static ScalarAttributeT< EntityPtr > * instanceAttribute
Definition: EntityModel.h:67
LoaderT(const char *modelPath)
Definition: EntityModel.h:215
Data storage for a collection of entity instances of a specified EntityModel.
Definition: EntityStorage.h:72
agx::Vector< EntityModel * > EntityModelPtrVector
Definition: EntityModel.h:44
A model is an abstract representation of the class of an agx::Object.
Definition: Model.h:40
#define AGX_TYPE_BINDING(_Type, _Name)
Definition: Type.h:179
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
agx::Vector< Attribute * > AttributePtrVector
Definition: EntityModel.h:47
A format is an implementation of a agxData::Type.
Definition: Format.h:63
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
agx::HashTable< Attribute *, EntityModel * > EntityDependencyTable
Definition: EntityModel.h:48
Representation of a name string.
Definition: Name.h:32
#define AGX_FORCE_INLINE
Definition: Object.h:59
agx::Model * getSource()
Definition: Model.h:220
StorageEvent storageCreationEvent
Definition: EntityModel.h:85
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
Type
Specifies which path to access.
Definition: Environment.h:47
An abstract description of a data entity stored using SOA (structure of arrays) pattern in a EntitySt...
Definition: EntityModel.h:63
virtual EntityModel * load()
Definition: EntityModel.h:216
AGXCORE_EXPORT Component * root()
A namespace is used as a group node in the agx::Model hierarchy.
Definition: Namespace.h:29
Inheritance with partial specialization due to bug with ref_ptr containers.
An agx::Device is an abstract representation of a device on which data can be stored and processed...
Definition: Device.h:57
Specifies paths for where to search for plugin&#39;s/runtime libraries.
Definition: Environment.h:49
virtual void construct(agxData::EntityPtr)
Definition: EntityModel.h:152
#define AGXCORE_EXPORT
AGXCORE_EXPORT bool hasEntity(const agx::Path &path)
AGXCORE_EXPORT EntityModel * getEntity(const agx::Path &path)
Get a entity with a specified path.