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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
---
title: \pkg{Rcpp} Extending
# Use letters for affiliations
author:
- name: Dirk Eddelbuettel
affiliation: a
- name: Romain François
affiliation: b
address:
- code: a
address: \url{http://dirk.eddelbuettel.com}
- code: b
address: \url{https://romain.rbind.io/}
# For footer text
lead_author_surname: Eddelbuettel and François
# Place DOI URL or CRAN Package URL here
doi: "https://cran.r-project.org/package=Rcpp"
# Abstract
abstract: |
This note provides an overview of the steps programmers should follow to
extend \pkg{Rcpp} \citep{CRAN:Rcpp,JSS:Rcpp} for use with their own classes. This document
is based on our experience in extending \pkg{Rcpp} to work with the
\pkg{Armadillo} \citep{Sanderson:2010:Armadillo} classes, available in the separate package
\pkg{RcppArmadillo} \citep{CRAN:RcppArmadillo}. This document assumes
knowledge of \pkg{Rcpp} as well as some knowledge of \proglang{C++}
templates \citep{Abrahams+Gurtovoy:2004:TemplateMetaprogramming}.
# Optional: Acknowledgements
# acknowledgements: |
# Optional: One or more keywords
keywords:
- Rcpp
- extending
- R
- C++
# Font size of the document, values of 9pt (default), 10pt, 11pt and 12pt
fontsize: 9pt
# Optional: Force one-column layout, default is two-column
#one_column: true
# Optional: Enables lineo mode, but only if one_column mode is also true
#lineno: true
# Optional: Enable one-sided layout, default is two-sided
#one_sided: true
# Optional: Enable section numbering, default is unnumbered
numbersections: true
# Optional: Specify the depth of section number, default is 5
#secnumdepth: 5
# Optional: Bibliography
bibliography: Rcpp
# Optional: Enable a 'Draft' watermark on the document
#watermark: false
# Customize footer, eg by referencing the vignette
footer_contents: "Rcpp Vignette"
# Omit \pnasbreak at end
skip_final_break: true
# Produce a pinp document
output:
pinp::pinp:
collapse: true
header-includes: >
\newcommand{\proglang}[1]{\textsf{#1}}
\newcommand{\pkg}[1]{\textbf{#1}}
vignette: >
%\VignetteIndexEntry{Rcpp-extending}
%\VignetteKeywords{Rcpp, extending, R, Cpp}
%\VignettePackage{Rcpp}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
# Introduction
\pkg{Rcpp} facilitates data interchange between \proglang{R} and
\proglang{C++} through the templated functions `Rcpp::as` (for
conversion of objects from \proglang{R} to \proglang{C++}) and
`Rcpp::wrap` (for conversion from \proglang{C++} to \proglang{R}). In
other words, we convert between the so-called \proglang{S}-expression
pointers (in type `SEXP`) to a templated \proglang{C++} type, and vice
versa. The corresponding function declarations are as follows:
```{Rcpp, eval = FALSE}
// conversion from R to C++
template <typename T> T as(SEXP x);
// conversion from C++ to R
template <typename T> SEXP wrap(const T& object);
```
These converters are often used implicitly, as in the following code chunk:
```{Rcpp}
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
List fx(List input) { // we get a list from R
// pull std::vector<double> from R list
// this is achieved through an implicit
// call to Rcpp::as
std::vector<double> x = input["x"];
// return an R list; this is achieved
// through an implicit call to Rcpp::wrap
return List::create(_["front"] = x.front(),
_["back"] = x.back());
}
```
Example:
```{r}
# Run sourceCpp compilation to include file
# Rcpp::sourceCpp(file= "code.cpp")
input <- list( x = seq(1, 10, by = 0.5) )
fx(input)
```
The \pkg{Rcpp} converter function `Rcpp::as` and `Rcpp::wrap` have been
designed to be extensible to user-defined types and third-party types.
# Extending `Rcpp::wrap`
The \pkg{Rcpp::wrap} converter is extensible in essentially two ways : intrusive
and non-intrusive.
## Intrusive extension
When extending \pkg{Rcpp} with your own data type, the recommended way is to
implement a conversion to `SEXP`. This lets `Rcpp::wrap` know
about the new data type. The template meta programming (or TMP) dispatch is able to
recognize that a type is convertible to a `SEXP` and
`Rcpp::wrap` will use that conversion.
The caveat is that the type must be declared before the main header
file `Rcpp.h` is included.
```{Rcpp, eval = FALSE}
#include <RcppCommon.h>
class Foo {
public:
Foo();
// this operator enables implicit Rcpp::wrap
operator SEXP();
}
#include <Rcpp.h>
```
This is called \emph{intrusive} because the conversion to `SEXP`
operator has to be declared within the class.
## Non-intrusive extension
It is often desirable to offer automatic conversion to third-party types, over
which the developer has no control and can therefore not include a conversion
to `SEXP` operator in the class definition.
To provide automatic conversion from \proglang{C++} to \proglang{R}, one must
declare a specialization of the `Rcpp::wrap` template between the
includes of `RcppCommon.h` and `Rcpp.h`.
```{Rcpp, eval = FALSE}
#include <RcppCommon.h>
// third party library that declares class Bar
#include <foobar.h>
// declaring the specialization
namespace Rcpp {
template <> SEXP wrap(const Bar&);
}
// this must appear after the specialization,
// otherwise the specialization will not be
// seen by Rcpp types
#include <Rcpp.h>
```
It should be noted that only the declaration is required. The implementation
can appear after the `Rcpp.h` file is included, and therefore take
full advantage of the \pkg{Rcpp} type system.
Another non-intrusive option is to expose an external pointer. The macro
`RCPP_EXPOSED_WRAP` provides an easy way to expose a \proglang{C++} class
to \proglang{R} as an external pointer. It can be used instead of specializing
`Rcpp::wrap`, and should not be used simultaneously.
```{Rcpp, eval = FALSE}
#include RcppCommon.h
#include foobar.h
RCPP_EXPOSED_WRAP(Bar)
```
## Templates and partial specialization
It is perfectly valid to declare a partial specialization for the
`Rcpp::wrap` template. The compiler will identify the appropriate
overload:
```{Rcpp, eval = FALSE}
#include <RcppCommon.h>
// third party library that declares
// a template class Bling<T>
#include <foobar.h>
// declaring the partial specialization
namespace Rcpp {
namespace traits {
template <typename T>
SEXP wrap(const Bling<T>&);
}
}
// this must appear after the specialization, or
// specialization will not be seen by Rcpp types
#include <Rcpp.h>
```
# Extending `Rcpp::as`
Conversion from \proglang{R} to \proglang{C++} is also possible
in both intrusive and non-intrusive ways.
## Intrusive extension
As part of its template meta programming dispatch logic, `Rcpp::as`
will attempt to use the constructor of the target class taking a `SEXP`.
```{Rcpp, eval = FALSE}
#include <RcppCommon.h>
class Foo{
public:
Foo();
// this ctor enables implicit Rcpp::as
Foo(SEXP);
}
// this must appear after the specialization, or
// specialization will not be seen by Rcpp types
#include <Rcpp.h>
```
## Non-intrusive extension
It is also possible to fully specialize `Rcpp::as` to enable
non-intrusive implicit conversion capabilities.
```{Rcpp, eval = FALSE}
#include <RcppCommon.h>
// third party library that declares class Bar
#include <foobar.h>
// declaring the specialization
namespace Rcpp {
template <> Bar as(SEXP);
}
// this must appear after the specialization, or
// specialization will not be seen by Rcpp types
#include <Rcpp.h>
```
Furthermore, another non-intrusive option is to opt for sharing an R
external pointer. The macro `RCPP_EXPOSED_AS` provides an easy way to
extend `Rcpp::as` to expose \proglang{R} external pointers to
\proglang{C++}. It can be used instead of specializing `Rcpp::as`, and
should not be used simultaneously.
```{Rcpp, eval = FALSE}
#include RcppCommon.h
#include foobar.h
RCPP_EXPOSED_AS(Bar)
```
With this being said, there is one additional macro that can be used to
simultaneously define both `Rcpp::wrap` and `Rcpp::as`
specialization for an external pointer. The macro `RCPP_EXPOSED_CLASS`
can be use to transparently exchange a class between \proglang{R} and
\proglang{C++} as an external pointer. Do not simultaneously use it alongside
`RCPP_EXPOSED_AS`, `RCPP_EXPOSED_WRAP`, `Rcpp::wrap`, or
`Rcpp::as`.
## Templates and partial specialization
The signature of `Rcpp::as` does not allow partial specialization.
When exposing a templated class to `Rcpp::as`, the programmer must
specialize the \pkg{Rcpp::traits::Exporter} template class. The TMP dispatch
will recognize that a specialization of `Exporter` is available
and delegate the conversion to this class. \pkg{Rcpp} defines
the `Rcpp::traits::Exporter` template class as follows :
```{Rcpp, eval = FALSE}
namespace Rcpp {
namespace traits {
template <typename T> class Exporter{
public:
Exporter(SEXP x) : t(x){}
inline T get() { return t; }
private:
T t;
};
}
}
```
This is the reason why the default behavior of `Rcpp::as` is to
invoke the constructor of the type `T` taking a `SEXP`.
Since partial specialization of class templates is allowed, we can expose
a set of classes as follows:
```{Rcpp, eval = FALSE}
#include <RcppCommon.h>
// third party library that declares
// a template class Bling<T>
#include <foobar.h>
// declaring the partial specialization
namespace Rcpp {
namespace traits {
template <typename T>
class Exporter< Bling<T> >;
}
}
// this must appear after the specialization, or
// specialization will not be seen by Rcpp types
#include <Rcpp.h>
```
Using this approach, the requirements for the
`Exporter< Bling<T> >` class are:
- it should have a constructor taking a `SEXP`
- it should have a methods called `get` that returns an instance
of the `Bling<T>` type.
# Summary
The \pkg{Rcpp} package greatly facilitates the transfer of objects between
\proglang{R} and \proglang{C++}. This note has shown how to extend \pkg{Rcpp}
to either user-defined or third-party classes via the `Rcpp::as` and
`Rcpp::wrap` template functions. Both intrusive and non-intrusive
approaches were discussed.
|