This is an automated email from the ASF dual-hosted git repository. isapego pushed a commit to branch ignite-17424 in repository https://gitbox.apache.org/repos/asf/ignite-3.git
commit 13ec1bbe94f8ab6415b6cb749b703911da8019da Author: Igor Sapego <[email protected]> AuthorDate: Tue Aug 30 17:34:13 2022 +0400 IGNITE-17424 Re-factoring --- modules/platforms/cpp/common/Platform.h | 34 +++++++++++++--------- .../platforms/cpp/schema/BinaryTupleBuilder.cpp | 6 ++-- modules/platforms/cpp/schema/BinaryTupleBuilder.h | 2 +- modules/platforms/cpp/schema/BinaryTupleParser.cpp | 4 +-- 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/modules/platforms/cpp/common/Platform.h b/modules/platforms/cpp/common/Platform.h index 4955529660..7bfcebc447 100644 --- a/modules/platforms/cpp/common/Platform.h +++ b/modules/platforms/cpp/common/Platform.h @@ -26,17 +26,25 @@ # define SWITCH_WIN_OTHER(x, y) (y) #endif -#define LITTLE_ENDIAN 1 -#define BIG_ENDIAN 2 - -#ifdef __BYTE_ORDER__ -# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ -# define BYTE_ORDER LITTLE_ENDIAN -# else -# define BYTE_ORDER BIG_ENDIAN -# endif -#else -//TODO: Fix this -# define BYTE_ORDER LITTLE_ENDIAN -#endif +namespace ignite::platform +{ + +/** + * Byte order utility class. + */ +class ByteOrder +{ +private: + static constexpr uint32_t fourBytes = 0x01020304; + static constexpr uint8_t lesserByte = (const uint8_t&)fourBytes; + +public: + ByteOrder() = delete; + + static constexpr bool littleEndian = lesserByte == 0x04; + static constexpr bool bigEndian = lesserByte == 0x01; + + static_assert(littleEndian || bigEndian, "Unknown byte order"); +}; +} // ignite::platform diff --git a/modules/platforms/cpp/schema/BinaryTupleBuilder.cpp b/modules/platforms/cpp/schema/BinaryTupleBuilder.cpp index e93d6c2f53..37583e9c95 100644 --- a/modules/platforms/cpp/schema/BinaryTupleBuilder.cpp +++ b/modules/platforms/cpp/schema/BinaryTupleBuilder.cpp @@ -110,21 +110,21 @@ void BinaryTupleBuilder::putInt8(BytesView bytes) { void BinaryTupleBuilder::putInt16(BytesView bytes) { SizeT size = sizeOfInt16(BinaryTupleParser::getInt16(bytes)); assert(size <= bytes.size()); - static_assert(BYTE_ORDER == LITTLE_ENDIAN); + static_assert(platform::ByteOrder::littleEndian); putBytes(BytesView{bytes.data(), size}); } void BinaryTupleBuilder::putInt32(BytesView bytes) { SizeT size = sizeOfInt32(BinaryTupleParser::getInt32(bytes)); assert(size <= bytes.size()); - static_assert(BYTE_ORDER == LITTLE_ENDIAN); + static_assert(platform::ByteOrder::littleEndian); putBytes(BytesView{bytes.data(), size}); } void BinaryTupleBuilder::putInt64(BytesView bytes) { SizeT size = sizeOfInt64(BinaryTupleParser::getInt64(bytes)); assert(size <= bytes.size()); - static_assert(BYTE_ORDER == LITTLE_ENDIAN); + static_assert(platform::ByteOrder::littleEndian); putBytes(BytesView{bytes.data(), size}); } diff --git a/modules/platforms/cpp/schema/BinaryTupleBuilder.h b/modules/platforms/cpp/schema/BinaryTupleBuilder.h index 812fc4f954..e55f35499f 100644 --- a/modules/platforms/cpp/schema/BinaryTupleBuilder.h +++ b/modules/platforms/cpp/schema/BinaryTupleBuilder.h @@ -387,7 +387,7 @@ private: */ void appendEntry() { uint64_t offset = nextValue - valueBase; - static_assert(BYTE_ORDER == LITTLE_ENDIAN); + static_assert(platform::ByteOrder::littleEndian); assert(nextEntry + entrySize <= valueBase); std::memcpy(nextEntry, &offset, entrySize); nextEntry += entrySize; diff --git a/modules/platforms/cpp/schema/BinaryTupleParser.cpp b/modules/platforms/cpp/schema/BinaryTupleParser.cpp index 4ebb75c5d4..64d3ea19c8 100644 --- a/modules/platforms/cpp/schema/BinaryTupleParser.cpp +++ b/modules/platforms/cpp/schema/BinaryTupleParser.cpp @@ -60,7 +60,7 @@ BinaryTupleParser::BinaryTupleParser(IntT numElements, BytesView data) // Fix tuple size if needed. uint64_t offset = 0; - static_assert(BYTE_ORDER == LITTLE_ENDIAN); + static_assert(platform::ByteOrder::littleEndian); memcpy(&offset, nextEntry + tableSize - entrySize, entrySize); const std::byte *tupleEnd = valueBase + offset; const std::byte *currentEnd = &(*binaryTuple.end()); @@ -75,7 +75,7 @@ ElementView BinaryTupleParser::getNext() { ++elementIndex; uint64_t offset = 0; - static_assert(BYTE_ORDER == LITTLE_ENDIAN); + static_assert(platform::ByteOrder::littleEndian); memcpy(&offset, nextEntry, entrySize); nextEntry += entrySize;
