Add overloads for operator[].

Also implement operator[] in terms of operator[] const.
This commit is contained in:
King_DuckZ 2015-08-16 22:30:08 +02:00
parent 78312526ac
commit 7085a98995
2 changed files with 46 additions and 14 deletions

View file

@ -25,6 +25,8 @@
#include <array>
#include <cassert>
#include <functional>
#include <cstdint>
#include <limits>
namespace vwr {
template <typename V>
@ -149,10 +151,14 @@ 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[] ( 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;
vector_type& data ( void ) { return m_wrapped; }
const vector_type& data ( void ) const { return m_wrapped; }

View file

@ -48,27 +48,53 @@ namespace vwr {
}
template <typename V>
auto VecBase<V>::operator[] (std::size_t parIndex) -> scalar_type& {
return VecGetter<V>::get_at(m_wrapped, parIndex);
auto VecBase<V>::operator[] (int32_t parIndex) -> scalar_type& {
return const_cast<scalar_type&>(const_cast<const VecBase<V>*>(this)->operator[](parIndex));
}
template <typename V>
auto VecBase<V>::operator[] (int parIndex) -> scalar_type& {
auto VecBase<V>::operator[] (int64_t parIndex) -> scalar_type& {
return const_cast<scalar_type&>(const_cast<const VecBase<V>*>(this)->operator[](parIndex));
}
template <typename V>
auto VecBase<V>::operator[] (uint64_t parIndex) -> scalar_type& {
return const_cast<scalar_type&>(const_cast<const VecBase<V>*>(this)->operator[](parIndex));
}
template <typename V>
auto VecBase<V>::operator[] (uint32_t parIndex) -> scalar_type& {
return const_cast<scalar_type&>(const_cast<const VecBase<V>*>(this)->operator[](parIndex));
}
template <typename V>
auto VecBase<V>::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<uint32_t>(std::numeric_limits<int32_t>::max()) <= std::numeric_limits<std::size_t>::max(), "Can't support int32_t");
assert(parIndex >= 0);
return VecGetter<V>::get_at(m_wrapped, static_cast<std::size_t>(parIndex));
return VecGetter<V>::get_at(const_cast<vector_type&>(m_wrapped), static_cast<std::size_t>(parIndex));
}
template <typename V>
auto VecBase<V>::operator[] (std::size_t parIndex) const -> const scalar_type& {
return VecGetter<V>::get_at(const_cast<vector_type&>(m_wrapped), parIndex);
}
template <typename V>
auto VecBase<V>::operator[] (int parIndex) const -> const scalar_type& {
auto VecBase<V>::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<uint64_t>(std::numeric_limits<int64_t>::max()) <= std::numeric_limits<std::size_t>::max(), "Can't support int64_t");
assert(parIndex >= 0);
return VecGetter<V>::get_at(const_cast<vector_type&>(m_wrapped), static_cast<std::size_t>(parIndex));
}
template <typename V>
auto VecBase<V>::operator[] (uint64_t parIndex) const -> const scalar_type& {
static_assert(std::numeric_limits<uint64_t>::max() <= std::numeric_limits<std::size_t>::max(), "Can't support uint64_t");
return VecGetter<V>::get_at(const_cast<vector_type&>(m_wrapped), static_cast<std::size_t>(parIndex));
}
template <typename V>
auto VecBase<V>::operator[] (uint32_t parIndex) const -> const scalar_type& {
static_assert(std::numeric_limits<uint32_t>::max() <= std::numeric_limits<std::size_t>::max(), "Can't support uint32_t");
return VecGetter<V>::get_at(const_cast<vector_type&>(m_wrapped), static_cast<std::size_t>(parIndex));
}
template <typename V>
template <typename V2>
const typename std::enable_if<is_vec<V2>::value, V2>::type& VecBase<V>::cast() const {