Skip to content

Commit ec51938

Browse files
authored
Merge pull request #868 from RicoAntonioFelix/dev
Changed getbits from a macro to a function template
2 parents 55c958e + a76074b commit ec51938

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

api/net/util.hpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#ifndef NET_UTIL_HPP
1919
#define NET_UTIL_HPP
2020

21-
#include <stdint.h>
21+
#include <cstdint>
2222

2323
namespace net {
2424

@@ -28,29 +28,32 @@ namespace net {
2828
* e.g., getbits(x, 31, 8) -- highest byte
2929
* getbits(x, 7, 8) -- lowest byte
3030
*/
31-
#define getbits(x, p, n) (((x) >> ((p) + 1 - (n))) & ~(~0 << (n)))
31+
template<typename T>
32+
constexpr inline auto getbits(T&& x, T&& p, T&& n) noexcept {
33+
return (x >> ((p + 1) - n)) & ~(~0 << n);
34+
}
3235

33-
inline uint16_t ntohs(uint16_t n) noexcept {
36+
constexpr inline uint16_t ntohs(const uint16_t n) noexcept {
3437
return __builtin_bswap16(n);
3538
}
3639

37-
inline uint16_t htons(uint16_t n) noexcept {
40+
constexpr inline uint16_t htons(const uint16_t n) noexcept {
3841
return __builtin_bswap16(n);
3942
}
4043

41-
inline uint32_t ntohl(uint32_t n) noexcept {
44+
constexpr inline uint32_t ntohl(const uint32_t n) noexcept {
4245
return __builtin_bswap32(n);
4346
}
4447

45-
inline uint32_t htonl(uint32_t n) noexcept {
48+
constexpr inline uint32_t htonl(const uint32_t n) noexcept {
4649
return __builtin_bswap32(n);
4750
}
4851

49-
inline uint64_t ntohll(uint64_t n) noexcept {
52+
constexpr inline uint64_t ntohll(const uint64_t n) noexcept {
5053
return __builtin_bswap64(n);
5154
}
5255

53-
inline uint64_t htonll(uint64_t n) noexcept {
56+
constexpr inline uint64_t htonll(const uint64_t n) noexcept {
5457
return __builtin_bswap64(n);
5558
}
5659

0 commit comments

Comments
 (0)