melder_templates.h 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #ifndef _melder_templates_h_
  2. #define _melder_templates_h_
  3. /* melder_templates.h
  4. *
  5. * Copyright (C) 1992-2018 Paul Boersma
  6. *
  7. * This code is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * This code is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. //#define Melder_ENABLE_IF_ISA(A,B) , class = typename std::enable_if<std::is_base_of<B,A>::value>::type
  21. #define Melder_ENABLE_IF_ISA(A,B) , class = typename std::enable_if_t<std::is_base_of<B,A>::value>
  22. //#define Melder_ENABLE_IF_ISA(A,B) , class = typename std::enable_if_v<std::is_base_of<B,A>>
  23. template <typename Ret, typename T, typename... Args>
  24. class MelderCallback {
  25. public:
  26. using FunctionType = Ret* (*) (T*, Args...);
  27. MelderCallback (FunctionType f = nullptr) : _f (f) { }
  28. template <typename T2 /*Melder_ENABLE_IF_ISA(T2,T)*/, typename Ret2 /*Melder_ENABLE_IF_ISA(Ret2,Ret)*/>
  29. MelderCallback (Ret2* (*f) (T2*, Args...)) : _f (reinterpret_cast<FunctionType> (f)) {
  30. static_assert (std::is_base_of <T, T2> :: value,
  31. "First argument of MelderCallback should have covariant type.");
  32. static_assert (std::is_base_of <Ret, Ret2> :: value,
  33. "Return type of MelderCallback should be covariant.");
  34. };
  35. Ret* operator () (T* data, Args ... args) { return _f (data, std::forward<Args>(args)...); }
  36. explicit operator bool () const { return !! _f; }
  37. private:
  38. FunctionType _f;
  39. };
  40. template <typename T, typename... Args>
  41. class MelderCallback <void, T, Args...> { // specialization
  42. public:
  43. using FunctionType = void (*) (T*, Args...);
  44. MelderCallback (FunctionType f = nullptr) : _f (f) { }
  45. template <typename T2 /*Melder_ENABLE_IF_ISA(T2,T)*/>
  46. MelderCallback (void (*f) (T2*, Args...)) : _f (reinterpret_cast<FunctionType> (f)) {
  47. static_assert (std::is_base_of <T, T2> :: value,
  48. "First argument of MelderCallback should have covariant type.");
  49. };
  50. void operator () (T* data, Args ... args) { _f (data, std::forward<Args>(args)...); }
  51. explicit operator bool () const { return !! _f; }
  52. private:
  53. FunctionType _f;
  54. };
  55. template <typename T, typename... Args>
  56. class MelderCallback <int, T, Args...> { // specialization
  57. public:
  58. using FunctionType = int (*) (T*, Args...);
  59. MelderCallback (FunctionType f = nullptr) : _f (f) { }
  60. template <typename T2 /*Melder_ENABLE_IF_ISA(T2,T)*/>
  61. MelderCallback (int (*f) (T2*, Args...)) : _f (reinterpret_cast<FunctionType> (f)) {
  62. static_assert (std::is_base_of <T, T2> :: value,
  63. "First argument of MelderCallback should have covariant type.");
  64. };
  65. int operator () (T* data, Args ... args) { return _f (data, std::forward<Args>(args)...); }
  66. explicit operator bool () const { return !! _f; }
  67. private:
  68. FunctionType _f;
  69. };
  70. template <typename T>
  71. class MelderCompareHook {
  72. public:
  73. typedef int (*FunctionType) (T*, T*);
  74. MelderCompareHook (FunctionType f = nullptr) : _f (f) { }
  75. template <typename T2 Melder_ENABLE_IF_ISA(T2,T)>
  76. MelderCompareHook (int (*f) (T2*, T2*)) : _f (reinterpret_cast<FunctionType> (f)) { };
  77. int operator () (T* data1, T* data2) noexcept { return _f (data1, data2); }
  78. explicit operator bool () const { return !! _f; }
  79. FunctionType get () { return _f; }
  80. private:
  81. FunctionType _f;
  82. };
  83. /* End of file melder_templates.h */
  84. #endif