Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #968 with a single argument XPtr constructor #1003

Merged
merged 2 commits into from
Oct 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
2019-10-19 Stephen Wade <stephematician@gmail.com>

* inst/include/Rcpp/XPtr.h: XPtr constructor split up, a single
argument does not modify tags and protected data of the external pointer
* inst/unitTests/cpp/Xptr.cpp: Added test
* inst/unitTests/runit.XPtr.R: Idem

2019-10-14 Dirk Eddelbuettel <edd@debian.org>

* README.md: Added CRAN + BioConductor badges for reverse depends,
Expand Down
13 changes: 11 additions & 2 deletions inst/include/Rcpp/XPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,22 @@ class XPtr :
*
* @param xp external pointer to wrap
*/
explicit XPtr(SEXP x, SEXP tag = R_NilValue, SEXP prot = R_NilValue) {
explicit XPtr(SEXP x) {
if (TYPEOF(x) != EXTPTRSXP) {
const char* fmt = "Expecting an external pointer: [type=%s].";
throw ::Rcpp::not_compatible(fmt, Rf_type2char(TYPEOF(x)));
}

Storage::set__(x);
};

/**
* constructs a XPtr wrapping the external pointer (EXTPTRSXP SEXP)
*
* @param xp external pointer to wrap
* @param tag tag to assign to external pointer
* @param prot protected data to assign to external pointer
*/
explicit XPtr(SEXP x, SEXP tag, SEXP prot) : XPtr(x) {
R_SetExternalPtrTag( x, tag);
R_SetExternalPtrProtected(x, prot);
};
Expand Down
11 changes: 11 additions & 0 deletions inst/unitTests/cpp/XPtr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ int xptr_2( XPtr< std::vector<int> > p){
return p->front() ;
}

// [[Rcpp::export]]
void xptr_self_tag( XPtr< std::vector<int> > p ){
XPtr< std::vector<int> > self_tag(wrap(p), wrap(p), R_NilValue) ;
}

// [[Rcpp::export]]
bool xptr_has_self_tag( XPtr< std::vector<int> > p ){
return wrap(p) == R_ExternalPtrTag(p);
}


// [[Rcpp::export]]
bool xptr_release( XPtr< std::vector<int> > p) {
p.release();
Expand Down
3 changes: 3 additions & 0 deletions inst/unitTests/runit.XPTr.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ if (.runThisTest) {
front <- xptr_2(xp)
checkEquals( front, 1L, msg = "check usage of external pointer" )

xptr_self_tag(xp)
checkEquals(xptr_has_self_tag(xp), T, msg = "check external pointer tag preserved")

checkTrue(xptr_release(xp), msg = "check release of external pointer")

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