Boost logo

Boost :

From: Jonathan Turkanis (technews_at_[hidden])
Date: 2004-09-30 14:39:23


"John Torjo" wrote:

> Vladimir Prus wrote:
>

> > 2. A mechanism to iterate over elements of composite type. After some
though,
> > I believe that the mechanism should be boost::serializaton.
>
> This sounds nice, but I don't know how achievable it is.
> Basically, when writing a composite type, you might want to write things
> in-between. Like, for a pair, you might want to write:
> [first, second]
> or
> <<first | second>>
> (where first and second are the members of the pair)
>
> Same goes for each composite type.

Here's the kind of approach I was pursuing to allow composite types to represent
their structure. It's a bit bulky, but quite general.

     template<typename T>
     struct is_composite {
          static const bool value = false;
     };

For a user-defined Dog class:

    template<>
    struct is_composite<Dog>
        : composite_type<
              Dog,
              element_by_functor<NameTag, Dog, std::string, DogNameGetter>,
              element_by_field<WeightTag, Dog, float, &Dog::weight_>,
              ...
>
        { };

( Macros could make this easier:

   BEGIN_COMPOSITE_TYPE(Dog)
   ADD_FUNCTOR_ELEMENT(std::string, DogNameGetter);
   ...
   END_COMPOSITE_TYPE(Dog); )

This information could be extracted by a Dog-formatter as follows:

       out << "[Dog: " << get_element<NameTag>(d) << "]";

Or if we assume the order of fields is known:

       out << "[Dog: " << get<0>(d) << "]";

Jonathan


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