reapack

Package manager for REAPER
Log | Files | Refs | Submodules | README | LICENSE

api_helper.hpp (3688B)


      1 /* ReaPack: Package manager for REAPER
      2  * Copyright (C) 2015-2025  Christian Fillion
      3  *
      4  * This program is free software: you can redistribute it and/or modify
      5  * it under the terms of the GNU Lesser General Public License as published by
      6  * the Free Software Foundation, either version 3 of the License, or
      7  * (at your option) any later version.
      8  *
      9  * This program is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12  * GNU Lesser General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU Lesser General Public License
     15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16  */
     17 
     18 #ifndef REAPACK_API_HELPER_HPP
     19 #define REAPACK_API_HELPER_HPP
     20 
     21 #include <cstdint>
     22 #include <tuple>
     23 
     24 #include <boost/preprocessor.hpp>
     25 
     26 template<typename T>
     27 struct ReaScriptAPI;
     28 
     29 template<typename R, typename... Args>
     30 struct ReaScriptAPI<R(*)(Args...)>
     31 {
     32   static void *applyVarArg(R(*fn)(Args...), void **argv, int argc)
     33   {
     34     if(static_cast<size_t>(argc) < sizeof...(Args))
     35       return nullptr;
     36 
     37     const auto &args { makeTuple(argv, std::index_sequence_for<Args...>{}) };
     38 
     39     if constexpr (std::is_void_v<R>) {
     40       std::apply(fn, args);
     41       return nullptr;
     42     }
     43     else if constexpr (std::is_floating_point_v<R>) {
     44       const auto value { std::apply(fn, args) };
     45       void *storage { argv[argc - 1] };
     46       *static_cast<double *>(storage) = value;
     47       return storage;
     48     }
     49     else {
     50       // cast numbers to have the same size as a pointer to avoid warnings
     51       using IntPtrR = std::conditional_t<std::is_pointer_v<R>, R, intptr_t>;
     52       const auto value { static_cast<IntPtrR>(std::apply(fn, args)) };
     53       return reinterpret_cast<void *>(value);
     54     }
     55   }
     56 
     57 private:
     58   template<size_t I>
     59   using NthType = typename std::tuple_element<I, std::tuple<Args...>>::type;
     60 
     61   template<size_t... I>
     62   static auto makeTuple(void **argv, std::index_sequence<I...>)
     63   {
     64     // C++17 is amazing
     65     return std::make_tuple(
     66       std::is_floating_point_v<NthType<I>> ?
     67         *reinterpret_cast<NthType<I>*>(argv[I]) :
     68         (NthType<I>)reinterpret_cast<intptr_t>(argv[I])
     69       ...
     70     );
     71   }
     72 };
     73 
     74 template<auto fn>
     75 void *InvokeReaScriptAPI(void **argv, int argc)
     76 {
     77   return ReaScriptAPI<decltype(fn)>::applyVarArg(fn, argv, argc);
     78 }
     79 
     80 #define ARG_TYPE(arg) BOOST_PP_TUPLE_ELEM(2, 0, arg)
     81 #define ARG_NAME(arg) BOOST_PP_TUPLE_ELEM(2, 1, arg)
     82 
     83 #define DEFARGS(r, data, i, arg) BOOST_PP_COMMA_IF(i) ARG_TYPE(arg) ARG_NAME(arg)
     84 #define DOCARGS(r, macro, i, arg) \
     85   BOOST_PP_EXPR_IF(i, ",") BOOST_PP_STRINGIZE(macro(arg))
     86 
     87 #define DEFINE_API(type, name, args, help, ...)                                 \
     88   static type API_##name(BOOST_PP_SEQ_FOR_EACH_I(DEFARGS, _, args)) __VA_ARGS__ \
     89                                                                                 \
     90   APIFunc API::name { #name,                                                    \
     91     reinterpret_cast<void *>(&API_##name),                                      \
     92     reinterpret_cast<void *>(&InvokeReaScriptAPI<&API_##name>),                 \
     93     reinterpret_cast<void *>(const_cast<char *>(                                \
     94       #type "\0"                                                                \
     95       BOOST_PP_SEQ_FOR_EACH_I(DOCARGS, ARG_TYPE, args) "\0"                     \
     96       BOOST_PP_SEQ_FOR_EACH_I(DOCARGS, ARG_NAME, args) "\0"                     \
     97       help                                                                      \
     98     ))                                                                          \
     99   }
    100 
    101 #endif