00001 #ifndef S11N_NET_S11N_1_3_CPPX0_HPP_INCLUDED
00002 #define S11N_NET_S11N_1_3_CPPX0_HPP_INCLUDED 1
00003
00004 #include <algorithm>
00005 #include <iterator>
00006 #include <tuple>
00007 #include <s11n.net/s11n/algo.hpp>
00008 #include <s11n.net/s11n/variant.hpp>
00009 #include <s11n.net/s11n/s11n_config.hpp>
00010 #if ! s11n_CONFIG_HAVE_CPP0X
00011 #error "s11n_CONFIG_HAVE_CPP0X is not set, which means this code probably won't compile (it needs C++0x support)"
00012 #endif
00013
00014 namespace s11n {
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 namespace cpp0x {
00025
00026
00027
00028
00029
00030 template <typename ... Args> struct count;
00031 template <typename T,typename ... Args>
00032 struct count<T,Args...> {
00033 static const int value = 1 + count<Args...>::value;
00034 };
00035 template <> struct count<> { static const int value = 0; };
00036
00037 namespace Detail {
00038
00039
00040
00041 template <typename NodeT>
00042 inline bool serialize_group_impl( const size_t, NodeT & )
00043 {
00044 return true;
00045 }
00046
00047
00048
00049
00050 template <typename NodeT, typename SerT, typename ... SerList>
00051 inline bool serialize_group_impl( const size_t high, NodeT & dest, SerT const &src, SerList && ... srcN )
00052 {
00053 typedef s11n::s11n_traits<SerT> ST;
00054 return s11n::serialize_subnode( dest,
00055 s11n::format_string( "i%d", high - s11n::cpp0x::count<SerList...>::value ),
00056 src )
00057 && serialize_group_impl( high, dest, srcN... );
00058 }
00059 }
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105 template <typename NodeT, typename SerT, typename ... SerList>
00106 inline bool serialize_group( NodeT & dest, std::string const & groupName, SerT const &src, SerList && ... srcN )
00107 {
00108 typedef s11n::s11n_traits<SerT> ST;
00109 NodeT & tgt( s11n::create_child( dest, groupName ) );
00110 s11n::node_traits<NodeT>::class_name(tgt, "serialize_group");
00111 return Detail::serialize_group_impl( 1+count<SerList...>::value, tgt, src, std::forward<SerList>(srcN)... );
00112 }
00113
00114 namespace Detail {
00115
00116
00117
00118 template <typename NodeT>
00119 inline bool deserialize_group_impl( size_t const, NodeT const & )
00120 {
00121 return true;
00122 }
00123
00124
00125
00126 template <typename NodeT, typename SerT, typename ... SerList>
00127 inline bool deserialize_group_impl( size_t const high, NodeT const & src, SerT & dest, SerList && ... destN )
00128 {
00129 typedef s11n::s11n_traits<SerT> ST;
00130 typedef s11n::node_traits<NodeT> NTR;
00131 return s11n::deserialize_subnode( src,
00132 s11n::format_string( "i%d", high - count<SerList...>::value ),
00133 dest )
00134 && deserialize_group_impl( high, src, std::forward<SerList>(destN)... );
00135 }
00136 }
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148 template <typename NodeT, typename SerT, typename ... SerList>
00149 inline bool deserialize_group( NodeT const & src, std::string const & groupName, SerT & dest, SerList && ... destN )
00150 {
00151 if( NodeT const * ch = s11n::find_child_by_name( src, groupName ) )
00152 {
00153 return Detail::deserialize_group_impl( 1 + count<SerList...>::value, *ch, dest, std::forward<SerList>(destN)... );
00154 }
00155 throw s11n::s11n_exception(S11N_SOURCEINFO,"deserialize_group(node,'%s',...): child node not found.", groupName.c_str() );
00156 }
00157
00158
00159 namespace Detail {
00160
00161
00162
00163 template <typename NodeT>
00164 inline bool serialize_subnodes( NodeT & ) throw()
00165 {
00166 return true;
00167 }
00168
00169
00170
00171 template <typename NodeT>
00172 inline bool deserialize_subnodes( NodeT const & ) throw()
00173 {
00174 return true;
00175 }
00176 }
00177
00178
00179
00180
00181
00182
00183
00184 template <typename NodeT, typename SerT, typename ... SerList>
00185 inline bool serialize_subnodes( NodeT & dest, std::string const & name, SerT const & src, SerList && ... srcN )
00186 {
00187 static_assert( 0 == (count<SerList...>::value % 2),
00188 "Wrong number of template args passed to serialize_subnodes(): args must be passed in pairs of (string,Serializable)");
00189 using namespace Detail;
00190 return s11n::serialize_subnode( dest, name, src )
00191 && serialize_subnodes( dest, std::forward<SerList>(srcN)... );
00192 }
00193
00194
00195
00196
00197
00198
00199
00200
00201 template <typename NodeT, typename SerT, typename ... SerList>
00202 inline bool deserialize_subnodes( NodeT const & src, std::string const & name, SerT & dest, SerList && ... destN )
00203 {
00204 static_assert( 0 == (count<SerList...>::value % 2),
00205 "Wrong number of template args passed to deserialize_subnodes(): args must be passed in pairs of (string,Serializable)");
00206 using namespace Detail;
00207 return s11n::deserialize_subnode( src, name, dest )
00208 && deserialize_subnodes( src, destN... );
00209 }
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225 template <typename NodeT, typename VersionT, typename SerT, typename ... SerList>
00226 inline bool serialize_versioned( NodeT & dest, VersionT ver, SerT const & src, SerList && ... srcN )
00227 {
00228 typedef s11n::node_traits<NodeT> NTR;
00229 NTR::set( dest, "version", ver );
00230 return serialize_group( dest, "vdata", src, srcN... );
00231 }
00232
00233
00234
00235
00236
00237
00238
00239
00240 template <typename NodeT, typename VersionT, typename SerT, typename ... SerList>
00241 bool deserialize_versioned( NodeT & src, VersionT ver, SerT & dest, SerList && ... destN )
00242 {
00243 typedef s11n::node_traits<NodeT> NTR;
00244 VersionT gotver( NTR::get( src, "version", VersionT() ) );
00245 if( ver != gotver )
00246 {
00247 using s11n::Detail::variant;
00248 variant v1( ver );
00249 variant v2( gotver );
00250 throw s11n_exception( S11N_SOURCEINFO, "Version mismatch. Expected '%s' but got '%s'.",
00251 v1.str().c_str(),
00252 v2.str().c_str() );
00253 }
00254 return deserialize_group( src, "vdata", dest, destN... );
00255 }
00256
00257
00258 } }
00259
00260 #endif // S11N_NET_S11N_1_3_CPPX0_HPP_INCLUDED