Util--  1.1
endian.hh
Go to the documentation of this file.
1 #ifndef __UTILMM_SYSTEM_ENDIAN_HH
2 #define __UTILMM_SYSTEM_ENDIAN_HH
3 
4 #include <utilmm/config/config.h>
5 #include <boost/integer.hpp>
6 
7 namespace utilmm
8 {
13  namespace endian
14  {
15  namespace details {
16  template<int size> struct type_from_size
17  { typedef typename boost::uint_t<size>::least least; };
18  template<> struct type_from_size<64>
19  { typedef uint64_t least; };
20 
21  template<int size, typename D>
22  void swap_helper(const D data, D& buffer);
23 
24  template<> inline void swap_helper<1, uint8_t>(const uint8_t data, uint8_t& buffer)
25  { buffer = data; }
26  template<> inline void swap_helper<2, uint16_t>(const uint16_t data, uint16_t& buffer)
27  { buffer = ((data >> 8) & 0xFF) | ((data << 8) & 0xFF00); }
28  template<> inline void swap_helper<4, uint32_t>(const uint32_t data, uint32_t& buffer)
29  { buffer = ((data & 0xFF000000) >> 24) | ((data & 0x00FF0000) >> 8) | ((data & 0xFF) << 24) | ((data & 0xFF00) << 8); }
30  template<> inline void swap_helper<8, uint64_t>(const uint64_t data, uint64_t& buffer)
31  {
32  const uint32_t
33  src_low (data & 0xFFFFFFFF)
34  , src_high(data >> 32);
35 
36  uint32_t dst_low, dst_high;
37  swap_helper<4, uint32_t>( src_high, dst_low );
38  swap_helper<4, uint32_t>( src_low, dst_high );
39 
40  buffer = static_cast<uint64_t>(dst_high) << 32 | dst_low;
41  }
42  }
43 
44  /* Returns in \c buffer the value of \c data with endianness swapped */
45  template<typename S>
46  inline void swap(const S data, S& buffer)
47  {
49  details::swap_helper<sizeof(S), T> (reinterpret_cast<const T&>(data), reinterpret_cast<T&>(buffer));
50  }
51 
52  /* Returns the value of \c data with endianness swapped */
53  template<typename S>
54  inline S swap(const S data)
55  { S ret;
56  swap(data, ret);
57  return ret;
58  }
59 
60 #ifdef WORDS_BIGENDIAN
61 
63  template<typename S>
64  inline void to_big(const S source, S& dest) { dest = source; }
67  template<typename S>
68  inline S to_big(const S source) { return source; }
71  template<typename S>
72  inline void to_little(const S source, S& dest) { swap<S>(source, dest); }
75  template<typename S>
76  inline S to_little(const S source) { return swap<S>(source); }
77 #else
78  template<typename S>
79  inline void to_big(const S source, S& dest) { swap<S>(source, dest); }
80  template<typename S>
81  inline S to_big(const S source) { return swap<S>(source); }
82  template<typename S>
83  inline void to_little(const S source, S& dest) { dest = source; }
84  template<typename S>
85  inline S to_little(const S source) { return source; }
86 #endif
87 
90  template<typename S>
91  inline void from_network(const S source, S& dest) { to_network(source, dest); }
94  template<typename S>
95  inline S from_network(const S source) { return to_network(source); }
98  template<typename S>
99  inline void from_little(const S source, S& dest) { to_little(source, dest); }
102  template<typename S>
103  inline S from_little(const S source) { return to_little(source); }
106  template<typename S>
107  inline void from_big(const S source, S& dest) { to_big(source, dest); }
110  template<typename S>
111  inline S from_big(const S source) { return to_big(source); }
112 
113  template<typename S>
114  inline void to_network(const S source, S& dest) { return to_big(source, dest); }
115  template<typename S>
116  inline S to_network(const S source) { return to_big(source); }
117  }
118 }
119 
120 #endif
121 
void swap_helper< 8, uint64_t >(const uint64_t data, uint64_t &buffer)
Definition: endian.hh:30
uint64_t least
Definition: endian.hh:19
boost::graph_traits< BidirectionalGraph >::vertex_descriptor source(const Edge &e, const undirected_graph< BidirectionalGraph, GRef > &g)
Definition: undirected_graph.hh:268
void to_little(const S source, S &dest)
Definition: endian.hh:83
void swap_helper(const D data, D &buffer)
boost::uint_t< size >::least least
Definition: endian.hh:17
void to_network(const S source, S &dest)
Definition: endian.hh:114
void from_little(const S source, S &dest)
Definition: endian.hh:99
void swap(const S data, S &buffer)
Definition: endian.hh:46
void from_big(const S source, S &dest)
Definition: endian.hh:107
void swap_helper< 2, uint16_t >(const uint16_t data, uint16_t &buffer)
Definition: endian.hh:26
void swap_helper< 4, uint32_t >(const uint32_t data, uint32_t &buffer)
Definition: endian.hh:28
void from_network(const S source, S &dest)
Definition: endian.hh:91
Definition: auto_flag.hh:6
void swap_helper< 1, uint8_t >(const uint8_t data, uint8_t &buffer)
Definition: endian.hh:24
void to_big(const S source, S &dest)
Definition: endian.hh:79

Generated on Mon Oct 22 2018 18:39:28 for Util-- by doxygen 1.8.13
SourceForge.net Project Page