« Return to Thread: upcoming Switch library review Jan 5th - Jan 9th

Re: upcoming Switch library review Jan 5th - Jan 9th

by Larry Evans-3 :: Rate this Message:

Reply to Author | View in Thread

On 01/02/08 15:08, Stjepan Rajko wrote:
> The review of the Switch library by Steven Watanabe will begin this
> Saturday, Jan 5th.
>
> Description:
>
> The built in C/C++ switch statement is very efficient. Unfortunately,
> unlike a chained if/else construct there is no easy way to use it when
> the number of cases depends on a template parameter. The Switch

The attachment contains an alternative which uses no preprocessing.
It uses more memory because of the static fun_vec in:

   fun_switch_impl::our_vec

However, it would probably use less code because of no preprocessor
generated switch statements.  OTOH, it would be slower because
the function has to be looked up in the vector.

Are there any other comparisons you can think of.  It would be
useful to outline the pro's and cons of alternative implementations
you've considered.

I've done a simple test of the attached by using it in a slightly
modified test_switch.cpp.  I just added the #include and:

     BOOST_CHECK_EQUAL((fun_switch<test_range>(5, f())), 6);

-regards,
Larry

//alternative to switch_
#ifndef FUN_SWITCH_HPP
#define FUN_SWITCH_HPP
#include <boost/mpl/size.hpp>
#include <boost/mpl/for_each.hpp>
template<class Cases, class Int, class F>
struct fun_switch_impl
{
        typedef
        typename
      F::result_type
    result_type
    ;
    template<class Case>
    struct fun_case
    {
            static
          result_type
        _(F f)
        {
            Case arg;
            return f(arg);
        }
    };
    struct fun_vec
    {
            typedef
          result_type
          (*
        fun_type
          )(F)
        ;
            static
          unsigned const
        vec_size
        = boost::mpl::size<Cases>::type::value
        ;
            typedef
          fun_type
        vec_type
          [ vec_size
          ]
        ;
        struct fill_vec
        {
              vec_type&
            my_vec
            ;
            fill_vec(vec_type& a_vec)
            : my_vec(a_vec)
            {}
              template
              < typename Case
              >
              void
            operator()(Case)
            {
                my_vec[Case::value]=fun_case<Case>::_;
            }
        };
          vec_type
        my_vec
        ;
        fun_vec(void)
        {
            boost::mpl::for_each<Cases>(fill_vec(my_vec));
        }
          fun_type
        operator[](Int i)const
        {
            return my_vec[i];
        }
         
    };
        static
      fun_vec const&
    our_vec(void)
    {
        static fun_vec const a_vec;
        return a_vec;
    }
        static
      result_type
    _(Int i, F f)
    {
        return our_vec()[i](f);
    }
};
template<class Cases, class Int, class F>
  typename F::result_type
fun_switch(Int i, F f)
{
    return fun_switch_impl<Cases,Int,F>::_(i,f);
}

#endif


_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

 « Return to Thread: upcoming Switch library review Jan 5th - Jan 9th