From simon.urbanek at r-project.org  Thu May 26 17:04:22 2011
From: simon.urbanek at r-project.org (Simon Urbanek)
Date: Thu, 26 May 2011 11:04:22 -0400
Subject: [Rd] serialization of external pointers
In-Reply-To: <4DDE6655.2050209@r-enthusiasts.com>
References: <4DDE6655.2050209@r-enthusiasts.com>
Message-ID: <E7D773D2-EFDF-4149-B230-889932BDC592@r-project.org>


On May 26, 2011, at 10:40 AM, Romain Francois wrote:

> Hello,
> 
> I'm looking for examples of how to handle serialization of external pointers.
> 
> We use external pointers intensively in Rcpp for modules and one popular request is to have persistence. And I guess we need to be able to serialize/deserialize external pointers for this.
> 
> Also, module objects are all instances of reference classes, so maybe that can be handled at the R level ?
> 

Reference classes have nothing to do with EXTPTR - they are simply built around environments and as such have no issues with serialization.

As for EXTPTR, well, you can perform custom serialization (see refhook in serialize() and unserialize()), so to answer your question, yes it's easy to serialize EXPPTRs - you just have to provide functions that convert them to some string representation and back.

So far, so good, but if you were thinking of using save()/save.image()/load() to automatically serialize references in a workspace then the bad new is you can't since there is no way to provide the hooks (that's why rJava caches serialized representation inside the object so that you can restore the pointers on load when you see a NULL pointer with a serialized cache - less ideal solution, but doable).

Cheers,
Simon



[Rd] serialization of external pointers
Simon Urbanek