Embedded Template Library  1.0
endianness.h
Go to the documentation of this file.
1 
3 /******************************************************************************
4 The MIT License(MIT)
5 
6 Embedded Template Library.
7 https://github.com/ETLCPP/etl
8 https://www.etlcpp.com
9 
10 Copyright(c) 2014 jwellbelove
11 
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files(the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions :
18 
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21 
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29 ******************************************************************************/
30 
31 #ifndef ETL_ENDIAN_INCLUDED
32 #define ETL_ENDIAN_INCLUDED
33 
34 #include <stdint.h>
35 
36 #include "platform.h"
37 #include "enum_type.h"
38 #include "binary.h"
39 
43 
44 namespace etl
45 {
46  //***************************************************************************
49  //***************************************************************************
50  struct endian
51  {
52  enum enum_type
53  {
54  little,
55  big,
56  native
57  };
58 
59  ETL_DECLARE_ENUM_TYPE(endian, int)
60  ETL_ENUM_TYPE(little, "little")
61  ETL_ENUM_TYPE(big, "big")
62  ETL_ENUM_TYPE(native, "native")
63  ETL_END_ENUM_TYPE
64  };
65 
66  //***************************************************************************
69  //***************************************************************************
70  struct endianness
71  {
72  etl::endian operator ()() const
73  {
74  return etl::endian(*this);
75  }
76 
77  operator etl::endian() const
78  {
79  return get();
80  }
81 
82  static etl::endian value()
83  {
84  return get();
85  }
86 
87  private:
88 
89  static etl::endian get()
90  {
91  static const union U
92  {
93  U()
94  : ui32(0x12345678)
95  {
96  }
97 
98  uint32_t ui32;
99  uint16_t ui16[2];
100  } u;
101 
102  return (u.ui16[0] == 0x5678) ? etl::endian::little : etl::endian::big;
103  }
104  };
105 
106  //***************************************************************************
107  inline uint8_t ntoh(const uint8_t network)
108  {
109  return network;
110  }
111 
112  //***************************************************************************
113  inline uint16_t ntoh(const uint16_t network)
114  {
115  if (endianness::value() == endian::little)
116  {
117  return etl::reverse_bytes(network);
118  }
119  else
120  {
121  return network;
122  }
123  }
124 
125  //***************************************************************************
126  inline uint32_t ntoh(const uint32_t network)
127  {
128  if (endianness::value() == endian::little)
129  {
130  return etl::reverse_bytes(network);
131  }
132  else
133  {
134  return network;
135  }
136  }
137 
138 #if ETL_USING_64BIT_TYPES
139  //***************************************************************************
140  inline uint64_t ntoh(const uint64_t network)
141  {
142  if (endianness::value() == endian::little)
143  {
144  return etl::reverse_bytes(network);
145  }
146  else
147  {
148  return network;
149  }
150  }
151 #endif
152 
153  //***************************************************************************
154  inline uint8_t hton(const uint8_t host)
155  {
156  return host;
157  }
158 
159  //***************************************************************************
160  inline uint16_t hton(const uint16_t host)
161  {
162  if (endianness::value() == endian::little)
163  {
164  return etl::reverse_bytes(host);
165  }
166  else
167  {
168  return host;
169  }
170  }
171 
172  //***************************************************************************
173  inline uint32_t hton(const uint32_t host)
174  {
175  if (endianness::value() == endian::little)
176  {
177  return etl::reverse_bytes(host);
178  }
179  else
180  {
181  return host;
182  }
183  }
184 
185 #if ETL_USING_64BIT_TYPES
186  //***************************************************************************
187  inline uint64_t hton(const uint64_t host)
188  {
189  if (endianness::value() == endian::little)
190  {
191  return etl::reverse_bytes(host);
192  }
193  else
194  {
195  return host;
196  }
197  }
198 #endif
199 }
200 
201 #endif
ETL_CONSTEXPR uint16_t reverse_bytes(uint16_t value)
Definition: binary.h:580
Definition: endianness.h:51
Definition: endianness.h:71
Definition: absolute.h:37