|
Boost : |
From: Marc Salaun (salaun_at_[hidden])
Date: 2007-03-14 10:51:11
All,
I have written an 'extensible' xml serialisation library (xxs) that
may be of interest to some people who contributed to this thread.
It tries to get as close as possible to python's 'batteries included'
philosophy, while still being as easy to extend as possible.
For a start, here is a full mutli-threaded xml-rpc server, with 10
worker threads:
#include "xxs/xxs.h"
#include <list>
#include <numeric>
int sum(const std::list<int>& values) {
return std::accumulate(values.begin(), values.end(), 0);
}
void main() {
xxs::server server("ldn013850", "8081", 10);
server.implement_xmlrpc_function("sum", &sum);
server.run();
}
You extend xxs to support a new STL-like container this way:
template<typename T, typename Format>
struct xxs::is_container<std::set<T, Format> : boost::true_type {
typedef T element_type;
static const bool ordered = true;
};
Containers, maps, smart pointers, basic types and classes can be
combined recursively
Here is how you add support for a struct or class:
a. In an intrusive manner, by adding a static xxs method
class circle {
point _center;
double _radius;
public:
../..
double get_radius() const { return _radius; }
void set_radius(double value) { _radius = value; }
template <typename Format, class T>
static void xxs(T& t) {
t.add("center", &circle::_center);
t.add("radius", &circle::get_radius, &circle::set_radius);
}
};
b. In a non intrusive manner, for std::pair<> in this case
template<typename KEY, typename VALUE, typename Format>
struct xxs::is_object<std::pair<KEY, VALUE>, typename Format>
: boost::true_type
{
template <typename Format, class M>
static void introspect(M& m) {
m.add("first", &std::pair<KEY, VALUE>::first);
m.add("second", &std::pair<KEY, VALUE>::second);
}
};
You serialize/deserialize 'data' this way:
std::cout << xxs::xml<>(data) << std::endl;
std::cout << xxs::xml<xxs::xmlrpc>(data) << std::endl;
std::cin >> xxs::xml<>(data);
You can specify xml-rpc or 'normal' xml formats with or without
attributes. A simplified soap format is also supported (see
http://www.soapware.org/bdg specs, interop untested).
You can extend the library to support additional smart pointers, basic
types, make certain C++ classes or enums to be processed as strings or
integers, etc..
Finally you have the option to serialize/deserialize arbitrary
structures using the xxs::is_generic_object trait class, for instance
to serialize/deserialize boost::python::object. It works very well
with python's xmlrpclib module :-)
So far the library compiles on VC7.1 and VC8.
I don't think there is anything groundbreaking there (it mimics and
uses other boost libraries - mainly traits and asio for the
client/server bits), but i believe the end result is very useful:
- It is very reliable, thread-safe, memory leak free and the code
generated is particularly efficient
- The omnipresent 'Format' template parameter allows for interesting
strategies for xml versioning and gives extra flexibility
- You can reuse the xxs template method for many other purposes
Now for the bad news:
- I will probably not have the time to push this library up to boost
standards all by myself - although i can spend some reasonable amount
of time on it
- The library uses expat - but plugging it in a Spirit equivalent
wouldn't be hard to do
The way i see this library going forward is to have to have some of
its parts rewritten using Spirit or Spirit techniques - not
necessarily to replace expat but to allow more user control over the
XML structure being generated, potentially covering the full SOAP
specs this way.
I have the following questions for the boost community:
- Is there any room for such a library in boost ?
- Is there anyone out there with a similar project/interest who would
like to join forces ?
thanks for your feedback
marc
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk