Boost serialization- looks great, but I'm confused about serializing polymorphic types
I am an MFC veteran that wants to use a more modern mechanism for archival, in MFC a base pointer can be written and the object recreated automatically upon the read. I've been reading the serialization documentation and I am very confused about what I need to do to make this work using Boost, the code I have so far fails to compile with a static assert in the save function in oserializer.hpp. class CMyClassBase { public: CMyClassBase():m_TagBase(0){} virtual ~CMyClassBase(){} int m_TagBase; friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int /* file_version */) { ar & m_TagBase; } }; class CMyClassDerived : public CMyClassBase { public: CMyClassDerived():m_TagDerived(1){} virtual ~CMyClassDerived(){} int m_TagDerived; friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int /* file_version */) { boost::serialization::base_object<CMyClassBase>(*this); ar & m_TagDerived; } }; BOOST_CLASS_EXPORT_GUID(CMyClassDerived, "CMyClassDerived") void save_myclass(CMyClassBase* pMyClassBase, const char * filename){ // make an archive std::ofstream ofs(filename); boost::archive::text_oarchive oa(ofs); oa << pMyClassBase; } void restore_myclass(CMyClassBase* pMyClassBase, const char * filename) { // open the archive std::ifstream ifs(filename); boost::archive::text_iarchive ia(ifs); // restore the schedule from the archive ia >> pMyClassBase; }
Terence Wilson wrote:
I am an MFC veteran that wants to use a more modern mechanism for archival, in MFC a base pointer can be written and the object recreated automatically upon the read.
The boost serialization library was very much inspired by the MFC version so I would expect that it should come naturally to you.
I've been reading the serialization documentation and I am very confused about what I need to do to make this work using Boost, the code I have so far fails to compile with a static assert in the save function in oserializer.hpp.
<snip>
void save_myclass(CMyClassBase* pMyClassBase, const char * filename){
try changing the above to: void save_myclass(CMyClassBase const * const pMyClassBase, const char * filename){ And read the rationale section on why this works (assuming it does) Robert Ramey
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Robert Ramey Sent: Saturday, December 24, 2005 9:58 AM To: boost-users@lists.boost.org Subject: Re: [Boost-users] Boost serialization- looks great,but I'm confused about serializing polymorphic types
Terence Wilson wrote:
I am an MFC veteran that wants to use a more modern mechanism for archival, in MFC a base pointer can be written and the object recreated automatically upon the read.
The boost serialization library was very much inspired by the MFC version so I would expect that it should come naturally to you.
Sure, the underlying principles may be the same but I'm caught up in the boilerplate required to make it work. To be honest, I found the documentation difficult to assimilate because it reads like a technical discussion of the implementation details rather than a recipe book. This isn't intended as a criticism, obviously people learn in different ways, but speaking from a personal standpoint, it would really help if you had code samples for the top 5 use cases of the library. One people have working code the pressure comes off and they are more willing to learn about the nuances.
I've been reading the serialization documentation and I am very confused about what I need to do to make this work using Boost, the code I have so far fails to compile with a static assert in the save function in oserializer.hpp.
<snip>
void save_myclass(CMyClassBase* pMyClassBase, const char * filename){
try changing the above to:
void save_myclass(CMyClassBase const * const pMyClassBase, const char * filename){
And read the rationale section on why this works (assuming it does)
Thanks. It did work, but the assertion led me in completely the wrong direction. I appreciate you getting back to me during the holiday period. Merry Christmas to you and yours.
Robert Ramey
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Robert Ramey
-
Terence Wilson