chromium/third_party/libc++/src/include/__memory/pointer_traits.h

// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCPP___MEMORY_POINTER_TRAITS_H
#define _LIBCPP___MEMORY_POINTER_TRAITS_H

#include <__config>
#include <__memory/addressof.h>
#include <__type_traits/conditional.h>
#include <__type_traits/conjunction.h>
#include <__type_traits/decay.h>
#include <__type_traits/is_class.h>
#include <__type_traits/is_function.h>
#include <__type_traits/is_void.h>
#include <__type_traits/void_t.h>
#include <__utility/declval.h>
#include <__utility/forward.h>
#include <cstddef>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#  pragma GCC system_header
#endif

_LIBCPP_PUSH_MACROS
#include <__undef_macros>

_LIBCPP_BEGIN_NAMESPACE_STD

// clang-format off
#define _LIBCPP_CLASS_TRAITS_HAS_XXX(NAME, PROPERTY)
// clang-format on

_LIBCPP_CLASS_TRAITS_HAS_XXX();
_LIBCPP_CLASS_TRAITS_HAS_XXX();

template <class _Ptr, bool = __has_element_type<_Ptr>::value>
struct __pointer_traits_element_type {};

__pointer_traits_element_type<_Ptr, true>;

__pointer_traits_element_type<_Sp<_Tp, _Args...>, true>;

__pointer_traits_element_type<_Sp<_Tp, _Args...>, false>;

template <class _Tp, class = void>
struct __has_difference_type : false_type {};

__has_difference_type<_Tp, __void_t<typename _Tp::difference_type>>;

template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
struct __pointer_traits_difference_type {};

__pointer_traits_difference_type<_Ptr, true>;

template <class _Tp, class _Up>
struct __has_rebind {};

template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
struct __pointer_traits_rebind {};

__pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>;

__pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>;

template <class _Ptr, class = void>
struct __pointer_traits_impl {};

__pointer_traits_impl<_Ptr, __void_t<typename __pointer_traits_element_type<_Ptr>::type>>;

template <class _Ptr>
struct _LIBCPP_TEMPLATE_VIS pointer_traits : __pointer_traits_impl<_Ptr> {};

pointer_traits<_Tp *>;

#ifndef _LIBCPP_CXX03_LANG
__rebind_pointer_t;
#else
template <class _From, class _To>
using __rebind_pointer_t = typename pointer_traits<_From>::template rebind<_To>::other;
#endif

// to_address

template <class _Pointer, class = void>
struct __to_address_helper;

template <class _Tp>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Tp* __to_address(_Tp* __p) _NOEXCEPT {}

template <class _Pointer, class = void>
struct _HasToAddress : false_type {};

_HasToAddress<_Pointer, decltype((void)pointer_traits<_Pointer>::to_address(std::declval<const _Pointer &>()))>;

template <class _Pointer, class = void>
struct _HasArrow : false_type {};

_HasArrow<_Pointer, decltype((void)std::declval<const _Pointer &>().operator->())>;

template <class _Pointer>
struct _IsFancyPointer {};

// enable_if is needed here to avoid instantiating checks for fancy pointers on raw pointers
template <class _Pointer, __enable_if_t< _And<is_class<_Pointer>, _IsFancyPointer<_Pointer> >::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI
_LIBCPP_CONSTEXPR __decay_t<decltype(__to_address_helper<_Pointer>::__call(std::declval<const _Pointer&>()))>
__to_address(const _Pointer& __p) _NOEXCEPT {}

template <class _Pointer, class>
struct __to_address_helper {};

__to_address_helper<_Pointer, decltype((void)pointer_traits<_Pointer>::to_address(std::declval<const _Pointer &>()))>;

#if _LIBCPP_STD_VER >= 20
template <class _Tp>
inline _LIBCPP_HIDE_FROM_ABI constexpr auto to_address(_Tp* __p) noexcept {}

template <class _Pointer>
inline _LIBCPP_HIDE_FROM_ABI constexpr auto
to_address(const _Pointer& __p) noexcept -> decltype(std::__to_address(__p)) {}
#endif

#if _LIBCPP_STD_VER >= 23

template <class _Tp>
struct __pointer_of {};

template <class _Tp>
  requires(__has_pointer<_Tp>::value)
struct __pointer_of<_Tp> {
  using type = typename _Tp::pointer;
};

template <class _Tp>
  requires(!__has_pointer<_Tp>::value && __has_element_type<_Tp>::value)
struct __pointer_of<_Tp> {
  using type = typename _Tp::element_type*;
};

template <class _Tp>
  requires(!__has_pointer<_Tp>::value && !__has_element_type<_Tp>::value &&
           __has_element_type<pointer_traits<_Tp>>::value)
struct __pointer_of<_Tp> {
  using type = typename pointer_traits<_Tp>::element_type*;
};

template <typename _Tp>
using __pointer_of_t = typename __pointer_of<_Tp>::type;

template <class _Tp, class _Up>
struct __pointer_of_or {
  using type _LIBCPP_NODEBUG = _Up;
};

template <class _Tp, class _Up>
  requires requires { typename __pointer_of_t<_Tp>; }
struct __pointer_of_or<_Tp, _Up> {
  using type _LIBCPP_NODEBUG = __pointer_of_t<_Tp>;
};

template <typename _Tp, typename _Up>
using __pointer_of_or_t = typename __pointer_of_or<_Tp, _Up>::type;

template <class _Smart>
concept __resettable_smart_pointer = requires(_Smart __s) { __s.reset(); };

template <class _Smart, class _Pointer, class... _Args>
concept __resettable_smart_pointer_with_args = requires(_Smart __s, _Pointer __p, _Args... __args) {
  __s.reset(static_cast<__pointer_of_or_t<_Smart, _Pointer>>(__p), std::forward<_Args>(__args)...);
};

#endif

_LIBCPP_END_NAMESPACE_STD

_LIBCPP_POP_MACROS

#endif // _LIBCPP___MEMORY_POINTER_TRAITS_H