|
1 | | -// Copyright (c) 2014-2020 The Bitcoin Core developers |
| 1 | +// Copyright (c) 2014-present The Bitcoin Core developers |
2 | 2 | // Distributed under the MIT software license, see the accompanying |
3 | 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
4 | 4 |
|
|
7 | 7 |
|
8 | 8 | #include <compat/endian.h> |
9 | 9 |
|
| 10 | +#include <concepts> |
| 11 | +#include <cstddef> |
10 | 12 | #include <cstdint> |
11 | 13 | #include <cstring> |
12 | 14 |
|
13 | | -uint16_t static inline ReadLE16(const unsigned char* ptr) |
| 15 | +template <typename B> |
| 16 | +concept ByteType = std::same_as<B, unsigned char> || std::same_as<B, std::byte>; |
| 17 | + |
| 18 | +template <ByteType B> |
| 19 | +inline uint16_t ReadLE16(const B* ptr) |
14 | 20 | { |
15 | 21 | uint16_t x; |
16 | 22 | memcpy(&x, ptr, 2); |
17 | 23 | return le16toh_internal(x); |
18 | 24 | } |
19 | 25 |
|
20 | | -uint32_t static inline ReadLE32(const unsigned char* ptr) |
| 26 | +template <ByteType B> |
| 27 | +inline uint32_t ReadLE32(const B* ptr) |
21 | 28 | { |
22 | 29 | uint32_t x; |
23 | 30 | memcpy(&x, ptr, 4); |
24 | 31 | return le32toh_internal(x); |
25 | 32 | } |
26 | 33 |
|
27 | | -uint64_t static inline ReadLE64(const unsigned char* ptr) |
| 34 | +template <ByteType B> |
| 35 | +inline uint64_t ReadLE64(const B* ptr) |
28 | 36 | { |
29 | 37 | uint64_t x; |
30 | 38 | memcpy(&x, ptr, 8); |
31 | 39 | return le64toh_internal(x); |
32 | 40 | } |
33 | 41 |
|
34 | | -void static inline WriteLE16(unsigned char* ptr, uint16_t x) |
| 42 | +template <ByteType B> |
| 43 | +inline void WriteLE16(B* ptr, uint16_t x) |
35 | 44 | { |
36 | 45 | uint16_t v = htole16_internal(x); |
37 | 46 | memcpy(ptr, &v, 2); |
38 | 47 | } |
39 | 48 |
|
40 | | -void static inline WriteLE32(unsigned char* ptr, uint32_t x) |
| 49 | +template <ByteType B> |
| 50 | +inline void WriteLE32(B* ptr, uint32_t x) |
41 | 51 | { |
42 | 52 | uint32_t v = htole32_internal(x); |
43 | 53 | memcpy(ptr, &v, 4); |
44 | 54 | } |
45 | 55 |
|
46 | | -void static inline WriteLE64(unsigned char* ptr, uint64_t x) |
| 56 | +template <ByteType B> |
| 57 | +inline void WriteLE64(B* ptr, uint64_t x) |
47 | 58 | { |
48 | 59 | uint64_t v = htole64_internal(x); |
49 | 60 | memcpy(ptr, &v, 8); |
50 | 61 | } |
51 | 62 |
|
52 | | -uint16_t static inline ReadBE16(const unsigned char* ptr) |
| 63 | +template <ByteType B> |
| 64 | +inline uint16_t ReadBE16(const B* ptr) |
53 | 65 | { |
54 | 66 | uint16_t x; |
55 | 67 | memcpy(&x, ptr, 2); |
56 | 68 | return be16toh_internal(x); |
57 | 69 | } |
58 | 70 |
|
59 | | -uint32_t static inline ReadBE32(const unsigned char* ptr) |
| 71 | +template <ByteType B> |
| 72 | +inline uint32_t ReadBE32(const B* ptr) |
60 | 73 | { |
61 | 74 | uint32_t x; |
62 | 75 | memcpy(&x, ptr, 4); |
63 | 76 | return be32toh_internal(x); |
64 | 77 | } |
65 | 78 |
|
66 | | -uint64_t static inline ReadBE64(const unsigned char* ptr) |
| 79 | +template <ByteType B> |
| 80 | +inline uint64_t ReadBE64(const B* ptr) |
67 | 81 | { |
68 | 82 | uint64_t x; |
69 | 83 | memcpy(&x, ptr, 8); |
70 | 84 | return be64toh_internal(x); |
71 | 85 | } |
72 | 86 |
|
73 | | -void static inline WriteBE16(unsigned char* ptr, uint16_t x) |
| 87 | +template <ByteType B> |
| 88 | +inline void WriteBE16(B* ptr, uint16_t x) |
74 | 89 | { |
75 | 90 | uint16_t v = htobe16_internal(x); |
76 | 91 | memcpy(ptr, &v, 2); |
77 | 92 | } |
78 | 93 |
|
79 | | -void static inline WriteBE32(unsigned char* ptr, uint32_t x) |
| 94 | +template <ByteType B> |
| 95 | +inline void WriteBE32(B* ptr, uint32_t x) |
80 | 96 | { |
81 | 97 | uint32_t v = htobe32_internal(x); |
82 | 98 | memcpy(ptr, &v, 4); |
83 | 99 | } |
84 | 100 |
|
85 | | -void static inline WriteBE64(unsigned char* ptr, uint64_t x) |
| 101 | +template <ByteType B> |
| 102 | +inline void WriteBE64(B* ptr, uint64_t x) |
86 | 103 | { |
87 | 104 | uint64_t v = htobe64_internal(x); |
88 | 105 | memcpy(ptr, &v, 8); |
|
0 commit comments