1 // Copyright 2020 The Pigweed Authors
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
7 // https://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
18 #include "pw_polyfill/standard_library/namespace.h"
20 // Defines the std::byte type if it is not present.
21 #ifndef __cpp_lib_byte
22 #define __cpp_lib_byte 201603L
24 _PW_POLYFILL_BEGIN_NAMESPACE_STD
26 enum class byte : unsigned char {};
29 constexpr I to_integer(byte b) noexcept {
33 constexpr byte operator|(byte l, byte r) noexcept {
34 return byte(static_cast<unsigned int>(l) | static_cast<unsigned int>(r));
37 constexpr byte operator&(byte l, byte r) noexcept {
38 return byte(static_cast<unsigned int>(l) & static_cast<unsigned int>(r));
41 constexpr byte operator^(byte l, byte r) noexcept {
42 return byte(static_cast<unsigned int>(l) ^ static_cast<unsigned int>(r));
45 constexpr byte operator~(byte b) noexcept {
46 return byte(~static_cast<unsigned int>(b));
50 constexpr byte operator<<(byte b, I shift) noexcept {
51 return byte(static_cast<unsigned int>(b) << shift);
55 constexpr byte operator>>(byte b, I shift) noexcept {
56 return byte(static_cast<unsigned int>(b) >> shift);
59 #if __cpp_constexpr >= 201304L
61 constexpr byte& operator|=(byte& l, byte r) noexcept { return l = l | r; }
62 constexpr byte& operator&=(byte& l, byte r) noexcept { return l = l & r; }
63 constexpr byte& operator^=(byte& l, byte r) noexcept { return l = l ^ r; }
65 #else // For C++11 compatiblity, these operators cannot be constexpr.
67 inline byte& operator|=(byte& l, byte r) noexcept { return l = l | r; }
68 inline byte& operator&=(byte& l, byte r) noexcept { return l = l & r; }
69 inline byte& operator^=(byte& l, byte r) noexcept { return l = l ^ r; }
71 #endif // __cpp_constexpr >= 201304L
74 inline byte& operator<<=(byte& b, I shift) noexcept {
75 return b = b << shift;
79 inline byte& operator>>=(byte& b, I shift) noexcept {
80 return b = b >> shift;
83 _PW_POLYFILL_END_NAMESPACE_STD
85 #endif // __cpp_lib_byte