Embedded Template Library  1.0
function.h
Go to the documentation of this file.
1 
3 /******************************************************************************
4 The MIT License(MIT)
5 
6 Embedded Template Library.
7 https://github.com/ETLCPP/etl
8 https://www.etlcpp.com
9 
10 Copyright(c) 2014 jwellbelove
11 
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files(the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions :
18 
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21 
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29 ******************************************************************************/
30 
31 #ifndef ETL_FUNCTION_INCLUDED
32 #define ETL_FUNCTION_INCLUDED
33 
34 #include "platform.h"
35 
36 //*****************************************************************************
43 //*****************************************************************************
44 
45 namespace etl
46 {
47  //***************************************************************************
51  //***************************************************************************
52  template <typename TParameter>
53  class ifunction
54  {
55  public:
56 
57  typedef TParameter parameter_type;
58 
59  virtual ~ifunction() {}
60 
61  //*************************************************************************
63  //*************************************************************************
64  virtual void operator ()(TParameter) const = 0;
65  };
66 
67  //***************************************************************************
70  //***************************************************************************
71  template <>
72  class ifunction<void>
73  {
74  public:
75 
76  typedef void parameter_type;
77 
78  virtual ~ifunction() {}
79 
80  //*************************************************************************
82  //*************************************************************************
83  virtual void operator ()() const = 0;
84  };
85 
86  //***************************************************************************
91  //***************************************************************************
92  template <typename TObject, typename TParameter>
93  class function : public ifunction<TParameter>
94  {
95  public:
96 
97  typedef TObject object_type;
98  typedef TParameter parameter_type;
99 
100  //*************************************************************************
104  //*************************************************************************
105  function(TObject& object_, void(TObject::* p_function_)(TParameter))
106  : p_object(&object_),
107  p_function(p_function_)
108  {
109  }
110 
111  //*************************************************************************
114  //*************************************************************************
115  virtual void operator ()(TParameter data) const ETL_OVERRIDE
116  {
117  // Call the object's member function with the data.
118  (p_object->*p_function)(data);
119  }
120 
121  private:
122 
123  TObject* p_object;
124  void (TObject::* p_function)(TParameter);
125  };
126 
127  //***************************************************************************
131  //***************************************************************************
132  template <typename TObject>
133  class function<TObject, void> : public ifunction<void>
134  {
135  public:
136 
137  //*************************************************************************
141  //*************************************************************************
142  function(TObject& object_, void(TObject::* p_function_)(void))
143  : p_object(&object_),
144  p_function(p_function_)
145  {
146  }
147 
148  //*************************************************************************
150  //*************************************************************************
151  virtual void operator ()() const ETL_OVERRIDE
152  {
153  // Call the object's member function.
154  (p_object->*p_function)();
155  }
156 
157  private:
158 
159  TObject* p_object;
160  void (TObject::* p_function)();
161  };
162 
163  //***************************************************************************
166  //***************************************************************************
167  template <typename TParameter>
168  class function<void, TParameter> : public ifunction<TParameter>
169  {
170  public:
171 
172  //*************************************************************************
175  //*************************************************************************
176  explicit function(void(*p_function_)(TParameter))
177  : p_function(p_function_)
178  {
179  }
180 
181  //*************************************************************************
184  //*************************************************************************
185  virtual void operator ()(TParameter data) const ETL_OVERRIDE
186  {
187  // Call the function with the data.
188  (*p_function)(data);
189  }
190 
191  private:
192 
193  void (*p_function)(TParameter);
194  };
195 
196  //***************************************************************************
199  //***************************************************************************
200  template <>
201  class function<void, void> : public ifunction<void>
202  {
203  public:
204 
205  //*************************************************************************
208  //*************************************************************************
209  explicit function(void(*p_function_)(void))
210  : p_function(p_function_)
211  {
212  }
213 
214  //*************************************************************************
216  //*************************************************************************
217  virtual void operator ()() const ETL_OVERRIDE
218  {
219  // Call the function.
220  (*p_function)();
221  }
222 
223  private:
224 
225  void (*p_function)();
226  };
227 
228  //***************************************************************************
233  //***************************************************************************
234  template <typename TObject, typename TParameter, void (TObject::*Function)(TParameter)>
235  class function_mp : public ifunction<TParameter>
236  {
237  public:
238 
239  typedef TObject object_type;
240  typedef TParameter parameter_type;
241 
242  //*************************************************************************
245  //*************************************************************************
246  explicit function_mp(TObject& object_)
247  : p_object(&object_)
248  {
249  }
250 
251  //*************************************************************************
254  //*************************************************************************
255  virtual void operator ()(TParameter data) const ETL_OVERRIDE
256  {
257  // Call the object's member function with the data.
258  (p_object->*Function)(data);
259  }
260 
261  private:
262 
263  TObject* p_object;
264  };
265 
266  //***************************************************************************
271  //***************************************************************************
272  template <typename TObject, void (TObject::*Function)(void)>
273  class function_mv : public ifunction<void>
274  {
275  public:
276 
277  typedef TObject object_type;
278  typedef void parameter_type;
279 
280  //*************************************************************************
283  //*************************************************************************
284  explicit function_mv(TObject& object_)
285  : p_object(&object_)
286  {
287  }
288 
289  //*************************************************************************
292  //*************************************************************************
293  virtual void operator ()() const ETL_OVERRIDE
294  {
295  // Call the object's member function.
296  (p_object->*Function)();
297  }
298 
299  private:
300 
301  TObject* p_object;
302  };
303 
304  //***************************************************************************
309  //***************************************************************************
310  template <typename TObject, typename TParameter, TObject& Instance, void (TObject::*Function)(TParameter)>
311  class function_imp : public ifunction<TParameter>
312  {
313  public:
314 
315  typedef TObject object_type;
316  typedef TParameter parameter_type;
317 
318  //*************************************************************************
321  //*************************************************************************
322  virtual void operator ()(TParameter data) const ETL_OVERRIDE
323  {
324  // Call the object's member function with the data.
325  (Instance.*Function)(data);
326  }
327  };
328 
329  //***************************************************************************
334  //***************************************************************************
335  template <typename TObject, TObject& Instance, void (TObject::*Function)(void)>
336  class function_imv : public ifunction<void>
337  {
338  public:
339 
340  typedef TObject object_type;
341  typedef void parameter_type;
342 
343  //*************************************************************************
346  //*************************************************************************
347  virtual void operator ()() const ETL_OVERRIDE
348  {
349  // Call the object's member function.
350  (Instance.*Function)();
351  }
352  };
353 
354  //***************************************************************************
358  //***************************************************************************
359  template <typename TParameter, void (*Function)(TParameter)>
360  class function_fp : public ifunction<TParameter>
361  {
362  public:
363 
364  typedef TParameter parameter_type;
365 
366  //*************************************************************************
370  //*************************************************************************
372  {
373  }
374 
375  //*************************************************************************
378  //*************************************************************************
379  virtual void operator ()(TParameter data) const ETL_OVERRIDE
380  {
381  // Call the object's member function with the data.
382  (*Function)(data);
383  }
384  };
385 
386  //***************************************************************************
390  //***************************************************************************
391  template <void(*Function)(void)>
392  class function_fv : public ifunction<void>
393  {
394  public:
395 
396  typedef void parameter_type;
397 
398  //*************************************************************************
402  //*************************************************************************
404  {
405  }
406 
407  //*************************************************************************
410  //*************************************************************************
411  virtual void operator ()() const ETL_OVERRIDE
412  {
413  // Call the function.
414  (*Function)();
415  }
416  };
417 
418 }
419 
420 #endif
TParameter parameter_type
The type of parameter sent to the function.
Definition: function.h:316
TObject object_type
The type of object.
Definition: function.h:239
TObject object_type
The type of object.
Definition: function.h:97
virtual void operator()(TParameter) const =0
The function operator that will be overridden.
TObject object_type
The type of object.
Definition: function.h:315
void parameter_type
The type of parameter sent to the function.
Definition: function.h:396
function_mv(TObject &object_)
Definition: function.h:284
function_fv()
Definition: function.h:403
TObject object_type
The type of object.
Definition: function.h:340
function_mp(TObject &object_)
Definition: function.h:246
virtual void operator()(TParameter data) const ETL_OVERRIDE
Definition: function.h:255
TParameter parameter_type
The type of parameter sent to the function.
Definition: function.h:240
virtual void operator()() const ETL_OVERRIDE
Definition: function.h:411
TParameter parameter_type
The type of parameter sent to the function.
Definition: function.h:364
virtual void operator()() const ETL_OVERRIDE
Definition: function.h:293
function_fp()
Definition: function.h:371
void parameter_type
The type of parameter sent to the function.
Definition: function.h:341
void parameter_type
The type of parameter sent to the function.
Definition: function.h:278
TParameter parameter_type
The type of parameter sent to the function.
Definition: function.h:98
TParameter parameter_type
The type of parameter sent to the function.
Definition: function.h:57
void parameter_type
The type of parameter sent to the function.
Definition: function.h:76
TObject object_type
The type of object.
Definition: function.h:277
virtual void operator()(TParameter data) const ETL_OVERRIDE
Definition: function.h:115
virtual void operator()(TParameter data) const ETL_OVERRIDE
Definition: function.h:322
virtual void operator()(TParameter data) const ETL_OVERRIDE
Definition: function.h:379
virtual void operator()() const ETL_OVERRIDE
Definition: function.h:347
Definition: function.h:94
Definition: function.h:361
Definition: function.h:393
Definition: function.h:312
Definition: function.h:337
Definition: function.h:236
Definition: function.h:274
Definition: function.h:54
Definition: absolute.h:37