From 489bb4bfab6f09a55dfaeddc4c9870a608c0539f Mon Sep 17 00:00:00 2001 From: Kevin Ushey Date: Tue, 15 Apr 2014 16:27:44 -0700 Subject: [PATCH] Error on coerce by default when ctor-ing Vector from SEXP (#142) --- .gitignore | 4 ++ inst/include/Rcpp/vector/impl/SimpleVector.h | 68 +++++++++++--------- 2 files changed, 40 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index ea3036e..f52f1d9 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,7 @@ src/*.so .Rhistory Rcpp11.Rproj .Rproj.user + +\#* +.\#* + diff --git a/inst/include/Rcpp/vector/impl/SimpleVector.h b/inst/include/Rcpp/vector/impl/SimpleVector.h index 3f15317..39f613d 100644 --- a/inst/include/Rcpp/vector/impl/SimpleVector.h +++ b/inst/include/Rcpp/vector/impl/SimpleVector.h @@ -2,12 +2,12 @@ #define Rcpp__vector__impl_SimpleVector_h namespace Rcpp{ - + #undef VEC #define VEC Vector - + template < - int RTYPE, + int RTYPE, template class StoragePolicy > class Vector : @@ -16,12 +16,12 @@ namespace Rcpp{ public SlotProxyPolicy, public AttributeProxyPolicy, public NamesProxyPolicy, - public AttributesProxyPolicy, - public RObjectMethods, + public AttributesProxyPolicy, + public RObjectMethods, public VectorOffset { public: - + typedef typename traits::storage_type::type value_type ; typedef value_type stored_type ; typedef typename std::conditional::type init_type ; @@ -29,81 +29,85 @@ namespace Rcpp{ typedef const stored_type& const_reference ; typedef reference Proxy ; typedef const_reference const_Proxy ; - typedef value_type* iterator ; + typedef value_type* iterator ; typedef const value_type* const_iterator ; - + typedef internal::simple_name_proxy NameProxy ; typedef internal::simple_const_name_proxy const_NameProxy ; - + using VectorOffset::size ; - + RCPP_GENERATE_CTOR_ASSIGN(Vector) - - Vector( SEXP x ) { + + Vector( SEXP x, bool coerce = false) { + if (!coerce && TYPEOF(x) != RTYPE) { + stop("Attempted to construct a '%s' from a vector of different type '%s'", + CHAR(Rf_type2str(RTYPE)), CHAR(Rf_type2str(TYPEOF(x)))); + } Storage::set__( r_cast( x ) ) ; } - + Vector(int n) { reset(n) ; } - + Vector(){ reset(0); } - + Vector( int n, init_type x ) { reset(n); std::fill( begin(), end(), x) ; } - + Vector( std::initializer_list list ){ reset(list.size()); std::copy( list.begin(), list.end(), begin() ) ; } - + template Vector( const SugarVectorExpression& other ) { reset(other.size()) ; other.apply(*this) ; } - + template Vector& operator=( const SugarVectorExpression& expr ){ int n = expr.size() ; if( n != size() ){ - reset(n) ; + reset(n) ; } expr.apply(*this) ; return *this ; } - + private: - + inline void reset(int n){ - Storage::set__(Rf_allocVector(RTYPE, n) ) ; + Storage::set__(Rf_allocVector(RTYPE, n) ) ; } - + inline stored_type* data(){ - return reinterpret_cast( DATAPTR(Storage::get__()) ); + return reinterpret_cast( DATAPTR(Storage::get__()) ); } inline const stored_type* data() const{ - return reinterpret_cast( DATAPTR(Storage::get__()) ); + return reinterpret_cast( DATAPTR(Storage::get__()) ); } - + public: inline iterator begin() { return data() ; } inline iterator end() { return data() + size() ; } - + inline const_iterator begin() const{ return data() ; } inline const_iterator end() const{ return data() + size() ; } - + inline Proxy operator[](int i){ return *( data() + i ) ;} inline const_Proxy operator[](int i) const { return *( data() + i ); } - + template static Vector create(Args... args) { - return typename create_type::type( args... ) ; + return typename create_type::type( args... ) ; } - + inline NameProxy operator[]( const std::string& name ){ return NameProxy( *this, name ) ; } @@ -113,7 +117,7 @@ namespace Rcpp{ } ; #undef VEC - + }