00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00039 #ifndef BLOCXX_BinarySerialization_HPP_
00040 #define BLOCXX_BinarySerialization_HPP_
00041 #include "blocxx/BLOCXX_config.h"
00042 #include "blocxx/Types.hpp"
00043 #include "blocxx/Bool.hpp"
00044 #include "blocxx/String.hpp"
00045 #include "blocxx/Array.hpp"
00046 #include "blocxx/ByteSwap.hpp"
00047 #include "IOException.hpp"
00048
00049 #include <iosfwd>
00050
00051
00052
00053
00054 namespace BLOCXX_NAMESPACE
00055 {
00056
00057 namespace BinarySerialization
00058 {
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 const UInt32 BinaryProtocolVersion = 4000002;
00074
00075
00076 const UInt32 MinBinaryProtocolVersion = 3000007;
00077
00078
00079 const UInt8 BIN_OK = 0;
00080 const UInt8 BIN_ERROR = 1;
00081 const UInt8 BIN_EXCEPTION = 2;
00082 const UInt8 BIN_END = 3;
00083
00084 const UInt8 BIN_LOG_MESSAGE = 45;
00085
00086
00087 const UInt8 BINSIG_BOOL = 104;
00088 const UInt8 BINSIG_STR = 106;
00089 const UInt8 BINSIG_STRARRAY = 107;
00090
00091 const UInt8 BINSIG_STRINGENUM = 115;
00092
00093 const UInt8 END_STRINGENUM = 154;
00094
00095
00096 BLOCXX_COMMON_API void verifySignature(std::streambuf & istrm, UInt8 validSig);
00097
00099 template <typename Handler, typename ReaderFunc>
00100 inline void readEnum(
00101 std::streambuf & istrm, Handler & result,
00102 const ReaderFunc & read, const UInt8 beginsig, const UInt8 endsig)
00103 {
00104 verifySignature(istrm, beginsig);
00105 bool done = false;
00106 while (!done)
00107 {
00108 try
00109 {
00110 result.handle(read(istrm));
00111 }
00112 catch (const BadSignatureException& e)
00113 {
00114
00115 verifySignature(istrm, endsig);
00116 done = true;
00117 }
00118 }
00119 }
00121 BLOCXX_COMMON_API void write(
00122 std::streambuf & ostrm, const void * dataOut, size_t dataOutLen
00123 );
00124
00125 inline void write(std::streambuf & ostrm, Int32 val)
00126 {
00127 val = hton32(val);
00128 BinarySerialization::write(ostrm, &val, sizeof(val));
00129 }
00130
00131 inline void write(std::streambuf & ostrm, UInt32 val)
00132 {
00133 val = hton32(val);
00134 BinarySerialization::write(ostrm, &val, sizeof(val));
00135 }
00136
00137 BLOCXX_COMMON_API void writeLen(std::streambuf & ostrm, UInt32 len);
00138
00139 inline void write(std::streambuf & ostrm, UInt8 val)
00140 {
00141 BinarySerialization::write(ostrm, &val, sizeof(val));
00142 }
00143
00144 inline void write(std::streambuf & ostrm, UInt16 val)
00145 {
00146 val = hton16(val);
00147 BinarySerialization::write(ostrm, &val, sizeof(val));
00148 }
00149
00150 inline void write(std::streambuf & ostrm, Int16 val)
00151 {
00152 val = hton16(val);
00153 BinarySerialization::write(ostrm, &val, sizeof(val));
00154 }
00155
00156 inline void write(std::streambuf & ostrm, UInt64 val)
00157 {
00158 val = hton64(val);
00159 BinarySerialization::write(ostrm, &val, sizeof(val));
00160 }
00161
00162 inline void write(std::streambuf & ostrm, Int64 val)
00163 {
00164 val = hton64(val);
00165 BinarySerialization::write(ostrm, &val, sizeof(val));
00166 }
00167
00168 inline void write(std::streambuf & ostrm, const String & str)
00169 {
00170 str.writeObject(ostrm);
00171 }
00172
00173 inline void writeBool(std::streambuf & ostrm, Bool arg)
00174 {
00175 BinarySerialization::write(ostrm, BINSIG_BOOL);
00176 arg.writeObject(ostrm);
00177 }
00178
00179 inline void writeString(std::streambuf & ostrm, const String & str)
00180 {
00181 BinarySerialization::write(ostrm, BINSIG_STR);
00182 str.writeObject(ostrm);
00183 }
00184
00185 BLOCXX_COMMON_API void readLen(std::streambuf & istrm, UInt32 & len);
00186
00188 template <typename T>
00189 inline void
00190 readArray(std::streambuf & istr, T & a)
00191 {
00192 a.clear();
00193 UInt32 len;
00194 BinarySerialization::readLen(istr, len);
00195
00196 a.reserve(len);
00197 for (UInt32 i = 0; i < len; i++)
00198 {
00199 typename T::value_type x;
00200 x.readObject(istr);
00201 a.push_back(x);
00202 }
00203 }
00204
00206 template <typename T>
00207 inline void
00208 writeArray(std::streambuf & ostrm, const T & a)
00209 {
00210 UInt32 len = static_cast<UInt32>(a.size());
00211 BinarySerialization::writeLen(ostrm, len);
00212 for (UInt32 i = 0; i < len; i++)
00213 {
00214 a.operator[](i).writeObject(ostrm);
00215 }
00216 }
00217
00218 inline void writeStringArray(
00219 std::streambuf & ostrm, const StringArray & stra
00220 )
00221 {
00222 BinarySerialization::write(ostrm, BINSIG_STRARRAY);
00223 writeArray(ostrm, stra);
00224 }
00225
00226 BLOCXX_COMMON_API void writeStringArray(
00227 std::streambuf & ostrm, const StringArray * propertyList
00228 );
00229
00230
00231 BLOCXX_COMMON_API void read(
00232 std::streambuf & istrm, void * dataIn, size_t dataInLen
00233 );
00234
00235 inline void read(std::streambuf & istrm, String & arg)
00236 {
00237 arg.readObject(istrm);
00238 }
00239
00240 inline void read(std::streambuf & istrm, UInt64 & val)
00241 {
00242 BinarySerialization::read(istrm, &val, sizeof(val));
00243 val = ntoh64(val);
00244 }
00245
00246 inline void read(std::streambuf & istrm, Int64 & val)
00247 {
00248 BinarySerialization::read(istrm, &val, sizeof(val));
00249 val = ntoh64(val);
00250 }
00251
00252 inline void read(std::streambuf & istrm, Int32 & val)
00253 {
00254 BinarySerialization::read(istrm, &val, sizeof(val));
00255 val = ntoh32(val);
00256 }
00257
00258 inline void read(std::streambuf & istrm, UInt32 & val)
00259 {
00260 BinarySerialization::read(istrm, &val, sizeof(val));
00261 val = ntoh32(val);
00262 }
00263
00264 inline void read(std::streambuf & istrm, UInt16 & val)
00265 {
00266 BinarySerialization::read(istrm, &val, sizeof(val));
00267 val = ntoh16(val);
00268 }
00269
00270 inline void read(std::streambuf & istrm, Int16 & val)
00271 {
00272 BinarySerialization::read(istrm, &val, sizeof(val));
00273 val = ntoh16(val);
00274 }
00275
00276 inline void read(std::streambuf & istrm, UInt8 & val)
00277 {
00278 BinarySerialization::read(istrm, &val, sizeof(val));
00279 }
00280
00281 inline Bool readBool(std::streambuf & istrm)
00282 {
00283 BinarySerialization::verifySignature(istrm, BINSIG_BOOL);
00284 Bool b;
00285 b.readObject(istrm);
00286 return b;
00287 }
00288
00289 inline String readString(std::streambuf & istrm)
00290 {
00291 BinarySerialization::verifySignature(istrm, BINSIG_STR);
00292 String rv;
00293 rv.readObject(istrm);
00294 return rv;
00295 }
00296
00297 inline StringArray readStringArray(std::streambuf & istrm)
00298 {
00299 BinarySerialization::verifySignature(istrm, BINSIG_STRARRAY);
00300 StringArray stra;
00301 readArray(istrm, stra);
00302 return stra;
00303 }
00304
00305 }
00306
00307 }
00308
00309 #endif // BLOCXX_BinarySerialization_HPP_