AGX Dynamics  2.37.2.0
StorageAgent.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 #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 
29 namespace 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 
67  };
68 
69  template< class T >
70  class DefStorageAgent : public StorageAgent
71  {
72  public:
73  DefStorageAgent() : StorageAgent(T::getConstructClassId()) { }
74  virtual ~DefStorageAgent() { }
75 
76  virtual Serializable* createInstance(InputArchive& in) override {
77  return T::create(in);
78  }
79  };
80 
81  AGX_FORCE_INLINE const char* StorageAgent::getClassName() const
82  {
83  return m_className.c_str();
84  }
85 
86 
87  AGX_DECLARE_POINTER_TYPES(StorageManager);
88  class AGXCORE_EXPORT StorageManager
89  {
90  public:
91 
92  StorageManager();
93  ~StorageManager();
94 
95  static StorageManager* instance();
96  void cleanup();
97 
98  void shutdown() { cleanup(); }
99 
100  struct ProxyBase {};
101 
102  template<class T>
103  class RegisterStorageProxy : public ProxyBase
104  {
105  public:
106 
107  RegisterStorageProxy() {
108  if (StorageManager::instance()) {
109  m_registerStorageProxy = new T;
110  StorageManager::instance()->add(m_registerStorageProxy);
111 
112  }
113  }
114 
115  virtual ~RegisterStorageProxy() {
116  delete m_registerStorageProxy;
117  }
118 
119  T* getStorageAgent() const {
120  return m_registerStorageProxy;
121  }
122 
123  protected:
124  T* m_registerStorageProxy;
125  };
126 
127  class AGXCORE_EXPORT ModificationCallback : public agx::Referenced
128  {
129  public:
130  ModificationCallback(const agx::StringVector& modifications);
131  ModificationCallback();
132 
133  bool operator() (const agxStream::InputArchive* archive) const;
134 
135  private:
136  ~ModificationCallback();
137 
138  agx::StringVector m_modifications;
139  };
140 
141 
142 
147  template<class T, class Deprecated_T>
148  class DeprecatedRegisterStorageProxy : public ProxyBase
149  {
150  public:
151 
152 
153  DeprecatedRegisterStorageProxy(const agx::StringVector& modificationList) {
154  if (StorageManager::instance()) {
155 
156  // Add the StorageAgent to the list of storage agents for the StorageManager
157  m_registerStorageProxy = new T;
158  m_deprecatedRegisterStorageProxy = new Deprecated_T;
159  StorageManager::instance()->add(m_registerStorageProxy, modificationList, m_deprecatedRegisterStorageProxy);
160  }
161  }
162 
163  virtual ~DeprecatedRegisterStorageProxy() {
164  delete m_registerStorageProxy;
165 
166  if (m_deprecatedRegisterStorageProxy)
167  delete m_deprecatedRegisterStorageProxy;
168  }
169 
170 
171  protected:
172  T* m_registerStorageProxy;
173  Deprecated_T* m_deprecatedRegisterStorageProxy;
174  };
175 
176 
178  void add(const char *className, StorageAgent* agent);
179 
181  void add(StorageAgent* agent);
182 
184  void add(StorageAgent* defaultAgent, const agx::StringVector& modificationList, StorageAgent* deprecatedAgent);
185 
187  void remove(StorageAgent* agent);
188 
190  StorageAgent* find(const char* className, const agxStream::InputArchive *archive = nullptr);
191 
192  protected:
193 
195 
196 
197  // This class stores a StorageAgent for a class
198  // In addition it can store another StorageAgent which is a deprecated fallback that will be used IF the callback returns true.
199  class AgentStorage : public agx::Referenced {
200  public:
201  AgentStorage(StorageAgent* defaultAgent, const agx::StringVector& modificationList, StorageAgent* deprecatedAgent);
202 
203  StorageAgent* getStorageAgent(const agxStream::InputArchive* archive);
204 
205  private:
207  StorageAgent* m_defaultAgent;
208  StorageAgent* m_deprecatedAgent;
209 
210  private:
211  ~AgentStorage();
212  };
213 
215  StorageAgentMap m_storageAgents;
216 
217  };
218 
219 
227  template <typename DefaultClass_T, typename DeprecatedClass_T>
228  struct DeprecatedStorage : public StorageManager::DeprecatedRegisterStorageProxy< DefStorageAgent<DefaultClass_T>, DefStorageAgent<DeprecatedClass_T> >
229  {
230  DeprecatedStorage(const agx::StringVector& modificationList) : StorageManager::DeprecatedRegisterStorageProxy< DefStorageAgent<DefaultClass_T>, DefStorageAgent<DeprecatedClass_T> >(modificationList)
231  {
232  }
233 
234  ~DeprecatedStorage() {}
235  };
236 
237 
242  template <typename T>
243  struct Storage : public StorageManager::RegisterStorageProxy< DefStorageAgent<T> >
244  {
245  Storage()
246  {
247  }
248 
249  ~Storage()
250  {
251  }
252  };
253 
254 
255 } // namespace
256 
258 
InputRef< T > in(const char *name, T &obj)
Create an object with a name and a reference to the object that should be restored (usually a pointer...
Definition: InputArchive.h:400
void AGXPHYSICS_EXPORT shutdown()
Shutdown of the AGX Dynamics API will be done when the number of shutdown matches the number of calls...
agx::ref_ptr< DynamicLibrary > DynamicLibraryRef
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
#define AGX_FORCE_INLINE
Definition: Object.h:59
Base class providing referencing counted objects.
Definition: Referenced.h:119
Smart pointer for handling referenced counted objects.
Definition: ref_ptr.h:29
Class for reading a binary stream of serialized data.
Definition: InputArchive.h:51
This namespace contain classes for streaming classes into archives, ASCII, binary for storage (serial...
Inheritance with partial specialization due to bug with ref_ptr containers.
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:98
#define AGXCORE_EXPORT
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:97