Embedded Template Library 1.0
Loading...
Searching...
No Matches
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 };
39
40 //*************************************************************************
42 //*************************************************************************
43 template <typename TInteger>
44 constexpr typename etl::enable_if<etl::is_integral<TInteger>::value, TInteger>::type to_integer(etl::byte b) ETL_NOEXCEPT
45 {
46 return TInteger(b);
47 }
48
49 //*************************************************************************
51 //*************************************************************************
52 template <typename TInteger>
53 constexpr typename etl::enable_if<etl::is_integral<TInteger>::value, etl::byte>::type operator<<(etl::byte b, TInteger shift) ETL_NOEXCEPT
54 {
55 return etl::byte(static_cast<unsigned int>(b) << shift);
56 }
57
58 //*************************************************************************
60 //*************************************************************************
61 template <typename TInteger>
62 constexpr typename etl::enable_if<etl::is_integral<TInteger>::value, etl::byte>::type operator>>(etl::byte b, TInteger shift) ETL_NOEXCEPT
63 {
64 return etl::byte(static_cast<unsigned int>(b) >> shift);
65 }
66
67 //*************************************************************************
69 //*************************************************************************
70 template <typename TInteger>
71 constexpr typename etl::enable_if<etl::is_integral<TInteger>::value, etl::byte&>::type operator<<=(etl::byte& b, TInteger shift) ETL_NOEXCEPT
72 {
73 return b = b << shift;
74 }
75
76 //*************************************************************************
78 //*************************************************************************
79 template <typename TInteger>
80 constexpr typename etl::enable_if<etl::is_integral<TInteger>::value, etl::byte&>::type operator>>=(etl::byte& b, TInteger shift) ETL_NOEXCEPT
81 {
82 return b = b >> shift;
83 }
84
85 //*************************************************************************
87 //*************************************************************************
88 inline constexpr etl::byte operator|(etl::byte lhs, etl::byte rhs) ETL_NOEXCEPT
89 {
90 return etl::byte(static_cast<unsigned int>(lhs) | static_cast<unsigned int>(rhs));
91 }
92
93 //*************************************************************************
95 //*************************************************************************
96 inline constexpr etl::byte operator&(etl::byte lhs, etl::byte rhs) ETL_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) ETL_NOEXCEPT
105 {
106 return etl::byte(static_cast<unsigned int>(lhs) ^ static_cast<unsigned int>(rhs));
107 }
108
109 //*************************************************************************
111 //*************************************************************************
112 inline ETL_CONSTEXPR14 etl::byte& operator|=(etl::byte& lhs, etl::byte rhs) ETL_NOEXCEPT
113 {
114 return lhs = lhs | rhs;
115 }
116
117 //*************************************************************************
119 //*************************************************************************
120 inline ETL_CONSTEXPR14 etl::byte& operator&=(etl::byte& lhs, etl::byte rhs) ETL_NOEXCEPT
121 {
122 return lhs = lhs & rhs;
123 }
124
125 //*************************************************************************
127 //*************************************************************************
128 inline ETL_CONSTEXPR14 etl::byte& operator^=(etl::byte& lhs, etl::byte rhs) ETL_NOEXCEPT
129 {
130 return lhs = lhs ^ rhs;
131 }
132
133 //*************************************************************************
135 //*************************************************************************
136 inline constexpr etl::byte operator~(etl::byte b) ETL_NOEXCEPT
137 {
138 return etl::byte(~static_cast<unsigned int>(b));
139 }
140
141#else
142
143 //*************************************************************************
145 //*************************************************************************
146 class byte
147 {
148 public:
149
150 // Friend functions
151 template <typename TInteger>
152 friend typename etl::enable_if<etl::is_integral<TInteger>::value, TInteger>::type to_integer(etl::byte b);
153
154 friend bool operator==(etl::byte lhs, etl::byte rhs);
155
156 // Default constructor
157 byte()
158 : value(0U)
159 {
160 }
161
162 // Construct from a value castable to unsigned char
163 template <typename T>
164 explicit byte(T v)
165 : value(static_cast<unsigned char>(v))
166 {
167 }
168
169 // Cast to a T
170 template <typename T>
171 operator T() const
172 {
173 return static_cast<T>(value);
174 }
175
176 private:
177
178 // The byte value
179 unsigned char value;
180 };
181
182 //*************************************************************************
184 //*************************************************************************
185 inline bool operator==(etl::byte lhs, etl::byte rhs)
186 {
187 return (lhs.value == rhs.value);
188 }
189
190 //*************************************************************************
192 //*************************************************************************
193 inline bool operator!=(etl::byte lhs, etl::byte rhs)
194 {
195 return !(lhs == rhs);
196 }
197
198 //*************************************************************************
200 //*************************************************************************
201 template <typename TInteger>
202 typename etl::enable_if<etl::is_integral<TInteger>::value, TInteger>::type to_integer(etl::byte b)
203 {
204 return TInteger(b);
205 }
206
207 //*************************************************************************
209 //*************************************************************************
210 template <typename TInteger>
211 typename etl::enable_if<etl::is_integral<TInteger>::value, etl::byte>::type operator<<(etl::byte b, TInteger shift)
212 {
213 return etl::byte(to_integer<unsigned int>(b) << shift);
214 }
215
216 //*************************************************************************
218 //*************************************************************************
219 template <typename TInteger>
220 typename etl::enable_if<etl::is_integral<TInteger>::value, etl::byte>::type operator>>(etl::byte b, TInteger shift)
221 {
222 return etl::byte(to_integer<unsigned int>(b) >> shift);
223 }
224
225 //*************************************************************************
227 //*************************************************************************
228 template <typename TInteger>
229 typename etl::enable_if<etl::is_integral<TInteger>::value, etl::byte&>::type operator<<=(etl::byte& b, TInteger shift)
230 {
231 b = b << shift;
232
233 return b;
234 }
235
236 //*************************************************************************
238 //*************************************************************************
239 template <typename TInteger>
240 typename etl::enable_if<etl::is_integral<TInteger>::value, etl::byte&>::type operator>>=(etl::byte& b, TInteger shift)
241 {
242 b = b >> shift;
243
244 return b;
245 }
246
247 //*************************************************************************
249 //*************************************************************************
254
255 //*************************************************************************
257 //*************************************************************************
262
263 //*************************************************************************
265 //*************************************************************************
270
271 //*************************************************************************
273 //*************************************************************************
275 {
276 return lhs = lhs | rhs;
277 }
278
279 //*************************************************************************
281 //*************************************************************************
283 {
284 return lhs = lhs & rhs;
285 }
286
287 //*************************************************************************
289 //*************************************************************************
291 {
292 return lhs = lhs ^ rhs;
293 }
294
295 //*************************************************************************
297 //*************************************************************************
299 {
301 }
302
303#endif
304
305} // namespace etl
306
307#endif
The byte class.
Definition byte.h:147
friend bool operator==(etl::byte lhs, etl::byte rhs)
Equality test.
Definition byte.h:185
friend etl::enable_if< etl::is_integral< TInteger >::value, TInteger >::type to_integer(etl::byte b)
To integer.
Definition byte.h:202
bitset_ext
Definition absolute.h:40
ETL_CONSTEXPR14 bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1081
std::basic_ostream< T, std::char_traits< T > > & operator<<(std::basic_ostream< T, std::char_traits< T > > &os, const etl::ibasic_string< T > &str)
Definition basic_string.h:3213
etl::byte operator~(etl::byte b)
Not.
Definition byte.h:298
etl::byte & operator^=(etl::byte &lhs, etl::byte rhs)
Exclusive or equals.
Definition byte.h:290
etl::enable_if< etl::is_integral< TInteger >::value, etl::byte & >::type operator<<=(etl::byte &b, TInteger shift)
Shift left equals.
Definition byte.h:229
etl::byte operator|(etl::byte lhs, etl::byte rhs)
Or.
Definition byte.h:250
etl::byte & operator|=(etl::byte &lhs, etl::byte rhs)
Or equals.
Definition byte.h:274
etl::enable_if< etl::is_integral< TInteger >::value, TInteger >::type to_integer(etl::byte b)
To integer.
Definition byte.h:202
etl::byte operator&(etl::byte lhs, etl::byte rhs)
And.
Definition byte.h:258
etl::enable_if< etl::is_integral< TInteger >::value, etl::byte >::type operator>>(etl::byte b, TInteger shift)
Shift right.
Definition byte.h:220
ETL_CONSTEXPR14 bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1093
etl::byte operator^(etl::byte lhs, etl::byte rhs)
Exclusive Or.
Definition byte.h:266
etl::enable_if< etl::is_integral< TInteger >::value, etl::byte & >::type operator>>=(etl::byte &b, TInteger shift)
Shift right equals.
Definition byte.h:240
etl::byte & operator&=(etl::byte &lhs, etl::byte rhs)
And equals.
Definition byte.h:282