Boost logo

Boost Users :

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


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

Using those sources you should be well on your way to implementing what
you need.

What you want to do is certainly possible. I've done it myself:

template < typename T >
T get(key) { return boost::any_cast<T>(get_any_from_key(key)); }

boost::any is probably one of the best libraries to look at for starting
to learn techniques behind the various boost libraries.

Have fun.

-- 
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