Boost logo

Boost Users :

Subject: Re: [Boost-users] [MPL] searching a vector at runtime
From: strasser_at_[hidden]
Date: 2010-05-16 03:07:53


Zitat von Alexander Lamaison <awl03_at_[hidden]>:

> I'm trying to recursively traverse an MPL vector_c but I'm not sure what
> the base-case type should be.
>

> template<typename Sequence> // MAP is a vector_c
> struct dispatcher
> {
> template<typename T>
> static result dispatch(T* obj, unsigned int message_id, int arg)
> {
> typedef boost::mpl::front<Sequence>::type head;
> typedef boost::mpl::pop_front<Sequence>::type tail;
>
> if (head::value == message_id)
> return obj->handle(message<head::value>, arg);
> else
> return dispatcher<tail>::dispatch(obj, message_id, arg);
> }
> };
>
> template<>
> struct dispatcher<boost::mpl::void_> // WHAT SHOULD THIS TYPE BE?
> {
> template<typename T>
> static result dispatch(T* obj, unsigned int message_id, int arg)
> {
> return obj->default_message_handler(message_id, arg);
> }
> };
>
> I'm stuck on what to make the dispatcher base case which is reached when
> pop_front is called on a list with one item. At first I though the
> resulting type was void_ but apparently this doesn't match.
>
> I'm new to MPL. Can someone explain what I'm doing wrong here or if
> there's an altogether better way?

if you encounter this type of thing regularily, you might want to have
a look at Boost.Fusion, which has runtime algorithms that can also be
used on Boost.MPL sequences.
however, using function templates is much simpler than the dispatch
class specialization you attempted above:

template<class It,class End>
void dispatch(int id,mpl::true_ endofvec){}

template<class It,class End>
void dispatch(int id,mpl::false_){
        if(id == mpl::deref<It>::type::value){
                ...
        }else{
                typedef typename mpl::next<It>::type Next;
                dispatch<Next,End>(id,typename is_same<Next,End>::type());
        }
}

call with mpl::begin<YourVec> and mpl::end<YourVec> as template arguments


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