Declare ArrayPortalValueReference::operator= as const

Declaring operator= as const seems a little weird because we are
changing the value. But remember that ArrayPortalReference is only a
reference class. The reference itself does not change, just the thing
that it is referencing. So declaring as const is correct and necessary
so that you can set the value of a reference returned from a function
(which is a right hand side value).
This commit is contained in:
Kenneth Moreland 2019-02-21 08:32:38 -07:00
parent b141f75150
commit 9e02cd33af

@ -63,14 +63,24 @@ struct ArrayPortalValueReference
VTKM_SUPPRESS_EXEC_WARNINGS
VTKM_EXEC_CONT
void Set(ValueType&& value) { this->Portal.Set(this->Index, std::move(value)); }
operator ValueType(void) const { return this->Portal.Get(this->Index); }
// Declaring Set as const seems a little weird because we are changing the value. But remember
// that ArrayPortalReference is only a reference class. The reference itself does not change,
// just the thing that it is referencing. So declaring as const is correct and necessary so that
// you can set the value of a reference returned from a function (which is a right hand side
// value).
VTKM_SUPPRESS_EXEC_WARNINGS
VTKM_EXEC_CONT
void Set(const ValueType& value) { this->Portal.Set(this->Index, value); }
void Set(ValueType&& value) const { this->Portal.Set(this->Index, std::move(value)); }
VTKM_SUPPRESS_EXEC_WARNINGS
VTKM_EXEC_CONT
void Set(const ValueType& value) const { this->Portal.Set(this->Index, value); }
VTKM_CONT
void Swap(ArrayPortalValueReference<ArrayPortalType>& rhs) throw()
void Swap(const ArrayPortalValueReference<ArrayPortalType>& rhs) const throw()
{
//we need use the explicit type not a proxy temp object
//A proxy temp object would point to the same underlying data structure
@ -80,10 +90,35 @@ struct ArrayPortalValueReference
rhs = aValue;
}
// Declaring operator= as const seems a little weird because we are changing the value. But
// remember that ArrayPortalReference is only a reference class. The reference itself does not
// change, just the thing that it is referencing. So declaring as const is correct and necessary
// so that you can set the value of a reference returned from a function (which is a right hand
// side value).
VTKM_SUPPRESS_EXEC_WARNINGS
VTKM_EXEC_CONT
const ArrayPortalValueReference<ArrayPortalType>& operator=(ValueType&& value) const
{
this->Set(std::move(value));
return *this;
}
VTKM_SUPPRESS_EXEC_WARNINGS
VTKM_EXEC_CONT
const ArrayPortalValueReference<ArrayPortalType>& operator=(const ValueType& value) const
{
this->Set(value);
return *this;
}
// This special overload of the operator= is to override the behavior of the default operator=
// (which has the wrong behavior) with behavior consistent with the other operator= methods.
// It is not declared const because the default operator= is not declared const. If you try
// to do this assignment with a const object, you will get one of the operator= above.
VTKM_SUPPRESS_EXEC_WARNINGS
template <typename OtherPortalType>
VTKM_EXEC_CONT ArrayPortalValueReference<ArrayPortalType>& operator=(
const ArrayPortalValueReference<OtherPortalType>& rhs)
const ArrayPortalValueReference<ArrayPortalType>& rhs)
{
this->Set(static_cast<ValueType>(rhs.Portal.Get(rhs.Index)));
return *this;
@ -91,19 +126,7 @@ struct ArrayPortalValueReference
VTKM_SUPPRESS_EXEC_WARNINGS
template <typename T>
VTKM_EXEC_CONT ValueType operator=(T&& value)
{
this->Set(std::forward<T>(value));
return value;
}
VTKM_SUPPRESS_EXEC_WARNINGS
VTKM_EXEC_CONT
operator ValueType(void) const { return this->Portal.Get(this->Index); }
VTKM_SUPPRESS_EXEC_WARNINGS
template <typename T>
VTKM_EXEC_CONT ValueType operator+=(const T& rhs)
VTKM_EXEC_CONT ValueType operator+=(const T& rhs) const
{
ValueType lhs = this->Get();
lhs += rhs;
@ -112,7 +135,7 @@ struct ArrayPortalValueReference
}
VTKM_SUPPRESS_EXEC_WARNINGS
template <typename T>
VTKM_EXEC_CONT ValueType operator+=(const ArrayPortalValueReference<T>& rhs)
VTKM_EXEC_CONT ValueType operator+=(const ArrayPortalValueReference<T>& rhs) const
{
ValueType lhs = this->Get();
lhs += rhs.Get();
@ -122,7 +145,7 @@ struct ArrayPortalValueReference
VTKM_SUPPRESS_EXEC_WARNINGS
template <typename T>
VTKM_EXEC_CONT ValueType operator-=(const T& rhs)
VTKM_EXEC_CONT ValueType operator-=(const T& rhs) const
{
ValueType lhs = this->Get();
lhs -= rhs;
@ -131,7 +154,7 @@ struct ArrayPortalValueReference
}
VTKM_SUPPRESS_EXEC_WARNINGS
template <typename T>
VTKM_EXEC_CONT ValueType operator-=(const ArrayPortalValueReference<T>& rhs)
VTKM_EXEC_CONT ValueType operator-=(const ArrayPortalValueReference<T>& rhs) const
{
ValueType lhs = this->Get();
lhs -= rhs.Get();
@ -141,7 +164,7 @@ struct ArrayPortalValueReference
VTKM_SUPPRESS_EXEC_WARNINGS
template <typename T>
VTKM_EXEC_CONT ValueType operator*=(const T& rhs)
VTKM_EXEC_CONT ValueType operator*=(const T& rhs) const
{
ValueType lhs = this->Get();
lhs *= rhs;
@ -150,7 +173,7 @@ struct ArrayPortalValueReference
}
VTKM_SUPPRESS_EXEC_WARNINGS
template <typename T>
VTKM_EXEC_CONT ValueType operator*=(const ArrayPortalValueReference<T>& rhs)
VTKM_EXEC_CONT ValueType operator*=(const ArrayPortalValueReference<T>& rhs) const
{
ValueType lhs = this->Get();
lhs *= rhs.Get();
@ -160,7 +183,7 @@ struct ArrayPortalValueReference
VTKM_SUPPRESS_EXEC_WARNINGS
template <typename T>
VTKM_EXEC_CONT ValueType operator/=(const T& rhs)
VTKM_EXEC_CONT ValueType operator/=(const T& rhs) const
{
ValueType lhs = this->Get();
lhs /= rhs;
@ -169,7 +192,7 @@ struct ArrayPortalValueReference
}
VTKM_SUPPRESS_EXEC_WARNINGS
template <typename T>
VTKM_EXEC_CONT ValueType operator/=(const ArrayPortalValueReference<T>& rhs)
VTKM_EXEC_CONT ValueType operator/=(const ArrayPortalValueReference<T>& rhs) const
{
ValueType lhs = this->Get();
lhs /= rhs.Get();
@ -179,7 +202,7 @@ struct ArrayPortalValueReference
VTKM_SUPPRESS_EXEC_WARNINGS
template <typename T>
VTKM_EXEC_CONT ValueType operator&=(const T& rhs)
VTKM_EXEC_CONT ValueType operator&=(const T& rhs) const
{
ValueType lhs = this->Get();
lhs &= rhs;
@ -188,7 +211,7 @@ struct ArrayPortalValueReference
}
VTKM_SUPPRESS_EXEC_WARNINGS
template <typename T>
VTKM_EXEC_CONT ValueType operator&=(const ArrayPortalValueReference<T>& rhs)
VTKM_EXEC_CONT ValueType operator&=(const ArrayPortalValueReference<T>& rhs) const
{
ValueType lhs = this->Get();
lhs &= rhs.Get();
@ -198,7 +221,7 @@ struct ArrayPortalValueReference
VTKM_SUPPRESS_EXEC_WARNINGS
template <typename T>
VTKM_EXEC_CONT ValueType operator|=(const T& rhs)
VTKM_EXEC_CONT ValueType operator|=(const T& rhs) const
{
ValueType lhs = this->Get();
lhs |= rhs;
@ -207,7 +230,7 @@ struct ArrayPortalValueReference
}
VTKM_SUPPRESS_EXEC_WARNINGS
template <typename T>
VTKM_EXEC_CONT ValueType operator|=(const ArrayPortalValueReference<T>& rhs)
VTKM_EXEC_CONT ValueType operator|=(const ArrayPortalValueReference<T>& rhs) const
{
ValueType lhs = this->Get();
lhs |= rhs.Get();
@ -217,7 +240,7 @@ struct ArrayPortalValueReference
VTKM_SUPPRESS_EXEC_WARNINGS
template <typename T>
VTKM_EXEC_CONT ValueType operator^=(const T& rhs)
VTKM_EXEC_CONT ValueType operator^=(const T& rhs) const
{
ValueType lhs = this->Get();
lhs ^= rhs;
@ -226,7 +249,7 @@ struct ArrayPortalValueReference
}
VTKM_SUPPRESS_EXEC_WARNINGS
template <typename T>
VTKM_EXEC_CONT ValueType operator^=(const ArrayPortalValueReference<T>& rhs)
VTKM_EXEC_CONT ValueType operator^=(const ArrayPortalValueReference<T>& rhs) const
{
ValueType lhs = this->Get();
lhs ^= rhs.Get();
@ -236,7 +259,7 @@ struct ArrayPortalValueReference
VTKM_SUPPRESS_EXEC_WARNINGS
template <typename T>
VTKM_EXEC_CONT ValueType operator>>=(const T& rhs)
VTKM_EXEC_CONT ValueType operator>>=(const T& rhs) const
{
ValueType lhs = this->Get();
lhs >>= rhs;
@ -245,7 +268,7 @@ struct ArrayPortalValueReference
}
VTKM_SUPPRESS_EXEC_WARNINGS
template <typename T>
VTKM_EXEC_CONT ValueType operator>>=(const ArrayPortalValueReference<T>& rhs)
VTKM_EXEC_CONT ValueType operator>>=(const ArrayPortalValueReference<T>& rhs) const
{
ValueType lhs = this->Get();
lhs >>= rhs.Get();
@ -255,7 +278,7 @@ struct ArrayPortalValueReference
VTKM_SUPPRESS_EXEC_WARNINGS
template <typename T>
VTKM_EXEC_CONT ValueType operator<<=(const T& rhs)
VTKM_EXEC_CONT ValueType operator<<=(const T& rhs) const
{
ValueType lhs = this->Get();
lhs <<= rhs;
@ -264,7 +287,7 @@ struct ArrayPortalValueReference
}
VTKM_SUPPRESS_EXEC_WARNINGS
template <typename T>
VTKM_EXEC_CONT ValueType operator<<=(const ArrayPortalValueReference<T>& rhs)
VTKM_EXEC_CONT ValueType operator<<=(const ArrayPortalValueReference<T>& rhs) const
{
ValueType lhs = this->Get();
lhs <<= rhs.Get();
@ -280,14 +303,14 @@ private:
//implement a custom swap function, since the std::swap won't work
//since we return RValues instead of Lvalues
template <typename T>
void swap(vtkm::internal::ArrayPortalValueReference<T> a,
vtkm::internal::ArrayPortalValueReference<T> b)
void swap(const vtkm::internal::ArrayPortalValueReference<T>& a,
const vtkm::internal::ArrayPortalValueReference<T>& b)
{
a.Swap(b);
}
template <typename T>
void swap(vtkm::internal::ArrayPortalValueReference<T> a,
void swap(const vtkm::internal::ArrayPortalValueReference<T>& a,
typename vtkm::internal::ArrayPortalValueReference<T>::ValueType& b)
{
using ValueType = typename vtkm::internal::ArrayPortalValueReference<T>::ValueType;
@ -298,7 +321,7 @@ void swap(vtkm::internal::ArrayPortalValueReference<T> a,
template <typename T>
void swap(typename vtkm::internal::ArrayPortalValueReference<T>::ValueType& a,
vtkm::internal::ArrayPortalValueReference<T> b)
const vtkm::internal::ArrayPortalValueReference<T>& b)
{
using ValueType = typename vtkm::internal::ArrayPortalValueReference<T>::ValueType;
const ValueType tmp = b;