[Rcpp-commits] r3120 - in pkg/Rcpp: . inst inst/include/Rcpp
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Jul 7 19:44:32 CEST 2011
Author: romain
Date: 2011-07-07 19:44:32 +0200 (Thu, 07 Jul 2011)
New Revision: 3120
Modified:
pkg/Rcpp/ChangeLog
pkg/Rcpp/inst/NEWS
pkg/Rcpp/inst/include/Rcpp/XPtr.h
Log:
allowing custom finalizers in XPtr
Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog 2011-07-07 01:51:36 UTC (rev 3119)
+++ pkg/Rcpp/ChangeLog 2011-07-07 17:44:32 UTC (rev 3120)
@@ -1,3 +1,9 @@
+2011-07-07 Romain Francois <romain at r-enthusiasts.com>
+
+ * inst/include/Rcpp/XPtr.h: The XPtr class template gains a second template
+ parameter allowing the developper to supply his/her own finalizer instead
+ of the default one that calls delete
+
2011-07-05 Dirk Eddelbuettel <edd at debian.org>
* DESCRIPTION: Release 0.9.5
Modified: pkg/Rcpp/inst/NEWS
===================================================================
--- pkg/Rcpp/inst/NEWS 2011-07-07 01:51:36 UTC (rev 3119)
+++ pkg/Rcpp/inst/NEWS 2011-07-07 17:44:32 UTC (rev 3120)
@@ -1,3 +1,10 @@
+0.9.6 (future)
+
+ o XPtr now accepts a second template parameter, which is a function
+ taking a pointer to the target class. This allows the developper to supply
+ his/her own finalizer. The template parameter has a default value
+ which retains the original behaviour (calling delete on the pointer)
+
0.9.5 2011-07-05
o New Rcpp-FAQ examples on using the plugin maker for inline's
Modified: pkg/Rcpp/inst/include/Rcpp/XPtr.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/XPtr.h 2011-07-07 01:51:36 UTC (rev 3119)
+++ pkg/Rcpp/inst/include/Rcpp/XPtr.h 2011-07-07 17:44:32 UTC (rev 3120)
@@ -25,7 +25,7 @@
#include <RcppCommon.h>
namespace Rcpp{
-
+
template <typename T>
void delete_finalizer(SEXP p){
if( TYPEOF(p) == EXTPTRSXP ){
@@ -35,6 +35,19 @@
}
template <typename T>
+void standard_delete_finalizer(T* obj){
+ delete obj ;
+}
+
+template <typename T, void Finalizer(T*) >
+void finalizer_wrapper(SEXP p){
+ if( TYPEOF(p) == EXTPTRSXP ){
+ T* ptr = (T*) R_ExternalPtrAddr(p) ;
+ Finalizer(ptr) ;
+ }
+}
+
+template <typename T, void Finalizer(T*) = standard_delete_finalizer<T> >
class XPtr : public RObject {
public:
@@ -59,7 +72,13 @@
* so you need to make sure the pointer can be "delete" d
* this way (has to be a C++ object)
*/
- explicit XPtr(T* p, bool set_delete_finalizer = true, SEXP tag = R_NilValue, SEXP prot = R_NilValue);
+ explicit XPtr(T* p, bool set_delete_finalizer = true, SEXP tag = R_NilValue, SEXP prot = R_NilValue){
+ setSEXP( R_MakeExternalPtr( (void*)p , tag, prot) ) ;
+ if( set_delete_finalizer ){
+ setDeleteFinalizer() ;
+ }
+
+ }
XPtr( const XPtr& other ) : RObject( other.asSexp() ) {}
@@ -72,15 +91,21 @@
* Returns a reference to the object wrapped. This allows this
* object to look and feel like a dumb pointer to T
*/
- T& operator*() const ;
+ T& operator*() const {
+ return *((T*)R_ExternalPtrAddr( m_sexp )) ;
+ }
/**
* Returns the dumb pointer. This allows to call the -> operator
* on this as if it was the dumb pointer
*/
- T* operator->() const ;
+ T* operator->() const {
+ return (T*)(R_ExternalPtrAddr(m_sexp));
+ }
- void setDeleteFinalizer() ;
+ void setDeleteFinalizer() {
+ R_RegisterCFinalizerEx( m_sexp, finalizer_wrapper<T,Finalizer> , FALSE) ;
+ }
inline operator T*(){ return (T*)( R_ExternalPtrAddr(m_sexp)) ; }
@@ -153,29 +178,6 @@
};
-template<typename T>
-XPtr<T>::XPtr(T* p, bool set_delete_finalizer, SEXP tag, SEXP prot) : RObject() {
- setSEXP( R_MakeExternalPtr( (void*)p , tag, prot) ) ;
- if( set_delete_finalizer ){
- setDeleteFinalizer() ;
- }
-}
-
-template<typename T>
-void XPtr<T>::setDeleteFinalizer(){
- R_RegisterCFinalizerEx( m_sexp, delete_finalizer<T> , FALSE) ;
-}
-
-template<typename T>
-T& XPtr<T>::operator*() const {
- return *((T*)R_ExternalPtrAddr( m_sexp )) ;
-}
-
-template<typename T>
-T* XPtr<T>::operator->() const {
- return (T*)(R_ExternalPtrAddr(m_sexp));
-}
-
} // namespace Rcpp
#endif
More information about the Rcpp-commits
mailing list