ML Reference
|
00001 // **InsertLicense** code 00002 //------------------------------------------------------------------------- 00010 //------------------------------------------------------------------------- 00011 00012 #ifndef __mlAbstractPersistenceStream_H 00013 #define __mlAbstractPersistenceStream_H 00014 00015 #include "mlUtilsSystem.h" 00016 #include "mlTypeDefs.h" 00017 00018 #include "mlSystemWarningsDisable.h" 00019 #include <list> 00020 #include <set> 00021 #include "mlSystemWarningsRestore.h" 00022 00023 ML_UTILS_START_NAMESPACE 00024 00025 class Base; 00026 00027 // pre-define templates that are defined in MLLinearAlgebra 00028 template <class T, size_t size, class DataContainer> 00029 class FloatingPointVector; 00030 00031 // pre-define templates that are defined in MLLinearAlgebra 00032 template<typename T, int n> 00033 class TVectorNDBase; 00034 00035 // pre-define templates that are defined in MLLinearAlgebra 00036 template <class VectorT, size_t size> 00037 class FloatingPointMatrix; 00038 00039 00043 class ML_UTILS_EXPORT AbstractPersistenceStream 00044 { 00045 public: 00054 void startList(const char* name = NULL, const char* xmlItemName = "Item", bool xmlSuppressScope = false); 00055 00057 void endList(); 00058 00061 void startStruct(const char* name = NULL); 00062 00064 void endStruct(); 00065 00069 virtual bool isBinary() const = 0; 00070 00071 protected: 00072 AbstractPersistenceStream(); 00073 ~AbstractPersistenceStream(); 00074 00077 virtual void nameCheck(const char* name); 00078 00081 00082 00083 virtual void startListImpl(const char* name, const char* xmlItemName, bool xmlSuppressScope) = 0; 00084 virtual void endListImpl() = 0; 00085 virtual void startStructImpl(const char* name) = 0; 00086 virtual void endStructImpl() = 0; 00088 00090 bool isInList() const { return !_isListStack.empty() && _isListStack.back(); } 00091 00093 size_t nestingDepth() const { return _isListStack.size(); } 00094 00096 virtual void internalError(const char* msg, const char* arg = NULL); 00097 00098 private: 00100 std::list<bool> _isListStack; 00101 }; 00102 00103 // ----------------------------------------------------------------------------- 00104 00114 class ML_UTILS_EXPORT AbstractPersistenceOutputStream : public AbstractPersistenceStream 00115 { 00116 public: 00117 ~AbstractPersistenceOutputStream() {} 00118 00120 void startStructWithVersion(int version, const char* name = NULL); 00121 00123 00124 00125 virtual void write(bool value, const char* name = NULL); 00126 00127 void write(MLint32 value, const char* name = NULL); 00128 void write(MLuint32 value, const char* name = NULL); 00129 void write(MLint64 value, const char* name = NULL); 00130 void write(MLuint64 value, const char* name = NULL); 00131 void write(MLfloat value, const char* name = NULL); 00132 void write(MLdouble value, const char* name = NULL); 00133 void write(const std::string& value, const char* name = NULL); 00134 00135 void write(const char* value, const char* name = NULL) { write(std::string(value), name); } 00137 00140 template<typename T> 00141 void write(const std::vector<T>& values, const char* name = NULL) 00142 { 00143 nameCheck(name); 00144 writeValues(name, &values.front(), values.size(), false); 00145 } 00146 00148 template<typename T, size_t n, class DataContainer> 00149 void write(const FloatingPointVector<T, n, DataContainer>& value, const char* name = NULL) 00150 { 00151 nameCheck(name); 00152 writeValues(name, &value[0], n, true); 00153 } 00154 00156 template<typename T, size_t n> 00157 void write(const TVectorNDBase<T, n>& value, const char* name = NULL) 00158 { 00159 nameCheck(name); 00160 writeValues(name, &value.array, n, true); 00161 } 00162 00164 template<typename T, size_t n> 00165 void write(const FloatingPointMatrix<T, n>& value, const char* name = NULL) 00166 { 00167 nameCheck(name); 00168 typename T::ComponentType components[FloatingPointMatrix<T, n>::ComponentCount]; 00169 value.getValuesToPtr(components); 00170 writeValues(name, components, FloatingPointMatrix<T, n>::ComponentCount, true); 00171 } 00172 00175 virtual void writeObject(const Base* const obj, const char* name = NULL); 00176 00178 virtual void writeData(const void* data, size_t len, const char* name = NULL) = 0; 00179 00183 virtual bool isValidElementName(const char* name); 00184 00185 protected: 00186 AbstractPersistenceOutputStream() {} 00187 00191 00192 00193 virtual void setObjectID(int id) = 0; 00195 virtual void setObjectType(const char* type) = 0; 00197 virtual void setObjectVersion(int version) = 0; 00199 00203 00204 virtual void writeValues(const char* name, const MLint32* values, size_t n, bool fixedList) = 0; 00205 virtual void writeValues(const char* name, const MLuint32* values, size_t n, bool fixedList) = 0; 00206 virtual void writeValues(const char* name, const MLint64* values, size_t n, bool fixedList) = 0; 00207 virtual void writeValues(const char* name, const MLuint64* values, size_t n, bool fixedList) = 0; 00208 virtual void writeValues(const char* name, const MLfloat* values, size_t n, bool fixedList) = 0; 00209 virtual void writeValues(const char* name, const MLdouble* values, size_t n, bool fixedList) = 0; 00211 00213 virtual void writeString(const char* name, const std::string& value) = 0; 00214 00217 virtual void nameCheck(const char* name); 00218 }; 00219 00220 // ----------------------------------------------------------------------------- 00221 00232 class ML_UTILS_EXPORT AbstractPersistenceInputStream : public AbstractPersistenceStream 00233 { 00234 public: 00235 ~AbstractPersistenceInputStream() {} 00236 00238 int startStructWithVersion(const char* name = NULL); 00239 00241 00242 00243 virtual void read(bool& value, const char* name = NULL); 00244 00245 void read(MLint32& value, const char* name = NULL); 00246 void read(MLuint32& value, const char* name = NULL); 00247 void read(MLint64& value, const char* name = NULL); 00248 void read(MLuint64& value, const char* name = NULL); 00249 void read(MLfloat& value, const char* name = NULL); 00250 void read(MLdouble& value, const char* name = NULL); 00251 void read(std::string& value, const char* name = NULL); 00253 00256 template<typename T> 00257 void read(std::vector<T>& values, const char* name = NULL) 00258 { 00259 nameCheck(name); 00260 readValues(name, 0, values); 00261 } 00262 00264 template<typename T, size_t n, class DataContainer> 00265 void read(FloatingPointVector<T, n, DataContainer>& value, const char* name = NULL) 00266 { 00267 nameCheck(name); 00268 std::vector<T> buffer; 00269 readValues(name, n, buffer); 00270 for(size_t i=0;i<n;i++) { 00271 value[i] = buffer[i]; 00272 } 00273 } 00274 00276 template<typename T, size_t n> 00277 void read(TVectorNDBase<T, n>& value, const char* name = NULL) 00278 { 00279 nameCheck(name); 00280 std::vector<T> buffer; 00281 readValues(name, n, buffer); 00282 for(size_t i=0;i<n;i++) { 00283 value[i] = buffer[i]; 00284 } 00285 } 00286 00288 template<typename T, size_t n> 00289 void read(FloatingPointMatrix<T, n>& value, const char* name = NULL) 00290 { 00291 nameCheck(name); 00292 std::vector<typename T::ComponentType> buffer; 00293 readValues(name, n, buffer); 00294 value.setValuesFromPtr(buffer.data()); 00295 } 00296 00299 template<typename T> 00300 void readOptional(T& value, const T& defaultValue, const char* name = NULL) 00301 { 00302 if (_isAvailable(name)) { 00303 read(value, name); 00304 } else { 00305 value = defaultValue; 00306 } 00307 } 00308 00311 virtual Base* readObject(const char* name = NULL); 00312 00314 virtual void readData(std::string& value, const char* name = NULL) = 0; 00315 00318 bool hasNextInStruct(std::string& name); 00319 00322 bool isNextInStruct(const char* name); 00323 00326 bool hasNextListItem(); 00327 00329 void versionCheck(const char* className, int objectVersion, int storedVersion); 00330 00331 protected: 00332 AbstractPersistenceInputStream() {} 00333 00336 virtual bool hasNextInStructImpl(std::string& name) = 0; 00337 00340 virtual bool hasNextListItemImpl() = 0; 00341 00343 00344 00345 virtual bool getObjectID(int& id) = 0; 00347 virtual bool getObjectType(std::string& type) = 0; 00349 virtual bool getObjectVersion(int& version) = 0; 00351 00355 00356 virtual void readValues(const char* name, size_t expected, std::vector<MLint32>& values) = 0; 00357 virtual void readValues(const char* name, size_t expected, std::vector<MLuint32>& values) = 0; 00358 virtual void readValues(const char* name, size_t expected, std::vector<MLint64>& values) = 0; 00359 virtual void readValues(const char* name, size_t expected, std::vector<MLuint64>& values) = 0; 00360 virtual void readValues(const char* name, size_t expected, std::vector<MLfloat>& values) = 0; 00361 virtual void readValues(const char* name, size_t expected, std::vector<MLdouble>& values) = 0; 00363 00365 virtual void readString(const char* name, std::string& value) = 0; 00366 00368 virtual void formatError(const std::string& msg); 00369 00370 private: 00373 bool _isAvailable(const char* name); 00374 00376 std::set<std::string> _futureVersionClasses; 00377 }; 00378 00379 // ----------------------------------------------------------------------------- 00380 00383 class ML_UTILS_EXPORT PersistenceStreamException 00384 { 00385 public: 00387 enum ErrorType { 00388 IOError, 00389 FormatError, 00390 InternalError 00391 }; 00392 00393 virtual ~PersistenceStreamException() {} 00394 00396 ErrorType getErrorType() const { return _type; } 00397 00399 virtual std::string getMessage() const { return _msg; } 00400 00401 protected: 00403 PersistenceStreamException(ErrorType type, const std::string& msg) : _type(type), _msg(msg) {} 00404 00405 ErrorType _type; 00406 std::string _msg; 00407 }; 00408 00411 class ML_UTILS_EXPORT PersistenceStreamIOException : public PersistenceStreamException 00412 { 00413 public: 00414 PersistenceStreamIOException(const std::string& msg) 00415 : PersistenceStreamException(IOError, msg) {} 00416 }; 00417 00420 class ML_UTILS_EXPORT PersistenceStreamFormatException : public PersistenceStreamException 00421 { 00422 public: 00423 PersistenceStreamFormatException(const std::string& msg) 00424 : PersistenceStreamException(FormatError, msg) {} 00425 }; 00426 00428 class ML_UTILS_EXPORT PersistenceStreamInternalError : public PersistenceStreamException 00429 { 00430 public: 00431 PersistenceStreamInternalError(const std::string& msg) 00432 : PersistenceStreamException(FormatError, msg) {} 00433 }; 00434 00435 ML_UTILS_END_NAMESPACE 00436 // ----------------------------------------------------------------------------- 00437 00440 #define ML_WRITETO_SUPER(SuperClass, stream) { \ 00441 stream->startStructWithVersion(SuperClass::getAddStateVersion(), "_" #SuperClass); \ 00442 SuperClass::writeTo(stream); \ 00443 stream->endStruct(); \ 00444 } 00445 00448 #define ML_READFROM_SUPER(SuperClass, stream) { \ 00449 int version = stream->startStructWithVersion("_" #SuperClass); \ 00450 SuperClass::readFrom(stream, version); \ 00451 stream->versionCheck(SuperClass::getTypeId()->getName(), SuperClass::getAddStateVersion(), version); \ 00452 stream->endStruct(); \ 00453 } 00454 00455 // ----------------------------------------------------------------------------- 00456 00457 #endif