Dev Essential  1.6.3
Loading...
Searching...
No Matches
delegate_impl.h
Go to the documentation of this file.
1
15
16#ifndef A_UTIL_UTIL_DELEGATE_IMPL_HEADER_INCLUDED
17#define A_UTIL_UTIL_DELEGATE_IMPL_HEADER_INCLUDED
18
20
21#include <stdexcept>
22
23namespace a_util {
24namespace detail {
25namespace delegates {
26template <typename ReturnType>
27inline DelegateInterface<ReturnType>::~DelegateInterface()
28{
29}
30
31template <typename ReturnType>
32inline FunctionDelegate<ReturnType>::FunctionDelegate(Function f) : _func(f)
33{
34}
35
36template <typename ReturnType>
37inline ReturnType FunctionDelegate<ReturnType>::invoke()
38{
39 return _func();
40}
41
42template <typename ReturnType>
43inline DelegateInterface<ReturnType>* FunctionDelegate<ReturnType>::clone()
44{
45 return new FunctionDelegate(_func);
46}
47
48template <typename ReturnType, typename Method, typename Instance>
49inline MethodDelegate<ReturnType, Method, Instance>::MethodDelegate(Method method,
50 Instance& instance)
51 : _method(method), _instance(instance)
52{
53}
54
55template <typename ReturnType, typename Method, typename Instance>
56inline ReturnType MethodDelegate<ReturnType, Method, Instance>::invoke()
57{
58 return (_instance.*_method)();
59}
60
61template <typename ReturnType, typename Method, typename Instance>
62inline DelegateInterface<ReturnType>* MethodDelegate<ReturnType, Method, Instance>::clone()
63{
64 return new MethodDelegate(_method, _instance);
65}
66
67} // namespace delegates
68
69namespace functions {
70template <typename ReturnType, typename ParamType>
71DelegateInterface<ReturnType, ParamType>::~DelegateInterface()
72{
73}
74
75template <typename ReturnType, typename ParamType>
76inline FunctionDelegate<ReturnType, ParamType>::FunctionDelegate(Function f) : _func(f)
77{
78}
79template <typename ReturnType, typename ParamType>
80inline ReturnType FunctionDelegate<ReturnType, ParamType>::invoke(ParamType param)
81{
82 return _func(param);
83}
84
85template <typename ReturnType, typename ParamType>
86inline DelegateInterface<ReturnType, ParamType>* FunctionDelegate<ReturnType, ParamType>::clone()
87{
88 return new FunctionDelegate(_func);
89}
90
91template <typename ReturnType, typename ParamType, typename Method, typename Instance>
92MethodDelegate<ReturnType, ParamType, Method, Instance>::MethodDelegate(Method method,
93 Instance& inst)
94 : _method(method), _instance(inst)
95{
96}
97
98template <typename ReturnType, typename ParamType, typename Method, typename Instance>
99inline ReturnType MethodDelegate<ReturnType, ParamType, Method, Instance>::invoke(ParamType param)
100{
101 return (_instance.*_method)(param);
102}
103
104template <typename ReturnType, typename ParamType, typename Method, typename Instance>
105inline DelegateInterface<ReturnType, ParamType>* MethodDelegate<ReturnType,
106 ParamType,
107 Method,
108 Instance>::clone()
109{
110 return new MethodDelegate(_method, _instance);
111}
112
113} // namespace functions
114
115template <typename DelegateType>
116inline DelegateBase<DelegateType>::~DelegateBase()
117{
118 delete _ptr_to_callee;
119}
120
121template <typename DelegateType>
122inline DelegateBase<DelegateType>::DelegateBase() : _ptr_to_callee(nullptr)
123{
124}
125
126template <typename DelegateType>
127inline DelegateBase<DelegateType>::DelegateBase(const DelegateBase& other) : _ptr_to_callee(nullptr)
128{
129 if (other._ptr_to_callee) {
130 _ptr_to_callee = other._ptr_to_callee->clone();
131 }
132}
133
134template <typename DelegateType>
135inline DelegateBase<DelegateType>& DelegateBase<DelegateType>::operator=(const DelegateBase& other)
136{
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();
142 }
143 }
144 return *this;
145}
146
147template <typename DelegateType>
148inline void DelegateBase<DelegateType>::setDelegate(DelegateType* delegate)
149{
150 _ptr_to_callee = delegate;
151}
152
153template <typename DelegateType>
154inline DelegateType* DelegateBase<DelegateType>::getDelegate()
155{
156 return _ptr_to_callee;
157}
158
159template <typename DelegateType>
160inline const DelegateType* DelegateBase<DelegateType>::getDelegate() const
161{
162 return _ptr_to_callee;
163}
164
165template <typename DelegateType>
166inline DelegateBase<DelegateType>::operator bool() const
167{
168 return getDelegate() != nullptr;
170
171} // namespace detail
172
173namespace experimental {
174template <typename ReturnType>
175inline NullaryDelegate<ReturnType>::NullaryDelegate(ReturnType (*Function)())
176{
177 if (nullptr != Function) {
178 using namespace detail::delegates;
179 this->setDelegate(new FunctionDelegate<ReturnType>(Function));
180 }
181}
182
183template <typename ReturnType>
184template <typename Method, typename Instance>
185inline NullaryDelegate<ReturnType>::NullaryDelegate(Method method, Instance& instance)
187 if (Method(nullptr) != method) {
188 using namespace detail::delegates;
189 this->setDelegate(new MethodDelegate<ReturnType, Method, Instance>(method, instance));
190 }
191}
192
193template <typename ReturnType>
195{
196 if (!(*this))
197 throw std::runtime_error("Uninitialized Delegate invoked!");
198 return this->getDelegate()->invoke();
199}
200
201template <typename ReturnType, typename ParamType>
202inline UnaryDelegate<ReturnType, ParamType>::UnaryDelegate(ReturnType (*Function)(ParamType))
203{
204 if (nullptr != Function) {
205 using namespace detail::functions;
206 this->setDelegate(new FunctionDelegate<ReturnType, ParamType>(Function));
207 }
208}
209
210template <typename ReturnType, typename ParamType>
211template <typename Method, typename Instance>
212inline UnaryDelegate<ReturnType, ParamType>::UnaryDelegate(Method method, Instance& instance)
213{
214 if (Method(nullptr) != method) {
215 using namespace detail::functions;
216 this->setDelegate(
217 new MethodDelegate<ReturnType, ParamType, Method, Instance>(method, instance));
218 }
219}
220
221template <typename ReturnType, typename ParamType>
222inline ReturnType UnaryDelegate<ReturnType, ParamType>::operator()(ParamType param)
223{
224 if (!(*this)) {
225 throw std::runtime_error("Uninitialized function invoked!");
226 }
227 return this->getDelegate()->invoke(param);
228}
229
230} // namespace experimental
231} // namespace a_util
232
233#endif // A_UTIL_UTIL_DELEGATE_IMPL_HEADER_INCLUDED
ReturnType operator()()
Definition delegate_impl.h:194
NullaryDelegate(ReturnType(*Function)())
Definition delegate_impl.h:175
ReturnType operator()(ParamType param)
Definition delegate_impl.h:222
UnaryDelegate(ReturnType(*Function)(ParamType))
Definition delegate_impl.h:202
Serves as component for features which are considered experimental.
Definition base.h:26
Serves as the root component, with common functionality documented in core functionality.
Definition base.h:24