[Rcpp-commits] r539 - in pkg: inst/doc src src/Rcpp/internal
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Mon Feb 1 11:52:38 CET 2010
Author: romain
Date: 2010-02-01 11:52:37 +0100 (Mon, 01 Feb 2010)
New Revision: 539
Added:
pkg/src/Rcpp/internal/convertible.h
pkg/src/Rcpp/internal/static.h
Modified:
pkg/inst/doc/Makefile
pkg/src/Rcpp/internal/wrap.h
pkg/src/RcppCommon.h
Log:
use the is_convertible type traits from tr1 or c++0x to identify if a type is convertible to SEXP rather than brutally force the conversion
Modified: pkg/inst/doc/Makefile
===================================================================
--- pkg/inst/doc/Makefile 2010-02-01 01:46:57 UTC (rev 538)
+++ pkg/inst/doc/Makefile 2010-02-01 10:52:37 UTC (rev 539)
@@ -12,10 +12,8 @@
Rcpp-unitTests.pdf:
rm -fr unitTests-results/*
cp unitTests/Rcpp-unitTests.Rnw .
- cp unitTests/Rcpp-unitTests.R .
- Rscript Rcpp-unitTests.R
- R CMD Sweave Rcpp-unitTests.Rnw
+ Rscript unitTests/Rcpp-unitTests.R
+ R CMD Sweave unitTests/Rcpp-unitTests.Rnw
Rscript -e "tools::texi2dvi( 'Rcpp-unitTests.tex', pdf = TRUE, clean = TRUE )"
- rm -fr Rcpp-unitTests.R
rm -fr Rcpp-unitTests.tex
Added: pkg/src/Rcpp/internal/convertible.h
===================================================================
--- pkg/src/Rcpp/internal/convertible.h (rev 0)
+++ pkg/src/Rcpp/internal/convertible.h 2010-02-01 10:52:37 UTC (rev 539)
@@ -0,0 +1,74 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+/* :tabSize=4:indentSize=4:noTabs=false:folding=explicit:collapseFolds=1: */
+//
+// convertible.h: Rcpp R/C++ interface class library -- dispatch the
+// implementation of is_convertible traits depending on whether we have
+// access to tr1 or c++0x
+//
+// 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_internal_is_convertible_h
+#define Rcpp_internal_is_convertible_h
+
+// this is a private header, do not include it directly
+
+namespace Rcpp{
+namespace internal{
+template <class _T, _T _V> struct integral_constant {
+ static const _T value = _V;
+ typedef _T value_type;
+ typedef integral_constant<_T,_V> type;
+ };
+ typedef integral_constant<bool, true> true_type;
+ typedef integral_constant<bool, false> false_type;
+}
+}
+
+#ifdef HAS_CXX0X
+// use the c++0x implementation
+#include <type_traits>
+namespace Rcpp{
+namespace internal{
+template <typename FROM, typename TO> struct is_convertible :
+ public integral_constant<bool,std::is_convertible<FROM,TO>::value>{} ;
+} // internal
+} // Rcpp
+#else
+#ifdef HAS_TR1
+#include <tr1/type_traits>
+// use the tr1 implementation
+namespace Rcpp{
+namespace internal{
+template <typename FROM, typename TO> struct is_convertible :
+ public integral_constant<bool,std::tr1::is_convertible<FROM,TO>::value>{} ;
+} // internal
+} // Rcpp
+#else
+// hope for the best
+namespace Rcpp{
+namespace internal{
+template <typename FROM, typename TO> struct is_convertible :
+ public integral_constant<bool,true>{} ;
+} // internal
+} // Rcpp
+#endif
+#endif
+
+
+
+#endif
Added: pkg/src/Rcpp/internal/static.h
===================================================================
--- pkg/src/Rcpp/internal/static.h (rev 0)
+++ pkg/src/Rcpp/internal/static.h 2010-02-01 10:52:37 UTC (rev 539)
@@ -0,0 +1,25 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+/* :tabSize=4:indentSize=4:noTabs=false:folding=explicit:collapseFolds=1: */
+//
+// wrap.h: Rcpp R/C++ interface class library -- wrap implementations
+//
+// 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_internal_static_h
+#define Rcpp_internal_static_h
+#endif
Modified: pkg/src/Rcpp/internal/wrap.h
===================================================================
--- pkg/src/Rcpp/internal/wrap.h 2010-02-01 01:46:57 UTC (rev 538)
+++ pkg/src/Rcpp/internal/wrap.h 2010-02-01 10:52:37 UTC (rev 539)
@@ -346,6 +346,27 @@
}
// }}}
+// {{{ unknown
+template <typename T>
+SEXP wrap_dispatch_unknown( const T& object, true_type ){
+ // here we know (or assume) that T is convertible to SEXP
+ SEXP x = object ;
+ return x ;
+}
+template <typename T>
+SEXP wrap_dispatch_unknown( const T& object, false_type){
+ // here we know that T is not convertible to SEXP
+#ifdef HAS_CXX0X
+ static_assert( !sizeof(T), "cannot convert type to SEXP" ) ;
+#else
+ // leave the cryptic message
+ SEXP x = object ;
+ return x ;
+#endif
+ return R_NilValue ; // -Wall
+}
+// }}}
+
// {{{ wrap dispatch
// generic wrap for stl containers
template <typename T> SEXP wrap_dispatch( const T& object, wrap_type_stl_container_tag ){
@@ -356,9 +377,9 @@
return primitive_wrap( object ) ;
}
// when we don't know how to deal with it, we try implicit conversion
+// if the type T is convertible to SEXP
template <typename T> SEXP wrap_dispatch( const T& object, wrap_type_unknown_tag ){
- SEXP x = object ;
- return x ;
+ return wrap_dispatch_unknown( object, typename is_convertible<T,SEXP>::type() ) ;
}
// }}}
Modified: pkg/src/RcppCommon.h
===================================================================
--- pkg/src/RcppCommon.h 2010-02-01 01:46:57 UTC (rev 538)
+++ pkg/src/RcppCommon.h 2010-02-01 10:52:37 UTC (rev 539)
@@ -27,6 +27,7 @@
#ifdef __GNUC__
#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#ifdef __GXX_EXPERIMENTAL_CXX0X__
+ #define HAS_CXX0X
#if GCC_VERSION >= 40300
#define HAS_VARIADIC_TEMPLATES
#endif
@@ -37,6 +38,7 @@
// FIXME: [romain] I did not actually check, tr1::unordered_map was
// probably introduced before GCC 4.3
#if GCC_VERSION >= 40300
+ #define HAS_TR1
#define HAS_TR1_UNORDERED_MAP
#define HAS_TR1_UNORDERED_SET
#endif
@@ -206,8 +208,9 @@
} // namespace Rcpp
+// DO NOT CHANGE THE ORDER OF THESE INCLUDES
+#include <Rcpp/internal/convertible.h>
#include <Rcpp/internal/wrap.h>
-
#include <Rcpp/RObject.h>
#endif
More information about the Rcpp-commits
mailing list