[Boost-users] [function/bind] function object type introspection

41 views
Skip to first unread message

Gennadiy Rozental

unread,
Oct 1, 2009, 5:02:45 AM10/1/09
to boost...@lists.boost.org
Hi,

I've got couple questions. Might be someone can point me in right direction.

Let's say I've got this class ConcreteFunctor which should handle all
kinds of functions. I want to store the function as boost::function
object, thus the class has template parameter - FunctionType:

template<typename FuncType>
ConcreteFunctor : public AbstractFunctor {
ConcreteFunctor( FuncType f ) ... {}

virtual invoke( std::vector<Object*> const& args )
{
// here I can use FuncType::arity and FuncType's argument types
// to properly convert arguments to desired types and invoke m_f
}

FuncType m_f;
}

Now I need to implement object maker

template<typename F>
boost::shared_ptr<AnstractFunctor>
make_functor( F f )
{
typedef boost::function<???> TypeName;
return boost::shared_ptr<AbstrctFunctor>(
new ConcreteFunctor<FuncType>( f ) );
}

I was able to work fine with free functions by just removing pointer
from F. Now to the questions:

1. How do I deduce boost::function type from pointer to member functions:

Essentially if F is R (A::*)(T1,T2) I need to get
boost::function<T (A*,T1,T2)>

2. This is more difficult: if f is bound object, how to I intrspect it's
type:

Essentially in use case like this:

int foo( std::string, int ) {...}

make_functor( boost::bind( &foo, "abc" ) )

I want to get FuncType as boost::function<int (int)>

Regards,

Gennadiy

_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Ovanes Markarian

unread,
Oct 1, 2009, 6:00:23 AM10/1/09
to boost...@lists.boost.org
Hi!

please see my partial suggestion below...

On Thu, Oct 1, 2009 at 11:02 AM, Gennadiy Rozental <rog...@gmail.com> wrote:
Hi,

....

Now I need to implement object maker

template<typename F>
boost::shared_ptr<AnstractFunctor>
make_functor( F f )
{
 typedef boost::function<???> TypeName;
 return boost::shared_ptr<AbstrctFunctor>(
    new ConcreteFunctor<FuncType>( f ) );
}

I was able to work fine with free functions by just removing pointer from F. Now to the questions:

1. How do I deduce boost::function type from pointer to member functions:

Essentially if F is R (A::*)(T1,T2) I need to get
boost::function<T (A*,T1,T2)>

...

 Is may be Boost.FunctionTypes what you are looking for?

Regards,
Ovanes

Gennadiy Rozental

unread,
Oct 1, 2009, 6:13:00 AM10/1/09
to boost...@lists.boost.org
Ovanes Markarian wrote:
> Essentially if F is R (A::*)(T1,T2) I need to get
> boost::function<T (A*,T1,T2)>
>
>

It looks like right area, but i can't see how would I apply these.

function_type expects mpl sequense.

Ovanes Markarian

unread,
Oct 1, 2009, 8:10:16 AM10/1/09
to boost...@lists.boost.org

function_type expects mpl sequense.

As far as I understand it, you need to apply result_type to your F and parameter_types to F. From there you can model your function. Or am I wrong?

template<class F>
some_return_type make_function(F f)
{
  typedef typename result_type<F>::type result_type;
  typedef typename parameter_types<F>::type mpl_seq_params;

  //insert the result type to the mpl_seq_params => fct_sig;

  ...
}

But I am not sure if it works, this is my assumption only.


Regards,
Ovanes

Ovanes Markarian

unread,
Oct 1, 2009, 8:19:37 AM10/1/09
to boost...@lists.boost.org
BTW, you can use enable if and is_function_pointer to allow passing function pointers only. In case with the function objects you need to implement other inspection strategies.

AlfC

unread,
Oct 2, 2009, 12:55:32 PM10/2/09
to boost...@lists.boost.org
> >  Is may be Boost.FunctionTypes what you are looking for?
> >http://www.boost.org/doc/libs/1_40_0/libs/function_types/doc/html/boo...

>
> It looks like right area, but i can't see how would I apply these.
>
> function_type expects mpl sequense.

Maybe what you need is a combinatioin of FunctionTypes and TypeTraits

//example with type_traits/function_traits
#include<boost/static_assert.hpp>
#include<boost/type_traits/function_traits.hpp>
struct newf : boost::function_traits<int(int,int)>{
BOOST_STATIC_ASSERT(arity==2);
result_type operator()(arg1_type a, arg2_type b){ //or
result_type operator()(first_argument_type a, second_argument_type b){
return a+b;
}
};

Reply all
Reply to author
Forward
0 new messages