cstddef 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 <__assert> // all public C++ headers provide the assertion handler
  27. #include <__config>
  28. #include <__type_traits/enable_if.h>
  29. #include <__type_traits/integral_constant.h>
  30. #include <__type_traits/is_integral.h>
  31. #include <stddef.h>
  32. #include <version>
  33. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  34. # pragma GCC system_header
  35. #endif
  36. #ifdef _LIBCPP_ABI_VCRUNTIME
  37. typedef double max_align_t;
  38. #endif
  39. _LIBCPP_BEGIN_NAMESPACE_STD
  40. using ::nullptr_t;
  41. using ::ptrdiff_t _LIBCPP_USING_IF_EXISTS;
  42. using ::size_t _LIBCPP_USING_IF_EXISTS;
  43. #if !defined(_LIBCPP_CXX03_LANG)
  44. using ::max_align_t _LIBCPP_USING_IF_EXISTS;
  45. #endif
  46. _LIBCPP_END_NAMESPACE_STD
  47. #if _LIBCPP_STD_VER > 14
  48. namespace std // purposefully not versioned
  49. {
  50. enum class byte : unsigned char {};
  51. constexpr byte operator| (byte __lhs, byte __rhs) noexcept
  52. {
  53. return static_cast<byte>(
  54. static_cast<unsigned char>(
  55. static_cast<unsigned int>(__lhs) | static_cast<unsigned int>(__rhs)
  56. ));
  57. }
  58. constexpr byte& operator|=(byte& __lhs, byte __rhs) noexcept
  59. { return __lhs = __lhs | __rhs; }
  60. constexpr byte operator& (byte __lhs, byte __rhs) noexcept
  61. {
  62. return static_cast<byte>(
  63. static_cast<unsigned char>(
  64. static_cast<unsigned int>(__lhs) & static_cast<unsigned int>(__rhs)
  65. ));
  66. }
  67. constexpr byte& operator&=(byte& __lhs, byte __rhs) noexcept
  68. { return __lhs = __lhs & __rhs; }
  69. constexpr byte operator^ (byte __lhs, byte __rhs) noexcept
  70. {
  71. return static_cast<byte>(
  72. static_cast<unsigned char>(
  73. static_cast<unsigned int>(__lhs) ^ static_cast<unsigned int>(__rhs)
  74. ));
  75. }
  76. constexpr byte& operator^=(byte& __lhs, byte __rhs) noexcept
  77. { return __lhs = __lhs ^ __rhs; }
  78. constexpr byte operator~ (byte __b) noexcept
  79. {
  80. return static_cast<byte>(
  81. static_cast<unsigned char>(
  82. ~static_cast<unsigned int>(__b)
  83. ));
  84. }
  85. template <class _Tp>
  86. using _EnableByteOverload = __enable_if_t<is_integral<_Tp>::value, byte>;
  87. template <class _Integer>
  88. constexpr _EnableByteOverload<_Integer> &
  89. operator<<=(byte& __lhs, _Integer __shift) noexcept
  90. { return __lhs = __lhs << __shift; }
  91. template <class _Integer>
  92. constexpr _EnableByteOverload<_Integer>
  93. operator<< (byte __lhs, _Integer __shift) noexcept
  94. { return static_cast<byte>(static_cast<unsigned char>(static_cast<unsigned int>(__lhs) << __shift)); }
  95. template <class _Integer>
  96. constexpr _EnableByteOverload<_Integer> &
  97. operator>>=(byte& __lhs, _Integer __shift) noexcept
  98. { return __lhs = __lhs >> __shift; }
  99. template <class _Integer>
  100. constexpr _EnableByteOverload<_Integer>
  101. operator>> (byte __lhs, _Integer __shift) noexcept
  102. { return static_cast<byte>(static_cast<unsigned char>(static_cast<unsigned int>(__lhs) >> __shift)); }
  103. template <class _Integer, class = _EnableByteOverload<_Integer> >
  104. _LIBCPP_NODISCARD_EXT constexpr _Integer
  105. to_integer(byte __b) noexcept { return static_cast<_Integer>(__b); }
  106. } // namespace std
  107. #endif
  108. #endif // _LIBCPP_CSTDDEF