micro_api.hpp
Go to the documentation of this file.00001 #ifndef s11nlite_MICROAPI_HPP_INCLUDED
00002 #define s11nlite_MICROAPI_HPP_INCLUDED 1
00003
00004 #include <s11n.net/s11n/s11nlite.hpp>
00005 namespace s11nlite
00006 {
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 template <typename SerializableType>
00043 class micro_api
00044 {
00045 public:
00046
00047
00048
00049
00050 typedef SerializableType serializable_type;
00051
00052 private:
00053 typedef s11nlite::node_type node_type;
00054 typedef s11nlite::node_traits node_traits;
00055 typedef s11nlite::serializer_interface serializer_interface;
00056 std::string m_serclass;
00057 std::string m_buffer;
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 serializer_interface *
00068 prepare_save( node_type & dest, const serializable_type & src ) const
00069 {
00070 if( ! ::s11n::serialize<node_type,serializable_type>( dest, src ) )
00071 {
00072 return 0;
00073 }
00074 return ::s11nlite::create_serializer(this->m_serclass);
00075 }
00076 public:
00077
00078
00079
00080 micro_api( const std::string & serclass ) : m_serclass(serclass),m_buffer()
00081 {
00082 }
00083
00084
00085 ~micro_api() {}
00086
00087
00088
00089
00090
00091 micro_api() : m_serclass(::s11nlite::serializer_class()),m_buffer()
00092 {
00093 }
00094
00095
00096 std::string serializer_class() const
00097 {
00098 return this->m_serclass;
00099 }
00100
00101
00102 void serializer_class(const std::string & s)
00103 {
00104 this->m_serclass = s;
00105 }
00106
00107
00108
00109
00110
00111 bool buffer( const serializable_type & src )
00112 {
00113 std::ostringstream os;
00114 if( ! this->save( src, os ) ) return false;
00115 this->m_buffer = os.str();
00116 return true;
00117 }
00118
00119
00120
00121
00122
00123
00124
00125 std::string buffer() const { return this->m_buffer; }
00126
00127
00128
00129
00130
00131 void clear_buffer() { this->m_buffer = std::string(); }
00132
00133
00134
00135
00136
00137
00138 bool save( const serializable_type & src, const std::string & dest ) const
00139 {
00140 node_type n;
00141 std::auto_ptr<serializer_interface> sap( this->prepare_save( n, src ) );
00142 return sap.get() ? sap->serialize( n, dest ) : false;
00143 }
00144
00145
00146 bool save( const serializable_type & src, std::ostream & dest ) const
00147 {
00148 node_type n;
00149 std::auto_ptr<serializer_interface> sap( this->prepare_save( n, src ) );
00150 return sap.get() ? sap->serialize( n, dest ) : false;
00151 }
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163 serializable_type * load( const std::string & src ) const
00164 {
00165 return ::s11nlite::load_serializable<serializable_type>( src );
00166 }
00167
00168
00169
00170
00171 serializable_type * load( std::istream & src ) const
00172 {
00173 return ::s11nlite::load_serializable<serializable_type>( src );
00174 }
00175
00176
00177
00178
00179
00180 serializable_type * load_buffer() const
00181 {
00182 if( this->m_buffer.empty() ) return 0;
00183 std::istringstream is( this->m_buffer );
00184 return ::s11nlite::load_serializable<serializable_type>( is );
00185 }
00186
00187
00188 };
00189
00190
00191 }
00192
00193
00194 #endif // s11nlite_MICROAPI_HPP_INCLUDED