FunctionPointer.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 "platform/Callback.h"
  19. #include "platform/mbed_toolchain.h"
  20. #include <string.h>
  21. #include <stdint.h>
  22. namespace mbed {
  23. /** \addtogroup platform */
  24. /** @{*/
  25. /**
  26. * \defgroup platform_FunctionPointer FunctionPointer class
  27. * @{
  28. */
  29. // Declarations for backwards compatibility
  30. // To be foward compatible, code should adopt the Callback class
  31. template <typename R, typename A1>
  32. class FunctionPointerArg1 : public Callback<R(A1)> {
  33. public:
  34. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  35. "FunctionPointerArg1<R, A> has been replaced by Callback<R(A)>")
  36. FunctionPointerArg1(R(*function)(A1) = 0)
  37. : Callback<R(A1)>(function) {}
  38. template<typename T>
  39. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  40. "FunctionPointerArg1<R, A> has been replaced by Callback<R(A)>")
  41. FunctionPointerArg1(T *object, R(T::*member)(A1))
  42. : Callback<R(A1)>(object, member) {}
  43. R(*get_function())(A1)
  44. {
  45. return *reinterpret_cast<R(* *)(A1)>(this);
  46. }
  47. R call(A1 a1) const
  48. {
  49. if (!Callback<R(A1)>::operator bool()) {
  50. return (R)0;
  51. }
  52. return Callback<R(A1)>::call(a1);
  53. }
  54. R operator()(A1 a1) const
  55. {
  56. return Callback<R(A1)>::call(a1);
  57. }
  58. };
  59. template <typename R>
  60. class FunctionPointerArg1<R, void> : public Callback<R()> {
  61. public:
  62. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  63. "FunctionPointer has been replaced by Callback<void()>")
  64. FunctionPointerArg1(R(*function)() = 0)
  65. : Callback<R()>(function) {}
  66. template<typename T>
  67. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  68. "FunctionPointer has been replaced by Callback<void()>")
  69. FunctionPointerArg1(T *object, R(T::*member)())
  70. : Callback<R()>(object, member) {}
  71. R(*get_function())()
  72. {
  73. return *reinterpret_cast<R(* *)()>(this);
  74. }
  75. R call() const
  76. {
  77. if (!Callback<R()>::operator bool()) {
  78. return (R)0;
  79. }
  80. return Callback<R()>::call();
  81. }
  82. R operator()() const
  83. {
  84. return Callback<R()>::call();
  85. }
  86. };
  87. typedef FunctionPointerArg1<void, void> FunctionPointer;
  88. /**@}*/
  89. /**@}*/
  90. } // namespace mbed
  91. #endif