[Rcpp-commits] r918 - in pkg/Rcpp/src: . Rcpp Rcpp/traits

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Mar 19 16:31:39 CET 2010


Author: romain
Date: 2010-03-19 16:31:39 +0100 (Fri, 19 Mar 2010)
New Revision: 918

Added:
   pkg/Rcpp/src/Rcpp/traits/named_object.h
Removed:
   pkg/Rcpp/src/grow.cpp
Modified:
   pkg/Rcpp/src/DottedPair.cpp
   pkg/Rcpp/src/Rcpp/DottedPair.h
   pkg/Rcpp/src/Rcpp/Named.h
   pkg/Rcpp/src/Rcpp/grow.h
   pkg/Rcpp/src/RcppCommon.h
Log:
Named becomes a templated function instead of a class, so that it can be used with the same interface in Vector<>::create

Modified: pkg/Rcpp/src/DottedPair.cpp
===================================================================
--- pkg/Rcpp/src/DottedPair.cpp	2010-03-19 13:02:10 UTC (rev 917)
+++ pkg/Rcpp/src/DottedPair.cpp	2010-03-19 15:31:39 UTC (rev 918)
@@ -65,12 +65,6 @@
 		return *this ;
 	}
 	
-	DottedPair::Proxy& DottedPair::Proxy::operator=(const Named& rhs){
-		SETCAR( node, rhs.getSEXP() ) ;
-		SET_TAG( node, Rf_install( rhs.getTag().c_str() ) ) ;
-		return *this ;
-	}
-	
 	const DottedPair::Proxy DottedPair::operator[]( int i ) const {
 		return Proxy( const_cast<DottedPair&>(*this), i) ;
 	}

Modified: pkg/Rcpp/src/Rcpp/DottedPair.h
===================================================================
--- pkg/Rcpp/src/Rcpp/DottedPair.h	2010-03-19 13:02:10 UTC (rev 917)
+++ pkg/Rcpp/src/Rcpp/DottedPair.h	2010-03-19 15:31:39 UTC (rev 918)
@@ -166,7 +166,8 @@
 	 * (this require traversing the entire pairlist)
 	 *
 	 * @param object anything that can be wrapped by one 
