11// Copyright (c) 2017 Dr. Colin Hirsch and Daniel Frey
22// Please see LICENSE for license or visit https://github.com/taocpp/json/
33
4- #ifndef TAOCPP_JSON_INCLUDE_INTERNAL_TEMPORARY_HPP
5- #define TAOCPP_JSON_INCLUDE_INTERNAL_TEMPORARY_HPP
6-
7- #include < cstdint>
8- #include < cstring>
9-
10- #if defined( _WIN32 ) && !defined( __MINGW32__ )
11- #include < stdlib.h> // TODO: Or is intrin.h the 'more correct' header?
12- #endif
13-
14- // TODO: Better endian detection?
15- // TODO: Where to put this header?
4+ #ifndef TAOCPP_JSON_INCLUDE_INTERNAL_ENDIAN_GCC_HPP
5+ #define TAOCPP_JSON_INCLUDE_INTERNAL_ENDIAN_GCC_HPP
166
177namespace tao
188{
199 namespace json
2010 {
2111 namespace internal
2212 {
13+ #if not defined( __BYTE_ORDER__ )
14+ #error TODO -- what?
15+ #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
2316
24- #if defined( _WIN32 ) && !defined( __MINGW32__ )
17+ template < unsigned S >
18+ struct to_and_from_be
19+ {
20+ template < typename T >
21+ static T convert ( const T n ) noexcept
22+ {
23+ return n;
24+ }
25+ };
2526
2627 template < unsigned S >
27- struct bswap ;
28+ struct to_and_from_le ;
2829
2930 template <>
30- struct bswap < 1 >
31+ struct to_and_from_le < 1 >
3132 {
3233 static std::uint8_t convert ( const std::uint8_t n ) noexcept
3334 {
@@ -36,16 +37,16 @@ namespace tao
3637 };
3738
3839 template <>
39- struct bswap < 2 >
40+ struct to_and_from_le < 2 >
4041 {
4142 static std::uint16_t convert ( const std::uint16_t n ) noexcept
4243 {
43- return _byteswap_ushort ( n );
44+ return __builtin_bswap16 ( n );
4445 }
4546 };
4647
4748 template <>
48- struct bswap < 4 >
49+ struct to_and_from_le < 4 >
4950 {
5051 static float convert ( float n ) noexcept
5152 {
@@ -58,12 +59,12 @@ namespace tao
5859
5960 static std::uint32_t convert ( const std::uint32_t n ) noexcept
6061 {
61- return _byteswap_ulong ( n );
62+ return __builtin_bswap32 ( n );
6263 }
6364 };
6465
6566 template <>
66- struct bswap < 8 >
67+ struct to_and_from_le < 8 >
6768 {
6869 static double convert ( double n ) noexcept
6970 {
@@ -76,18 +77,14 @@ namespace tao
7677
7778 static std::uint64_t convert ( const std::uint64_t n ) noexcept
7879 {
79- return _byteswap_uint64 ( n );
80+ return __builtin_bswap64 ( n );
8081 }
8182 };
8283
83- #elif not defined( __BYTE_ORDER__ )
84-
85- #error TODO!
86-
87- #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
84+ #elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
8885
8986 template < unsigned S >
90- struct bswap
87+ struct to_and_from_le
9188 {
9289 template < typename T >
9390 static T convert ( const T n ) noexcept
@@ -96,13 +93,11 @@ namespace tao
9693 }
9794 };
9895
99- #elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
100-
10196 template < unsigned S >
102- struct bswap ;
97+ struct to_and_from_be ;
10398
10499 template <>
105- struct bswap < 1 >
100+ struct to_and_from_be < 1 >
106101 {
107102 static std::uint8_t convert ( const std::uint8_t n ) noexcept
108103 {
@@ -111,7 +106,7 @@ namespace tao
111106 };
112107
113108 template <>
114- struct bswap < 2 >
109+ struct to_and_from_be < 2 >
115110 {
116111 static std::uint16_t convert ( const std::uint16_t n ) noexcept
117112 {
@@ -120,7 +115,7 @@ namespace tao
120115 };
121116
122117 template <>
123- struct bswap < 4 >
118+ struct to_and_from_be < 4 >
124119 {
125120 static float convert ( float n ) noexcept
126121 {
@@ -138,7 +133,7 @@ namespace tao
138133 };
139134
140135 template <>
141- struct bswap < 8 >
136+ struct to_and_from_be < 8 >
142137 {
143138 static double convert ( double n ) noexcept
144139 {
@@ -156,35 +151,12 @@ namespace tao
156151 };
157152
158153#else
159-
160154#error Unknown host byte order!
161-
162155#endif
156+ } // internal
163157
164- template < typename N >
165- N h_to_be ( const N n ) noexcept
166- {
167- return N ( bswap< sizeof ( N ) >::convert ( n ) );
168- }
169-
170- template < typename N >
171- N be_to_h ( const N n ) noexcept
172- {
173- return h_to_be ( n );
174- }
175-
176- template < typename N >
177- N be_to_h ( const void * p ) noexcept
178- {
179- N n;
180- std::memcpy ( &n, p, sizeof ( n ) );
181- return internal::be_to_h ( n );
182- }
183-
184- } // namespace internal
185-
186- } // namespace json
158+ } // json
187159
188- } // namespace tao
160+ } // tao
189161
190162#endif
0 commit comments