VCCC  2024.05
VisualCamp Common C++ library
byte.hpp
Go to the documentation of this file.
1 //
2 // Created by yonggyulee on 2023/12/30.
3 //
4 
5 #ifndef VCCC_CORE_BYTE_HPP
6 #define VCCC_CORE_BYTE_HPP
7 
8 #include <cassert>
9 #include <cstddef>
10 #include <initializer_list>
11 #include <type_traits>
12 
13 // #include "vccc/__type_traits/"
14 
15 namespace vccc {
16 
19 
20 // TODO: Use CheckCXXTypeExists
21 #if __cplusplus < 201703L
22 
23 namespace detail {
24 struct byte_ctor_tag {};
25 }
26 
27 class byte {
28  public:
29  byte() noexcept = default;
30 
31  constexpr explicit byte(std::initializer_list<unsigned char> il)
32  : value_(*il.begin()) {
33  assert(((void)"Size must be one", il.size() == 1));
34  }
35 
36  // Internal use only
37  constexpr explicit byte(unsigned char c, detail::byte_ctor_tag) noexcept : value_(c) {}
38 
39  constexpr explicit operator unsigned char() const noexcept {
40  return value_;
41  }
42 
43  private:
44  unsigned char value_;
45 };
46 
47 template<typename IntegerType>
49 to_integer(byte b) noexcept {
50  return IntegerType(b.operator unsigned char());
51 }
52 
53 template <class IntegerType>
55 operator<<(byte b, IntegerType shift) noexcept {
56  return byte(
57  static_cast<unsigned char>(
58  static_cast<unsigned int>(b.operator unsigned char()) << shift
59  ),
60  detail::byte_ctor_tag{}
61  );
62 }
63 
64 template <class IntegerType>
66 operator>>(byte b, IntegerType shift) noexcept {
67  return byte(
68  static_cast<unsigned char>(
69  static_cast<unsigned int>(b.operator unsigned char()) >> shift
70  ),
71  detail::byte_ctor_tag{}
72  );
73 }
74 
75 template <class IntegerType>
77 operator<<=(byte& b, IntegerType shift) noexcept {
78  return b = b << shift;
79 }
80 
81 template <class IntegerType>
83 operator>>=(byte& b, IntegerType shift) noexcept {
84  return b = b >> shift;
85 }
86 
87 constexpr byte operator|(byte l, byte r) noexcept {
88  return byte(
89  static_cast<unsigned char>(
90  static_cast<unsigned int>(l.operator unsigned char()) | static_cast<unsigned int>(r.operator unsigned char())
91  ),
92  detail::byte_ctor_tag{}
93  );
94 }
95 
96 constexpr byte operator&(byte l, byte r) noexcept {
97  return byte(
98  static_cast<unsigned char>(
99  static_cast<unsigned int>(l.operator unsigned char()) & static_cast<unsigned int>(r.operator unsigned char())
100  ),
101  detail::byte_ctor_tag{}
102  );
103 }
104 
105 constexpr byte operator^(byte l, byte r) noexcept {
106  return byte(
107  static_cast<unsigned char>(
108  static_cast<unsigned int>(l.operator unsigned char()) ^ static_cast<unsigned int>(r.operator unsigned char())
109  ),
110  detail::byte_ctor_tag{}
111  );
112 }
113 constexpr byte operator~(byte b) noexcept {
114  return byte(
115  static_cast<unsigned char>(
116  ~static_cast<unsigned int>(b.operator unsigned char())
117  ),
118  detail::byte_ctor_tag{}
119  );
120 }
121 
122 constexpr byte& operator|=(byte& l, byte r) noexcept {
123  return l = l | r;
124 }
125 
126 constexpr byte& operator&=(byte& l, byte r) noexcept {
127  return l = l & r;
128 }
129 
130 constexpr byte& operator^=(byte& l, byte r) noexcept {
131  return l = l ^ r;
132 }
133 #else
134 using byte = std::byte;
135 
136 template<typename IntegerType>
138 to_integer(byte b) noexcept {
139  return IntegerType(b);
140 }
141 
142 #endif
143 
145 
146 } // namespace vccc
147 
148 #endif // VCCC_CORE_BYTE_HPP
Definition: byte.hpp:27
constexpr byte(unsigned char c, detail::byte_ctor_tag) noexcept
Definition: byte.hpp:37
byte() noexcept=default
constexpr std::enable_if_t< std::is_integral< IntegerType >::value, byte & > operator<<=(byte &b, IntegerType shift) noexcept
Definition: byte.hpp:77
constexpr byte & operator&=(byte &l, byte r) noexcept
Definition: byte.hpp:126
constexpr std::enable_if_t< std::is_integral< IntegerType >::value, byte > operator>>(byte b, IntegerType shift) noexcept
Definition: byte.hpp:66
constexpr byte operator^(byte l, byte r) noexcept
Definition: byte.hpp:105
constexpr byte operator~(byte b) noexcept
Definition: byte.hpp:113
constexpr std::enable_if_t< std::is_integral< IntegerType >::value, IntegerType > to_integer(byte b) noexcept
Definition: byte.hpp:49
constexpr std::enable_if_t< std::is_integral< IntegerType >::value, byte & > operator>>=(byte &b, IntegerType shift) noexcept
Definition: byte.hpp:83
constexpr byte & operator|=(byte &l, byte r) noexcept
Definition: byte.hpp:122
constexpr byte operator&(byte l, byte r) noexcept
Definition: byte.hpp:96
constexpr byte & operator^=(byte &l, byte r) noexcept
Definition: byte.hpp:130
constexpr byte operator|(byte l, byte r) noexcept
Definition: byte.hpp:87
std::ostream & operator<<(std::ostream &os, const MatrixBase< E > &mat_expr)
Definition: matrix_ostream.hpp:17
constexpr VCCC_INLINE_OR_STATIC detail::begin_niebloid begin
returns an iterator to the beginning of a range
Definition: begin.hpp:116
Definition: matrix.hpp:495
Definition: directory.h:12
constexpr VCCC_INLINE_OR_STATIC detail::element_niebloid< 1 > value
Definition: key_value.hpp:35