Utility functions for working with foreign C data types
utils.Rd
Functions for low-level operations on C pointers as well as helper
functions and objects to handle C float
arrays and strings.
Usage
is.nullptr(x)
as.externalptr(x)
is.externalptr(x)
floatraw(n)
as.floatraw(x)
floatraw2numeric(x)
ptr2str(x)
strarrayptr(x)
strptr(x)
offset_ptr(x, offset)
Details
is.nullptr
tests if the external pointer given by x
represents
a C NULL
pointer.
as.externalptr
returns an external pointer to the data area of atomic
vector given by x
. The external pointer holds an additional reference
to the x
R object to prevent it from garbage collection.
is.externalptr
tests if the object given by x
is an external
pointer.
floatraw
creates an array with a capacity to store n
single-precision C float
values. The array is implemented via a
raw
vector.
as.floatraw
coerces a numeric vector into a single-precision C
float
vector. Values given by x
are converted to C float
values and stored in the R raw vector via pack
. This function is
useful when calling foreign functions that expect a C float
pointer
via dyncall
.
floatraw2numeric
coerces a C float
(raw) vector to a numeric
vector.
ptr2str
, strarrayptr
, strptr
are currently
experimental.
offset_ptr
creates a new external pointer pointing to x
plus
the byte offset
. If x
is given as an external pointer, the
address is increased by the offset
, or, if x
is given as a
atomic vector, the address of the data (pointing to offset zero) is taken as
basis and increased by the offset
. The returned external pointer is
protected (as offered by the C function R_MakeExternalPtr
) by the
external pointer x
.
Value
A logical value is returned by is.nullptr
and is.externalptr
.
as.externalptr
and offset_ptr
returns an external pointer
value. floatraw
and as.floatraw
return an atomic vector of
type raw
tagged with class 'floatraw'
. floatraw2numeric
returns a numeric
atomic vector.
Examples
is.nullptr(NULL)
#> [1] FALSE
one <- as.externalptr(1)
is.externalptr(one)
#> [1] TRUE
floatraw(1)
#> [1] 00 00 00 00
#> attr(,"class")
#> [1] "floatraw"
floats <- as.floatraw(1:10)
all.equal(floatraw2numeric(floats), 1:10)
#> [1] TRUE