Skip to content

Commit 25bf2e6

Browse files
fix #968 with a single argument XPtr constructor
1 parent 7a6aa01 commit 25bf2e6

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

inst/include/Rcpp/XPtr.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,22 @@ class XPtr :
6363
*
6464
* @param xp external pointer to wrap
6565
*/
66-
explicit XPtr(SEXP x, SEXP tag = R_NilValue, SEXP prot = R_NilValue) {
66+
explicit XPtr(SEXP x) {
6767
if (TYPEOF(x) != EXTPTRSXP) {
6868
const char* fmt = "Expecting an external pointer: [type=%s].";
6969
throw ::Rcpp::not_compatible(fmt, Rf_type2char(TYPEOF(x)));
7070
}
71-
7271
Storage::set__(x);
72+
};
73+
74+
/**
75+
* constructs a XPtr wrapping the external pointer (EXTPTRSXP SEXP)
76+
*
77+
* @param xp external pointer to wrap
78+
* @param tag tag to assign to external pointer
79+
* @param prot protected data to assign to external pointer
80+
*/
81+
explicit XPtr(SEXP x, SEXP tag, SEXP prot) : XPtr(x) {
7382
R_SetExternalPtrTag( x, tag);
7483
R_SetExternalPtrProtected(x, prot);
7584
};

inst/unitTests/cpp/XPtr.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ int xptr_2( XPtr< std::vector<int> > p){
4646
return p->front() ;
4747
}
4848

49+
// [[Rcpp::export]]
50+
void xptr_self_tag( XPtr< std::vector<int> > p ){
51+
XPtr< std::vector<int> > self_tag(wrap(p), wrap(p), R_NilValue) ;
52+
}
53+
54+
// [[Rcpp::export]]
55+
bool xptr_has_self_tag( XPtr< std::vector<int> > p ){
56+
return wrap(p) == R_ExternalPtrTag(p);
57+
}
58+
59+
4960
// [[Rcpp::export]]
5061
bool xptr_release( XPtr< std::vector<int> > p) {
5162
p.release();

inst/unitTests/runit.XPTr.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ if (.runThisTest) {
3131
front <- xptr_2(xp)
3232
checkEquals( front, 1L, msg = "check usage of external pointer" )
3333

34+
xptr_self_tag(xp)
35+
checkEquals(xptr_has_self_tag(xp), T, msg = "check external pointer tag preserved")
36+
3437
checkTrue(xptr_release(xp), msg = "check release of external pointer")
3538

3639
checkTrue(xptr_access_released(xp), msg = "check access of released external pointer")

0 commit comments

Comments
 (0)