Skip to content

Commit bd56309

Browse files
no meat in FieldProxy. #104
1 parent 3f2368d commit bd56309

3 files changed

Lines changed: 59 additions & 92 deletions

File tree

inst/include/Rcpp/api/meat/FieldProxy.h

Lines changed: 0 additions & 78 deletions
This file was deleted.

inst/include/Rcpp/api/meat/meat.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <Rcpp/api/meat/TagProxy.h>
77
#include <Rcpp/api/meat/ProtectedProxy.h>
88
#include <Rcpp/api/meat/StretchyList.h>
9-
#include <Rcpp/api/meat/FieldProxy.h>
109
#include <Rcpp/api/meat/DottedPairProxy.h>
1110
#include <Rcpp/api/meat/PreserveStorage.h>
1211
#include <Rcpp/api/meat/NoProtectStorage.h>

inst/include/Rcpp/proxy/FieldProxy.h

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,84 @@ class FieldProxyPolicy {
1212
FieldProxy( CLASS& v, const std::string& name) :
1313
parent(v), field_name( Rf_mkChar(name.c_str())) {}
1414

15-
FieldProxy& operator=(const FieldProxy& rhs) ;
15+
FieldProxy& operator=(const FieldProxy& rhs){
16+
set( rhs.get() ) ;
17+
return *this ;
18+
}
1619

17-
template <typename T> FieldProxy& operator=(const T& rhs) ;
20+
template <typename T> FieldProxy& operator=(const T& rhs) {
21+
set( wrap( rhs ) ) ;
22+
return *this ;
23+
}
1824

19-
template <typename T> operator T() const ;
20-
inline operator SEXP() const { return get() ; }
25+
template <typename T> operator T() const {
26+
return as<T>(get()) ;
27+
}
28+
29+
inline operator SEXP() const {
30+
return get() ;
31+
}
2132

2233
private:
2334
CLASS& parent;
2435
SEXP field_name ;
2536

26-
SEXP get() const ;
27-
void set(SEXP x ) ;
37+
SEXP get() const {
38+
Shield<SEXP> call = Rf_lang3(
39+
R_DollarSymbol,
40+
parent,
41+
Rf_ScalarString(field_name)
42+
) ;
43+
return Rcpp_eval( call ) ;
44+
}
45+
void set(SEXP x ){
46+
SEXP dollarGetsSym = Rf_install( "$<-");
47+
Shield<SEXP> name = Rf_ScalarString( field_name ) ;
48+
Shield<SEXP> call = Rf_lang4(
49+
dollarGetsSym,
50+
parent,
51+
name ,
52+
x
53+
);
54+
parent = Rf_eval( call, R_GlobalEnv ) ;
55+
}
2856
} ;
2957

3058
class const_FieldProxy : GenericProxy<const_FieldProxy> {
3159
public:
32-
const_FieldProxy( const CLASS& v, const std::string& name) ;
33-
const_FieldProxy& operator=(const const_FieldProxy& rhs) ;
60+
const_FieldProxy( const CLASS& v, const std::string& name)
61+
: parent(v), field_name(Rf_mkChar(name.c_str())){}
3462

35-
template <typename T> operator T() const ;
36-
inline operator SEXP() const { return get() ; }
63+
template <typename T> operator T() const {
64+
return as<T>( get() );
65+
}
66+
inline operator SEXP() const {
67+
return get() ;
68+
}
3769

3870
private:
3971
const CLASS& parent;
4072
SEXP field_name ;
4173

42-
SEXP get() const ;
74+
SEXP get() const {
75+
Shield<SEXP> call = Rf_lang3(
76+
R_DollarSymbol,
77+
parent,
78+
Rf_ScalarString(field_name)
79+
);
80+
return Rcpp_eval( call ) ;
81+
}
82+
4383
} ;
4484

45-
FieldProxy field(const std::string& name) ;
46-
const_FieldProxy field(const std::string& name) const ;
85+
FieldProxy field(const std::string& name) {
86+
return FieldProxy( static_cast<CLASS&>(*this) , name ) ;
87+
}
88+
const_FieldProxy field(const std::string& name) const {
89+
SEXP x = static_cast<const CLASS&>(*this) ;
90+
if( !Rf_isS4(x) ) throw not_s4() ;
91+
return const_FieldProxy( static_cast<const CLASS&>(*this) , name ) ;
92+
}
4793

4894
} ;
4995

0 commit comments

Comments
 (0)