16 #ifndef A_UTIL_UTIL_DELEGATE_IMPL_HEADER_INCLUDED
17 #define A_UTIL_UTIL_DELEGATE_IMPL_HEADER_INCLUDED
26 template <
typename ReturnType>
27 inline DelegateInterface<ReturnType>::~DelegateInterface()
31 template <
typename ReturnType>
32 inline FunctionDelegate<ReturnType>::FunctionDelegate(Function f) : _func(f)
36 template <
typename ReturnType>
37 inline ReturnType FunctionDelegate<ReturnType>::invoke()
42 template <
typename ReturnType>
43 inline DelegateInterface<ReturnType>* FunctionDelegate<ReturnType>::clone()
45 return new FunctionDelegate(_func);
48 template <
typename ReturnType,
typename Method,
typename Instance>
49 inline MethodDelegate<ReturnType, Method, Instance>::MethodDelegate(Method method,
51 : _method(method), _instance(instance)
55 template <
typename ReturnType,
typename Method,
typename Instance>
56 inline ReturnType MethodDelegate<ReturnType, Method, Instance>::invoke()
58 return (_instance.*_method)();
61 template <
typename ReturnType,
typename Method,
typename Instance>
62 inline DelegateInterface<ReturnType>* MethodDelegate<ReturnType, Method, Instance>::clone()
64 return new MethodDelegate(_method, _instance);
70 template <
typename ReturnType,
typename ParamType>
71 DelegateInterface<ReturnType, ParamType>::~DelegateInterface()
75 template <
typename ReturnType,
typename ParamType>
76 inline FunctionDelegate<ReturnType, ParamType>::FunctionDelegate(Function f) : _func(f)
79 template <
typename ReturnType,
typename ParamType>
80 inline ReturnType FunctionDelegate<ReturnType, ParamType>::invoke(ParamType param)
85 template <
typename ReturnType,
typename ParamType>
86 inline DelegateInterface<ReturnType, ParamType>* FunctionDelegate<ReturnType, ParamType>::clone()
88 return new FunctionDelegate(_func);
91 template <
typename ReturnType,
typename ParamType,
typename Method,
typename Instance>
92 MethodDelegate<ReturnType, ParamType, Method, Instance>::MethodDelegate(Method method,
94 : _method(method), _instance(inst)
98 template <
typename ReturnType,
typename ParamType,
typename Method,
typename Instance>
99 inline ReturnType MethodDelegate<ReturnType, ParamType, Method, Instance>::invoke(ParamType param)
101 return (_instance.*_method)(param);
104 template <
typename ReturnType,
typename ParamType,
typename Method,
typename Instance>
105 inline DelegateInterface<ReturnType, ParamType>* MethodDelegate<ReturnType,
110 return new MethodDelegate(_method, _instance);
115 template <
typename DelegateType>
116 inline DelegateBase<DelegateType>::~DelegateBase()
118 delete _ptr_to_callee;
121 template <
typename DelegateType>
122 inline DelegateBase<DelegateType>::DelegateBase() : _ptr_to_callee(nullptr)
126 template <
typename DelegateType>
127 inline DelegateBase<DelegateType>::DelegateBase(
const DelegateBase& other) : _ptr_to_callee(nullptr)
129 if (other._ptr_to_callee) {
130 _ptr_to_callee = other._ptr_to_callee->clone();
134 template <
typename DelegateType>
135 inline DelegateBase<DelegateType>& DelegateBase<DelegateType>::operator=(
const DelegateBase& other)
137 if (&other !=
this) {
138 delete _ptr_to_callee;
139 _ptr_to_callee =
nullptr;
140 if (other._ptr_to_callee) {
141 _ptr_to_callee = other._ptr_to_callee->clone();
147 template <
typename DelegateType>
148 inline void DelegateBase<DelegateType>::setDelegate(DelegateType* delegate)
150 _ptr_to_callee = delegate;
153 template <
typename DelegateType>
154 inline DelegateType* DelegateBase<DelegateType>::getDelegate()
156 return _ptr_to_callee;
159 template <
typename DelegateType>
160 inline const DelegateType* DelegateBase<DelegateType>::getDelegate()
const
162 return _ptr_to_callee;
165 template <
typename DelegateType>
166 inline DelegateBase<DelegateType>::operator bool()
const
168 return getDelegate() !=
nullptr;
173 namespace experimental {
174 template <
typename ReturnType>
177 if (
nullptr != Function) {
178 using namespace detail::delegates;
179 this->setDelegate(
new FunctionDelegate<ReturnType>(Function));
183 template <
typename ReturnType>
184 template <
typename Method,
typename Instance>
187 if (Method(
nullptr) != method) {
188 using namespace detail::delegates;
189 this->setDelegate(
new MethodDelegate<ReturnType, Method, Instance>(method, instance));
193 template <
typename ReturnType>
197 throw std::runtime_error(
"Uninitialized Delegate invoked!");
198 return this->getDelegate()->invoke();
201 template <
typename ReturnType,
typename ParamType>
204 if (
nullptr != Function) {
205 using namespace detail::functions;
206 this->setDelegate(
new FunctionDelegate<ReturnType, ParamType>(Function));
210 template <
typename ReturnType,
typename ParamType>
211 template <
typename Method,
typename Instance>
214 if (Method(
nullptr) != method) {
215 using namespace detail::functions;
217 new MethodDelegate<ReturnType, ParamType, Method, Instance>(method, instance));
221 template <
typename ReturnType,
typename ParamType>
225 throw std::runtime_error(
"Uninitialized function invoked!");
227 return this->getDelegate()->invoke(param);
General delegate template: encapsulates a function or a method without a parameter.
General function template: encapsulates a function or a method with a single parameter.
Public API for NullaryDelegate and UnaryDelegate types.
Serves as the root component, with common functionality documented in core functionality.