-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathFieldProxy.h
More file actions
97 lines (80 loc) · 2.57 KB
/
FieldProxy.h
File metadata and controls
97 lines (80 loc) · 2.57 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#ifndef Rcpp_FieldProxy_h
#define Rcpp_FieldProxy_h
namespace Rcpp{
template <typename CLASS>
class FieldProxyPolicy {
public:
class FieldProxy : GenericProxy<FieldProxy> {
public:
FieldProxy( CLASS& v, const std::string& name) :
parent(v), field_name( Rf_mkChar(name.c_str())) {}
FieldProxy& operator=(const FieldProxy& rhs){
set( rhs.get() ) ;
return *this ;
}
template <typename T> FieldProxy& operator=(const T& rhs) {
set( wrap( rhs ) ) ;
return *this ;
}
template <typename T> operator T() const {
return as<T>(get()) ;
}
inline operator SEXP() const {
return get() ;
}
private:
CLASS& parent;
SEXP field_name ;
SEXP get() const {
Shield<SEXP> call = Rf_lang3(
R_DollarSymbol,
parent,
Rf_ScalarString(field_name)
) ;
return Rcpp_eval( call ) ;
}
void set(SEXP x ){
SEXP dollarGetsSym = Rf_install( "$<-");
Shield<SEXP> name = Rf_ScalarString( field_name ) ;
Shield<SEXP> call = Rf_lang4(
dollarGetsSym,
parent,
name ,
x
);
parent = Rf_eval( call, R_GlobalEnv ) ;
}
} ;
class const_FieldProxy : GenericProxy<const_FieldProxy> {
public:
const_FieldProxy( const CLASS& v, const std::string& name)
: parent(v), field_name(Rf_mkChar(name.c_str())){}
template <typename T> operator T() const {
return as<T>( get() );
}
inline operator SEXP() const {
return get() ;
}
private:
const CLASS& parent;
SEXP field_name ;
SEXP get() const {
Shield<SEXP> call = Rf_lang3(
R_DollarSymbol,
parent,
Rf_ScalarString(field_name)
);
return Rcpp_eval( call ) ;
}
} ;
FieldProxy field(const std::string& name) {
return FieldProxy( static_cast<CLASS&>(*this) , name ) ;
}
const_FieldProxy field(const std::string& name) const {
SEXP x = static_cast<const CLASS&>(*this) ;
if( !Rf_isS4(x) ) throw not_s4() ;
return const_FieldProxy( static_cast<const CLASS&>(*this) , name ) ;
}
} ;
}
#endif