Skip to content

Commit b791b37

Browse files
resolving ambiguity
1 parent 37d306b commit b791b37

File tree

3 files changed

+41
-62
lines changed

3 files changed

+41
-62
lines changed

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

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,6 @@
2020

2121
namespace Rcpp{
2222

23-
template <typename CLASS>
24-
typename SlotProxyPolicy<CLASS>::SlotProxy& SlotProxyPolicy<CLASS>::SlotProxy::operator=( const SlotProxy& rhs ){
25-
set( rhs.get() ) ;
26-
return *this ;
27-
}
28-
29-
template <typename CLASS>
30-
SEXP SlotProxyPolicy<CLASS>::SlotProxy::get() const {
31-
return R_do_slot( parent, slot_name ) ;
32-
}
33-
34-
template <typename CLASS>
35-
void SlotProxyPolicy<CLASS>::SlotProxy::set( SEXP x) {
36-
parent = R_do_slot_assign(parent, slot_name, x);
37-
}
38-
39-
template <typename CLASS>
40-
typename SlotProxyPolicy<CLASS>::SlotProxy SlotProxyPolicy<CLASS>::slot(const std::string& name) {
41-
SEXP x = static_cast<CLASS&>(*this) ;
42-
if( !Rf_isS4(x) ) throw not_s4() ;
43-
return SlotProxy( static_cast<CLASS&>(*this) , name ) ;
44-
}
45-
4623
template <typename CLASS>
4724
template <typename T>
4825
typename SlotProxyPolicy<CLASS>::SlotProxy& SlotProxyPolicy<CLASS>::SlotProxy::operator=( const T& rhs ){
@@ -56,36 +33,11 @@ namespace Rcpp{
5633
return as<T>(get()) ;
5734
}
5835

59-
60-
6136
template <typename CLASS>
62-
SlotProxyPolicy<CLASS>::const_SlotProxy::const_SlotProxy( const CLASS& v, const std::string& name)
63-
: parent(v), slot_name(Rf_install(name.c_str()))
64-
{
65-
if( !R_has_slot( v, slot_name) ){
66-
throw no_such_slot() ;
67-
}
68-
}
69-
70-
template <typename CLASS>
71-
SEXP SlotProxyPolicy<CLASS>::const_SlotProxy::get() const {
72-
return R_do_slot( parent, slot_name ) ;
73-
}
74-
75-
template <typename CLASS>
76-
typename SlotProxyPolicy<CLASS>::const_SlotProxy SlotProxyPolicy<CLASS>::slot(const std::string& name) const {
77-
SEXP x = static_cast<const CLASS&>(*this) ;
78-
if( !Rf_isS4(x) ) throw not_s4() ;
79-
return const_SlotProxy( static_cast<const CLASS&>(*this) , name ) ;
37+
template <typename T>
38+
SlotProxyPolicy<CLASS>::const_SlotProxy::operator T() const {
39+
return as<T>(get()) ;
8040
}
8141

82-
template <typename CLASS>
83-
bool SlotProxyPolicy<CLASS>::hasSlot(const std::string& name) const {
84-
SEXP x = static_cast<const CLASS&>(*this).get__() ;
85-
if( !Rf_isS4(x) ) throw not_s4() ;
86-
return R_has_slot( x, Rf_mkString(name.c_str()) ) ;
87-
}
88-
89-
9042
}
9143
#endif

inst/include/Rcpp/proxy/SlotProxy.h

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,28 @@ class SlotProxyPolicy {
3232
}
3333
}
3434

35-
SlotProxy& operator=(const SlotProxy& rhs) ;
35+
SlotProxy& operator=(const SlotProxy& rhs){
36+
set( rhs.get() ) ;
37+
return *this ;
38+
}
3639

3740
template <typename T> SlotProxy& operator=(const T& rhs) ;
3841

3942
template <typename T> operator T() const ;
40-
inline operator SEXP() const { return get() ; }
43+
inline operator SEXP() const {
44+
return get() ;
45+
}
4146

4247
private:
4348
CLASS& parent;
4449
SEXP slot_name ;
4550

46-
SEXP get() const ;
47-
void set(SEXP x ) ;
51+
SEXP get() const {
52+
return R_do_slot( parent, slot_name ) ;
53+
}
54+
void set(SEXP x ) {
55+
parent = R_do_slot_assign(parent, slot_name, x);
56+
}
4857
} ;
4958

5059
class const_SlotProxy {
@@ -53,19 +62,35 @@ class SlotProxyPolicy {
5362
const_SlotProxy& operator=(const const_SlotProxy& rhs) ;
5463

5564
template <typename T> operator T() const ;
56-
inline operator SEXP() const { return get() ; }
65+
inline operator SEXP() const {
66+
return get() ;
67+
}
5768

5869
private:
5970
const CLASS& parent;
6071
SEXP slot_name ;
6172

62-
SEXP get() const ;
73+
SEXP get() const {
74+
return R_do_slot( parent, slot_name ) ;
75+
}
6376
} ;
6477

65-
SlotProxy slot(const std::string& name) ;
66-
const_SlotProxy slot(const std::string& name) const ;
78+
SlotProxy slot(const std::string& name){
79+
SEXP x = static_cast<CLASS&>(*this) ;
80+
if( !Rf_isS4(x) ) throw not_s4() ;
81+
return SlotProxy( static_cast<CLASS&>(*this) , name ) ;
82+
}
83+
const_SlotProxy slot(const std::string& name) const {
84+
SEXP x = static_cast<const CLASS&>(*this) ;
85+
if( !Rf_isS4(x) ) throw not_s4() ;
86+
return const_SlotProxy( static_cast<const CLASS&>(*this) , name ) ;
87+
}
6788

68-
bool hasSlot(const std::string& name) const ;
89+
bool hasSlot(const std::string& name) const{
90+
SEXP x = static_cast<const CLASS&>(*this).get__() ;
91+
if( !Rf_isS4(x) ) throw not_s4() ;
92+
return R_has_slot( x, Rf_mkString(name.c_str()) ) ;
93+
}
6994

7095
} ;
7196

inst/unitTests/cpp/S4.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,14 @@ bool S4_is_trackCurve(S4 tr){
6666

6767
// [[Rcpp::export]]
6868
NumericVector S4_get_slot_x(S4 o){
69-
return NumericVector(o.slot("x")) ;
69+
NumericVector res = o.slot("x") ;
70+
return res ;
7071
}
7172

7273
// [[Rcpp::export]]
7374
CharacterVector S4_get_attr_x(IntegerVector o){
74-
return CharacterVector(o.attr("foo")) ;
75+
CharacterVector res = o.attr("foo") ;
76+
return res ;
7577
}
7678

7779
// [[Rcpp::export]]

0 commit comments

Comments
 (0)