printing, reading and writing CFunc objects

Share:

Description

writeDynLib saves the DLL and the CFunc or CFuncList object as generated by cfunction; readDynLib loads it.

The print and code methods respectively print the entire object or the code parts.

Usage

1
2

Arguments

x

A CFunc or CFuncList object as created by cfunction to be saved.

file

base name of the file to write the object to or to read from. Two files will be saved, one for the shared object or DLL (extension so or DLL) and one that holds the CFunc or CFuncList specification, without the function address (extension CFunc).

Details

Both the CFunc or CFuncList object and the shared object or DLL are saved, in two files; the first has extension CFunc; the second so or DLL, depending on the operating system used.

When reading, both files are loaded, and the compiled function address added to the object.

Value

Function readDynLib returns a CFunc or CFuncList object.

Methods

  • Method print(x, ...) prints the entire object x

    signature(x = "CFunc")

    Prints the CFunc object generated by cfunction, including the code that generated it.

    signature(x = "CFuncList")

    Print all CFunc objects generated by cfunction, including the code that generated them.

  • Method code(x, linenumbers = TRUE, ...) prints the code only

    signature(x)

    The CFunc or CFuncList object as generated by cfunction.

    linenumbers

    If TRUE all code lines will be numbered.

Note

  • The code of a CFunc or CFuncList object x can be extracted (rather than printed), using:

    x@code.

  • To write the code to a file (here called "fn"), without the new-line character "\n":

    write (strsplit(x, "\n")[[1]], file = "fn")

Author(s)

Karline Soetaert

See Also

getDynLib

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
x <- as.numeric(1:10)
n <- as.integer(10)

code <- "
      integer i
      do 1 i=1, n(1)
    1 x(i) = x(i)**3
"
cubefn <- cfunction(signature(n="integer", x="numeric"), code, convention=".Fortran")
code(cubefn)

cubefn(n, x)$x

## Not run: 
 fname <- tempfile()
 writeDynLib(cubefn, file = fname)
 # load and assign different name to object
 cfn <- readDynLib(fname)
 print(cfn)
 cfn(2, 1:2)

## End(Not run)