[Boost Users] Serialization -- serializing a map with shared_ptr values

I have the following two data structures: std::map<const std::string, boost::shared_ptr< C >, jedox::util::CaseInsensitiv > m_LookupName; std::map<unsigned int, boost::shared_ptr< C > > m_LookupId; Before serialization, the shared pointer - values of the maps do point to sth. meaningful. Afterwards, however, they point to data-structures with meaningless content. The keys (strings / ints) are serialized / unserialized correctly. I guess its a general problem with serializing shared pointers contained in a std::map as values !? BTW: the structures the shared pointers point to do have a serialization method themselves and I a assume that it is called when serializing the map. I serialize / unserialize as follows: template<class Archive> void save( Archive &ar, unsigned int version) const{ m_offlineMode = true; bool oldCachedAll = m_CachedAll; m_CachedAll = true; ar & m_CachedAll; ar & m_offlineMode; // do not serialize the lock -- use the default constructor. // we assume that no iterators that lock the cache do exist when serializing. // ar & m_Lock; ar & m_LookupId; // 1st structure ar & m_LookupName; // 2nd structure ar & m_SequenceNumber; //reset old state for the case we want to continue working m_CachedAll = oldCachedAll; m_offlineMode = false; } template<class Archive> void load( Archive &ar, unsigned int version) { ar & m_CachedAll; ar & m_offlineMode; // do not serialize the lock -- use the default constructor. // we assume that no iterators that lock the cache do exist when serializing. // ar & m_Lock; ar & m_LookupId; ar & m_LookupName; ar & m_SequenceNumber; //reset old state for the case we want to continue working } kind regards, Oliver

I can't imagin how something like m_offlineMode = true; even compiles given that save is a "const" function. If you can't live without this, it suggests that your saving operation is altering the thing its saving - which would conflict with the the idea that only one copy of anything is actually saved.
From looking at this - that's the only think I can ay.
Robert Ramey "Oliver Kania" <kania.oliver@googlemail.com> wrote in message news:1262c4ee0711090524w3a2e1a8i3355283c8c65ed98@mail.gmail.com... I have the following two data structures: std::map<const std::string, boost::shared_ptr< C >, jedox::util::CaseInsensitiv > m_LookupName; std::map<unsigned int, boost::shared_ptr< C > > m_LookupId; Before serialization, the shared pointer - values of the maps do point to sth. meaningful. Afterwards, however, they point to data-structures with meaningless content. The keys (strings / ints) are serialized / unserialized correctly. I guess its a general problem with serializing shared pointers contained in a std::map as values !? BTW: the structures the shared pointers point to do have a serialization method themselves and I a assume that it is called when serializing the map. I serialize / unserialize as follows: template<class Archive> void save( Archive &ar, unsigned int version) const{ m_offlineMode = true; bool oldCachedAll = m_CachedAll; m_CachedAll = true; ar & m_CachedAll; ar & m_offlineMode; // do not serialize the lock -- use the default constructor. // we assume that no iterators that lock the cache do exist when serializing. // ar & m_Lock; ar & m_LookupId; // 1st structure ar & m_LookupName; // 2nd structure ar & m_SequenceNumber; //reset old state for the case we want to continue working m_CachedAll = oldCachedAll; m_offlineMode = false; } template<class Archive> void load( Archive &ar, unsigned int version) { ar & m_CachedAll; ar & m_offlineMode; // do not serialize the lock -- use the default constructor. // we assume that no iterators that lock the cache do exist when serializing. // ar & m_Lock; ar & m_LookupId; ar & m_LookupName; ar & m_SequenceNumber; //reset old state for the case we want to continue working } kind regards, Oliver ------------------------------------------------------------------------------ _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Hello Robert, it compiles by making m_offlineMode mutable :) I can live without it by simply putting the bools into the archive like ar & true ar & true ( Someone said this works, didnt try it though) I can assure you I know what I am doing. I need to alter the state of the program depending on whether I am using something loaded from disk or not. Of course, there would be other ways but this seems to be most elegant to me ... Since I am deserializing the same vars in the same sequence as I am serializing, there shouldnt be a problem. (Or is there ? The bools I am changing during save() affect the maps in no way ...) kind regards, Oliver On Nov 9, 2007 5:13 PM, Robert Ramey <ramey@rrsd.com> wrote:
I can't imagin how something like
m_offlineMode = true; even compiles given that save is a "const" function.
If you can't live without this, it suggests that your saving operation is altering the thing its saving - which would conflict with the the idea that only one copy of anything is actually saved.
From looking at this - that's the only think I can ay.
Robert Ramey
"Oliver Kania" <kania.oliver@googlemail.com> wrote in message news:1262c4ee0711090524w3a2e1a8i3355283c8c65ed98@mail.gmail.com... I have the following two data structures:
std::map<const std::string, boost::shared_ptr< C >, jedox::util::CaseInsensitiv > m_LookupName; std::map<unsigned int, boost::shared_ptr< C > > m_LookupId;
Before serialization, the shared pointer - values of the maps do point to sth. meaningful. Afterwards, however, they point to data-structures with meaningless content. The keys (strings / ints) are serialized / unserialized correctly. I guess its a general problem with serializing shared pointers contained in a std::map as values !? BTW: the structures the shared pointers point to do have a serialization method themselves and I a assume that it is called when serializing the map.
I serialize / unserialize as follows:
template<class Archive> void save( Archive &ar, unsigned int version) const{
m_offlineMode = true; bool oldCachedAll = m_CachedAll; m_CachedAll = true; ar & m_CachedAll; ar & m_offlineMode; // do not serialize the lock -- use the default constructor. // we assume that no iterators that lock the cache do exist when serializing. // ar & m_Lock; ar & m_LookupId; // 1st structure ar & m_LookupName; // 2nd structure ar & m_SequenceNumber; //reset old state for the case we want to continue working m_CachedAll = oldCachedAll; m_offlineMode = false; }
template<class Archive> void load( Archive &ar, unsigned int version) {
ar & m_CachedAll; ar & m_offlineMode; // do not serialize the lock -- use the default constructor. // we assume that no iterators that lock the cache do exist when serializing. // ar & m_Lock; ar & m_LookupId; ar & m_LookupName; ar & m_SequenceNumber; //reset old state for the case we want to continue working }
kind regards, Oliver
------------------------------
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Oliver Kania
-
Robert Ramey