From 8eca42cd4f6b07b072dd3df7a5e4ebcf1ddd8869 Mon Sep 17 00:00:00 2001 From: Ludovic 'Archivist' Lagouardette Date: Sun, 10 May 2020 12:32:47 +0200 Subject: [PATCH] moved bitops --- include/bitops.hpp | 2 -- include/gp/bitops.hpp | 74 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 2 deletions(-) delete mode 100644 include/bitops.hpp create mode 100644 include/gp/bitops.hpp diff --git a/include/bitops.hpp b/include/bitops.hpp deleted file mode 100644 index 3f59c93..0000000 --- a/include/bitops.hpp +++ /dev/null @@ -1,2 +0,0 @@ -#pragma once - diff --git a/include/gp/bitops.hpp b/include/gp/bitops.hpp new file mode 100644 index 0000000..b6a62ec --- /dev/null +++ b/include/gp/bitops.hpp @@ -0,0 +1,74 @@ +#pragma once +#include +#include + +namespace gp{ + namespace _impl{ + constexpr uint32_t uint32_ = 0x01020304; + constexpr uint8_t magic_ = (const uint8_t&)uint32_; + } + + enum class endian : uint16_t + { +#ifdef _WIN32 + little = 0, + big = 1, + native = little +#elseif defined(__BYTE_ORDER__) + little = __ORDER_LITTLE_ENDIAN__, + big = __ORDER_BIG_ENDIAN__, + native = __BYTE_ORDER__ +#else + little = true, + big = false, +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wfour-char-constants" +#pragma gcc diagnostic push +#pragma gcc diagnostic ignored "-Wfour-char-constants" + native = ('ABCD'==0x41424344UL) +#pragma gcc diagnostic pop +#pragma clang diagnostic pop +#endif + }; + + + template + T swap_endian(T value) + { + union + { + T v; + uint8_t u8[sizeof(T)]; + } src, dest; + + src.v = value; + + for (size_t idx = 0;idx + struct endian_wrapper { + T value; + endian_wrapper(){}; + endian_wrapper(T v) : value{ + mode == endian::native ? v : swap_endian(v) + }{} + endian_wrapper(endian_wrapper& v) : value{ + v.value + }{} + + endian_wrapper& operator=(T value) { + mode == endian::native ? value : swap_endian(value); + return *this; + } + + operator T(){ + return mode == endian::native ? value : swap_endian(value); + } + }; +} \ No newline at end of file