3 # Copyright (C) 2009 - 2010 Romain Francois
5 # This file is part of Rcpp.
7 # Rcpp is free software: you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 2 of the License, or
10 # (at your option) any later version.
12 # Rcpp is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
24 ## NOTE: This is the old way to compile Rcpp code inline.
25 ## The code here has left as a historical artifact and tribute to the old way.
26 ## Please use the code under the "new" inline compilation section.
28 funx_old <- cxxfunction(signature(), '
29 /* creating a pointer to a vector<int> */
30 std::vector<int>* v = new std::vector<int> ;
34 /* wrap the pointer as an external pointer */
35 /* this automatically protected the external pointer from R garbage
36 collection until p goes out of scope. */
37 Rcpp::XPtr< std::vector<int> > p(v) ;
39 /* return it back to R, since p goes out of scope after the return
40 the external pointer is no more protected by p, but it gets
41 protected by being on the R side */
45 stopifnot( identical( typeof( xp ), "externalptr" ) )
47 # passing the pointer back to C++
48 funx_old <- cxxfunction(signature(x = "externalptr" ), '
49 /* wrapping x as smart external pointer */
50 /* The SEXP based constructor does not protect the SEXP from
51 garbage collection automatically, it is already protected
52 because it comes from the R side, however if you want to keep
53 the Rcpp::XPtr object on the C(++) side
54 and return something else to R, you need to protect the external
55 pointer, by using the protect member function */
56 Rcpp::XPtr< std::vector<int> > p(x) ;
58 /* just return the front of the vector as a SEXP */
59 return( Rcpp::wrap( p->front() ) ) ;
62 stopifnot( identical( front, 1L ) )
65 ## NOTE: Within this section, the new way to compile Rcpp code inline has been
66 ## written. Please use the code next as a template for your own project.
68 ## Use of the cppFunction() gives the ability to immediately compile embedded
69 ## C++ directly within R without having to worry about header specification or
73 Rcpp::XPtr< std::vector<int> > funx(){
74 /* creating a pointer to a vector<int> */
75 std::vector<int>* v = new std::vector<int> ;
79 /* wrap the pointer as an external pointer */
80 /* this automatically protected the external pointer from R garbage
81 * collection until p goes out of scope.
83 Rcpp::XPtr< std::vector<int> > p(v) ;
85 /* return it back to R, since p goes out of scope after the return
86 * the external pointer is no more protected by p, but it gets
87 * protected by being on the R side
93 stopifnot( identical( typeof( xp ), "externalptr" ) )
95 # passing the pointer back to C++
97 SEXP funx_pt(Rcpp::XPtr< std::vector<int> > p){
98 /* Wrapping x as smart external pointer */
100 /* The SEXP based constructor does not protect the SEXP from
101 * garbage collection automatically, it is already protected
102 * because it comes from the R side, however if you want to keep
103 * the Rcpp::XPtr object on the C(++) side
104 * and return something else to R, you need to protect the external
105 * pointer, by using the protect member function
108 /* Just return the front of the vector as a SEXP */
109 return Rcpp::wrap(p->front());
112 stopifnot( identical( front, 1L ) )