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 std::ostream * get_ostream( const std::string name );
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 std::istream * get_istream( const std::string name, bool ExternalData = true );
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 std::string get_magic_cookie( const std::string & src, bool ExternalData = true );
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106 std::string get_magic_cookie( std::istream & is );
00107
00108 #if s11n_SERIALIZER_ENABLE_INTERACTIVE
00109 struct progress_reporter
00110 {
00111 progress_reporter() {}
00112 virtual ~progress_reporter() {}
00113 virtual void operator()( size_t pos, size_t total ) = 0;
00114 };
00115 #endif // s11n_SERIALIZER_ENABLE_INTERACTIVE
00116
00117
00118
00119
00120
00121
00122
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 template <typename NodeT>
00155 class S11N_EXPORT_API data_node_serializer
00156 {
00157 public:
00158
00159
00160
00161
00162 typedef NodeT node_type;
00163
00164
00165 data_node_serializer()
00166 {
00167 this->magic_cookie( "WARNING: magic_cookie() not set!" );
00168
00169 typedef ::s11n::node_traits<node_type> NTR;
00170 NTR::name( this->metadata(), "serializer_metadata" );
00171
00172 using namespace s11n::debug;
00173 S11N_TRACE(TRACE_CTOR) << "data_node_serialier()\n";
00174
00175 };
00176 virtual ~data_node_serializer()
00177 {
00178 using namespace s11n::debug;
00179 S11N_TRACE(TRACE_DTOR) << "~data_node_serialier() ["<<this->magic_cookie()<<"]\n";
00180 }
00181
00182
00183
00184
00185
00186 typedef std::map<std::string,std::string> translation_map;
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201 virtual const translation_map & entity_translations() const
00202 {
00203 typedef ::s11n::Detail::phoenix<translation_map,data_node_serializer<node_type> > TMap;
00204 return TMap::instance();
00205 }
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220 virtual bool serialize( const node_type & , std::ostream & )
00221 {
00222 return false;
00223 }
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 virtual bool serialize( const node_type & src, const std::string & destfile )
00248 {
00249 if( destfile.empty() ) return false;
00250 std::ostream * os = ::s11n::io::get_ostream( destfile );
00251 if( ! os ) return false;
00252 bool b = this->serialize( src, *os );
00253 delete( os );
00254 return b;
00255 }
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278 virtual node_type * deserialize( std::istream & )
00279 {
00280 return 0;
00281 }
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294 virtual node_type * deserialize( const std::string & src )
00295 {
00296 typedef std::auto_ptr<std::istream> AP;
00297 AP is = AP( ::s11n::io::get_istream( src ) );
00298 if( ! is.get() ) return 0;
00299 return this->deserialize( *is );
00300 }
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310 std::string magic_cookie() const
00311 {
00312 return this->m_cookie;
00313 }
00314
00315
00316 #if s11n_SERIALIZER_ENABLE_INTERACTIVE
00317 bool is_cancelled() const { return m_cancelled; }
00318 void cancel() { this->m_cancelled = true; }
00319
00320 node_type * deserialize( std::string const & src, progress_reporter & p )
00321 {
00322 this->m_prog = &p;
00323 node_type * n = 0;
00324 try
00325 {
00326 n = this->deserialize( src );
00327 this->m_prog = 0;
00328 }
00329 catch(...)
00330 {
00331 this->m_prog = 0;
00332 throw;
00333 }
00334 return n;
00335 }
00336
00337 node_type * deserialize( std::istream & src, progress_reporter & p )
00338 {
00339 this->m_prog = &p;
00340 node_type * n = 0;
00341 try
00342 {
00343 n = this->deserialize( src );
00344 this->m_prog = 0;
00345 }
00346 catch(...)
00347 {
00348 this->m_prog = 0;
00349 throw;
00350 }
00351 return n;
00352 }
00353
00354 bool serialize( const node_type & src, std::ostream & dest, progress_reporter & p )
00355 {
00356 this->m_prog = &p;
00357 bool b = false;
00358 try
00359 {
00360 b = this->serialize( src, dest );
00361 this->m_prog = 0;
00362 }
00363 catch(...)
00364 {
00365 this->m_prog = 0;
00366 throw;
00367 }
00368 return b;
00369 }
00370
00371 bool serialize( const node_type & src, std::string const & dest, progress_reporter & p )
00372 {
00373 this->m_prog = &p;
00374 bool b = false;
00375 try
00376 {
00377 b = this->serialize( src, dest );
00378 this->m_prog = 0;
00379 }
00380 catch(...)
00381 {
00382 this->m_prog = 0;
00383 throw;
00384 }
00385 return b;
00386 }
00387 #endif // s11n_SERIALIZER_ENABLE_INTERACTIVE
00388
00389 protected:
00390
00391
00392
00393 void magic_cookie( const std::string & c )
00394 {
00395 this->m_cookie = c;
00396 }
00397
00398
00399
00400
00401
00402
00403
00404 node_type & metadata()
00405 { return this->m_meta; }
00406
00407
00408
00409 const node_type & metadata() const
00410 { return this->m_meta;}
00411
00412 #if s11n_SERIALIZER_ENABLE_INTERACTIVE
00413 void progress( size_t pos, size_t total )
00414 {
00415 if( this->m_prog )
00416 {
00417 this->m_prog->operator()( pos, total );
00418 }
00419 }
00420 void clear_cancel() { this->m_cancelled = false; }
00421 void assert_not_cancelled()
00422 {
00423 if( this->is_cancelled() )
00424 {
00425 throw ::s11n::s11n_exception("Serializer operation was cancelled.");
00426 }
00427 }
00428 #endif // s11n_SERIALIZER_ENABLE_INTERACTIVE
00429
00430 private:
00431 std::string m_cookie;
00432 node_type m_meta;
00433 #if s11n_SERIALIZER_ENABLE_INTERACTIVE
00434 bool m_cancelled;
00435 progress_reporter * m_prog;
00436 #endif
00437 };
00438
00439
00440
00441
00442
00443
00444
00445
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 template <typename NodeType>
00471 data_node_serializer<NodeType> * guess_serializer( std::istream & is )
00472 {
00473 typedef data_node_serializer<NodeType> ST;
00474 ST * ser = 0;
00475 std::string cookie;
00476
00477 cookie = get_magic_cookie( is );
00478 if( cookie.empty() ) return 0;
00479 std::string opencmd = "#s11n::io::serializer ";
00480 std::string::size_type at = cookie.find( opencmd );
00481 if( std::string::npos == at )
00482 {
00483 opencmd = "#!/s11n/io/serializer ";
00484 at = cookie.find( opencmd );
00485 }
00486
00487 if( 0 == at )
00488 {
00489 std::string dll = cookie.substr( opencmd.size() );
00490 ser = ::s11n::cl::classload<ST>( dll );
00491 }
00492 else
00493 {
00494 ser = ::s11n::cl::classload<ST>( cookie );
00495 }
00496 return ser;
00497 }
00498
00499
00500
00501
00502
00503
00504 template <typename NodeType>
00505 data_node_serializer<NodeType> * guess_serializer( std::string const & infile )
00506 {
00507 std::auto_ptr<std::istream> is( get_istream( infile.c_str() ) );
00508 return is.get()
00509 ? guess_serializer<NodeType>( *is )
00510 : 0;
00511 }
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531 template <typename NodeType>
00532 NodeType *
00533 load_node_classload_serializer( std::istream & is )
00534 {
00535 try
00536 {
00537 typedef data_node_serializer<NodeType> ST;
00538 std::auto_ptr<ST> ser( guess_serializer<NodeType>( is ) );
00539 return ser.get()
00540 ? ser->deserialize( is )
00541 : 0;
00542 }
00543 catch( const s11n_exception & sex )
00544 {
00545 throw sex;
00546 }
00547 catch( const std::exception & ex )
00548 {
00549 throw ::s11n::io_exception( ex.what(), __FILE__, __LINE__ );
00550 }
00551 catch( ... )
00552 {
00553 throw ::s11n::io_exception( std::string("Stream-level deserialization failed for unknown reason."),
00554 __FILE__, __LINE__ );
00555 }
00556 return 0;
00557 }
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568 template <typename NodeType>
00569 NodeType *
00570 load_node_classload_serializer( std::string const & src )
00571 {
00572 try
00573 {
00574 typedef data_node_serializer<NodeType> ST;
00575 std::auto_ptr<ST> ser( guess_serializer<NodeType>( src ) );
00576 return ser.get()
00577 ? ser->deserialize( src )
00578 : 0;
00579 }
00580 catch( const s11n_exception & sex )
00581 {
00582 throw sex;
00583 }
00584 catch( const std::exception & ex )
00585 {
00586 throw ::s11n::io_exception( ex.what(), __FILE__, __LINE__ );
00587 }
00588 catch( ... )
00589 {
00590 throw ::s11n::io_exception( std::string("Stream-level deserialization failed for unknown reason."),
00591 __FILE__, __LINE__ );
00592 }
00593 return 0;
00594 }
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604 template <typename NodeType>
00605 NodeType * load_node( std::istream & is )
00606 {
00607 return load_node_classload_serializer< NodeType >( is );
00608 }
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630 template <typename NodeType>
00631 NodeType * load_node( const std::string & src, bool ExternalData = true )
00632 {
00633 if( ! ExternalData )
00634 {
00635 typedef std::auto_ptr<std::istream> AP;
00636 AP is( ::s11n::io::get_istream( src, ExternalData ) );
00637 if( ! is.get() ) return 0;
00638 return load_node<NodeType>( *is );
00639 }
00640 return load_node_classload_serializer<NodeType>( src );
00641 }
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651 template <typename NodeT,typename SerializableT>
00652 SerializableT * load_serializable( std::istream & src )
00653 {
00654 typedef std::auto_ptr<NodeT> AP;
00655 AP node( load_node<NodeT>( src ) );
00656 if( ! node.get() )
00657 {
00658 CERR << "load_serializable<>(istream) Could not load a root node from the input.\n";
00659 return 0;
00660 }
00661 return ::s11n::deserialize<NodeT,SerializableT>( *node );
00662 }
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677 template <typename NodeT,typename SerializableT>
00678 SerializableT * load_serializable( const std::string & src, bool ExternalData = true )
00679 {
00680 if( ! ExternalData )
00681 {
00682 typedef std::auto_ptr<std::istream> AP;
00683 AP is( ::s11n::io::get_istream( src, ExternalData ) );
00684 if( ! is.get() )
00685 {
00686
00687 return 0;
00688 }
00689 return load_serializable<NodeT,SerializableT>( *is );
00690 }
00691 typedef std::auto_ptr<NodeT> AP;
00692 AP node( load_node<NodeT>( src ) );
00693 if( ! node.get() )
00694 {
00695
00696 return 0;
00697 }
00698 return ::s11n::deserialize<NodeT,SerializableT>( *node );
00699 }
00700
00701 }
00702
00703 }
00704
00705 #endif // s11n_DATA_NODE_IO_H_INCLUDED