2

I'm not able to use boost::serialization because it has library dependencies so I'm trying to figure out a way to do it myself. It doesn't matter if that means copying from boost::serialization.

After reading this answer to a similar question, I had a look at boost/serialization/variant.hpp and found save() function which is straight forward and understandable for me.

However the load() function looks more complicated: There is a recursion involving load() and variant_impl<types>::load() and a decremented which parameter. So apparently the code iterates over each type of the variant in order to convert the int which into a type. The rest is beyond me.

I know that boost has lot of code to make it portable so maybe there is a less-portable but easier way to do this?

Community
  • 1
  • 1
foraidt
  • 5,519
  • 5
  • 52
  • 80

1 Answers1

1

If you were to remove the serialization stuff from a copy of boost/serialization/variant.hpp (apart from the Archive template parameter) - i.e. get throw your own exception types and change e.g.

ar >> BOOST_SERIALIZATION_NVP(which);
// to:
ar >> which;

Then it looks like you should be able to replace Archive with std::ostream or std::istream in the save/load functions, respectively.

Not tried it, but at a glance it looks like it should work.

I guess it does depend on what you are actually using to serialize the data if not using boots::serialization?

Pete
  • 4,784
  • 26
  • 33
  • I'm trying to put my data into an MFC `CArchive` object... I tried removing the NVP stuff but the `CArchive` doesn't seem to be a good fit for all this.. – foraidt Jul 20 '11 at 16:15
  • @foraidt : `CArchive` only works with types that derive from `CObject`, which `boost::variant<>` clearly does not. – ildjarn Jul 20 '11 at 16:24
  • @ildjarn `CArchive::operator<<()` is overloaded for quite a few types (see http://msdn.microsoft.com/en-us/library/331a7zh8%28v=vs.80%29.aspx). This allows serializing much more than `CObject`-derived classes. My goal is to serialize not the `variant` itself but the data it contains. – foraidt Jul 20 '11 at 16:32
  • If you are trying to serialize the data contained by the variant, then surely you'll have to serialize the variant, because by its nature, it varies what the data actually is. Have you tried folloing my suggestion and passing in CArchive as the archive type? Looking more closely, you'll also need to get rid of the 'serialize' function from the header and call 'load' or 'save' as appropriate. When you say that CArchive is not a good fit, is this because it won't compile? – Pete Jul 21 '11 at 08:50
  • i.e. call int version = 0; CArchive ar; load(ar, my_variant, version) – Pete Jul 21 '11 at 08:51
  • Ok, i got it to work now. It boils down to removing the `ar.reset_object_address(...)` call and the `NVP` stuff. Another problem was that one of the variant's types was `std::string`, which `CArchive` does not know. Adding `operator <<(CArchive &, const std::string &)` and `operator >>(CArchive &, std::string &)` fixed this. – foraidt Jul 26 '11 at 14:32