|
1 |
| -// Extracted from ReaPack's and ReaImGui's source code (LGPL v3) |
2 |
| - |
3 |
| -/* Usage example: |
4 |
| -static int HelloWorld(int foo, int bar) { return foo * bar; } |
5 |
| -
|
6 |
| -extern "C" REAPER_PLUGIN_DLL_EXPORT int REAPER_PLUGIN_ENTRYPOINT( |
7 |
| - REAPER_PLUGIN_HINSTANCE instance, reaper_plugin_info_t *rec) |
8 |
| -{ |
9 |
| - // ... |
10 |
| - // API_HelloWorld and APIdef_HelloWorld must also be registered as usual |
11 |
| - plugin_register("APIvararg_HelloWorld", |
12 |
| - reinterpret_cast<void *>(&InvokeReaScriptAPI<&HelloWorld>)); |
13 |
| - // ... |
14 |
| -} |
15 |
| -*/ |
16 |
| - |
17 |
| -#include <tuple> |
18 |
| - |
19 |
| -template<typename T> |
20 |
| -struct ReaScriptAPI; |
21 |
| - |
22 |
| -template<typename R, typename... Args> |
23 |
| -struct ReaScriptAPI<R(*)(Args...)> |
24 |
| -{ |
25 |
| - static const void *applyVarArg(R(*fn)(Args...), void **argv, const int argc) |
26 |
| - { |
27 |
| - if(static_cast<size_t>(argc) < sizeof...(Args)) |
28 |
| - return nullptr; |
29 |
| - |
30 |
| - const auto &args { makeTuple(argv, std::index_sequence_for<Args...>{}) }; |
31 |
| - |
32 |
| - if constexpr (std::is_void_v<R>) { |
33 |
| - std::apply(fn, args); |
34 |
| - return nullptr; |
35 |
| - } |
36 |
| - else if constexpr (std::is_floating_point_v<R>) { |
37 |
| - const auto value { std::apply(fn, args) }; |
38 |
| - void *storage { argv[argc - 1] }; |
39 |
| - *static_cast<double *>(storage) = value; |
40 |
| - return storage; |
41 |
| - } |
42 |
| - else { |
43 |
| - // cast numbers to have the same size as a pointer to avoid warnings |
44 |
| - using IntPtrR = std::conditional_t<std::is_pointer_v<R>, R, uintptr_t>; |
45 |
| - const auto value { static_cast<IntPtrR>(std::apply(fn, args)) }; |
46 |
| - return reinterpret_cast<const void *>(value); |
47 |
| - } |
48 |
| - } |
49 |
| - |
50 |
| -private: |
51 |
| - template<size_t I> |
52 |
| - using NthType = typename std::tuple_element<I, std::tuple<Args...>>::type; |
53 |
| - |
54 |
| - template<size_t... I> |
55 |
| - static auto makeTuple(void **argv, std::index_sequence<I...>) |
56 |
| - { |
57 |
| - // C++17 is amazing |
58 |
| - return std::make_tuple( |
59 |
| - std::is_floating_point_v<NthType<I>> ? |
60 |
| - *reinterpret_cast<NthType<I>*>(argv[I]) : |
61 |
| - (NthType<I>)reinterpret_cast<uintptr_t>(argv[I]) |
62 |
| - ... |
63 |
| - ); |
64 |
| - } |
65 |
| - |
66 |
| -}; |
67 |
| - |
68 |
| -template<auto fn> |
69 |
| -const void *InvokeReaScriptAPI(void **argv, int argc) |
70 |
| -{ |
71 |
| - return ReaScriptAPI<decltype(fn)>::applyVarArg(fn, argv, argc); |
72 |
| -} |
| 1 | +// Extracted from ReaPack's and ReaImGui's source code (LGPL v3) |
| 2 | + |
| 3 | +/* Usage example: |
| 4 | +static int HelloWorld(int foo, int bar) { return foo * bar; } |
| 5 | +
|
| 6 | +extern "C" REAPER_PLUGIN_DLL_EXPORT int REAPER_PLUGIN_ENTRYPOINT( |
| 7 | + REAPER_PLUGIN_HINSTANCE instance, reaper_plugin_info_t *rec) |
| 8 | +{ |
| 9 | + // ... |
| 10 | + // API_HelloWorld and APIdef_HelloWorld must also be registered as usual |
| 11 | + plugin_register("APIvararg_HelloWorld", |
| 12 | + reinterpret_cast<void *>(&InvokeReaScriptAPI<&HelloWorld>)); |
| 13 | + // ... |
| 14 | +} |
| 15 | +*/ |
| 16 | + |
| 17 | +#include <tuple> |
| 18 | + |
| 19 | +template<typename T> |
| 20 | +struct ReaScriptAPI; |
| 21 | + |
| 22 | +template<typename R, typename... Args> |
| 23 | +struct ReaScriptAPI<R(*)(Args...)> |
| 24 | +{ |
| 25 | + static const void *applyVarArg(R(*fn)(Args...), void **argv, const int argc) |
| 26 | + { |
| 27 | + if(static_cast<size_t>(argc) < sizeof...(Args)) |
| 28 | + return nullptr; |
| 29 | + |
| 30 | + const auto &args { makeTuple(argv, std::index_sequence_for<Args...>{}) }; |
| 31 | + |
| 32 | + if constexpr (std::is_void_v<R>) { |
| 33 | + std::apply(fn, args); |
| 34 | + return nullptr; |
| 35 | + } |
| 36 | + else if constexpr (std::is_floating_point_v<R>) { |
| 37 | + const auto value { std::apply(fn, args) }; |
| 38 | + void *storage { argv[argc - 1] }; |
| 39 | + *static_cast<double *>(storage) = value; |
| 40 | + return storage; |
| 41 | + } |
| 42 | + else { |
| 43 | + // cast numbers to have the same size as a pointer to avoid warnings |
| 44 | + using IntPtrR = std::conditional_t<std::is_pointer_v<R>, R, uintptr_t>; |
| 45 | + const auto value { static_cast<IntPtrR>(std::apply(fn, args)) }; |
| 46 | + return reinterpret_cast<const void *>(value); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | +private: |
| 51 | + template<size_t I> |
| 52 | + using NthType = typename std::tuple_element<I, std::tuple<Args...>>::type; |
| 53 | + |
| 54 | + template<size_t... I> |
| 55 | + static auto makeTuple(void **argv, std::index_sequence<I...>) |
| 56 | + { |
| 57 | + // C++17 is amazing |
| 58 | + return std::make_tuple( |
| 59 | + std::is_floating_point_v<NthType<I>> ? |
| 60 | + *reinterpret_cast<NthType<I>*>(argv[I]) : |
| 61 | + (NthType<I>)reinterpret_cast<uintptr_t>(argv[I]) |
| 62 | + ... |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | +}; |
| 67 | + |
| 68 | +template<auto fn> |
| 69 | +const void *InvokeReaScriptAPI(void **argv, int argc) |
| 70 | +{ |
| 71 | + return ReaScriptAPI<decltype(fn)>::applyVarArg(fn, argv, argc); |
| 72 | +} |
0 commit comments