Boost logo

Boost Users :

Subject: Re: [Boost-users] Generic Properties and serializable boost::any.
From: Noah Roberts (roberts.noah_at_[hidden])
Date: 2010-02-24 12:14:06


In article <MPG.25e491bc9040612998968c_at_[hidden]>,
roberts.noah_at_[hidden] says...
>
> In article <6d5e15f61002151148h2d3ce4d6u799b11ea61ebf9a4
> @mail.gmail.com>, mgpensar_at_[hidden] says...
> >
> > Dear All,
> >
> > Googling the subject I see there were some discussion about the subject
> > sometime ago.
> >
> > Does anybody has implemented a serializable version (in the sense of the
> > boost serialization library) of boost::any ?
> >
> > If you do, would you mind sharing it ?
> >
> > I want to implement a generic Property class. I have taken a look at
> > Property Tree but that requires an homogeneous container (it requires to
> > pass the data type in the constructor template parameter. I would like to
> > pass only the key type and use boost::any to store the values.
> > However I need it to be serializable. I am learning generic programming and
> > I am a bit out of my league here so I would like to check if anybody has
> > already ventured in making boost::any serializable before I try it myself.
> >
> > The goal would be to get something that would give me functionality similar
> > to get<T>(key) as we can find in Property Tree but requiring the data type
> > in a set<T>(key) method instead of asking for it in the constructor.
> > Does it make sense ? Am I in the right path using boost::any ? Is it even
> > possible in C++ ?
> >
> > Thank you for your attention,
>
> The methods used by boost::any are pretty easy and well documented in
> the cited document "Valued Conversions" ( http://www.two-
> sdg.demon.co.uk/curbralan/papers/ValuedConversions.pdf ).
>
> To make it serializable you'd simply need to roll your own "any" and
> make the placeholder a polymorphic, serializable object...which is well
> documented in the boost::serialization library documentation:
>
> http://www.boost.org/doc/libs/1_39_
> 0/libs/serialization/doc/tutorial.html#derivedclasses

I was mistaken. This is NOT easy. In fact, I don't think it's even
possible without breaking some form of the static polymorphism available
in the serialization library.

The only option I could think of was to provide a registration function
in the "my_any" that would register each of the handle<T> objects that
could be saved and loaded. I can't think of any way to provide the
necessary information for that though that does not come from the
developer, like:

my_any::register_types< mpl::vector<T1,T2,T3...> >();

Of course, once I require this, the benefits of "any" over "variant" are
gone.

Option2 is to provide another override in the internal handler classes:

struct placeholder
{
  virtual void register(ArchiveType & ar) = 0;
  ...
};

With this done you can use a static initializer to register each
concrete holder only once. However, you cannot use any old archive with
this method of course (since you can't have virtual templates) but do
have the option at least of selecting one if you make the whole thing a
template:

template < typename ArchiveType >
struct my_serializable_any
{
...
};

So there it is. The BOOST_CLASS_EXPORT method won't work either.
You're either going to have to register everything ahead of time (with
register<T>() or BOOST_CLASS_EXPORT) or you'll have to limit your
archive options.

-- 
http://crazyeddiecpp.blogspot.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net