AGXDynamics  2.31.1.0
OutputArchive.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 
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 
71  public:
72 
76  OutputArchive( std::ostream& outputStream );
77  OutputArchive( std::ostream& outputStream, ObjectToIdMap& cache );
78 
80  std::ostream& getStream() { return m_outputStream; }
81 
85  void open(const std::string& appName);
86 
87  const ObjectToIdMap& getObjectToIdMap() const;
88  void setObjectToIdMap(const ObjectToIdMap& map);
89 
93  int getNumObjectsWritten() const {
94  return m_numObjectsWritten;
95  }
96 
100  int getNumObjectsSkipped() const {
101  return m_numObjectsSkipped;
102  }
103 
108  static void setUseCurrentDate(bool flag);
109 
114  bool getUseCurrentDate();
115 
116  virtual void write(char val);
117  virtual void write(bool val);
118  virtual void write(agx::UInt8 val);
119  virtual void write(agx::Int8 val);
120 
121  virtual void write(agx::Int16 val);
122  virtual void write(agx::UInt16 val);
123 
124  virtual void write(agx::Int32 val);
125  virtual void write(agx::UInt32 val);
126 
127  virtual void write(agx::Int64 val);
128  virtual void write(agx::UInt64 val);
129 #ifdef __APPLE__
130  virtual void write(size_t val) { write((agx::UInt64)val); }
131 #endif
132 
133 
134  virtual void write(float val);
135  virtual void write(double val);
136 
137  virtual void write(const std::string& val);
138  virtual void write(const agx::String& val);
139  virtual void write(const agx::Name& val)
140  {
141  agx::String tmp = val;
142  write(tmp);
143  }
144  virtual void write(const agx::Uuid& val);
145 
146 
147  void write(const agx::RangeReal& val);
148  void write(const agx::Vec2f& val);
149  void write(const agx::Vec2d& val);
150  void write(const agx::Vec2i32& val);
151  void write(const agx::Vec2i64& val);
152 
153  void write(const agx::Vec3f& val);
154  void write(const agx::Vec3d& val);
155  void write(const agx::Vec3i32& val);
156  void write(const agx::Vec3i64& val);
157  void write(const agx::Vec4f& val);
158  void write(const agx::Vec4d& val);
159  void write(const agx::Quat& val);
160  void write(const agx::SPDMatrix3x3& val);
161  void write(const agx::Matrix3x3d& val);
162  void write(const agx::Matrix3x3f& val);
163  void write(const agx::AffineMatrix4x4f& val);
164  void write(const agx::AffineMatrix4x4d& val);
165 
166  void write(const Serializable *serializable, StorageMode mode);
167  void write(const Serializable &serializable, StorageMode mode);
168 
169 
170  virtual void addAttribute(const char *name, bool value);
171  virtual void addAttribute(const char *name, const char* value);
172  virtual void addAttribute(const char *name, char value);
173  virtual void addAttribute(const char *name, agx::Int8 value);
174  virtual void addAttribute(const char *name, agx::UInt8 value);
175  virtual void addAttribute(const char *name, agx::UInt16 value);
176  virtual void addAttribute(const char *name, agx::Int32 value);
177  virtual void addAttribute(const char *name, agx::UInt32 value);
178  virtual void addAttribute(const char *name, agx::UInt64 value);
179  virtual void addAttribute(const char *name, float value);
180  virtual void addAttribute(const char *name, double value);
181  virtual void addAttribute(const char *name, const agx::Uuid& value);
182 
183 #ifdef __APPLE__
184  virtual void addAttribute(const char *name, size_t value) { addAttribute(name, (agx::UInt64)value); }
185 #endif
186 
187  bool getEndianMatch() const {
188  return m_endianMatch == 1;
189  }
190 
191  virtual void beginSection(const char *) override {}
192  virtual void endSection(const char *) override {}
193 
194  virtual void writeObjectByRef(const Serializable* obj);
195  virtual void writeObjectByVal(const Serializable& obj);
196 
205  virtual void write(const void* buf, size_t len);
206 
208  void flush();
209 
211  void close() override;
212 
217  void setWriteBufferSize(agx::UInt32 bufferSize);
218 
220  agx::UInt32 getWriteBufferSize() const;
221 
227  bool addModification( const char *str);
228 
232  bool hasModification(const agx::String& modification) const;
233 
239  template<typename VECTOR_TYPE>
240  AGXCORE_EXPORT void write_vector(const agx::String& name, const VECTOR_TYPE& val);
241 
242  protected:
243 
244  // Need this to verify the modifications
245  friend class InputArchive;
246 
247  // This constructor is only used by InputArchive during modification verification
248  OutputArchive();
249 
251  virtual ~OutputArchive();
252 
253  virtual void writeHeaderData(agx::UInt32 magic,
254  const std::string& appName,
255  agx::UInt8 generationVersion,
256  agx::UInt8 majorVersion,
257  agx::UInt8 minorVersion,
258  agx::UInt8 patchVersion,
259  const std::string& revision,
260  agx::UInt16 serializationVersion);
261 
262  virtual void writeArchiveHeader(const std::string& appName,
263  agx::UInt8 generationVersion,
264  agx::UInt8 majorVersion,
265  agx::UInt8 minorVersion,
266  agx::UInt8 patchVersion,
267  const std::string& revision,
268  agx::UInt16 serializationVersion);
269 
270  virtual void writeObjectHeader(StorageMode mode, StorageAgent* agent);
271  virtual void writeTypeHeader(StorageAgent* agent);
272 
273 
274  protected:
275  void writeBitSize();
276  std::ostream& m_outputStream;
277  ObjectToIdMap m_defaultCache;
278  ObjectToIdMap& m_objectToIdMap;
279  StorageAgentToIdMap m_storageAgentToIdMap;
280 
281 
282  int m_numObjectsWritten; // for diagnostic purposes
284 
285  // copy constructor and assignment operator prohibited
287  OutputArchive& operator=(const OutputArchive&);
288 
289  private:
290  char m_endianMatch;
291  agx::Vector<char> m_buffer;
292  agx::UInt32 m_bufferPosition;
293  agx::UInt32 m_bufferSize;
294 
295  void addModifications();
296  void writeModificationData();
297  bool m_hasAddedModifications;
298  static bool s_useCurrentDate;
299 
300  agx::HashSet<agx::String> m_modificationHash;
301  };
302 
303 
304 
305  template <typename T>
306  struct OutputRef
307  {
308  OutputRef(const char *name, const T& obj, StorageMode mode) :
309  m_name(name),
310  m_obj(&obj),
311  m_mode(mode)
312  {
313  }
314 
315  OutputRef(const char *name, const T* obj, StorageMode mode) :
316  m_name(name),
317  m_obj(obj),
318  m_mode(mode)
319  {
320  }
321 
323  {
324  m_obj = other.m_obj;
325  m_name = other.m_name;
326  m_mode = other.m_mode;
327  return *this;
328  }
329 
330  const char *m_name;
331  const T *m_obj;
333  };
335 
336 
337 
338  template <typename T>
339  struct OutputVal {
340  OutputVal(const char *name, const T obj) : m_name(name), m_obj(obj), m_mode(STORE_BY_VALUE) {}
341 
342  const char *m_name;
343  T m_obj;
344 
346  OutputVal & operator=(const OutputVal& other) { m_obj = other.m_obj; m_name = other.m_name; m_mode = other.m_mode; return *this; }
347  };
348 
349 
350 #define DECLARE_OUTPUT_BASIC_TYPE(TYPE) \
351  AGXCORE_EXPORT inline agxStream::OutputArchive& operator<<( OutputArchive& out, OutputVal<TYPE> val ) \
352  { \
353  out.beginSection(val.m_name); \
354  out.write( val.m_obj ); \
355  out.endSection(val.m_name); \
356  return out;\
357  }\
358  AGXCORE_EXPORT inline OutputVal<TYPE> out(const char *name, TYPE obj) { return OutputVal<TYPE>(name, obj); }
359 
360  DECLARE_OUTPUT_BASIC_TYPE(std::string)
373 #ifdef __APPLE__
375 #endif
395 
396 
397 
403  inline OutputRef<Serializable> out(const char *name, const Serializable* obj)
404  {
405  return OutputRef<Serializable>(name, obj, STORE_BY_REF);
406  }
407 
414  template <typename T>
415  OutputRef<T> out_val(const char *name, const T& obj) { return OutputRef<T>(name, obj, STORE_BY_VALUE); }
416 
417 
418  template <typename T>
419  inline OutputArchive& operator<<(OutputArchive& out, OutputRef<T> val)
420  {
421  out.beginSection(val.m_name);
422  out.write(val.m_obj, val.m_mode);
423  out.endSection(val.m_name);
424  return out;
425  }
426 
427  template <typename T>
428  inline OutputArchive& operator<<(OutputArchive& out, OutputVal<T> val)
429  {
430  out.beginSection(val.m_name);
431  out.write(val.m_obj);
432  out.endSection(val.m_name);
433  return out;
434  }
435 }
436 
int64_t Int64
Definition: Integer.h:46
Inheritance with partial specialization due to bug with ref_ptr containers.
Definition: HashSet.h:665
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
ObjectToIdMap & m_objectToIdMap
A class holding 4 dimensional vectors and providing basic arithmetic.
Definition: Vec4Template.h:34
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
GLenum GLuint id
Definition: gl.h:5539
agx::Event2< const Serializable *, agx::UInt32 > StoreEvent
Definition: OutputArchive.h:59
#define DECLARE_OUTPUT_BASIC_TYPE(TYPE)
agx::HashTable< StorageAgent *, agx::UInt32 > StorageAgentToIdMap
Definition: OutputArchive.h:69
OutputVal & operator=(const OutputVal &other)
Namespace agx 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
ObjectToIdMap m_defaultCache
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:80
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.
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
Definition: OutputArchive.h:93
std::ostream & m_outputStream
OutputVal(const char *name, const T obj)
agx::HashTable< const Serializable *, IdEntry > ObjectToIdMap
Definition: OutputArchive.h:68