Embedded Template Library  1.0
 All Classes Files Functions Variables Typedefs Friends Modules Pages
function.h
Go to the documentation of this file.
1 
3 /******************************************************************************
4 The MIT License(MIT)
5 
6 Embedded Template Library.
7 
8 Copyright(c) 2014 jwellbelove
9 
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files(the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions :
16 
17 The above copyright notice and this permission notice shall be included in all
18 copies or substantial portions of the Software.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 SOFTWARE.
27 ******************************************************************************/
28 
29 #ifndef __ETL_FUNCTION__
30 #define __ETL_FUNCTION__
31 
32 //*****************************************************************************
39 //*****************************************************************************
40 
41 namespace etl
42 {
43  //***************************************************************************
47  //***************************************************************************
48  template <typename TParameter>
49  class ifunction
50  {
51  public:
52 
53  typedef TParameter parameter_type;
54 
55  //*************************************************************************
57  //*************************************************************************
58  virtual void operator ()(TParameter) = 0;
59  };
60 
61  //***************************************************************************
64  //***************************************************************************
65  template <>
66  class ifunction<void>
67  {
68  public:
69 
70  //*************************************************************************
72  //*************************************************************************
73  virtual void operator ()() = 0;
74  };
75 
76  //***************************************************************************
81  //***************************************************************************
82  template <typename TObject, typename TParameter>
83  class function : public ifunction<TParameter>
84  {
85  public:
86 
87  typedef TObject object_type;
88  typedef TParameter parameter_type;
89 
90  //*************************************************************************
94  //*************************************************************************
95  function(TObject& object, void(TObject::* p_function)(TParameter))
96  : p_object(&object),
97  p_function(p_function)
98  {
99  }
100 
101  //*************************************************************************
104  //*************************************************************************
105  virtual void operator ()(TParameter data)
106  {
107  // Call the object's member function with the data.
108  (p_object->*p_function)(data);
109  }
110 
111  private:
112 
113  TObject* p_object;
114  void (TObject::* p_function)(TParameter);
115  };
116 
117  //***************************************************************************
121  //***************************************************************************
122  template <typename TObject>
123  class function<TObject, void> : public ifunction<void>
124  {
125  public:
126 
127  //*************************************************************************
131  //*************************************************************************
132  function(TObject& object, void(TObject::* p_function)(void))
133  : p_object(&object),
134  p_function(p_function)
135  {
136  }
137 
138  //*************************************************************************
140  //*************************************************************************
141  virtual void operator ()()
142  {
143  // Call the object's member function.
144  (p_object->*p_function)();
145  }
146 
147  private:
148 
149  TObject* p_object;
150  void (TObject::* p_function)();
151  };
152 
153  //***************************************************************************
156  //***************************************************************************
157  template <typename TParameter>
158  class function<void, TParameter> : public ifunction<TParameter>
159  {
160  public:
161 
162  //*************************************************************************
165  //*************************************************************************
166  function(void(*p_function)(TParameter))
167  : p_function(p_function)
168  {
169  }
170 
171  //*************************************************************************
174  //*************************************************************************
175  virtual void operator ()(TParameter data)
176  {
177  // Call the function with the data.
178  (*p_function)(data);
179  }
180 
181  private:
182 
183  void (*p_function)(TParameter);
184  };
185 
186  //***************************************************************************
189  //***************************************************************************
190  template <>
191  class function<void, void> : public ifunction<void>
192  {
193  public:
194 
195  //*************************************************************************
198  //*************************************************************************
199  function(void(*p_function)(void))
200  : p_function(p_function)
201  {
202  }
203 
204  //*************************************************************************
206  //*************************************************************************
207  virtual void operator ()()
208  {
209  // Call the function.
210  (*p_function)();
211  }
212 
213  private:
214 
215  void (*p_function)();
216  };
217 }
218 
219 #endif
TParameter parameter_type
The type of parameter sent to the function.
Definition: function.h:53
TParameter parameter_type
The type of parameter sent to the function.
Definition: function.h:88
Definition: function.h:49
Definition: algorithm.h:43
virtual void operator()(TParameter)=0
The function operator that will be overridden.
virtual void operator()(TParameter data)
Definition: function.h:105
Definition: function.h:83
TObject object_type
The type of object.
Definition: function.h:87