[Rcpp-commits] r262 - in pkg: inst inst/unitTests src src/Rcpp
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sun Jan 3 17:01:14 CET 2010
Author: romain
Date: 2010-01-03 17:01:13 +0100 (Sun, 03 Jan 2010)
New Revision: 262
Added:
pkg/src/Named.cpp
pkg/src/Rcpp/Named.h
pkg/src/Rcpp/grow.h
pkg/src/Rcpp/pairlist.h
pkg/src/grow.cpp
pkg/src/pairlist.cpp
Modified:
pkg/inst/ChangeLog
pkg/inst/unitTests/runit.Language.R
pkg/src/RObject.cpp
pkg/src/Rcpp.h
pkg/src/Rcpp/Language.h
pkg/src/Rcpp/RObject.h
pkg/src/RcppCommon.cpp
pkg/src/RcppCommon.h
Log:
+Named class, pairlist variadic templates, grow template
Modified: pkg/inst/ChangeLog
===================================================================
--- pkg/inst/ChangeLog 2010-01-03 12:24:50 UTC (rev 261)
+++ pkg/inst/ChangeLog 2010-01-03 16:01:13 UTC (rev 262)
@@ -1,5 +1,26 @@
2010-01-03 Romain Francois <francoisromain at free.fr>
+ * src/Rcpp/pairlist.h : variadic templates to
+ recursively generate a pairlist
+ (CAR and CDR) from any number of "wrap("'able objects.
+ (factored out of Language)
+
+ * src/pairlist.cpp: end of recursion for the above
+
+ * src/Rcpp/grow.h : grow a pairlist with any wrappable
+ object. Used in pairlist
+
+ * src/grow.cpp : specific implementation for Named object
+ to allow tagging the head of the pairlist.
+
+ * src/Rcpp/Named.h : new class Named that is used to specify
+ named elements in a pairlist.
+
+ * src/Named.cpp: implementation
+
+ * src/Rcpp/RObject.h: added wrap(const char* const) RObject factory
+ method
+
* src/Rcpp/RObject.h: rework the garbage collection mechanism so that
it is automatic and hidden. methods preserve and release are now
private to the RObject class and the SEXP may only be changed using
@@ -8,8 +29,6 @@
* src/Rcpp/RObject.h: RObject gains assignment operators and copy constructors
-2010-01-03 Romain Francois <francoisromain at free.fr>
-
* src/RcppCommon.h: added the CXX0X define that controls whether
we can use C++0x features offered by the gcc. currently the define
is hardcoded, but this will eventually be a configure guess. The
@@ -20,8 +39,6 @@
code linking against Rcpp (inline code or packages) can take
advantage of it
-2010-01-03 Romain Francois <francoisromain at free.fr>
-
* src/Rcpp/Language.h : new class Rcpp::Language to manage calls
(LANGSXP SEXP)
@@ -29,15 +46,11 @@
* inst/unitTests/runit.Language.R: unit tests
-2010-01-03 Romain Francois <francoisromain at free.fr>
-
* src/Rcpp/Environment.h : added constructors and made the SEXP based
constructor smarter (using as.environment)
* inst/unitTests/runit.environments.R: more unit tests
-2010-01-03 Romain Francois <francoisromain at free.fr>
-
* src/Rcpp/Symbol.h: new class Rcpp::Symbol to encapsulate
symbols. This allows to use Symbol("rnorm") instead of the most cryptic
Rf_install("rnorm")
Modified: pkg/inst/unitTests/runit.Language.R
===================================================================
--- pkg/inst/unitTests/runit.Language.R 2010-01-03 12:24:50 UTC (rev 261)
+++ pkg/inst/unitTests/runit.Language.R 2010-01-03 16:01:13 UTC (rev 262)
@@ -34,3 +34,19 @@
checkException( funx(as.raw(1) ), msg = "Language not compatible with raw" )
}
+test.Language.variadic <- function(){
+ if( Rcpp:::canUseCXX0X() ){
+ funx <- cfunction(signature(), '
+ return Language( "rnorm", 10, 0.0, 2.0 ) ;
+ ', Rcpp=TRUE, verbose=TRUE, includes = "using namespace Rcpp;" )
+ checkEquals( funx(), call("rnorm", 10L, 0.0, 2.0 ),
+ msg = "variadic templates" )
+
+ funx <- cfunction(signature(), '
+ return Language( "rnorm", 10, Named("mean",0.0), 2.0 ) ;
+ ', Rcpp=TRUE, verbose=TRUE, includes = "using namespace Rcpp;" )
+ checkEquals( funx(), call("rnorm", 10L, mean = 0.0, 2.0 ),
+ msg = "variadic templates" )
+ }
+}
+
Added: pkg/src/Named.cpp
===================================================================
--- pkg/src/Named.cpp (rev 0)
+++ pkg/src/Named.cpp 2010-01-03 16:01:13 UTC (rev 262)
@@ -0,0 +1,34 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// Named.cpp: Rcpp R/C++ interface class library -- named object
+//
+// 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/Named.h>
+
+namespace Rcpp{
+
+ SEXP Named::getSEXP() const {
+ return object.asSexp() ;
+ }
+
+ std::string Named::getTag() const{
+ return tag ;
+ }
+
+} // namespace Rcpp
Modified: pkg/src/RObject.cpp
===================================================================
--- pkg/src/RObject.cpp 2010-01-03 12:24:50 UTC (rev 261)
+++ pkg/src/RObject.cpp 2010-01-03 16:01:13 UTC (rev 262)
@@ -75,7 +75,7 @@
}
return RObject(m_sexp) ;
}
-
+
RObject wrap(const bool & v){
logTxt("RObject from bool\n");
RObject o(Rf_ScalarLogical(v));
@@ -100,6 +100,10 @@
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()));
Modified: pkg/src/Rcpp/Language.h
===================================================================
--- pkg/src/Rcpp/Language.h 2010-01-03 12:24:50 UTC (rev 261)
+++ pkg/src/Rcpp/Language.h 2010-01-03 16:01:13 UTC (rev 262)
@@ -25,6 +25,8 @@
#include <RcppCommon.h>
#include <Rcpp/RObject.h>
#include <Rcpp/Symbol.h>
+#include <Rcpp/Named.h>
+#include <Rcpp/pairlist.h>
namespace Rcpp{
@@ -100,32 +102,12 @@
*/
#ifdef CXX0X
template<typename... Args>
- Language( const std::string& symbol, const Args&... args) : RObject(R_NilValue) {
+ Language( const std::string& symbol, const Args&... args) : RObject() {
/* TODO: should we first allocate and protect the list ?*/
- setSEXP( Rf_lcons( Symbol(symbol), pack( args... ) ) );
+ setSEXP( Rf_lcons( Symbol(symbol), pairlist( args... ) ) );
}
#endif
~Language() ;
-
-private:
-
- /* recursive packing of the arguments into a list,
- use first as the CAR and build the CDR from the remaining args recursively */
-#ifdef CXX0X
- template<typename T, typename... Args>
- SEXP pack( const T& first, const Args&... args ){
- return Rf_cons( wrap(first), pack( args... ) ) ;
- }
-#endif
-
- /* end of the recursion, wrap first to make the CAR and use
- R_NilValue as the CDR of the list */
-#ifdef CXX0X
-template<typename T>
- SEXP pack( const T& first){
- return Rf_cons( wrap(first), R_NilValue ) ;
- }
-#endif
};
} // namespace Rcpp
Added: pkg/src/Rcpp/Named.h
===================================================================
--- pkg/src/Rcpp/Named.h (rev 0)
+++ pkg/src/Rcpp/Named.h 2010-01-03 16:01:13 UTC (rev 262)
@@ -0,0 +1,69 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// Named.h: Rcpp R/C++ interface class library -- named object
+//
+// 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_Named_h
+#define Rcpp_Named_h
+
+#include <Rcpp/RObject.h>
+#include <Rcpp/Symbol.h>
+
+namespace Rcpp{
+
+/**
+ * Facility to have named arguments in pairlist, such
+ * as Language objects
+ */
+class Named{
+public:
+ /** default constructor */
+ Named( ) : object(R_NilValue), 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_NilValue), tag(tag){} ;
+
+#ifdef CXX0X
+ template<typename T>
+ Named( const std::string& tag, const T& value ) : object(R_NilValue), tag(tag) {
+ object = wrap( value ) ;
+ }
+#endif
+
+ SEXP getSEXP() const ;
+
+ std::string getTag() const ;
+
+private:
+ RObject object ;
+ std::string tag ;
+} ;
+
+} // namespace Rcpp
+
+#endif
Modified: pkg/src/Rcpp/RObject.h
===================================================================
--- pkg/src/Rcpp/RObject.h 2010-01-03 12:24:50 UTC (rev 261)
+++ pkg/src/Rcpp/RObject.h 2010-01-03 16:01:13 UTC (rev 262)
@@ -148,6 +148,7 @@
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);
@@ -162,7 +163,6 @@
RObject wrap(const std::set<std::string> & v);
RObject wrap(const std::set<Rbyte> & v);
-
} // namespace Rcpp
#endif
Added: pkg/src/Rcpp/grow.h
===================================================================
--- pkg/src/Rcpp/grow.h (rev 0)
+++ pkg/src/Rcpp/grow.h 2010-01-03 16:01:13 UTC (rev 262)
@@ -0,0 +1,42 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// grow.h: 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/>.
+
+#ifndef Rcpp_grow_h
+#define Rcpp_grow_h
+
+#include <RcppCommon.h>
+#include <Rcpp/Named.h>
+
+namespace Rcpp{
+
+/**
+ * grows a pairlist. First wrap the head into a SEXP, then
+ * grow the tail pairlist
+ */
+template<typename T>
+SEXP grow(const T& head, SEXP tail){
+ return Rf_cons( wrap(head), tail ) ;
+}
+SEXP grow(const Named& head, SEXP tail) ;
+
+} // namespace Rcpp
+
+#endif
Added: pkg/src/Rcpp/pairlist.h
===================================================================
--- pkg/src/Rcpp/pairlist.h (rev 0)
+++ pkg/src/Rcpp/pairlist.h 2010-01-03 16:01:13 UTC (rev 262)
@@ -0,0 +1,48 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// pairlist.h: Rcpp R/C++ interface class library -- variadic templates to create pairlists
+//
+// 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_pairlist_h
+#define Rcpp_pairlist_h
+
+#include <RcppCommon.h>
+#include <Rcpp/Named.h>
+#include <Rcpp/grow.h>
+
+namespace Rcpp{
+ /* recursive packing of the arguments into a list,
+ use first as the CAR and build the CDR from the remaining args recursively */
+#ifdef CXX0X
+ SEXP pairlist() ;
+ template<typename T, typename... Args>
+ SEXP pairlist( const T& first, const Args&... args ){
+ return grow(first, pairlist(args...) ) ;
+ }
+ /* end of the recursion, wrap first to make the CAR and use
+ R_NilValue as the CDR of the list */
+ template<typename T>
+ SEXP pairlist( const T& first){
+ return grow(first, R_NilValue ) ;
+ }
+#endif
+
+} // namespace Rcpp
+
+#endif
Modified: pkg/src/Rcpp.h
===================================================================
--- pkg/src/Rcpp.h 2010-01-03 12:24:50 UTC (rev 261)
+++ pkg/src/Rcpp.h 2010-01-03 16:01:13 UTC (rev 262)
@@ -42,11 +42,14 @@
#include <RcppVectorView.h>
/* new api */
+#include <Rcpp/pairlist.h>
+#include <Rcpp/grow.h>
#include <Rcpp/RObject.h>
#include <Rcpp/XPtr.h>
#include <Rcpp/Environment.h>
#include <Rcpp/Evaluator.h>
#include <Rcpp/Symbol.h>
#include <Rcpp/Language.h>
+#include <Rcpp/Named.h>
#endif
Modified: pkg/src/RcppCommon.cpp
===================================================================
--- pkg/src/RcppCommon.cpp 2010-01-03 12:24:50 UTC (rev 261)
+++ pkg/src/RcppCommon.cpp 2010-01-03 16:01:13 UTC (rev 262)
@@ -68,4 +68,12 @@
#endif
}
-
+/* this is mainly here so that variadic template errors show up
+ at compile time */
+SEXP test_named(){
+#ifdef CXX0X
+ return Rcpp::Language( "foobar", Rcpp::Named("foo", 2 ), 2, Rcpp::Named("bar", 10) ) ;
+#else
+ return R_NilValue ;
+#endif
+}
Modified: pkg/src/RcppCommon.h
===================================================================
--- pkg/src/RcppCommon.h 2010-01-03 12:24:50 UTC (rev 261)
+++ pkg/src/RcppCommon.h 2010-01-03 16:01:13 UTC (rev 262)
@@ -73,5 +73,6 @@
RcppExport SEXP test_variadic() ;
RcppExport SEXP canUseCXX0X() ;
+RcppExport SEXP test_named() ;
#endif
Added: pkg/src/grow.cpp
===================================================================
--- pkg/src/grow.cpp (rev 0)
+++ pkg/src/grow.cpp 2010-01-03 16:01:13 UTC (rev 262)
@@ -0,0 +1,35 @@
+// -*- 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>
+#include <Rcpp/Named.h>
+
+namespace Rcpp{
+
+SEXP grow(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
Added: pkg/src/pairlist.cpp
===================================================================
--- pkg/src/pairlist.cpp (rev 0)
+++ pkg/src/pairlist.cpp 2010-01-03 16:01:13 UTC (rev 262)
@@ -0,0 +1,28 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// pairlist.cpp: Rcpp R/C++ interface class library -- variadic templates to create pairlists
+//
+// 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/pairlist.h>
+
+namespace Rcpp{
+
+ SEXP pairlist(){ return R_NilValue ; }
+
+} // namespace Rcpp
More information about the Rcpp-commits
mailing list