Skip to content

Commit 5287dba

Browse files
rstubeddelbuettel
authored andcommitted
Describe RCPP_EXPOSED_* macros in modules vignette (#959)
1 parent c500870 commit 5287dba

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

vignettes/Rcpp-extending.Rmd

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,13 @@ full advantage of the \pkg{Rcpp} type system.
204204
Another non-intrusive option is to expose an external pointer. The macro
205205
`RCPP_EXPOSED_WRAP` provides an easy way to expose a \proglang{C++} class
206206
to \proglang{R} as an external pointer. It can be used instead of specializing
207-
`Rcpp::wrap`, and should not be used simultaneously.
207+
`Rcpp::wrap`, and should not be used simultaneously. Note that the
208+
\proglang{C++} class has to use Rcpp modules. See the Rcpp modules vignette for
209+
more details.
208210

209211
```{Rcpp, eval = FALSE}
210-
#include RcppCommon.h
211-
#include foobar.h
212+
#include <Rcpp.h>
213+
#include <foobar.h>
212214
213215
RCPP_EXPOSED_WRAP(Bar)
214216
```
@@ -295,11 +297,12 @@ Furthermore, another non-intrusive option is to opt for sharing an R
295297
external pointer. The macro `RCPP_EXPOSED_AS` provides an easy way to
296298
extend `Rcpp::as` to expose \proglang{R} external pointers to
297299
\proglang{C++}. It can be used instead of specializing `Rcpp::as`, and
298-
should not be used simultaneously.
300+
should not be used simultaneously. Note that the \proglang{C++} class
301+
has to use Rcpp modules. See the Rcpp modules vignette for more details.
299302

300303
```{Rcpp, eval = FALSE}
301-
#include RcppCommon.h
302-
#include foobar.h
304+
#include <Rcpp.h>
305+
#include <foobar.h>
303306
304307
RCPP_EXPOSED_AS(Bar)
305308
```

vignettes/Rcpp-modules.Rmd

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,58 @@ setMethod("show", yada$World , function(object) {
942942

943943
TODO: mention R inheritance (John ?)
944944

945+
### Extending `Rcpp::as` and `Rcpp::wrap`
946+
947+
Sometimes it is necessary to extend `Rcpp::as` or `Rcpp::wrap` for
948+
classes that are also exposed using Rcpp modules. Instead of using the
949+
general methods described in the _Rcpp Extending_ vignette, one can
950+
use the `RCPP_EXPOSED_AS` or `RCPP_EXPOSED_WRAP` macros.
951+
Alternatively the `RCPP_EXPOSED_CLASS` macro defines both `Rcpp::as`
952+
and `Rcpp::wrap` specializations. Do not use these macros together
953+
with the generic extension mechanisms. Note that opposesd to the
954+
generic methods, these macros can be used _after_ `Rcpp.h` has been
955+
loaded. Here an example of a pair of Rcpp modules exposed classes
956+
where one of them has a method taking an instance of the other class
957+
as argument. In this case it is suffcient to use `RCPP_EXPOSED_AS` to
958+
enable the transparent conversion from \proglang{R} to \proglang{C++}:
959+
960+
```cpp
961+
#include <Rcpp.h>
962+
963+
class Foo {
964+
public:
965+
Foo() = default;
966+
};
967+
968+
class Bar {
969+
public:
970+
Bar() = default;
971+
void handleFoo(Foo foo) {
972+
Rcpp::Rcout << "Got a Foo!" << std::endl;
973+
};
974+
};
975+
976+
RCPP_EXPOSED_AS(Foo)
977+
978+
RCPP_MODULE(Foo){
979+
Rcpp::class_<Foo>("Foo")
980+
.constructor();
981+
}
982+
983+
RCPP_MODULE(Barl){
984+
Rcpp::class_<Bar>("Bar")
985+
.constructor()
986+
.method("handleFoo", &Bar::handleFoo);
987+
}
988+
```
989+
990+
```{r, eval=FALSE}
991+
foo <- new(Foo)
992+
bar <- new(Bar)
993+
bar$handleFoo(foo)
994+
#> Got a Foo!
995+
```
996+
945997
### Full example
946998

947999
<!-- TODO: maybe replace this by something from wls or RcppModels ? -->

0 commit comments

Comments
 (0)