addressof.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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___MEMORY_ADDRESSOF_H
  10. #define _LIBCPP___MEMORY_ADDRESSOF_H
  11. #include <__config>
  12. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  13. # pragma GCC system_header
  14. #endif
  15. _LIBCPP_BEGIN_NAMESPACE_STD
  16. template <class _Tp>
  17. inline _LIBCPP_CONSTEXPR_SINCE_CXX17
  18. _LIBCPP_NO_CFI _LIBCPP_INLINE_VISIBILITY
  19. _Tp*
  20. addressof(_Tp& __x) _NOEXCEPT
  21. {
  22. return __builtin_addressof(__x);
  23. }
  24. #if defined(_LIBCPP_HAS_OBJC_ARC) && !defined(_LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF)
  25. // Objective-C++ Automatic Reference Counting uses qualified pointers
  26. // that require special addressof() signatures. When
  27. // _LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF is defined, the compiler
  28. // itself is providing these definitions. Otherwise, we provide them.
  29. template <class _Tp>
  30. inline _LIBCPP_INLINE_VISIBILITY
  31. __strong _Tp*
  32. addressof(__strong _Tp& __x) _NOEXCEPT
  33. {
  34. return &__x;
  35. }
  36. #ifdef _LIBCPP_HAS_OBJC_ARC_WEAK
  37. template <class _Tp>
  38. inline _LIBCPP_INLINE_VISIBILITY
  39. __weak _Tp*
  40. addressof(__weak _Tp& __x) _NOEXCEPT
  41. {
  42. return &__x;
  43. }
  44. #endif
  45. template <class _Tp>
  46. inline _LIBCPP_INLINE_VISIBILITY
  47. __autoreleasing _Tp*
  48. addressof(__autoreleasing _Tp& __x) _NOEXCEPT
  49. {
  50. return &__x;
  51. }
  52. template <class _Tp>
  53. inline _LIBCPP_INLINE_VISIBILITY
  54. __unsafe_unretained _Tp*
  55. addressof(__unsafe_unretained _Tp& __x) _NOEXCEPT
  56. {
  57. return &__x;
  58. }
  59. #endif
  60. #if !defined(_LIBCPP_CXX03_LANG)
  61. template <class _Tp> _Tp* addressof(const _Tp&&) noexcept = delete;
  62. #endif
  63. _LIBCPP_END_NAMESPACE_STD
  64. #endif // _LIBCPP___MEMORY_ADDRESSOF_H