|
| 1 | +// cpp11 version: 0.5.2 |
| 2 | +// vendored on: 2025-05-07 |
| 3 | +#pragma once |
| 4 | + |
| 5 | +#ifdef R_INTERNALS_H_ |
| 6 | +#if !(defined(R_NO_REMAP) && defined(STRICT_R_HEADERS)) |
| 7 | +#error R headers were included before cpp11 headers \ |
| 8 | + and at least one of R_NO_REMAP or STRICT_R_HEADERS \ |
| 9 | + was not defined. |
| 10 | +#endif |
| 11 | +#endif |
| 12 | + |
| 13 | +#ifndef R_NO_REMAP |
| 14 | +#define R_NO_REMAP |
| 15 | +#endif |
| 16 | + |
| 17 | +#ifndef STRICT_R_HEADERS |
| 18 | +#define STRICT_R_HEADERS |
| 19 | +#endif |
| 20 | + |
| 21 | +#include "R_ext/Boolean.h" |
| 22 | +#include "Rinternals.h" |
| 23 | +#include "Rversion.h" |
| 24 | + |
| 25 | +// clang-format off |
| 26 | +#ifdef __clang__ |
| 27 | +# pragma clang diagnostic push |
| 28 | +# pragma clang diagnostic ignored "-Wattributes" |
| 29 | +#endif |
| 30 | + |
| 31 | +#ifdef __GNUC__ |
| 32 | +# pragma GCC diagnostic push |
| 33 | +# pragma GCC diagnostic ignored "-Wattributes" |
| 34 | +#endif |
| 35 | +// clang-format on |
| 36 | + |
| 37 | +#include <type_traits> |
| 38 | + |
| 39 | +#if defined(R_VERSION) && R_VERSION >= R_Version(4, 4, 0) |
| 40 | +// Use R's new macro |
| 41 | +#define CPP11_PRIdXLEN_T R_PRIdXLEN_T |
| 42 | +#else |
| 43 | +// Recreate what new R does |
| 44 | +#ifdef LONG_VECTOR_SUPPORT |
| 45 | +#define CPP11_PRIdXLEN_T "td" |
| 46 | +#else |
| 47 | +#define CPP11_PRIdXLEN_T "d" |
| 48 | +#endif |
| 49 | +#endif |
| 50 | + |
| 51 | +namespace cpp11 { |
| 52 | +namespace literals { |
| 53 | + |
| 54 | +constexpr R_xlen_t operator""_xl(unsigned long long int value) { return value; } |
| 55 | + |
| 56 | +} // namespace literals |
| 57 | + |
| 58 | +namespace traits { |
| 59 | +template <typename T> |
| 60 | +struct get_underlying_type { |
| 61 | + using type = T; |
| 62 | +}; |
| 63 | +} // namespace traits |
| 64 | + |
| 65 | +namespace detail { |
| 66 | + |
| 67 | +// Annoyingly, `TYPEOF()` returns an `int` rather than a `SEXPTYPE`, |
| 68 | +// which can throw warnings with `-Wsign-compare` on Windows. |
| 69 | +inline SEXPTYPE r_typeof(SEXP x) { return static_cast<SEXPTYPE>(TYPEOF(x)); } |
| 70 | + |
| 71 | +/// Get an object from an environment |
| 72 | +/// |
| 73 | +/// SAFETY: Keep as a pure C function. Call like an R API function, i.e. wrap in `safe[]` |
| 74 | +/// as required. |
| 75 | +inline SEXP r_env_get(SEXP env, SEXP sym) { |
| 76 | +#if defined(R_VERSION) && R_VERSION >= R_Version(4, 5, 0) |
| 77 | + const Rboolean inherits = FALSE; |
| 78 | + return R_getVar(sym, env, inherits); |
| 79 | +#else |
| 80 | + SEXP out = Rf_findVarInFrame3(env, sym, TRUE); |
| 81 | + |
| 82 | + // Replicate the 3 checks from `R_getVar()` (along with exact error message): |
| 83 | + // - Object must be found in the `env` |
| 84 | + // - `R_MissingArg` can't leak from an `env` anymore |
| 85 | + // - Promises can't leak from an `env` anymore |
| 86 | + |
| 87 | + if (out == R_MissingArg) { |
| 88 | + Rf_errorcall(R_NilValue, "argument \"%s\" is missing, with no default", |
| 89 | + CHAR(PRINTNAME(sym))); |
| 90 | + } |
| 91 | + |
| 92 | + if (out == R_UnboundValue) { |
| 93 | + Rf_errorcall(R_NilValue, "object '%s' not found", CHAR(PRINTNAME(sym))); |
| 94 | + } |
| 95 | + |
| 96 | + if (r_typeof(out) == PROMSXP) { |
| 97 | + PROTECT(out); |
| 98 | + out = Rf_eval(out, env); |
| 99 | + UNPROTECT(1); |
| 100 | + } |
| 101 | + |
| 102 | + return out; |
| 103 | +#endif |
| 104 | +} |
| 105 | + |
| 106 | +/// Check if an object exists in an environment |
| 107 | +/// |
| 108 | +/// SAFETY: Keep as a pure C function. Call like an R API function, i.e. wrap in `safe[]` |
| 109 | +/// as required. |
| 110 | +inline bool r_env_has(SEXP env, SEXP sym) { |
| 111 | +#if R_VERSION >= R_Version(4, 2, 0) |
| 112 | + return R_existsVarInFrame(env, sym); |
| 113 | +#else |
| 114 | + return Rf_findVarInFrame3(env, sym, FALSE) != R_UnboundValue; |
| 115 | +#endif |
| 116 | +} |
| 117 | + |
| 118 | +} // namespace detail |
| 119 | + |
| 120 | +template <typename T> |
| 121 | +inline T na(); |
| 122 | + |
| 123 | +template <typename T> |
| 124 | +inline typename std::enable_if<!std::is_same<typename std::decay<T>::type, double>::value, |
| 125 | + bool>::type |
| 126 | +is_na(const T& value) { |
| 127 | + return value == na<T>(); |
| 128 | +} |
| 129 | + |
| 130 | +template <typename T> |
| 131 | +inline typename std::enable_if<std::is_same<typename std::decay<T>::type, double>::value, |
| 132 | + bool>::type |
| 133 | +is_na(const T& value) { |
| 134 | + return ISNA(value); |
| 135 | +} |
| 136 | + |
| 137 | +} // namespace cpp11 |
0 commit comments