Tom Joseph | 15d88fb | 2016-08-05 03:30:04 -0500 | [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_ipmi(T) = delete; |
| 14 | static T from_ipmi(T) = delete; |
| 15 | static T to_network(T) = delete; |
| 16 | static T from_network(T) = delete; |
| 17 | }; |
| 18 | |
| 19 | template<> struct convert<uint16_t> |
| 20 | { |
| 21 | static uint16_t to_ipmi(uint16_t i) { return htole16(i); }; |
| 22 | static uint16_t from_ipmi(uint16_t i) { return le16toh(i); }; |
| 23 | static uint16_t to_network(uint16_t i) { return htobe16(i); }; |
| 24 | static uint16_t from_network(uint16_t i) { return be16toh(i); }; |
| 25 | }; |
| 26 | |
| 27 | template<> struct convert<uint32_t> |
| 28 | { |
| 29 | static uint32_t to_ipmi(uint32_t i) { return htole32(i); }; |
| 30 | static uint32_t from_ipmi(uint32_t i) { return le32toh(i); }; |
| 31 | static uint32_t to_network(uint32_t i) { return htobe32(i); }; |
| 32 | static uint32_t from_network(uint32_t i) { return be32toh(i); }; |
| 33 | }; |
| 34 | } |
| 35 | |
| 36 | template<typename T> T to_ipmi(T i) |
| 37 | { |
| 38 | return details::convert<T>::to_ipmi(i); |
| 39 | } |
| 40 | |
| 41 | template<typename T> T from_ipmi(T i) |
| 42 | { |
| 43 | return details::convert<T>::from_ipmi(i); |
| 44 | } |
| 45 | |
| 46 | template<typename T> T to_network(T i) |
| 47 | { |
| 48 | return details::convert<T>::to_network(i); |
| 49 | } |
| 50 | template<typename T> T from_network(T i) |
| 51 | { |
| 52 | return details::convert<T>::from_network(i); |
| 53 | } |
| 54 | } |