cstddef 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 <version>
  32. #include <stddef.h>
  33. #ifndef _LIBCPP_STDDEF_H
  34. # error <cstddef> tried including <stddef.h> but didn't find libc++'s <stddef.h> header. \
  35. This usually means that your header search paths are not configured properly. \
  36. The header search paths should contain the C++ Standard Library headers before \
  37. any C Standard Library, and you are probably using compiler flags that make that \
  38. not be the case.
  39. #endif
  40. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  41. # pragma GCC system_header
  42. #endif
  43. _LIBCPP_BEGIN_NAMESPACE_STD
  44. using ::nullptr_t;
  45. using ::ptrdiff_t _LIBCPP_USING_IF_EXISTS;
  46. using ::size_t _LIBCPP_USING_IF_EXISTS;
  47. #if !defined(_LIBCPP_CXX03_LANG)
  48. using ::max_align_t _LIBCPP_USING_IF_EXISTS;
  49. #endif
  50. _LIBCPP_END_NAMESPACE_STD
  51. #if _LIBCPP_STD_VER > 14
  52. namespace std // purposefully not versioned
  53. {
  54. enum class byte : unsigned char {};
  55. _LIBCPP_HIDE_FROM_ABI constexpr byte operator| (byte __lhs, byte __rhs) noexcept
  56. {
  57. return static_cast<byte>(
  58. static_cast<unsigned char>(
  59. static_cast<unsigned int>(__lhs) | static_cast<unsigned int>(__rhs)
  60. ));
  61. }
  62. _LIBCPP_HIDE_FROM_ABI constexpr byte& operator|=(byte& __lhs, byte __rhs) noexcept
  63. { return __lhs = __lhs | __rhs; }
  64. _LIBCPP_HIDE_FROM_ABI constexpr byte operator& (byte __lhs, byte __rhs) noexcept
  65. {
  66. return static_cast<byte>(
  67. static_cast<unsigned char>(
  68. static_cast<unsigned int>(__lhs) & static_cast<unsigned int>(__rhs)
  69. ));
  70. }
  71. _LIBCPP_HIDE_FROM_ABI constexpr byte& operator&=(byte& __lhs, byte __rhs) noexcept
  72. { return __lhs = __lhs & __rhs; }
  73. _LIBCPP_HIDE_FROM_ABI constexpr byte operator^ (byte __lhs, byte __rhs) noexcept
  74. {
  75. return static_cast<byte>(
  76. static_cast<unsigned char>(
  77. static_cast<unsigned int>(__lhs) ^ static_cast<unsigned int>(__rhs)
  78. ));
  79. }
  80. _LIBCPP_HIDE_FROM_ABI constexpr byte& operator^=(byte& __lhs, byte __rhs) noexcept
  81. { return __lhs = __lhs ^ __rhs; }
  82. _LIBCPP_HIDE_FROM_ABI constexpr byte operator~ (byte __b) noexcept
  83. {
  84. return static_cast<byte>(
  85. static_cast<unsigned char>(
  86. ~static_cast<unsigned int>(__b)
  87. ));
  88. }
  89. template <class _Tp>
  90. using _EnableByteOverload = __enable_if_t<is_integral<_Tp>::value, byte>;
  91. template <class _Integer>
  92. _LIBCPP_HIDE_FROM_ABI constexpr _EnableByteOverload<_Integer> &
  93. operator<<=(byte& __lhs, _Integer __shift) noexcept
  94. { return __lhs = __lhs << __shift; }
  95. template <class _Integer>
  96. _LIBCPP_HIDE_FROM_ABI constexpr _EnableByteOverload<_Integer>
  97. operator<< (byte __lhs, _Integer __shift) noexcept
  98. { return static_cast<byte>(static_cast<unsigned char>(static_cast<unsigned int>(__lhs) << __shift)); }
  99. template <class _Integer>
  100. _LIBCPP_HIDE_FROM_ABI constexpr _EnableByteOverload<_Integer> &
  101. operator>>=(byte& __lhs, _Integer __shift) noexcept
  102. { return __lhs = __lhs >> __shift; }
  103. template <class _Integer>
  104. _LIBCPP_HIDE_FROM_ABI constexpr _EnableByteOverload<_Integer>
  105. operator>> (byte __lhs, _Integer __shift) noexcept
  106. { return static_cast<byte>(static_cast<unsigned char>(static_cast<unsigned int>(__lhs) >> __shift)); }
  107. template <class _Integer, class = _EnableByteOverload<_Integer> >
  108. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr _Integer
  109. to_integer(byte __b) noexcept { return static_cast<_Integer>(__b); }
  110. } // namespace std
  111. #endif
  112. #endif // _LIBCPP_CSTDDEF