Boost logo

Boost :

From: Johan Nilsson (johan.nilsson_at_[hidden])
Date: 2004-11-23 08:16:06


"Robert Ramey" <ramey_at_[hidden]> wrote in message
news:cnt6jr$m32$1_at_sea.gmane.org...
> Basically, this looks OK to me as well. I would like to see it made more
> automatic and more inline with what other serializations uses. On one
> level, this is merely a cosmetic issue. On the other level, its about
> leverage of user interface patterns. Ideally, I would like to see usage
> something as simple as:
>
> Struct X {
> variant<int, char, unsigned long> x
> template<class Archive, class T>
> void serialize(Archive &ar, T& t, unsigned int version){
> ar & x;
> }
> };
>
> A couple of questions though
>
> a) would it not be interesting for variant to return an integer
> corresponding to is current type?
> unsigned variant::get_type_index();

What about "int variant::which()" ?

>
> b) and the reverse
> variant::set_type_index(unsigned int ti); // of course this destroys any
> variable stored therein
>
> I would hope this might be implementable. My experience with mpl suggests
> that it might be. In this case the serialization would be a dream come
> true.
>
> so we could something like:
> template<class Archive, class T>
> void save(Archive &ar, const T & v){
> usigned int ti = x.get_type_index();
> ar << ti
> apply_visitor( bind( variant_saver(), ref(ar), _1 ), v );
> }
>
>
> template<class Archive, class T>
> void load(Archive &ar, T & v, unsigned int version){
> unsigned int ti;
> ar >> ti;
> v.set_type_index(ti);
> apply_visitor( bind( variant_loader(), ref(ar), _1 ), v );
> }

The above would be nice indeed. I've also been deserializing variants and
wondered if there was any another way to do it - this would definitely make
things easier.

// Johan


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk