AGX Dynamics 2.42.1.1
Loading...
Searching...
No Matches
OutputArchive.h
Go to the documentation of this file.
1/*
2Copyright 2007-2025. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx 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
36namespace agx
37{
38 class Uuid;
39}
40
41namespace 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
103 return m_numObjectsWritten;
104 }
105
110 return m_numObjectsSkipped;
111 }
112
117 static void setUseCurrentDate(bool flag);
118
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::Matrix3x3f& val);
171 void write(const agx::Matrix3x3d& val);
172 void write(const agx::Matrix4x4f& val);
173 void write(const agx::Matrix4x4d& val);
174 void write(const agx::AffineMatrix4x4f& val);
175 void write(const agx::AffineMatrix4x4d& val);
176
177 void write(const Serializable *serializable, StorageMode mode);
178 void write(const Serializable &serializable, StorageMode mode);
179
180
181 virtual void addAttribute(const char *name, bool value);
182 virtual void addAttribute(const char *name, const char* value);
183 virtual void addAttribute(const char *name, char value);
184 virtual void addAttribute(const char *name, agx::Int8 value);
185 virtual void addAttribute(const char *name, agx::UInt8 value);
186 virtual void addAttribute(const char *name, agx::UInt16 value);
187 virtual void addAttribute(const char *name, agx::Int32 value);
188 virtual void addAttribute(const char *name, agx::UInt32 value);
189 virtual void addAttribute(const char *name, agx::UInt64 value);
190 virtual void addAttribute(const char *name, float value);
191 virtual void addAttribute(const char *name, double value);
192 virtual void addAttribute(const char *name, const agx::Uuid& value);
193
194#ifdef __APPLE__
195 virtual void addAttribute(const char *name, size_t value) { addAttribute(name, (agx::UInt64)value); }
196#endif
197
198 bool getEndianMatch() const {
199 return m_endianMatch == 1;
200 }
201
202 virtual void beginSection(const char *) override {}
203 virtual void endSection(const char *) override {}
204
205 virtual void writeObjectByRef(const Serializable* obj);
206 virtual void writeObjectByVal(const Serializable& obj);
207
216 virtual void write(const void* buf, size_t len);
217
219 void flush();
220
222 void close() override;
223
229
232
238 bool addModification( const char *str);
239
243 bool hasModification(const agx::String& modification) const;
244
250 template<typename VECTOR_TYPE>
251 AGXCORE_EXPORT void write_vector(const agx::String& name, const VECTOR_TYPE& val);
252
253 protected:
254
255 // Need this to verify the modifications
256 friend class InputArchive;
257
258 // This constructor is only used by InputArchive during modification verification
260
262 virtual ~OutputArchive();
263
264 virtual void writeHeaderData(agx::UInt32 magic,
265 const std::string& appName,
266 agx::UInt8 generationVersion,
267 agx::UInt8 majorVersion,
268 agx::UInt8 minorVersion,
269 agx::UInt8 patchVersion,
270 const std::string& revision,
271 agx::UInt16 serializationVersion);
272
273 virtual void writeArchiveHeader(const std::string& appName,
274 agx::UInt8 generationVersion,
275 agx::UInt8 majorVersion,
276 agx::UInt8 minorVersion,
277 agx::UInt8 patchVersion,
278 const std::string& revision,
279 agx::UInt16 serializationVersion);
280
281 virtual void writeObjectHeader(StorageMode mode, StorageAgent* agent);
282 virtual void writeTypeHeader(StorageAgent* agent);
283
284
285 protected:
287 std::ostream& m_outputStream;
291
292
293 int m_numObjectsWritten; // for diagnostic purposes
295
296 // copy constructor and assignment operator prohibited
299
300 private:
301 char m_endianMatch;
302 agx::Vector<char> m_buffer;
303 agx::UInt32 m_bufferPosition;
304 agx::UInt32 m_bufferSize;
305
306 void addModifications();
307 void writeModificationData();
308 bool m_hasAddedModifications;
309 static bool s_useCurrentDate;
310
311 agx::HashSet<agx::String> m_modificationHash;
312 };
313
314
315
316 template <typename T>
318 {
319 OutputRef(const char *name, const T& obj, StorageMode mode) :
320 m_name(name),
321 m_obj(&obj),
322 m_mode(mode)
323 {
324 }
325
326 OutputRef(const char *name, const T* obj, StorageMode mode) :
327 m_name(name),
328 m_obj(obj),
329 m_mode(mode)
330 {
331 }
332
334 {
335 m_obj = other.m_obj;
336 m_name = other.m_name;
337 m_mode = other.m_mode;
338 return *this;
339 }
340
341 const char *m_name;
342 const T *m_obj;
344 };
346
347
348
349 template <typename T>
350 struct OutputVal {
351 OutputVal(const char *name, const T obj) : m_name(name), m_obj(obj), m_mode(STORE_BY_VALUE) {}
352
353 const char *m_name;
355
357 OutputVal & operator=(const OutputVal& other) { m_obj = other.m_obj; m_name = other.m_name; m_mode = other.m_mode; return *this; }
358 };
359
360
361#define DECLARE_OUTPUT_BASIC_TYPE(TYPE) \
362 AGXCORE_EXPORT inline agxStream::OutputArchive& operator<<( OutputArchive& out, OutputVal<TYPE> val ) \
363 { \
364 out.beginSection(val.m_name); \
365 out.write( val.m_obj ); \
366 out.endSection(val.m_name); \
367 return out;\
368 }\
369 AGXCORE_EXPORT inline OutputVal<TYPE> out(const char *name, TYPE obj) { return OutputVal<TYPE>(name, obj); }
370
371 DECLARE_OUTPUT_BASIC_TYPE(std::string)
384#ifdef __APPLE__
386#endif
408
409
410
416 inline OutputRef<Serializable> out(const char *name, const Serializable* obj)
417 {
418 return OutputRef<Serializable>(name, obj, STORE_BY_REF);
419 }
420
427 template <typename T>
428 OutputRef<T> out_val(const char *name, const T& obj) { return OutputRef<T>(name, obj, STORE_BY_VALUE); }
429
430
431 template <typename T>
433 {
434 out.beginSection(val.m_name);
435 out.write(val.m_obj, val.m_mode);
436 out.endSection(val.m_name);
437 return out;
438 }
439
440 template <typename T>
442 {
443 out.beginSection(val.m_name);
444 out.write(val.m_obj);
445 out.endSection(val.m_name);
446 return out;
447 }
448}
449
#define DECLARE_OUTPUT_BASIC_TYPE(TYPE)
#define AGXCORE_EXPORT
Abstract base class for input/output storage of Serializable classes.
Definition: Archive.h:56
Class for reading a binary stream of serialized data.
Definition: InputArchive.h:51
Class for writing serialized data in binary format to a stream.
Definition: OutputArchive.h:57
virtual void write(char val)
virtual void write(agx::UInt64 val)
virtual void write(agx::UInt32 val)
virtual void beginSection(const char *) override
void write(const agx::Vec4d &val)
virtual void write(agx::Int16 val)
virtual void write(float val)
OutputArchive(const OutputArchive &)
AGXCORE_EXPORT void write_vector(const agx::String &name, const VECTOR_TYPE &val)
Write an array of data.
virtual void write(agx::UInt16 val)
virtual void write(bool val)
virtual void write(const agx::Name &val)
void write(const agx::Vec2f &val)
bool addModification(const char *str)
Add a modification string that will be stored in the archive.
ObjectToIdMapContext m_defaultCache
ObjectToIdMapContext & m_objectToIdMap
int getNumObjectsWritten() const
void write(const agx::AffineMatrix4x4f &val)
OutputArchive(std::ostream &outputStream, ObjectToIdMapContext &cache)
void open(const std::string &appName)
Open a new stream and write an archive header with some version information.
OutputArchive & operator=(const OutputArchive &)
virtual void write(const agx::String &val)
virtual void write(double val)
void flush()
Make sure all data in buffer is written to associated stream.
virtual void write(const std::string &val)
virtual void write(agx::Int8 val)
virtual void write(agx::Int64 val)
virtual void writeObjectByRef(const Serializable *obj)
agx::UInt32 getWriteBufferSize() const
void write(const agx::Matrix4x4d &val)
OutputArchive(std::ostream &outputStream)
Create and associate an OutputArchive for writing to an open stream.
virtual void addAttribute(const char *name, agx::Int8 value)
void write(const agx::Matrix3x3d &val)
void close() override
Call flush.
void write(const agx::AffineMatrix4x4d &val)
void write(const agx::Matrix4x4f &val)
virtual void writeObjectHeader(StorageMode mode, StorageAgent *agent)
virtual void writeObjectByVal(const Serializable &obj)
void write(const agx::Vec2i64 &val)
void write(const agx::Matrix3x3f &val)
agx::HashTable< const Serializable *, IdEntry > ObjectToIdMap
Definition: OutputArchive.h:68
static void setUseCurrentDate(bool flag)
Specify if the current date (and time) at execution time should be used when creating archive,...
void write(const agx::Quat &val)
virtual ~OutputArchive()
Destructor.
virtual void write(const agx::Uuid &val)
virtual void write(agx::UInt8 val)
virtual void writeHeaderData(agx::UInt32 magic, const std::string &appName, agx::UInt8 generationVersion, agx::UInt8 majorVersion, agx::UInt8 minorVersion, agx::UInt8 patchVersion, const std::string &revision, agx::UInt16 serializationVersion)
virtual void addAttribute(const char *name, agx::UInt16 value)
virtual void writeTypeHeader(StorageAgent *agent)
virtual void addAttribute(const char *name, agx::UInt64 value)
virtual void addAttribute(const char *name, agx::UInt8 value)
StorageAgentToIdMap m_storageAgentToIdMap
void write(const agx::Vec4f &val)
virtual void endSection(const char *) override
agx::Event2< const Serializable *, agx::UInt32 > StoreEvent
Definition: OutputArchive.h:59
virtual void addAttribute(const char *name, const char *value)
void write(const Serializable *serializable, StorageMode mode)
void write(const agx::Vec3f &val)
std::ostream & getStream()
Return a reference to the output stream.
Definition: OutputArchive.h:92
virtual void addAttribute(const char *name, float value)
bool hasModification(const agx::String &modification) const
void write(const agx::Vec3d &val)
void write(const agx::Vec3i32 &val)
void write(const Serializable &serializable, StorageMode mode)
agx::HashTable< StorageAgent *, agx::UInt32 > StorageAgentToIdMap
Definition: OutputArchive.h:69
virtual void addAttribute(const char *name, bool value)
void write(const agx::Vec2i32 &val)
virtual void addAttribute(const char *name, agx::Int32 value)
void write(const agx::RangeReal &val)
void setWriteBufferSize(agx::UInt32 bufferSize)
Specify a new buffer size for the write buffer.
virtual void addAttribute(const char *name, char value)
virtual void write(agx::Int32 val)
void write(const agx::Vec2d &val)
bool getUseCurrentDate()
Return if the current date (and time) at execution time should be used when creating archive,...
virtual void writeArchiveHeader(const std::string &appName, agx::UInt8 generationVersion, agx::UInt8 majorVersion, agx::UInt8 minorVersion, agx::UInt8 patchVersion, const std::string &revision, agx::UInt16 serializationVersion)
virtual void write(const void *buf, size_t len)
This method will write raw data into the archive.
virtual void addAttribute(const char *name, const agx::Uuid &value)
void write(const agx::SPDMatrix3x3 &val)
std::ostream & m_outputStream
int getNumObjectsSkipped() const
virtual void addAttribute(const char *name, agx::UInt32 value)
virtual void addAttribute(const char *name, double value)
void write(const agx::Vec3i64 &val)
This class is an abstract base class for all classes that can be stored and retrieved from an Archive...
Definition: Serializable.h:44
Matrix class for rigid transformations (translation, rotation).
An event with two arguments.
Definition: Event.h:122
Inheritance with partial specialization due to bug with ref_ptr containers.
Definition: agx/HashSet.h:670
Inheritance with partial specialization due to bug with ref_ptr containers.
Matrix class for affine transformations.
Definition: Matrix4x4.h:58
Representation of a name string.
Definition: Name.h:33
Specialized type of matrices for holding symmetric positive definite matrices.
Definition: SPDMatrix3x3.h:48
A UUID, or Universally unique identifier, is intended to uniquely identify information in a distribut...
Definition: Uuid.h:42
A class holding 4 dimensional vectors and providing basic arithmetic.
Definition: Vec4Template.h:35
Templated vector class.
Definition: agx/Vector.h:53
Smart pointer for handling referenced counted objects.
Definition: ref_ptr.h:30
This namespace contain classes for streaming classes into archives, ASCII, binary for storage (serial...
agx::ref_ptr< OutputArchive > OutputArchiveRef
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.
StorageMode
Definition: Archive.h:45
@ STORE_BY_VALUE
Definition: Archive.h:46
@ STORE_BY_REF
Definition: Archive.h:47
bool AGXPHYSICS_EXPORT write(const std::string &filename, const agxStream::Serializable *data)
Write a Serializable object to a file.
OutputArchive & operator<<(OutputArchive &out, OutputRef< T > val)
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.
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
uint16_t UInt16
Definition: Integer.h:31
int32_t Int32
Definition: Integer.h:37
uint32_t UInt32
Definition: Integer.h:32
uint64_t UInt64
Definition: Integer.h:33
int64_t Int64
Definition: Integer.h:38
uint8_t UInt8
Definition: Integer.h:30
AGXCORE_EXPORT const InvalidIndexStruct InvalidIndex
int8_t Int8
Definition: Integer.h:35
int16_t Int16
Definition: Integer.h:36
IdEntry(agx::UInt32 _id=agx::InvalidIndex)
Definition: OutputArchive.h:63
This struct is used to handle id-collisions when using incremental journal.
Definition: OutputArchive.h:76
OutputRef & operator=(const OutputRef &other)
OutputRef(const char *name, const T &obj, StorageMode mode)
OutputRef(const char *name, const T *obj, StorageMode mode)
OutputVal & operator=(const OutputVal &other)
OutputVal(const char *name, const T obj)