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/debuggering_macros.h>
00028
#include <s11n/file_util.h>
00029
#include <s11n/s11n_core.h>
00030
00031
#include "data_node_serialize.h"
00032
00033
00034
00035
00036
00037
00038
namespace s11n {
00039
00040
namespace io {
00041
00042
00043
00044
00045
00046
00047
00048
00049 std::string
get_magic_cookie(
const std::string & src,
bool AsFile =
true );
00050
00051
00052
00053
00054
00055
00056
00057
00058 std::string
get_magic_cookie( std::istream & is );
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
template <
typename NodeT>
00082 class data_node_serializer
00083 {
00084
public:
00085
00086
00087
00088
00089 typedef NodeT node_type;
00090
00091
data_node_serializer()
00092 {
00093 this->
magic_cookie(
"WARNING: magic_cookie() not set!" );
00094
00095 this->
metadata().name(
"serializer_metadata" );
00096 };
00097
virtual ~
data_node_serializer(){};
00098
00099
00100
00101
00102
00103 typedef std::map<std::string,std::string>
translation_map;
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119 virtual const translation_map &
entity_translations()
const
00120
{
00121
typedef phoenix<translation_map,data_node_serializer<node_type> > TMap;
00122
return TMap::instance();
00123 }
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 virtual bool serialize(
const node_type & src, std::ostream & dest )
00135 {
00136
return false;
00137 }
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148 bool serialize(
const node_type & src,
const std::string & destfile )
00149 {
00150 std::ostream * os =
s11n::get_ostream( destfile );
00151
if( ! os )
return false;
00152
bool b = this->
serialize( src, *os );
00153
delete( os );
00154
return b;
00155 }
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 virtual node_type *
deserialize( std::istream & )
00166 {
00167
return false;
00168 }
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182 virtual node_type *
deserialize(
const std::string & src )
00183 {
00184
typedef std::auto_ptr<std::istream> AP;
00185 AP is = AP( s11n::get_istream( src ) );
00186
if( ! is.get() )
return 0;
00187
return this->
deserialize( *is );
00188 }
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198 std::string
magic_cookie()
const
00199
{
00200
return this->m_cookie;
00201 }
00202
00203
protected:
00204
00205
00206
00207 void magic_cookie(
const std::string & c )
00208 {
00209 this->m_cookie = c;
00210 }
00211
00212
00213
00214
00215
00216
00217 node_type &
metadata()
00218 {
return this->m_meta; }
00219
00220
00221
00222 const node_type &
metadata()
const
00223
{
return this->m_meta;}
00224
private:
00225 std::string m_cookie;
00226 node_type m_meta;
00227 };
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
template <
typename NodeType,
typename SerializerBaseType>
00249 NodeType *
load_node_classload_serializer( std::istream & is )
00250 {
00251
typedef SerializerBaseType NS;
00252 std::string cookie =
get_magic_cookie( is );
00253
00254
if( cookie.empty() )
00255 {
00256 CERR <<
"Odd: got a null cookie from the istream.\n";
00257
return 0;
00258 }
00259
typedef std::auto_ptr<NS> AP;
00260 AP ser = AP( s11n::classload<NS>( cookie ) );
00261
if( ! (ser.get()) )
00262 {
00263 CERR <<
"Did not find serializer for cookie ["<<cookie<<
"]."<<std::endl;
00264
return NULL;
00265 }
00266
00267
return ser->deserialize( is );
00268 }
00269
00270
00271
00272
00273
00274
00275
00276
00277
template <
typename NodeType>
00278 NodeType *
load_node( std::istream & is )
00279 {
00280
return load_node_classload_serializer<NodeType,data_node_serializer<NodeType> >( is );
00281 }
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
template <
typename NodeType>
00298 NodeType *
load_node(
const std::string & src,
bool AsFile =
true )
00299 {
00300
typedef std::auto_ptr<std::istream> AP;
00301 AP is = AP( s11n::get_istream( src, AsFile ) );
00302
if( ! is.get() )
return 0;
00303
return load_node<NodeType>( *is );
00304 }
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
template <
typename NodeT,
typename SerializableT>
00315 SerializableT *
load_serializable( std::istream & src )
00316 {
00317
typedef std::auto_ptr<NodeT> AP;
00318 AP node = AP( load_node<NodeT>( src ) );
00319
if( ! node.get() )
00320 {
00321 CERR <<
"load_serializable<>(istream) Could not load a root node from the input.\n";
00322
return 0;
00323 }
00324
return s11n::deserialize<NodeT,SerializableT>( *node );
00325 }
00326
00327
00328
00329
00330
00331
00332
00333
00334
template <
typename NodeT,
typename SerializableT>
00335 SerializableT *
load_serializable(
const std::string & src,
bool AsFile =
true )
00336 {
00337
typedef std::auto_ptr<std::istream> AP;
00338 AP is = AP(
get_istream( src, AsFile ) );
00339
if( ! is.get() )
00340 {
00341 CERR <<
"load_serializable<>(string) Could not load a root node from the input.\n";
00342
return 0;
00343 }
00344
return load_serializable<NodeT,SerializableT>( *is );
00345 }
00346
00347
00348
00349
00350
00351
00352
00353
00354
template <
typename NodeType,
typename SerializerT>
00355 bool save_node(
const NodeType & src, std::ostream & dest )
00356 {
00357
return SerializerT().serialize( src, dest );
00358 }
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
template <
typename SerializerT,
typename SerializableT>
00377 bool save_serializable(
const SerializableT & src,
00378 std::ostream & dest )
00379 {
00380
typedef typename SerializerT::node_type node_type;
00381 node_type node(
"serializable" );
00382
if( ! s11n::serialize<node_type,SerializableT>( node, src ) )
return false;
00383
return SerializerT().serialize( node, dest );
00384 }
00385
00386
00387
00388
00389
00390
00391
template <
typename SerializerT,
typename SerializableT>
00392 bool save_serializable(
const SerializableT & src,
00393
const std::string & filename )
00394 {
00395
typedef std::auto_ptr<std::ostream> AP;
00396 AP os = AP( s11n::get_ostream( filename ) );
00397
if( ! os.get() )
return false;
00398
return save_serializable( src, *os );
00399 }
00400
00401
00402 }
00403
00404 }
00405
00406
#endif // s11n_DATA_NODE_IO_H_INCLUDED