Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1 | From a614d8e20fa9e4fd16b699d581ddac2956c120f5 Mon Sep 17 00:00:00 2001 |
| 2 | From: Khem Raj <raj.khem@gmail.com> |
| 3 | Date: Tue, 19 Sep 2017 10:04:02 -0700 |
| 4 | Subject: [PATCH 1/2] flatbuffers: Move EndianSwap template to |
| 5 | flatbuffers/base.h |
| 6 | |
| 7 | Clang complains |
| 8 | call to function 'EndianSwap' that is neither visible in the template definition nor found by argument-dependent lookup |
| 9 | return EndianSwap(t); |
| 10 | |
| 11 | This seems to be due to limitation of two-phase lookup of dependent names in template definitions |
| 12 | |
| 13 | Its not being found using associated namespaces therefore |
| 14 | it has to be made visible at the template definition site as well |
| 15 | |
| 16 | Signed-off-by: Khem Raj <raj.khem@gmail.com> |
| 17 | --- |
| 18 | Upstream-Status: Submitted |
| 19 | |
| 20 | include/flatbuffers/base.h | 33 +++++++++++++++++++++++++++++++++ |
| 21 | include/flatbuffers/flatbuffers.h | 32 -------------------------------- |
| 22 | 2 files changed, 33 insertions(+), 32 deletions(-) |
| 23 | |
| 24 | diff --git a/include/flatbuffers/base.h b/include/flatbuffers/base.h |
| 25 | index f051755..c73fb2d 100644 |
| 26 | --- a/include/flatbuffers/base.h |
| 27 | +++ b/include/flatbuffers/base.h |
| 28 | @@ -150,6 +150,39 @@ typedef uintmax_t largest_scalar_t; |
| 29 | // We support aligning the contents of buffers up to this size. |
| 30 | #define FLATBUFFERS_MAX_ALIGNMENT 16 |
| 31 | |
| 32 | +template<typename T> T EndianSwap(T t) { |
| 33 | + #if defined(_MSC_VER) |
| 34 | + #define FLATBUFFERS_BYTESWAP16 _byteswap_ushort |
| 35 | + #define FLATBUFFERS_BYTESWAP32 _byteswap_ulong |
| 36 | + #define FLATBUFFERS_BYTESWAP64 _byteswap_uint64 |
| 37 | + #else |
| 38 | + #if defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ < 408 |
| 39 | + // __builtin_bswap16 was missing prior to GCC 4.8. |
| 40 | + #define FLATBUFFERS_BYTESWAP16(x) \ |
| 41 | + static_cast<uint16_t>(__builtin_bswap32(static_cast<uint32_t>(x) << 16)) |
| 42 | + #else |
| 43 | + #define FLATBUFFERS_BYTESWAP16 __builtin_bswap16 |
| 44 | + #endif |
| 45 | + #define FLATBUFFERS_BYTESWAP32 __builtin_bswap32 |
| 46 | + #define FLATBUFFERS_BYTESWAP64 __builtin_bswap64 |
| 47 | + #endif |
| 48 | + if (sizeof(T) == 1) { // Compile-time if-then's. |
| 49 | + return t; |
| 50 | + } else if (sizeof(T) == 2) { |
| 51 | + auto r = FLATBUFFERS_BYTESWAP16(*reinterpret_cast<uint16_t *>(&t)); |
| 52 | + return *reinterpret_cast<T *>(&r); |
| 53 | + } else if (sizeof(T) == 4) { |
| 54 | + auto r = FLATBUFFERS_BYTESWAP32(*reinterpret_cast<uint32_t *>(&t)); |
| 55 | + return *reinterpret_cast<T *>(&r); |
| 56 | + } else if (sizeof(T) == 8) { |
| 57 | + auto r = FLATBUFFERS_BYTESWAP64(*reinterpret_cast<uint64_t *>(&t)); |
| 58 | + return *reinterpret_cast<T *>(&r); |
| 59 | + } else { |
| 60 | + assert(0); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | template<typename T> T EndianScalar(T t) { |
| 66 | #if FLATBUFFERS_LITTLEENDIAN |
| 67 | return t; |
| 68 | diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h |
| 69 | index 9216cf4..f749dcb 100644 |
| 70 | --- a/include/flatbuffers/flatbuffers.h |
| 71 | +++ b/include/flatbuffers/flatbuffers.h |
| 72 | @@ -37,38 +37,6 @@ inline void EndianCheck() { |
| 73 | (void)endiantest; |
| 74 | } |
| 75 | |
| 76 | -template<typename T> T EndianSwap(T t) { |
| 77 | - #if defined(_MSC_VER) |
| 78 | - #define FLATBUFFERS_BYTESWAP16 _byteswap_ushort |
| 79 | - #define FLATBUFFERS_BYTESWAP32 _byteswap_ulong |
| 80 | - #define FLATBUFFERS_BYTESWAP64 _byteswap_uint64 |
| 81 | - #else |
| 82 | - #if defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ < 408 |
| 83 | - // __builtin_bswap16 was missing prior to GCC 4.8. |
| 84 | - #define FLATBUFFERS_BYTESWAP16(x) \ |
| 85 | - static_cast<uint16_t>(__builtin_bswap32(static_cast<uint32_t>(x) << 16)) |
| 86 | - #else |
| 87 | - #define FLATBUFFERS_BYTESWAP16 __builtin_bswap16 |
| 88 | - #endif |
| 89 | - #define FLATBUFFERS_BYTESWAP32 __builtin_bswap32 |
| 90 | - #define FLATBUFFERS_BYTESWAP64 __builtin_bswap64 |
| 91 | - #endif |
| 92 | - if (sizeof(T) == 1) { // Compile-time if-then's. |
| 93 | - return t; |
| 94 | - } else if (sizeof(T) == 2) { |
| 95 | - auto r = FLATBUFFERS_BYTESWAP16(*reinterpret_cast<uint16_t *>(&t)); |
| 96 | - return *reinterpret_cast<T *>(&r); |
| 97 | - } else if (sizeof(T) == 4) { |
| 98 | - auto r = FLATBUFFERS_BYTESWAP32(*reinterpret_cast<uint32_t *>(&t)); |
| 99 | - return *reinterpret_cast<T *>(&r); |
| 100 | - } else if (sizeof(T) == 8) { |
| 101 | - auto r = FLATBUFFERS_BYTESWAP64(*reinterpret_cast<uint64_t *>(&t)); |
| 102 | - return *reinterpret_cast<T *>(&r); |
| 103 | - } else { |
| 104 | - assert(0); |
| 105 | - } |
| 106 | -} |
| 107 | - |
| 108 | template<typename T> FLATBUFFERS_CONSTEXPR size_t AlignOf() { |
| 109 | #ifdef _MSC_VER |
| 110 | return __alignof(T); |
| 111 | -- |
| 112 | 2.14.1 |
| 113 | |