Skip to content

Instantly share code, notes, and snippets.

@exjam
Created April 14, 2017 22:53
Show Gist options
  • Save exjam/301649036ab7900a03a707719188bccb to your computer and use it in GitHub Desktop.
Save exjam/301649036ab7900a03a707719188bccb to your computer and use it in GitHub Desktop.
template<typename Type>
class be_val
{
public:
be_val()
{
}
be_val(Type x)
{
mStorage = x;
}
Type value() const
{
return mStorage; // byte_swap(storage)
}
operator Type() const
{
return value();
}
template<typename OtherType, typename = decltype(std::declval<const Type>() == std::declval<const OtherType>())>
bool operator ==(const OtherType &other) const
{
return value() == other;
}
template<typename OtherType, typename = decltype(std::declval<const Type>() != std::declval<const OtherType>())>
bool operator !=(const OtherType &other) const
{
return value() != other;
}
template<typename OtherType, typename = decltype(std::declval<const Type>() >= std::declval<const OtherType>())>
bool operator >=(const OtherType &other) const
{
return value() >= other;
}
template<typename OtherType, typename = decltype(std::declval<const Type>() <= std::declval<const OtherType>())>
bool operator <=(const OtherType &other) const
{
return value() <= other;
}
template<typename OtherType, typename = decltype(std::declval<const Type>() > std::declval<const OtherType>())>
bool operator >(const OtherType &other) const
{
return value() > other;
}
template<typename OtherType, typename = decltype(std::declval<const Type>() < std::declval<const OtherType>())>
bool operator <(const OtherType &other) const
{
return value() < other;
}
template<typename OtherType, typename = decltype(std::declval<const Type>() + std::declval<const OtherType>())>
Type operator +(const OtherType &other) const
{
return value() + other;
}
template<typename OtherType, typename = decltype(std::declval<const Type>() - std::declval<const OtherType>())>
Type operator -(const OtherType &other) const
{
return value() - other;
}
template<typename OtherType, typename = decltype(std::declval<const Type>() * std::declval<const OtherType>())>
Type operator *(const OtherType &other) const
{
return value() * other;
}
template<typename OtherType, typename = decltype(std::declval<const Type>() / std::declval<const OtherType>())>
Type operator /(const OtherType &other) const
{
return value() / other;
}
template<typename OtherType, typename = decltype(std::declval<const Type>() % std::declval<const OtherType>())>
Type operator %(const OtherType &other) const
{
return value() % other;
}
template<typename OtherType, typename = decltype(std::declval<const Type>() | std::declval<const OtherType>())>
Type operator |(const OtherType &other) const
{
return value() | other;
}
template<typename OtherType, typename = decltype(std::declval<const Type>() & std::declval<const OtherType>())>
Type operator &(const OtherType &other) const
{
return value() & other;
}
template<typename OtherType, typename = decltype(std::declval<const Type>() ^ std::declval<const OtherType>())>
Type operator ^(const OtherType &other) const
{
return value() ^ other;
}
template<typename OtherType, typename = decltype(std::declval<const Type>() + std::declval<const OtherType>())>
be_val &operator +=(const OtherType &other)
{
*this = value() + other;
return *this;
}
template<typename OtherType, typename = decltype(std::declval<const Type>() - std::declval<const OtherType>())>
be_val &operator -=(const OtherType &other)
{
*this = value() - other;
return *this;
}
template<typename OtherType, typename = decltype(std::declval<const Type>() * std::declval<const OtherType>())>
be_val &operator *=(const OtherType &other)
{
*this = value() * other;
return *this;
}
template<typename OtherType, typename = decltype(std::declval<const Type>() / std::declval<const OtherType>())>
be_val &operator /=(const OtherType &other)
{
*this = value() / other;
return *this;
}
template<typename OtherType, typename = decltype(std::declval<const Type>() % std::declval<const OtherType>())>
be_val &operator %=(const OtherType &other)
{
*this = value() % other;
return *this;
}
template<typename OtherType, typename = decltype(std::declval<const Type>() | std::declval<const OtherType>())>
be_val &operator |=(const OtherType &other)
{
*this = value() | other;
return *this;
}
template<typename OtherType, typename = decltype(std::declval<const Type>() & std::declval<const OtherType>())>
be_val &operator &=(const OtherType &other)
{
*this = value() & other;
return *this;
}
template<typename OtherType, typename = decltype(std::declval<const Type>() ^ std::declval<const OtherType>())>
be_val &operator ^=(const OtherType &other)
{
*this = value() ^ other;
return *this;
}
template<typename = decltype(std::declval<const Type>() + 1)>
be_val &operator ++()
{
*this = value() + 1;
return *this;
}
template<typename = decltype(std::declval<const Type>() + 1)>
be_val operator ++(int)
{
auto before = *this;
*this = value() + 1;
return before;
}
template<typename = decltype(std::declval<const Type>() - 1)>
be_val &operator --()
{
*this = value() - 1;
return *this;
}
template<typename = decltype(std::declval<const Type>() - 1)>
be_val operator --(int)
{
auto before = *this;
*this = value() - 1;
return before;
}
template<typename = decltype(std::declval<Type>().operator ->())>
auto operator ->() -> decltype(std::declval<Type>().operator ->())
{
return value().operator ->();
}
template<typename = decltype(std::declval<const Type>().operator ->())>
auto operator ->() const -> decltype(std::declval<const Type>().operator ->())
{
return value().operator ->();
}
template<typename = decltype(std::declval<Type>().operator *())>
auto operator *() -> decltype(std::declval<Type>().operator *())
{
return value().operator *();
}
template<typename = decltype(std::declval<const Type>().operator *())>
auto operator *() const -> decltype(std::declval<const Type>().operator *())
{
return value().operator *();
}
private:
Type mStorage;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment