13#ifndef ML_ABSTRACT_PERSISTENCE_STREAM_H
14#define ML_ABSTRACT_PERSISTENCE_STREAM_H
22#include <ThirdPartyWarningsDisable.h>
27#include <ThirdPartyWarningsRestore.h>
29ML_UTILS_START_NAMESPACE
34template <
class T,
size_t size,
class DataContainer>
35class FloatingPointVector;
38template<
typename T,
int n>
42template <
class VectorT,
size_t size>
43class FloatingPointMatrix;
60 void startList(
const char* name =
nullptr,
const char* xmlItemName =
"Item",
bool xmlSuppressScope =
false);
89 virtual void startListImpl(
const char* name,
const char* xmlItemName,
bool xmlSuppressScope) = 0;
96 bool isInList()
const {
return !_isListStack.empty() && _isListStack.back(); }
106 std::list<bool> _isListStack;
131 virtual void write(
bool value,
const char* name =
nullptr);
139 void write(
const std::string& value,
const char* name =
nullptr);
141 void write(
const char* value,
const char* name =
nullptr) {
write(std::string(value), name); }
147 void write(
const std::vector<T>& values,
const char* name =
nullptr)
150 writeValues(name, &values.front(), values.size(),
false);
154 template<
typename T,
size_t n,
class DataContainer>
158 writeValues(name, &value[0], n,
true);
162 template<
typename T,
size_t n>
166 writeValues(name, &value.
array, n,
true);
170 template<
typename T,
size_t n>
184 virtual void writeData(
const void* data,
size_t len,
const char* name =
nullptr) = 0;
219 virtual void writeString(
const char* name,
const std::string& value) = 0;
249 virtual void read(
bool& value,
const char* name =
nullptr);
257 void read(std::string& value,
const char* name =
nullptr);
263 void read(std::vector<T>& values,
const char* name =
nullptr)
266 readValues(name, 0, values);
270 template<
typename T,
size_t n,
class DataContainer>
274 std::vector<T> buffer;
275 readValues(name, n, buffer);
276 for(
size_t i=0;i<n;i++) {
277 value[i] = buffer[i];
282 template<
typename T,
size_t n>
286 std::vector<T> buffer;
287 readValues(name, n, buffer);
288 for(
size_t i=0;i<n;i++) {
289 value[i] = buffer[i];
294 template<
typename T,
size_t n>
298 std::vector<typename T::ComponentType> buffer;
299 readValues(name, n, buffer);
306 void readOptional(T& value,
const T& defaultValue,
const char* name =
nullptr)
308 if (_isAvailable(name)) {
311 value = defaultValue;
320 virtual void readData(std::string& value,
const char* name =
nullptr) = 0;
335 void versionCheck(
const char* className,
int objectVersion,
int storedVersion);
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;
371 virtual void readString(
const char* name, std::string& value) = 0;
379 bool _isAvailable(
const char* name);
382 std::set<std::string> _futureVersionClasses;
441ML_UTILS_END_NAMESPACE
446#define ML_WRITETO_SUPER(SuperClass, stream) { \
447 stream->startStructWithVersion(SuperClass::getAddStateVersion(), "_" #SuperClass); \
448 SuperClass::writeTo(stream); \
449 stream->endStruct(); \
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(); \
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)
AbstractPersistenceOutputStream()
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 ~AbstractPersistenceStream()
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.
AbstractPersistenceStream()
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...
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 ~PersistenceStreamException()
virtual std::string getMessage() const
get error message
ErrorType getErrorType() const
get exception sub-type
ErrorType
sub types of this exception
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.
INT64 MLint64
Include 64 bit integer support for Windows or Unix.
#define ML_UTILS_EXPORT
Defines platform dependent DLL export macro for mlUtils.