| 1 | #ifndef _melder_templates_h_ |
| 2 | #define _melder_templates_h_ |
| 3 | /* melder_templates.h |
| 4 | * |
| 5 | * Copyright (C) 1992-2019,2022 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 | |
| 21 | //#define Melder_ENABLE_IF_ISA(A,B) , class = typename std::enable_if<std::is_base_of<B,A>::value>::type |
| 22 | #define Melder_ENABLE_IF_ISA(A,B) , class = typename std::enable_if_t<std::is_base_of<B,A>::value> |
| 23 | //#define Melder_ENABLE_IF_ISA(A,B) , class = typename std::enable_if_v<std::is_base_of<B,A>> |
| 24 | |
| 25 | template <typename Ret, typename T, typename... Args> |
| 26 | class MelderCallback { |
| 27 | public: |
| 28 | using FunctionType = Ret* (*) (T*, Args...); |
| 29 | MelderCallback (FunctionType f = nullptr) : _f (f) { } |
| 30 | template <typename T2 /*Melder_ENABLE_IF_ISA(T2,T)*/, typename Ret2 /*Melder_ENABLE_IF_ISA(Ret2,Ret)*/> |
| 31 | MelderCallback (Ret2* (*f) (T2*, Args...)) : _f (reinterpret_cast<FunctionType> (f)) { |
| 32 | static_assert (std::is_base_of <T, T2> :: value, |
| 33 | "First argument of MelderCallback should have covariant type."); |
| 34 | static_assert (std::is_base_of <Ret, Ret2> :: value, |
| 35 | "Return type of MelderCallback should be covariant."); |
| 36 | }; |
| 37 | Ret* operator () (T* data, Args ... args) { return _f (data, std::forward<Args>(args)...); } |
| 38 | explicit operator bool () const noexcept { return !! _f; } |
| 39 | FunctionType get() { return _f; } |
| 40 | private: |
| 41 | FunctionType _f; |
| 42 | }; |
| 43 | template <typename T, typename... Args> |
| 44 | class MelderCallback <void, T, Args...> { // specialization |
| 45 | public: |
| 46 | using FunctionType = void (*) (T*, Args...); |
| 47 | MelderCallback (FunctionType f = nullptr) : _f (f) { } |
| 48 | template <typename T2 /*Melder_ENABLE_IF_ISA(T2,T)*/> |
| 49 | MelderCallback (void (*f) (T2*, Args...)) : _f (reinterpret_cast<FunctionType> (f)) { |
| 50 | static_assert (std::is_base_of <T, T2> :: value, |
| 51 | "First argument of MelderCallback should have covariant type."); |
| 52 | }; |
| 53 | void operator () (T* data, Args ... args) { _f (data, std::forward<Args>(args)...); } |
| 54 | explicit operator bool () const noexcept { return !! _f; } |
| 55 | FunctionType get() { return _f; } |
| 56 | private: |
| 57 | FunctionType _f; |
| 58 | }; |
| 59 | template <typename T, typename... Args> |
| 60 | class MelderCallback <int, T, Args...> { // specialization |
| 61 | public: |
| 62 | using FunctionType = int (*) (T*, Args...); |
| 63 | MelderCallback (FunctionType f = nullptr) : _f (f) { } |
| 64 | template <typename T2 /*Melder_ENABLE_IF_ISA(T2,T)*/> |
| 65 | MelderCallback (int (*f) (T2*, Args...)) : _f (reinterpret_cast<FunctionType> (f)) { |
| 66 | static_assert (std::is_base_of <T, T2> :: value, |
| 67 | "First argument of MelderCallback should have covariant type."); |
| 68 | }; |
| 69 | int operator () (T* data, Args ... args) { return _f (data, std::forward<Args>(args)...); } |
| 70 | explicit operator bool () const noexcept { return !! _f; } |
| 71 | FunctionType get() { return _f; } |
| 72 | private: |
| 73 | FunctionType _f; |
| 74 | }; |
| 75 | |
| 76 | template <typename T> |
| 77 | class MelderCompareHook { |
| 78 | public: |
| 79 | typedef int (*FunctionType) (T*, T*); |
| 80 | MelderCompareHook (FunctionType f = nullptr) : _f (f) { } |
| 81 | template <typename T2 Melder_ENABLE_IF_ISA(T2,T)> |
| 82 | MelderCompareHook (int (*f) (T2*, T2*)) : _f (reinterpret_cast<FunctionType> (f)) { }; |
| 83 | int operator () (T* data1, T* data2) noexcept { return _f (data1, data2); } |
| 84 | explicit operator bool () const noexcept { return !! _f; } |
| 85 | FunctionType get() { return _f; } |
| 86 | private: |
| 87 | FunctionType _f; |
| 88 | }; |
| 89 | |
| 90 | template <typename T, typename Y Melder_ENABLE_IF_ISA(Y,T)> |
| 91 | T** MelderPointerToPointerCast (Y **thing) { |
| 92 | return (T**) thing; |
| 93 | } |
| 94 | |
| 95 | /* End of file melder_templates.h */ |
| 96 | #endif |