#pragma once #ifndef __PL_BIG_ENDIAN_H__ #define __PL_BIG_ENDIAN_H__ #include template struct BEInteger { public: BEInteger(); BEInteger(const BEInteger &other); explicit BEInteger(T i); operator T() const; BEInteger &operator=(T value); template BEInteger &operator+=(TOther value); template BEInteger &operator-=(TOther value); template BEInteger &operator*=(TOther value); template BEInteger &operator/=(TOther value); template BEInteger &operator%=(TOther value); BEInteger& operator--(); BEInteger operator--(int); BEInteger& operator++(); BEInteger operator++(int); private: uint8_t m_beValueBytes[sizeof(T)]; }; template struct BEInteger_SwapHelper { }; #include "ByteSwap.h" template<> struct BEInteger_SwapHelper { inline static void Swap(int16_t &v) { PortabilityLayer::ByteSwap::BigInt16(v); } }; template<> struct BEInteger_SwapHelper { inline static void Swap(int32_t &v) { PortabilityLayer::ByteSwap::BigInt32(v); } }; template<> struct BEInteger_SwapHelper { inline static void Swap(uint16_t &v) { PortabilityLayer::ByteSwap::BigUInt16(v); } }; template<> struct BEInteger_SwapHelper { inline static void Swap(uint32_t &v) { PortabilityLayer::ByteSwap::BigUInt32(v); } }; #include template inline BEInteger::BEInteger() { memset(m_beValueBytes, 0, sizeof(T)); } template inline BEInteger::BEInteger(const BEInteger &other) { memcpy(m_beValueBytes, other.m_beValueBytes, sizeof(T)); } template inline BEInteger::BEInteger(T i) { BEInteger_SwapHelper::Swap(i); memcpy(m_beValueBytes, &i, sizeof(T)); } template inline BEInteger::operator T() const { T result; memcpy(&result, m_beValueBytes, sizeof(T)); BEInteger_SwapHelper::Swap(result); return result; } template inline BEInteger &BEInteger::operator=(T value) { BEInteger_SwapHelper::Swap(value); memcpy(m_beValueBytes, &value, sizeof(T)); return *this; } template template BEInteger &BEInteger::operator+=(TOther value) { T storedValue; memcpy(&storedValue, m_beValueBytes, sizeof(T)); BEInteger_SwapHelper::Swap(storedValue); storedValue += value; BEInteger_SwapHelper::Swap(storedValue); memcpy(m_beValueBytes, &storedValue, sizeof(T)); return *this; } template template BEInteger &BEInteger::operator-=(TOther value) { T storedValue; memcpy(&storedValue, m_beValueBytes, sizeof(T)); BEInteger_SwapHelper::Swap(storedValue); storedValue -= value; BEInteger_SwapHelper::Swap(storedValue); memcpy(m_beValueBytes, &storedValue, sizeof(T)); return *this; } template template BEInteger &BEInteger::operator*=(TOther value) { T storedValue; memcpy(&storedValue, m_beValueBytes, sizeof(T)); BEInteger_SwapHelper::Swap(storedValue); storedValue *= value; BEInteger_SwapHelper::Swap(storedValue); memcpy(m_beValueBytes, &storedValue, sizeof(T)); return *this; } template template BEInteger &BEInteger::operator/=(TOther value) { T storedValue; memcpy(&storedValue, m_beValueBytes, sizeof(T)); BEInteger_SwapHelper::Swap(storedValue); storedValue /= value; BEInteger_SwapHelper::Swap(storedValue); memcpy(m_beValueBytes, &storedValue, sizeof(T)); return *this; } template template BEInteger &BEInteger::operator%=(TOther value) { T storedValue; memcpy(&storedValue, m_beValueBytes, sizeof(T)); BEInteger_SwapHelper::Swap(storedValue); storedValue %= value; BEInteger_SwapHelper::Swap(storedValue); memcpy(m_beValueBytes, &storedValue, sizeof(T)); return *this; } template BEInteger& BEInteger::operator--() { T storedValue; memcpy(&storedValue, m_beValueBytes, sizeof(T)); BEInteger_SwapHelper::Swap(storedValue); storedValue--; BEInteger_SwapHelper::Swap(storedValue); memcpy(m_beValueBytes, &storedValue, sizeof(T)); return *this; } template BEInteger BEInteger::operator--(int) { BEInteger orig(*this); --(*this); return orig; } template BEInteger& BEInteger::operator++() { T storedValue; memcpy(&storedValue, m_beValueBytes, sizeof(T)); BEInteger_SwapHelper::Swap(storedValue); storedValue++; BEInteger_SwapHelper::Swap(storedValue); memcpy(m_beValueBytes, &storedValue, sizeof(T)); return *this; } template BEInteger BEInteger::operator++(int) { BEInteger orig(*this); ++(*this); return orig; } typedef BEInteger BEInt16_t; typedef BEInteger BEInt32_t; typedef BEInteger BEUInt16_t; typedef BEInteger BEUInt32_t; struct BEFixed32_t { BEInt16_t m_intPart; BEUInt16_t m_fracPart; }; #endif