00001 #ifndef s11nlite_MICROAPI_HPP_INCLUDED
00002 #define s11nlite_MICROAPI_HPP_INCLUDED 1
00003
00004 #include <s11n.net/s11n/s11nlite.hpp>
00005 #include <s11n.net/s11n/mutex.hpp>
00006
00007 namespace s11nlite
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
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 template <typename SerializableType>
00055 class micro_api
00056 {
00057 public:
00058
00059
00060
00061
00062 typedef SerializableType serializable_type;
00063
00064 private:
00065 typedef s11nlite::node_type node_type;
00066 typedef s11nlite::node_traits node_traits;
00067 typedef s11nlite::serializer_interface serializer_interface;
00068 std::string m_serclass;
00069 std::string m_buffer;
00070 mutable s11n::mutex m_mutex;
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 serializer_interface *
00081 prepare_save( node_type & dest, const serializable_type & src ) const
00082 {
00083 if( ! ::s11n::serialize<node_type,serializable_type>( dest, src ) )
00084 {
00085 return 0;
00086 }
00087 return ::s11nlite::create_serializer(this->m_serclass);
00088 }
00089 public:
00090
00091
00092
00093 explicit micro_api( const std::string & serclass )
00094 : m_serclass(serclass),
00095 m_buffer(),
00096 m_mutex()
00097 {
00098 }
00099
00100
00101 ~micro_api() {}
00102
00103
00104
00105
00106
00107 micro_api()
00108 : m_serclass(::s11nlite::serializer_class()),
00109 m_buffer(),
00110 m_mutex()
00111 {
00112 }
00113
00114
00115 std::string serializer_class() const
00116 {
00117 return this->m_serclass;
00118 }
00119
00120
00121
00122
00123 void serializer_class(const std::string & s)
00124 {
00125 s11n::mutex_sentry lock(this->m_mutex);
00126
00127 this->m_serclass = s;
00128 }
00129
00130
00131
00132
00133
00134 bool buffer( const serializable_type & src )
00135 {
00136 std::ostringstream os;
00137 if( ! this->save( src, os ) ) return false;
00138
00139 s11n::mutex_sentry lock(this->m_mutex);
00140 this->m_buffer = os.str();
00141 return true;
00142 }
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154 std::string buffer() const
00155 {
00156 s11n::mutex_sentry lock(this->m_mutex);
00157
00158 return this->m_buffer;
00159 }
00160
00161
00162
00163
00164
00165 void clear_buffer()
00166 {
00167 s11n::mutex_sentry lock(this->m_mutex);
00168
00169 this->m_buffer = std::string();
00170 }
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187 bool save( const serializable_type & src, const std::string & dest ) const
00188 {
00189 s11n::mutex_sentry lock(this->m_mutex);
00190 node_type n;
00191 std::auto_ptr<serializer_interface> sap( this->prepare_save( n, src ) );
00192 return sap.get() ? sap->serialize( n, dest ) : false;
00193 }
00194
00195
00196 bool save( const serializable_type & src, std::ostream & dest ) const
00197 {
00198 s11n::mutex_sentry lock(this->m_mutex);
00199 node_type n;
00200 std::auto_ptr<serializer_interface> sap( this->prepare_save( n, src ) );
00201 return sap.get() ? sap->serialize( n, dest ) : false;
00202 }
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214 serializable_type * load( const std::string & src ) const
00215 {
00216 return ::s11nlite::load_serializable<serializable_type>( src );
00217 }
00218
00219
00220
00221
00222 serializable_type * load( std::istream & src ) const
00223 {
00224 return ::s11nlite::load_serializable<serializable_type>( src );
00225 }
00226
00227
00228
00229
00230
00231 serializable_type * load_buffer() const
00232 {
00233 s11n::mutex_sentry lock(this->m_mutex);
00234 if( this->m_buffer.empty() ) return 0;
00235 std::istringstream is( this->m_buffer );
00236 return ::s11nlite::load_serializable<serializable_type>( is );
00237 }
00238
00239
00240 };
00241
00242
00243 }
00244
00245
00246 #endif // s11nlite_MICROAPI_HPP_INCLUDED