[Serialization] getting version of items stored in STL collection
I'm converting parts of a program to use boost::shared_ptr. I have a vector of pointers to objects std::vector<Object*> which now becomes std::vector<ObjectPtr> where ObjectPtr = make_shared<Object>(). I must support archives (XML files) created previously. Boost::serialization looks after saving STL collections very nicely. However, now, in reading a file, I need to determine the item_version in the collection so I can read in a vector of Object* or ObjectPtr. I also need to change the item_version when saving new files to indicate ObjectPtrs are being saved. I've dug through the documentation and source for hours but can't find anything that I can use (my own limitations perhaps?). This vector is NOT a class member and so class versioning does not apply unfortunately. Also, I am using bespoke load/save functions and NOT the serialize(Archive& ar, const unsigned int version) which would avoid this problem (I posted about this before - due to move from MFC to boost and mix of Doc/View architecture). I realize now I should have put a class version member of the MyAppDoc file structure which would be read in first. This would save all the pain. I've written some functions to interface with the file for the meantime, but I'm sure there must be a better solution: template<class Archive, class T> inline void load_vsp_from_vp(Archive& ar, std::vector<boost::shared_ptr<T>>& vPtr, const char* name) { std::vector<T*> vT; ar & boost::serialization::make_nvp(name, vT); boost::shared_ptr<T> ptr; vPtr.assign(vT.size(), ptr); for(int i = 0; i != vT.size(); ++i) vPtr.at(i).reset( vT.at(i) ); } template<class Archive, class T> inline void save_vp_from_vsp(Archive& ar, std::vector<boost::shared_ptr<T>>& vPtr, const char* name) { std::vector<T*> vT; for(int i = 0; i != vPtr.size(); ++i) vT.push_back( vPtr.at(i).get() ); ar & boost::serialization::make_nvp(name, vT); } Use as: save_vp_from_vsp<boost::archive::xml_oarchive, Object>(oa, m_vObject, "m_vObject"); Any help appreciated.
I have to say it's very hard for me to follow all this. For most standard collections such as std::vector<T> the serialization library takes care of the version of T. There are only a few exceptions to shis - such as vector<std::string> etc. So everything should "just work" Robert Ramey
On Thu, Jul 21, 2011 at 4:41 AM, Robert Ramey <ramey@rrsd.com> wrote:
I have to say it's very hard for me to follow all this.
For most standard collections such as std::vector<T> the serialization library takes care of the version of T. There are only a few exceptions to shis - such as vector<std::string> etc. So everything should "just work"
Robert, Thanks for replying and apologies the explanation wasn't good. I think the solution is to introduce into the MFC Doc class a member variable m_Version and load either std::vector<T*> or std::vector< shared_ptr<T> > as appropriate. Trying to read one line and determine the item_version is probably silly. This issue is closely related to my other post today ([Boost-users][serialization] Memory leaks even with shared_ptrs) and I'd be grateful if you were able to give that a look. And a million thanks for the library - it really is an incredibly useful tool. Colin
participants (2)
-
cc caprani
-
Robert Ramey