AGX Dynamics 2.41.3.0
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::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
227
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
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:
285 std::ostream& m_outputStream;
289
290
291 int m_numObjectsWritten; // for diagnostic purposes
293
294 // copy constructor and assignment operator prohibited
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>
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;
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
#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
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)
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:45
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.
Representation of a name string.
Definition: Name.h:33
Specialized type of matrices for holding symmetric positive definite matrices.
Definition: SPDMatrix3x3.h:49
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)