Skip to content

Commit 85250ec

Browse files
committed
Finish meat reintroduction for proxies; add tests (#165)
1 parent dbed32f commit 85250ec

File tree

5 files changed

+80
-21
lines changed

5 files changed

+80
-21
lines changed

ChangeLog

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2014-09-02 Kevin Ushey <[email protected]>
2+
3+
* inst/include/Rcpp/api/meat/proxy.h: Finish meat reintroduction + tests
4+
* inst/unitTests/runit.wrap.R: Idem
5+
* inst/unitTests/cpp/wrap.cpp: Idem
6+
* inst/include/Rcpp/proxy/FieldProxy.h: Idem
7+
18
2014-08-13 Kevin Ushey <[email protected]>
29

310
* inst/include/Rcpp/api/meat/meat.h: Reintroduce meat

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ namespace Rcpp {
154154
return *this;
155155
}
156156

157+
template <typename CLASS>
158+
template <typename T>
159+
typename DottedPairProxyPolicy<CLASS>::DottedPairProxy&
160+
DottedPairProxyPolicy<CLASS>::DottedPairProxy::operator=(const traits::named_object<T>& rhs) {
161+
return set(wrap(rhs.object), rhs.name);
162+
}
163+
157164
template <typename CLASS>
158165
template <typename T>
159166
DottedPairProxyPolicy<CLASS>::DottedPairProxy::operator T() const {
@@ -166,6 +173,36 @@ namespace Rcpp {
166173
return as<T>(get());
167174
}
168175

176+
// Field proxies
177+
template <typename CLASS>
178+
typename FieldProxyPolicy<CLASS>::FieldProxy&
179+
FieldProxyPolicy<CLASS>::FieldProxy::operator=(const FieldProxyPolicy<CLASS>::FieldProxy& rhs) {
180+
if (this != &rhs) set(rhs.get());
181+
return *this;
182+
}
183+
184+
template <typename CLASS>
185+
template <typename T>
186+
typename FieldProxyPolicy<CLASS>::FieldProxy&
187+
FieldProxyPolicy<CLASS>::FieldProxy::operator=(const T& rhs) {
188+
SEXP tmp = PROTECT(wrap(rhs));
189+
set(tmp);
190+
UNPROTECT(1);
191+
return *this;
192+
}
193+
194+
template <typename CLASS>
195+
template <typename T>
196+
FieldProxyPolicy<CLASS>::FieldProxy::operator T() const {
197+
return as<T>(get());
198+
}
199+
200+
template <typename CLASS>
201+
template <typename T>
202+
FieldProxyPolicy<CLASS>::const_FieldProxy::operator T() const {
203+
return as<T>(get());
204+
}
205+
169206
}
170207

171208
#endif

inst/include/Rcpp/proxy/FieldProxy.h

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,12 @@ class FieldProxyPolicy {
2929
FieldProxy( CLASS& v, const std::string& name) :
3030
parent(v), field_name(name) {}
3131

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

37-
template <typename T> FieldProxy& operator=(const T& rhs) {
38-
SEXP tmp = PROTECT( wrap(rhs) );
39-
set(tmp);
40-
UNPROTECT(1);
41-
return *this;
42-
}
34+
template <typename T> FieldProxy& operator=(const T& rhs);
4335

44-
template <typename T> operator T() const {
45-
return as<T>( get() );
46-
}
47-
inline operator SEXP() const {
48-
return get() ;
49-
}
36+
template <typename T> operator T() const;
37+
inline operator SEXP() const { return get(); }
5038

5139
private:
5240
CLASS& parent;
@@ -68,9 +56,7 @@ class FieldProxyPolicy {
6856
const_FieldProxy( const CLASS& v, const std::string& name) :
6957
parent(v), field_name(name) {}
7058

71-
template <typename T> operator T() const {
72-
return as<T>( get() );
73-
}
59+
template <typename T> operator T() const;
7460
inline operator SEXP() const {
7561
return get() ;
7662
}

inst/unitTests/cpp/wrap.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,34 @@
1919
// You should have received a copy of the GNU General Public License
2020
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
2121

22+
// Testing 'wrap', 'as' with custom class -- see
23+
// https://github.com/RcppCore/Rcpp/issues/165
24+
template<typename T>
25+
class Bar {
26+
public:
27+
Bar(T t) : t(t) {}
28+
~Bar() {}
29+
T t;
30+
};
31+
32+
#include <RcppCommon.h>
33+
namespace Rcpp {
34+
template<typename T> SEXP wrap(const Bar<T> &b);
35+
}
36+
2237
#include <Rcpp.h>
23-
using namespace Rcpp ;
38+
39+
template<typename T> SEXP Rcpp::wrap(const Bar<T> &b) {
40+
return Rcpp::wrap(b.t);
41+
}
42+
43+
// [[Rcpp::export]]
44+
Bar<int> test_wrap_custom_class() {
45+
Bar<int> b(42);
46+
return b;
47+
}
48+
49+
using namespace Rcpp;
2450

2551
// [[Rcpp::export]]
2652
IntegerVector map_string_int(){
@@ -255,4 +281,3 @@ SEXP vector_Foo(){
255281
vec[1] = Foo( 3 ) ;
256282
return wrap(vec) ;
257283
}
258-

inst/unitTests/runit.wrap.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,5 +183,9 @@ if (.runThisTest) {
183183
)
184184
}
185185

186+
test.wrap.custom.class <- function() {
187+
checkEquals(test_wrap_custom_class(), 42)
188+
}
189+
186190
}
187191

0 commit comments

Comments
 (0)