Embedded Template Library 1.0
byte.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5Embedded Template Library.
6https://github.com/ETLCPP/etl
7https://www.etlcpp.com
8Copyright(c) 2022 John Wellbelove
9Permission is hereby granted, free of charge, to any person obtaining a copy
10of this software and associated documentation files(the "Software"), to deal
11in the Software without restriction, including without limitation the rights
12to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
13copies of the Software, and to permit persons to whom the Software is
14furnished to do so, subject to the following conditions :
15The above copyright notice and this permission notice shall be included in all
16copies or substantial portions of the Software.
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24******************************************************************************/
25
26#ifndef ETL_BYTE_INCLUDED
27#define ETL_BYTE_INCLUDED
28
29#include "platform.h"
30#include "type_traits.h"
31
32namespace etl
33{
34#if ETL_USING_CPP11 && !defined(ETL_BYTE_FORCE_CPP03_IMPLEMENTATION)
35
36 enum class byte : unsigned char {};
37
38 //*************************************************************************
40 //*************************************************************************
41 template <typename TInteger>
42 constexpr
44 to_integer(etl::byte b) noexcept
45 {
46 return TInteger(b);
47 }
48
49 //*************************************************************************
51 //*************************************************************************
52 template <typename TInteger>
53 constexpr
55 operator <<(etl::byte b, TInteger shift) noexcept
56 {
57 return etl::byte(static_cast<unsigned int>(b) << shift);
58 }
59
60 //*************************************************************************
62 //*************************************************************************
63 template <typename TInteger>
64 constexpr
66 operator >>(etl::byte b, TInteger shift) noexcept
67 {
68 return etl::byte(static_cast<unsigned int>(b) >> shift);
69 }
70
71 //*************************************************************************
73 //*************************************************************************
74 template <typename TInteger>
75 constexpr
77 operator <<=(etl::byte& b, TInteger shift) noexcept
78 {
79 return b = b << shift;;
80 }
81
82 //*************************************************************************
84 //*************************************************************************
85 template <typename TInteger>
86 constexpr
88 operator >>=(etl::byte& b, TInteger shift) noexcept
89 {
90 return b = b >> shift;
91 }
92
93 //*************************************************************************
95 //*************************************************************************
96 inline constexpr etl::byte operator |(etl::byte lhs, etl::byte rhs) noexcept
97 {
98 return etl::byte(static_cast<unsigned int>(lhs) | static_cast<unsigned int>(rhs));
99 }
100
101 //*************************************************************************
103 //*************************************************************************
104 inline constexpr etl::byte operator &(etl::byte lhs, etl::byte rhs) noexcept
105 {
106 return etl::byte(static_cast<unsigned int>(lhs) & static_cast<unsigned int>(rhs));
107 }
108
109 //*************************************************************************
111 //*************************************************************************
112 inline constexpr etl::byte operator ^(etl::byte lhs, etl::byte rhs) noexcept
113 {
114 return etl::byte(static_cast<unsigned int>(lhs) ^ static_cast<unsigned int>(rhs));
115 }
116
117 //*************************************************************************
119 //*************************************************************************
120 inline ETL_CONSTEXPR14 etl::byte& operator |=(etl::byte& lhs, etl::byte rhs) noexcept
121 {
122 return lhs = lhs | rhs;
123 }
124
125 //*************************************************************************
127 //*************************************************************************
128 inline ETL_CONSTEXPR14 etl::byte& operator &=(etl::byte& lhs, etl::byte rhs) noexcept
129 {
130 return lhs = lhs & rhs;
131 }
132
133 //*************************************************************************
135 //*************************************************************************
136 inline ETL_CONSTEXPR14 etl::byte& operator ^=(etl::byte& lhs, etl::byte rhs) noexcept
137 {
138 return lhs = lhs ^ rhs;
139 }
140
141 //*************************************************************************
143 //*************************************************************************
144 inline constexpr etl::byte operator ~(etl::byte b) noexcept
145 {
146 return etl::byte(~static_cast<unsigned int>(b));
147 }
148
149#else
150
151 //*************************************************************************
153 //*************************************************************************
154 class byte
155 {
156 public:
157
158 // Friend functions
159 template <typename TInteger>
160 friend
163
164 friend bool operator ==(etl::byte lhs, etl::byte rhs);
165
166 // Default constructor
167 byte()
168 : value(0U)
169 {
170 }
171
172 // Construct from a value castable to unsigned char
173 template <typename T>
174 explicit byte(T v)
175 : value(static_cast<unsigned char>(v))
176 {
177 }
178
179 private:
180
181 // Cast to a T
182 template <typename T>
183 operator T() const
184 {
185 return static_cast<T>(value);
186 }
187
188 // The byte value
189 unsigned char value;
190 };
191
192 //*************************************************************************
194 //*************************************************************************
195 inline bool operator ==(etl::byte lhs, etl::byte rhs)
196 {
197 return (lhs.value == rhs.value);
198 }
199
200 //*************************************************************************
202 //*************************************************************************
203 inline bool operator !=(etl::byte lhs, etl::byte rhs)
204 {
205 return !(lhs == rhs);
206 }
207
208 //*************************************************************************
210 //*************************************************************************
211 template <typename TInteger>
214 {
215 return TInteger(b);
216 }
217
218 //*************************************************************************
220 //*************************************************************************
221 template <typename TInteger>
223 operator <<(etl::byte b, TInteger shift)
224 {
225 return etl::byte(to_integer<unsigned int>(b) << shift);
226 }
227
228 //*************************************************************************
230 //*************************************************************************
231 template <typename TInteger>
233 operator >>(etl::byte b, TInteger shift)
234 {
235 return etl::byte(to_integer<unsigned int>(b) >> shift);
236 }
237
238 //*************************************************************************
240 //*************************************************************************
241 template <typename TInteger>
243 operator <<=(etl::byte& b, TInteger shift)
244 {
245 b = b << shift;
246
247 return b;
248 }
249
250 //*************************************************************************
252 //*************************************************************************
253 template <typename TInteger>
255 operator >>=(etl::byte& b, TInteger shift)
256 {
257 b = b >> shift;
258
259 return b;
260 }
261
262 //*************************************************************************
264 //*************************************************************************
266 {
267 return etl::byte(to_integer<unsigned int>(lhs) | to_integer<unsigned int>(rhs));
268 }
269
270 //*************************************************************************
272 //*************************************************************************
274 {
275 return etl::byte(to_integer<unsigned int>(lhs) & to_integer<unsigned int>(rhs));
276 }
277
278 //*************************************************************************
280 //*************************************************************************
282 {
283 return etl::byte(to_integer<unsigned int>(lhs) ^ to_integer<unsigned int>(rhs));
284 }
285
286 //*************************************************************************
288 //*************************************************************************
290 {
291 return lhs = lhs | rhs;
292 }
293
294 //*************************************************************************
296 //*************************************************************************
298 {
299 return lhs = lhs & rhs;
300 }
301
302 //*************************************************************************
304 //*************************************************************************
306 {
307 return lhs = lhs ^ rhs;
308 }
309
310 //*************************************************************************
312 //*************************************************************************
314 {
315 return etl::byte(~to_integer<unsigned char>(b));
316 }
317
318#endif
319
320
321}
322
323#endif
The byte class.
Definition: byte.h:155
friend bool operator==(etl::byte lhs, etl::byte rhs)
Equality test.
Definition: byte.h:195
friend etl::enable_if< etl::is_integral< TInteger >::value, TInteger >::type to_integer(etl::byte b)
To integer.
Definition: byte.h:213
enable_if
Definition: type_traits_generator.h:1191
bitset_ext
Definition: absolute.h:38
etl::enable_if< etl::is_integral< TInteger >::value, etl::byte >::type operator<<(etl::byte b, TInteger shift)
Shift left.
Definition: byte.h:223
etl::byte operator~(etl::byte b)
Not.
Definition: byte.h:313
etl::byte & operator^=(etl::byte &lhs, etl::byte rhs)
Exclusive or equals.
Definition: byte.h:305
etl::enable_if< etl::is_integral< TInteger >::value, etl::byte & >::type operator<<=(etl::byte &b, TInteger shift)
Shift left equals.
Definition: byte.h:243
etl::byte operator|(etl::byte lhs, etl::byte rhs)
Or.
Definition: byte.h:265
etl::byte & operator|=(etl::byte &lhs, etl::byte rhs)
Or equals.
Definition: byte.h:289
etl::enable_if< etl::is_integral< TInteger >::value, TInteger >::type to_integer(etl::byte b)
To integer.
Definition: byte.h:213
etl::byte operator&(etl::byte lhs, etl::byte rhs)
And.
Definition: byte.h:273
bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:645
etl::enable_if< etl::is_integral< TInteger >::value, etl::byte >::type operator>>(etl::byte b, TInteger shift)
Shift right.
Definition: byte.h:233
etl::byte operator^(etl::byte lhs, etl::byte rhs)
Exclusive Or.
Definition: byte.h:281
bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:633
etl::enable_if< etl::is_integral< TInteger >::value, etl::byte & >::type operator>>=(etl::byte &b, TInteger shift)
Shift right equals.
Definition: byte.h:255
etl::byte & operator&=(etl::byte &lhs, etl::byte rhs)
And equals.
Definition: byte.h:297