Add ox::LittleEndian template

This commit is contained in:
Gary Talent 2018-02-17 00:47:28 -06:00
parent b8a6b47b29
commit 193492c518

View File

@ -60,36 +60,8 @@ inline uint64_t byteSwap(uint64_t i) {
/**
* Takes an int and byte swaps if the platform is big endian.
*/
inline int8_t bigEndianAdapt(int8_t i) {
return i;
}
/**
* Takes an int and byte swaps if the platform is big endian.
*/
inline int16_t bigEndianAdapt(int16_t i) {
#ifdef __BIG_ENDIAN__
return byteSwap(i);
#else
return i;
#endif
}
/**
* Takes an int and byte swaps if the platform is big endian.
*/
inline int32_t bigEndianAdapt(int32_t i) {
#ifdef __BIG_ENDIAN__
return byteSwap(i);
#else
return i;
#endif
}
/**
* Takes an int and byte swaps if the platform is big endian.
*/
inline int64_t bigEndianAdapt(int64_t i) {
template<typename T>
inline T bigEndianAdapt(T i) {
#ifdef __BIG_ENDIAN__
return byteSwap(i);
#else
@ -98,44 +70,35 @@ inline int64_t bigEndianAdapt(int64_t i) {
}
/**
* Takes an int and byte swaps if the platform is big endian.
*/
inline uint8_t bigEndianAdapt(uint8_t i) {
return i;
}
template<typename T>
class LittleEndian {
private:
T m_value;
/**
* Takes an int and byte swaps if the platform is big endian.
*/
inline uint16_t bigEndianAdapt(uint16_t i) {
#ifdef __BIG_ENDIAN__
return byteSwap(i);
#else
return i;
#endif
}
public:
inline LittleEndian() = default;
/**
* Takes an int and byte swaps if the platform is big endian.
*/
inline uint32_t bigEndianAdapt(uint32_t i) {
#ifdef __BIG_ENDIAN__
return byteSwap(i);
#else
return i;
#endif
}
inline LittleEndian(T value) {
m_value = ox::bigEndianAdapt(value);
}
/**
* Takes an int and byte swaps if the platform is big endian.
*/
inline uint64_t bigEndianAdapt(uint64_t i) {
#ifdef __BIG_ENDIAN__
return byteSwap(i);
#else
return i;
#endif
}
inline T operator=(T value) {
m_value = ox::bigEndianAdapt(value);
return value;
}
inline operator T() {
return ox::bigEndianAdapt(m_value);
}
inline bool operator==(T value) {
return value == ox::bigEndianAdapt(m_value);
}
inline bool operator!=(T value) {
return value != ox::bigEndianAdapt(m_value);
}
};
}