Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
function.h
00001 ///\file 00002 00003 /****************************************************************************** 00004 The MIT License(MIT) 00005 00006 Embedded Template Library. 00007 https://github.com/ETLCPP/etl 00008 http://www.etlcpp.com 00009 00010 Copyright(c) 2014 jwellbelove 00011 00012 Permission is hereby granted, free of charge, to any person obtaining a copy 00013 of this software and associated documentation files(the "Software"), to deal 00014 in the Software without restriction, including without limitation the rights 00015 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell 00016 copies of the Software, and to permit persons to whom the Software is 00017 furnished to do so, subject to the following conditions : 00018 00019 The above copyright notice and this permission notice shall be included in all 00020 copies or substantial portions of the Software. 00021 00022 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00023 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00024 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE 00025 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00026 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00027 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 00028 SOFTWARE. 00029 ******************************************************************************/ 00030 00031 #ifndef __ETL_FUNCTION__ 00032 #define __ETL_FUNCTION__ 00033 00034 #include "platform.h " 00035 00036 //***************************************************************************** 00037 ///\defgroup function function 00038 /// A set of wrapper templates to allow a member or static function to be called 00039 /// without the caller having to know the specific details of the callee. 00040 /// This template class may be used to link interrupt vectors to specific member 00041 /// functions of a handler class. 00042 ///\ingroup utilities 00043 //***************************************************************************** 00044 00045 namespace etl 00046 { 00047 //*************************************************************************** 00048 ///\ingroup function 00049 /// The base interface template for function template specialisations. 00050 ///\tparam TParameter The parameter type expected by the function. 00051 //*************************************************************************** 00052 template <typename TParameter> 00053 class ifunction 00054 { 00055 public: 00056 00057 typedef TParameter parameter_type; ///< The type of parameter sent to the function. 00058 00059 //************************************************************************* 00060 /// The function operator that will be overridden. 00061 //************************************************************************* 00062 virtual void operator ()(TParameter) = 0; 00063 }; 00064 00065 //*************************************************************************** 00066 ///\ingroup function 00067 /// The base interface template for functions taking <b>void</b> parameters. 00068 //*************************************************************************** 00069 template <> 00070 class ifunction<void> 00071 { 00072 public: 00073 00074 typedef void parameter_type; ///< The type of parameter sent to the function. 00075 00076 //************************************************************************* 00077 /// The function operator that will be overridden. 00078 //************************************************************************* 00079 virtual void operator ()() = 0; 00080 }; 00081 00082 //*************************************************************************** 00083 ///\ingroup function 00084 /// A derived function template that takes an object type and parameter type. 00085 ///\tparam TObject The object type that contains the member function. 00086 ///\tparam TParameter The parameter type accepted by the member function. 00087 //*************************************************************************** 00088 template <typename TObject, typename TParameter> 00089 class function : public ifunction<TParameter> 00090 { 00091 public: 00092 00093 typedef TObject object_type; ///< The type of object. 00094 typedef TParameter parameter_type; ///< The type of parameter sent to the function. 00095 00096 //************************************************************************* 00097 /// Constructor. 00098 ///\param object Reference to the object 00099 ///\param p_function Pointer to the member function 00100 //************************************************************************* 00101 function(TObject& object_, void(TObject::* p_function_)(TParameter)) 00102 : p_object(&object_), 00103 p_function(p_function_) 00104 { 00105 } 00106 00107 //************************************************************************* 00108 /// The function operator that calls the destination function. 00109 ///\param data The data to pass to the function. 00110 //************************************************************************* 00111 virtual void operator ()(TParameter data) 00112 { 00113 // Call the object's member function with the data. 00114 (p_object->*p_function)(data); 00115 } 00116 00117 private: 00118 00119 TObject* p_object; ///< Pointer to the object that contains the function. 00120 void (TObject::* p_function)(TParameter); ///< Pointer to the member function. 00121 }; 00122 00123 //*************************************************************************** 00124 ///\ingroup function 00125 /// A derived function template that takes a parameter type. 00126 ///\tparam TObject The object type that contains the member function. 00127 //*************************************************************************** 00128 template <typename TObject> 00129 class function<TObject, void> : public ifunction<void> 00130 { 00131 public: 00132 00133 //************************************************************************* 00134 /// Constructor. 00135 ///\param object Reference to the object 00136 ///\param p_function Pointer to the member function 00137 //************************************************************************* 00138 function(TObject& object_, void(TObject::* p_function_)(void)) 00139 : p_object(&object_), 00140 p_function(p_function_) 00141 { 00142 } 00143 00144 //************************************************************************* 00145 /// The function operator that calls the destination function. 00146 //************************************************************************* 00147 virtual void operator ()() 00148 { 00149 // Call the object's member function. 00150 (p_object->*p_function)(); 00151 } 00152 00153 private: 00154 00155 TObject* p_object; ///< Pointer to the object that contains the function. 00156 void (TObject::* p_function)(); ///< Pointer to the member function. 00157 }; 00158 00159 //*************************************************************************** 00160 ///\ingroup function 00161 /// Specialisation for static or global functions that takes a parameter. 00162 //*************************************************************************** 00163 template <typename TParameter> 00164 class function<void, TParameter> : public ifunction<TParameter> 00165 { 00166 public: 00167 00168 //************************************************************************* 00169 /// Constructor. 00170 ///\param p_function Pointer to the function 00171 //************************************************************************* 00172 function(void(*p_function_)(TParameter)) 00173 : p_function(p_function_) 00174 { 00175 } 00176 00177 //************************************************************************* 00178 /// The function operator that calls the destination function. 00179 ///\param data The data to pass to the function. 00180 //************************************************************************* 00181 virtual void operator ()(TParameter data) 00182 { 00183 // Call the function with the data. 00184 (*p_function)(data); 00185 } 00186 00187 private: 00188 00189 void (*p_function)(TParameter); ///< Pointer to the function. 00190 }; 00191 00192 //*************************************************************************** 00193 ///\ingroup function 00194 /// Specialisation static functions taking void parameter. 00195 //*************************************************************************** 00196 template <> 00197 class function<void, void> : public ifunction<void> 00198 { 00199 public: 00200 00201 //************************************************************************* 00202 /// Constructor. 00203 ///\param p_function Pointer to the function. 00204 //************************************************************************* 00205 function(void(*p_function_)(void)) 00206 : p_function(p_function_) 00207 { 00208 } 00209 00210 //************************************************************************* 00211 /// The function operator that calls the destination function. 00212 //************************************************************************* 00213 virtual void operator ()() 00214 { 00215 // Call the function. 00216 (*p_function)(); 00217 } 00218 00219 private: 00220 00221 void (*p_function)(); ///< Pointer to the function. 00222 }; 00223 00224 //*************************************************************************** 00225 ///\ingroup function 00226 /// A derived function template that takes an object type and parameter type. 00227 ///\tparam TObject The object type that contains the member function. 00228 ///\tparam TParameter The parameter type accepted by the member function. 00229 //*************************************************************************** 00230 template <typename TObject, typename TParameter, void (TObject::*Function)(TParameter)> 00231 class function_mp : public ifunction<TParameter> 00232 { 00233 public: 00234 00235 typedef TObject object_type; ///< The type of object. 00236 typedef TParameter parameter_type; ///< The type of parameter sent to the function. 00237 00238 //************************************************************************* 00239 /// Constructor. 00240 ///\param object Reference to the object 00241 //************************************************************************* 00242 function_mp(TObject& object_) 00243 : p_object(&object_) 00244 { 00245 } 00246 00247 //************************************************************************* 00248 /// The function operator that calls the destination function. 00249 ///\param data The data to pass to the function. 00250 //************************************************************************* 00251 virtual void operator ()(TParameter data) 00252 { 00253 // Call the object's member function with the data. 00254 (p_object->*Function)(data); 00255 } 00256 00257 private: 00258 00259 TObject* p_object; ///< Pointer to the object that contains the function. 00260 }; 00261 00262 //*************************************************************************** 00263 ///\ingroup function 00264 /// A derived function template that takes an object type and parameter type. 00265 ///\tparam TObject The object type that contains the member function. 00266 ///\tparam TParameter The parameter type accepted by the member function. 00267 //*************************************************************************** 00268 template <typename TObject, void (TObject::*Function)(void)> 00269 class function_mv : public ifunction<void> 00270 { 00271 public: 00272 00273 typedef TObject object_type; ///< The type of object. 00274 typedef void parameter_type; ///< The type of parameter sent to the function. 00275 00276 //************************************************************************* 00277 /// Constructor. 00278 ///\param object Reference to the object 00279 //************************************************************************* 00280 function_mv(TObject& object_) 00281 : p_object(&object_) 00282 { 00283 } 00284 00285 //************************************************************************* 00286 /// The function operator that calls the destination function. 00287 ///\param data The data to pass to the function. 00288 //************************************************************************* 00289 virtual void operator ()() 00290 { 00291 // Call the object's member function. 00292 (p_object->*Function)(); 00293 } 00294 00295 private: 00296 00297 TObject* p_object; ///< Pointer to the object that contains the function. 00298 }; 00299 00300 //*************************************************************************** 00301 ///\ingroup function 00302 /// A derived function template that takes an object type and parameter type. 00303 ///\tparam TObject The object type that contains the member function. 00304 ///\tparam TParameter The parameter type accepted by the member function. 00305 //*************************************************************************** 00306 template <typename TObject, typename TParameter, TObject& Instance, void (TObject::*Function)(TParameter)> 00307 class function_imp : public ifunction<TParameter> 00308 { 00309 public: 00310 00311 typedef TObject object_type; ///< The type of object. 00312 typedef TParameter parameter_type; ///< The type of parameter sent to the function. 00313 00314 //************************************************************************* 00315 /// The function operator that calls the destination function. 00316 ///\param data The data to pass to the function. 00317 //************************************************************************* 00318 virtual void operator ()(TParameter data) 00319 { 00320 // Call the object's member function with the data. 00321 (Instance.*Function)(data); 00322 } 00323 }; 00324 00325 //*************************************************************************** 00326 ///\ingroup function 00327 /// A derived function template that takes an object type and parameter type. 00328 ///\tparam TObject The object type that contains the member function. 00329 ///\tparam TParameter The parameter type accepted by the member function. 00330 //*************************************************************************** 00331 template <typename TObject, TObject& Instance, void (TObject::*Function)(void)> 00332 class function_imv : public ifunction<void> 00333 { 00334 public: 00335 00336 typedef TObject object_type; ///< The type of object. 00337 typedef void parameter_type; ///< The type of parameter sent to the function. 00338 00339 //************************************************************************* 00340 /// The function operator that calls the destination function. 00341 ///\param data The data to pass to the function. 00342 //************************************************************************* 00343 virtual void operator ()() 00344 { 00345 // Call the object's member function. 00346 (Instance.*Function)(); 00347 } 00348 }; 00349 00350 //*************************************************************************** 00351 ///\ingroup function 00352 /// A derived function template that takes a parameter type. 00353 ///\tparam TParameter The parameter type accepted by the member function. 00354 //*************************************************************************** 00355 template <typename TParameter, void (*Function)(TParameter)> 00356 class function_fp : public ifunction<TParameter> 00357 { 00358 public: 00359 00360 typedef TParameter parameter_type; ///< The type of parameter sent to the function. 00361 00362 //************************************************************************* 00363 /// Constructor. 00364 ///\param object Reference to the object 00365 ///\param p_function Pointer to the member function 00366 //************************************************************************* 00367 function_fp() 00368 { 00369 } 00370 00371 //************************************************************************* 00372 /// The function operator that calls the destination function. 00373 ///\param data The data to pass to the function. 00374 //************************************************************************* 00375 virtual void operator ()(TParameter data) 00376 { 00377 // Call the object's member function with the data. 00378 (*Function)(data); 00379 } 00380 }; 00381 00382 //*************************************************************************** 00383 ///\ingroup function 00384 /// A derived function template that takes a parameter type. 00385 ///\tparam TParameter The parameter type accepted by the member function. 00386 //*************************************************************************** 00387 template <void(*Function)(void)> 00388 class function_fv : public ifunction<void> 00389 { 00390 public: 00391 00392 typedef void parameter_type; ///< The type of parameter sent to the function. 00393 00394 //************************************************************************* 00395 /// Constructor. 00396 ///\param object Reference to the object 00397 ///\param p_function Pointer to the member function 00398 //************************************************************************* 00399 function_fv() 00400 { 00401 } 00402 00403 //************************************************************************* 00404 /// The function operator that calls the destination function. 00405 ///\param data The data to pass to the function. 00406 //************************************************************************* 00407 virtual void operator ()() 00408 { 00409 // Call the function. 00410 (*Function)(); 00411 } 00412 }; 00413 00414 } 00415 00416 #endif 00417
Generated on Tue Jul 12 2022 14:05:41 by
