1 #ifndef _SIGC_ADAPTORS_RETYPE_H_
2 #define _SIGC_ADAPTORS_RETYPE_H_
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>
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()().
16 * Use this adaptor for inline conversion between numeric or other simple types.
20 * sigc::retype(sigc::ptr_fun(&foo))(5.7F); // calls foo(5)
23 * The functor that sigc::retype() returns can be passed directly into
24 * sigc::signal::connect().
28 * sigc::signal<void(float)> some_signal;
30 * some_signal.connect(sigc::retype(sigc::ptr_fun(&foo)));
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.
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.
45 * std::cout << sigc::retype_return<int>(&foo)(); // converts foo's return value to an integer
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.
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.
60 template <class T_functor, class... T_type>
62 : public adapts<T_functor>
64 using adaptor_type = typename adapts<T_functor>::adaptor_type;
65 using result_type = typename adapts<T_functor>::result_type;
67 template <class... T_arg>
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)...);
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()().
77 explicit retype_functor(type_trait_take_t<T_functor> _A_functor)
78 : adapts<T_functor>(_A_functor)
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.
90 template <class T_functor, class... T_type>
91 struct visitor<retype_functor<T_functor, T_type...> >
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)
97 sigc::visit_each(_A_action, _A_target.functor_);
100 #endif // DOXYGEN_SHOULD_SKIP_THIS
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.
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.
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...>
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.
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.
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...>
130 } /* namespace sigc */
132 #endif /* _SIGC_ADAPTORS_RETYPE_H_ */