Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_polyfill / standard_library_public / pw_polyfill / standard_library / cstddef.h
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 <cstddef>
17
18 #include "pw_polyfill/standard_library/namespace.h"
19
20 // Defines the std::byte type if it is not present.
21 #ifndef __cpp_lib_byte
22 #define __cpp_lib_byte 201603L
23
24 _PW_POLYFILL_BEGIN_NAMESPACE_STD
25
26 enum class byte : unsigned char {};
27
28 template <typename I>
29 constexpr I to_integer(byte b) noexcept {
30   return I(b);
31 }
32
33 constexpr byte operator|(byte l, byte r) noexcept {
34   return byte(static_cast<unsigned int>(l) | static_cast<unsigned int>(r));
35 }
36
37 constexpr byte operator&(byte l, byte r) noexcept {
38   return byte(static_cast<unsigned int>(l) & static_cast<unsigned int>(r));
39 }
40
41 constexpr byte operator^(byte l, byte r) noexcept {
42   return byte(static_cast<unsigned int>(l) ^ static_cast<unsigned int>(r));
43 }
44
45 constexpr byte operator~(byte b) noexcept {
46   return byte(~static_cast<unsigned int>(b));
47 }
48
49 template <typename I>
50 constexpr byte operator<<(byte b, I shift) noexcept {
51   return byte(static_cast<unsigned int>(b) << shift);
52 }
53
54 template <typename I>
55 constexpr byte operator>>(byte b, I shift) noexcept {
56   return byte(static_cast<unsigned int>(b) >> shift);
57 }
58
59 #if __cpp_constexpr >= 201304L
60
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; }
64
65 #else  // For C++11 compatiblity, these operators cannot be constexpr.
66
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; }
70
71 #endif  // __cpp_constexpr >= 201304L
72
73 template <typename I>
74 inline byte& operator<<=(byte& b, I shift) noexcept {
75   return b = b << shift;
76 }
77
78 template <typename I>
79 inline byte& operator>>=(byte& b, I shift) noexcept {
80   return b = b >> shift;
81 }
82
83 _PW_POLYFILL_END_NAMESPACE_STD
84
85 #endif  // __cpp_lib_byte