0

Is it possible to access the arguments contained in a boost::function type?

I'd like to be able to retrieve the address of the function to be called, and the values of the arguments provided for that function.

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
  • I think you might be confused about "the values of the arguments." It doesn't store values of the arguments. When you call `function`'s `operator()`, it simply forwards the call to the stored function, and it passes along all the arguments you provided. It doesn't store any copies for itself. You might query about the *types* of the arguments, but not the values. Or are you talking about a function you've bound with the likes of `boost::bind`? In that case `boost::function` is wrapping some *other* functor object, which in turn holds values for one or more bound arguments. – Rob Kennedy Sep 08 '09 at 18:37

1 Answers1

2

boost::function erases the implementation type, but if you know it, you can cast to it; since boost::function are comparable by value (== !=) the information is clearly available.

It looks like (from the function_base superclass of functionN) you can get the implementation object with:

f.target<concrete_functor_type>()

Which will return NULL if you provided the wrong concrete type.

Also in function_base (probably not helpful beyond the target method above):

public: // should be protected, but GCC 2.95.3 will fail to allow access
  detail::function::vtable_base* vtable;
  mutable detail::function::function_buffer functor;

vtable gives you access to:

      struct vtable_base
      {
        void (*manager)(const function_buffer& in_buffer, 
                        function_buffer& out_buffer, 
                        functor_manager_operation_type op);
      };

which can get you the typeid of the functor:

  case get_functor_type_tag:
    out_buffer.type.type = &typeid(F);
    out_buffer.type.const_qualified = in_buffer.obj_ref.is_const_qualified;
    out_buffer.type.volatile_qualified = in_buffer.obj_ref.is_volatile_qualified;
    return;
  }

function_buffer (functor) is only useful for refs to function objects, bound (this is fixed) member functions ptrs, and free functions, where you haven't bound any arguments

Jonathan Graehl
  • 9,182
  • 36
  • 40