Vector of Polymorphic Pointers
I am trying to serialize a vector of polymorphic pointers. My code looks as follows: class ASTNode{ ... protected: vector <ASTNode*> children; string name; string type; private: // Code for serialization friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, unsigned int version) { ar & name; ar & type; ar & children; } } class ASTNode2: public ASTNode{ ... private: // Code for serialization friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, unsigned int version) { // serialize base class information ar & boost::serialization::base_object<ASTNode>(*this); } // End code for serialization } I use the vector "children" to hold pointers to both base and derived objects. I have tried registering the derived classes at the top of the serialize function call, with the use of ar.register(static_cast<ASTNode*>(NULL)). I have also tried using the BOOST_CLASS_EXPORT_GUID macro to register the classes in main. Yet, I still get a unregistered class exception. Any guidance would be greatly appreciated.
Hmmm - it looks like ASTNode might not be polymorphic - it doesn't seem tohave a virtual function defined. Try the following change: "Juan Quiroz" <juancq@gmail.com> wrote in message news:f4e027970605011410p207777c1m1abef4f5771acc07@mail.gmail.com... I am trying to serialize a vector of polymorphic pointers. My code looks as follows: class ASTNode{ ... protected: vector <ASTNode*> children; string name; string type; private: // Code for serialization friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, unsigned int version) { ar & name; ar & type; ar & children; } virtual ~ASTNode(); // <== try adding this in! } class ASTNode2: public ASTNode{ ... private: // Code for serialization friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, unsigned int version) { // serialize base class information ar & boost::serialization::base_object<ASTNode>(*this); } // End code for serialization } I use the vector "children" to hold pointers to both base and derived objects. I have tried registering the derived classes at the top of the serialize function call, with the use of ar.register(static_cast<ASTNode*>(NULL)). I have also tried using the BOOST_CLASS_EXPORT_GUID macro to register the classes in main. Yet, I still get a unregistered class exception. Any guidance would be greatly appreciated. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
I forgot to include more details on the ASTNode class, it's actually like this: class ASTNode { public: ASTNode(string _type="", string _name="", string _value="", string _type_specifier=""); virtual ~ASTNode(); virtual void Code_Gen(); virtual void DumpOut(FILE *); ... void setName(string _name); void clear(); protected: string name; string type; string type_specifier; string value; // Children nodes vector<ASTNode *> children; private: // Code for serialization friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, unsigned int version) { ar & name; ar & type; ar & type_specifier; ar & value; ar & children; } }; The derived classes implement the CodeGen and DumpOut methods. Thank you for your prompt reply! On 5/1/06, Robert Ramey <ramey@rrsd.com> wrote:
Hmmm - it looks like ASTNode might not be polymorphic - it doesn't seem tohave a virtual function defined.
Try the following change: "Juan Quiroz" <juancq@gmail.com> wrote in message news:f4e027970605011410p207777c1m1abef4f5771acc07@mail.gmail.com...
I am trying to serialize a vector of polymorphic pointers. My code looks as follows:
class ASTNode{ ... protected: vector <ASTNode*> children; string name; string type;
private: // Code for serialization friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, unsigned int version) { ar & name; ar & type; ar & children; } virtual ~ASTNode(); // <== try adding this in! } class ASTNode2: public ASTNode{ ... private: // Code for serialization friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, unsigned int version) { // serialize base class information ar & boost::serialization::base_object<ASTNode>(*this); } // End code for serialization }
I use the vector "children" to hold pointers to both base and derived objects. I have tried registering the derived classes at the top of the serialize function call, with the use of ar.register(static_cast<ASTNode*>(NULL)). I have also tried using the BOOST_CLASS_EXPORT_GUID macro to register the classes in main. Yet, I still get a unregistered class exception. Any guidance would be greatly appreciated.
_______________________________________________ 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
I use the vector "children" to hold pointers to both base and derived objects. I have tried registering the derived classes at the top of the serialize function call, with the use of ar.register(static_cast<ASTNode*>(NULL)). That is probably the wrong place to put it. should be in a place that guarentees that it will be called. Try it just after the archive is created. If that does it, you can experiment with moving it around. I have also tried using the BOOST_CLASS_EXPORT_GUID macro to register the classes in main. Yet, I still get a unregistered class exception. Any guidance would be greatly appreciated. Note that its not enough to include BOOST_CLASS_EXPORT. It has to be included after the *archive.hpp classes that you're using. Double check that this is the case. Robert Ramey
Thank you so much!! I moved the register calls just after the archive is created and it worked! Thanks again!! You have literally saved me hours of work!! Thanks for the prompt responses! On 5/1/06, Robert Ramey <ramey@rrsd.com> wrote:
I use the vector "children" to hold pointers to both base and derived objects. I have tried registering the derived classes at the top of the serialize function call, with the use of ar.register(static_cast<ASTNode*>(NULL)).
That is probably the wrong place to put it. should be in a place that guarentees that it will be called. Try it just after the archive is created. If that does it, you can experiment with moving it around.
I have also tried using the BOOST_CLASS_EXPORT_GUID macro to register the classes in main. Yet, I still get a unregistered class exception. Any guidance would be greatly appreciated.
Note that its not enough to include BOOST_CLASS_EXPORT. It has to be included after the *archive.hpp classes that you're using. Double check that this is the case.
Robert Ramey
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Juan Quiroz wrote:
Thank you so much!! I moved the register calls just after the
archive is created and it worked! Thanks again!! You have literally saved me hours of work!! Thanks for the prompt responses! Juan, You might want to take a look at Boost.Ptr Container: http://www.boost.org/libs/ptr_container/doc/ptr_container.html The new version (Boost 1.34) in the release branch contains full support for Serialization (and lots of improved documentation). -Thorsten
Robert Ramey wrote:
Note that its not enough to include BOOST_CLASS_EXPORT. It has to be included after the *archive.hpp classes that you're using. Double check that this is the case.
Robert, why is this needed? It seems like something that is *very* easy to forget and get wrong. -Thorsten
This is explained in the docmentation under Reference/Special Considerations/Exporting Class Serialization Robert Ramey Thorsten Ottosen wrote:
Robert Ramey wrote:
Note that its not enough to include BOOST_CLASS_EXPORT. It has to be included after the *archive.hpp classes that you're using. Double check that this is the case.
Robert, why is this needed? It seems like something that is *very* easy to forget and get wrong.
-Thorsten
Thorsten Ottosen <tottosen@dezide.com> writes:
Robert Ramey wrote:
Note that its not enough to include BOOST_CLASS_EXPORT. It has to be included after the *archive.hpp classes that you're using. Double check that this is the case.
Robert, why is this needed? It seems like something that is *very* easy to forget and get wrong.
Yes, header ordering is notoriously hard to control. Robert, I'd like to discuss the design problem it is solving and see if we can come up with a better approach. Can you boil it down to a really simple example? -- Dave Abrahams Boost Consulting www.boost-consulting.com
This is explained in the docmentation under Reference/Special Considerations/Exporting Class Serialization Robert Ramey David Abrahams wrote:
Thorsten Ottosen <tottosen@dezide.com> writes:
Robert Ramey wrote:
Note that its not enough to include BOOST_CLASS_EXPORT. It has to be included after the *archive.hpp classes that you're using. Double check that this is the case.
Robert, why is this needed? It seems like something that is *very* easy to forget and get wrong.
Yes, header ordering is notoriously hard to control. Robert, I'd like to discuss the design problem it is solving and see if we can come up with a better approach. Can you boil it down to a really simple example?
"Robert Ramey" <ramey@rrsd.com> writes:
This is explained in the docmentation under
Reference/Special Considerations/Exporting Class Serialization
Robert Ramey
I have analyzed your issue and I believe there is a better way; one that avoids the header ordering requirement. I have put together a small demonstration, which is enclosed. This shows that we can instantiate a class template (instantiator) for the exported class and each archive that's been seen. Then the only challenge is causing that class template instantiation to actually instantiate executable code. Fortunately, I've recently addressed that problem in changes to the serialization library. The basics are in CVS at: boost/boost/concept/detail/general.hpp boost/boost/concept/detail/msvc.hpp boost/boost/concept/detail/borland.hpp (Ignore the part that says BOOST_OLD_CONCEPT_SUPPORT), so we have the technology. And if you use the function pointer technique of BOOST_CONCEPT_ASSERT(( ... )) in boost/boost/concept/assert.hpp you'll be able to use BOOST_CLASS_EXPORT on class template instances with multiple arguments. BOOST_CLASS_EXPORT(foo<bar,baz>) // an error, today. I hope all this is helpful. -- Dave Abrahams Boost Consulting www.boost-consulting.com
David Abrahams <dave@boost-consulting.com> writes:
"Robert Ramey" <ramey@rrsd.com> writes:
This is explained in the docmentation under
Reference/Special Considerations/Exporting Class Serialization
Robert Ramey
I have analyzed your issue and I believe there is a better way; one that avoids the header ordering requirement. I have put together a small demonstration, which is enclosed. This shows that we can instantiate a class template (instantiator) for the exported class and each archive that's been seen.
Then the only challenge is causing that class template instantiation to actually instantiate executable code. Fortunately, I've recently addressed that problem in changes to the serialization library. The basics are in CVS at:
boost/boost/concept/detail/general.hpp boost/boost/concept/detail/msvc.hpp boost/boost/concept/detail/borland.hpp
(Ignore the part that says BOOST_OLD_CONCEPT_SUPPORT), so we have the technology. And if you use the function pointer technique of BOOST_CONCEPT_ASSERT(( ... )) in boost/boost/concept/assert.hpp you'll be able to use BOOST_CLASS_EXPORT on class template instances with multiple arguments.
BOOST_CLASS_EXPORT(foo<bar,baz>) // an error, today.
I hope all this is helpful.
Sorry, the attachment came with the wrong MIME type; I don't know if that's a problem for anyone, but here it is again: -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (4)
-
David Abrahams
-
Juan Quiroz
-
Robert Ramey
-
Thorsten Ottosen