Implement binary assignment operators for scalar types.

This commit is contained in:
King_DuckZ 2017-01-27 19:07:15 +00:00
parent 48d7b5ddec
commit 69e342460c
3 changed files with 62 additions and 0 deletions

View file

@ -165,12 +165,18 @@ namespace vwr {
template <typename V2> VecBase& operator-= ( const VecBase<V2>& parOther );
template <typename V2> VecBase& operator*= ( const VecBase<V2>& parOther );
template <typename V2> VecBase& operator/= ( const VecBase<V2>& parOther );
VecBase& operator+= ( const scalar_type& parOther );
VecBase& operator-= ( const scalar_type& parOther );
VecBase& operator*= ( const scalar_type& parOther );
VecBase& operator/= ( const scalar_type& parOther );
private:
template <size_type... I, typename... Args>
void assign_values (const bt::number_seq<size_type, I...>&, Args... parArgs);
template <typename Op, typename V2, size_type... I>
void assign_values_op (Op parOp, const bt::number_seq<size_type, I...>& parSeq, const VecBase<V2>& parOther);
template <typename Op, size_type... I>
void assign_values_op_scalar (Op parOp, const bt::number_seq<size_type, I...>& parSeq, const scalar_type& parOther);
vector_type m_wrapped;
};

View file

@ -61,6 +61,12 @@ namespace vwr {
this->assign_values(parSeq, parOp((*this)[I], parOther[I])...);
}
template <typename V>
template <typename Op, size_type... I>
void VecBase<V>::assign_values_op_scalar (Op parOp, const bt::number_seq<size_type, I...>& parSeq, const scalar_type& parOther) {
this->assign_values(parSeq, parOp((*this)[I], parOther)...);
}
template <typename V>
auto VecBase<V>::operator[] (size_type parIndex) -> scalar_type& {
return VecGetter<V>::get_at(m_wrapped, parIndex);
@ -126,6 +132,27 @@ namespace vwr {
return *this;
}
template <typename V>
VecBase<V>& VecBase<V>::operator+= (const scalar_type& parOther) {
this->assign_values_op_scalar(std::plus<scalar_type>(), bt::number_range<size_type, 0, VectorWrapperInfo<V>::dimensions>(), parOther);
return *this;
}
template <typename V>
VecBase<V>& VecBase<V>::operator-= (const scalar_type& parOther) {
this->assign_values_op_scalar(std::minus<scalar_type>(), bt::number_range<size_type, 0, VectorWrapperInfo<V>::dimensions>(), parOther);
return *this;
}
template <typename V>
VecBase<V>& VecBase<V>::operator*= (const scalar_type& parOther) {
this->assign_values_op_scalar(std::multiplies<scalar_type>(), bt::number_range<size_type, 0, VectorWrapperInfo<V>::dimensions>(), parOther);
return *this;
}
template <typename V>
VecBase<V>& VecBase<V>::operator/= (const scalar_type& parOther) {
this->assign_values_op_scalar(std::divides<scalar_type>(), bt::number_range<size_type, 0, VectorWrapperInfo<V>::dimensions>(), parOther);
return *this;
}
template <typename T>
typename VectorWrapperInfo<T>::scalar_type& VecGetter<T, true>::get_at (T& parVec, size_type parIndex) {
assert(parIndex < VectorWrapperInfo<T>::dimensions);

View file

@ -166,3 +166,32 @@ TEST(vwr, bin_assign_op) {
EXPECT_EQ(res, a);
}
}
TEST(vwr, bin_assign_op_scalar) {
using namespace vwr;
{
ivec3 a(2, 4, 8);
ivec3 res(2 + 20, 4 + 20, 8 + 20);
a += 20;
EXPECT_EQ(res, a);
}
{
ivec3 a(2, 4, 8);
ivec3 res(2 - 20, 4 - 20, 8 - 20);
a -= 20;
EXPECT_EQ(res, a);
}
{
ivec3 a(2, 4, 8);
ivec3 res(2 * 20, 4 * 20, 8 * 20);
a *= 20;
EXPECT_EQ(res, a);
}
{
ivec3 a(2, 4, 8);
ivec3 res(2 / 2, 4 / 2, 8 / 2);
a /= 2;
EXPECT_EQ(res, a);
}
}