Imported Upstream version 2.99.2
[platform/upstream/libsigc++.git] / sigc++ / functors / ptr_fun.h
1 #ifndef _SIGC_FUNCTORS_PTR_FUN_H_
2 #define _SIGC_FUNCTORS_PTR_FUN_H_
3 #include <sigc++/type_traits.h>
4
5 namespace sigc {
6
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.
12  *
13  * @par Example:
14  * @code
15  * void foo(int) {}
16  * sigc::slot<void(int)> sl = sigc::ptr_fun(&foo);
17  * @endcode
18  *
19  * @par Example:
20  * @code
21  * void foo(int) {}  // choose this one
22  * void foo(float) {}
23  * void foo(int, int) {}
24  * sigc::slot<void(long)> sl = sigc::ptr_fun<void, int>(&foo);
25  * @endcode
26  *
27  * ptr_fun() can also be used to convert a pointer to a static member
28  * function to a functor, like so:
29  *
30  * @par Example:
31  * @code
32  * struct foo
33  * {
34  *   static void bar(int) {}
35  * };
36  * sigc::slot<void(int)> sl = sigc::ptr_fun(&foo::bar);
37  * @endcode
38  *
39  * @ingroup sigcfunctors
40  */
41
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.
44  *
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()().
48  *
49  * @ingroup ptr_fun
50  */
51 template <class T_return, class... T_args>
52 class pointer_functor;
53
54 template <class T_return, class... T_args>
55 class pointer_functor<T_return(T_args...)>
56   : public functor_base
57 {
58   using function_type = T_return (*)(T_args...);
59 protected: 
60   function_type func_ptr_;
61 public:
62   using result_type = T_return;
63
64   /// Constructs an invalid functor.
65   pointer_functor() {}
66
67   /** Constructs a pointer_functor2 object that wraps an existing function.
68    * @param _A_func Pointer to function that will be invoked from operator()().
69    */
70   explicit pointer_functor(function_type _A_func): func_ptr_(_A_func) {}
71
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.
76    */
77   T_return operator()(type_trait_take_t<T_args>... _A_a) const 
78     { return func_ptr_(_A_a...); }
79 };
80
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.
84  *
85  * @ingroup ptr_fun
86  */
87 template <class T_return, class... T_args>
88 inline decltype(auto)
89 ptr_fun(T_return (*_A_func)(T_args...))
90 { return pointer_functor<T_return(T_args...)>(_A_func); }
91
92 } /* namespace sigc */
93 #endif /* _SIGC_FUNCTORS_PTR_FUN_H_ */