Skip to content

Commit 6668471

Browse files
committed
Merge branch 'feature/pr592'
2 parents b3c79bf + 28a4d00 commit 6668471

File tree

3 files changed

+52
-13
lines changed

3 files changed

+52
-13
lines changed

ChangeLog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* inst/unitTests/runit.environments.R: Added environment find unit tests
44
as well as a symbol access test for environment get.
55
* inst/unitTests/cpp/Environment.cpp: Idem
6+
2016-11-18 James J Balamuta <[email protected]>
7+
* vignettes/Rcpp-extending: Switched to attributes and
8+
added external class pointer macro notes by MathurinD
69

710
2016-11-16 Dirk Eddelbuettel <[email protected]>
811

inst/NEWS.Rd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
\itemize{
99
\item Added Environment::find unit tests and an Environment::get(Symbol)
1010
test (James Balamuta in \ghpr{595} addressing issue \ghit{594}).
11+
\section{Changes in Rcpp version 0.12.8 (2017-01-xx)}{
12+
\item Changes in Rcpp Documentation:
13+
\itemize{
14+
\item Exposed pointers macros were included in the Rcpp Extending vignette
15+
(MathurinD; James Balamuta in \ghpr{592} addressing \ghit{418}).
1116
}
1217
}
1318
\section{Changes in Rcpp version 0.12.8 (2016-11-16)}{

vignettes/Rcpp-extending.Rnw

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,11 @@ These converters are often used implicitly, as in the following code chunk:
9191

9292
<<echo=FALSE>>=
9393
code <- '
94-
// we get a list from R
95-
List input(input_) ;
94+
#include <Rcpp.h>
95+
using namespace Rcpp;
96+
97+
// [[Rcpp::export]]
98+
List fx(List input){ // we get a list from R
9699
97100
// pull std::vector<double> from R list
98101
// this is achieved through an implicit call to Rcpp::as
@@ -103,7 +106,8 @@ std::vector<double> x = input["x"] ;
103106
return List::create(
104107
_["front"] = x.front(),
105108
_["back"] = x.back()
106-
) ;
109+
);
110+
}
107111
'
108112
writeLines( code, "code.cpp" )
109113
@
@@ -112,10 +116,7 @@ external_highlight( "code.cpp", type = "LATEX", doc = FALSE )
112116
@
113117

114118
<<>>=
115-
fx <- cxxfunction( signature( input_ = "list"),
116-
paste( readLines( "code.cpp" ), collapse = "\n" ),
117-
plugin = "Rcpp"
118-
)
119+
Rcpp::sourceCpp(file= "code.cpp")
119120
input <- list( x = seq(1, 10, by = 0.5) )
120121
fx( input )
121122
@
@@ -186,6 +187,18 @@ It should be noted that only the declaration is required. The implementation
186187
can appear after the \texttt{Rcpp.h} file is included, and therefore take
187188
full advantage of the \pkg{Rcpp} type system.
188189

190+
Another non-intrusive option is to expose an external pointer. The macro
191+
\texttt{RCPP\_EXPORT\_WRAP} provides an easy way to expose a \proglang{C++} class
192+
to \proglang{R} as an external pointer. It can be used instead of specializing
193+
\texttt{Rcpp::wrap}, and should not be used simultaneously.
194+
195+
<<lang=cpp>>=
196+
#include RcppCommon.h
197+
#include foobar.h
198+
199+
RCPP_EXPORT_WRAP(Bar);
200+
@
201+
189202
\subsection{Templates and partial specialization}
190203

191204
It is perfectly valid to declare a partial specialization for the
@@ -227,8 +240,6 @@ will attempt to use the constructor of the target class taking a \texttt{SEXP}.
227240
<<lang=cpp>>=
228241
#include <RcppCommon.h>
229242
230-
#include <RcppCommon.h>
231-
232243
class Foo{
233244
public:
234245
Foo() ;
@@ -237,18 +248,16 @@ class Foo{
237248
Foo(SEXP) ;
238249
}
239250
240-
#include <Rcpp.h>
241-
242251
243252
// this must appear after the specialization,
244253
// otherwise the specialization will not be seen by Rcpp types
245254
#include <Rcpp.h>
246255
@
247256

248-
\subsection{Non intrusive extension}
257+
\subsection{Non-intrusive extension}
249258

250259
It is also possible to fully specialize \texttt{Rcpp::as} to enable
251-
non intrusive implicit conversion capabilities.
260+
non-intrusive implicit conversion capabilities.
252261

253262
<<lang=cpp>>=
254263
#include <RcppCommon.h>
@@ -266,6 +275,28 @@ namespace Rcpp {
266275
#include <Rcpp.h>
267276
@
268277

278+
Furthermore, another non-intrusive option is to opt for sharing an R
279+
external pointer. The macro \texttt{RCPP\_EXPORT\_AS} provides an easy way to
280+
extend \texttt{Rcpp::as} to expose \proglang{R} external pointers to
281+
\proglang{C++}. It can be used instead of specializing \texttt{Rcpp::as}, and
282+
should not be used simultaneously.
283+
284+
<<lang=cpp>>=
285+
#include RcppCommon.h
286+
#include foobar.h
287+
288+
RCPP_EXPORT_AS(Bar);
289+
@
290+
291+
With this being said, there is one additional macro that can be used to
292+
simultaneously define both \texttt{Rcpp::wrap} and \texttt{Rcpp::as}
293+
specialization for an external pointer. The macro \texttt{RCPP\_EXPOSED\_CLASS}
294+
can be use to transparently exchange a class between \proglang{R} and
295+
\proglang{C++} as an external pointer. Do not simultaneously use it alongside
296+
\texttt{RCPP\_EXPOSED\_AS}, \texttt{RCPP\_EXPOSED\_WRAP}, \texttt{Rcpp::wrap}, or
297+
\texttt{Rcpp::as}.
298+
299+
269300
\subsection{Templates and partial specialization}
270301

271302
The signature of \texttt{Rcpp::as} does not allow partial specialization.

0 commit comments

Comments
 (0)