Sample client-side s11n codeSerializing 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:
*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:
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:
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 appRather 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)
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. |
[home] | This site is developed in conjunction with: toc, SpiderApe, & wanderinghorse.net. | [top] |