]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* |
2 | * Copyright (c) 2017 Uber Technologies, Inc. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | #ifndef JAEGERTRACING_PLATFORM_ENDIAN_H | |
18 | #define JAEGERTRACING_PLATFORM_ENDIAN_H | |
19 | ||
20 | #include <cstdint> | |
21 | ||
22 | #if defined(__APPLE__) | |
23 | ||
24 | #include <libkern/OSByteOrder.h> | |
25 | #define htobe16(x) OSSwapHostToBigInt16(x) | |
26 | #define htobe32(x) OSSwapHostToBigInt32(x) | |
27 | #define htobe64(x) OSSwapHostToBigInt64(x) | |
28 | #define be16toh(x) OSSwapBigToHostInt16(x) | |
29 | #define be32toh(x) OSSwapBigToHostInt32(x) | |
30 | #define be64toh(x) OSSwapBigToHostInt64(x) | |
31 | ||
32 | #elif defined(__linux__) | |
33 | #include <endian.h> | |
34 | #elif defined WIN32 | |
35 | #include <winsock2.h> | |
36 | ||
37 | #if BYTE_ORDER == LITTLE_ENDIAN | |
38 | ||
39 | #define htobe16(x) htons(x) | |
40 | #define be16toh(x) ntohs(x) | |
41 | ||
42 | #define htobe32(x) htonl(x) | |
43 | #define be32toh(x) ntohl(x) | |
44 | ||
45 | #define htobe64(x) htonll(x) | |
46 | #define be64toh(x) ntohll(x) | |
47 | #elif BYTE_ORDER == BIG_ENDIAN | |
48 | #define htobe16(x) (x) | |
49 | #define be16toh(x) (x) | |
50 | ||
51 | #define htobe32(x) (x) | |
52 | #define be32toh(x) (x) | |
53 | ||
54 | #define htobe64(x) (x) | |
55 | #define be64toh(x) (x) | |
56 | #else | |
57 | #error byte order not supported | |
58 | #endif | |
59 | #else | |
60 | #error "unsupported platform" | |
61 | #endif | |
62 | ||
63 | namespace jaegertracing { | |
64 | namespace platform { | |
65 | namespace endian { | |
66 | ||
67 | inline uint16_t toBigEndian(uint16_t value) { return htobe16(value); } | |
68 | ||
69 | inline uint32_t toBigEndian(uint32_t value) { return htobe32(value); } | |
70 | ||
71 | inline uint64_t toBigEndian(uint64_t value) { return htobe64(value); } | |
72 | ||
73 | inline uint16_t fromBigEndian(uint16_t value) { return be16toh(value); } | |
74 | ||
75 | inline uint32_t fromBigEndian(uint32_t value) { return be32toh(value); } | |
76 | ||
77 | inline uint64_t fromBigEndian(uint64_t value) { return be64toh(value); } | |
78 | ||
79 | } // namespace endian | |
80 | } // namespace platform | |
81 | } // namespace jaegertracing | |
82 | ||
83 | #endif // JAEGERTRACING_PLATFORM_ENDIAN_H |