AGXDynamics  2.31.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Serializable.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2021. 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 <agx/agxCore_export.h>
19 
20 #include <agx/Integer.h>
21 #include <functional>
22 #include <agxStream/StorageAgent.h>
23 #include <agx/Uuid.h>
24 
25 namespace agxStream
26 {
27  class InputArchive;
28  class OutputArchive;
29  class StorageAgent;
30  class StorageStream;
31 
45  {
46  public:
47 
48 #if !defined(SWIG)
49  virtual ~Serializable();
51 #endif
52 
56  void setFinished();
57 
61  bool isFinished() const;
62 
64  agx::Uuid getUuid() const;
65 
70  void setUuid( const agx::Uuid& uuid );
71 
79  agx::UInt32 getIndex() const;
80 
85  void setEnableSerialization( bool flag );
86 
90  bool getEnableSerialization( ) const;
91 
92 #ifndef SWIG
93 
96  virtual StorageAgent* getStorageAgent() const = 0;
97 
99  virtual const char* getClassName() const;
100 #endif // SWIG
101 
103  static void setEnableUuidGeneration( bool flag );
104 
106  bool getEnableUuidGeneration( );
107 
108  public:
110 
111 #ifndef SWIG
112 
115  virtual void storeLightData( agxStream::StorageStream& ) const {}
116 
120  virtual void restoreLightData( agxStream::StorageStream& ) {}
121 
122  bool _store( agxStream::OutputArchive& out ) const;
123  void _restore( agxStream::InputArchive& in );
125 #endif
126  protected:
127 
132 #if defined(SWIG)
133  virtual ~Serializable();
134 #endif
135 
137 
138 
143  void setDynamicallyAllocated();
144 
145  bool isDynamicallyAllocated() const;
146 
147 
148 #ifndef SWIG
149  virtual void store( agxStream::OutputArchive& out ) const = 0;
150  virtual void restore( agxStream::InputArchive& in ) = 0;
151 #endif
152 
153  friend class InputArchive;
154  friend class OutputArchive;
155 
157  void setArchive( InputArchive* archive );
158 
160  InputArchive* getArchive( );
162 
163  private:
164  int m_flags;
165  static StorageAgent* s_storageAgent;
166 
167  enum {
168  FINISHED = 1,
169  DYNAMICALLY_ALLOCATED = 2
170  };
171 
172  protected:
174  Serializable();
175 
176  void generateUuid();
177 
179  Serializable(const Serializable& other);
180 
181  private:
182  InputArchive* m_archive;
183  agx::Uuid m_uuid;
184  agx::UInt32 m_index;
185  bool m_serializationEnabled;
186  static bool s_uuidGeneration_enabled;
187  };
188 #ifndef SWIG
189 #define AGXSTREAM_CLASS_NAME(T) \
191  inline const char* getClassName() const override { return #T; } \
192 
193 
194 #define AGXSTREAM_DECLARE_SERIALIZABLE_BASE( T ) \
195  inline agxStream::StorageAgent* getStorageAgent() const override { return agxStream::StorageManager::instance()->find( #T ); } \
196  inline static const char* getConstructClassId() { return #T; } \
197  friend class agxStream::DefStorageAgent<T>; \
198  AGXSTREAM_CLASS_NAME(T) \
199  static agxStream::Serializable *create() { return new T(); } \
200  virtual void store( agxStream::OutputArchive& out ) const override;\
201  virtual void restore( agxStream::InputArchive& in ) override
202 
206 #define AGXSTREAM_DECLARE_SERIALIZABLE( T ) \
207  AGXSTREAM_DECLARE_SERIALIZABLE_BASE(T); \
208  static agxStream::Serializable *create(agxStream::InputArchive& /*in*/) { return new T(); }
209 
213 #define AGXSTREAM_DECLARE_SERIALIZABLE_CUSTOM_CREATE( T ) \
214  AGXSTREAM_DECLARE_SERIALIZABLE_BASE(T); \
215  static agxStream::Serializable *create(agxStream::InputArchive& in)
216 
220 #define AGXSTREAM_DECLARE_ABSTRACT_SERIALIZABLE( T ) \
221  inline agxStream::StorageAgent* getStorageAgent() const override { return agxStream::StorageManager::instance()->find(#T); } \
222  inline static const char* getConstructClassId() { return #T; } \
223  AGXSTREAM_CLASS_NAME(T)
224 
226 #define AGXSTREAM_INSTANTIATE_STORAGE( C ) \
227  static agxStream::Storage<C> storage ## C;
228 
230 #define AGXSTREAM_INSTANTIATE_STORAGE_VARIABLE( V,C ) \
231  static agxStream::Storage<C> storage ## V;
232 
233  // Macro for delaying expansion of comma (,) in a macro argument. This allows for submitting arguments such as: "first", "second" etc..
234 #define AGXARCHIVE_MODIFICATIONS(...) {__VA_ARGS__}
235 
236 
243 #define AGXSTREAM_INSTANTIATE_STORAGE_DEPRECATION_CHECK_VARIABLE( V, DEFAULT_CLASS, DEPRECATED_CLASS, MODIFICATION_LIST ) \
244  static agxStream::DeprecatedStorage<DEFAULT_CLASS, DEPRECATED_CLASS> V(MODIFICATION_LIST);
245 
246 
248  {
249  return m_index;
250  }
251 
253  {
254  return (m_flags & FINISHED) != 0;
255  }
256 
257  AGX_FORCE_INLINE bool Serializable::isDynamicallyAllocated() const
258  {
259  return (m_flags & DYNAMICALLY_ALLOCATED) != 0;
260  }
261 
262 
263  AGX_FORCE_INLINE InputArchive* Serializable::getArchive()
264  {
265  return m_archive;
266  }
267 
269  {
270  return m_uuid;
271  }
272 
274  {
275  return m_serializationEnabled;
276  }
277 
278 #else
279 
280  // Prevent swig from seeing macro usage of hidden macros
281  #define AGXSTREAM_DECLARE_SERIALIZABLE( T )
282  #define AGXSTREAM_DECLARE_ABSTRACT_SERIALIZABLE( T )
283  #define AGXSTREAM_DECLARE_SERIALIZABLE_CUSTOM_CREATE( T )
284 
285 #endif // SWIG
286 } // Namespace agxStream
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
OutputRef< Serializable > out(const char *name, const Serializable *obj)
Return an object that contain the name and the object that should be serialized to an archive...
#define AGX_FORCE_INLINE
Definition: Object.h:62
bool isFinished() const
Definition: Serializable.h:252
agx::UInt32 getIndex() const
This index is given at creation of this object.
Definition: Serializable.h:247
Class for reading a binary stream of serialized data.
Definition: InputArchive.h:51
Abstract base class for storing/restoring a line/drums with version control.
Definition: StorageStream.h:48
A UUID, or Universally unique identifier, is intended to uniquely identify information in a distribut...
Definition: Uuid.h:41
This class is an abstract base class for all classes that can be stored and retrieved from an Archive...
Definition: Serializable.h:44
Class for writing serialized data in binary format to a stream.
Definition: OutputArchive.h:56
agx::Uuid getUuid() const
Definition: Serializable.h:268
bool getEnableSerialization() const
Definition: Serializable.h:273
uint32_t UInt32
Definition: Integer.h:40
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:101
#define AGXCORE_EXPORT
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:100