Boost logo

Boost :

From: Aleksey Gurtovoy (agurtovoy_at_[hidden])
Date: 2003-11-05 05:37:29


Joel de Guzman wrote:
> >> I think it is possible to work directly on mpl sequences.
> >
> > That would be really cool, although AFAICS for that to work reliably
> > we would need something like 'fusion::is_sequence'.
>
> Although I wish to have is_sequence<S>, it might not be necessary.
> Fusion uses MPL style tag mechanism. I can just use MPL's sequence tags
> to specialize fusion::begin(s) and fusion::end(s) inside fusion's
> namespace.

Well, yes, but I was thinking about user-defined MPL sequences which are
at the same time Fusion sequences. When passed to Fusion algorithms by
value, these probably have to be treated as Fusion sequences, for which we
would need to be able to detect one.

> In the same manner, I can also specialize mpl::begin<s> and mpl::end<t>
> for fusion sequences, thus giving us full round-trip interoperability.
Very
> cool, IMO :-)

It is!

>
> >> you don't need the identity, so you can further simplify:
> >>
> >> typedef mpl::vector<vehicle*,car*,bicycle*,train*> vec;
> >> vehicle* result = fusion::fold(vec(), static_cast<vehicle*>(0),
> > make_vehicle);
> >
> > IMO, it's beautiful. Or at least as beautiful as it can get within the
> > current
> > language. I would love to see things work this way.
>
> How about putting identity_view inside MPL instead? Example:
>
> namespace mpl
> {
> template <class s>
> struct identity_view : transform_view<s, mpl::identity<_> > {};
> };
>
> Or how about pointer_view:
>
> namespace mpl
> {
> template <class s>
> struct pointer_view : transform_view<s, add_pointer<_> > {};
> };
>
> Views are one honking great idea -- let's do more of those! :-)

Agreed, actually, at least in general. Convenience is everything :).

>
> Then we can write:
>
> typedef mpl::pointer_view<mpl::vector<vehicle, car, bicycle, train> >
vec;
> vehicle* result = fusion::fold(vec(), static_cast<vehicle*>(0),
make_vehicle);
>
> I'm not sure, however, if that's any better than:
>
> mpl::transform_view<
> mpl::vector<vehicle, car, bicycle, train>
> , add_pointer<_>
> > vec;
>
> vehicle* result = fusion::fold(vec, static_cast<vehicle*>(0),
make_vehicle);

It's probably a little better if you 'fold' something that comes from
outside:

    template< typename VehicleTypes >
    vehicle*
    make_vehicles()
    {
        return fusion::fold(
              mpl::pointer_view<VehicleTypes>()
            , static_cast<vehicle*>(0)
            , make_vehicle
            );
    }

>
> Or simply:
>
> mpl::vector<vehicle*,car*,bicycle*,train*> vec;
> vehicle* result = fusion::fold(vec, static_cast<vehicle*>(0),
make_vehicle);
>
> Jeez, it's all connected somehow! I can imagine an Phoenix/LL extension
> that will allow:
>
> mpl::identity_view<vehicle, car, bicycle, train> vec;
> vehicle* result = fusion::fold(
> vec
> , static_cast<vehicle*>(0)
> , new_<identity_of<mpl::_1> >(ll::_2)
> );

I thought about this run-time/compile-time connection a couple of months
ago, although mine use case was even more ambitious:

    template< typename Event >
    void is_allowed( boost::type<Event> = boost::type<Event>() )
    {
        typedef typename Derived::transition_table STT_;
        // 'STT_' is an MPL sequence of something like
        // struct transition {
        // typedef mpl::int_<...> state;
        // typedef ... from_event;
        // ...
        // };

        // iterate the states (#1), find the one that is equal to
        // 'this->m_state' (#2), if found, call a function object (#3)
        // that returns 'true' if 'STT_' segment from the found transition
        // to the end of the sequence (#4) contains event 'Event'

        // #5 - run-time argument goes here

        return hal::find_if( // hal == fusion
              mpl::transform_view<STT_,state<mpl::_1> >() // #1
            , std::bind1st( std::equal_to<long>(), this->m_state ) // #2
            , mpl::contains<
                  mpl::filter_view<
                      mpl::iterator_range<
                           mpl::base<mpl::_1()> // #5
                         , typename mpl::end<STT_>::type
> // #4
                    , from_event<mpl::_1>
>
                , Event
>() // #3
            );
    }

Hope it makes sense :)

> Exciting!

Very, if we can make it work :).

Aleksey


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