-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXPtr.cpp
More file actions
27 lines (22 loc) · 838 Bytes
/
XPtr.cpp
File metadata and controls
27 lines (22 loc) · 838 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <Rcpp.h>
using namespace Rcpp ;
// [[Rcpp::export]]
XPtr< std::vector<int> > xptr_1(){
/* creating a pointer to a vector<int> */
std::vector<int>* v = new std::vector<int> ;
v->push_back( 1 ) ;
v->push_back( 2 ) ;
/* wrap the pointer as an external pointer */
/* this automatically protected the external pointer from R garbage
collection until p goes out of scope. */
XPtr< std::vector<int> > p(v) ;
/* return it back to R, since p goes out of scope after the return
the external pointer is no more protected by p, but it gets
protected by being on the R side */
return( p ) ;
}
// [[Rcpp::export]]
int xptr_2( XPtr< std::vector<int> > p){
/* just return the front of the vector as a SEXP */
return p->front() ;
}