blob: 5597a0ba0b3ed258e1e84cc95a19b256aef5abe4 [file] [log] [blame]
Ratan Gupta37a7a072016-12-14 00:51:23 +05301#pragma once
2
3#include <endian.h>
4#include <stdint.h>
5
6namespace endian
7{
8namespace details
9{
10template <typename T>
11struct convert
12{
13 static T to_network(T) = delete;
14 static T from_network(T) = delete;
15};
16
Patrick Venture537ff142018-11-01 16:37:09 -070017template <>
18struct convert<uint16_t>
Ratan Gupta37a7a072016-12-14 00:51:23 +053019{
20 static uint16_t to_network(uint16_t i)
21 {
22 return htobe16(i);
23 };
24 static uint16_t from_network(uint16_t i)
25 {
26 return be16toh(i);
27 };
28};
29
Patrick Venture537ff142018-11-01 16:37:09 -070030template <>
31struct convert<uint32_t>
Ratan Gupta37a7a072016-12-14 00:51:23 +053032{
33 static uint32_t to_network(uint32_t i)
34 {
35 return htobe32(i);
36 };
37 static uint32_t from_network(uint32_t i)
38 {
39 return be32toh(i);
40 };
41};
Patrick Venture537ff142018-11-01 16:37:09 -070042} // namespace details
43template <typename T>
44T to_network(T i)
Ratan Gupta37a7a072016-12-14 00:51:23 +053045{
46 return details::convert<T>::to_network(i);
47}
Patrick Venture537ff142018-11-01 16:37:09 -070048template <typename T>
49T from_network(T i)
Ratan Gupta37a7a072016-12-14 00:51:23 +053050{
51 return details::convert<T>::from_network(i);
52}
Patrick Venture537ff142018-11-01 16:37:09 -070053} // namespace endian