s11n.net
Saving untold millions of trees... of data.
Project powered by:
SourceForge.net

Sample client-side s11n code

Serializing std-namespace containers: list, vector, [multi]set, and [multi]map.

Updated for 0.9.x...

Here we show how to serialize STL containers of Serializables (see terms & definitions).

Gary Boone says the following about s11n's 100% non-intrusive, proxy-based approach to serialization, which provides a way of transparently proxying de/serialization requests for a given type via a proxy functor:
  

Dude, it works!! That's amazing! That's huge, allowing you to code serialization into your projects without even touching other people's code in distributed projects. It means you can experiment with the library without having to hack/unhack your primary codebase.

Actually, it means a lot more than the s11n/client-class separation Gary's so happy about, but appreciating some of the more subtle implications requires some working knowledge of, and experience with, s11n.

*blush* "Huge" is a great compliment.
i feel good. :)
The simplest way in the universe, by far, to save and load almost any std::map, multimap, pair, list, vector, set, multiset, or valarray is this:

#include <s11n.net/s11n/pods_streamable.hpp> // install proxies for basic PODs
// include the appropriate container registration header(s):
clude <s11n.net/s11n/list.hpp> // for list/vector containers
#include <s11n.net/s11n/map.hpp> // for set/map/pair containers
#include <s11n.net/s11n/valarray.hpp> // for valarray containers
// save and load an object:
//save to a file or stream:
s11nlite::save( mycontainer, "myfile.s11n" );
// load object from a file or stream:
MyContainerT * c = s11nlite::deserialize<MyContainerT>( "myfile.s11n" );
// Or skip streams and save/load to/from "data nodes":
// serialize the container to a data node:
s11nlite::serialize( targetnode, mycontainer );
// deserialize mycontainer from to a data node:
s11nlite::deserialize( srcnode, mycontainer );

As of version 0.8.2, container users need not register every combination of containers they use - C++ takes care of that for us when we include the appropriate proxy registration headers (as shown above).

This means we can , as long as the types contained in the containers are Serializable (that's quite a broad definition, and includes pointers to those Serializable types).

This feature completely eliminates the client-side maintenance involved with making such containers Serializable. For example, if you have a map containing only PODs, you must do nothing to make the map serializable. Even a container of containers requires no special support, e.g., map<string,list<int>>!

s11n can also easily "cast" almost-compatible containers to each other, e.g., a list<int> to a vector<double*>! The easiest way to do this is via s11nlite::s11n_cast(), as in this example:

// assume src is a list<MyType *> and target is a vector<MyType>
s11nlite::s11n_cast( src, target );

This also demonstrates how s11n is largely ignorant of pointerness, and will gladly convert serialized objects between (T) and (T*) (in many cases, anyway - this is algorithm-dependent).

Demo app

Rather that update all this text (again), i'll just link to the sample application code, which shows the serialization of structs and containers step-by-step. This demo is quite long, but shows a lot of s11n's features and tests them on various data structures.
(updated 11 June 2004, for 0.9.x)
(This source code lives under src/client/sample in the s11n source tree.)

The output from the above demo is in this file, but keep in mind that the data format is irrelevant for purposes of s11n. The library it comes with several Serializers (i.e., data formats/grammars), and adding your own can be done without touching the library code.

The above code shows very clearly how to serialize std::list, std::map and std::vector types. The concepts and techniques are identical for any type. It also shows how we can serialize any type which supplies accessor/mutator functions to allow us to get/save it's state, without the type knowing it's now a full-fledged member of the Serializable community. The ability to transparently and non-intrusively proxy serialization requests for a given type is one of s11n's most powerful features, as it allows us to serialize just about anything without that "anything" knowing "anything" about the serialization. s11n can even do so polymorphically, and can load new types from DLLs as needed.