ADTF  3.18.2
delegate_impl.h
Go to the documentation of this file.
1 
16 #ifndef A_UTIL_UTIL_DELEGATE_IMPL_HEADER_INCLUDED
17 #define A_UTIL_UTIL_DELEGATE_IMPL_HEADER_INCLUDED
18 
20 
21 #include <stdexcept>
22 
23 namespace a_util {
24 namespace detail {
25 namespace delegates {
26 template <typename ReturnType>
27 inline DelegateInterface<ReturnType>::~DelegateInterface()
28 {
29 }
30 
31 template <typename ReturnType>
32 inline FunctionDelegate<ReturnType>::FunctionDelegate(Function f) : _func(f)
33 {
34 }
35 
36 template <typename ReturnType>
37 inline ReturnType FunctionDelegate<ReturnType>::invoke()
38 {
39  return _func();
40 }
41 
42 template <typename ReturnType>
43 inline DelegateInterface<ReturnType>* FunctionDelegate<ReturnType>::clone()
44 {
45  return new FunctionDelegate(_func);
46 }
47 
48 template <typename ReturnType, typename Method, typename Instance>
49 inline MethodDelegate<ReturnType, Method, Instance>::MethodDelegate(Method method,
50  Instance& instance)
51  : _method(method), _instance(instance)
52 {
53 }
54 
55 template <typename ReturnType, typename Method, typename Instance>
56 inline ReturnType MethodDelegate<ReturnType, Method, Instance>::invoke()
57 {
58  return (_instance.*_method)();
59 }
60 
61 template <typename ReturnType, typename Method, typename Instance>
62 inline DelegateInterface<ReturnType>* MethodDelegate<ReturnType, Method, Instance>::clone()
63 {
64  return new MethodDelegate(_method, _instance);
65 }
66 
67 } // namespace delegates
68 
69 namespace functions {
70 template <typename ReturnType, typename ParamType>
71 DelegateInterface<ReturnType, ParamType>::~DelegateInterface()
72 {
73 }
74 
75 template <typename ReturnType, typename ParamType>
76 inline FunctionDelegate<ReturnType, ParamType>::FunctionDelegate(Function f) : _func(f)
77 {
78 }
79 template <typename ReturnType, typename ParamType>
80 inline ReturnType FunctionDelegate<ReturnType, ParamType>::invoke(ParamType param)
81 {
82  return _func(param);
83 }
84 
85 template <typename ReturnType, typename ParamType>
86 inline DelegateInterface<ReturnType, ParamType>* FunctionDelegate<ReturnType, ParamType>::clone()
87 {
88  return new FunctionDelegate(_func);
89 }
90 
91 template <typename ReturnType, typename ParamType, typename Method, typename Instance>
92 MethodDelegate<ReturnType, ParamType, Method, Instance>::MethodDelegate(Method method,
93  Instance& inst)
94  : _method(method), _instance(inst)
95 {
96 }
97 
98 template <typename ReturnType, typename ParamType, typename Method, typename Instance>
99 inline ReturnType MethodDelegate<ReturnType, ParamType, Method, Instance>::invoke(ParamType param)
100 {
101  return (_instance.*_method)(param);
102 }
103 
104 template <typename ReturnType, typename ParamType, typename Method, typename Instance>
105 inline 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 
115 template <typename DelegateType>
116 inline DelegateBase<DelegateType>::~DelegateBase()
117 {
118  delete _ptr_to_callee;
119 }
120 
121 template <typename DelegateType>
122 inline DelegateBase<DelegateType>::DelegateBase() : _ptr_to_callee(nullptr)
123 {
124 }
125 
126 template <typename DelegateType>
127 inline 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 
134 template <typename DelegateType>
135 inline 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 
147 template <typename DelegateType>
148 inline void DelegateBase<DelegateType>::setDelegate(DelegateType* delegate)
149 {
150  _ptr_to_callee = delegate;
151 }
152 
153 template <typename DelegateType>
154 inline DelegateType* DelegateBase<DelegateType>::getDelegate()
155 {
156  return _ptr_to_callee;
157 }
158 
159 template <typename DelegateType>
160 inline const DelegateType* DelegateBase<DelegateType>::getDelegate() const
161 {
162  return _ptr_to_callee;
163 }
164 
165 template <typename DelegateType>
166 inline DelegateBase<DelegateType>::operator bool() const
167 {
168  return getDelegate() != nullptr;
169 }
170 
171 } // namespace detail
172 
173 namespace experimental {
174 template <typename ReturnType>
175 inline 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 
183 template <typename ReturnType>
184 template <typename Method, typename Instance>
185 inline NullaryDelegate<ReturnType>::NullaryDelegate(Method method, Instance& instance)
186 {
187  if (Method(nullptr) != method) {
188  using namespace detail::delegates;
189  this->setDelegate(new MethodDelegate<ReturnType, Method, Instance>(method, instance));
190  }
191 }
192 
193 template <typename ReturnType>
195 {
196  if (!(*this))
197  throw std::runtime_error("Uninitialized Delegate invoked!");
198  return this->getDelegate()->invoke();
199 }
200 
201 template <typename ReturnType, typename ParamType>
202 inline 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 
210 template <typename ReturnType, typename ParamType>
211 template <typename Method, typename Instance>
212 inline 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 
221 template <typename ReturnType, typename ParamType>
222 inline 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
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.
Definition: base.h:24