MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
mlAbstractPersistenceStream.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2012, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13#ifndef ML_ABSTRACT_PERSISTENCE_STREAM_H
14#define ML_ABSTRACT_PERSISTENCE_STREAM_H
15
18
19#include "mlUtilsSystem.h"
20#include "mlTypeDefs.h"
21
22#include <ThirdPartyWarningsDisable.h>
23#include <list>
24#include <vector>
25#include <set>
26#include <string>
27#include <ThirdPartyWarningsRestore.h>
28
30
31class Base;
32
33// Pre-define templates that are defined in MLLinearAlgebra.
34template <class T, size_t size, class DataContainer> class FloatingPointVector;
35
36// Pre-define templates that are defined in MLLinearAlgebra.
37template <typename T, int n> class TVectorNDBase;
38
39// Pre-define templates that are defined in MLLinearAlgebra.
40template <class VectorT, size_t size> class FloatingPointMatrix;
41
42
47{
48public:
57 void startList(const char* name = nullptr, const char* xmlItemName = "Item", bool xmlSuppressScope = false);
58
60 void endList();
61
64 void startStruct(const char* name = nullptr);
65
67 void endStruct();
68
72 virtual bool isBinary() const = 0;
73
74protected:
77
80 virtual void nameCheck(const char* name);
81
86 virtual void startListImpl(const char* name, const char* xmlItemName, bool xmlSuppressScope) = 0;
87 virtual void endListImpl() = 0;
88 virtual void startStructImpl(const char* name) = 0;
89 virtual void endStructImpl() = 0;
91
93 bool isInList() const
94 {
95 return !_isListStack.empty() && _isListStack.back();
96 }
97
99 size_t nestingDepth() const
100 {
101 return _isListStack.size();
102 }
103
105 virtual void internalError(const char* msg, const char* arg = nullptr);
106
107private:
109 std::list<bool> _isListStack;
110};
111
112// -----------------------------------------------------------------------------
113
124{
125public:
127
129 void startStructWithVersion(int version, const char* name = nullptr);
130
134 virtual void write(bool value, const char* name = nullptr);
135
136 void write(MLint32 value, const char* name = nullptr);
137 void write(MLuint32 value, const char* name = nullptr);
138 void write(MLint64 value, const char* name = nullptr);
139 void write(MLuint64 value, const char* name = nullptr);
140 void write(MLfloat value, const char* name = nullptr);
141 void write(MLdouble value, const char* name = nullptr);
142 void write(const std::string& value, const char* name = nullptr);
143
144 void write(const char* value, const char* name = nullptr)
145 {
146 write(std::string(value), name);
147 }
149
152 template <typename T> void write(const std::vector<T>& values, const char* name = nullptr)
153 {
154 nameCheck(name);
155 writeValues(name, &values.front(), values.size(), false);
156 }
157
159 template <typename T, size_t n, class DataContainer>
160 void write(const FloatingPointVector<T, n, DataContainer>& value, const char* name = nullptr)
161 {
162 nameCheck(name);
163 writeValues(name, &value[0], n, true);
164 }
165
167 template <typename T, size_t n> void write(const TVectorNDBase<T, n>& value, const char* name = nullptr)
168 {
169 nameCheck(name);
170 writeValues(name, &value.array, n, true);
171 }
172
174 template <typename T, size_t n> void write(const FloatingPointMatrix<T, n>& value, const char* name = nullptr)
175 {
176 nameCheck(name);
177 typename T::ComponentType components[FloatingPointMatrix<T, n>::ComponentCount];
180 }
181
184 virtual void writeObject(const Base* const obj, const char* name = nullptr);
185
187 virtual void writeData(const void* data, size_t len, const char* name = nullptr) = 0;
188
192 virtual bool isValidElementName(const char* name);
193
194protected:
197
203 virtual void setObjectID(int id) = 0;
205 virtual void setObjectType(const char* type) = 0;
207 virtual void setObjectVersion(int version) = 0;
209
214 virtual void writeValues(const char* name, const MLint32* values, size_t n, bool fixedList) = 0;
215 virtual void writeValues(const char* name, const MLuint32* values, size_t n, bool fixedList) = 0;
216 virtual void writeValues(const char* name, const MLint64* values, size_t n, bool fixedList) = 0;
217 virtual void writeValues(const char* name, const MLuint64* values, size_t n, bool fixedList) = 0;
218 virtual void writeValues(const char* name, const MLfloat* values, size_t n, bool fixedList) = 0;
219 virtual void writeValues(const char* name, const MLdouble* values, size_t n, bool fixedList) = 0;
221
223 virtual void writeString(const char* name, const std::string& value) = 0;
224
227 void nameCheck(const char* name) override;
228};
229
230// -----------------------------------------------------------------------------
231
243{
244public:
247
249 int startStructWithVersion(const char* name = nullptr);
250
254 virtual void read(bool& value, const char* name = nullptr);
255
256 void read(MLint32& value, const char* name = nullptr);
257 void read(MLuint32& value, const char* name = nullptr);
258 void read(MLint64& value, const char* name = nullptr);
259 void read(MLuint64& value, const char* name = nullptr);
260 void read(MLfloat& value, const char* name = nullptr);
261 void read(MLdouble& value, const char* name = nullptr);
262 void read(std::string& value, const char* name = nullptr);
264
267 template <typename T> void read(std::vector<T>& values, const char* name = nullptr)
268 {
269 nameCheck(name);
270 readValues(name, 0, values);
271 }
272
274 template <typename T, size_t n, class DataContainer>
275 void read(FloatingPointVector<T, n, DataContainer>& value, const char* name = nullptr)
276 {
277 nameCheck(name);
278 std::vector<T> buffer;
279 readValues(name, n, buffer);
280 for (size_t i = 0; i < n; i++) {
281 value[i] = buffer[i];
282 }
283 }
284
286 template <typename T, size_t n> void read(TVectorNDBase<T, n>& value, const char* name = nullptr)
287 {
288 nameCheck(name);
289 std::vector<T> buffer;
290 readValues(name, n, buffer);
291 for (size_t i = 0; i < n; i++) {
292 value[i] = buffer[i];
293 }
294 }
295
297 template <typename T, size_t n> void read(FloatingPointMatrix<T, n>& value, const char* name = nullptr)
298 {
299 nameCheck(name);
300 std::vector<typename T::ComponentType> buffer;
301 readValues(name, n, buffer);
302 value.setValuesFromPtr(buffer.data());
303 }
304
307 template <typename T> void readOptional(T& value, const T& defaultValue, const char* name = nullptr)
308 {
309 if (_isAvailable(name)) {
310 read(value, name);
311 }
312 else {
313 value = defaultValue;
314 }
315 }
316
319 virtual Base* readObject(const char* name = nullptr);
320
322 virtual void readData(std::string& value, const char* name = nullptr) = 0;
323
326 bool hasNextInStruct(std::string& name);
327
330 bool isNextInStruct(const char* name);
331
335
338
339protected:
342
345 virtual bool hasNextInStructImpl(std::string& name) = 0;
346
349 virtual bool hasNextListItemImpl() = 0;
350
354 virtual bool getObjectID(int& id) = 0;
356 virtual bool getObjectType(std::string& type) = 0;
358 virtual bool getObjectVersion(int& version) = 0;
360
365 virtual void readValues(const char* name, size_t expected, std::vector<MLint32>& values) = 0;
366 virtual void readValues(const char* name, size_t expected, std::vector<MLuint32>& values) = 0;
367 virtual void readValues(const char* name, size_t expected, std::vector<MLint64>& values) = 0;
368 virtual void readValues(const char* name, size_t expected, std::vector<MLuint64>& values) = 0;
369 virtual void readValues(const char* name, size_t expected, std::vector<MLfloat>& values) = 0;
370 virtual void readValues(const char* name, size_t expected, std::vector<MLdouble>& values) = 0;
372
374 virtual void readString(const char* name, std::string& value) = 0;
375
377 virtual void formatError(const std::string& msg);
378
379private:
382 bool _isAvailable(const char* name);
383
385 std::set<std::string> _futureVersionClasses;
386};
387
388// -----------------------------------------------------------------------------
389
393{
394public:
399 InternalError
400 };
401
404
407 {
408 return _type;
409 }
410
412 virtual std::string getMessage() const
413 {
414 return _msg;
415 }
416
417protected:
419 PersistenceStreamException(ErrorType type, const std::string& msg)
420 : _type(type)
421 , _msg(msg)
422 {}
423
425 std::string _msg;
426};
427
437
447
456
458// -----------------------------------------------------------------------------
459
462#define ML_WRITETO_SUPER(SuperClass, stream) \
463 { \
464 stream->startStructWithVersion(SuperClass::getAddStateVersion(), "_" #SuperClass); \
465 SuperClass::writeTo(stream); \
466 stream->endStruct(); \
467 }
468
471#define ML_READFROM_SUPER(SuperClass, stream) \
472 { \
473 int version = stream->startStructWithVersion("_" #SuperClass); \
474 SuperClass::readFrom(stream, version); \
475 stream->versionCheck(SuperClass::getTypeId()->getName(), SuperClass::getAddStateVersion(), version); \
476 stream->endStruct(); \
477 }
478
479// -----------------------------------------------------------------------------
480
481#endif
@ T
Class for reading object data from a stream.
virtual void readValues(const char *name, size_t expected, std::vector< MLint32 > &values)=0
Abstract reading methods that need to be implemented by derived classes.
void read(MLuint64 &value, const char *name=nullptr)
virtual void readValues(const char *name, size_t expected, std::vector< MLfloat > &values)=0
void read(std::vector< T > &values, const char *name=nullptr)
Reads vectors of primitive values from the stream.
virtual bool getObjectVersion(int &version)=0
Returns persistence version.
virtual bool getObjectType(std::string &type)=0
Returns base type.
void read(MLint32 &value, const char *name=nullptr)
virtual void formatError(const std::string &msg)
Indicates format error, by default, throw an exception.
virtual void readData(std::string &value, const char *name=nullptr)=0
Same as readObject for binary data (std::string misused as binary data container).
void read(MLuint32 &value, const char *name=nullptr)
virtual Base * readObject(const char *name=nullptr)
Reads Base object from stream.
virtual void read(bool &value, const char *name=nullptr)
Reads primitive values from the stream.
void versionCheck(const char *className, int objectVersion, int storedVersion)
Prints a warning if the storedVersion is greater than the objectVersion.
virtual void readValues(const char *name, size_t expected, std::vector< MLuint32 > &values)=0
virtual void readValues(const char *name, size_t expected, std::vector< MLuint64 > &values)=0
virtual bool hasNextInStructImpl(std::string &name)=0
Checks whether an item with the given name comes next in the stream.
bool isNextInStruct(const char *name)
Checks whether an item with the given name comes next in the stream.
void read(FloatingPointMatrix< T, n > &value, const char *name=nullptr)
Reads float matrices like mat4 from the stream.
void read(std::string &value, const char *name=nullptr)
void read(MLdouble &value, const char *name=nullptr)
virtual bool getObjectID(int &id)=0
Implementation of Base object persistence, get attribute of currently open struct.
int startStructWithVersion(const char *name=nullptr)
Starts a struct, returns the version of struct (0 if not given).
bool hasNextListItem()
Checks whether there is another item in the current list.
void read(FloatingPointVector< T, n, DataContainer > &value, const char *name=nullptr)
Reads float vectors like vec3f from the stream.
bool hasNextInStruct(std::string &name)
Returns true if there is another element in the current struct scope, and if so, sets name to its nam...
virtual bool hasNextListItemImpl()=0
Checks whether there is another item in the current list.
void read(TVectorNDBase< T, n > &value, const char *name=nullptr)
Reads integer vectors like ImageVector from the stream.
void read(MLfloat &value, const char *name=nullptr)
void read(MLint64 &value, const char *name=nullptr)
void readOptional(T &value, const T &defaultValue, const char *name=nullptr)
Performs optional reading for all above methods.
virtual void readString(const char *name, std::string &value)=0
Abstract reading method that needs to be implemented by derived classes.
virtual void readValues(const char *name, size_t expected, std::vector< MLint64 > &values)=0
virtual void readValues(const char *name, size_t expected, std::vector< MLdouble > &values)=0
Class for writing object data to a stream.
~AbstractPersistenceOutputStream() override=default
virtual void writeValues(const char *name, const MLuint64 *values, size_t n, bool fixedList)=0
void write(const FloatingPointVector< T, n, DataContainer > &value, const char *name=nullptr)
Writes float vectors like vec3f to the stream.
virtual void writeValues(const char *name, const MLfloat *values, size_t n, bool fixedList)=0
void write(const char *value, const char *name=nullptr)
void startStructWithVersion(int version, const char *name=nullptr)
Starts a versioned struct.
void write(MLfloat value, const char *name=nullptr)
void write(MLuint32 value, const char *name=nullptr)
void write(const std::string &value, const char *name=nullptr)
virtual void setObjectID(int id)=0
Implementation of Base object persistence, sets attribute on currently open struct Implementation hin...
virtual void setObjectType(const char *type)=0
Sets base type.
void write(MLdouble value, const char *name=nullptr)
virtual void writeString(const char *name, const std::string &value)=0
Abstract writing method that needs to be implemented by derived classes.
virtual bool isValidElementName(const char *name)
Checks whether name is valid for use with persistence entries.
virtual void writeValues(const char *name, const MLint32 *values, size_t n, bool fixedList)=0
Abstract writing methods that need to be implemented by derived classes.
virtual void writeValues(const char *name, const MLint64 *values, size_t n, bool fixedList)=0
void write(MLint64 value, const char *name=nullptr)
virtual void setObjectVersion(int version)=0
Sets persistence version.
virtual void writeObject(const Base *const obj, const char *name=nullptr)
Writes a Base object to the stream.
void nameCheck(const char *name) override
Overridden to perform an additional check in Debug mode if name is suitable for the persistence forma...
virtual void writeData(const void *data, size_t len, const char *name=nullptr)=0
Writes binary data to stream.
void write(const FloatingPointMatrix< T, n > &value, const char *name=nullptr)
Writes float matrices like mat4 to the stream.
virtual void writeValues(const char *name, const MLuint32 *values, size_t n, bool fixedList)=0
virtual void write(bool value, const char *name=nullptr)
Writes primitive values to the stream.
void write(const TVectorNDBase< T, n > &value, const char *name=nullptr)
Writes integer vectors like ImageVector to the stream.
void write(const std::vector< T > &values, const char *name=nullptr)
Writes vectors of primitive values to the stream.
virtual void writeValues(const char *name, const MLdouble *values, size_t n, bool fixedList)=0
void write(MLuint64 value, const char *name=nullptr)
void write(MLint32 value, const char *name=nullptr)
AbstactPersistenceStream is the base class for AbstractPersistenceOutputStream and AbstractPersistenc...
virtual void internalError(const char *msg, const char *arg=nullptr)
Logs an internal error. Usually called to indicate wrong usage of interface.
virtual bool isBinary() const =0
Is the stream in a binary format? Otherwise, it is in a human-readable text format.
bool isInList() const
Checks whether the stream is currently in 'list' mode. Otherwise, it is in 'struct' mode.
void endList()
Ends reading/writing of the list.
virtual void nameCheck(const char *name)
Checks whether the name is provided when being in a subgroup, or that no name is provided when being ...
size_t nestingDepth() const
Returns depth of nesting stack (created by startList/startStruct), used to check nesting rules.
void endStruct()
Ends reading/writing of struct values.
virtual void startStructImpl(const char *name)=0
virtual void endStructImpl()=0
virtual void endListImpl()=0
void startStruct(const char *name=nullptr)
Starts a new struct of values in the data stream, must be ended with endStruct().
virtual void startListImpl(const char *name, const char *xmlItemName, bool xmlSuppressScope)=0
Abstract methods called by above methods, needs to be implemented in derived classes.
void startList(const char *name=nullptr, const char *xmlItemName="Item", bool xmlSuppressScope=false)
Starts a new list of values in the data stream, must be ended with endList().
Class representing general ML objects that support import/export via strings (setPersistentState() an...
Definition mlBase.h:63
Base class of all matrix classes that holds the data buffer and provides some general access methods.
void getValuesToPtr(ComponentType *values) const
Copies contents of *this into an array matrix, row by row; type and size must match.
void setValuesFromPtr(const ComponentType *const values)
Copies the contents from an array matrix into *this, row by row; type and size must match.
Template class for vector arithmetic with floating point datatypes.
This class represents the exceptions that can be thrown while reading from or writing to the persiste...
virtual std::string getMessage() const
Returns error message.
ErrorType getErrorType() const
Returns exception subtype.
PersistenceStreamException(ErrorType type, const std::string &msg)
This is set to protected, because you should not create instances of the base class.
Derived class. This exception usually denotes programming errors.
Declaration of integer vector type traits.
ComponentType array[NumberOfDimensions]
Provides access to all members as an array; the caller must guarantee that indexes are within [0,...
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
UINT64 MLuint64
Introduce platform-independent 64-bit unsigned integer type.
Definition mlTypeDefs.h:424
unsigned int MLuint32
Definition mlTypeDefs.h:185
double MLdouble
Definition mlTypeDefs.h:217
INT64 MLint64
Include 64-bit integer support for Windows or Unix.
Definition mlTypeDefs.h:411
signed int MLint32
Definition mlTypeDefs.h:161
float MLfloat
Definition mlTypeDefs.h:201
#define ML_UTILS_EXPORT
Definition mlUtilities.h:18