Boost logo

Boost Users :

Subject: Re: [Boost-users] [serialization] transparently treating a class like a built-in type
From: Stefan Strasser (strasser_at_[hidden])
Date: 2010-07-27 11:22:56

Zitat von Kenneth Riddile <kfriddile_at_[hidden]>:

> I have some xml archives that store a lot of floating point values.
> I would like to read these into a program that is designed to easily
> switch between using floats and a custom fixed-point type via a
> typedef. The way that would be easiest would be to add a
> serialize() method to the fixed-point class definition. However,
> the xml archive requires that I use an nvp wrapper. Since the data
> was originally serialized out as plain floats, the data looks
> something like this:
> <x>0.95260203</x>
> <y>0.87517798</y>
> <z>0.54323343</z>

I'm not sure if it was intended to be used that way, but you could use
Boost.Serialization's versioning.

namespace serialization{
struct version<your_type>
   : mpl::integral_c<unsigned int,mpl::if_int_then_0_else_1>{};

void load(Archive &ar,unsigned int version){
   if(version == 0) load fixed point...
   else load floating point...
   ...into whatever type is currently typedef'ed

wrt to your "value" "indirection", just load the float directly, or if
that's not an option because of encapsulation use a temporary and
initialize "x" later on.

ar >> make_nvp("x",x.value);

instead of

ar & NVP(x);

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at