From 981ec02afda65dee1717fcf117187bc89bb0d636 Mon Sep 17 00:00:00 2001 From: Michele Santullo Date: Mon, 17 Aug 2015 13:34:17 +0200 Subject: [PATCH] Add support for custom dimension type. Also remove overloads of operator[] and use a template instead. --- .../include/vectorwrapper/vectorwrapper.hpp | 40 +++++++------ .../include/vectorwrapper/vectorwrapper.inl | 59 ++++--------------- 2 files changed, 34 insertions(+), 65 deletions(-) diff --git a/lib/vectorwrapper/include/vectorwrapper/vectorwrapper.hpp b/lib/vectorwrapper/include/vectorwrapper/vectorwrapper.hpp index 4e8065d..77221dd 100644 --- a/lib/vectorwrapper/include/vectorwrapper/vectorwrapper.hpp +++ b/lib/vectorwrapper/include/vectorwrapper/vectorwrapper.hpp @@ -29,10 +29,16 @@ #include namespace vwr { +#if !defined(VWR_DIM_TYPE) + typedef std::size_t dim_type; +#else + typedef VWR_DIM_TYPE dim_type; +#endif + template struct VectorWrapperInfo; - template ::dimensions> + template ::dimensions> class Vec; namespace implem { @@ -42,12 +48,12 @@ namespace vwr { define_has_method(get_at, GetAt); define_has_enum(cast_ignore_trailing_properties, CastIgnoreTrailingProperties); - template + template Vec& assign ( Vec& parLeft, const Vec& parRight ); template Vec& assign_same_type ( Vec& parLeft, const Vec& parRight ); - template struct get_offset_enum_from_index; + template struct get_offset_enum_from_index; template struct get_offset_enum_from_index { enum { value = VectorWrapperInfo::offset_x }; }; @@ -61,7 +67,7 @@ namespace vwr { enum { value = VectorWrapperInfo::offset_w }; }; - template ::dimensions> struct min_offset { + template ::dimensions> struct min_offset { enum { value = ( static_cast(get_offset_enum_from_index::value) < static_cast(min_offset::value) ? @@ -77,7 +83,7 @@ namespace vwr { template < typename T, typename U, - std::size_t S=( + dim_type S=( static_cast(VectorWrapperInfo::dimensions) < static_cast(VectorWrapperInfo::dimensions) ? static_cast(VectorWrapperInfo::dimensions) : @@ -151,14 +157,10 @@ namespace vwr { VecBase ( scalar_type parX, scalar_type parY, Args... parArgs ); ~VecBase ( void ) = default; - scalar_type& operator[] ( int32_t parIndex ); - scalar_type& operator[] ( int64_t parIndex ); - scalar_type& operator[] ( uint32_t parIndex ); - scalar_type& operator[] ( uint64_t parIndex ); - const scalar_type& operator[] ( int32_t parIndex ) const; - const scalar_type& operator[] ( int64_t parIndex ) const; - const scalar_type& operator[] ( uint32_t parIndex ) const; - const scalar_type& operator[] ( uint64_t parIndex ) const; + template + scalar_type& operator[] ( I parIndex ); + template + const scalar_type& operator[] ( I parIndex ) const; vector_type& data ( void ) { return m_wrapped; } const vector_type& data ( void ) const { return m_wrapped; } @@ -181,7 +183,7 @@ namespace vwr { vector_type m_wrapped; }; - template ::dimensions> + template ::dimensions> struct offsets_array_wrapper { template offsets_array_wrapper ( const bt::index_seq& ); @@ -193,19 +195,19 @@ namespace vwr { class VecGetter; template struct VecGetter { - static typename VectorWrapperInfo::scalar_type& get_at ( T& parVec, std::size_t parIndex ); + static typename VectorWrapperInfo::scalar_type& get_at ( T& parVec, dim_type parIndex ); }; template struct VecGetter { private: static_assert(HasGetAtMethod>::value, "You must provide a get_at() static method for this vector_type"); typedef typename VectorWrapperInfo::scalar_type scalar_type; - using return_type = decltype(std::declval>().get_at(std::declval(), std::declval())); + using return_type = decltype(std::declval>().get_at(std::declval(), std::declval())); static_assert(not std::is_rvalue_reference::value, "rvalue ref return types not implemented"); static_assert(std::is_lvalue_reference::value, "Read-only vectors not implemented"); public: - static typename VectorWrapperInfo::scalar_type& get_at ( T& parVec, std::size_t parIndex ); + static typename VectorWrapperInfo::scalar_type& get_at ( T& parVec, dim_type parIndex ); }; template struct Vec1Promotion; @@ -261,7 +263,7 @@ namespace vwr { lower_vector_type yz ( void ) const; }; - template + template struct VecAccessors; //Workaround for visual studio - VecAccessors should inherit from @@ -305,7 +307,7 @@ namespace vwr { bool operator_op ( const T& parLeft, const Vec& parRight ); } //namespace implem - template + template class Vec : public implem::VecBase { public: enum { diff --git a/lib/vectorwrapper/include/vectorwrapper/vectorwrapper.inl b/lib/vectorwrapper/include/vectorwrapper/vectorwrapper.inl index 5cc69ff..24212f6 100644 --- a/lib/vectorwrapper/include/vectorwrapper/vectorwrapper.inl +++ b/lib/vectorwrapper/include/vectorwrapper/vectorwrapper.inl @@ -42,57 +42,24 @@ namespace vwr { VecGetter::get_at(m_wrapped, 1) = parY; const scalar_type args[sizeof...(Args)] = {parArgs...}; - for (std::size_t z = 0; z < sizeof...(Args); ++z) { + for (dim_type z = 0; z < sizeof...(Args); ++z) { VecGetter::get_at(m_wrapped, z + 2) = args[z]; } } template - auto VecBase::operator[] (int32_t parIndex) -> scalar_type& { + template + auto VecBase::operator[] (I parIndex) -> scalar_type& { return const_cast(const_cast*>(this)->operator[](parIndex)); } template - auto VecBase::operator[] (int64_t parIndex) -> scalar_type& { - return const_cast(const_cast*>(this)->operator[](parIndex)); - } - - template - auto VecBase::operator[] (uint64_t parIndex) -> scalar_type& { - return const_cast(const_cast*>(this)->operator[](parIndex)); - } - - template - auto VecBase::operator[] (uint32_t parIndex) -> scalar_type& { - return const_cast(const_cast*>(this)->operator[](parIndex)); - } - - template - auto VecBase::operator[] (int32_t parIndex) const -> const scalar_type& { - static_assert(sizeof(int32_t) <= sizeof(std::size_t), "Can't support int"); - static_assert(static_cast(std::numeric_limits::max()) <= std::numeric_limits::max(), "Can't support int32_t"); + template + auto VecBase::operator[] (I parIndex) const -> const scalar_type& { + static_assert(sizeof(I) <= sizeof(dim_type), "Index type is too large"); + static_assert(static_cast::type>(std::numeric_limits::max()) <= std::numeric_limits::max(), "This index type can't hold dim_type's max"); assert(parIndex >= 0); - return VecGetter::get_at(const_cast(m_wrapped), static_cast(parIndex)); - } - - template - auto VecBase::operator[] (int64_t parIndex) const -> const scalar_type& { - static_assert(sizeof(int64_t) <= sizeof(std::size_t), "Can't support int"); - static_assert(static_cast(std::numeric_limits::max()) <= std::numeric_limits::max(), "Can't support int64_t"); - assert(parIndex >= 0); - return VecGetter::get_at(const_cast(m_wrapped), static_cast(parIndex)); - } - - template - auto VecBase::operator[] (uint64_t parIndex) const -> const scalar_type& { - static_assert(std::numeric_limits::max() <= std::numeric_limits::max(), "Can't support uint64_t"); - return VecGetter::get_at(const_cast(m_wrapped), static_cast(parIndex)); - } - - template - auto VecBase::operator[] (uint32_t parIndex) const -> const scalar_type& { - static_assert(std::numeric_limits::max() <= std::numeric_limits::max(), "Can't support uint32_t"); - return VecGetter::get_at(const_cast(m_wrapped), static_cast(parIndex)); + return VecGetter::get_at(const_cast(m_wrapped), static_cast(parIndex)); } template @@ -192,7 +159,7 @@ namespace vwr { } template - typename VectorWrapperInfo::scalar_type& VecGetter::get_at (T& parVec, std::size_t parIndex) { + typename VectorWrapperInfo::scalar_type& VecGetter::get_at (T& parVec, dim_type parIndex) { assert(parIndex < VectorWrapperInfo::dimensions); typedef T vector_type; typedef typename VectorWrapperInfo::scalar_type scalar_type; @@ -202,14 +169,14 @@ namespace vwr { } template - typename VectorWrapperInfo::scalar_type& VecGetter::get_at (T& parVec, std::size_t parIndex) { + typename VectorWrapperInfo::scalar_type& VecGetter::get_at (T& parVec, dim_type parIndex) { assert(parIndex < VectorWrapperInfo::dimensions); return VectorWrapperInfo::get_at(parIndex, parVec); } - template + template inline Vec& assign (Vec& parLeft, const Vec& parRight) { - for (std::size_t z = 0; z < D; ++z) { + for (dim_type z = 0; z < D; ++z) { parLeft[z] = parRight[z]; } return parLeft; @@ -335,7 +302,7 @@ namespace vwr { return this_vec[2]; } - template + template template offsets_array_wrapper::offsets_array_wrapper (const bt::index_seq&) : offsets({get_offset_enum_from_index::value...})