AGXDynamics  2.35.0.3
OutputArchive.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. 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:
61 
62  struct IdEntry {
63  IdEntry(agx::UInt32 _id = agx::InvalidIndex) : id(_id), valid(true) {}
65  bool valid;
66  };
67 
70 
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;
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>
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>
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 
agx::UInt64
uint64_t UInt64
Definition: Integer.h:41
agxStream::OutputRef::m_mode
StorageMode m_mode
Definition: OutputArchive.h:341
AGXCORE_EXPORT
#define AGXCORE_EXPORT
agxStream::OutputRef::OutputRef
OutputRef(const char *name, const T *obj, StorageMode mode)
Definition: OutputArchive.h:324
agxStream::OutputRef::OutputRef
OutputRef(const char *name, const T &obj, StorageMode mode)
Definition: OutputArchive.h:317
SPDMatrix3x3.h
agx::Int16
int16_t Int16
Definition: Integer.h:44
agxStream::Archive
Abstract base class for input/output storage of Serializable classes.
Definition: Archive.h:55
Matrix3x3.h
agxStream::OutputArchive::m_numObjectsWritten
int m_numObjectsWritten
Definition: OutputArchive.h:291
agxStream::OutputVal::m_mode
StorageMode m_mode
Definition: OutputArchive.h:354
agx::Vector< char >
agxStream::OutputArchiveRef
agx::ref_ptr< OutputArchive > OutputArchiveRef
Definition: OutputArchive.h:343
agxStream::out
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.
Definition: OutputArchive.h:412
agxStream::OutputVal::OutputVal
OutputVal(const char *name, const T obj)
Definition: OutputArchive.h:349
agxStream::OutputArchive::getNumObjectsSkipped
int getNumObjectsSkipped() const
Definition: OutputArchive.h:109
agxStream::Serializable
This class is an abstract base class for all classes that can be stored and retrieved from an Archive...
Definition: Serializable.h:44
agxStream::OutputArchive::StorageAgentToIdMap
agx::HashTable< StorageAgent *, agx::UInt32 > StorageAgentToIdMap
Definition: OutputArchive.h:69
agxStream::OutputVal::operator=
OutputVal & operator=(const OutputVal &other)
Definition: OutputArchive.h:355
agxStream::OutputArchive::beginSection
virtual void beginSection(const char *) override
Definition: OutputArchive.h:200
HashTable.h
agx::AffineMatrix4x4T
Matrix class for rigid transformations (translation, rotation).
Definition: AffineMatrix4x4.h:52
agxStream::OutputArchive::ObjectToIdMapContext::ObjectToIdMapContext
ObjectToIdMapContext()
Definition: OutputArchive.h:80
agx::Vec4T
A class holding 4 dimensional vectors and providing basic arithmetic.
Definition: Vec4Template.h:34
agxStream::OutputArchive::ObjectToIdMapContext::table
ObjectToIdMap table
Definition: OutputArchive.h:78
Name.h
agx
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
Definition: AddedMassInteraction.h:22
agxStream::OutputArchive::ObjectToIdMapContext
This struct is used to handle id-collisions when using incremental journal.
Definition: OutputArchive.h:76
agxStream::OutputArchive::m_numObjectsSkipped
int m_numObjectsSkipped
Definition: OutputArchive.h:292
agxStream::out_val
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.
Definition: OutputArchive.h:424
Vec2.h
agxStream::StorageMode
StorageMode
Definition: Archive.h:45
agxStream::OutputArchive::storeEvent
StoreEvent storeEvent
Definition: OutputArchive.h:60
agx::UInt16
uint16_t UInt16
Definition: Integer.h:39
Vec3.h
agxStream::OutputArchive::ObjectToIdMap
agx::HashTable< const Serializable *, IdEntry > ObjectToIdMap
Definition: OutputArchive.h:68
agx::ref_ptr< OutputArchive >
agxStream::OutputArchive::IdEntry::IdEntry
IdEntry(agx::UInt32 _id=agx::InvalidIndex)
Definition: OutputArchive.h:63
agx::UInt8
uint8_t UInt8
Definition: Integer.h:38
agxStream::OutputArchive::endSection
virtual void endSection(const char *) override
Definition: OutputArchive.h:201
agxStream::OutputRef::m_name
const char * m_name
Definition: OutputArchive.h:339
agxStream::OutputArchive::IdEntry::valid
bool valid
Definition: OutputArchive.h:65
agxStream::OutputArchive::getNumObjectsWritten
int getNumObjectsWritten() const
Definition: OutputArchive.h:102
agxStream::OutputVal::m_name
const char * m_name
Definition: OutputArchive.h:351
agxStream::OutputArchive::ObjectToIdMapContext::idCounter
agx::UInt32 idCounter
Definition: OutputArchive.h:77
agxStream::OutputArchive::IdEntry
Definition: OutputArchive.h:62
agxStream::OutputArchive::getStream
std::ostream & getStream()
Return a reference to the output stream.
Definition: OutputArchive.h:92
DECLARE_OUTPUT_BASIC_TYPE
#define DECLARE_OUTPUT_BASIC_TYPE(TYPE)
Definition: OutputArchive.h:359
Archive.h
agxStream::operator<<
OutputArchive & operator<<(OutputArchive &out, OutputRef< T > val)
Definition: OutputArchive.h:428
agx::LinearProbingHashTable
Inheritance with partial specialization due to bug with ref_ptr containers.
Definition: LinearProbingHashTable.h:871
agx::Int8
int8_t Int8
Definition: Integer.h:43
Range.h
agx::Vec2T
Definition: Vec2Template.h:30
agxCore_export.h
agx::Name
Representation of a name string.
Definition: Name.h:32
agxStream::write
bool AGXPHYSICS_EXPORT write(const std::string &filename, const agxStream::Serializable *data)
Write a Serializable object to a file.
agxStream::OutputVal::m_obj
T m_obj
Definition: OutputArchive.h:352
agxStream::OutputArchive
Class for writing serialized data in binary format to a stream.
Definition: OutputArchive.h:56
agx::SPDMatrix3x3
Specialized type of matrices for holding symmetric positive definite matrices.
Definition: SPDMatrix3x3.h:48
agx::HashSet
Inheritance with partial specialization due to bug with ref_ptr containers.
Definition: HashSet.h:665
agx::QuatT< Real >
agxStream::OutputArchive::getEndianMatch
bool getEndianMatch() const
Definition: OutputArchive.h:196
agxStream::InputArchive
Class for reading a binary stream of serialized data.
Definition: InputArchive.h:51
agxStream::OutputRef::operator=
OutputRef & operator=(const OutputRef &other)
Definition: OutputArchive.h:331
agxStream::OutputArchive::m_defaultCache
ObjectToIdMapContext m_defaultCache
Definition: OutputArchive.h:286
Uuid.h
Quat.h
agxStream::OutputVal
Definition: OutputArchive.h:348
agxStream::OutputArchive::m_objectToIdMap
ObjectToIdMapContext & m_objectToIdMap
Definition: OutputArchive.h:287
agxStream::OutputArchive::write
virtual void write(const agx::Name &val)
Definition: OutputArchive.h:148
agxStream
This namespace contain classes for streaming classes into archives, ASCII, binary for storage (serial...
Definition: ElementaryConstraint.h:31
agxStream::STORE_BY_VALUE
@ STORE_BY_VALUE
Definition: Archive.h:46
agx::Int64
int64_t Int64
Definition: Integer.h:46
String.h
agxStream::OutputArchive::m_outputStream
std::ostream & m_outputStream
Definition: OutputArchive.h:285
agx::AgXString
Definition: String.h:31
agxStream::OutputArchive::IdEntry::id
agx::UInt32 id
Definition: OutputArchive.h:64
agx::Range< Real >
agx::Vec3T< Real32 >
agx::InvalidIndex
const AGXCORE_EXPORT InvalidIndexStruct InvalidIndex
agx::Uuid
A UUID, or Universally unique identifier, is intended to uniquely identify information in a distribut...
Definition: Uuid.h:41
agxStream::OutputArchive::m_storageAgentToIdMap
StorageAgentToIdMap m_storageAgentToIdMap
Definition: OutputArchive.h:288
agxStream::OutputRef
Definition: OutputArchive.h:315
agx::Matrix3x3T
Definition: EulerAngles.h:31
agxStream::OutputArchive::StoreEvent
agx::Event2< const Serializable *, agx::UInt32 > StoreEvent
Definition: OutputArchive.h:59
agx::Int32
int32_t Int32
Definition: Integer.h:45
agx::UInt32
uint32_t UInt32
Definition: Integer.h:40
agx::Event2< const Serializable *, agx::UInt32 >
agxStream::STORE_BY_REF
@ STORE_BY_REF
Definition: Archive.h:47
agxStream::OutputRef::m_obj
const T * m_obj
Definition: OutputArchive.h:340