SerUt

serializationinterface.h

Go to the documentation of this file.
00001 /*
00002     
00003   This file is a part of SerUt, a library containing some serialization
00004   utilities.
00005   
00006   Copyright (C) 2008-2012 Jori Liesenborgs
00007 
00008   Contact: jori.liesenborgs@gmail.com
00009 
00010   This library is free software; you can redistribute it and/or
00011   modify it under the terms of the GNU Lesser General Public
00012   License as published by the Free Software Foundation; either
00013   version 2.1 of the License, or (at your option) any later version.
00014 
00015   This library is distributed in the hope that it will be useful,
00016   but WITHOUT ANY WARRANTY; without even the implied warranty of
00017   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018   Lesser General Public License for more details.
00019 
00020   You should have received a copy of the GNU Lesser General Public
00021   License along with this library; if not, write to the Free Software
00022   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  
00023   USA
00024 
00025 */
00026 
00031 #ifndef SERUT_SERIALIZATIONINTERFACE_H
00032 
00033 #define SERUT_SERIALIZATIONINTERFACE_H
00034 
00035 #include "serutconfig.h"
00036 #include <errut/errorbase.h>
00037 #include <sys/types.h>
00038 #include <stdint.h>
00039 #include <vector>
00040 
00041 namespace serut
00042 {
00043 
00057 class SERUT_IMPORTEXPORT SerializationInterface : public errut::ErrorBase
00058 {
00059 public:
00060         SerializationInterface()                                                { }
00061         ~SerializationInterface()                                               { }
00062         
00069         virtual bool readBytes(void *pBuffer, size_t amount) = 0;
00070 
00076         virtual bool writeBytes(const void *pBuffer, size_t amount) = 0;
00077 
00079         bool writeLongDouble(double x)                                          { return writeSingle(x); }
00080 
00082         bool writeDouble(double x)                                              { return writeSingle(x); }
00083 
00085         bool writeFloat(float x)                                                { return writeSingle(x); }
00086 
00088         bool writeInt32(int32_t x)                                              { return writeSingle(x); }
00089         
00091         bool writeLongDoubles(const long double *pX, size_t amount)             { return writeArray(pX, amount); }
00092 
00094         bool writeDoubles(const double *pX, size_t amount)                      { return writeArray(pX, amount); }
00095 
00097         bool writeFloats(const float *pX, size_t amount)                        { return writeArray(pX, amount); }
00098 
00100         bool writeInt32s(const int32_t *pX, size_t amount)                      { return writeArray(pX, amount); }
00101 
00103         bool writeLongDoubles(const std::vector<long double> &x)                { return writeStdVector(x); }
00104 
00106         bool writeDoubles(const std::vector<double> &x)                         { return writeStdVector(x); }
00107 
00109         bool writeFloats(const std::vector<float> &x)                           { return writeStdVector(x); }
00110 
00112         bool writeInt32s(const std::vector<int32_t> &x)                         { return writeStdVector(x); }
00113 
00115         bool writeString(const std::string &x);
00116         
00118         bool readLongDouble(long double *pX)                                    { return readSingle(pX); }
00119 
00121         bool readDouble(double *pX)                                             { return readSingle(pX); }
00122 
00124         bool readFloat(float *pX)                                               { return readSingle(pX); }
00125 
00127         bool readInt32(int32_t *pX)                                             { return readSingle(pX); }
00128 
00130         bool readLongDoubles(long double *pX, size_t amount)                    { return readArray(pX, amount); }
00131 
00133         bool readDoubles(double *pX, size_t amount)                             { return readArray(pX, amount); }
00134 
00136         bool readFloats(float *pX, size_t amount)                               { return readArray(pX, amount); }
00137 
00139         bool readInt32s(int32_t *pX, size_t amount)                             { return readArray(pX, amount); }
00140 
00142         bool readLongDoubles(std::vector<long double> &x)                       { return readStdVector(x); }
00143 
00145         bool readDoubles(std::vector<double> &x)                                { return readStdVector(x); }
00146 
00148         bool readFloats(std::vector<float> &x)                                  { return readStdVector(x); }
00149 
00151         bool readInt32s(std::vector<int32_t> &x)                                { return readStdVector(x); }
00152 
00154         bool readString(std::string &x);
00155 private:
00156         template<class T>
00157         bool writeSingle(T x)
00158         {
00159                 if (!writeBytes(&x, sizeof(T)))
00160                         return false;
00161                 return true;
00162         }
00163 
00164         template<class T>
00165         bool readSingle(T *x)
00166         {
00167                 if (!readBytes(x, sizeof(T)))
00168                         return false;
00169                 return true;
00170         }
00171 
00172         template<class T>
00173         bool writeArray(const T *x, size_t amount)
00174         {
00175                 if (!writeBytes(x, sizeof(T)*amount))
00176                         return false;
00177                 return true;
00178         }
00179 
00180         template<class T>
00181         bool readArray(T *x, size_t amount)
00182         {
00183                 if (!readBytes(x, sizeof(T)*amount))
00184                         return false;
00185                 return true;
00186         }
00187 
00188         template<class T>
00189         bool writeStdVector(const std::vector<T> &v)
00190         {
00191                 return writeArray(&(v[0]), v.size());
00192         }
00193 
00194         template<class T>
00195         bool readStdVector(std::vector<T> &v)
00196         {
00197                 return readArray(&(v[0]), v.size());
00198         }
00199 };
00200 
00201 } // end namespace
00202 
00203 #endif // SERUT_SERIALIZATIONINTERFACE_H
00204