Boost logo

Boost :

From: Joel de Guzman (joel_at_[hidden])
Date: 2007-10-04 07:52:44


Phil Endecott wrote:
> Simonson, Lucanus J wrote:

> * xyz or [n] notation: using .x, .y and .z to access the coordinates
> seems simpler to me, but the higher-dimension people would prefer to
> use [0], [1] ... [n]. That notation also has the advantage that you
> can iterate through the dimensions. Is it possible to support both?

Yes! Use fusion to do the mapping, making all your structs, arrays or
whatnots, whatever they are, compatible. You deal with them in
a uniform manner. You don't care if the point type (for example)
is from library A or library B. It just works. Here's an example:

     namespace A // library A
     {
         struct point
         {
             int x;
             int y;
         };
     }

     namespace B // library B
     {
         struct Point
         {
             float y; // y comes first!
             float x;
         };
     }

     namespace C // library B
     {
         typedef boost::array<int, 2> Point;
     }

Now, let's adapt these structs to make them tuples (boost::array
is already adapted without doing anything else):

     BOOST_FUSION_ADAPT_STRUCT(
         A::point,
         (int, x)
         (int, y)
     )

     BOOST_FUSION_ADAPT_STRUCT(
         B::point,
         (float, x)
         (float, y)
     )

Now, in your generic code, you simply refer to X and Y as:

    get<1>(p); // x
    get<2>(p); // y

regardless if p is a boost::array, a boost::tuple, a B::Point,
or an A::point. They are all compatible. And, the bonus is that
because they are now first class fusion sequences (A::point and
B::point), all fusion iterators and algorithms can be used.
Yes, you can iterate through all the dimensions!

Regards,

-- 
Joel de Guzman
http://www.boost-consulting.com
http://spirit.sf.net

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