AGX Dynamics  2.37.3.2
OutputArchive.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 
19 
20 #include <agx/agxCore_export.h>
21 
22 #include <string>
23 #include <agx/HashTable.h>
24 #include <agx/Name.h>
25 
26 #include <agxStream/Archive.h>
27 #include <agx/String.h>
28 #include <agx/Vec2.h>
29 #include <agx/Vec3.h>
30 #include <agx/Quat.h>
31 #include <agx/Range.h>
32 #include <agx/Matrix3x3.h>
33 #include <agx/SPDMatrix3x3.h>
34 #include <agx/Uuid.h>
35 
36 namespace agx
37 {
38  class Uuid;
39 }
40 
41 namespace agxStream
42 {
43 
44  class StorageAgent;
45  class Serializable;
46 
47 
48 
57  {
58  public:
60  StoreEvent storeEvent;
61 
62  struct IdEntry {
63  IdEntry(agx::UInt32 _id = agx::InvalidIndex) : id(_id), valid(true) {}
65  bool valid;
66  };
67 
70 
78  ObjectToIdMap table;
79 
80  ObjectToIdMapContext() : idCounter(0) {}
81  };
82 
83  public:
84 
88  OutputArchive( std::ostream& outputStream );
89  OutputArchive( std::ostream& outputStream, ObjectToIdMapContext& cache );
90 
92  std::ostream& getStream() { return m_outputStream; }
93 
97  void open(const std::string& appName);
98 
102  int getNumObjectsWritten() const {
103  return m_numObjectsWritten;
104  }
105 
109  int getNumObjectsSkipped() const {
110  return m_numObjectsSkipped;
111  }
112 
117  static void setUseCurrentDate(bool flag);
118 
123  bool getUseCurrentDate();
124 
125  virtual void write(char val);
126  virtual void write(bool val);
127  virtual void write(agx::UInt8 val);
128  virtual void write(agx::Int8 val);
129 
130  virtual void write(agx::Int16 val);
131  virtual void write(agx::UInt16 val);
132 
133  virtual void write(agx::Int32 val);
134  virtual void write(agx::UInt32 val);
135 
136  virtual void write(agx::Int64 val);
137  virtual void write(agx::UInt64 val);
138 #ifdef __APPLE__
139  virtual void write(size_t val) { write((agx::UInt64)val); }
140 #endif
141 
142 
143  virtual void write(float val);
144  virtual void write(double val);
145 
146  virtual void write(const std::string& val);
147  virtual void write(const agx::String& val);
148  virtual void write(const agx::Name& val)
149  {
150  agx::String tmp = val;
151  write(tmp);
152  }
153  virtual void write(const agx::Uuid& val);
154 
155 
156  void write(const agx::RangeReal& val);
157  void write(const agx::Vec2f& val);
158  void write(const agx::Vec2d& val);
159  void write(const agx::Vec2i32& val);
160  void write(const agx::Vec2i64& val);
161 
162  void write(const agx::Vec3f& val);
163  void write(const agx::Vec3d& val);
164  void write(const agx::Vec3i32& val);
165  void write(const agx::Vec3i64& val);
166  void write(const agx::Vec4f& val);
167  void write(const agx::Vec4d& val);
168  void write(const agx::Quat& val);
169  void write(const agx::SPDMatrix3x3& val);
170  void write(const agx::Matrix3x3d& val);
171  void write(const agx::Matrix3x3f& val);
172  void write(const agx::AffineMatrix4x4f& val);
173  void write(const agx::AffineMatrix4x4d& val);
174 
175  void write(const Serializable *serializable, StorageMode mode);
176  void write(const Serializable &serializable, StorageMode mode);
177 
178 
179  virtual void addAttribute(const char *name, bool value);
180  virtual void addAttribute(const char *name, const char* value);
181  virtual void addAttribute(const char *name, char value);
182  virtual void addAttribute(const char *name, agx::Int8 value);
183  virtual void addAttribute(const char *name, agx::UInt8 value);
184  virtual void addAttribute(const char *name, agx::UInt16 value);
185  virtual void addAttribute(const char *name, agx::Int32 value);
186  virtual void addAttribute(const char *name, agx::UInt32 value);
187  virtual void addAttribute(const char *name, agx::UInt64 value);
188  virtual void addAttribute(const char *name, float value);
189  virtual void addAttribute(const char *name, double value);
190  virtual void addAttribute(const char *name, const agx::Uuid& value);
191 
192 #ifdef __APPLE__
193  virtual void addAttribute(const char *name, size_t value) { addAttribute(name, (agx::UInt64)value); }
194 #endif
195 
196  bool getEndianMatch() const {
197  return m_endianMatch == 1;
198  }
199 
200  virtual void beginSection(const char *) override {}
201  virtual void endSection(const char *) override {}
202 
203  virtual void writeObjectByRef(const Serializable* obj);
204  virtual void writeObjectByVal(const Serializable& obj);
205 
214  virtual void write(const void* buf, size_t len);
215 
217  void flush();
218 
220  void close() override;
221 
226  void setWriteBufferSize(agx::UInt32 bufferSize);
227 
229  agx::UInt32 getWriteBufferSize() const;
230 
236  bool addModification( const char *str);
237 
241  bool hasModification(const agx::String& modification) const;
242 
248  template<typename VECTOR_TYPE>
249  AGXCORE_EXPORT void write_vector(const agx::String& name, const VECTOR_TYPE& val);
250 
251  protected:
252 
253  // Need this to verify the modifications
254  friend class InputArchive;
255 
256  // This constructor is only used by InputArchive during modification verification
257  OutputArchive();
258 
260  virtual ~OutputArchive();
261 
262  virtual void writeHeaderData(agx::UInt32 magic,
263  const std::string& appName,
264  agx::UInt8 generationVersion,
265  agx::UInt8 majorVersion,
266  agx::UInt8 minorVersion,
267  agx::UInt8 patchVersion,
268  const std::string& revision,
269  agx::UInt16 serializationVersion);
270 
271  virtual void writeArchiveHeader(const std::string& appName,
272  agx::UInt8 generationVersion,
273  agx::UInt8 majorVersion,
274  agx::UInt8 minorVersion,
275  agx::UInt8 patchVersion,
276  const std::string& revision,
277  agx::UInt16 serializationVersion);
278 
279  virtual void writeObjectHeader(StorageMode mode, StorageAgent* agent);
280  virtual void writeTypeHeader(StorageAgent* agent);
281 
282 
283  protected:
284  void writeBitSize();
285  std::ostream& m_outputStream;
288  StorageAgentToIdMap m_storageAgentToIdMap;
289 
290 
291  int m_numObjectsWritten; // for diagnostic purposes
293 
294  // copy constructor and assignment operator prohibited
296  OutputArchive& operator=(const OutputArchive&);
297 
298  private:
299  char m_endianMatch;
300  agx::Vector<char> m_buffer;
301  agx::UInt32 m_bufferPosition;
302  agx::UInt32 m_bufferSize;
303 
304  void addModifications();
305  void writeModificationData();
306  bool m_hasAddedModifications;
307  static bool s_useCurrentDate;
308 
309  agx::HashSet<agx::String> m_modificationHash;
310  };
311 
312 
313 
314  template <typename T>
315  struct OutputRef
316  {
317  OutputRef(const char *name, const T& obj, StorageMode mode) :
318  m_name(name),
319  m_obj(&obj),
320  m_mode(mode)
321  {
322  }
323 
324  OutputRef(const char *name, const T* obj, StorageMode mode) :
325  m_name(name),
326  m_obj(obj),
327  m_mode(mode)
328  {
329  }
330 
332  {
333  m_obj = other.m_obj;
334  m_name = other.m_name;
335  m_mode = other.m_mode;
336  return *this;
337  }
338 
339  const char *m_name;
340  const T *m_obj;
342  };
344 
345 
346 
347  template <typename T>
348  struct OutputVal {
349  OutputVal(const char *name, const T obj) : m_name(name), m_obj(obj), m_mode(STORE_BY_VALUE) {}
350 
351  const char *m_name;
352  T m_obj;
353 
355  OutputVal & operator=(const OutputVal& other) { m_obj = other.m_obj; m_name = other.m_name; m_mode = other.m_mode; return *this; }
356  };
357 
358 
359 #define DECLARE_OUTPUT_BASIC_TYPE(TYPE) \
360  AGXCORE_EXPORT inline agxStream::OutputArchive& operator<<( OutputArchive& out, OutputVal<TYPE> val ) \
361  { \
362  out.beginSection(val.m_name); \
363  out.write( val.m_obj ); \
364  out.endSection(val.m_name); \
365  return out;\
366  }\
367  AGXCORE_EXPORT inline OutputVal<TYPE> out(const char *name, TYPE obj) { return OutputVal<TYPE>(name, obj); }
368 
369  DECLARE_OUTPUT_BASIC_TYPE(std::string)
382 #ifdef __APPLE__
384 #endif
404 
405 
406 
412  inline OutputRef<Serializable> out(const char *name, const Serializable* obj)
413  {
414  return OutputRef<Serializable>(name, obj, STORE_BY_REF);
415  }
416 
423  template <typename T>
424  OutputRef<T> out_val(const char *name, const T& obj) { return OutputRef<T>(name, obj, STORE_BY_VALUE); }
425 
426 
427  template <typename T>
428  inline OutputArchive& operator<<(OutputArchive& out, OutputRef<T> val)
429  {
430  out.beginSection(val.m_name);
431  out.write(val.m_obj, val.m_mode);
432  out.endSection(val.m_name);
433  return out;
434  }
435 
436  template <typename T>
437  inline OutputArchive& operator<<(OutputArchive& out, OutputVal<T> val)
438  {
439  out.beginSection(val.m_name);
440  out.write(val.m_obj);
441  out.endSection(val.m_name);
442  return out;
443  }
444 }
445 
int64_t Int64
Definition: Integer.h:46
Inheritance with partial specialization due to bug with ref_ptr containers.
Definition: HashSet.h:669
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...
int32_t Int32
Definition: Integer.h:45
A class holding 4 dimensional vectors and providing basic arithmetic.
Definition: Vec4Template.h:34
ObjectToIdMapContext & m_objectToIdMap
OutputRef(const char *name, const T &obj, StorageMode mode)
uint8_t UInt8
Definition: Integer.h:38
StorageAgentToIdMap m_storageAgentToIdMap
AGXCORE_EXPORT const InvalidIndexStruct InvalidIndex
OutputRef< T > out_val(const char *name, const T &obj)
Return an object that contain the name and the object that should be serialized to an archive...
Representation of a name string.
Definition: Name.h:32
agx::Event2< const Serializable *, agx::UInt32 > StoreEvent
Definition: OutputArchive.h:59
This struct is used to handle id-collisions when using incremental journal.
Definition: OutputArchive.h:76
#define DECLARE_OUTPUT_BASIC_TYPE(TYPE)
agx::HashTable< StorageAgent *, agx::UInt32 > StorageAgentToIdMap
Definition: OutputArchive.h:69
OutputVal & operator=(const OutputVal &other)
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
virtual void write(const agx::Name &val)
Matrix class for rigid transformations (translation, rotation).
Specialized type of matrices for holding symmetric positive definite matrices.
Definition: SPDMatrix3x3.h:48
int getNumObjectsSkipped() const
Abstract base class for input/output storage of Serializable classes.
Definition: Archive.h:55
bool AGXPHYSICS_EXPORT write(const std::string &filename, const agxStream::Serializable *data)
Write a Serializable object to a file.
int8_t Int8
Definition: Integer.h:43
Class for reading a binary stream of serialized data.
Definition: InputArchive.h:51
std::ostream & getStream()
Return a reference to the output stream.
Definition: OutputArchive.h:92
A UUID, or Universally unique identifier, is intended to uniquely identify information in a distribut...
Definition: Uuid.h:41
StorageMode
Definition: Archive.h:45
uint64_t UInt64
Definition: Integer.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
IdEntry(agx::UInt32 _id=agx::InvalidIndex)
Definition: OutputArchive.h:63
OutputRef & operator=(const OutputRef &other)
int16_t Int16
Definition: Integer.h:44
uint16_t UInt16
Definition: Integer.h:39
This namespace contain classes for streaming classes into archives, ASCII, binary for storage (serial...
OutputRef(const char *name, const T *obj, StorageMode mode)
Inheritance with partial specialization due to bug with ref_ptr containers.
ObjectToIdMapContext m_defaultCache
virtual void beginSection(const char *) override
uint32_t UInt32
Definition: Integer.h:40
agx::ref_ptr< OutputArchive > OutputArchiveRef
virtual void endSection(const char *) override
#define AGXCORE_EXPORT
int getNumObjectsWritten() const
std::ostream & m_outputStream
OutputVal(const char *name, const T obj)
agx::HashTable< const Serializable *, IdEntry > ObjectToIdMap
Definition: OutputArchive.h:68