Skip to content

Commit 706b699

Browse files
FieldProxy was incomplete
1 parent 5ce0b4c commit 706b699

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <Rcpp/api/meat/NamesProxy.h>
3131
#include <Rcpp/api/meat/DottedPairProxy.h>
3232
#include <Rcpp/api/meat/SlotProxy.h>
33+
#include <Rcpp/api/meat/FieldProxy.h>
3334

3435
#include <Rcpp/api/meat/Date.h>
3536
#include <Rcpp/api/meat/Datetime.h>

inst/include/Rcpp/proxy/FieldProxy.h

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,40 +27,61 @@ class FieldProxyPolicy {
2727
class FieldProxy {
2828
public:
2929
FieldProxy( CLASS& v, const std::string& name) :
30-
parent(v), field_name( Rf_mkChar(name.c_str())) {}
30+
parent(v), field_name(name) {}
3131

32-
FieldProxy& operator=(const FieldProxy& rhs) ;
32+
FieldProxy& operator=(const FieldProxy& rhs){
33+
if( this != &rhs ) set( rhs.get() ) ;
34+
return *this ;
35+
}
3336

3437
template <typename T> FieldProxy& operator=(const T& rhs) ;
3538

3639
template <typename T> operator T() const ;
37-
inline operator SEXP() const { return get() ; }
40+
inline operator SEXP() const {
41+
return get() ;
42+
}
3843

3944
private:
4045
CLASS& parent;
41-
SEXP field_name ;
46+
const std::string& field_name ;
4247

43-
SEXP get() const ;
44-
void set(SEXP x ) ;
48+
SEXP get() const {
49+
Shield<SEXP> call( Rf_lang3( R_DollarSymbol, parent, Rf_mkString(field_name.c_str()) ) ) ;
50+
return Rf_eval( call, R_GlobalEnv ) ;
51+
}
52+
void set(SEXP x ) {
53+
SEXP dollarGetsSym = Rf_install( "$<-");
54+
Shield<SEXP> call( Rf_lang4( dollarGetsSym, parent, Rf_mkString(field_name.c_str()) , x ) ) ;
55+
parent.set__( Rf_eval( call, R_GlobalEnv ) );
56+
}
4557
} ;
4658

4759
class const_FieldProxy {
4860
public:
49-
const_FieldProxy( const CLASS& v, const std::string& name) ;
50-
const_FieldProxy& operator=(const const_FieldProxy& rhs) ;
61+
const_FieldProxy( const CLASS& v, const std::string& name) :
62+
parent(v), field_name(name) {}
5163

5264
template <typename T> operator T() const ;
53-
inline operator SEXP() const { return get() ; }
65+
inline operator SEXP() const {
66+
return get() ;
67+
}
5468

5569
private:
5670
const CLASS& parent;
57-
SEXP field_name ;
71+
const std::string& field_name ;
5872

59-
SEXP get() const ;
73+
SEXP get() const {
74+
Shield<SEXP> call( Rf_lang3( R_DollarSymbol, parent, Rf_mkString(field_name.c_str()) ) ) ;
75+
return Rf_eval( call, R_GlobalEnv ) ;
76+
}
6077
} ;
6178

62-
FieldProxy field(const std::string& name) ;
63-
const_FieldProxy field(const std::string& name) const ;
79+
FieldProxy field(const std::string& name){
80+
return FieldProxy( static_cast<CLASS&>(*this), name ) ;
81+
}
82+
const_FieldProxy field(const std::string& name) const {
83+
return const_FieldProxy( static_cast<const CLASS&>(*this), name ) ;
84+
}
6485

6586
} ;
6687

0 commit comments

Comments
 (0)