Ratan Gupta | 37a7a07 | 2016-12-14 00:51:23 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <endian.h> |
| 4 | #include <stdint.h> |
| 5 | |
| 6 | namespace endian |
| 7 | { |
| 8 | namespace details |
| 9 | { |
| 10 | template <typename T> |
| 11 | struct convert |
| 12 | { |
| 13 | static T to_network(T) = delete; |
| 14 | static T from_network(T) = delete; |
| 15 | }; |
| 16 | |
| 17 | template<> 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 | |
| 29 | template<> 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 | } |
| 41 | template<typename T> T to_network(T i) |
| 42 | { |
| 43 | return details::convert<T>::to_network(i); |
| 44 | } |
| 45 | template<typename T> T from_network(T i) |
| 46 | { |
| 47 | return details::convert<T>::from_network(i); |
| 48 | } |
| 49 | } |