1 #ifndef _SIGC_FUNCTORS_PTR_FUN_H_
2 #define _SIGC_FUNCTORS_PTR_FUN_H_
3 #include <sigc++/type_traits.h>
7 /** @defgroup ptr_fun ptr_fun()
8 * ptr_fun() is used to convert a pointer to a function to a functor.
9 * If the function pointer is to an overloaded type, you must specify
10 * the types using template arguments starting with the first argument.
11 * It is not necessary to supply the return type.
16 * sigc::slot<void(int)> sl = sigc::ptr_fun(&foo);
21 * void foo(int) {} // choose this one
23 * void foo(int, int) {}
24 * sigc::slot<void(long)> sl = sigc::ptr_fun<void, int>(&foo);
27 * ptr_fun() can also be used to convert a pointer to a static member
28 * function to a functor, like so:
34 * static void bar(int) {}
36 * sigc::slot<void(int)> sl = sigc::ptr_fun(&foo::bar);
39 * @ingroup sigcfunctors
42 /** pointer_functor wraps existing non-member functions with, or without, arguments.
43 * Use the convenience function ptr_fun() to create an instance of pointer_functor.
45 * The following template arguments are used:
46 * - @e T_args... Argument types used in the definition of operator()().
47 * - @e T_return The return type of operator()().
51 template <class T_return, class... T_args>
52 class pointer_functor;
54 template <class T_return, class... T_args>
55 class pointer_functor<T_return(T_args...)>
58 using function_type = T_return (*)(T_args...);
60 function_type func_ptr_;
62 using result_type = T_return;
64 /// Constructs an invalid functor.
67 /** Constructs a pointer_functor2 object that wraps an existing function.
68 * @param _A_func Pointer to function that will be invoked from operator()().
70 explicit pointer_functor(function_type _A_func): func_ptr_(_A_func) {}
72 /** Execute the wrapped function.
73 * @param _A_a1 Argument to be passed on to the function.
74 * @param _A_a2 Argument to be passed on to the function.
75 * @return The return value of the function invocation.
77 T_return operator()(type_trait_take_t<T_args>... _A_a) const
78 { return func_ptr_(_A_a...); }
81 /** Creates a functor of type sigc::pointer_functor which wraps an existing non-member function.
82 * @param _A_func Pointer to function that should be wrapped.
83 * @return Functor that executes @e _A_func on invokation.
87 template <class T_return, class... T_args>
89 ptr_fun(T_return (*_A_func)(T_args...))
90 { return pointer_functor<T_return(T_args...)>(_A_func); }
92 } /* namespace sigc */
93 #endif /* _SIGC_FUNCTORS_PTR_FUN_H_ */