00001 #ifndef s11n_DATA_NODE_IO_H_INCLUDED
00002 #define s11n_DATA_NODE_IO_H_INCLUDED
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <string>
00013 #include <sstream>
00014 #include <list>
00015 #include <map>
00016 #include <deque>
00017 #include <iostream>
00018 #include <memory>
00019
00020 #include <cassert>
00021 #include <typeinfo>
00022
00023
00024
00025
00026
00027 #include <s11n.net/s11n/phoenix.hpp>
00028
00029 #include <s11n.net/s11n/exception.hpp>
00030 #include <s11n.net/s11n/s11n_debuggering_macros.hpp>
00031 #include <s11n.net/s11n/classload.hpp>
00032 #include <s11n.net/s11n/serialize.hpp>
00033 #include <s11n.net/s11n/traits.hpp>
00034
00035 #include <s11n.net/s11n/export.hpp>
00036
00037
00038
00039
00040
00041
00042 namespace s11n {
00043
00044 namespace io {
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 std::ostream * get_ostream( const std::string name );
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 std::istream * get_istream( const std::string name, bool ExternalData = true );
00073
00074
00075
00076
00077
00078
00079
00080 std::string get_magic_cookie( const std::string & src, bool ExternalData = true );
00081
00082
00083
00084
00085
00086
00087
00088
00089 std::string get_magic_cookie( std::istream & is );
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 template <typename NodeT>
00112 class S11N_EXPORT_API data_node_serializer
00113 {
00114 public:
00115
00116
00117
00118
00119 typedef NodeT node_type;
00120
00121
00122 data_node_serializer()
00123 {
00124 this->magic_cookie( "WARNING: magic_cookie() not set!" );
00125
00126 typedef ::s11n::node_traits<node_type> NTR;
00127 NTR::name( this->metadata(), "serializer_metadata" );
00128
00129 };
00130 virtual ~data_node_serializer(){};
00131
00132
00133
00134
00135
00136 typedef std::map<std::string,std::string> translation_map;
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 virtual const translation_map & entity_translations() const
00152 {
00153 typedef ::s11n::Detail::phoenix<translation_map,data_node_serializer<node_type> > TMap;
00154 return TMap::instance();
00155 }
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 virtual bool serialize( const node_type & , std::ostream & )
00171 {
00172 return false;
00173 }
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197 virtual bool serialize( const node_type & src, const std::string & destfile )
00198 {
00199 if( destfile.empty() ) return false;
00200 std::ostream * os = ::s11n::io::get_ostream( destfile );
00201 if( ! os ) return false;
00202 bool b = this->serialize( src, *os );
00203 delete( os );
00204 return b;
00205 }
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228 virtual node_type * deserialize( std::istream & )
00229 {
00230 return 0;
00231 }
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244 virtual node_type * deserialize( const std::string & src )
00245 {
00246 typedef std::auto_ptr<std::istream> AP;
00247 AP is = AP( ::s11n::io::get_istream( src ) );
00248 if( ! is.get() ) return 0;
00249 return this->deserialize( *is );
00250 }
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260 std::string magic_cookie() const
00261 {
00262 return this->m_cookie;
00263 }
00264
00265 protected:
00266
00267
00268
00269 void magic_cookie( const std::string & c )
00270 {
00271 this->m_cookie = c;
00272 }
00273
00274
00275
00276
00277
00278
00279
00280 node_type & metadata()
00281 { return this->m_meta; }
00282
00283
00284
00285 const node_type & metadata() const
00286 { return this->m_meta;}
00287 private:
00288 std::string m_cookie;
00289 node_type m_meta;
00290 };
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325 template <typename NodeType>
00326 NodeType *
00327 load_node_classload_serializer( std::istream & is )
00328 {
00329 typedef data_node_serializer<NodeType> ST;
00330 std::string cookie;
00331
00332 if( ! std::getline( is, cookie ) )
00333 {
00334 CERR << "Odd: got a null cookie from the istream.\n";
00335 return 0;
00336 }
00337
00338
00339 try
00340 {
00341 typedef std::auto_ptr<ST> AP;
00342 AP ser;
00343 std::string opencmd = "#s11n::io::serializer ";
00344 std::string::size_type at = cookie.find( opencmd );
00345 if( std::string::npos == at )
00346 {
00347 opencmd = "#!/s11n/io/serializer ";
00348 at = cookie.find( opencmd );
00349 }
00350
00351 if( 0 == at )
00352 {
00353 std::string dll = cookie.substr( opencmd.size() );
00354
00355 ser = AP( ::s11n::cl::classload<ST>( dll ) );
00356 }
00357 else
00358 {
00359 ser = AP( ::s11n::cl::classload<ST>( cookie ) );
00360 }
00361
00362 if( ! (ser.get()) )
00363 {
00364 CERR << "Did not find serializer for cookie ["<<cookie<<"]."<<std::endl;
00365 return 0;
00366 }
00367 return ser->deserialize( is );
00368 }
00369 catch( const s11n_exception & sex )
00370 {
00371 throw sex;
00372 }
00373 catch( const std::exception & ex )
00374 {
00375 throw ::s11n::io_exception( ex.what(), __FILE__, __LINE__ );
00376 }
00377 catch( ... )
00378 {
00379 throw ::s11n::io_exception( std::string("Stream-level deserialization failed for unknown reason. Cookie=")+cookie,
00380 __FILE__, __LINE__ );
00381 }
00382 return 0;
00383 }
00384
00385
00386
00387
00388
00389
00390
00391
00392 template <typename NodeType>
00393 NodeType * load_node( std::istream & is )
00394 {
00395 return load_node_classload_serializer< NodeType >( is );
00396 }
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412 template <typename NodeType>
00413 NodeType * load_node( const std::string & src, bool ExternalData = true )
00414 {
00415 typedef std::auto_ptr<std::istream> AP;
00416 AP is = AP( ::s11n::io::get_istream( src, ExternalData ) );
00417 if( ! is.get() ) return 0;
00418 return load_node<NodeType>( *is );
00419 }
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429 template <typename NodeT,typename SerializableT>
00430 SerializableT * load_serializable( std::istream & src )
00431 {
00432 typedef std::auto_ptr<NodeT> AP;
00433 AP node = AP( load_node<NodeT>( src ) );
00434 if( ! node.get() )
00435 {
00436 CERR << "load_serializable<>(istream) Could not load a root node from the input.\n";
00437 return 0;
00438 }
00439 return ::s11n::deserialize<NodeT,SerializableT>( *node );
00440 }
00441
00442
00443
00444
00445
00446
00447
00448
00449 template <typename NodeT,typename SerializableT>
00450 SerializableT * load_serializable( const std::string & src, bool ExternalData = true )
00451 {
00452 typedef std::auto_ptr<std::istream> AP;
00453 AP is = AP( ::s11n::io::get_istream( src, ExternalData ) );
00454 if( ! is.get() )
00455 {
00456 CERR << "load_serializable<>(string) Could not load a root node from the input.\n";
00457 return 0;
00458 }
00459 return load_serializable<NodeT,SerializableT>( *is );
00460 }
00461 }
00462
00463 }
00464
00465 #endif // s11n_DATA_NODE_IO_H_INCLUDED