AGX Dynamics 2.42.2.1
Loading...
Searching...
No Matches
StorageAgent.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#pragma once
17
18#include <agxStream/agxStream.h>
19#include <agx/DynamicLibrary.h>
20#include <agx/Singleton.h>
21
22#include <string>
23#include <agx/HashTable.h>
24#include <agx/Referenced.h>
25
27
28
29namespace agxStream
30{
31 class Serializable;
32 class InputArchive;
33
35 class AGXCORE_EXPORT StorageAgent
36 {
37 private:
38 std::string m_className;
39
40 // copy constructor and assignment operator prohibited
41 StorageAgent(const StorageAgent&);
42 StorageAgent& operator=(const StorageAgent&);
43
44
45 protected:
46 StorageAgent(const char* className);
47
48 public:
50 virtual ~StorageAgent();
51
53 const char* getClassName() const;
54
55 //virtual Serializable* createInstance() = 0;
56 virtual Serializable* createInstance(InputArchive& in) = 0;
57
63 static StorageAgent* lookup( const char* className, const agxStream::InputArchive* archive );
64
65 static std::string stripClassName(const char* functionName, const char* className);
66
68 virtual bool isBaseOf(const agxStream::Serializable* /*derived*/) const = 0;
69 };
70
71 template< class T >
72 class DefStorageAgent : public StorageAgent
73 {
74 public:
75 DefStorageAgent() : StorageAgent(T::getConstructClassId()) { }
76 virtual ~DefStorageAgent() { }
77
78 virtual Serializable* createInstance(InputArchive& in) override {
79 return T::create(in);
80 }
81
82 virtual bool isBaseOf(const agxStream::Serializable* derived) const override {
83 return dynamic_cast<const T*>(derived) != nullptr;
84 }
85 };
86
87 AGX_FORCE_INLINE const char* StorageAgent::getClassName() const
88 {
89 return m_className.c_str();
90 }
91
92
93 AGX_DECLARE_POINTER_TYPES(StorageManager);
94 class AGXCORE_EXPORT StorageManager
95 {
96 public:
97
98 StorageManager();
99 ~StorageManager();
100
101 static StorageManager* instance();
102 void cleanup();
103
104 void shutdown() { cleanup(); }
105
106 struct ProxyBase {};
107
108 template<class T>
109 class RegisterStorageProxy : public ProxyBase
110 {
111 public:
112
113 RegisterStorageProxy() {
114 if (StorageManager::instance()) {
115 m_registerStorageProxy = new T;
116 StorageManager::instance()->add(m_registerStorageProxy);
117
118 }
119 }
120
121 virtual ~RegisterStorageProxy() {
122 delete m_registerStorageProxy;
123 }
124
125 T* getStorageAgent() const {
126 return m_registerStorageProxy;
127 }
128
129 protected:
130 T* m_registerStorageProxy;
131 };
132
133 class AGXCORE_EXPORT ModificationCallback : public agx::Referenced
134 {
135 public:
136 ModificationCallback(const agx::StringVector& modifications);
137 ModificationCallback();
138
139 bool operator() (const agxStream::InputArchive* archive) const;
140
141 private:
142 ~ModificationCallback();
143
144 agx::StringVector m_modifications;
145 };
146
147
148
153 template<class T, class Deprecated_T>
154 class DeprecatedRegisterStorageProxy : public ProxyBase
155 {
156 public:
157
158
159 DeprecatedRegisterStorageProxy(const agx::StringVector& modificationList) {
160 if (StorageManager::instance()) {
161
162 // Add the StorageAgent to the list of storage agents for the StorageManager
163 m_registerStorageProxy = new T;
164 m_deprecatedRegisterStorageProxy = new Deprecated_T;
165 StorageManager::instance()->add(m_registerStorageProxy, modificationList, m_deprecatedRegisterStorageProxy);
166 }
167 }
168
169 virtual ~DeprecatedRegisterStorageProxy() {
170 delete m_registerStorageProxy;
171
172 if (m_deprecatedRegisterStorageProxy)
173 delete m_deprecatedRegisterStorageProxy;
174 }
175
176
177 protected:
178 T* m_registerStorageProxy;
179 Deprecated_T* m_deprecatedRegisterStorageProxy;
180 };
181
182
184 void add(const char *className, StorageAgent* agent);
185
187 void add(StorageAgent* agent);
188
190 void add(StorageAgent* defaultAgent, const agx::StringVector& modificationList, StorageAgent* deprecatedAgent);
191
193 void remove(StorageAgent* agent);
194
196 StorageAgent* find(const char* className, const agxStream::InputArchive *archive = nullptr);
197
198 protected:
199
201
202
203 // This class stores a StorageAgent for a class
204 // In addition it can store another StorageAgent which is a deprecated fallback that will be used IF the callback returns true.
205 class AgentStorage : public agx::Referenced {
206 public:
207 AgentStorage(StorageAgent* defaultAgent, const agx::StringVector& modificationList, StorageAgent* deprecatedAgent);
208
209 StorageAgent* getStorageAgent(const agxStream::InputArchive* archive);
210
211 private:
213 StorageAgent* m_defaultAgent;
214 StorageAgent* m_deprecatedAgent;
215
216 private:
217 ~AgentStorage();
218 };
219
220 public:
222
223 private:
224 StorageAgentMap m_storageAgents;
225
226 };
227
228
236 template <typename DefaultClass_T, typename DeprecatedClass_T>
237 struct DeprecatedStorage : public StorageManager::DeprecatedRegisterStorageProxy< DefStorageAgent<DefaultClass_T>, DefStorageAgent<DeprecatedClass_T> >
238 {
239 DeprecatedStorage(const agx::StringVector& modificationList) : StorageManager::DeprecatedRegisterStorageProxy< DefStorageAgent<DefaultClass_T>, DefStorageAgent<DeprecatedClass_T> >(modificationList)
240 {
241 }
242
243 ~DeprecatedStorage() {}
244 };
245
246
251 template <typename T>
252 struct Storage : public StorageManager::RegisterStorageProxy< DefStorageAgent<T> >
253 {
254 Storage()
255 {
256 }
257
258 ~Storage()
259 {
260 }
261 };
262
263
264} // namespace
265
267
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
#define AGXCORE_EXPORT
Class for reading a binary stream of serialized data.
Definition: InputArchive.h:51
This class is an abstract base class for all classes that can be stored and retrieved from an Archive...
Definition: Serializable.h:44
Inheritance with partial specialization due to bug with ref_ptr containers.
Base class providing referencing counted objects.
Definition: Referenced.h:120
Smart pointer for handling referenced counted objects.
Definition: ref_ptr.h:30
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: macros.h:89
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:88
#define AGX_FORCE_INLINE
Definition: macros.h:58
This namespace contain classes for streaming classes into archives, ASCII, binary for storage (serial...
agx::ref_ptr< DynamicLibrary > DynamicLibraryRef
void AGXPHYSICS_EXPORT shutdown()
Shutdown of the AGX Dynamics API will be done when the number of shutdown matches the number of calls...