16 template <
typename _FnSig>
class callback;
28 template <
typename _ReturnT,
typename ... _Args>
class callback < _ReturnT(_Args...) >{
31 using ptr = std::unique_ptr<invoker>;
32 using vector = std::vector<ptr>;
33 virtual ~invoker() =
default;
34 virtual _ReturnT invoke(_Args...)
const = 0;
37 template <
typename _MethodT, _MethodT * _method>
class method_invoker :
public invoker{
39 ~method_invoker()
override =
default;
40 virtual _ReturnT invoke(_Args...oArgs)
const override {
return (*_method)(std::forward<_Args>(oArgs)...); }
43 template <
typename _LambdaT>
class lamdba_invoker :
public invoker{
45 ~lamdba_invoker()
override =
default;
46 virtual _ReturnT invoke(_Args...oArgs)
const override {
return _Lambda(std::forward<_Args>(oArgs)...); }
47 explicit lamdba_invoker(_LambdaT oLambda) : _Lambda(oLambda){}
48 lamdba_invoker(lamdba_invoker&& src) : _Lambda(std::move(src._Lambda)){}
49 lamdba_invoker(
const lamdba_invoker& src) : _Lambda(src._Lambda){}
50 lamdba_invoker& operator=(
const lamdba_invoker& src){
52 _Lambda = src._Lambda;
56 lamdba_invoker& operator=(lamdba_invoker&&) =
delete;
60 template <
typename _DestT, _ReturnT(_DestT::*_member)(_Args...)>
class member_invoker :
public invoker{
62 ~member_invoker()
override =
default;
63 member_invoker() =
delete;
64 member_invoker& operator=(
const member_invoker&) =
delete;
65 member_invoker(member_invoker&& oSrc) : _dest(oSrc._dest){}
66 member_invoker(
const member_invoker& oSrc) : _dest(oSrc._dest){}
67 explicit member_invoker(_DestT* dest) : _dest(dest){}
68 virtual _ReturnT invoke(_Args...oArgs)
const override {
return (_dest->*_member)(oArgs...); }
72 typename invoker::vector _Invokers;
82 callback(callback&& src) : _Invokers(std::move(src._Invokers)){}
83 callback(
const callback&) =
delete;
85 ~callback() =
default;
86 callback& operator=(
const callback&) =
delete;
90 typename invoker::vector::size_type i = 0;
91 for (
const auto & oInvoker : _Invokers){
92 if ((result_policy::first == result && 0 == i) || (result_policy::last == result && (_Invokers.size() - 1) == i)){
93 oRet = oInvoker->invoke(std::forward<_Args>(oArgs)...);
95 oInvoker->invoke(oArgs...);
102 _ReturnT operator()(_Args...oArgs)
const{
104 for (
const auto & oInvoker : _Invokers){
105 oRet = oInvoker->invoke(oArgs...);
111 template <
typename _ClassT, _ReturnT(_ClassT::*_MemberT)(_Args...)>
112 void connect(_ClassT *pClass){ _Invokers.emplace_back(
new member_invoker<_ClassT, _MemberT>(pClass)); }
115 template <
typename method>
116 void connect(method oMethod){ _Invokers.emplace_back(
new lamdba_invoker< method >(oMethod)); }
119 template <_ReturnT(*method)(_Args...)>
120 void connect(){ _Invokers.emplace_back(
new method_invoker<_ReturnT(_Args...), method>()); }
124 template <
typename _Ty>
125 callback& operator += (_Ty&& addend){ connect(std::forward<_Ty>(addend));
return *
this; }
131 template <
typename ... _Args>
class callback < void(_Args...) >{
133 using _ReturnT = void;
137 using ptr = std::unique_ptr<invoker>;
138 using vector = std::vector<ptr>;
139 virtual ~invoker() =
default;
140 virtual _ReturnT invoke(_Args...)
const = 0;
143 template <
typename _MethodT, _MethodT * _method>
class method_invoker :
public invoker{
145 ~method_invoker()
override =
default;
146 virtual _ReturnT invoke(_Args...oArgs)
const override { (*_method)(std::forward<_Args>(oArgs)...); }
149 template <
typename _LambdaT>
class lamdba_invoker :
public invoker{
151 ~lamdba_invoker()
override =
default;
152 virtual _ReturnT invoke(_Args...oArgs)
const override { _Lambda(std::forward<_Args>(oArgs)...); }
153 explicit lamdba_invoker(_LambdaT oLambda) : _Lambda(oLambda){}
154 lamdba_invoker(lamdba_invoker&& src) : _Lambda(std::move(src._Lambda)){}
155 lamdba_invoker(
const lamdba_invoker& src) : _Lambda(src._Lambda){}
156 lamdba_invoker& operator=(
const lamdba_invoker& src){
158 _Lambda = src._Lambda;
162 lamdba_invoker& operator=(lamdba_invoker&&) =
delete;
166 template <
typename _DestT, _ReturnT(_DestT::*_member)(_Args...)>
class member_invoker :
public invoker{
168 ~member_invoker()
override =
default;
169 member_invoker() =
delete;
170 member_invoker& operator=(
const member_invoker&) =
delete;
171 member_invoker(member_invoker&& oSrc) : _dest(oSrc._dest){}
172 member_invoker(
const member_invoker& oSrc) : _dest(oSrc._dest){}
173 explicit member_invoker(_DestT* dest) : _dest(dest){}
174 virtual _ReturnT invoke(_Args...oArgs)
const override { (_dest->*_member)(oArgs...); }
178 typename invoker::vector _Invokers;
181 ~callback() =
default;
182 callback() =
default;
183 callback(
const callback&) =
delete;
184 callback(callback&& src) : _Invokers(std::move(src._Invokers)){}
185 callback& operator=(
const callback&) =
delete;
188 void operator()(_Args...oArgs)
const{
189 for (
auto & oInvoker : _Invokers){
190 oInvoker->invoke(oArgs...);
194 template <
typename _ClassT, _ReturnT(_ClassT::*_MemberT)(_Args...)>
195 void connect(_ClassT *pClass){ _Invokers.emplace_back(
new member_invoker<_ClassT, _MemberT>(pClass)); }
197 template <
typename method>
198 void connect(method oMethod){ _Invokers.emplace_back(
new lamdba_invoker< method >(oMethod)); }
200 template <_ReturnT(*method)(_Args...)>
201 void connect(){ _Invokers.emplace_back(
new method_invoker<
void(_Args...), method>()); }
203 template <
typename _Ty>
204 callback& operator += (_Ty&& addend){ connect(std::forward<_Ty>(addend));
return *
this; }
result_policy
behavior of invocation when multiple receivers are attached
return the result of the first attached target
void connect(method oMethod)
connect a lambda
host, target and build configurations and settings Various components are purpose built for specific ...
void connect()
connect a static method
_ReturnT operator()(result_policy result, _Args...oArgs) const
Invokes all the attached targets and returns the result of the target as specified by the result poli...
void connect(_ClassT *pClass)
connect a class instance and member function