Naze32 clone with Frysky receiver
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CallChain.h 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2013 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_CALLCHAIN_H
  17. #define MBED_CALLCHAIN_H
  18. #include "FunctionPointer.h"
  19. #include <string.h>
  20. namespace mbed {
  21. /** Group one or more functions in an instance of a CallChain, then call them in
  22. * sequence using CallChain::call(). Used mostly by the interrupt chaining code,
  23. * but can be used for other purposes.
  24. *
  25. * Example:
  26. * @code
  27. * #include "mbed.h"
  28. *
  29. * CallChain chain;
  30. *
  31. * void first(void) {
  32. * printf("'first' function.\n");
  33. * }
  34. *
  35. * void second(void) {
  36. * printf("'second' function.\n");
  37. * }
  38. *
  39. * class Test {
  40. * public:
  41. * void f(void) {
  42. * printf("A::f (class member).\n");
  43. * }
  44. * };
  45. *
  46. * int main() {
  47. * Test test;
  48. *
  49. * chain.add(second);
  50. * chain.add_front(first);
  51. * chain.add(&test, &Test::f);
  52. * chain.call();
  53. * }
  54. * @endcode
  55. */
  56. typedef FunctionPointer* pFunctionPointer_t;
  57. class CallChain {
  58. public:
  59. /** Create an empty chain
  60. *
  61. * @param size (optional) Initial size of the chain
  62. */
  63. CallChain(int size = 4);
  64. virtual ~CallChain();
  65. /** Add a function at the end of the chain
  66. *
  67. * @param function A pointer to a void function
  68. *
  69. * @returns
  70. * The function object created for 'function'
  71. */
  72. pFunctionPointer_t add(void (*function)(void));
  73. /** Add a function at the end of the chain
  74. *
  75. * @param tptr pointer to the object to call the member function on
  76. * @param mptr pointer to the member function to be called
  77. *
  78. * @returns
  79. * The function object created for 'tptr' and 'mptr'
  80. */
  81. template<typename T>
  82. pFunctionPointer_t add(T *tptr, void (T::*mptr)(void)) {
  83. return common_add(new FunctionPointer(tptr, mptr));
  84. }
  85. /** Add a function at the beginning of the chain
  86. *
  87. * @param function A pointer to a void function
  88. *
  89. * @returns
  90. * The function object created for 'function'
  91. */
  92. pFunctionPointer_t add_front(void (*function)(void));
  93. /** Add a function at the beginning of the chain
  94. *
  95. * @param tptr pointer to the object to call the member function on
  96. * @param mptr pointer to the member function to be called
  97. *
  98. * @returns
  99. * The function object created for 'tptr' and 'mptr'
  100. */
  101. template<typename T>
  102. pFunctionPointer_t add_front(T *tptr, void (T::*mptr)(void)) {
  103. return common_add_front(new FunctionPointer(tptr, mptr));
  104. }
  105. /** Get the number of functions in the chain
  106. */
  107. int size() const;
  108. /** Get a function object from the chain
  109. *
  110. * @param i function object index
  111. *
  112. * @returns
  113. * The function object at position 'i' in the chain
  114. */
  115. pFunctionPointer_t get(int i) const;
  116. /** Look for a function object in the call chain
  117. *
  118. * @param f the function object to search
  119. *
  120. * @returns
  121. * The index of the function object if found, -1 otherwise.
  122. */
  123. int find(pFunctionPointer_t f) const;
  124. /** Clear the call chain (remove all functions in the chain).
  125. */
  126. void clear();
  127. /** Remove a function object from the chain
  128. *
  129. * @arg f the function object to remove
  130. *
  131. * @returns
  132. * true if the function object was found and removed, false otherwise.
  133. */
  134. bool remove(pFunctionPointer_t f);
  135. /** Call all the functions in the chain in sequence
  136. */
  137. void call();
  138. #ifdef MBED_OPERATORS
  139. void operator ()(void) {
  140. call();
  141. }
  142. pFunctionPointer_t operator [](int i) const {
  143. return get(i);
  144. }
  145. #endif
  146. private:
  147. void _check_size();
  148. pFunctionPointer_t common_add(pFunctionPointer_t pf);
  149. pFunctionPointer_t common_add_front(pFunctionPointer_t pf);
  150. pFunctionPointer_t* _chain;
  151. int _size;
  152. int _elements;
  153. /* disallow copy constructor and assignment operators */
  154. private:
  155. CallChain(const CallChain&);
  156. CallChain & operator = (const CallChain&);
  157. };
  158. } // namespace mbed
  159. #endif