On Fri, 3 Dec 2004, Marcos Aur??lio Carrero wrote:
> Hi,
> We are developing a package using S4 classes. The S4 classes
> are wrappers to C++ classes. So S4 classes contain an integer
> that is the memory address of one C++ object. If an user
> calls the rm() function, the C++ object must be deleted. But our generic
> rm() function apparently doesn't work. Here is the code:
>
> # The class
> setClass("component", representation(pointer="integer",
"VIRTUAL"))
>
> # The generic method
> setGeneric("rm", function(..., list = object, pos = -1, envir =
as.environment(pos), inherits = FALSE) standardGeneric("rm"))
>
> setMethod("rm", "component", function(list=object, pos,
envir, inherits) {
> warning("object deleted")
> .Call("Rm");i
> })
>
> We implemented generic methods for print(), show() and summary()
succesfully.
>
I think that you may find reading the code written by Timothy Keitt in the
rgdal package, especially R/gdal.R, useful. He defines a class with
representation(handle = 'externalptr')
which I think should be considered as an alternative to integer, because
integer is a vector and externalptr is not - it is an external pointer and
just that. In addition, his code shows how to use .setCollectorFun() as
the collector calling reg.finalizer() in the body of the class definition.
It may well be that you don't need an rm method for your object, I think
reading Tim's code may help you to find a cleaner structure, and
contribute to solving this question.
Roger
> Best,
> Marcos Carrero
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
>
--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Breiviksveien 40, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93
e-mail: Roger.Bivand at nhh.no