s11n and C++0x
May 9, 2008 on 7:46 pm | In General, libs11n | 3 CommentsA couple weeks ago i came across PEGTL (http://code.google.com/p/pegtl/) and that got me really interested in some of the new C++0x features. gcc 4.3 has beta support for 0x, and i’ve decided that i will start adding some optional parts to libs11n 1.3.x. While i’m not yet entirely sure that variadic templates will be useful in s11n, i’ve put together some code which shows some of the potential:
#include "s11n.net/s11n/s11nlite.hpp"
#include "s11n.net/s11n/s11n_debuggering_macros.hpp"
#include "s11n.net/s11n/proxy/pod/int.hpp"
#include "s11n.net/s11n/proxy/pod/double.hpp"
#include "s11n.net/s11n/proxy/pod/string.hpp"
#include "s11n.net/s11n/0x/ohex.hpp"
int main(int argc, char *argv[])
{
int vi = 42;
double vd = 42.42;
std::string vs(”i’m a std::string”);
try
{
s11nlite::node_type node;
bool ret = s11n::cpp0x::serialize_v(node, vi, vd, vs);
// ^^^ we can pass any number of Serializables here
if( ret )
{
s11nlite::save( node, std::cout );
}
}
catch(std::exception const & ex)
{
CERR < < "EXCEPTION: " << ex.what() << '\n';
return 1;
}
return 0;
}
That outputs:
#SerialTree 1
s11n_node class=s11n::s11n_node
{
int class=int
{
v 42
}
double class=double
{
v 42.42
}
string class=string
{
v i'm a std::string
}
}
This approach has some serious limitations (mainly involving naming of the children serialized this way), but as i get more comfortable with 0x i hope to find new ways to use it to empower s11n more. Your ideas are of course welcomed…
[A short while later...]
i’ve refined the above to de/serialize_group(), which can be used like this:
int main(int argc, char *argv[])
{
int vi = 42;
double vd = 42.42;
std::string vs(”i’m a std::string”);
try
{
s11nlite::node_type node;
bool ret = s11n::cpp0x::serialize_group(node, “pods”, vi, vd, vs);
if( ret )
{
s11nlite::save( node, std::cout );
}
else
{
throw s11n::s11n_exception( S11N_SOURCEINFO, “serialize_group() failed” );
}
int di = -1;
double dd = -1;
std::string ds = “error”;
ret = s11n::cpp0x::deserialize_group(node, “pods”, di, dd, ds);
if( ret )
{
COUT < < "Deserialized group: ("<< di <<", "<< dd <<", ["<< ds <<"])\n";
}
else
{
throw s11n::s11n_exception( S11N_SOURCEINFO, "deserialize_group() failed" );
}
}
catch(std::exception const & ex)
{
CERR << "EXCEPTION: " << ex.what() << '\n';
return 1;
}
return 0;
}
In addition to the output shown above, it shows us the results of the deserialization:
0x.cpp:36 : Deserialized group: (42, 42.42, [i'm a std::string])
[And a while later ...]
These variadics are cool. We can now easily serialize multiple child objects more flexibly than the above example:
serialize_subnodes( node, "myInt", di, "myDouble", dd, "myString", ds );
That takes pairs of (string,Serializable) and serializes each object into a subnode with the given name.
We can then deserialize with:
int myi; double myd; std::string mystr; deserialize_subnodes( node, "myInt", myi, "myDouble", myd, "myString", mystr );
And the order of the (string,Serializable) pairs passed to deserialize_subnodes() is independent of the order passed to serialize_subnodes(). The first functions shown above do not have that property - they require the args in the same order. With deserialize_subnodes() we also have the option of partial deserialization - fetching a subset of the serialized data.
Powered by WordPress with Pool theme design by Borja Fernandez.
Entries and comments feeds.
Valid XHTML and CSS. ^Top^