cstddef 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_CSTDDEF
  10. #define _LIBCPP_CSTDDEF
  11. /*
  12. cstddef synopsis
  13. Macros:
  14. offsetof(type,member-designator)
  15. NULL
  16. namespace std
  17. {
  18. Types:
  19. ptrdiff_t
  20. size_t
  21. max_align_t // C++11
  22. nullptr_t
  23. byte // C++17
  24. } // std
  25. */
  26. #include <__config>
  27. #include <stddef.h>
  28. #include <version>
  29. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  30. # pragma GCC system_header
  31. #endif
  32. #ifdef _LIBCPP_ABI_VCRUNTIME
  33. typedef double max_align_t;
  34. #endif
  35. _LIBCPP_BEGIN_NAMESPACE_STD
  36. using ::nullptr_t;
  37. using ::ptrdiff_t _LIBCPP_USING_IF_EXISTS;
  38. using ::size_t _LIBCPP_USING_IF_EXISTS;
  39. #if !defined(_LIBCPP_CXX03_LANG)
  40. using ::max_align_t _LIBCPP_USING_IF_EXISTS;
  41. #endif
  42. template <class _Tp> struct __libcpp_is_integral { enum { value = 0 }; };
  43. template <> struct __libcpp_is_integral<bool> { enum { value = 1 }; };
  44. template <> struct __libcpp_is_integral<char> { enum { value = 1 }; };
  45. template <> struct __libcpp_is_integral<signed char> { enum { value = 1 }; };
  46. template <> struct __libcpp_is_integral<unsigned char> { enum { value = 1 }; };
  47. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  48. template <> struct __libcpp_is_integral<wchar_t> { enum { value = 1 }; };
  49. #endif
  50. #ifndef _LIBCPP_HAS_NO_CHAR8_T
  51. template <> struct __libcpp_is_integral<char8_t> { enum { value = 1 }; };
  52. #endif
  53. #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
  54. template <> struct __libcpp_is_integral<char16_t> { enum { value = 1 }; };
  55. template <> struct __libcpp_is_integral<char32_t> { enum { value = 1 }; };
  56. #endif
  57. template <> struct __libcpp_is_integral<short> { enum { value = 1 }; };
  58. template <> struct __libcpp_is_integral<unsigned short> { enum { value = 1 }; };
  59. template <> struct __libcpp_is_integral<int> { enum { value = 1 }; };
  60. template <> struct __libcpp_is_integral<unsigned int> { enum { value = 1 }; };
  61. template <> struct __libcpp_is_integral<long> { enum { value = 1 }; };
  62. template <> struct __libcpp_is_integral<unsigned long> { enum { value = 1 }; };
  63. template <> struct __libcpp_is_integral<long long> { enum { value = 1 }; };
  64. template <> struct __libcpp_is_integral<unsigned long long> { enum { value = 1 }; };
  65. #ifndef _LIBCPP_HAS_NO_INT128
  66. template <> struct __libcpp_is_integral<__int128_t> { enum { value = 1 }; };
  67. template <> struct __libcpp_is_integral<__uint128_t> { enum { value = 1 }; };
  68. #endif
  69. _LIBCPP_END_NAMESPACE_STD
  70. #if _LIBCPP_STD_VER > 14
  71. namespace std // purposefully not versioned
  72. {
  73. enum class byte : unsigned char {};
  74. template <bool> struct __enable_if_integral_imp {};
  75. template <> struct __enable_if_integral_imp<true> { using type = byte; };
  76. template <class _Tp> using _EnableByteOverload = typename __enable_if_integral_imp<__libcpp_is_integral<_Tp>::value>::type;
  77. constexpr byte operator| (byte __lhs, byte __rhs) noexcept
  78. {
  79. return static_cast<byte>(
  80. static_cast<unsigned char>(
  81. static_cast<unsigned int>(__lhs) | static_cast<unsigned int>(__rhs)
  82. ));
  83. }
  84. constexpr byte& operator|=(byte& __lhs, byte __rhs) noexcept
  85. { return __lhs = __lhs | __rhs; }
  86. constexpr byte operator& (byte __lhs, byte __rhs) noexcept
  87. {
  88. return static_cast<byte>(
  89. static_cast<unsigned char>(
  90. static_cast<unsigned int>(__lhs) & static_cast<unsigned int>(__rhs)
  91. ));
  92. }
  93. constexpr byte& operator&=(byte& __lhs, byte __rhs) noexcept
  94. { return __lhs = __lhs & __rhs; }
  95. constexpr byte operator^ (byte __lhs, byte __rhs) noexcept
  96. {
  97. return static_cast<byte>(
  98. static_cast<unsigned char>(
  99. static_cast<unsigned int>(__lhs) ^ static_cast<unsigned int>(__rhs)
  100. ));
  101. }
  102. constexpr byte& operator^=(byte& __lhs, byte __rhs) noexcept
  103. { return __lhs = __lhs ^ __rhs; }
  104. constexpr byte operator~ (byte __b) noexcept
  105. {
  106. return static_cast<byte>(
  107. static_cast<unsigned char>(
  108. ~static_cast<unsigned int>(__b)
  109. ));
  110. }
  111. template <class _Integer>
  112. constexpr _EnableByteOverload<_Integer> &
  113. operator<<=(byte& __lhs, _Integer __shift) noexcept
  114. { return __lhs = __lhs << __shift; }
  115. template <class _Integer>
  116. constexpr _EnableByteOverload<_Integer>
  117. operator<< (byte __lhs, _Integer __shift) noexcept
  118. { return static_cast<byte>(static_cast<unsigned char>(static_cast<unsigned int>(__lhs) << __shift)); }
  119. template <class _Integer>
  120. constexpr _EnableByteOverload<_Integer> &
  121. operator>>=(byte& __lhs, _Integer __shift) noexcept
  122. { return __lhs = __lhs >> __shift; }
  123. template <class _Integer>
  124. constexpr _EnableByteOverload<_Integer>
  125. operator>> (byte __lhs, _Integer __shift) noexcept
  126. { return static_cast<byte>(static_cast<unsigned char>(static_cast<unsigned int>(__lhs) >> __shift)); }
  127. template <class _Integer, class = _EnableByteOverload<_Integer> >
  128. _LIBCPP_NODISCARD_EXT constexpr _Integer
  129. to_integer(byte __b) noexcept { return static_cast<_Integer>(__b); }
  130. } // namespace std
  131. #endif
  132. #endif // _LIBCPP_CSTDDEF