[Rcpp-commits] r263 - in pkg: inst src src/Rcpp

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Jan 3 17:17:33 CET 2010


Author: romain
Date: 2010-01-03 17:17:32 +0100 (Sun, 03 Jan 2010)
New Revision: 263

Added:
   pkg/src/Rcpp/wrap.h
   pkg/src/wrap.cpp
Modified:
   pkg/inst/ChangeLog
   pkg/src/RObject.cpp
   pkg/src/Rcpp.h
   pkg/src/Rcpp/Evaluator.h
   pkg/src/Rcpp/RObject.h
Log:
factoring wrap out of RObject

Modified: pkg/inst/ChangeLog
===================================================================
--- pkg/inst/ChangeLog	2010-01-03 16:01:13 UTC (rev 262)
+++ pkg/inst/ChangeLog	2010-01-03 16:17:32 UTC (rev 263)
@@ -1,5 +1,12 @@
 2010-01-03  Romain Francois <francoisromain at free.fr>
 
+	* src/Rcpp/wrap.h : factored out from RObject. there is now a
+	template wrap in addition to the specific implementations. The 
+	template generates a warning and return NULL
+	
+	* src/wrap.cpp: specific implementations of the wrap template
+	(factored out of RObject)
+
 	* src/Rcpp/pairlist.h : variadic templates to 
 	recursively generate a pairlist 
 	(CAR and CDR) from any number of "wrap("'able objects. 

Modified: pkg/src/RObject.cpp
===================================================================
--- pkg/src/RObject.cpp	2010-01-03 16:01:13 UTC (rev 262)
+++ pkg/src/RObject.cpp	2010-01-03 16:17:32 UTC (rev 263)
@@ -63,157 +63,6 @@
 	logTxt("~RObject");
 }
 
