File: list_of.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 (71 lines) | stat: -rw-r--r-- 2,014 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#pragma once

#include <string>  // for string, basic_string

#include "cpp11/R.hpp"     // for R_xlen_t, SEXP, SEXPREC, LONG_VECTOR_SUPPORT
#include "cpp11/list.hpp"  // for list

namespace cpp11 {

template <typename T>
class list_of : public list {
 public:
  list_of(const list& data) : list(data) {}

#ifdef LONG_VECTOR_SUPPORT
  T operator[](const int pos) const { return operator[](static_cast<R_xlen_t>(pos)); }
#endif

  T operator[](const R_xlen_t pos) const { return list::operator[](pos); }

  T operator[](const char* pos) const { return list::operator[](pos); }

  T operator[](const std::string& pos) const { return list::operator[](pos.c_str()); }
};

namespace writable {
template <typename T>
class list_of : public writable::list {
 public:
  list_of(const list& data) : writable::list(data) {}
  list_of(R_xlen_t n) : writable::list(n) {}

  class proxy {
   private:
    writable::list::proxy data_;

   public:
    proxy(const writable::list::proxy& data) : data_(data) {}

    operator T() const { return static_cast<SEXP>(*this); }
    operator SEXP() const { return static_cast<SEXP>(data_); }
#ifdef LONG_VECTOR_SUPPORT
    typename T::proxy operator[](int pos) { return static_cast<T>(data_)[pos]; }
#endif
    typename T::proxy operator[](R_xlen_t pos) { return static_cast<T>(data_)[pos]; }
    proxy operator[](const char* pos) { static_cast<T>(data_)[pos]; }
    proxy operator[](const std::string& pos) { return static_cast<T>(data_)[pos]; }
    proxy& operator=(const T& rhs) {
      data_ = rhs;

      return *this;
    }
  };

#ifdef LONG_VECTOR_SUPPORT
  proxy operator[](int pos) {
    return {writable::list::operator[](static_cast<R_xlen_t>(pos))};
  }
#endif

  proxy operator[](R_xlen_t pos) { return writable::list::operator[](pos); }

  proxy operator[](const char* pos) { return {writable::list::operator[](pos)}; }

  proxy operator[](const std::string& pos) {
    return writable::list::operator[](pos.c_str());
  }
};
}  // namespace writable

}  // namespace cpp11