testcpp: Testing external pointers with finalization in R using C++

Description Usage Arguments Details Value Note References Examples

Description

Show how to create C++ objects with external pointers from R which are deleted automatically from R.

Usage

1
2
3
4

Arguments

str

Character string.

p

Pointer to Foo object.

Details

This package shows how to create C++ objects from R which are stored in memory until they are deleted from R. The idea is to be able to call methods of the C++ class before freeing the object from memory. createFoo create an object of class Foo. getStrFoo returns the string stored in Foo. setStrFoo set the string in Foo. .closeFoo(p) is the internal function used to free the object from memory.

Have a look in the src folder to see how the C and C++ code are made.

The test package is inspired by the “Interfacing C++ code” section in the R extensions manual [1] and Luke Tierney's page about simple references with finalization [2].

Value

createFoo returns a pointer to the object of class Foo created in memory. getStrFoo returns the string stored in Foo. setStrFoo returns NULL. .closeFoo(p) return NULL.

Note

Use the extension .cc and .hh for C++ files in the src folder. If you use the extension .c and .h the source code will be compiled using gcc instead of g++!

References

[1] R Development Core Team. Writing R Extensions. Version 2.8.1 (2008-12-22) R Foundation for Statistical Computing, Vienna, Austria. ISBN 3-900051-11-9, http://www.R-project.org.

[2] Simple References with Finalization http://www.stat.uiowa.edu/~luke/R/simpleref.html.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# Creating an object
p<-createFoo("Testing 1")
getStrFoo(p)
setStrFoo(p,"Testing 2")
getStrFoo(p)
trace(.closeFoo)
rm(p)   # remove the object when the garbage collector is called
gc()

# Creating two objects
p1<-createFoo("Testing 1")
p2<-createFoo("Testing 2")
getStrFoo(p1)
getStrFoo(p2)
rm(p1)  
rm(p2) 
gc()

testcpp documentation built on May 2, 2019, 6:07 p.m.

Related to testcpp in testcpp...