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 #define s11n_SERIALIZER_ENABLE_INTERACTIVE 0
00043
00044 namespace s11n {
00045
00046 namespace io {
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 std::ostream * get_ostream( const std::string name );
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 std::istream * get_istream( const std::string name );
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 std::string get_magic_cookie( const std::string & src );
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 std::string get_magic_cookie( std::istream & is );
00114
00115 #if s11n_SERIALIZER_ENABLE_INTERACTIVE
00116 struct progress_reporter
00117 {
00118 progress_reporter() {}
00119 virtual ~progress_reporter() {}
00120 virtual void operator()( size_t pos, size_t total ) = 0;
00121 };
00122 #endif // s11n_SERIALIZER_ENABLE_INTERACTIVE
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161 template <typename NodeT>
00162 class S11N_EXPORT_API data_node_serializer
00163 {
00164 public:
00165
00166
00167
00168
00169 typedef NodeT node_type;
00170
00171
00172 data_node_serializer()
00173 {
00174 this->magic_cookie( "WARNING: magic_cookie() not set!" );
00175
00176 typedef ::s11n::node_traits<node_type> NTR;
00177 NTR::name( this->metadata(), "serializer_metadata" );
00178
00179 using namespace s11n::debug;
00180 S11N_TRACE(TRACE_CTOR) << "data_node_serialier()\n";
00181
00182 };
00183 virtual ~data_node_serializer()
00184 {
00185 using namespace s11n::debug;
00186 S11N_TRACE(TRACE_DTOR) << "~data_node_serialier() ["<<this->magic_cookie()<<"]\n";
00187 }
00188
00189
00190
00191
00192
00193 typedef std::map<std::string,std::string> translation_map;
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208 virtual const translation_map & entity_translations() const
00209 {
00210 typedef ::s11n::Detail::phoenix<translation_map,data_node_serializer<node_type> > TMap;
00211 return TMap::instance();
00212 }
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227 virtual bool serialize( const node_type & , std::ostream & )
00228 {
00229 return false;
00230 }
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257 virtual bool serialize( const node_type & src, const std::string & destfile )
00258 {
00259 if( destfile.empty() ) return false;
00260 std::auto_ptr<std::ostream> os( ::s11n::io::get_ostream( destfile ) );
00261 if( ! os.get() ) return false;
00262 bool b = this->serialize( src, *os );
00263 return b;
00264 }
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287 virtual node_type * deserialize( std::istream & )
00288 {
00289 return 0;
00290 }
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303 virtual node_type * deserialize( const std::string & src )
00304 {
00305 typedef std::auto_ptr<std::istream> AP;
00306 AP is = AP( ::s11n::io::get_istream( src ) );
00307 if( ! is.get() ) return 0;
00308 return this->deserialize( *is );
00309 }
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319 std::string magic_cookie() const
00320 {
00321 return this->m_cookie;
00322 }
00323
00324
00325 #if s11n_SERIALIZER_ENABLE_INTERACTIVE
00326 bool is_cancelled() const { return m_cancelled; }
00327 void cancel() { this->m_cancelled = true; }
00328
00329 node_type * deserialize( std::string const & src, progress_reporter & p )
00330 {
00331 this->m_prog = &p;
00332 node_type * n = 0;
00333 try
00334 {
00335 n = this->deserialize( src );
00336 this->m_prog = 0;
00337 }
00338 catch(...)
00339 {
00340 this->m_prog = 0;
00341 throw;
00342 }
00343 return n;
00344 }
00345
00346 node_type * deserialize( std::istream & src, progress_reporter & p )
00347 {
00348 this->m_prog = &p;
00349 node_type * n = 0;
00350 try
00351 {
00352 n = this->deserialize( src );
00353 this->m_prog = 0;
00354 }
00355 catch(...)
00356 {
00357 this->m_prog = 0;
00358 throw;
00359 }
00360 return n;
00361 }
00362
00363 bool serialize( const node_type & src, std::ostream & dest, progress_reporter & p )
00364 {
00365 this->m_prog = &p;
00366 bool b = false;
00367 try
00368 {
00369 b = this->serialize( src, dest );
00370 this->m_prog = 0;
00371 }
00372 catch(...)
00373 {
00374 this->m_prog = 0;
00375 throw;
00376 }
00377 return b;
00378 }
00379
00380 bool serialize( const node_type & src, std::string const & dest, progress_reporter & p )
00381 {
00382 this->m_prog = &p;
00383 bool b = false;
00384 try
00385 {
00386 b = this->serialize( src, dest );
00387 this->m_prog = 0;
00388 }
00389 catch(...)
00390 {
00391 this->m_prog = 0;
00392 throw;
00393 }
00394 return b;
00395 }
00396 #endif // s11n_SERIALIZER_ENABLE_INTERACTIVE
00397
00398 protected:
00399
00400
00401
00402 void magic_cookie( const std::string & c )
00403 {
00404 this->m_cookie = c;
00405 }
00406
00407
00408
00409
00410
00411
00412
00413 node_type & metadata()
00414 { return this->m_meta; }
00415
00416
00417
00418 const node_type & metadata() const
00419 { return this->m_meta;}
00420
00421 #if s11n_SERIALIZER_ENABLE_INTERACTIVE
00422 void progress( size_t pos, size_t total )
00423 {
00424 if( this->m_prog )
00425 {
00426 this->m_prog->operator()( pos, total );
00427 }
00428 }
00429 void clear_cancel() { this->m_cancelled = false; }
00430 void assert_not_cancelled()
00431 {
00432 if( this->is_cancelled() )
00433 {
00434 throw ::s11n::s11n_exception("Serializer operation was cancelled.");
00435 }
00436 }
00437 #endif // s11n_SERIALIZER_ENABLE_INTERACTIVE
00438
00439 private:
00440 std::string m_cookie;
00441 node_type m_meta;
00442 #if s11n_SERIALIZER_ENABLE_INTERACTIVE
00443 bool m_cancelled;
00444 progress_reporter * m_prog;
00445 #endif
00446 };
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479 template <typename NodeType>
00480 data_node_serializer<NodeType> *
00481 guess_serializer( std::istream & is )
00482 {
00483 typedef data_node_serializer<NodeType> ST;
00484 ST * ser = 0;
00485 std::string cookie;
00486
00487 cookie = get_magic_cookie( is );
00488 if( cookie.empty() ) return 0;
00489 std::string opencmd = "#s11n::io::serializer ";
00490 std::string::size_type at = cookie.find( opencmd );
00491 if( std::string::npos == at )
00492 {
00493 opencmd = "#!/s11n/io/serializer ";
00494 at = cookie.find( opencmd );
00495 }
00496
00497 if( 0 == at )
00498 {
00499 std::string dll = cookie.substr( opencmd.size() );
00500 ser = ::s11n::cl::classload<ST>( dll );
00501 }
00502 else
00503 {
00504 ser = ::s11n::cl::classload<ST>( cookie );
00505 }
00506 return ser;
00507 }
00508
00509
00510
00511
00512
00513
00514 template <typename NodeType>
00515 data_node_serializer<NodeType> *
00516 guess_serializer( std::string const & infile )
00517 {
00518 std::auto_ptr<std::istream> is( get_istream( infile.c_str() ) );
00519 return is.get()
00520 ? guess_serializer<NodeType>( *is )
00521 : 0;
00522 }
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542 template <typename NodeType>
00543 NodeType * load_node( std::istream & is )
00544 {
00545 typedef data_node_serializer<NodeType> ST;
00546 std::auto_ptr<ST> ser( guess_serializer<NodeType>( is ) );
00547 return ser.get()
00548 ? ser->deserialize( is )
00549 : 0;
00550 }
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568 template <typename NodeType>
00569 NodeType * load_node( const std::string & src )
00570 {
00571 typedef data_node_serializer<NodeType> ST;
00572 std::auto_ptr<ST> ser( guess_serializer<NodeType>( src ) );
00573 return ser.get()
00574 ? ser->deserialize( src )
00575 : 0;
00576 }
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586 template <typename NodeT,typename SerializableT>
00587 SerializableT * load_serializable( std::istream & src )
00588 {
00589 typedef std::auto_ptr<NodeT> AP;
00590 AP node( load_node<NodeT>( src ) );
00591 if( ! node.get() )
00592 {
00593 CERR << "load_serializable<>(istream) Could not load a root node from the input.\n";
00594 return 0;
00595 }
00596 return ::s11n::deserialize<NodeT,SerializableT>( *node );
00597 }
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609 template <typename NodeT,typename SerializableT>
00610 SerializableT * load_serializable( const std::string & src )
00611 {
00612 typedef std::auto_ptr<NodeT> AP;
00613 AP node( load_node<NodeT>( src ) );
00614 if( ! node.get() )
00615 {
00616 return 0;
00617 }
00618 return ::s11n::deserialize<NodeT,SerializableT>( *node );
00619 }
00620
00621 }
00622
00623 }
00624
00625 #endif // s11n_DATA_NODE_IO_H_INCLUDED