[ox/std] Improve ability to create unique int types, regardless of underlying type

This commit is contained in:
Gary Talent 2019-07-28 00:57:19 -05:00
parent b107dc756b
commit 142387aa04
3 changed files with 142 additions and 138 deletions

View File

@ -22,7 +22,9 @@
namespace ox { namespace ox {
using Error = Uint64; class BaseError {};
using Error = Integer<uint64_t, BaseError>;
constexpr Error errCode(Error err) { constexpr Error errCode(Error err) {
return (err >> 59) & onMask<Error>(5); return (err >> 59) & onMask<Error>(5);

View File

@ -12,12 +12,14 @@
namespace ox { namespace ox {
class BaseInteger {};
/** /**
* Integer is a strongly typed integer wrapper used to create strongly typed * Integer is a strongly typed integer wrapper used to create strongly typed
* integers. * integers.
*/ */
template<typename T> template<typename T, class Base = BaseInteger>
class Integer { class Integer: Base {
private: private:
T m_i; T m_i;
@ -26,71 +28,71 @@ class Integer {
constexpr explicit Integer(T i) noexcept; constexpr explicit Integer(T i) noexcept;
constexpr Integer<T> operator=(Integer<T> i) noexcept; constexpr Integer<T, Base> operator=(Integer<T, Base> i) noexcept;
constexpr Integer<T> operator==(Integer<T> i) const noexcept; constexpr Integer<T, Base> operator==(Integer<T, Base> i) const noexcept;
constexpr Integer<T> operator!=(Integer<T> i) const noexcept; constexpr Integer<T, Base> operator!=(Integer<T, Base> i) const noexcept;
constexpr Integer<T> operator<(Integer<T> i) const noexcept; constexpr Integer<T, Base> operator<(Integer<T, Base> i) const noexcept;
constexpr Integer<T> operator>(Integer<T> i) const noexcept; constexpr Integer<T, Base> operator>(Integer<T, Base> i) const noexcept;
constexpr Integer<T> operator<=(Integer<T> i) const noexcept; constexpr Integer<T, Base> operator<=(Integer<T, Base> i) const noexcept;
constexpr Integer<T> operator>=(Integer<T> i) const noexcept; constexpr Integer<T, Base> operator>=(Integer<T, Base> i) const noexcept;
constexpr Integer<T> operator+(Integer<T> i) const noexcept; constexpr Integer<T, Base> operator+(Integer<T, Base> i) const noexcept;
constexpr Integer<T> operator+=(Integer<T> i) noexcept; constexpr Integer<T, Base> operator+=(Integer<T, Base> i) noexcept;
constexpr Integer<T> operator-(Integer<T> i) const noexcept; constexpr Integer<T, Base> operator-(Integer<T, Base> i) const noexcept;
constexpr Integer<T> operator-=(Integer<T> i) noexcept; constexpr Integer<T, Base> operator-=(Integer<T, Base> i) noexcept;
constexpr Integer<T> operator*(Integer<T> i) const noexcept; constexpr Integer<T, Base> operator*(Integer<T, Base> i) const noexcept;
constexpr Integer<T> operator*=(Integer<T> i) noexcept; constexpr Integer<T, Base> operator*=(Integer<T, Base> i) noexcept;
constexpr Integer<T> operator/(Integer<T> i) const noexcept; constexpr Integer<T, Base> operator/(Integer<T, Base> i) const noexcept;
constexpr Integer<T> operator/=(Integer<T> i) noexcept; constexpr Integer<T, Base> operator/=(Integer<T, Base> i) noexcept;
constexpr Integer<T> operator%(Integer<T> i) const noexcept; constexpr Integer<T, Base> operator%(Integer<T, Base> i) const noexcept;
constexpr Integer<T> operator%=(Integer<T> i) noexcept; constexpr Integer<T, Base> operator%=(Integer<T, Base> i) noexcept;
constexpr Integer<T> operator>>(int i) const noexcept; constexpr Integer<T, Base> operator>>(int i) const noexcept;
constexpr Integer<T> operator>>=(int i) noexcept; constexpr Integer<T, Base> operator>>=(int i) noexcept;
constexpr Integer<T> operator<<(int i) const noexcept; constexpr Integer<T, Base> operator<<(int i) const noexcept;
constexpr Integer<T> operator<<=(int i) noexcept; constexpr Integer<T, Base> operator<<=(int i) noexcept;
constexpr Integer<T> operator|(Integer<T> i) const noexcept; constexpr Integer<T, Base> operator|(Integer<T, Base> i) const noexcept;
constexpr Integer<T> operator|=(Integer<T> i) noexcept; constexpr Integer<T, Base> operator|=(Integer<T, Base> i) noexcept;
constexpr Integer<T> operator&(Integer<T> i) const noexcept; constexpr Integer<T, Base> operator&(Integer<T, Base> i) const noexcept;
constexpr Integer<T> operator&=(Integer<T> i) noexcept; constexpr Integer<T, Base> operator&=(Integer<T, Base> i) noexcept;
constexpr Integer<T> operator^(Integer<T> i) const noexcept; constexpr Integer<T, Base> operator^(Integer<T, Base> i) const noexcept;
constexpr Integer<T> operator^=(Integer<T> i) noexcept; constexpr Integer<T, Base> operator^=(Integer<T, Base> i) noexcept;
// Prefix increment // Prefix increment
constexpr Integer<T> operator++() noexcept; constexpr Integer<T, Base> operator++() noexcept;
// Postfix increment // Postfix increment
constexpr Integer<T> operator++(int) noexcept; constexpr Integer<T, Base> operator++(int) noexcept;
// Prefix decrement // Prefix decrement
constexpr Integer<T> operator--() noexcept; constexpr Integer<T, Base> operator--() noexcept;
// Postfix decrement // Postfix decrement
constexpr Integer<T> operator--(int) noexcept; constexpr Integer<T, Base> operator--(int) noexcept;
constexpr explicit operator T() const noexcept; constexpr explicit operator T() const noexcept;
@ -98,177 +100,177 @@ class Integer {
}; };
template<typename T> template<typename T, class Base>
constexpr Integer<T>::Integer(T i) noexcept { constexpr Integer<T, Base>::Integer(T i) noexcept {
m_i = i; m_i = i;
} }
template<typename T> template<typename T, class Base>
constexpr Integer<T> Integer<T>::operator=(Integer<T> i) noexcept { constexpr Integer<T, Base> Integer<T, Base>::operator=(Integer<T, Base> i) noexcept {
return Integer<T>(m_i = i.m_i); return Integer<T, Base>(m_i = i.m_i);
} }
template<typename T> template<typename T, class Base>
constexpr Integer<T> Integer<T>::operator==(Integer<T> i) const noexcept { constexpr Integer<T, Base> Integer<T, Base>::operator==(Integer<T, Base> i) const noexcept {
return Integer<T>(m_i == i.m_i); return Integer<T, Base>(m_i == i.m_i);
} }
template<typename T> template<typename T, class Base>
constexpr Integer<T> Integer<T>::operator!=(Integer<T> i) const noexcept { constexpr Integer<T, Base> Integer<T, Base>::operator!=(Integer<T, Base> i) const noexcept {
return Integer<T>(m_i != i.m_i); return Integer<T, Base>(m_i != i.m_i);
} }
template<typename T> template<typename T, class Base>
constexpr Integer<T> Integer<T>::operator<(Integer<T> i) const noexcept { constexpr Integer<T, Base> Integer<T, Base>::operator<(Integer<T, Base> i) const noexcept {
return Integer<T>(m_i < i.m_i); return Integer<T, Base>(m_i < i.m_i);
} }
template<typename T> template<typename T, class Base>
constexpr Integer<T> Integer<T>::operator>(Integer<T> i) const noexcept { constexpr Integer<T, Base> Integer<T, Base>::operator>(Integer<T, Base> i) const noexcept {
return Integer<T>(m_i > i.m_i); return Integer<T, Base>(m_i > i.m_i);
} }
template<typename T> template<typename T, class Base>
constexpr Integer<T> Integer<T>::operator<=(Integer<T> i) const noexcept { constexpr Integer<T, Base> Integer<T, Base>::operator<=(Integer<T, Base> i) const noexcept {
return Integer<T>(m_i <= i.m_i); return Integer<T, Base>(m_i <= i.m_i);
} }
template<typename T> template<typename T, class Base>
constexpr Integer<T> Integer<T>::operator>=(Integer<T> i) const noexcept { constexpr Integer<T, Base> Integer<T, Base>::operator>=(Integer<T, Base> i) const noexcept {
return Integer<T>(m_i >= i.m_i); return Integer<T, Base>(m_i >= i.m_i);
} }
template<typename T> template<typename T, class Base>
constexpr Integer<T> Integer<T>::operator+(Integer<T> i) const noexcept { constexpr Integer<T, Base> Integer<T, Base>::operator+(Integer<T, Base> i) const noexcept {
return Integer<T>(m_i + i.m_i); return Integer<T, Base>(m_i + i.m_i);
} }
template<typename T> template<typename T, class Base>
constexpr Integer<T> Integer<T>::operator+=(Integer<T> i) noexcept { constexpr Integer<T, Base> Integer<T, Base>::operator+=(Integer<T, Base> i) noexcept {
return Integer<T>(m_i += i.m_i); return Integer<T, Base>(m_i += i.m_i);
} }
template<typename T> template<typename T, class Base>
constexpr Integer<T> Integer<T>::operator-(Integer<T> i) const noexcept { constexpr Integer<T, Base> Integer<T, Base>::operator-(Integer<T, Base> i) const noexcept {
return Integer<T>(m_i - i.m_i); return Integer<T, Base>(m_i - i.m_i);
} }
template<typename T> template<typename T, class Base>
constexpr Integer<T> Integer<T>::operator-=(Integer<T> i) noexcept { constexpr Integer<T, Base> Integer<T, Base>::operator-=(Integer<T, Base> i) noexcept {
return Integer<T>(m_i -= i.m_i); return Integer<T, Base>(m_i -= i.m_i);
} }
template<typename T> template<typename T, class Base>
constexpr Integer<T> Integer<T>::operator*(Integer<T> i) const noexcept { constexpr Integer<T, Base> Integer<T, Base>::operator*(Integer<T, Base> i) const noexcept {
return Integer<T>(m_i * i.m_i); return Integer<T, Base>(m_i * i.m_i);
} }
template<typename T> template<typename T, class Base>
constexpr Integer<T> Integer<T>::operator*=(Integer<T> i) noexcept { constexpr Integer<T, Base> Integer<T, Base>::operator*=(Integer<T, Base> i) noexcept {
return Integer<T>(m_i *= i.m_i); return Integer<T, Base>(m_i *= i.m_i);
} }
template<typename T> template<typename T, class Base>
constexpr Integer<T> Integer<T>::operator/(Integer<T> i) const noexcept { constexpr Integer<T, Base> Integer<T, Base>::operator/(Integer<T, Base> i) const noexcept {
return Integer<T>(m_i / i.m_i); return Integer<T, Base>(m_i / i.m_i);
} }
template<typename T> template<typename T, class Base>
constexpr Integer<T> Integer<T>::operator/=(Integer<T> i) noexcept { constexpr Integer<T, Base> Integer<T, Base>::operator/=(Integer<T, Base> i) noexcept {
return Integer<T>(m_i /= i.m_i); return Integer<T, Base>(m_i /= i.m_i);
} }
template<typename T> template<typename T, class Base>
constexpr Integer<T> Integer<T>::operator%(Integer<T> i) const noexcept { constexpr Integer<T, Base> Integer<T, Base>::operator%(Integer<T, Base> i) const noexcept {
return Integer<T>(m_i % i.m_i); return Integer<T, Base>(m_i % i.m_i);
} }
template<typename T> template<typename T, class Base>
constexpr Integer<T> Integer<T>::operator%=(Integer<T> i) noexcept { constexpr Integer<T, Base> Integer<T, Base>::operator%=(Integer<T, Base> i) noexcept {
return Integer<T>(m_i %= i.m_i); return Integer<T, Base>(m_i %= i.m_i);
} }
template<typename T> template<typename T, class Base>
constexpr Integer<T> Integer<T>::operator>>(int i) const noexcept { constexpr Integer<T, Base> Integer<T, Base>::operator>>(int i) const noexcept {
return Integer<T>(m_i >> i); return Integer<T, Base>(m_i >> i);
} }
template<typename T> template<typename T, class Base>
constexpr Integer<T> Integer<T>::operator>>=(int i) noexcept { constexpr Integer<T, Base> Integer<T, Base>::operator>>=(int i) noexcept {
return Integer<T>(m_i >>= i); return Integer<T, Base>(m_i >>= i);
} }
template<typename T> template<typename T, class Base>
constexpr Integer<T> Integer<T>::operator<<(int i) const noexcept { constexpr Integer<T, Base> Integer<T, Base>::operator<<(int i) const noexcept {
return Integer<T>(m_i << i); return Integer<T, Base>(m_i << i);
} }
template<typename T> template<typename T, class Base>
constexpr Integer<T> Integer<T>::operator<<=(int i) noexcept { constexpr Integer<T, Base> Integer<T, Base>::operator<<=(int i) noexcept {
return Integer<T>(m_i <<= i); return Integer<T, Base>(m_i <<= i);
} }
template<typename T> template<typename T, class Base>
constexpr Integer<T> Integer<T>::operator|(Integer<T> i) const noexcept { constexpr Integer<T, Base> Integer<T, Base>::operator|(Integer<T, Base> i) const noexcept {
return Integer<T>(m_i | i.m_i); return Integer<T, Base>(m_i | i.m_i);
} }
template<typename T> template<typename T, class Base>
constexpr Integer<T> Integer<T>::operator|=(Integer<T> i) noexcept { constexpr Integer<T, Base> Integer<T, Base>::operator|=(Integer<T, Base> i) noexcept {
return Integer<T>(m_i |= i.m_i); return Integer<T, Base>(m_i |= i.m_i);
} }
template<typename T> template<typename T, class Base>
constexpr Integer<T> Integer<T>::operator&(Integer<T> i) const noexcept { constexpr Integer<T, Base> Integer<T, Base>::operator&(Integer<T, Base> i) const noexcept {
return Integer<T>(m_i & i.m_i); return Integer<T, Base>(m_i & i.m_i);
} }
template<typename T> template<typename T, class Base>
constexpr Integer<T> Integer<T>::operator&=(Integer<T> i) noexcept { constexpr Integer<T, Base> Integer<T, Base>::operator&=(Integer<T, Base> i) noexcept {
return Integer<T>(m_i &= i.m_i); return Integer<T, Base>(m_i &= i.m_i);
} }
template<typename T> template<typename T, class Base>
constexpr Integer<T> Integer<T>::operator^(Integer<T> i) const noexcept { constexpr Integer<T, Base> Integer<T, Base>::operator^(Integer<T, Base> i) const noexcept {
return Integer<T>(m_i ^ i.m_i); return Integer<T, Base>(m_i ^ i.m_i);
} }
template<typename T> template<typename T, class Base>
constexpr Integer<T> Integer<T>::operator^=(Integer<T> i) noexcept { constexpr Integer<T, Base> Integer<T, Base>::operator^=(Integer<T, Base> i) noexcept {
return Integer<T>(m_i ^= i.m_i); return Integer<T, Base>(m_i ^= i.m_i);
} }
// Prefix increment // Prefix increment
template<typename T> template<typename T, class Base>
constexpr inline Integer<T> Integer<T>::operator++() noexcept { constexpr inline Integer<T, Base> Integer<T, Base>::operator++() noexcept {
return Integer<T>(++m_i); return Integer<T, Base>(++m_i);
} }
// Postfix increment // Postfix increment
template<typename T> template<typename T, class Base>
constexpr inline Integer<T> Integer<T>::operator++(int) noexcept { constexpr inline Integer<T, Base> Integer<T, Base>::operator++(int) noexcept {
return Integer<T>(m_i++); return Integer<T, Base>(m_i++);
} }
// Prefix decrement // Prefix decrement
template<typename T> template<typename T, class Base>
constexpr inline Integer<T> Integer<T>::operator--() noexcept { constexpr inline Integer<T, Base> Integer<T, Base>::operator--() noexcept {
return Integer<T>(--m_i); return Integer<T, Base>(--m_i);
} }
// Postfix decrement // Postfix decrement
template<typename T> template<typename T, class Base>
constexpr inline Integer<T> Integer<T>::operator--(int) noexcept { constexpr inline Integer<T, Base> Integer<T, Base>::operator--(int) noexcept {
return Integer<T>(m_i--); return Integer<T, Base>(m_i--);
} }
template<typename T> template<typename T, class Base>
constexpr Integer<T>::operator T() const noexcept { constexpr Integer<T, Base>::operator T() const noexcept {
return m_i; return m_i;
} }
template<typename T> template<typename T, class Base>
constexpr Integer<T>::operator bool() const noexcept { constexpr Integer<T, Base>::operator bool() const noexcept {
return m_i; return m_i;
} }

View File

@ -22,12 +22,12 @@ struct TraceMsg {
template<typename T> template<typename T>
Error model(T *io, ox::trace::TraceMsg *obj) { Error model(T *io, ox::trace::TraceMsg *obj) {
Error err = 0; auto err = OxError(0);
io->setTypeInfo("ox::trace::TraceMsg", 5); io->setTypeInfo("ox::trace::TraceMsg", 5);
err |= io->field("file", &obj->file); oxReturnError(io->field("file", &obj->file));
err |= io->field("line", &obj->line); oxReturnError(io->field("line", &obj->line));
err |= io->field("time", &obj->time); oxReturnError(io->field("time", &obj->time));
err |= io->field("msg", &obj->msg); oxReturnError(io->field("msg", &obj->msg));
return err; return err;
} }