MeVisLab Toolbox Reference
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 
29 ML_UTILS_START_NAMESPACE
30 
31 class Base;
32 
33 // pre-define templates that are defined in MLLinearAlgebra
34 template <class T, size_t size, class DataContainer>
35 class FloatingPointVector;
36 
37 // pre-define templates that are defined in MLLinearAlgebra
38 template<typename T, int n>
39 class TVectorNDBase;
40 
41 // pre-define templates that are defined in MLLinearAlgebra
42 template <class VectorT, size_t size>
43 class FloatingPointMatrix;
44 
45 
50 {
51 public:
60  void startList(const char* name = nullptr, const char* xmlItemName = "Item", bool xmlSuppressScope = false);
61 
63  void endList();
64 
67  void startStruct(const char* name = nullptr);
68 
70  void endStruct();
71 
75  virtual bool isBinary() const = 0;
76 
77 protected:
80 
83  virtual void nameCheck(const char* name);
84 
89  virtual void startListImpl(const char* name, const char* xmlItemName, bool xmlSuppressScope) = 0;
90  virtual void endListImpl() = 0;
91  virtual void startStructImpl(const char* name) = 0;
92  virtual void endStructImpl() = 0;
94 
96  bool isInList() const { return !_isListStack.empty() && _isListStack.back(); }
97 
99  size_t nestingDepth() const { return _isListStack.size(); }
100 
102  virtual void internalError(const char* msg, const char* arg = nullptr);
103 
104 private:
106  std::list<bool> _isListStack;
107 };
108 
109 // -----------------------------------------------------------------------------
110 
121 {
122 public:
123  ~AbstractPersistenceOutputStream() override = default;
124 
126  void startStructWithVersion(int version, const char* name = nullptr);
127 
131  virtual void write(bool value, const char* name = nullptr);
132 
133  void write(MLint32 value, const char* name = nullptr);
134  void write(MLuint32 value, const char* name = nullptr);
135  void write(MLint64 value, const char* name = nullptr);
136  void write(MLuint64 value, const char* name = nullptr);
137  void write(MLfloat value, const char* name = nullptr);
138  void write(MLdouble value, const char* name = nullptr);
139  void write(const std::string& value, const char* name = nullptr);
140 
141  void write(const char* value, const char* name = nullptr) { write(std::string(value), name); }
143 
146  template<typename T>
147  void write(const std::vector<T>& values, const char* name = nullptr)
148  {
149  nameCheck(name);
150  writeValues(name, &values.front(), values.size(), false);
151  }
152 
154  template<typename T, size_t n, class DataContainer>
155  void write(const FloatingPointVector<T, n, DataContainer>& value, const char* name = nullptr)
156  {
157  nameCheck(name);
158  writeValues(name, &value[0], n, true);
159  }
160 
162  template<typename T, size_t n>
163  void write(const TVectorNDBase<T, n>& value, const char* name = nullptr)
164  {
165  nameCheck(name);
166  writeValues(name, &value.array, n, true);
167  }
168 
170  template<typename T, size_t n>
171  void write(const FloatingPointMatrix<T, n>& value, const char* name = nullptr)
172  {
173  nameCheck(name);
174  typename T::ComponentType components[FloatingPointMatrix<T, n>::ComponentCount];
175  value.getValuesToPtr(components);
176  writeValues(name, components, FloatingPointMatrix<T, n>::ComponentCount, true);
177  }
178 
181  virtual void writeObject(const Base* const obj, const char* name = nullptr);
182 
184  virtual void writeData(const void* data, size_t len, const char* name = nullptr) = 0;
185 
189  virtual bool isValidElementName(const char* name);
190 
191 protected:
193 
199  virtual void setObjectID(int id) = 0;
201  virtual void setObjectType(const char* type) = 0;
203  virtual void setObjectVersion(int version) = 0;
205 
210  virtual void writeValues(const char* name, const MLint32* values, size_t n, bool fixedList) = 0;
211  virtual void writeValues(const char* name, const MLuint32* values, size_t n, bool fixedList) = 0;
212  virtual void writeValues(const char* name, const MLint64* values, size_t n, bool fixedList) = 0;
213  virtual void writeValues(const char* name, const MLuint64* values, size_t n, bool fixedList) = 0;
214  virtual void writeValues(const char* name, const MLfloat* values, size_t n, bool fixedList) = 0;
215  virtual void writeValues(const char* name, const MLdouble* values, size_t n, bool fixedList) = 0;
217 
219  virtual void writeString(const char* name, const std::string& value) = 0;
220 
223  void nameCheck(const char* name) override;
224 };
225 
226 // -----------------------------------------------------------------------------
227 
239 {
240 public:
242 
244  int startStructWithVersion(const char* name = nullptr);
245 
249  virtual void read(bool& value, const char* name = nullptr);
250 
251  void read(MLint32& value, const char* name = nullptr);
252  void read(MLuint32& value, const char* name = nullptr);
253  void read(MLint64& value, const char* name = nullptr);
254  void read(MLuint64& value, const char* name = nullptr);
255  void read(MLfloat& value, const char* name = nullptr);
256  void read(MLdouble& value, const char* name = nullptr);
257  void read(std::string& value, const char* name = nullptr);
259 
262  template<typename T>
263  void read(std::vector<T>& values, const char* name = nullptr)
264  {
265  nameCheck(name);
266  readValues(name, 0, values);
267  }
268 
270  template<typename T, size_t n, class DataContainer>
271  void read(FloatingPointVector<T, n, DataContainer>& value, const char* name = nullptr)
272  {
273  nameCheck(name);
274  std::vector<T> buffer;
275  readValues(name, n, buffer);
276  for(size_t i=0;i<n;i++) {
277  value[i] = buffer[i];
278  }
279  }
280 
282  template<typename T, size_t n>
283  void read(TVectorNDBase<T, n>& value, const char* name = nullptr)
284  {
285  nameCheck(name);
286  std::vector<T> buffer;
287  readValues(name, n, buffer);
288  for(size_t i=0;i<n;i++) {
289  value[i] = buffer[i];
290  }
291  }
292 
294  template<typename T, size_t n>
295  void read(FloatingPointMatrix<T, n>& value, const char* name = nullptr)
296  {
297  nameCheck(name);
298  std::vector<typename T::ComponentType> buffer;
299  readValues(name, n, buffer);
300  value.setValuesFromPtr(buffer.data());
301  }
302 
305  template<typename T>
306  void readOptional(T& value, const T& defaultValue, const char* name = nullptr)
307  {
308  if (_isAvailable(name)) {
309  read(value, name);
310  } else {
311  value = defaultValue;
312  }
313  }
314 
317  virtual Base* readObject(const char* name = nullptr);
318 
320  virtual void readData(std::string& value, const char* name = nullptr) = 0;
321 
324  bool hasNextInStruct(std::string& name);
325 
328  bool isNextInStruct(const char* name);
329 
333 
335  void versionCheck(const char* className, int objectVersion, int storedVersion);
336 
337 protected:
339 
342  virtual bool hasNextInStructImpl(std::string& name) = 0;
343 
346  virtual bool hasNextListItemImpl() = 0;
347 
351  virtual bool getObjectID(int& id) = 0;
353  virtual bool getObjectType(std::string& type) = 0;
355  virtual bool getObjectVersion(int& version) = 0;
357 
362  virtual void readValues(const char* name, size_t expected, std::vector<MLint32>& values) = 0;
363  virtual void readValues(const char* name, size_t expected, std::vector<MLuint32>& values) = 0;
364  virtual void readValues(const char* name, size_t expected, std::vector<MLint64>& values) = 0;
365  virtual void readValues(const char* name, size_t expected, std::vector<MLuint64>& values) = 0;
366  virtual void readValues(const char* name, size_t expected, std::vector<MLfloat>& values) = 0;
367  virtual void readValues(const char* name, size_t expected, std::vector<MLdouble>& values) = 0;
369 
371  virtual void readString(const char* name, std::string& value) = 0;
372 
374  virtual void formatError(const std::string& msg);
375 
376 private:
379  bool _isAvailable(const char* name);
380 
382  std::set<std::string> _futureVersionClasses;
383 };
384 
385 // -----------------------------------------------------------------------------
386 
390 {
391 public:
393  enum ErrorType {
396  InternalError
397  };
398 
400 
402  ErrorType getErrorType() const { return _type; }
403 
405  virtual std::string getMessage() const { return _msg; }
406 
407 protected:
409  PersistenceStreamException(ErrorType type, const std::string& msg) : _type(type), _msg(msg) {}
410 
412  std::string _msg;
413 };
414 
418 {
419 public:
420  PersistenceStreamIOException(const std::string& msg)
421  : PersistenceStreamException(IOError, msg) {}
422 };
423 
427 {
428 public:
429  PersistenceStreamFormatException(const std::string& msg)
430  : PersistenceStreamException(FormatError, msg) {}
431 };
432 
435 {
436 public:
437  PersistenceStreamInternalError(const std::string& msg)
438  : PersistenceStreamException(FormatError, msg) {}
439 };
440 
441 ML_UTILS_END_NAMESPACE
442 // -----------------------------------------------------------------------------
443 
446 #define ML_WRITETO_SUPER(SuperClass, stream) { \
447  stream->startStructWithVersion(SuperClass::getAddStateVersion(), "_" #SuperClass); \
448  SuperClass::writeTo(stream); \
449  stream->endStruct(); \
450 }
451 
454 #define ML_READFROM_SUPER(SuperClass, stream) { \
455  int version = stream->startStructWithVersion("_" #SuperClass); \
456  SuperClass::readFrom(stream, version); \
457  stream->versionCheck(SuperClass::getTypeId()->getName(), SuperClass::getAddStateVersion(), version); \
458  stream->endStruct(); \
459 }
460 
461 // -----------------------------------------------------------------------------
462 
463 #endif
@ T
Definition: SoKeyGrabber.h:71
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)
Read vectors of primitive values from the stream.
virtual bool getObjectVersion(int &version)=0
persistence version
virtual bool getObjectType(std::string &type)=0
base type name
void read(MLint32 &value, const char *name=nullptr)
virtual void formatError(const std::string &msg)
indicate format error, by default throw exception
virtual void readData(std::string &value, const char *name=nullptr)=0
Same as above for binary data (std::string misused as binary data container)
void read(MLuint32 &value, const char *name=nullptr)
virtual void read(bool &value, const char *name=nullptr)
Read primitive values from the stream.
void versionCheck(const char *className, int objectVersion, int storedVersion)
print a warning if the storedVersion is greater than the objectVersion
virtual Base * readObject(const char *name=nullptr)
Read Base object from stream.
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
Check if an item with the given name comes next in the stream.
bool isNextInStruct(const char *name)
Check if an item with the given name comes next in the stream.
void read(FloatingPointMatrix< T, n > &value, const char *name=nullptr)
Read 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 structget unique id of obj...
int startStructWithVersion(const char *name=nullptr)
start a struct, returns version of struct (0 if not given)
bool hasNextListItem()
Check if there is another item in the current list.
void read(FloatingPointVector< T, n, DataContainer > &value, const char *name=nullptr)
Read 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 then sets name to its...
virtual bool hasNextListItemImpl()=0
Check if there is another item in the current list.
void read(TVectorNDBase< T, n > &value, const char *name=nullptr)
Read 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)
Perform 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)
Write 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)
start 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, set attribute on currently open struct (implementation hin...
virtual void setObjectType(const char *type)=0
base type name
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)
check if name is valid for use with persistence entries (by default this checks if the name is valid ...
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
persistence version
virtual void writeObject(const Base *const obj, const char *name=nullptr)
Write a Base object to the stream.
void nameCheck(const char *name) override
overriden to perform additional check in Debug mode if name is suitable for the persistence format (u...
virtual void writeData(const void *data, size_t len, const char *name=nullptr)=0
Write binary data to stream.
void write(const FloatingPointMatrix< T, n > &value, const char *name=nullptr)
Write 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)
Write primitive values to the stream.
void write(const TVectorNDBase< T, n > &value, const char *name=nullptr)
Write integer vectors like ImageVector to the stream.
void write(const std::vector< T > &values, const char *name=nullptr)
Write 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)
Log an internal error. Usually called to indicate wrong usage of interface.
virtual bool isBinary() const =0
Is the stream in a binary format? (otherwise in a readable text format)
bool isInList() const
Check if the stream is currently in "list" mode. Otherwise it is in "struct" mode.
void endList()
End reading/writing of the list.
virtual void nameCheck(const char *name)
Check if the name is provided when being in a sub-group, or that no name is provided when being in a ...
size_t nestingDepth() const
return depth of nesting stack (created by startList/startStruct), used to check nesting rules
void endStruct()
End 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)
Start 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, need to be implemented in derived classes.
void startList(const char *name=nullptr, const char *xmlItemName="Item", bool xmlSuppressScope=false)
Start 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:62
Base class of all matrix classes which holds the data buffer and provides some general access methods...
void getValuesToPtr(ComponentType *values) const
Copies contents of *this into array mat, row by row; type and size must match.
void setValuesFromPtr(const ComponentType *const values)
Copies contents from array mat into *this, row by row; type and size must match.
Template class for vector arithmetic with floating point data types.
This class represents the exceptions that can be thrown while reading from or writing to the persiste...
virtual std::string getMessage() const
get error message
ErrorType getErrorType() const
get exception sub-type
PersistenceStreamException(ErrorType type, const std::string &msg)
you shouldn't create instances of the base class...
PersistenceStreamIOException(const std::string &msg)
Derived class. This exception usually denotes programming errors.
PersistenceStreamInternalError(const std::string &msg)
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,...
UINT64 MLuint64
Introduce platform independent 64 bit unsigned integer type.
Definition: mlTypeDefs.h:513
unsigned int MLuint32
Definition: mlTypeDefs.h:191
double MLdouble
Definition: mlTypeDefs.h:223
INT64 MLint64
Include 64 bit integer support for Windows or Unix.
Definition: mlTypeDefs.h:500
signed int MLint32
Definition: mlTypeDefs.h:167
float MLfloat
Definition: mlTypeDefs.h:207
#define ML_UTILS_EXPORT
Defines platform dependent DLL export macro for mlUtils.
Definition: mlUtilities.h:20