CallChain.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Suppress deprecation warnings since this whole
  2. // class is deprecated already
  3. #include "mbed_toolchain.h"
  4. #undef MBED_DEPRECATED_SINCE
  5. #define MBED_DEPRECATED_SINCE(...)
  6. #include "platform/CallChain.h"
  7. #include "cmsis.h"
  8. #include "platform/mbed_critical.h"
  9. namespace mbed {
  10. class CallChainLink {
  11. public:
  12. CallChainLink(): cb(), next(NULL)
  13. {
  14. // No work to do
  15. }
  16. CallChainLink(Callback<void()> &callback): cb(callback), next(NULL)
  17. {
  18. // No work to do
  19. }
  20. Callback<void()> cb;
  21. CallChainLink *next;
  22. };
  23. CallChain::CallChain(int size) : _chain(NULL)
  24. {
  25. // No work to do
  26. }
  27. CallChain::~CallChain()
  28. {
  29. clear();
  30. }
  31. pFunctionPointer_t CallChain::add(Callback<void()> func)
  32. {
  33. CallChainLink *new_link = new CallChainLink(func);
  34. if (NULL == _chain) {
  35. _chain = new_link;
  36. return &new_link->cb;
  37. }
  38. CallChainLink *link = _chain;
  39. while (true) {
  40. if (NULL == link->next) {
  41. link->next = new_link;
  42. return &new_link->cb;
  43. }
  44. link = link->next;
  45. }
  46. }
  47. pFunctionPointer_t CallChain::add_front(Callback<void()> func)
  48. {
  49. CallChainLink *link = new CallChainLink(func);
  50. link->next = _chain;
  51. _chain = link;
  52. return &link->cb;
  53. }
  54. int CallChain::size() const
  55. {
  56. CallChainLink *link = _chain;
  57. int elements = 0;
  58. while (link != NULL) {
  59. elements++;
  60. link = link->next;
  61. }
  62. return elements;
  63. }
  64. pFunctionPointer_t CallChain::get(int idx) const
  65. {
  66. CallChainLink *link = _chain;
  67. for (int i = 0; i < idx; i++) {
  68. if (NULL == link) {
  69. break;
  70. }
  71. link = link->next;
  72. }
  73. return &link->cb;
  74. }
  75. int CallChain::find(pFunctionPointer_t f) const
  76. {
  77. CallChainLink *link = _chain;
  78. int i = 0;
  79. while (link != NULL) {
  80. if (f == &link->cb) {
  81. return i;
  82. }
  83. i++;
  84. link = link->next;
  85. }
  86. return -1;
  87. }
  88. void CallChain::clear()
  89. {
  90. CallChainLink *link = _chain;
  91. _chain = NULL;
  92. while (link != NULL) {
  93. CallChainLink *temp = link->next;
  94. delete link;
  95. link = temp;
  96. }
  97. }
  98. bool CallChain::remove(pFunctionPointer_t f)
  99. {
  100. CallChainLink *link = _chain;
  101. while (link != NULL) {
  102. if (f == &link->cb) {
  103. delete link;
  104. return true;
  105. }
  106. link = link->next;
  107. }
  108. return false;
  109. }
  110. void CallChain::call()
  111. {
  112. CallChainLink *link = _chain;
  113. while (link != NULL) {
  114. link->cb.call();
  115. link = link->next;
  116. }
  117. }
  118. } // namespace mbed