#pragma once #include template struct LEInteger { public: LEInteger(); LEInteger(const LEInteger &other); explicit LEInteger(T i); operator T() const; LEInteger &operator=(T value); template LEInteger &operator+=(TOther value); template LEInteger &operator-=(TOther value); template LEInteger &operator*=(TOther value); template LEInteger &operator/=(TOther value); template LEInteger &operator%=(TOther value); LEInteger& operator--(); LEInteger operator--(int); LEInteger& operator++(); LEInteger operator++(int); private: uint8_t m_leValueBytes[sizeof(T)]; }; template struct LEInteger_SwapHelper { }; #include "ByteSwap.h" template<> struct LEInteger_SwapHelper { inline static void Swap(int16_t &v) { PortabilityLayer::ByteSwap::LittleInt16(v); } }; template<> struct LEInteger_SwapHelper { inline static void Swap(int32_t &v) { PortabilityLayer::ByteSwap::LittleInt32(v); } }; template<> struct LEInteger_SwapHelper { inline static void Swap(uint16_t &v) { PortabilityLayer::ByteSwap::LittleUInt16(v); } }; template<> struct LEInteger_SwapHelper { inline static void Swap(uint32_t &v) { PortabilityLayer::ByteSwap::LittleUInt32(v); } }; #include template inline LEInteger::LEInteger() { memset(m_leValueBytes, 0, sizeof(T)); } template inline LEInteger::LEInteger(const LEInteger &other) { memcpy(m_leValueBytes, other.m_leValueBytes, sizeof(T)); } template inline LEInteger::LEInteger(T i) { LEInteger_SwapHelper::Swap(i); memcpy(m_leValueBytes, &i, sizeof(T)); } template inline LEInteger::operator T() const { T result; memcpy(&result, m_leValueBytes, sizeof(T)); LEInteger_SwapHelper::Swap(result); return result; } template inline LEInteger &LEInteger::operator=(T value) { LEInteger_SwapHelper::Swap(value); memcpy(m_leValueBytes, &value, sizeof(T)); return *this; } template template LEInteger &LEInteger::operator+=(TOther value) { T storedValue; memcpy(&storedValue, m_leValueBytes, sizeof(T)); LEInteger_SwapHelper::Swap(storedValue); storedValue += value; LEInteger_SwapHelper::Swap(storedValue); memcpy(m_leValueBytes, &storedValue, sizeof(T)); return *this; } template template LEInteger &LEInteger::operator-=(TOther value) { T storedValue; memcpy(&storedValue, m_leValueBytes, sizeof(T)); LEInteger_SwapHelper::Swap(storedValue); storedValue -= value; LEInteger_SwapHelper::Swap(storedValue); memcpy(m_leValueBytes, &storedValue, sizeof(T)); return *this; } template template LEInteger &LEInteger::operator*=(TOther value) { T storedValue; memcpy(&storedValue, m_leValueBytes, sizeof(T)); LEInteger_SwapHelper::Swap(storedValue); storedValue *= value; LEInteger_SwapHelper::Swap(storedValue); memcpy(m_leValueBytes, &storedValue, sizeof(T)); return *this; } template template LEInteger &LEInteger::operator/=(TOther value) { T storedValue; memcpy(&storedValue, m_leValueBytes, sizeof(T)); LEInteger_SwapHelper::Swap(storedValue); storedValue /= value; LEInteger_SwapHelper::Swap(storedValue); memcpy(m_leValueBytes, &storedValue, sizeof(T)); return *this; } template template LEInteger &LEInteger::operator%=(TOther value) { T storedValue; memcpy(&storedValue, m_leValueBytes, sizeof(T)); LEInteger_SwapHelper::Swap(storedValue); storedValue %= value; LEInteger_SwapHelper::Swap(storedValue); memcpy(m_leValueBytes, &storedValue, sizeof(T)); return *this; } template LEInteger& LEInteger::operator--() { T storedValue; memcpy(&storedValue, m_leValueBytes, sizeof(T)); LEInteger_SwapHelper::Swap(storedValue); storedValue--; LEInteger_SwapHelper::Swap(storedValue); memcpy(m_leValueBytes, &storedValue, sizeof(T)); return *this; } template LEInteger LEInteger::operator--(int) { LEInteger orig(*this); --(*this); return orig; } template LEInteger& LEInteger::operator++() { T storedValue; memcpy(&storedValue, m_leValueBytes, sizeof(T)); LEInteger_SwapHelper::Swap(storedValue); storedValue++; LEInteger_SwapHelper::Swap(storedValue); memcpy(m_leValueBytes, &storedValue, sizeof(T)); return *this; } template LEInteger LEInteger::operator++(int) { LEInteger orig(*this); ++(*this); return orig; } typedef LEInteger LEInt16_t; typedef LEInteger LEInt32_t; typedef LEInteger LEUInt16_t; typedef LEInteger LEUInt32_t;