-
-RObject wrap(SEXP m_sexp=R_NilValue){
-	switch( TYPEOF(m_sexp) ){
-		case ENVSXP:
-			return Environment(m_sexp); 
-		case SYMSXP:
-			return Symbol(m_sexp) ;
-		default:
-			break ;
-	}
-	return RObject(m_sexp) ;
-}
-
-RObject wrap(const bool & v){
-    logTxt("RObject from bool\n");
-    RObject o(Rf_ScalarLogical(v));
-    return o ;
-}
-
-RObject wrap(const double & v){
-    logTxt("RObject from double\n");
-    RObject o(Rf_ScalarReal(v));
-    return o ;
-}
-
-RObject wrap(const int & v){
-    logTxt("RObject from int\n");
-    RObject o(Rf_ScalarInteger(v));
-    return o ;
-}
-
-RObject wrap(const Rbyte & v){
-    logTxt("RObject from raw\n");
-    RObject o(Rf_ScalarRaw(v));
-    return o ;
-}
-
-RObject wrap( const char * const v){
-	return wrap( std::string(v) ) ;
-}
-
-RObject wrap(const std::string & v){
-    logTxt("RObject from std::string\n");
-    RObject o(Rf_mkString(v.c_str()));
-    return o ;
-}
-
-RObject wrap(const std::vector<bool> & v){
-    logTxt("RObject from bool vector\n");
-    int n = v.size();
-    SEXP m_sexp = PROTECT( Rf_allocVector(LGLSXP, n) );
-    copy( v.begin(), v.end(), LOGICAL(m_sexp) ) ;
-    RObject o(m_sexp) ;
-    UNPROTECT(1) ; /* m_sexp now preserved by o */
-    return o ;
-}
-
-RObject wrap(const std::vector<int> & v){
-    logTxt("RObject from int vector\n");
-    int n = v.size();
-    SEXP m_sexp = PROTECT( Rf_allocVector(INTSXP, n) );
-    copy( v.begin(), v.end(), INTEGER(m_sexp) ) ;
-    RObject o(m_sexp) ;
-    UNPROTECT(1) ;
-    return o ;
-}
-
-RObject wrap(const std::vector<double> & v){
-    logTxt("RObject from double vector\n");
-    int n = v.size();
-    SEXP m_sexp = PROTECT( Rf_allocVector(REALSXP, n) );
-    copy( v.begin(), v.end(), REAL(m_sexp) ) ;
-    RObject o(m_sexp) ;
-    UNPROTECT(1) ;
-    return o ;
-}
-
-RObject wrap(const std::vector<Rbyte> & v){
-    logTxt("RObject from vector<Rbyte> \n");
-    int n = v.size();
-    SEXP m_sexp = PROTECT(Rf_allocVector(RAWSXP, n));
-    copy( v.begin(), v.end(), RAW(m_sexp) ) ;
-    RObject o(m_sexp) ;
-    UNPROTECT(1) ;
-    return o ;
-}
-
-RObject wrap(const std::vector<std::string> & v){
-    logTxt("RObject from std::string vector\n");
-    int n = v.size();
-    SEXP m_sexp = PROTECT(Rf_allocVector(STRSXP, n));
-    int i=0; 
-    std::vector<std::string>::const_iterator it = v.begin() ;
-    while( i<n ){
-    	SET_STRING_ELT(m_sexp, i, Rf_mkChar(it->c_str()));
-    	i++ ;
-    	it++; 
-    }
-    RObject o(m_sexp) ;
-    UNPROTECT(1) ;
-    return o ;
-}
-
-/* sets */
-
-RObject wrap(const std::set<int> & v){
-    logTxt("RObject from set<int>\n");
-    int n = v.size();
-    SEXP m_sexp = Rf_allocVector(INTSXP, n);
-    copy( v.begin(), v.end(), INTEGER(m_sexp) ) ;
-    RObject o(m_sexp) ;
-    UNPROTECT(1) ;
-    return o ;
-}
-
-RObject wrap(const std::set<double> & v){
-    logTxt("RObject from set<double>\n");
-    int n = v.size();
-    SEXP m_sexp = Rf_allocVector(REALSXP, n);
-    copy( v.begin(), v.end(), REAL(m_sexp) ) ;
-    RObject o(m_sexp) ;
-    UNPROTECT(1) ;
-    return o ;
-}
-
-RObject wrap(const std::set<Rbyte> & v){
-    logTxt("RObject from set<Rbyte> \n");
-    int n = v.size();
-    SEXP m_sexp = Rf_allocVector(RAWSXP, n);
-    copy( v.begin(), v.end(), RAW(m_sexp) ) ;
-    RObject o(m_sexp) ;
-    UNPROTECT(1) ;
-    return o ;
-}
-
-RObject wrap(const std::set<std::string> & v){
-    logTxt("RObject from set<string>\n");
-    int n = v.size();
-    SEXP m_sexp = Rf_allocVector(STRSXP, n);
-    int i=0;
-    std::set<std::string>::iterator it = v.begin(); 
-    while( i<n ){
-    	SET_STRING_ELT(m_sexp, i, Rf_mkChar(it->c_str()));
-    	i++ ;
-    	it++; 
-    }
-    RObject o(m_sexp) ;
-    UNPROTECT(1) ;
-    return o ;
-}
-
 double RObject::asDouble() const {
     if (Rf_length(m_sexp) != 1) {
 	throw std::range_error("RObject::asDouble expects single value");

Modified: pkg/src/Rcpp/Evaluator.h
===================================================================
--- pkg/src/Rcpp/Evaluator.h	2010-01-03 16:01:13 UTC (rev 262)
+++ pkg/src/Rcpp/Evaluator.h	2010-01-03 16:17:32 UTC (rev 263)
@@ -25,6 +25,7 @@
 #include <RcppCommon.h>
 #include <Rcpp/RObject.h>
 #include <Rcpp/Environment.h>
+#include <Rcpp/wrap.h>
 
 namespace Rcpp{ 
 

Modified: pkg/src/Rcpp/RObject.h
===================================================================
--- pkg/src/Rcpp/RObject.h	2010-01-03 16:01:13 UTC (rev 262)
+++ pkg/src/Rcpp/RObject.h	2010-01-03 16:17:32 UTC (rev 263)
@@ -142,27 +142,6 @@
     
 };
 
-// factories
-RObject wrap(SEXP m_sexp) ;
-
-RObject wrap(const bool & v);
-RObject wrap(const double & v);
-RObject wrap(const int & v);
-RObject wrap(const char* const v);
-RObject wrap(const Rbyte & v);
-RObject wrap(const std::string & v);
-
-RObject wrap(const std::vector<int> & v);
-RObject wrap(const std::vector<double> & v);
-RObject wrap(const std::vector<std::string> & v);
-RObject wrap(const std::vector<Rbyte> & v);
-RObject wrap(const std::vector<bool> & v);
-
-RObject wrap(const std::set<int> & v);
-RObject wrap(const std::set<double> & v);
-RObject wrap(const std::set<std::string> & v);
-RObject wrap(const std::set<Rbyte> & v);
-
 } // namespace Rcpp
 
 #endif

Added: pkg/src/Rcpp/wrap.h
===================================================================
--- pkg/src/Rcpp/wrap.h	                        (rev 0)
+++ pkg/src/Rcpp/wrap.h	2010-01-03 16:17:32 UTC (rev 263)
@@ -0,0 +1,60 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// wrap.h: Rcpp R/C++ interface class library -- general R object wrapper
+//
+// 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_wrap_h
+#define Rcpp_wrap_h
+
+#include <RcppCommon.h>
+#include <Rcpp/RObject.h>
+#include <set>
+
+namespace Rcpp{ 
+
+// factories
+template <typename T>
+RObject wrap(const T& v ){
+	Rf_warning( "not implemented" ) ;
+	return RObject(R_NilValue) ;
+}
+
+RObject wrap(SEXP m_sexp) ;
+
+RObject wrap(const bool & v);
+RObject wrap(const double & v);
+RObject wrap(const int & v);
+RObject wrap(const char* const v);
+RObject wrap(const Rbyte & v);
+RObject wrap(const std::string & v);
+
+RObject wrap(const std::vector<int> & v);
+RObject wrap(const std::vector<double> & v);
+RObject wrap(const std::vector<std::string> & v);
+RObject wrap(const std::vector<Rbyte> & v);
+RObject wrap(const std::vector<bool> & v);
+
+RObject wrap(const std::set<int> & v);
+RObject wrap(const std::set<double> & v);
+RObject wrap(const std::set<std::string> & v);
+RObject wrap(const std::set<Rbyte> & v);
+
+} // namespace Rcpp
+
+#endif

Modified: pkg/src/Rcpp.h
===================================================================
--- pkg/src/Rcpp.h	2010-01-03 16:01:13 UTC (rev 262)
+++ pkg/src/Rcpp.h	2010-01-03 16:17:32 UTC (rev 263)
@@ -44,6 +44,7 @@
 /* new api */
 #include <Rcpp/pairlist.h>
 #include <Rcpp/grow.h>
+#include <Rcpp/wrap.h>
 #include <Rcpp/RObject.h>
 #include <Rcpp/XPtr.h>
 #include <Rcpp/Environment.h>

Added: pkg/src/wrap.cpp
===================================================================
--- pkg/src/wrap.cpp	                        (rev 0)
+++ pkg/src/wrap.cpp	2010-01-03 16:17:32 UTC (rev 263)
@@ -0,0 +1,178 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// wrap.cpp: Rcpp R/C++ interface class library -- general R object wrapper
+//
+// 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 <Rcpp/wrap.h>
+#include <Rcpp/Symbol.h>
+#include <Rcpp/Environment.h>
+
+namespace Rcpp{
+
+RObject wrap(SEXP m_sexp=R_NilValue){
+	switch( TYPEOF(m_sexp) ){
+		case ENVSXP:
+			return Environment(m_sexp); 
+		case SYMSXP:
+			return Symbol(m_sexp) ;
+		default:
+			break ;
+	}
+	return RObject(m_sexp) ;
+}
+
+RObject wrap(const bool & v){
+    logTxt("RObject from bool\n");
+    RObject o(Rf_ScalarLogical(v));
+    return o ;
+}
+
+RObject wrap(const double & v){
+    logTxt("RObject from double\n");
+    RObject o(Rf_ScalarReal(v));
+    return o ;
+}
+
+RObject wrap(const int & v){
+    logTxt("RObject from int\n");
+    RObject o(Rf_ScalarInteger(v));
+    return o ;
+}
+
+RObject wrap(const Rbyte & v){
+    logTxt("RObject from raw\n");
+    RObject o(Rf_ScalarRaw(v));
+    return o ;
+}
+
+RObject wrap( const char * const v){
+	return wrap( std::string(v) ) ;
+}
+
+RObject wrap(const std::string & v){
+    logTxt("RObject from std::string\n");
+    RObject o(Rf_mkString(v.c_str()));
+    return o ;
+}
+
+RObject wrap(const std::vector<bool> & v){
+    logTxt("RObject from bool vector\n");
+    int n = v.size();
+    SEXP m_sexp = PROTECT( Rf_allocVector(LGLSXP, n) );
+    copy( v.begin(), v.end(), LOGICAL(m_sexp) ) ;
+    RObject o(m_sexp) ;
+    UNPROTECT(1) ; /* m_sexp now preserved by o */
+    return o ;
+}
+
+RObject wrap(const std::vector<int> & v){
+    logTxt("RObject from int vector\n");
+    int n = v.size();
+    SEXP m_sexp = PROTECT( Rf_allocVector(INTSXP, n) );
+    copy( v.begin(), v.end(), INTEGER(m_sexp) ) ;
+    RObject o(m_sexp) ;
+    UNPROTECT(1) ;
+    return o ;
+}
+
+RObject wrap(const std::vector<double> & v){
+    logTxt("RObject from double vector\n");
+    int n = v.size();
+    SEXP m_sexp = PROTECT( Rf_allocVector(REALSXP, n) );
+    copy( v.begin(), v.end(), REAL(m_sexp) ) ;
+    RObject o(m_sexp) ;
+    UNPROTECT(1) ;
+    return o ;
+}
+
+RObject wrap(const std::vector<Rbyte> & v){
+    logTxt("RObject from vector<Rbyte> \n");
+    int n = v.size();
+    SEXP m_sexp = PROTECT(Rf_allocVector(RAWSXP, n));
+    copy( v.begin(), v.end(), RAW(m_sexp) ) ;
+    RObject o(m_sexp) ;
+    UNPROTECT(1) ;
+    return o ;
+}
+
+RObject wrap(const std::vector<std::string> & v){
+    logTxt("RObject from std::string vector\n");
+    int n = v.size();
+    SEXP m_sexp = PROTECT(Rf_allocVector(STRSXP, n));
+    int i=0; 
+    std::vector<std::string>::const_iterator it = v.begin() ;
+    while( i<n ){
+    	SET_STRING_ELT(m_sexp, i, Rf_mkChar(it->c_str()));
+    	i++ ;
+    	it++; 
+    }
+    RObject o(m_sexp) ;
+    UNPROTECT(1) ;
+    return o ;
+}
+
+/* sets */
+
+RObject wrap(const std::set<int> & v){
+    logTxt("RObject from set<int>\n");
+    int n = v.size();
+    SEXP m_sexp = Rf_allocVector(INTSXP, n);
+    copy( v.begin(), v.end(), INTEGER(m_sexp) ) ;
+    RObject o(m_sexp) ;
+    UNPROTECT(1) ;
+    return o ;
+}
+
+RObject wrap(const std::set<double> & v){
+    logTxt("RObject from set<double>\n");
+    int n = v.size();
+    SEXP m_sexp = Rf_allocVector(REALSXP, n);
+    copy( v.begin(), v.end(), REAL(m_sexp) ) ;
+    RObject o(m_sexp) ;
+    UNPROTECT(1) ;
+    return o ;
+}
+
+RObject wrap(const std::set<Rbyte> & v){
+    logTxt("RObject from set<Rbyte> \n");
+    int n = v.size();
+    SEXP m_sexp = Rf_allocVector(RAWSXP, n);
+    copy( v.begin(), v.end(), RAW(m_sexp) ) ;
+    RObject o(m_sexp) ;
+    UNPROTECT(1) ;
+    return o ;
+}
+
+RObject wrap(const std::set<std::string> & v){
+    logTxt("RObject from set<string>\n");
+    int n = v.size();
+    SEXP m_sexp = Rf_allocVector(STRSXP, n);
+    int i=0;
+    std::set<std::string>::iterator it = v.begin(); 
+    while( i<n ){
+    	SET_STRING_ELT(m_sexp, i, Rf_mkChar(it->c_str()));
+    	i++ ;
+    	it++; 
+    }
+    RObject o(m_sexp) ;
+    UNPROTECT(1) ;
+    return o ;
+}
+
+} // namespace Rcpp



More information about the Rcpp-commits mailing list