RDMA++
Loading...
Searching...
No Matches
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
9#include <infiniband/verbs.h>
10
11namespace rdmapp {
12namespace detail {
13
14static inline uint16_t ntoh(uint16_t const &value) { return ::be16toh(value); }
15
16static inline uint32_t ntoh(uint32_t const &value) { return ::be32toh(value); }
17
18static inline uint64_t ntoh(uint64_t const &value) { return ::be64toh(value); }
19
20static inline uint16_t hton(uint16_t const &value) { return ::htobe16(value); }
21
22static inline uint32_t hton(uint32_t const &value) { return ::htobe32(value); }
23
24static inline uint64_t hton(uint64_t const &value) { return ::htobe64(value); }
25
26template <class T, class It>
27typename std::enable_if<std::is_integral<T>::value>::type
28serialize(T const &value, It &it) {
29 T nvalue = hton(value);
30 std::copy_n(reinterpret_cast<uint8_t *>(&nvalue), sizeof(T), it);
31}
32
33template <class T, class It>
34typename std::enable_if<std::is_same<T, union ibv_gid>::value>::type
35serialize(T const &value, It &it) {
36 std::copy_n(reinterpret_cast<uint8_t const *>(&value), sizeof(T), it);
37}
38
39template <class T, class It>
40typename std::enable_if<std::is_integral<T>::value>::type
41deserialize(It &it, T &value) {
42 std::copy_n(it, sizeof(T), reinterpret_cast<uint8_t *>(&value));
43 it += sizeof(T);
44 value = ntoh(value);
45}
46
47template <class T, class It>
48typename std::enable_if<std::is_same<T, void *>::value>::type
49deserialize(It &it, T &value) {
50 std::copy_n(it, sizeof(T), reinterpret_cast<uint8_t *>(&value));
51 it += sizeof(T);
52 value = reinterpret_cast<void *>(ntoh(reinterpret_cast<uint64_t>(value)));
53}
54
55template <class T, class It>
56typename std::enable_if<std::is_same<T, union ibv_gid>::value>::type
57deserialize(It &it, T &value) {
58 std::copy_n(it, sizeof(T), reinterpret_cast<uint8_t *>(&value));
59 it += sizeof(T);
60}
61
62} // namespace detail
63} // namespace rdmapp