00001 #ifndef s11n_DATA_NODE_FUNCTOR_H_INCLUDED
00002 #define s11n_DATA_NODE_FUNCTOR_H_INCLUDED
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <string>
00012 #include <list>
00013 #include <map>
00014 #include <typeinfo>
00015 #include <utility>
00016 #include <iterator>
00017 #include <iostream>
00018
00019 #include <s11n/phoenix.h>
00020 #include <s11n/to_string.h>
00021 #include <s11n/debuggering_macros.h>
00022 #include <s11n/string_util.h>
00023 #include <s11n/pointer_stripper.h>
00024 #include <s11n/class_name.h>
00025 #include <s11n/abstract_creator.h>
00026 #include <s11n/data_node_serialize.h>
00027 #include <s11n/data_node_algo.h>
00028
00029
00030
00031
00032
00033 namespace s11n {
00034
00035
00036
00037
00038
00039
00040
00041 template <typename NodeType>
00042 void dump_node_debug( const NodeType & n, std::ostream & os )
00043 {
00044 os << "node dump: ["<<n.name()<<"]["<<n.impl_class()<<"]@"<<std::hex<<&n<<"\n";
00045 typedef typename NodeType::const_iterator PT;
00046 PT b = n.begin(), e = n.end();
00047 os << "==================== properties:\n";
00048 for( ; e != b; ++b )
00049 {
00050 os << (*b).first << " = " << (*b).second << "\n";
00051 }
00052 os << "[end node]\n";
00053 }
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 template <typename NodeType>
00069 struct data_node_child_serializer
00070 {
00071 typedef NodeType node_type;
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 data_node_child_serializer( node_type & dest, const std::string & subname )
00084 : result(true), m_name(subname), m_root(&dest)
00085 {
00086 }
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 template <typename SerializableT>
00102 bool operator()( const SerializableT * src )
00103 {
00104 if( ! this->result ) return false;
00105 return this->result =
00106 serialize_subnode( *this->m_root,
00107 this->m_name,
00108 *src );
00109 }
00110
00111
00112
00113
00114
00115
00116
00117 bool result;
00118 private:
00119 std::string m_name;
00120 node_type * m_root;
00121 };
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131 template <typename ListType>
00132 struct data_node_child_deserializer
00133 {
00134
00135
00136
00137
00138
00139 typedef ListType list_type;
00140
00141
00142
00143 typedef typename pointer_stripper<
00144 typename list_type::value_type
00145 >::value_type value_type;
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163 data_node_child_deserializer( list_type & dest,
00164 bool tolerant = false )
00165 : result(true), m_tolerant(tolerant), m_list(&dest)
00166 {
00167 }
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 template <typename NodeType>
00180 bool operator()( const NodeType * src )
00181 {
00182 if( ! result && ! this->m_tolerant ) return false;
00183 if( ! src ) return result = false;
00184 value_type * ch = 0;
00185 ch = s11n::deserialize<NodeType,value_type>( *src );
00186
00187
00188 if( ! ch ) return result = false;
00189 this->m_list->push_back( ch );
00190 return result = true;
00191 }
00192
00193
00194
00195
00196
00197
00198
00199 bool result;
00200 private:
00201 bool m_tolerant;
00202 list_type * m_list;
00203 };
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234 struct streamable_type_serializer_proxy
00235 {
00236
00237
00238
00239
00240 explicit streamable_type_serializer_proxy( const std::string & subnodename )
00241 : m_subname(subnodename)
00242 {}
00243
00244
00245
00246
00247 streamable_type_serializer_proxy() : m_subname("")
00248 {}
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259 template <typename NodeType, typename SerType>
00260 bool operator()( NodeType & dest, const SerType & src ) const
00261 {
00262 dest.impl_class( class_name<SerType>::name() );
00263
00264
00265
00266
00267 if( ! this->m_subname.empty() ) dest.name( this->m_subname );
00268 dest.set( "v", src );
00269 return true;
00270 }
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287 template <typename NodeType, typename SerType>
00288 bool operator()( const NodeType & src, SerType & dest ) const
00289 {
00290 if( ! src.is_set( "v" ) )
00291 {
00292 CERR << "streamable_serializer_proxy: deser failed: property 'v' missing!\n";
00293 dump_node_debug( src, std::cerr );
00294 return false;
00295 }
00296 dest = src.get( "v", dest );
00297 return true;
00298 }
00299 private:
00300 std::string m_subname;
00301 };
00302
00303
00304
00305
00306
00307 namespace io {
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320 template <typename NodeType>
00321 class key_value_serializer
00322 {
00323 public:
00324 typedef NodeType node_type;
00325 typedef typename node_type::value_type pair_type;
00326
00327 typedef std::map<std::string,std::string> entity_translation_map;
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344 key_value_serializer( const entity_translation_map * map,
00345 std::ostream & dest,
00346 const std::string & prefix,
00347 const std::string & separator,
00348 const std::string & suffix
00349 )
00350 : m_pre(prefix), m_sep(separator), m_suf(suffix), m_os(dest), m_map(map)
00351 {
00352 }
00353
00354
00355
00356
00357
00358
00359
00360 void operator()( const pair_type & src ) const
00361 {
00362 static const std::string errval = "";
00363 std::string key = s11n::to_string( src.first );
00364 std::string val = s11n::to_string( src.second );
00365
00366 if( this->m_map )
00367 {
00368 s11n::translate_entities( val, *(this->m_map) );
00369 }
00370 this->m_os << this->m_pre;
00371 this->m_os << key;
00372 this->m_os << this->m_sep;
00373 this->m_os << val;
00374 this->m_os << this->m_suf;
00375 }
00376 private:
00377 std::string m_pre;
00378 std::string m_sep;
00379 std::string m_suf;
00380 std::ostream & m_os;
00381 const entity_translation_map * m_map;
00382 };
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398 template <typename SerializerT>
00399 struct node_child_simple_formatter
00400 {
00401 typedef SerializerT serializer_type;
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414 node_child_simple_formatter( serializer_type & ser, std::ostream & os,
00415 const std::string & prefix = "", const std::string & suffix = "\n" )
00416 : m_ser(ser), m_os(&os), m_pre(prefix), m_suf(suffix)
00417 {
00418 }
00419
00420
00421
00422
00423
00424 template <typename NodeType>
00425 bool operator()( const NodeType * src ) const
00426 {
00427 if( ! src ) return false;
00428 if( ! this->m_pre.empty() ) *(this->m_os) << this->m_pre;
00429 bool b = this->m_ser.serialize( *src, *(this->m_os) );
00430 if( ! this->m_suf.empty() ) *(this->m_os) << this->m_suf;
00431 return b;
00432 }
00433
00434 private:
00435 serializer_type & m_ser;
00436 std::ostream * m_os;
00437 std::string m_pre;
00438 std::string m_suf;
00439 };
00440
00441
00442 }
00443
00444 }
00445
00446
00447 #endif // s11n_DATA_NODE_FUNCTOR_H_INCLUDED