blob: 284fd03bd270385c647c3ac56d9e7cc284842542 [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
17template<> struct convert<uint16_t>
18{
19 static uint16_t to_network(uint16_t i)
20 {
21 return htobe16(i);
22 };
23 static uint16_t from_network(uint16_t i)
24 {
25 return be16toh(i);
26 };
27};
28
29template<> struct convert<uint32_t>
30{
31 static uint32_t to_network(uint32_t i)
32 {
33 return htobe32(i);
34 };
35 static uint32_t from_network(uint32_t i)
36 {
37 return be32toh(i);
38 };
39};
40}
41template<typename T> T to_network(T i)
42{
43 return details::convert<T>::to_network(i);
44}
45template<typename T> T from_network(T i)
46{
47 return details::convert<T>::from_network(i);
48}
49}