UCX++
serdes.h
1#pragma once
2
3#include <algorithm>
4#include <cstdint>
5#include <endian.h>
6#include <netinet/in.h>
7#include <type_traits>
8
9namespace ucxpp {
10namespace detail {
11
12static inline uint16_t ntoh(uint16_t const &value) { return ::be16toh(value); }
13
14static inline uint32_t ntoh(uint32_t const &value) { return ::be32toh(value); }
15
16static inline uint64_t ntoh(uint64_t const &value) { return ::be64toh(value); }
17
18static inline uint16_t hton(uint16_t const &value) { return ::htobe16(value); }
19
20static inline uint32_t hton(uint32_t const &value) { return ::htobe32(value); }
21
22static inline uint64_t hton(uint64_t const &value) { return ::htobe64(value); }
23
24template <class T, class It,
25 class U = typename std::enable_if<std::is_integral<T>::value>::type>
26void serialize(T const &value, It &it) {
27 T nvalue = hton(value);
28 std::copy_n(reinterpret_cast<uint8_t *>(&nvalue), sizeof(T), it);
29}
30
31template <class T, class It,
32 class U = typename std::enable_if<std::is_integral<T>::value>::type>
33void deserialize(It &it, T &value) {
34 std::copy_n(it, sizeof(T), reinterpret_cast<uint8_t *>(&value));
35 it += sizeof(T);
36 value = ntoh(value);
37}
38
39} // namespace detail
40} // namespace ucxpp