GCS  0.2.3
gu_byteswap.hpp
1 // Copyright (C) 2012 Codership Oy <info@codership.com>
2 
9 #ifndef _gu_byteswap_hpp_
10 #define _gu_byteswap_hpp_
11 
12 #include "gu_byteswap.h"
13 
14 #include <stdint.h>
15 
16 namespace gu
17 {
18 
19 /* General template: undefined */
20 template <typename T> T gtoh (const T& val)
21 {
22  // to generate error on compilation rather then linking
23  return val.this_template_does_not_support_this_type();
24 }
25 
26 /* Specialized templates */
27 
28 template <> GU_FORCE_INLINE uint8_t gtoh (const uint8_t& val)
29 {
30  return val;
31 }
32 
33 template <> GU_FORCE_INLINE uint16_t gtoh (const uint16_t& val)
34 {
35  return gtoh16(val);
36 }
37 
38 template <> GU_FORCE_INLINE unsigned int gtoh (const unsigned int& val)
39 {
40  return gtoh32(val);
41 }
42 
43 #if __LONG_MAX__ == __INT_MAX__
44 template <> GU_FORCE_INLINE unsigned long gtoh (const unsigned long& val)
45 {
46  return gtoh32(val);
47 }
48 #elif __LONG_MAX__ == __LONG_LONG_MAX__
49 template <> GU_FORCE_INLINE unsigned long gtoh (const unsigned long& val)
50 {
51  return gtoh64(val);
52 }
53 #else
54 # error can not determine size of long
55 #endif
56 
57 template <> GU_FORCE_INLINE unsigned long long gtoh (const unsigned long long& val)
58 {
59  return gtoh64(val);
60 }
61 
62 template <typename T> T htog (const T& val) { return gtoh<T>(val); }
63 
64 } /* namespace gu */
65 
66 #endif /* _gu_byteswap_hpp_ */