#pragma once #include "utils.h" #include "type_traits.h" #include template class be_val { public: static_assert(!std::is_array::value, "be_val invalid type: array"); static_assert(!std::is_pointer::value, "be_val invalid type: pointer"); static_assert(sizeof(Type) == 1 || sizeof(Type) == 2 || sizeof(Type) == 4 || sizeof(Type) == 8, "be_val invalid type size"); using value_type = Type; be_val() = default; be_val(const value_type &value) : mStorage(byte_swap(value)) { } value_type value() const { return byte_swap(mStorage); } void setValue(value_type value) { mStorage = byte_swap(value); } operator value_type() const { return value(); } template::value || std::is_constructible::value >::type> explicit operator bool() const { return static_cast(value()); } template::value || std::is_constructible::value || std::is_convertible::type>::value >::type> explicit operator OtherType() const { return static_cast(value()); } template::value || std::is_convertible::value>::type> be_val & operator =(const OtherType &other) { if constexpr (std::is_constructible::value) { setValue(value_type { other }); } else { setValue(static_cast(other)); } return *this; } template::value || std::is_convertible::value>::type> be_val & operator =(OtherType &&other) { if constexpr (std::is_constructible::value) { setValue(value_type { std::forward(other) }); } else { setValue(static_cast(std::forward(other))); } return *this; } template::value || std::is_constructible::value>::type> be_val & operator =(const be_val &other) { if constexpr (std::is_constructible::value) { setValue(value_type { other.value() }); } else { setValue(static_cast(other.value())); } return *this; } template::value || std::is_constructible::value>::type> be_val & operator =(be_val &&other) { if constexpr (std::is_constructible::value) { setValue(value_type { other.value() }); } else { setValue(static_cast(other.value())); } return *this; } template auto operator ==(const OtherType &other) -> decltype(std::declval().operator ==(std::declval())) const { return value() == other; } template auto operator !=(const OtherType &other) -> decltype(std::declval().operator !=(std::declval())) const { return value() != other; } template auto operator >=(const OtherType &other) -> decltype(std::declval().operator >=(std::declval())) const { return value() >= other; } template auto operator <=(const OtherType &other) -> decltype(std::declval().operator <=(std::declval())) const { return value() <= other; } template auto operator >(const OtherType &other) -> decltype(std::declval().operator >(std::declval())) const { return value() > other; } template auto operator <(const OtherType &other) -> decltype(std::declval().operator <(std::declval())) const { return value() < other; } template auto operator +() -> decltype(std::declval(). operator+()) const { return +value(); } template auto operator -() -> decltype(std::declval(). operator-()) const { return -value(); } template auto operator +(const OtherType &other) -> decltype(std::declval().operator +(std::declval())) const { return value() + other; } template auto operator -(const OtherType &other) -> decltype(std::declval().operator -(std::declval())) const { return value() - other; } template auto operator *(const OtherType &other) -> decltype(std::declval().operator *(std::declval())) const { return value() * other; } template auto operator /(const OtherType &other) -> decltype(std::declval().operator /(std::declval())) const { return value() / other; } template auto operator %(const OtherType &other) -> decltype(std::declval().operator %(std::declval())) const { return value() % other; } template auto operator |(const OtherType &other) -> decltype(std::declval().operator |(std::declval())) const { return value() | other; } template auto operator &(const OtherType &other) -> decltype(std::declval().operator &(std::declval())) const { return value() & other; } template auto operator ^(const OtherType &other) -> decltype(std::declval().operator ^(std::declval())) const { return value() ^ other; } template auto operator <<(const OtherType &other) -> decltype(std::declval().operator <<(std::declval())) const { return value() << other; } template auto operator >>(const OtherType &other) -> decltype(std::declval().operator >>(std::declval())) const { return value() >> other; } template() + std::declval())> be_val &operator +=(const OtherType &other) { *this = value() + other; return *this; } template() - std::declval())> be_val &operator -=(const OtherType &other) { *this = value() - other; return *this; } template() * std::declval())> be_val &operator *=(const OtherType &other) { *this = value() * other; return *this; } template() / std::declval())> be_val &operator /=(const OtherType &other) { *this = value() / other; return *this; } template() % std::declval())> be_val &operator %=(const OtherType &other) { *this = value() % other; return *this; } template() | std::declval())> be_val &operator |=(const OtherType &other) { *this = static_cast(value() | other); return *this; } template() & std::declval())> be_val &operator &=(const OtherType &other) { *this = static_cast(value() & other); return *this; } template() ^ std::declval())> be_val &operator ^=(const OtherType &other) { *this = static_cast(value() ^ other); return *this; } template() << std::declval())> be_val &operator <<=(const OtherType &other) { *this = value() << other; return *this; } template() >> std::declval())> be_val &operator >>=(const OtherType &other) { *this = value() >> other; return *this; } template() + 1)> be_val &operator ++() { setValue(value() + 1); return *this; } template() + 1)> be_val operator ++(int) { auto before = *this; setValue(value() + 1); return before; } template() - 1)> be_val &operator --() { setValue(value() - 1); return *this; } template() - 1)> be_val operator --(int) { auto before = *this; setValue(value() - 1); return before; } template auto operator [](const IndexType &index) -> decltype(std::declval().operator [](std::declval())) { return value().operator [](index); } template auto operator ->() -> decltype(std::declval().operator ->()) { return value().operator ->(); } template auto operator ->() const -> decltype(std::declval().operator ->()) { return value().operator ->(); } template auto operator *() -> decltype(std::declval().operator *()) { return value().operator *(); } template auto operator *() const -> decltype(std::declval().operator *()) { return value().operator ->(); } private: value_type mStorage; };