1//===-- A self contained equivalent of cstddef ------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_LIBC_SRC___SUPPORT_CPP_CSTDDEF_H
10#define LLVM_LIBC_SRC___SUPPORT_CPP_CSTDDEF_H
11
12#include "src/__support/macros/attributes.h"
13#include "type_traits.h" // For enable_if_t, is_integral_v.
14
15namespace LIBC_NAMESPACE::cpp {
16
17enum class byte : unsigned char {};
18
19template <class IntegerType>
20LIBC_INLINE constexpr enable_if_t<is_integral_v<IntegerType>, byte>
21operator>>(byte b, IntegerType shift) noexcept {
22 return static_cast<byte>(static_cast<unsigned char>(b) >> shift);
23}
24template <class IntegerType>
25LIBC_INLINE constexpr enable_if_t<is_integral_v<IntegerType>, byte &>
26operator>>=(byte &b, IntegerType shift) noexcept {
27 return b = b >> shift;
28}
29template <class IntegerType>
30LIBC_INLINE constexpr enable_if_t<is_integral_v<IntegerType>, byte>
31operator<<(byte b, IntegerType shift) noexcept {
32 return static_cast<byte>(static_cast<unsigned char>(b) << shift);
33}
34template <class IntegerType>
35LIBC_INLINE constexpr enable_if_t<is_integral_v<IntegerType>, byte &>
36operator<<=(byte &b, IntegerType shift) noexcept {
37 return b = b << shift;
38}
39LIBC_INLINE constexpr byte operator|(byte l, byte r) noexcept {
40 return static_cast<byte>(static_cast<unsigned char>(l) |
41 static_cast<unsigned char>(r));
42}
43LIBC_INLINE constexpr byte &operator|=(byte &l, byte r) noexcept {
44 return l = l | r;
45}
46LIBC_INLINE constexpr byte operator&(byte l, byte r) noexcept {
47 return static_cast<byte>(static_cast<unsigned char>(l) &
48 static_cast<unsigned char>(r));
49}
50LIBC_INLINE constexpr byte &operator&=(byte &l, byte r) noexcept {
51 return l = l & r;
52}
53LIBC_INLINE constexpr byte operator^(byte l, byte r) noexcept {
54 return static_cast<byte>(static_cast<unsigned char>(l) ^
55 static_cast<unsigned char>(r));
56}
57LIBC_INLINE constexpr byte &operator^=(byte &l, byte r) noexcept {
58 return l = l ^ r;
59}
60LIBC_INLINE constexpr byte operator~(byte b) noexcept {
61 return static_cast<byte>(~static_cast<unsigned char>(b));
62}
63template <typename IntegerType>
64LIBC_INLINE constexpr enable_if_t<is_integral_v<IntegerType>, IntegerType>
65to_integer(byte b) noexcept {
66 return static_cast<IntegerType>(b);
67}
68
69} // namespace LIBC_NAMESPACE::cpp
70
71#endif // LLVM_LIBC_SRC___SUPPORT_CPP_CSTDDEF_H
72

source code of libc/src/__support/CPP/cstddef.h