-	 * of the wrap functions, or an object of class Named
+	 * of the wrap functions, named objects (instances of traits::named_object<> 
+	 * are treated specially
 	 */
 	template <typename T>
 	void push_back( const T& object){
@@ -268,8 +269,14 @@
 			SETCAR( node, wrap(rhs) ) ;
 			return *this ;
 		}
-		Proxy& operator=(const Named& rhs) ;
 		
+		template <typename U>
+		Proxy& operator=(const traits::named_object<U>& rhs){
+			SETCAR( node, rhs.object ) ;
+			SET_TAG( node, Rf_install( rhs.name.c_str() ) ) ;
+			return *this ;
+		}
+		
 		template <typename T> operator T() const {
 			return as<T>( CAR(node) ) ;
 		}
@@ -283,8 +290,6 @@
 	
 	friend class Proxy; 
 	
-	
-	
 	virtual ~DottedPair() = 0 ;
 	
 	template <typename T>

Modified: pkg/Rcpp/src/Rcpp/Named.h
===================================================================
--- pkg/Rcpp/src/Rcpp/Named.h	2010-03-19 13:02:10 UTC (rev 917)
+++ pkg/Rcpp/src/Rcpp/Named.h	2010-03-19 15:31:39 UTC (rev 918)
@@ -27,76 +27,8 @@
 #include <Rcpp/Symbol.h>
 #include <Rcpp/RObject.h>
 
-namespace Rcpp{  
+namespace Rcpp{ 
 
-/** 
- * Facility to have named arguments in pairlist, such 
- * as Language objects
- */
-class Named{
-public:
-	/** default constructor */
-	Named( ) : object(R_MissingArg), tag("") {} ;
-	
-	/**
-	 * @param tag name to give to the object 
-	 * @param value value of the object
-	 */
-	Named( const std::string& tag, SEXP value ) : object(value), tag(tag) {}; 
-	
-	/**
-	 * uses NULL as the value
-	 * @param tag name to give to the object
-	 */
-	Named( const std::string& tag ) : object(R_MissingArg), tag(tag){} ;
-	
-	template<typename T>
-	Named( const std::string& tag, const T& value ) : object(wrap(value)), tag(tag) {}
-	
-	/**
-	 * This allows the syntax : 
-	 * Language( "rnorm", Named( "mean" ) = 10 ) ;
-	 */
-	template <typename T>
-	Named& operator=( const T& o ){
-		object = wrap( o ) ;
-		return *this ;
-	}
-	
-	inline SEXP getSEXP() const { return object.asSexp() ; }
-	
-	inline std::string getTag() const { return tag ; }
-	
-private:
-	RObject object ;
-	std::string tag ;
-} ;
-
-namespace traits {
-
-template <typename T> class named_object {
-	public:
-		named_object( const std::string& name_, const T& o_) : 
-			name(name_), object(o_){} 
-		const std::string& name ;
-		const T& object ;
-		operator ::Rcpp::Named(){
-			return ::Rcpp::Named(name, object) ;	
-		}
-} ;
-
-template <typename T>
-named_object<T> named( const std::string& name, const T& o){
-	return named_object<T>( name, o );	
-} ;
-
-template <typename T> struct is_named : public false_type{} ;
-template <typename T> struct is_named< named_object<T> >   : public true_type {} ;
-
-
-} // namespace traits
-
-
 class Argument {
 public:
 	Argument() : name(){} ;
@@ -112,6 +44,15 @@
 } ;
 
 
+template <typename T>
+Argument Named( const std::string& name){
+	return Argument( name );	
+}
+template <typename T>
+traits::named_object<T> Named( const std::string& name, const T& o){
+	return traits::named_object<T>( name, o );	
+}
+
 namespace internal{
 
 class NamedPlaceHolder {

Modified: pkg/Rcpp/src/Rcpp/grow.h
===================================================================
--- pkg/Rcpp/src/Rcpp/grow.h	2010-03-19 13:02:10 UTC (rev 917)
+++ pkg/Rcpp/src/Rcpp/grow.h	2010-03-19 15:31:39 UTC (rev 918)
@@ -27,9 +27,8 @@
 
 namespace Rcpp{
 
-SEXP pairlist() ;
+inline SEXP pairlist() { return R_NilValue ; }
 
-
 #ifdef HAS_VARIADIC_TEMPLATES
 
 /* end of the recursion, wrap first to make the CAR and use 
@@ -155,16 +154,33 @@
 /* </code-bloat> */
 #endif
 	
-	
+namespace internal{
+
+template <typename T>
+SEXP grow__dispatch( ::Rcpp::traits::false_type, const T& head, SEXP tail ){
+	return Rf_cons( wrap(head), tail ) ;	
+}
+
+template <typename T>
+SEXP grow__dispatch( ::Rcpp::traits::true_type, const T& head, SEXP tail ){
+	SEXP x;
+	x = PROTECT( Rf_cons( wrap( head.object) , tail) ) ;
+	SET_TAG( x, Rf_install( head.name.c_str() ) ); 
+	UNPROTECT(1); 
+	return x; 	
+}
+
+} // namespace internal
+
+
 /**
  * grows a pairlist. First wrap the head into a SEXP, then 
  * grow the tail pairlist
  */
-template<typename T>
+template <typename T>
 SEXP grow(const T& head, SEXP tail){
-	return Rf_cons( wrap(head), tail ) ;
+	return internal::grow__dispatch( typename traits::is_named<T>::type(), head, tail ) ;
 }
-template<> SEXP grow<Named>(const Named& head, SEXP tail) ;
 
 
 } // namespace Rcpp

Added: pkg/Rcpp/src/Rcpp/traits/named_object.h
===================================================================
--- pkg/Rcpp/src/Rcpp/traits/named_object.h	                        (rev 0)
+++ pkg/Rcpp/src/Rcpp/traits/named_object.h	2010-03-19 15:31:39 UTC (rev 918)
@@ -0,0 +1,42 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+/* :tabSize=4:indentSize=4:noTabs=false:folding=explicit:collapseFolds=1: */
+//
+// has_iterator.h: Rcpp R/C++ interface class library -- identify if a class has a nested iterator typedef
+//
+// 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__traits__named_object__h
+#define Rcpp__traits__named_object__h
+
+namespace Rcpp{
+namespace traits{
+
+template <typename T> class named_object {
+	public:
+		named_object( const std::string& name_, const T& o_) : 
+			name(name_), object(o_){} 
+		const std::string& name ;
+		const T& object ;
+} ;
+template <typename T> struct is_named : public false_type{} ;
+template <typename T> struct is_named< named_object<T> >   : public true_type {} ;
+
+} // namespace traits
+} // namespace Rcpp
+
+#endif 

Modified: pkg/Rcpp/src/RcppCommon.h
===================================================================
--- pkg/Rcpp/src/RcppCommon.h	2010-03-19 13:02:10 UTC (rev 917)
+++ pkg/Rcpp/src/RcppCommon.h	2010-03-19 15:31:39 UTC (rev 918)
@@ -195,6 +195,7 @@
 // DO NOT CHANGE THE ORDER OF THESE INCLUDES
 #include <Rcpp/traits/integral_constant.h>
 #include <Rcpp/traits/same_type.h>
+#include <Rcpp/traits/named_object.h>
 #include <Rcpp/traits/is_convertible.h>
 #include <Rcpp/traits/has_iterator.h>
 #include <Rcpp/traits/has_na.h>

Deleted: pkg/Rcpp/src/grow.cpp
===================================================================
--- pkg/Rcpp/src/grow.cpp	2010-03-19 13:02:10 UTC (rev 917)
+++ pkg/Rcpp/src/grow.cpp	2010-03-19 15:31:39 UTC (rev 918)
@@ -1,36 +0,0 @@
-// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
-//
-// grow.cpp: Rcpp R/C++ interface class library -- grow a pairlist
-//
-// 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/grow.h>
-
-namespace Rcpp{
-
-SEXP pairlist(){ return R_NilValue ; }
-
-template<> SEXP grow<Named>(const Named& head, SEXP tail){
-	SEXP x;
-	x = PROTECT( Rf_cons( head.getSEXP(), tail) ) ;
-	SET_TAG( x, Rf_install( head.getTag().c_str() ) ); 
-	UNPROTECT(1); 
-	return x; 
-}
-
-} //namespace Rcpp



More information about the Rcpp-commits mailing list