Boost logo

Boost Users :

From: Agustín K-ballo Bergé (kaballo86_at_[hidden])
Date: 2008-05-02 01:45:44


Your first thoughts on a solution using a mpl::map are valid. All you
need to do is create a free function pointer template to wrap the calls.
The following code shows how:

#include <iostream>

#include <boost/mpl/map.hpp>
#include <boost/mpl/at.hpp>

void SetParamFloat( char* name, float value )
{
        std::cout << "SetParamFloat called: " << name << ", " << value <<
std::endl;
}

void SetParamInt( char* name, int value )
{
        std::cout << "SetParamInt called: " << name << ", " << value << std::endl;
}

void SetParamString( char* name, char* value )
{
        std::cout << "SetParamString called: " << name << ", " << value <<
std::endl;
}

template< typename T, void ( *ptr_to_function )( char*, T ) > class
free_function_ptr
{
public:
        static void call( char* name, T value )
        {
                ptr_to_function( name, value );
        }
};

typedef boost::mpl::map<
        boost::mpl::pair< float, free_function_ptr< float, SetParamFloat > >,
        boost::mpl::pair< int, free_function_ptr< int, SetParamInt > >,
        boost::mpl::pair< char*, free_function_ptr< char*, SetParamString > >
>::type functions_map;

template < typename T > void set_param( char* name, T value )
{
        typedef boost::mpl::at< functions_map, T >::type function_;

        function_::call( name, value );
}

int main()
{
        set_param( "float", 1.3f );

        set_param( "int", 1 );

        char *text = "property value";
        set_param( "char*", text );

        return 0;
}

Running the code will output:
     SetParamFloat called: float, 1.3
     SetParamInt called: int, 1
     SetParamString called: char*, property value

I have compiled the code in MS VC++ 9 and the machine code generated is
the same as calling the actual functions directly (actually, the
provided test functions are inlined).

The shortcome of this solutions is that special care should be taken
when specifying the value. For instance, set_param( "float", 1.3 );
won't work since the value provided is a double, and there is no entry
for double at the function map. A quick (and tedious) solution would be
to add more entries to the map. Anyone can think of a better solution?

Agustín K-ballo Bergé.-

Robert Dailey escribió:
> On Thu, May 1, 2008 at 7:36 PM, Steven Watanabe <watanabesj_at_[hidden]
> <mailto:watanabesj_at_[hidden]>> wrote:
>
> AMDG
>
> Robert Dailey wrote:
> > Hey guys,
> >
> > I was wondering if there was some sort of MPL Switch concept in the
> > boost library. Before I go any further on that topic, let me first
> > explain what I'm trying to do.
> >
> > I'm currently using a library that has several functions as follows:
> >
> > void SetParamFloat( char* name, float value );
> > void SetParamInt( char* name, int value );
> > void SetParamString( char* name, char* value );
> >
> > I'm creating a sort of facade around this library, and as a result I
> > have a function that looks as follows:
> >
> > template< typename t_type >
> > void SetParam( char* name, t_type value );
> >
> > <snip>
> >
> > Any help is greatly appreciated. I guess what I really need is some
> > guidance on the matter. I strive for a generic way to implement this,
> > but I don't want it to look messy, as most template code has the bad
> > habit of turning out that way.
>
> I think that the easiest way to get what you want is just to overload
> SetParam for each type
> that you need to deal with.
>
>
> I thought about that too, but I am always curious as to what other
> solutions boost could bring to the table. After this much research I'm
> starting to find that overloads seem to be the cleanest solution. Thanks
> for helping Steven.
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users


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