1// Copyright 2020 The Pigweed Authors
2//
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
5// the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
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
13// the License.
14#pragma once
15
16#include <stddef.h>
17
18#include "pw_polyfill/standard_library/namespace.h"
19
20_PW_POLYFILL_BEGIN_NAMESPACE_STD
21
22using ::ptrdiff_t;
23using ::size_t;
24using nullptr_t = decltype(nullptr);
25using ::max_align_t;
26
27#define __cpp_lib_byte 201603L
28
29enum class byte : unsigned char {};
30
31template <typename I>
32constexpr I to_integer(byte b) noexcept {
33  return I(b);
34}
35
36constexpr byte operator|(byte l, byte r) noexcept {
37  return byte(static_cast<unsigned int>(l) | static_cast<unsigned int>(r));
38}
39
40constexpr byte operator&(byte l, byte r) noexcept {
41  return byte(static_cast<unsigned int>(l) & static_cast<unsigned int>(r));
42}
43
44constexpr byte operator^(byte l, byte r) noexcept {
45  return byte(static_cast<unsigned int>(l) ^ static_cast<unsigned int>(r));
46}
47
48constexpr byte operator~(byte b) noexcept {
49  return byte(~static_cast<unsigned int>(b));
50}
51
52template <typename I>
53constexpr byte operator<<(byte b, I shift) noexcept {
54  return byte(static_cast<unsigned int>(b) << shift);
55}
56
57template <typename I>
58constexpr byte operator>>(byte b, I shift) noexcept {
59  return byte(static_cast<unsigned int>(b) >> shift);
60}
61
62constexpr byte& operator|=(byte& l, byte r) noexcept { return l = l | r; }
63constexpr byte& operator&=(byte& l, byte r) noexcept { return l = l & r; }
64constexpr byte& operator^=(byte& l, byte r) noexcept { return l = l ^ r; }
65
66template <typename I>
67inline byte& operator<<=(byte& b, I shift) noexcept {
68  return b = b << shift;
69}
70
71template <typename I>
72inline byte& operator>>=(byte& b, I shift) noexcept {
73  return b = b >> shift;
74}
75
76_PW_POLYFILL_END_NAMESPACE_STD
77