Reputation: 1447
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.
Upvotes: 0
Views: 1101
Reputation: 20282
For the container itself you can use std::for_each
, thus iterating through the whole thing, but each separate instance of data (the one that's of type T
), you'll have to implement a serialization method which you'll call from the function called by thefor_each
.
That function will probably have to use dynamic_cast
operator, to get the actual object out of the pointer, and make sure that it is valid.
Upvotes: 0
Reputation: 24174
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_ptr
already. Though you may need to add serialization methods for T
depending on what it is.
Upvotes: 2