% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/cpp_source.R
\name{cpp_source}
\alias{cpp_source}
\alias{cpp_eval}
\title{Compile C++20 code}
\usage{
cpp_source(
  file,
  code = NULL,
  env = parent.frame(),
  clean = TRUE,
  quiet = TRUE,
  debug = FALSE,
  preserve_altrep = FALSE,
  cxx_std = Sys.getenv("CXX_STD", "CXX20"),
  dir = tempfile()
)

cpp_eval(
  code,
  env = curr_env(),
  clean = TRUE,
  quiet = TRUE,
  debug = FALSE,
  preserve_altrep = FALSE,
  simplify = TRUE,
  cxx_std = Sys.getenv("CXX_STD", "CXX20")
)
}
\arguments{
\item{file}{C++ file.}

\item{code}{For \code{cpp_source()} - If \code{file} is \code{NULL}
then a string of C++ code to compile.
This can include the contents of a cpp file which
can contain multiple \verb{[[cppally::register]]} tagged functions.
For \code{cpp_eval} - This can be a character vector of single-line expressions.}

\item{env}{Environment where R functions should be defined.}

\item{clean}{Should files be cleaned up after sourcing? Default is \code{TRUE}.}

\item{quiet}{Should compiler output be suppressed? Default is \code{TRUE}.}

\item{debug}{Should C++ code be compiled in a debug build?
Default is \code{FALSE}.}

\item{preserve_altrep}{Should ALTREP vectors be preserved by avoiding
materialisation where possible? Default is \code{FALSE}.}

\item{cxx_std}{C++ standard to use. Should be >= C++20.}

\item{dir}{Directory to store the source files.
The default is a temporary directory via \code{tempfile()} which is removed when
\code{clean = TRUE}.}

\item{simplify}{Applies to \code{cpp_eval}. A list of results is returned unless
\code{length(code) == 1} and \code{simplify = TRUE}.}
}
\value{
\code{cpp_source()} invisibly compiles the C++ code and registers
the \verb{[[cppally::register]]} tagged functions to R. \cr
\code{cpp_eval()} returns the results of the evaluated C++ expressions.
}
\description{
cpp11-style helpers to compile cppally code outside of a cppally-linked package
context.

\code{cpp_source()} compiles and loads a single C++ file for use in R,
either from an expression or a cpp file.
This may include multiple C++ functions.

\code{cpp_eval()} evaluates a single C++ expression and returns the result.
For example \code{cpp_eval('get_threads()')} will run the C++ function
\code{cppally::get_threads()} and return the number of OMP threads currently set
for use. For expressions no return result, the call is evaluated and returns
\code{NULL} invisibly.
}
\examples{

library(cppally)
library(bit64)
\donttest{
cpp_eval('print("hello world!")')

# Default values of all cppally scalars
cpp_eval(c(
  'r_lgl()',
  'r_int()',
  'r_dbl()',
  'r_int64()',
  'r_str()',
  'r_raw()',
  'r_cplx()',
  'r_date()',
  'r_psxct()'
))

cpp_source(code = '
  #include <cppally.hpp>
  using namespace cppally;

  [[cppally::register]]
  r_dbl add(r_dbl x, r_dbl y){
    return x + y;
  }
', debug = TRUE)
add(1, 2)
add(2, NA)

### ALTREP ###

# cppally also supports lazy ALTREP materialisation as an opt-in feature.
# To opt-in, set `preserve_altrep = TRUE`

cpp_source(
  code = '
  #include <cppally.hpp>
  using namespace cppally;

  [[cppally::register]]
  r_int last_altrep_unaware(r_vec<r_int> x){
    r_int out;
    r_size_t n = x.length();

    if (n > 0){
      out = x.get(n - 1);
    }
    return out;
  }
', debug = TRUE
)

cpp_source(
  code = '
  #include <cppally.hpp>
  using namespace cppally;

  [[cppally::register]]
  r_int last_altrep_aware(r_vec<r_int> x){
    r_int out;
    r_size_t n = x.length();

    if (n > 0){
      out = x.get(n - 1);
    }
    return out;
  }
', debug = TRUE,
  preserve_altrep = TRUE
)

library(bench)
mark(last_altrep_aware(1:10^5)) # No materialisation
mark(last_altrep_unaware(1:10^5)) # Materialises full vector

}

}
