SerUt  1.1.1 (development version)
serializationinterface.h
Go to the documentation of this file.
1 /*
2 
3  This file is a part of SerUt, a library containing some serialization
4  utilities.
5 
6  Copyright (C) 2008-2018 Jori Liesenborgs
7 
8  Contact: jori.liesenborgs@gmail.com
9 
10  This library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU Lesser General Public
12  License as published by the Free Software Foundation; either
13  version 2.1 of the License, or (at your option) any later version.
14 
15  This library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public
21  License along with this library; if not, write to the Free Software
22  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23  USA
24 
25 */
26 
31 #ifndef SERUT_SERIALIZATIONINTERFACE_H
32 
33 #define SERUT_SERIALIZATIONINTERFACE_H
34 
35 #include "serutconfig.h"
36 #include <errut/errorbase.h>
37 #include <sys/types.h>
38 #include <stdint.h>
39 #include <vector>
40 
41 namespace serut
42 {
43 
57 class SERUT_IMPORTEXPORT SerializationInterface : public errut::ErrorBase
58 {
59 public:
62 
69  virtual bool readBytes(void *pBuffer, size_t amount) = 0;
70 
76  virtual bool writeBytes(const void *pBuffer, size_t amount) = 0;
77 
79  bool writeLongDouble(double x) { return writeSingle(x); }
80 
82  bool writeDouble(double x) { return writeSingle(x); }
83 
85  bool writeFloat(float x) { return writeSingle(x); }
86 
88  bool writeInt32(int32_t x) { return writeSingle(x); }
89 
91  bool writeLongDoubles(const long double *pX, size_t amount) { return writeArray(pX, amount); }
92 
94  bool writeDoubles(const double *pX, size_t amount) { return writeArray(pX, amount); }
95 
97  bool writeFloats(const float *pX, size_t amount) { return writeArray(pX, amount); }
98 
100  bool writeInt32s(const int32_t *pX, size_t amount) { return writeArray(pX, amount); }
101 
103  bool writeLongDoubles(const std::vector<long double> &x) { return writeStdVector(x); }
104 
106  bool writeDoubles(const std::vector<double> &x) { return writeStdVector(x); }
107 
109  bool writeFloats(const std::vector<float> &x) { return writeStdVector(x); }
110 
112  bool writeInt32s(const std::vector<int32_t> &x) { return writeStdVector(x); }
113 
115  bool writeString(const std::string &x);
116 
118  bool readLongDouble(long double *pX) { return readSingle(pX); }
119 
121  bool readDouble(double *pX) { return readSingle(pX); }
122 
124  bool readFloat(float *pX) { return readSingle(pX); }
125 
127  bool readInt32(int32_t *pX) { return readSingle(pX); }
128 
130  bool readLongDoubles(long double *pX, size_t amount) { return readArray(pX, amount); }
131 
133  bool readDoubles(double *pX, size_t amount) { return readArray(pX, amount); }
134 
136  bool readFloats(float *pX, size_t amount) { return readArray(pX, amount); }
137 
139  bool readInt32s(int32_t *pX, size_t amount) { return readArray(pX, amount); }
140 
142  bool readLongDoubles(std::vector<long double> &x) { return readStdVector(x); }
143 
145  bool readDoubles(std::vector<double> &x) { return readStdVector(x); }
146 
148  bool readFloats(std::vector<float> &x) { return readStdVector(x); }
149 
151  bool readInt32s(std::vector<int32_t> &x) { return readStdVector(x); }
152 
154  bool readString(std::string &x);
155 private:
156  template<class T>
157  bool writeSingle(T x)
158  {
159  if (!writeBytes(&x, sizeof(T)))
160  return false;
161  return true;
162  }
163 
164  template<class T>
165  bool readSingle(T *x)
166  {
167  if (!readBytes(x, sizeof(T)))
168  return false;
169  return true;
170  }
171 
172  template<class T>
173  bool writeArray(const T *x, size_t amount)
174  {
175  if (!writeBytes(x, sizeof(T)*amount))
176  return false;
177  return true;
178  }
179 
180  template<class T>
181  bool readArray(T *x, size_t amount)
182  {
183  if (!readBytes(x, sizeof(T)*amount))
184  return false;
185  return true;
186  }
187 
188  template<class T>
189  bool writeStdVector(const std::vector<T> &v)
190  {
191  return writeArray(&(v[0]), v.size());
192  }
193 
194  template<class T>
195  bool readStdVector(std::vector<T> &v)
196  {
197  return readArray(&(v[0]), v.size());
198  }
199 };
200 
201 } // end namespace
202 
203 #endif // SERUT_SERIALIZATIONINTERFACE_H
204 
Generic serialization interface.
Definition: serializationinterface.h:58
bool readFloats(std::vector< float > &x)
Read a vector of floats (the current size of the vector specifies the amount).
Definition: serializationinterface.h:148
bool readDoubles(std::vector< double > &x)
Read a vector of doubles (the current size of the vector specifies the amount).
Definition: serializationinterface.h:145
bool readLongDouble(long double *pX)
Read a long double.
Definition: serializationinterface.h:118
bool writeLongDouble(double x)
Write a long double.
Definition: serializationinterface.h:79
bool writeInt32(int32_t x)
Write a 32-bit integer.
Definition: serializationinterface.h:88
bool writeInt32s(const std::vector< int32_t > &x)
Write the 32-bit integers which are stored in the vector.
Definition: serializationinterface.h:112
bool writeLongDoubles(const std::vector< long double > &x)
Write the long doubles stored in the vector.
Definition: serializationinterface.h:103
bool writeDoubles(const std::vector< double > &x)
Write the doubles stored in the vector.
Definition: serializationinterface.h:106
bool readString(std::string &x)
Read a string.
bool readInt32(int32_t *pX)
Read a 32-bit integer.
Definition: serializationinterface.h:127
bool readDouble(double *pX)
Read a double.
Definition: serializationinterface.h:121
bool readDoubles(double *pX, size_t amount)
Read an array of doubles.
Definition: serializationinterface.h:133
bool writeInt32s(const int32_t *pX, size_t amount)
Write an array of 32-bit integers.
Definition: serializationinterface.h:100
bool readLongDoubles(std::vector< long double > &x)
Read a vector of long doubles (the current size of the vector specifies the amount).
Definition: serializationinterface.h:142
bool readInt32s(int32_t *pX, size_t amount)
Read an array of 32-bit integers.
Definition: serializationinterface.h:139
bool writeDouble(double x)
Write a double.
Definition: serializationinterface.h:82
bool writeString(const std::string &x)
Write a string.
bool readInt32s(std::vector< int32_t > &x)
Read a vector of 32-bit integers (the current size of the vector specifies the amount).
Definition: serializationinterface.h:151
bool readFloats(float *pX, size_t amount)
Read an array of floats.
Definition: serializationinterface.h:136
virtual bool writeBytes(const void *pBuffer, size_t amount)=0
Write a specific amount of data to the serializer.
bool readFloat(float *pX)
Read a float.
Definition: serializationinterface.h:124
bool writeLongDoubles(const long double *pX, size_t amount)
Write an array of doubles.
Definition: serializationinterface.h:91
bool writeFloats(const float *pX, size_t amount)
Write an array of floats.
Definition: serializationinterface.h:97
bool writeFloats(const std::vector< float > &x)
Write the floats stored in the vector.
Definition: serializationinterface.h:109
bool writeDoubles(const double *pX, size_t amount)
Write an array of doubles.
Definition: serializationinterface.h:94
bool writeFloat(float x)
Write a float.
Definition: serializationinterface.h:85
bool readLongDoubles(long double *pX, size_t amount)
Read an array of long doubles.
Definition: serializationinterface.h:130
virtual bool readBytes(void *pBuffer, size_t amount)=0
Read a specific amount of data from the serializer.