00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef __SERIALIZATION_H__
00020 #define __SERIALIZATION_H__
00021
00022 #include <dtoolbase.h>
00023
00024 #include "config_setup.h"
00025
00026 namespace Serialize {
00027
00028 template <class X>
00029 class StdIns {
00030 public:
00031 INLINE ConfigString operator()(const X& val) {
00032 ostringstream oss;
00033 oss << val;
00034 return oss.str();
00035 }
00036 };
00037
00038 template <class X>
00039 class StdExt {
00040 public:
00041 INLINE X operator()(ConfigString S) {
00042 istringstream iss(S);
00043 X ret;
00044 iss >> ret;
00045 return ret;
00046 }
00047 };
00048
00049 template <class X>
00050 INLINE int Length(X c) {
00051 return c.length();
00052 }
00053
00054 template <class Collection, class Inserter = StdIns<TYPENAME Collection::value_type> >
00055 class Serializer {
00056 private:
00057 ConfigString _result;
00058
00059 ConfigString SerializeToString(const Collection&, const ConfigString&);
00060 Serializer() {}
00061 public:
00062 Serializer(const Collection& C, ConfigString Delim = ":") :
00063 _result(Serializer::SerializeToString(C, Delim)) {}
00064 Serializer(const Serializer<Collection, Inserter>& c) :
00065 _result(c._result) {}
00066 ~Serializer() {}
00067 INLINE ConfigString operator()() { return _result; }
00068 INLINE ConfigString operator()(const Collection& C,
00069 const ConfigString& Delim = ":") {
00070 _result = SerializeToString(C, Delim);
00071 return _result;
00072 }
00073 INLINE operator ConfigString() { return _result; }
00074 };
00075
00076 template <class Collection, class Inserter>
00077 ConfigString
00078 Serializer<Collection, Inserter>::SerializeToString(const Collection& C,
00079 const ConfigString& Delim)
00080 {
00081 ConfigString ret;
00082 Inserter in;
00083
00084 for (TYPENAME Collection::const_iterator i=C.begin(); i!=C.end(); ++i) {
00085 if (i != C.begin())
00086 ret += Delim;
00087 ret += in(*i);
00088 }
00089 return ret;
00090 }
00091
00092 template <class Collection, class Extractor = StdExt<TYPENAME Collection::value_type> >
00093 class Deserializer {
00094 private:
00095 Collection _result;
00096
00097 INLINE void Clear() { _result.erase(_result.begin(), _result.end()); }
00098 template <class ForwardIterator>
00099 int FindFirstOfInString(ConfigString S, ForwardIterator DelimBegin,
00100 ForwardIterator DelimEnd) {
00101 int i = ConfigString::npos;
00102 ForwardIterator j = DelimBegin;
00103
00104 while (j != DelimEnd) {
00105 int k = S.find(*j);
00106 if (k != ConfigString::npos)
00107 if ((i == ConfigString::npos) || (i > k))
00108 i = k;
00109 ++j;
00110 }
00111 return i;
00112 }
00113 template <class ForwardIterator>
00114 int FindFirstNotOfInString(ConfigString S, ForwardIterator DelimBegin,
00115 ForwardIterator DelimEnd) {
00116 int i = ConfigString::npos;
00117 ForwardIterator j = DelimBegin;
00118 ForwardIterator k = DelimBegin;
00119
00120 while (j != DelimEnd) {
00121 int l = S.find(*j);
00122 if (l != ConfigString::npos)
00123 if ((i == ConfigString::npos) || (i > l)) {
00124 i = l;
00125 k = j;
00126 }
00127 ++j;
00128 }
00129 if (i != ConfigString::npos) {
00130 i += Serialize::Length(*k);
00131 if (i >= S.length())
00132 i = ConfigString::npos;
00133 }
00134 return i;
00135 }
00136 template <class ForwardIterator>
00137 void DeserializeFromString(ConfigString S, ForwardIterator DelimBegin,
00138 ForwardIterator DelimEnd) {
00139 Clear();
00140 Extractor ex;
00141
00142 while (!S.empty()) {
00143 int i = FindFirstOfInString(S, DelimBegin, DelimEnd);
00144 _result.push_back(ex(S.substr(0, i)));
00145 S.erase(0, i);
00146 i = FindFirstNotOfInString(S, DelimBegin, DelimEnd);
00147 S.erase(0, i);
00148 }
00149 }
00150 void DeserializeFromString(ConfigString S, ConfigString Delim) {
00151 Clear();
00152 Extractor ex;
00153
00154 while (!S.empty()) {
00155 size_t i = S.find_first_of(Delim);
00156 _result.push_back(ex(S.substr(0, i)));
00157 if (i == ConfigString::npos)
00158 S.erase(0, i);
00159 else
00160 S.erase(0, i+1);
00161 }
00162 }
00163 Deserializer() {}
00164 public:
00165 Deserializer(ConfigString S, ConfigString Delim = ":") {
00166 Deserializer::DeserializeFromString(S, Delim);
00167 }
00168 template <class ForwardIterator>
00169 Deserializer(ConfigString S, ForwardIterator DelimBegin,
00170 ForwardIterator DelimEnd) {
00171 Deserializer::DeserializeFromString(S, DelimBegin, DelimEnd);
00172 }
00173 ~Deserializer() {}
00174 INLINE const Collection& operator()() { return _result; }
00175 template <class ForwardIterator>
00176 INLINE const Collection& operator()(ConfigString S,
00177 ForwardIterator DelimBegin,
00178 ForwardIterator DelimEnd) {
00179 Deserializer::DeserializeFromString(S, DelimBegin, DelimEnd);
00180 }
00181 INLINE operator const Collection&() { return _result; }
00182 };
00183
00184 #include "serialization.I"
00185
00186 }
00187
00188 #endif