Access boost::function arguments

不问归期 提交于 2020-01-06 09:33:46

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/1395312/access-boostfunction-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!