]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/r/inst/include/cpp11/attribute_proxy.hpp
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / r / inst / include / cpp11 / attribute_proxy.hpp
1 // cpp11 version: 0.3.1.1
2 // vendored on: 2021-08-11
3 #pragma once
4
5 #include <initializer_list> // for initializer_list
6 #include <string> // for string, basic_string
7
8 #include "cpp11/R.hpp" // for SEXP, SEXPREC, Rf_install, PROTECT, Rf_...
9 #include "cpp11/as.hpp" // for as_sexp
10 #include "cpp11/protect.hpp" // for protect, safe, protect::function
11
12 namespace cpp11 {
13
14 class sexp;
15
16 template <typename T>
17 class attribute_proxy {
18 private:
19 const T& parent_;
20 SEXP symbol_;
21
22 public:
23 attribute_proxy(const T& parent, const char* index)
24 : parent_(parent), symbol_(safe[Rf_install](index)) {}
25
26 attribute_proxy(const T& parent, const std::string& index)
27 : parent_(parent), symbol_(safe[Rf_install](index.c_str())) {}
28
29 attribute_proxy(const T& parent, SEXP index) : parent_(parent), symbol_(index) {}
30
31 template <typename C>
32 attribute_proxy& operator=(C rhs) {
33 SEXP value = PROTECT(as_sexp(rhs));
34 Rf_setAttrib(parent_.data(), symbol_, value);
35 UNPROTECT(1);
36 return *this;
37 }
38
39 template <typename C>
40 attribute_proxy& operator=(std::initializer_list<C> rhs) {
41 SEXP value = PROTECT(as_sexp(rhs));
42 Rf_setAttrib(parent_.data(), symbol_, value);
43 UNPROTECT(1);
44 return *this;
45 }
46
47 operator SEXP() const { return safe[Rf_getAttrib](parent_.data(), symbol_); }
48 };
49
50 } // namespace cpp11