[Rcpp-devel] Fwd: Re: Making objects in the C++ "side" persistent

Martin Jakt mjakt at z2.keio.jp
Fri Jun 13 08:24:32 CEST 2014


I forgot to include the mailing list in my previous email.

(Fernando: apologies for the double mailing)

Hi Fernando,

> 
> If losing the information is no problem, and you only want to make sure
> your pointers are valid, perhaps you could add a timestamp to them when
> created.
> 
> On loading your package you would have to note the time (perhaps setting
> a global variable?) and then before using each pointer check that it is
> current.

thanks for the suggestion. I'm not quite sure as to how I would go about using 
the timestamp in this case, or how I would check them at load time.

So, I'll need to read more of the load() man pages to see how that can be 
done.

What I can do, which sort of works is to either interpose the save() function 
using assign("save", my.save, envir=environment(save)). (Though I have to use 
unlockBinding first). This is pretty horrible, for lots of reasons, and even if 
I could do it safely, it would be bad idea as only one package at a time can 
implement it.

Somewhat less harmful, but still not so good, is to register a .Last function 
that looks through the objects and reassigns the Module objects to something 
harmless. That's pretty easy to do, but again, it doesn't seem like a good 
idea for a package to define the .Last function as it can overwrite previous 
definitions.

I supppose one could do something like check if a .Last function is defined, 
and if so rename the .Last function to something else and call it from the new 
.Last function. Again, though, that's probably a bad idea as it reloading the 
package after a restart would then end up extending the .Last function.

So, I'm still, not very sure; I suspect that changing the way the object is 
serialised is the way to go, and I will pursue Dirk's suggestion, and see if I 
can come up with something.

(I'm having this awful feeling that I read a somewhat similar discussion on 
this email list previously, but can't seem to find it).

thanks,

Martin

PS. code to neutralise the objects, feel free to tear it apart ;-)

## it seems I need to use eval(expression, environment) in order
## to change the items in the parent frame
purge.squeezers <- expression({
    obs <- ls()
    for(o in obs){
        print(class(get(o)))
        if(class(get(o)) == "Rcpp_DimSqueezer")
            assign(o, "Expired Rcpp_DimSqueezer")
        if(class(get(o)) == "Rcpp_DimSqueezer_CL")
            assign(o, "Expired Rcpp_DimSqueezer_CL")
    }
    rm(o)
    rm(obs)
})

.Last <- function(){
    eval(purge.squeezers, parent.frame())
#    eval(purge.squeezers, globalenv())
}

## both parent.frame and globalenv() seem to work. Not sure what is
## better.

> ft.

-- 
Martin Jakt
Department of Systems Medicine
Mitsunada Sakaguchi Laboratory
Keio University School of Medicine
-----------------------------------------
-- 
Martin Jakt
Department of Systems Medicine
Mitsunada Sakaguchi Laboratory
Keio University School of Medicine


More information about the Rcpp-devel mailing list