1414#include " flutter/fml/build_config.h"
1515
1616// Compiler intrinsics for flipping endianness.
17- #define BYTESWAP16 (n ) __builtin_bswap16(n)
18- #define BYTESWAP32 (n ) __builtin_bswap32(n)
19- #define BYTESWAP64 (n ) __builtin_bswap64(n)
17+ #define FML_BYTESWAP_16 (n ) __builtin_bswap16(n)
18+ #define FML_BYTESWAP_32 (n ) __builtin_bswap32(n)
19+ #define FML_BYTESWAP_64 (n ) __builtin_bswap64(n)
2020
2121#if defined(_MSC_VER)
22- #define BYTESWAP16 (n ) _byteswap_ushort(n)
23- #define BYTESWAP32 (n ) _byteswap_ulong(n)
24- #define BYTESWAP64 (n ) _byteswap_uint64(n)
22+ #define FML_BYTESWAP_16 (n ) _byteswap_ushort(n)
23+ #define FML_BYTESWAP_32 (n ) _byteswap_ulong(n)
24+ #define FML_BYTESWAP_64 (n ) _byteswap_uint64(n)
2525#endif
2626
2727namespace fml {
2828
2929// / @brief Flips the endianness of the given value.
30- template <typename T>
30+ // / The given value must be an integral type of size 1, 2, 4, or 8.
31+ template <typename T, class = std::enable_if_t <std::is_integral_v<T>>>
3132constexpr T ByteSwap (T n) {
3233 if constexpr (sizeof (T) == 1 ) {
3334 return n;
3435 } else if constexpr (sizeof (T) == 2 ) {
35- return (T)BYTESWAP16 ((uint16_t )n);
36+ return (T)FML_BYTESWAP_16 ((uint16_t )n);
3637 } else if constexpr (sizeof (T) == 4 ) {
37- return (T)BYTESWAP32 ((uint32_t )n);
38+ return (T)FML_BYTESWAP_32 ((uint32_t )n);
3839 } else if constexpr (sizeof (T) == 8 ) {
39- return (T)BYTESWAP64 ((uint64_t )n);
40+ return (T)FML_BYTESWAP_64 ((uint64_t )n);
4041 } else {
4142 static_assert (!sizeof (T), " Unsupported size" );
4243 }
@@ -45,7 +46,8 @@ constexpr T ByteSwap(T n) {
4546// / @brief Convert a known big endian value to match the endianness of the
4647// / current architecture. This is effectively a cross platform
4748// / ntohl/ntohs (as network byte order is always Big Endian).
48- template <typename T>
49+ // / The given value must be an integral type of size 1, 2, 4, or 8.
50+ template <typename T, class = std::enable_if_t <std::is_integral_v<T>>>
4951constexpr T BigEndianToArch (T n) {
5052#if ARCH_CPU_LITTLE_ENDIAN
5153 return ByteSwap<T>(n);
@@ -56,7 +58,8 @@ constexpr T BigEndianToArch(T n) {
5658
5759// / @brief Convert a known little endian value to match the endianness of the
5860// / current architecture.
59- template <typename T>
61+ // / The given value must be an integral type of size 1, 2, 4, or 8.
62+ template <typename T, class = std::enable_if_t <std::is_integral_v<T>>>
6063constexpr T LittleEndianToArch (T n) {
6164#if !ARCH_CPU_LITTLE_ENDIAN
6265 return ByteSwap<T>(n);
0 commit comments