Naze32 clone with Frysky receiver
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

FunctionPointer.h 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2015 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MBED_FUNCTIONPOINTER_H
  17. #define MBED_FUNCTIONPOINTER_H
  18. #include <string.h>
  19. #include <stdint.h>
  20. namespace mbed {
  21. /* If we had variaditic templates, this wouldn't be a problem, but until C++11 is enabled, we are stuck with multiple classes... */
  22. /** A class for storing and calling a pointer to a static or member function
  23. */
  24. template <typename R, typename A1>
  25. class FunctionPointerArg1{
  26. public:
  27. /** Create a FunctionPointer, attaching a static function
  28. *
  29. * @param function The static function to attach (default is none)
  30. */
  31. FunctionPointerArg1(R (*function)(A1) = 0) {
  32. attach(function);
  33. }
  34. /** Create a FunctionPointer, attaching a member function
  35. *
  36. * @param object The object pointer to invoke the member function on (i.e. the this pointer)
  37. * @param function The address of the member function to attach
  38. */
  39. template<typename T>
  40. FunctionPointerArg1(T *object, R (T::*member)(A1)) {
  41. attach(object, member);
  42. }
  43. /** Attach a static function
  44. *
  45. * @param function The static function to attach (default is none)
  46. */
  47. void attach(R (*function)(A1)) {
  48. _p.function = function;
  49. _membercaller = 0;
  50. }
  51. /** Attach a member function
  52. *
  53. * @param object The object pointer to invoke the member function on (i.e. the this pointer)
  54. * @param function The address of the member function to attach
  55. */
  56. template<typename T>
  57. void attach(T *object, R (T::*member)(A1)) {
  58. _p.object = static_cast<void*>(object);
  59. *reinterpret_cast<R (T::**)(A1)>(_member) = member;
  60. _membercaller = &FunctionPointerArg1::membercaller<T>;
  61. }
  62. /** Call the attached static or member function
  63. */
  64. R call(A1 a) {
  65. if (_membercaller == 0 && _p.function) {
  66. return _p.function(a);
  67. } else if (_membercaller && _p.object) {
  68. return _membercaller(_p.object, _member, a);
  69. }
  70. return (R)0;
  71. }
  72. /** Get registered static function
  73. */
  74. R(*get_function(A1))() {
  75. return _membercaller ? (R(*)(A1))0 : (R(*)(A1))_p.function;
  76. }
  77. #ifdef MBED_OPERATORS
  78. R operator ()(A1 a) {
  79. return call(a);
  80. }
  81. operator bool(void) const {
  82. return (_membercaller != NULL ? _p.object : (void*)_p.function) != NULL;
  83. }
  84. #endif
  85. private:
  86. template<typename T>
  87. static R membercaller(void *object, uintptr_t *member, A1 a) {
  88. T* o = static_cast<T*>(object);
  89. R (T::**m)(A1) = reinterpret_cast<R (T::**)(A1)>(member);
  90. return (o->**m)(a);
  91. }
  92. union {
  93. R (*function)(A1); // static function pointer
  94. void *object; // object this pointer
  95. } _p;
  96. uintptr_t _member[4]; // aligned raw member function pointer storage - converted back by registered _membercaller
  97. R (*_membercaller)(void*, uintptr_t*, A1); // registered membercaller function to convert back and call _m.member on _object
  98. };
  99. /** A class for storing and calling a pointer to a static or member function (R ()(void))
  100. */
  101. template <typename R>
  102. class FunctionPointerArg1<R, void>{
  103. public:
  104. /** Create a FunctionPointer, attaching a static function
  105. *
  106. * @param function The static function to attach (default is none)
  107. */
  108. FunctionPointerArg1(R (*function)(void) = 0) {
  109. attach(function);
  110. }
  111. /** Create a FunctionPointer, attaching a member function
  112. *
  113. * @param object The object pointer to invoke the member function on (i.e. the this pointer)
  114. * @param function The address of the void member function to attach
  115. */
  116. template<typename T>
  117. FunctionPointerArg1(T *object, R (T::*member)(void)) {
  118. attach(object, member);
  119. }
  120. /** Attach a static function
  121. *
  122. * @param function The void static function to attach (default is none)
  123. */
  124. void attach(R (*function)(void)) {
  125. _p.function = function;
  126. _membercaller = 0;
  127. }
  128. /** Attach a member function
  129. *
  130. * @param object The object pointer to invoke the member function on (i.e. the this pointer)
  131. * @param function The address of the void member function to attach
  132. */
  133. template<typename T>
  134. void attach(T *object, R (T::*member)(void)) {
  135. _p.object = static_cast<void*>(object);
  136. *reinterpret_cast<R (T::**)(void)>(_member) = member;
  137. _membercaller = &FunctionPointerArg1::membercaller<T>;
  138. }
  139. /** Call the attached static or member function
  140. */
  141. R call(){
  142. if (_membercaller == 0 && _p.function) {
  143. return _p.function();
  144. } else if (_membercaller && _p.object) {
  145. return _membercaller(_p.object, _member);
  146. }
  147. return (R)0;
  148. }
  149. /** Get registered static function
  150. */
  151. R(*get_function())() {
  152. return _membercaller ? (R(*)())0 : (R(*)())_p.function;
  153. }
  154. #ifdef MBED_OPERATORS
  155. R operator ()(void) {
  156. return call();
  157. }
  158. operator bool(void) const {
  159. return (_membercaller != NULL ? _p.object : (void*)_p.function) != NULL;
  160. }
  161. #endif
  162. private:
  163. template<typename T>
  164. static R membercaller(void *object, uintptr_t *member) {
  165. T* o = static_cast<T*>(object);
  166. R (T::**m)(void) = reinterpret_cast<R (T::**)(void)>(member);
  167. return (o->**m)();
  168. }
  169. union {
  170. R (*function)(void); // static function pointer
  171. void *object; // object this pointer
  172. } _p;
  173. uintptr_t _member[4]; // aligned raw member function pointer storage - converted back by registered _membercaller
  174. R (*_membercaller)(void*, uintptr_t*); // registered membercaller function to convert back and call _m.member on _object
  175. };
  176. typedef FunctionPointerArg1<void, void> FunctionPointer;
  177. typedef FunctionPointerArg1<void, int> event_callback_t;
  178. } // namespace mbed
  179. #endif