1 /*
2 * RRoutines.hpp
3 *
4 * Copyright (C) 2021 by RStudio, PBC
5 *
6 * Unless you have received this program directly from RStudio pursuant
7 * to the terms of a commercial license agreement with RStudio, then
8 * this program is licensed to you under the terms of version 3 of the
9 * GNU Affero General Public License. This program is distributed WITHOUT
10 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
11 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
12 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
13 *
14 */
15
16 #ifndef R_ROUTINES_HPP
17 #define R_ROUTINES_HPP
18
19 #include <vector>
20
21 #include <r/RSexp.hpp>
22
23 #include <R_ext/Rdynload.h>
24
25 #include <core/type_traits/TypeTraits.hpp>
26
27 namespace rstudio {
28 namespace r {
29 namespace routines {
30
31 namespace internal {
32
33 template <typename ReturnType, typename... ArgumentTypes>
n_arguments(ReturnType (ArgumentTypes...))34 int n_arguments(ReturnType(ArgumentTypes...)) {
35 return sizeof...(ArgumentTypes);
36 }
37
38 template <typename... ArgumentTypes>
39 struct validate_args
40 {
41 typedef std::true_type type;
42 };
43
44 template <typename T, typename... ArgumentTypes>
45 struct validate_args<T, ArgumentTypes...>
46 {
47 static_assert(
48 std::is_same<T, SEXP>::value,
49 "Registered .Call methods should only accept SEXP parameters");
50
51 typedef typename validate_args<ArgumentTypes...>::type type;
52 };
53
54 template <typename ReturnType, typename... ArgumentTypes>
validate(ReturnType (ArgumentTypes...))55 void validate(ReturnType(ArgumentTypes...))
56 {
57 static_assert(
58 std::is_same<ReturnType, SEXP>::value,
59 "Registered .Call methods should have SEXP return type");
60
61 (void) typename validate_args<ArgumentTypes...>::type{};
62 }
63
64 } // end namespace internal
65
66 void addCallMethod(const R_CallMethodDef method);
67 void registerCallMethod(const char* name, DL_FUNC fun, int numArgs);
68 void registerAll();
69
70 // NOTE: This macro accepts multiple arguments but only uses the first one.
71 // This is just for backwards compatibility, so that existing calls that
72 // attempt to declare the number of arguments can continue to do so, while new
73 // usages can omit it and still do the right thing. (This is done primarily to
74 // avoid noisy diffs across the codebase where this macro is used)
75 #define RS_REGISTER_CALL_METHOD(__NAME__, ...) \
76 do \
77 { \
78 using namespace core::type_traits; \
79 ::rstudio::r::routines::internal::validate(__NAME__); \
80 int n = ::rstudio::r::routines::internal::n_arguments(__NAME__); \
81 R_CallMethodDef callMethodDef; \
82 callMethodDef.name = #__NAME__; \
83 callMethodDef.fun = reinterpret_cast<DL_FUNC>(__NAME__); \
84 callMethodDef.numArgs = n; \
85 ::rstudio::r::routines::addCallMethod(callMethodDef); \
86 } \
87 while (false)
88
89 } // namespace routines
90 } // namespace r
91 } // namespace rstudio
92
93
94 #endif // R_ROUTINES_HPP
95
96