Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright (C) 2016 fastman92 <fastman92@gmail.com>, website: http://fastman92.ml
- * Licensed under the MIT License, see LICENSE at top level directory.
- *
- */
- #pragma once
- template <typename T>
- using NoWrapper = T;
- // Checks if currently running machine is little endian
- inline bool IsCurrentMachineLittleEndian()
- {
- unsigned __int16 value = 0x0201;
- return *(unsigned __int8*)&value == 0x01;
- }
- // Checks if currently running machine is little endian
- inline bool IsCurrentMachineBigEndian()
- {
- unsigned __int16 value = 0x0201;
- return *(unsigned __int8*)&value == 0x02;
- }
- // Endian base
- template<typename T, bool(*func)()> struct tEndianBase
- {
- private:
- T value;
- public:
- // Default constructor
- tEndianBase() {}
- // Copy constructor
- tEndianBase(const tEndianBase& other)
- {
- *this = other;
- }
- // Copy constructor
- tEndianBase(const T& other)
- {
- *this = other;
- }
- // Assignment
- tEndianBase operator=(const tEndianBase other)
- {
- this->value = other.value;
- return *this;
- }
- // Assignment
- tEndianBase operator=(const T other)
- {
- if (func())
- this->value = other;
- else
- for (int i = 0; i < sizeof(T); i++)
- *((char*)&this->value + i) = *((char*)&other + sizeof(T) - i - 1);
- return *this;
- }
- // Convert to base type
- operator T()
- {
- T result;
- for (int i = 0; i < sizeof(T); i++)
- *((char*)&result + i) = *((char*)&this->value + sizeof(T) - i - 1);
- return result;
- }
- };
- // Little endian
- template <class T>
- using tLittleEndian = tEndianBase<T, IsCurrentMachineBigEndian>;
- // Big endian
- template <typename T>
- using tBigEndian = tEndianBase<T, IsCurrentMachineBigEndian>;
- // Endian
- template <typename T> struct tVariedEndian
- {
- T value;
- public:
- // Sets value
- void SetValue(const T value, bool bIsBigEndian)
- {
- if (bIsBigEndian != IsCurrentMachineBigEndianEndian())
- {
- for (int i = 0; i < sizeof(T); i++)
- *((char*)&this->value + i) = *((char*)&value + sizeof(T) - i - 1);
- }
- else
- this->value = value;
- }
- // Returns a value
- const T GetValue(bool bIsBigEndian) const
- {
- if (bIsBigEndian != IsCurrentMachineBigEndianEndian())
- {
- T result;
- for (int i = 0; i < sizeof(T); i++)
- *((char*)&result + i) = *((char*)&this->value + sizeof(T) - i - 1);
- return result;
- }
- else
- return this->value;
- }
- // Returns value as little endian
- const T GetValueAsLittleEndian() const
- {
- return GetValue(false);
- }
- // Returns value as big endian
- const T GetValueAsBigEndian() const
- {
- return GetValue(true);
- }
- };
- #define DEFINE_ENDIAN_STRUCTURE(normal_name, fields_name) \
- typedef fields_name<NoWrapper> CVector; \
- template<> class tVariedEndian<normal_name> : public fields_name<tVariedEndian>{}; \
- template<> class tEndianBase<normal_name, IsCurrentMachineLittleEndian> : public fields_name<tLittleEndian> {}; \
- template<> class tEndianBase<normal_name, IsCurrentMachineBigEndian> : public fields_name<tLittleEndian> {};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement