My Project
byte.hpp
Go to the documentation of this file.
1 
28 #ifndef BSL_BYTE_HPP
29 #define BSL_BYTE_HPP
30 
31 #include "cstdint.hpp"
32 #include "move.hpp"
33 
34 #include "enable_if.hpp"
35 #include "is_integral.hpp"
36 #include "is_same.hpp"
37 #include "is_unsigned.hpp"
38 
39 namespace bsl
40 {
49  class byte final
50  {
51  public:
54 
63  byte() noexcept = default;
64 
80  constexpr byte(value_type const val) noexcept // PRQA S 2180 // NOLINT
81  : m_data{val}
82  {}
83 
94  template<typename T = value_type, enable_if_t<is_integral<T>::value, bool> = true>
95  [[nodiscard]] constexpr T
96  to_integer() const noexcept
97  {
98  return static_cast<T>(m_data);
99  }
100 
104  ~byte() noexcept = default;
105 
112  constexpr byte(byte const &o) noexcept = default;
113 
120  constexpr byte(byte &&o) noexcept = default;
121 
129  constexpr byte &operator=(byte const &o) &noexcept = default;
130 
138  constexpr byte &operator=(byte &&o) &noexcept = default;
139 
155  template<typename O>
156  byte(O val) noexcept = delete; // PRQA S 2180
157 
158  private:
160  value_type m_data;
161  };
162 
173  constexpr bool
174  operator==(byte const &lhs, byte const &rhs) noexcept
175  {
176  return lhs.to_integer() == rhs.to_integer();
177  }
178 
189  constexpr bool
190  operator!=(byte const &lhs, byte const &rhs) noexcept
191  {
192  return !(lhs == rhs);
193  }
194 
205  constexpr byte &
206  operator<<=(byte &b, bsl::uint32 const shift) noexcept
207  {
208  b = byte{static_cast<bsl::uint8>(b.to_integer<bsl::uint32>() << shift)};
209  return b;
210  }
211 
222  constexpr byte &
223  operator>>=(byte &b, bsl::uint32 const shift) noexcept
224  {
225  b = byte{static_cast<bsl::uint8>(b.to_integer<bsl::uint32>() >> shift)};
226  return b;
227  }
228 
239  constexpr byte
240  operator<<(byte const &b, bsl::uint32 const shift) noexcept
241  {
242  byte tmp{b};
243  tmp <<= shift;
244  return tmp;
245  }
246 
257  constexpr byte
258  operator>>(byte const &b, bsl::uint32 const shift) noexcept
259  {
260  byte tmp{b};
261  tmp >>= shift;
262  return tmp;
263  }
264 
275  constexpr byte &
276  operator|=(byte &lhs, byte const &rhs) noexcept
277  {
278  auto const lhs32{lhs.to_integer<bsl::uint32>()};
279  auto const rhs32{rhs.to_integer<bsl::uint32>()};
280 
281  lhs = byte{static_cast<bsl::uint8>(lhs32 | rhs32)};
282  return lhs;
283  }
284 
295  constexpr byte &
296  operator&=(byte &lhs, byte const &rhs) noexcept
297  {
298  auto const lhs32{lhs.to_integer<bsl::uint32>()};
299  auto const rhs32{rhs.to_integer<bsl::uint32>()};
300 
301  lhs = byte{static_cast<bsl::uint8>(lhs32 & rhs32)};
302  return lhs;
303  }
304 
315  constexpr byte &
316  operator^=(byte &lhs, byte const &rhs) noexcept
317  {
318  auto const lhs32{lhs.to_integer<bsl::uint32>()};
319  auto const rhs32{rhs.to_integer<bsl::uint32>()};
320 
321  lhs = byte{static_cast<bsl::uint8>(lhs32 ^ rhs32)};
322  return lhs;
323  }
324 
335  constexpr byte
336  operator|(byte const &lhs, byte const &rhs) noexcept
337  {
338  byte tmp{lhs};
339  tmp |= rhs;
340  return tmp;
341  }
342 
353  constexpr byte
354  operator&(byte const &lhs, byte const &rhs) noexcept
355  {
356  byte tmp{lhs};
357  tmp &= rhs;
358  return tmp;
359  }
360 
371  constexpr byte
372  operator^(byte const &lhs, byte const &rhs) noexcept
373  {
374  byte tmp{lhs};
375  tmp ^= rhs;
376  return tmp;
377  }
378 
388  constexpr byte
389  operator~(byte const &b) noexcept
390  {
391  return byte{static_cast<bsl::uint8>(~b.to_integer<bsl::uint32>())};
392  }
393 }
394 
395 #endif
~byte() noexcept=default
Destroyes a previously created bsl::byte.
::uint8_t uint8
defines an 8bit unsigned integer
Definition: cstdint.hpp:45
byte() noexcept=default
Default constructor. This ensures the byte type is a POD type, allowing it to be constructed as a glo...
constexpr bool operator!=(byte const &lhs, byte const &rhs) noexcept
The same as !(lhs == rhs)
Definition: byte.hpp:190
constexpr byte & operator&=(byte &lhs, byte const &rhs) noexcept
The same as lhs = byte{lhs.to_integer() & rhs.to_integer()};.
Definition: byte.hpp:296
constexpr byte operator>>(byte const &b, bsl::uint32 const shift) noexcept
The same as byte tmp{b}; tmp >>= shift;.
Definition: byte.hpp:258
constexpr byte operator^(byte const &lhs, byte const &rhs) noexcept
The same as tmp{lhs}; tmp ^= rhs;.
Definition: byte.hpp:372
::uint32_t uint32
defines an 32bit unsigned integer
Definition: cstdint.hpp:49
constexpr T to_integer() const noexcept
Returns the bsl::byte as a given integer type using a static_cast to perform the conversion.
Definition: byte.hpp:96
constexpr byte operator~(byte const &b) noexcept
The same as byte{~b.to_integer()}.
Definition: byte.hpp:389
constexpr byte operator|(byte const &lhs, byte const &rhs) noexcept
The same as tmp{lhs}; tmp |= rhs;.
Definition: byte.hpp:336
constexpr byte operator<<(byte const &b, bsl::uint32 const shift) noexcept
The same as byte tmp{b}; tmp <<= shift;.
Definition: byte.hpp:240
constexpr byte & operator^=(byte &lhs, byte const &rhs) noexcept
The same as lhs = byte{lhs.to_integer() ^ rhs.to_integer()};.
Definition: byte.hpp:316
constexpr byte operator &(byte const &lhs, byte const &rhs) noexcept
The same as tmp{lhs}; tmp &= rhs;.
Definition: byte.hpp:354
constexpr byte & operator|=(byte &lhs, byte const &rhs) noexcept
The same as lhs = byte{lhs.to_integer() | rhs.to_integer()};.
Definition: byte.hpp:276
constexpr byte & operator<<=(byte &b, bsl::uint32 const shift) noexcept
The same as b = byte{b.to_integer() << shift}.
Definition: byte.hpp:206
constexpr byte & operator>>=(byte &b, bsl::uint32 const shift) noexcept
The same as b = byte{b.to_integer() >> shift}.
Definition: byte.hpp:223
bsl::uint8 value_type
alias for: T
Definition: byte.hpp:53
std::byte is a distinct type that implements the concept of byte as specified in the C++ language def...
Definition: byte.hpp:49