diff --git a/include/vectorwrapper/size_type.hpp b/include/vectorwrapper/size_type.hpp new file mode 100644 index 0000000..28fc265 --- /dev/null +++ b/include/vectorwrapper/size_type.hpp @@ -0,0 +1,33 @@ +/* + * Copyright 2015-2016 Michele "King_DuckZ" Santullo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef id85B9A9D5E08C4C97B0E3E69757C6AA54 +#define id85B9A9D5E08C4C97B0E3E69757C6AA54 + +#include +#if defined(VWR_SIZE_TYPE) +# include +#endif + +namespace vwr { +#if !defined(VWR_SIZE_TYPE) + typedef std::size_t size_type; +#else + typedef VWR_SIZE_TYPE size_type; +#endif +} //namespace vwr + +#endif diff --git a/include/vectorwrapper/vector_cast.hpp b/include/vectorwrapper/vector_cast.hpp index e50bf35..18b5d15 100644 --- a/include/vectorwrapper/vector_cast.hpp +++ b/include/vectorwrapper/vector_cast.hpp @@ -17,12 +17,14 @@ #ifndef idE24EF7737A7F4882B2BF254F0A3EEC34 #define idE24EF7737A7F4882B2BF254F0A3EEC34 -#include +#include "vectorwrapper/sequence_bt.hpp" +#include "vectorwrapper/size_type.hpp" +#include "vectorwrapper/vectorwrapper.hpp" namespace vwr { namespace implem { - template - Vec vector_cast (const Vec& parVec, Vec*, std::index_sequence) { + template + Vec vector_cast (const Vec& parVec, Vec*, bt::number_seq) { static_assert( static_cast(Vec::dimensions) == static_cast(Vec::dimensions), "Mismatching dimensions" @@ -37,7 +39,7 @@ namespace vwr { template TOVec vector_cast (const Vec& parVec) { TOVec* const to = nullptr; - return implem::vector_cast(parVec, to, std::make_index_sequence::dimensions>()); + return implem::vector_cast(parVec, to, bt::number_range::dimensions>()); } } //namespace vwr diff --git a/include/vectorwrapper/vectorops.hpp b/include/vectorwrapper/vectorops.hpp index 1a0148a..1e59495 100644 --- a/include/vectorwrapper/vectorops.hpp +++ b/include/vectorwrapper/vectorops.hpp @@ -17,18 +17,19 @@ #ifndef id8949C80C36BA42CABC49EA4C1DB54BC7 #define id8949C80C36BA42CABC49EA4C1DB54BC7 -#include "vectorwrapper.hpp" +#include "vectorwrapper/vectorwrapper.hpp" +#include "vectorwrapper/size_type.hpp" #include #include namespace vwr { - template + template typename std::common_type::scalar_type, typename Vec::scalar_type>::type dot ( const Vec& parLeft, const Vec& parRight ); - template + template inline typename std::common_type::scalar_type, typename Vec::scalar_type>::type dot (const Vec& parLeft, const Vec& parRight) { auto retval = parLeft.x() * parRight.x(); - for (std::size_t z = 1; z < S; ++z) { + for (size_type z = 1; z < S; ++z) { retval += parLeft[z] * parRight[z]; } return retval; diff --git a/include/vectorwrapper/vectorwrapper.hpp b/include/vectorwrapper/vectorwrapper.hpp index 5339c4e..4892f96 100644 --- a/include/vectorwrapper/vectorwrapper.hpp +++ b/include/vectorwrapper/vectorwrapper.hpp @@ -19,7 +19,7 @@ #include "vectorwrapper/has_method.hpp" #include "vectorwrapper/sequence_bt.hpp" -#include +#include "vectorwrapper/size_type.hpp" #include #include #include @@ -29,7 +29,7 @@ namespace vwr { template struct VectorWrapperInfo; - template ::dimensions> + template ::dimensions> class Vec; namespace implem { @@ -40,13 +40,13 @@ namespace vwr { define_has_enum(cast_ignore_trailing_properties, CastIgnoreTrailingProperties); #if defined(VWR_WITH_IMPLICIT_CONVERSIONS) - template + template Vec& assign ( Vec& parLeft, const Vec& parRight ); #endif 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 }; }; @@ -60,7 +60,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) ? @@ -76,7 +76,7 @@ namespace vwr { template < typename T, typename U, - std::size_t S=( + size_type S=( static_cast(VectorWrapperInfo::dimensions) < static_cast(VectorWrapperInfo::dimensions) ? static_cast(VectorWrapperInfo::dimensions) : @@ -150,10 +150,8 @@ namespace vwr { VecBase ( scalar_type parX, scalar_type parY, Args... parArgs ); ~VecBase ( void ) = default; - scalar_type& operator[] ( std::size_t parIndex ); - scalar_type& operator[] ( int parIndex ); - const scalar_type& operator[] ( std::size_t parIndex ) const; - const scalar_type& operator[] ( int parIndex ) const; + scalar_type& operator[] ( size_type parIndex ); + const scalar_type& operator[] ( size_type parIndex ) const; vector_type& data ( void ) { return m_wrapped; } const vector_type& data ( void ) const { return m_wrapped; } @@ -169,16 +167,16 @@ namespace vwr { template VecBase& operator/= ( const VecBase& parOther ); private: - template - void assign_values (const bt::index_seq&, Args... parArgs); + template + void assign_values (const bt::number_seq&, Args... parArgs); vector_type m_wrapped; }; - template ::dimensions> + template ::dimensions> struct offsets_array_wrapper { - template - offsets_array_wrapper ( const bt::index_seq& ); + template + offsets_array_wrapper ( const bt::number_seq& ); const std::array offsets; }; @@ -187,7 +185,7 @@ namespace vwr { struct 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, size_type parIndex ); }; template struct VecGetter { @@ -195,14 +193,14 @@ namespace vwr { static_assert(HasGetAtMethod>::value, "You must provide a get_at() static method for this vector_type"); typedef typename VectorWrapperInfo::scalar_type scalar_type; typedef T vector_type; - using get_at_func = decltype(&VectorWrapperInfo::get_at)(std::size_t, vector_type&); + using get_at_func = decltype(&VectorWrapperInfo::get_at)(size_type, vector_type&); using get_at_rettype = typename std::result_of::type; 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 get_at_rettype get_at ( T& parVec, std::size_t parIndex ); + static get_at_rettype get_at ( T& parVec, size_type parIndex ); }; template struct Vec1Promotion; @@ -258,7 +256,7 @@ namespace vwr { lower_vector_type yz ( void ) const; }; - template + template struct VecAccessors; //Workaround for visual studio - VecAccessors should inherit from @@ -295,7 +293,7 @@ namespace vwr { }; } //namespace implem - template + template class Vec : public implem::VecBase { public: enum { diff --git a/include/vectorwrapper/vectorwrapper.inl b/include/vectorwrapper/vectorwrapper.inl index 9b622e2..e68883f 100644 --- a/include/vectorwrapper/vectorwrapper.inl +++ b/include/vectorwrapper/vectorwrapper.inl @@ -23,7 +23,7 @@ namespace vwr { template template VecBase::VecBase (const T& parInit, typename std::enable_if::value and not std::is_same::value, bool>::type) { - for (std::size_t z = 0; z < VectorWrapperInfo::dimensions; ++z) { + for (size_type z = 0; z < VectorWrapperInfo::dimensions; ++z) { VecGetter::get_at(m_wrapped, z) = parInit; } } @@ -41,12 +41,12 @@ namespace vwr { VecGetter::get_at(m_wrapped, 0) = parX; VecGetter::get_at(m_wrapped, 1) = parY; - assign_values(bt::index_range<2, dimensions>(), std::forward(parArgs)...); + assign_values(bt::number_range(), std::forward(parArgs)...); } template - template - void VecBase::assign_values (const bt::index_seq&, Args... parArgs) { + template + void VecBase::assign_values (const bt::number_seq&, Args... parArgs) { static_assert(sizeof...(I) == sizeof...(Args), "Argument count and indices count mismatch"); std::initializer_list t { @@ -56,27 +56,15 @@ namespace vwr { } template - auto VecBase::operator[] (std::size_t parIndex) -> scalar_type& { + auto VecBase::operator[] (size_type parIndex) -> scalar_type& { return VecGetter::get_at(m_wrapped, parIndex); } template - auto VecBase::operator[] (int parIndex) -> scalar_type& { - assert(parIndex >= 0); - return VecGetter::get_at(m_wrapped, static_cast(parIndex)); - } - - template - auto VecBase::operator[] (std::size_t parIndex) const -> const scalar_type& { + auto VecBase::operator[] (size_type parIndex) const -> const scalar_type& { return VecGetter::get_at(const_cast(m_wrapped), parIndex); } - template - auto VecBase::operator[] (int parIndex) const -> const scalar_type& { - assert(parIndex >= 0); - return VecGetter::get_at(const_cast(m_wrapped), static_cast(parIndex)); - } - template template const typename std::enable_if::value, V2>::type& VecBase::cast() const { @@ -107,7 +95,7 @@ namespace vwr { template VecBase& VecBase::operator+= (const VecBase& parOther) { static_assert(static_cast(VectorWrapperInfo::dimensions) == static_cast(VectorWrapperInfo::dimensions), "Dimensions mismatch"); - for (int z = 0; z < VectorWrapperInfo::dimensions; ++z) { + for (size_type z = 0; z < VectorWrapperInfo::dimensions; ++z) { (*this)[z] += parOther[z]; } return *this; @@ -116,7 +104,7 @@ namespace vwr { template VecBase& VecBase::operator-= (const VecBase& parOther) { static_assert(static_cast(VectorWrapperInfo::dimensions) == static_cast(VectorWrapperInfo::dimensions), "Dimensions mismatch"); - for (int z = 0; z < VectorWrapperInfo::dimensions; ++z) { + for (size_type z = 0; z < VectorWrapperInfo::dimensions; ++z) { (*this)[z] -= parOther[z]; } return *this; @@ -125,7 +113,7 @@ namespace vwr { template VecBase& VecBase::operator*= (const VecBase& parOther) { static_assert(static_cast(VectorWrapperInfo::dimensions) == static_cast(VectorWrapperInfo::dimensions), "Dimensions mismatch"); - for (int z = 0; z < VectorWrapperInfo::dimensions; ++z) { + for (size_type z = 0; z < VectorWrapperInfo::dimensions; ++z) { (*this)[z] *= parOther[z]; } return *this; @@ -141,24 +129,24 @@ 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, size_type parIndex) { assert(parIndex < VectorWrapperInfo::dimensions); typedef T vector_type; typedef typename VectorWrapperInfo::scalar_type scalar_type; static_assert(std::is_standard_layout::value, "Can't use this function with this vector_type"); - const offsets_array_wrapper oaw((bt::index_range<0, VectorWrapperInfo::dimensions>())); + const offsets_array_wrapper oaw((bt::number_range::dimensions>())); return *reinterpret_cast(reinterpret_cast(&parVec) + oaw.offsets[parIndex]); } template - auto VecGetter::get_at (T& parVec, std::size_t parIndex) -> get_at_rettype { + auto VecGetter::get_at (T& parVec, size_type parIndex) -> get_at_rettype { 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 (size_type z = 0; z < D; ++z) { parLeft[z] = parRight[z]; } return parLeft; @@ -284,9 +272,9 @@ namespace vwr { return this_vec[2]; } - template - template - offsets_array_wrapper::offsets_array_wrapper (const bt::index_seq&) : + template + template + offsets_array_wrapper::offsets_array_wrapper (const bt::number_seq&) : offsets({get_offset_enum_from_index::value...}) { static_assert(sizeof...(I) == S, "Bug?"); @@ -309,7 +297,7 @@ namespace vwr { inline bool operator== (const Vec& parLeft, const Vec& parRight) { static_assert(static_cast(VectorWrapperInfo::dimensions) == static_cast(VectorWrapperInfo::dimensions), "Dimensions mismatch"); bool retval = true; - for (int z = 0; z < VectorWrapperInfo::dimensions; ++z) { + for (size_type z = 0; z < VectorWrapperInfo::dimensions; ++z) { retval &= (parLeft[z] == parRight[z]); } return retval; @@ -318,7 +306,7 @@ namespace vwr { inline bool operator< (const Vec& parLeft, const Vec& parRight) { static_assert(static_cast(VectorWrapperInfo::dimensions) == static_cast(VectorWrapperInfo::dimensions), "Dimensions mismatch"); bool retval = true; - for (int z = 0; z < VectorWrapperInfo::dimensions; ++z) { + for (size_type z = 0; z < VectorWrapperInfo::dimensions; ++z) { retval &= (parLeft[z] < parRight[z]); } return retval; @@ -327,7 +315,7 @@ namespace vwr { template inline bool operator== (const Vec& parLeft, const typename VectorWrapperInfo::scalar_type& parRight) { bool retval = true; - for (int z = 0; z < VectorWrapperInfo::dimensions; ++z) { + for (size_type z = 0; z < VectorWrapperInfo::dimensions; ++z) { retval &= (parLeft[z] == parRight); } return retval; @@ -335,7 +323,7 @@ namespace vwr { template inline bool operator< ( const Vec& parLeft, const typename VectorWrapperInfo::scalar_type& parRight) { bool retval = true; - for (int z = 0; z < VectorWrapperInfo::dimensions; ++z) { + for (size_type z = 0; z < VectorWrapperInfo::dimensions; ++z) { retval &= (parLeft[z] < parRight); } return retval; @@ -366,7 +354,7 @@ namespace vwr { static_assert(static_cast(VectorWrapperInfo::dimensions) == static_cast(VectorWrapperInfo::dimensions), "Dimensions mismatch"); Vec::type> retval; - for (int z = 0; z < VectorWrapperInfo::dimensions; ++z) { + for (size_type z = 0; z < VectorWrapperInfo::dimensions; ++z) { #if defined(VWR_STATIC_CAST_RESULTS) retval[z] = static_cast(parLeft[z] + parRight[z]); #else @@ -383,7 +371,7 @@ namespace vwr { static_assert(static_cast(VectorWrapperInfo::dimensions) == static_cast(VectorWrapperInfo::dimensions), "Dimensions mismatch"); Vec::type> retval; - for (int z = 0; z < VectorWrapperInfo::dimensions; ++z) { + for (size_type z = 0; z < VectorWrapperInfo::dimensions; ++z) { #if defined(VWR_STATIC_CAST_RESULTS) retval[z] = static_cast(parLeft[z] - parRight[z]); #else @@ -400,7 +388,7 @@ namespace vwr { static_assert(static_cast(VectorWrapperInfo::dimensions) == static_cast(VectorWrapperInfo::dimensions), "Dimensions mismatch"); Vec::type> retval; - for (int z = 0; z < VectorWrapperInfo::dimensions; ++z) { + for (size_type z = 0; z < VectorWrapperInfo::dimensions; ++z) { #if defined(VWR_STATIC_CAST_RESULTS) retval[z] = static_cast(parLeft[z] * parRight[z]); #else @@ -417,7 +405,7 @@ namespace vwr { static_assert(static_cast(VectorWrapperInfo::dimensions) == static_cast(VectorWrapperInfo::dimensions), "Dimensions mismatch"); Vec::type> retval; - for (int z = 0; z < VectorWrapperInfo::dimensions; ++z) { + for (size_type z = 0; z < VectorWrapperInfo::dimensions; ++z) { #if defined(VWR_STATIC_CAST_RESULTS) retval[z] = static_cast(parLeft[z] / parRight[z]); #else diff --git a/test/unit/sample_vectors.hpp b/test/unit/sample_vectors.hpp index defc683..344cd1e 100644 --- a/test/unit/sample_vectors.hpp +++ b/test/unit/sample_vectors.hpp @@ -62,7 +62,7 @@ namespace vwr { typedef float scalar_type; typedef SimpleVector2 higher_vector_type; - static scalar_type& get_at (std::size_t, vector_type& parVector) { + static scalar_type& get_at (size_type, vector_type& parVector) { return parVector; } };