Jump to content
  • Advertisement
Sign in to follow this  
Ultima2876

[C++] Boost::Serialize on class derived from std::vector

This topic is 3266 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm using the BEAGLE puppy library for some genetic programming stuff, and I need to code in the ability to serialize a tree so that I can resume the evolution process after quitting the program. The problem is, I don't know if I can (or how to) get boost::serialize to work with something like this: class Tree : public std::vector<Node> { public: template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & mFitness; ar & mValid; } float mFitness; //!< Fitness value of the GP tree bool mValid; //!< Flag indicating whether the fitness is valid. }; They key being that the class is derived from std::vector<Node>. It's also a very difficult problem to google because of the wording - "boost::serialize from std::vector derived class" was the best wording I could think of, and it didn't get me any useful results. Some help on this would be hugely appreciated :)

Share this post


Link to post
Share on other sites
Advertisement
I didn't write BEAGLE Puppy =/

If I knew how without having to change a billion [] and size() accesses throughout BEAGLE puppy, I would change the fact that Tree is derived from std::vector<Node>, but unfortunately I can't...

is there a workaround?

Share this post


Link to post
Share on other sites
Potshot in the dark: Try ar & (std::vector<Node>*)this; -- although I'm guessing it won't work. Nope, the docs say it won't.

I stumbled upon this -- try ar & boost::serialization::base_object<std::vector<Node> >(*this);

Share this post


Link to post
Share on other sites
Quote:
Original post by Ultima2876
I didn't write BEAGLE Puppy =/

If I knew how without having to change a billion [] and size() accesses throughout BEAGLE puppy, I would change the fact that Tree is derived from std::vector<Node>, but unfortunately I can't...

I'm pretty sure MaulingMonkey's suggestion is the right one. Alternatively, however, you could change Tree to have a vector as a private member, and simply write operator[] and size() and whatever to delegate to it. That's only really a reasonable proposition if the code is using the base class interface in a very limited way.... if it's taking a Tree::const_reverse_iterator and whatnot, it's probably not worth the pain to expose the full interface.

Share this post


Link to post
Share on other sites
Write your own operator[] and size() in Tree, then change Tree to contain a std::vector<Node> as one of its private members, and have the new operator[] and size() call vector_[k] and vector_.size().

Share this post


Link to post
Share on other sites
MaulingMonkey's suggestion is correct - this worked perfectly. Thanks!

Sneftel, you're right, it's also using other std::vector methods (including iterators) dotted around the place.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!