Imported Upstream version 2.99.2
[platform/upstream/libsigc++.git] / sigc++ / adaptors / retype.h
1 #ifndef _SIGC_ADAPTORS_RETYPE_H_
2 #define _SIGC_ADAPTORS_RETYPE_H_
3
4 #include <sigc++/adaptors/adaptor_trait.h>
5 #include <sigc++/functors/ptr_fun.h>
6 #include <sigc++/functors/mem_fun.h>
7 #include <sigc++/functors/slot.h>
8
9 namespace sigc {
10
11 /** @defgroup retype retype(), retype_return()
12  * sigc::retype() alters a sigc::pointer_functor, a sigc::mem_functor or a sigc::slot
13  * in that it makes C-style casts to the functor's parameter types
14  * of all parameters passed through operator()().
15  *
16  * Use this adaptor for inline conversion between numeric or other simple types.
17  * @par Example:
18  * @code
19  * void foo(int);
20  * sigc::retype(sigc::ptr_fun(&foo))(5.7F); // calls foo(5)
21  * @endcode
22  *
23  * The functor that sigc::retype() returns can be passed directly into
24  * sigc::signal::connect().
25  *
26  * @par Example:
27  * @code
28  * sigc::signal<void(float)> some_signal;
29  * void foo(int);
30  * some_signal.connect(sigc::retype(sigc::ptr_fun(&foo)));
31  * @endcode
32  *
33  * This adaptor builds an exception in that it only works on sig::pointer_functor,
34  * sigc::mem_functor and sigc::slot because it needs sophisticated information about
35  * the parameter types that cannot be deduced from arbitrary functor types.
36  *
37  * sigc::retype_return() alters the return type of an arbitrary functor.
38  * Like in sigc::retype() a C-style cast is performed. Usage sigc::retype_return() is
39  * not restricted to libsigc++ functor types but you need to
40  * specify the new return type as a template parameter.
41  *
42  * @par Example:
43  * @code
44  * float foo();
45  * std::cout << sigc::retype_return<int>(&foo)(); // converts foo's return value to an integer
46  * @endcode
47  *
48  * @ingroup adaptors
49  */
50
51 /** Adaptor that performs C-style casts on the parameters passed on to the functor.
52  * Use the convenience function sigc::retype() to create an instance of retype_functor.
53  *
54  * The following template arguments are used:
55  * - @e T_functor Type of the functor to wrap.
56  * - @e T_type Types of @e T_functor's arguments.
57  *
58  * @ingroup retype
59  */
60 template <class T_functor, class... T_type>
61 struct retype_functor
62   : public adapts<T_functor>
63 {
64   using adaptor_type = typename adapts<T_functor>::adaptor_type;
65   using result_type = typename adapts<T_functor>::result_type;
66
67   template <class... T_arg>
68   decltype(auto)
69   operator()(T_arg... _A_a)
70     { return this->functor_.template operator()<type_trait_take_t<T_type>...>
71         (static_cast<T_type>(_A_a)...);
72     }
73
74   /** Constructs a retype_functor object that performs C-style casts on the parameters passed on to the functor.
75    * @param _A_functor Functor to invoke from operator()().
76    */
77   explicit retype_functor(type_trait_take_t<T_functor> _A_functor)
78     : adapts<T_functor>(_A_functor)
79     {}
80 };
81
82 #ifndef DOXYGEN_SHOULD_SKIP_THIS
83 //template specialization of visitor<>::do_visit_each<>(action, functor):
84 /** Performs a functor on each of the targets of a functor.
85  * The function overload for sigc::retype_functor performs a functor on the
86  * functor stored in the sigc::retype_functor object.
87  *
88  * @ingroup retype
89  */
90 template <class T_functor, class... T_type>
91 struct visitor<retype_functor<T_functor, T_type...> >
92 {
93   template <typename T_action>
94   static void do_visit_each(const T_action& _A_action,
95                             const retype_functor<T_functor, T_type...>& _A_target)
96   {
97     sigc::visit_each(_A_action, _A_target.functor_);
98   }
99 };
100 #endif // DOXYGEN_SHOULD_SKIP_THIS
101
102 //This one takes, for instance, a mem_functor or bound_mem_functor:
103 /** Creates an adaptor of type sigc::retype_functor which performs C-style casts on the parameters passed on to the functor.
104  *
105  * @param _A_functor Functor that should be wrapped.
106  * @return Adaptor that executes @e _A_functor performing C-style casts on the paramters passed on.
107  *
108  * @ingroup retype
109  */
110 template <template<class T_func, class... T_arg> class T_functor, class T_func, class... T_arg>
111 inline decltype(auto)
112 retype(const T_functor<T_func, T_arg...>& _A_functor)
113 { return retype_functor<T_functor<T_func, T_arg...>, T_arg...>
114     (_A_functor); }
115
116 //This one takes, for instance, a pointer_functor or slot:
117 /** Creates an adaptor of type sigc::retype_functor which performs C-style casts on the parameters passed on to the functor.
118  *
119  * @param _A_functor Functor that should be wrapped.
120  * @return Adaptor that executes @e _A_functor performing C-style casts on the paramters passed on.
121  *
122  * @ingroup retype
123  */
124 template <template<class T_return, class... T_arg> class T_functor, class T_return, class... T_arg>
125 inline decltype(auto)
126 retype(const T_functor<T_return(T_arg...)>& _A_functor)
127 { return retype_functor<T_functor<T_return(T_arg...)>, T_arg...>
128     (_A_functor); }
129
130 } /* namespace sigc */
131
132 #endif /* _SIGC_ADAPTORS_RETYPE_H_ */