[Rcpp-commits] r462 - in pkg: inst inst/unitTests src src/Rcpp
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Jan 26 11:44:36 CET 2010
Author: romain
Date: 2010-01-26 11:44:36 +0100 (Tue, 26 Jan 2010)
New Revision: 462
Added:
pkg/inst/unitTests/runit.clone.R
pkg/src/Rcpp/clone.h
pkg/src/clone.cpp
Modified:
pkg/inst/ChangeLog
pkg/src/Rcpp.h
pkg/src/Rcpp/RObject.h
Log:
specialization of clone for SEXP
Modified: pkg/inst/ChangeLog
===================================================================
--- pkg/inst/ChangeLog 2010-01-26 10:05:30 UTC (rev 461)
+++ pkg/inst/ChangeLog 2010-01-26 10:44:36 UTC (rev 462)
@@ -1,5 +1,10 @@
2010-01-26 Romain Francois <francoisromain at free.fr>
+ * src/Rcpp/clone.h: explicit cloning of RObjects. The clone
+ template function creates a new object of the same class
+ as the object it is passed in, encapsulating a duplicate
+ of the SEXP contained by the input RObject.
+
* src/Rcpp/r_cast.h: new template function to handle casts
from one SEXP to another. This is mostly useful internally
Added: pkg/inst/unitTests/runit.clone.R
===================================================================
--- pkg/inst/unitTests/runit.clone.R (rev 0)
+++ pkg/inst/unitTests/runit.clone.R 2010-01-26 10:44:36 UTC (rev 462)
@@ -0,0 +1,39 @@
+#!/usr/bin/r -t
+#
+# Copyright (C) 2010 Dirk Eddelbuettel and Romain Francois
+#
+# This file is part of Rcpp.
+#
+# Rcpp is free software: you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 2 of the License, or
+# (at your option) any later version.
+#
+# Rcpp is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
+
+
+test.clone <- function(){
+
+ x <- 1:10
+ funx <- cfunction(signature(x="integer"), '
+ IntegerVector vec(x) ;
+ IntegerVector dolly = clone( vec ) ;
+ for( size_t i=0; i<10; i++){
+ dolly[i] = 10 - i ;
+ }
+ return dolly ;
+ ', Rcpp = TRUE, includes = "using namespace Rcpp;" )
+ y <- funx(x)
+ checkEquals( x, 1:10, msg = "clone" )
+ checkEquals( y, 10:1, msg = "clone" )
+
+ # TODO: add more
+
+}
+
Modified: pkg/src/Rcpp/RObject.h
===================================================================
--- pkg/src/Rcpp/RObject.h 2010-01-26 10:05:30 UTC (rev 461)
+++ pkg/src/Rcpp/RObject.h 2010-01-26 10:44:36 UTC (rev 462)
@@ -206,7 +206,7 @@
RObject slot(const std::string& name) const throw(not_s4) ;
/* TODO : implement the proxy pattern here so that we can get and
set the slot the same way */
-
+
protected:
/**
Added: pkg/src/Rcpp/clone.h
===================================================================
--- pkg/src/Rcpp/clone.h (rev 0)
+++ pkg/src/Rcpp/clone.h 2010-01-26 10:44:36 UTC (rev 462)
@@ -0,0 +1,42 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// clone.h: Rcpp R/C++ interface class library -- clone RObject's
+//
+// Copyright (C) 2010 Dirk Eddelbuettel and Romain Francois
+//
+// This file is part of Rcpp.
+//
+// Rcpp is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// Rcpp is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef Rcpp_clone_h
+#define Rcpp_clone_h
+
+#include <RcppCommon.h>
+#include <Rcpp/wrap.h>
+
+namespace Rcpp{
+
+/* cloning type T is possible if :
+ - T can be converted to SEXP
+ - T has a SEXP constructor
+*/
+template <typename T> T clone(T object) {
+ SEXP x = object ;
+ return T( Rf_duplicate( x ) ) ;
+}
+template<> SEXP clone(SEXP object) ;
+
+} // namespace Rcpp
+
+#endif
Modified: pkg/src/Rcpp.h
===================================================================
--- pkg/src/Rcpp.h 2010-01-26 10:05:30 UTC (rev 461)
+++ pkg/src/Rcpp.h 2010-01-26 10:44:36 UTC (rev 462)
@@ -43,6 +43,7 @@
#include <RcppVectorView.h>
/* new api */
+#include <Rcpp/clone.h>
#include <Rcpp/r_cast.h>
#include <Rcpp/grow.h>
#include <Rcpp/wrap.h>
Added: pkg/src/clone.cpp
===================================================================
--- pkg/src/clone.cpp (rev 0)
+++ pkg/src/clone.cpp 2010-01-26 10:44:36 UTC (rev 462)
@@ -0,0 +1,31 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// clone.cpp: Rcpp R/C++ interface class library -- clone SEXP
+//
+// Copyright (C) 2010 Dirk Eddelbuettel and Romain Francois
+//
+// This file is part of Rcpp.
+//
+// Rcpp is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// Rcpp is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
+
+#include <clone.h>
+
+namespace Rcpp{
+
+template<> SEXP clone( SEXP object){
+ return Rf_duplicate( object ) ;
+}
+
+} // namespace Rcpp
+
More information about the Rcpp-commits
mailing list