blob: c6fd87a5b09a9442d6042cee2342d7da973db7e9 [file] [log] [blame]
Wyatt Hepler0412a7d2020-01-28 16:27:32 -08001// 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
Wyatt Hepler023f35b2020-07-01 09:40:50 -070018#include "pw_polyfill/standard_library/namespace.h"
19
20_PW_POLYFILL_BEGIN_NAMESPACE_STD
Wyatt Hepler0412a7d2020-01-28 16:27:32 -080021
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
Wyatt Hepler023f35b2020-07-01 09:40:50 -070076_PW_POLYFILL_END_NAMESPACE_STD