File: named_arg.hpp

package info (click to toggle)
r-cran-cpp11 0.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,428 kB
  • sloc: cpp: 9,732; sh: 14; makefile: 2
file content (48 lines) | stat: -rw-r--r-- 1,029 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#pragma once

#include <stddef.h>  // for size_t

#include <initializer_list>  // for initializer_list

#include "cpp11/R.hpp"     // for SEXP, SEXPREC, literals
#include "cpp11/as.hpp"    // for as_sexp
#include "cpp11/sexp.hpp"  // for sexp

namespace cpp11 {
class named_arg {
 public:
  explicit named_arg(const char* name) : name_(name), value_(R_NilValue) {}
  named_arg& operator=(std::initializer_list<int> il) {
    value_ = as_sexp(il);
    return *this;
  }

  template <typename T>
  named_arg& operator=(T rhs) {
    value_ = as_sexp(rhs);
    return *this;
  }

  template <typename T>
  named_arg& operator=(std::initializer_list<T> rhs) {
    value_ = as_sexp(rhs);
    return *this;
  }

  const char* name() const { return name_; }
  SEXP value() const { return value_; }

 private:
  const char* name_;
  sexp value_;
};

namespace literals {

inline named_arg operator""_nm(const char* name, std::size_t) { return named_arg(name); }

}  // namespace literals

using namespace literals;

}  // namespace cpp11