I have a templated container class that internally stores data in a following, static, data variable:
template<typename T>
class Container {
// ... Access methods
std::map<unsigned int, std::map<unsigned int, boost::shared_ptr<T>>
};
The first unsigned int is an id of a Client using this collection, the second unsigned int is used to identify SubClient within the Client object. The Client object holds just the ids of SubClient’s and SubClient’s don’t hold any data themselves.
Now I need to save the whole client structure to a file. That is calling:
Client c;
/* Some operations on c */
c.Serialize(output);
should result in a file that contains the ids of SubClient’s and the associated data from the container classes. Now since the types that can be used in container classes can be of almost any type, and new types are being added and removed all the time from the codebase how can one serialize and deserialize such data?
The problem, how I see it, is in the fact that data types have no unique IDs that can be used to identify the part of a file as belonging to Container holding specific data nor can be ordered in any unambiguous way (or can they?).
How can I solve this with as little as possible changes in the data types used for containers.
You’ve described what Boost.Serialization does perfectly. No changes to your containers or their data types should be necessary. The library has built-in support for serializing STL containers and
boost::shared_ptralready. Though you may need to add serialization methods forTdepending on what it is.