Sign in to follow this  
RunningInt

Boost Serialization

Recommended Posts

RunningInt    133
I have decided to try learning the boost serialization library to try and make writing code to save and load game state to disk a lot faster. Maybe if I get more productive I might actually finish a game one day. After far too much time I finally managed to figure out how to compile the boost serialization lib. I am using gcc. Then I tried to compile a very simple example:
#include <fstream>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/utility.hpp>

class Test
{
  private:
  friend class boost::serialization::access;             
  int myint;
  
  template<class Archive>
  void serialize(Archive & ar, const unsigned int version) const
  {
    ar & myint;
  }
  public:
  Test(int myint) : myint(myint) { }
};

int main(int argc, char *argv[])
{
    Test tt(5);
    std::ofstream ofs("filename.txt");
    {
      boost::archive::text_oarchive oa(ofs);
      oa << tt;
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

But I run into the following compile error: incomplete type `boost::STATIC_ASSERTION_FAILURE<false>' does not have member `value' This is caused by the following line in oserializer.hpp which gets called as part of the << operator being invoked above: BOOST_STATIC_ASSERT(check_tracking<T>::value); After a few wasted hours of trying to figure out what was going on I finally saw the comment just before that line in oserializer.hpp. It reads: // if your program traps here, it indicates taht your doing one of the following: // a) serializing an object of a type marked "track_never" through a pointer. // b) saving an non-const object of a type not markd "track_never) // Either of these conditions may be an indicator of an error usage of the // serialization library and should be double checked. See documentation on // object tracking. ***************** At this point I have wasted another 2 hours thinking that the above comment did not apply because i was serializing an int. I tried making the member int const and all kinds of stupid things. It was only in copy pasting the above comment into this post that I realized I am serializing a Test object, not an int. By changing the line of my code: test tt(5); to const test tt(5); it compiles and works. So do I really need to post this? Probably not, but like hell im going to delete everything I've typed so far. Fortunately I new questions. The serialization library is protecting me from some danger by forcing me to only use const objects. But I don't like having to declare all my objects as const. Obviously const_casting is just going to cancel out the protection and I doubt that is a good idea as I have very little idea what it is protecting me from. Is it normal practice to declare objects as const when using the boost serialization library, or dao people typically const_cast and turn off object tracking here and there? In other words do I have to actually understand the purpose of object tracking in order to use the serialization library? Thanks

Share this post


Link to post
Share on other sites
Sutekh    122
He explains here http://www.boost.org/libs/serialization/doc/rationale.html#trap
why there is a trap on non-const objects. I agree tho, its a bit of a pain to have to cast everything to a const, in his demo's he uses save_x or load_x functions that just cast the object when its passed in.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this