00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef _GLASS_PACKAGER_HH_
00022 #define _GLASS_PACKAGER_HH_
00023
00024 #include <iostream>
00025 #include "chain.h"
00026 #include "types.h"
00027 #include <sstream>
00028
00029
00030
00031 namespace libglass {
00032
00033 #define Serializer(name, type) \
00034 bool (name)(chain &, type const &, unsigned int, void *)
00035 #define Unserializer(name, type) \
00036 bool (name)(const chain &, unsigned int, type &, unsigned int &, \
00037 unsigned int, void *)
00038
00039
00040
00041
00042
00053 template<typename T>
00054 bool toString(chain &c, T const &t, unsigned int nmemb = 1, void *extra = NULL) {
00055 std::ostringstream s;
00056 s << t;
00057
00058 string st = s.str();
00059
00060 #if defined(_MSC_VER)
00061
00062 # pragma warning( push )
00063
00064 # pragma warning( disable: 4267 )
00065 #endif // _MSC_VER
00066 unsigned int size = st.size();
00067 #if defined(_MSC_VER)
00068
00069 # pragma warning( pop )
00070 #endif // _MSC_VER
00071
00072
00073 char *p = new char[size];
00074 memcpy(p, st.data(), size);
00075 c.append(p, size, true);
00076 return true;
00077 }
00078
00079 template<typename T>
00080 bool toString(chain &c, T *const &t, unsigned int nmemb = 1, void *extra = NULL) {
00081 std::ostringstream s;
00082 T *ptr = t;
00083
00084 while (nmemb-- > 0) {
00085 s << *ptr++ << " ";
00086 }
00087 string st = s.str();
00088 int size = st.size();
00089 char *p = new char[size];
00090 memcpy(p, st.data(), size);
00091
00092 c.append(p, size, true);
00093 return true;
00094 }
00095
00109 template<typename T>
00110 bool fromString(const chain &c, unsigned int offset, T &data, unsigned int &used,
00111 unsigned int nmemb = 1, void *extra = NULL) {
00112 try {
00113 std::istringstream is(c.toString(offset));
00114 is >> data;
00115 used = is.tellg();
00116 }
00117 catch (Exception e) {
00118 return false;
00119 }
00120 return true;
00121 }
00122
00123 template<typename T>
00124 bool fromString(const chain &c, unsigned int offset, T * const &data, unsigned int &used,
00125 unsigned int nmemb = 1, void *extra = NULL) {
00126 T *ptr = data;
00127
00128 try {
00129 std::istringstream is(c.toString(offset));
00130 while (nmemb-- > 0) {
00131 is >> *ptr++;
00132 used += is.tellg();
00133 }
00134 }
00135 catch (Exception e) {
00136 return false;
00137 }
00138 return true;
00139 }
00140
00141
00142
00143
00144 template<typename T>
00145 bool rawPack(chain &c, T const &t, unsigned int nmemb = 1, void *extra = NULL) {
00146 c.append(reinterpret_cast<const char *>(&t), sizeof(T));
00147 return true;
00148 }
00149 template<typename T>
00150 bool rawPack(chain &c, T *const &t, unsigned int nmemb = 1, void *extra = NULL) {
00151 unsigned int used = sizeof(T)*nmemb;
00152 c.append(reinterpret_cast<const char *>(t), used);
00153 return true;
00154 }
00155
00156 template<typename T>
00157 bool rawUnpack(const chain &c, unsigned int offset, T &data, unsigned int &used,
00158 unsigned int nmemb = 1, void *extra = NULL) {
00159 used = sizeof(T);
00160 try {
00161 c.substrcopy(reinterpret_cast<char *>(&data), offset, offset+used-1);
00162 }
00163 catch (Exception e) {
00164 return false;
00165 }
00166 return true;
00167 }
00168 template<typename T>
00169 bool rawUnpack(const chain &c, unsigned int offset, T * const &data, unsigned int &used,
00170 unsigned int nmemb = 1, void *extra = NULL) {
00171 used = sizeof(T)*nmemb;
00172 try {
00173 c.substrcopy(reinterpret_cast<char *>(data), offset, offset+used-1);
00174 }
00175 catch (Exception e) {
00176 return false;
00177 }
00178 return true;
00179 }
00180
00181
00182
00183
00184 #ifdef TODO
00185 template<>
00186 bool toString<int>(chain &c, const int & t, unsigned int nmemb = 1,
00187 void *extra = NULL);
00188 template<>
00189 bool toString<int>(chain &c, int *const &t, unsigned int nmemb = 1,
00190 void *extra = NULL);
00191
00192 template<>
00193 bool fromString(const chain &c, unsigned int offset, int &data,
00194 unsigned int &used, unsigned int nmemb = 1, void *extra = NULL);
00195 template<>
00196 bool fromString(const chain &c, unsigned int offset, int * &data,
00197 unsigned int &used, unsigned int nmemb = 1, void *extra = NULL);
00198
00199
00200 template<>
00201 bool toString<float>(chain &c, const T& t);
00202 template<>
00203 bool fromString<float>(const chain &c, unsigned int offset, T &data, unsigned int &used);
00204 #endif
00205
00206 template<>
00207 bool fromString<std::string>(const chain &c, unsigned int offset,
00208 std::string &data, unsigned int &used, unsigned int nmemb,
00209 void *extra);
00210
00211 template<>
00212 bool rawPack<std::string>(chain &c, std::string const &t,
00213 unsigned int nmemb, void *extra);
00214 template<>
00215 bool rawUnpack<std::string>(const chain &c, unsigned int offset,
00216 std::string &data, unsigned int &used, unsigned int nmemb,
00217 void *extra);
00218
00219 template<>
00220 bool rawPack<char *>(chain &c, char* const &t, unsigned int nmemb, void *extra);
00221 template<>
00222 bool rawUnpack<char *>(const chain &c, unsigned int offset,
00223 char * &data, unsigned int &used, unsigned int nmemb,
00224 void *extra);
00225
00226
00227 }
00228
00229 #endif