13 #ifndef ML_ABSTRACT_PERSISTENCE_STREAM_H
14 #define ML_ABSTRACT_PERSISTENCE_STREAM_H
22 #include <ThirdPartyWarningsDisable.h>
27 #include <ThirdPartyWarningsRestore.h>
29 ML_UTILS_START_NAMESPACE
34 template <
class T,
size_t size,
class DataContainer>
35 class FloatingPointVector;
38 template<
typename T,
int n>
42 template <
class VectorT,
size_t size>
43 class 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);
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;
441 ML_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)
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)
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)
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 ~AbstractPersistenceStream()
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.
AbstractPersistenceStream()
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...
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 ~PersistenceStreamException()
virtual std::string getMessage() const
Returns error message.
ErrorType getErrorType() const
Returns exception subtype.
ErrorType
Subtypes of this exception.
PersistenceStreamException(ErrorType type, const std::string &msg)
This is set to protected, because you should not 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.