File indexing completed on 2026-05-03 08:13:35
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LIBCPP___CXX03___OUT_PTR_H
0011 #define _LIBCPP___CXX03___OUT_PTR_H
0012
0013 #include <__cxx03/__config>
0014 #include <__cxx03/__memory/addressof.h>
0015 #include <__cxx03/__memory/pointer_traits.h>
0016 #include <__cxx03/__memory/shared_ptr.h>
0017 #include <__cxx03/__memory/unique_ptr.h>
0018 #include <__cxx03/__type_traits/is_specialization.h>
0019 #include <__cxx03/__type_traits/is_void.h>
0020 #include <__cxx03/__utility/forward.h>
0021 #include <__cxx03/__utility/move.h>
0022 #include <__cxx03/tuple>
0023
0024 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0025 # pragma GCC system_header
0026 #endif
0027
0028 _LIBCPP_PUSH_MACROS
0029 #include <__cxx03/__undef_macros>
0030
0031 _LIBCPP_BEGIN_NAMESPACE_STD
0032
0033 #if _LIBCPP_STD_VER >= 23
0034
0035 template <class _Smart, class _Pointer, class... _Args>
0036 class _LIBCPP_TEMPLATE_VIS out_ptr_t {
0037 static_assert(!__is_specialization_v<_Smart, shared_ptr> || sizeof...(_Args) > 0,
0038 "Using std::shared_ptr<> without a deleter in std::out_ptr is not supported.");
0039
0040 public:
0041 _LIBCPP_HIDE_FROM_ABI explicit out_ptr_t(_Smart& __smart, _Args... __args)
0042 : __s_(__smart), __a_(std::forward<_Args>(__args)...), __p_() {
0043 using _Ptr = decltype(__smart);
0044 if constexpr (__resettable_smart_pointer<_Ptr>) {
0045 __s_.reset();
0046 } else if constexpr (is_constructible_v<_Smart>) {
0047 __s_ = _Smart();
0048 } else {
0049 static_assert(__resettable_smart_pointer<_Ptr> || is_constructible_v<_Smart>,
0050 "The adapted pointer type must have a reset() member function or be default constructible.");
0051 }
0052 }
0053
0054 _LIBCPP_HIDE_FROM_ABI out_ptr_t(const out_ptr_t&) = delete;
0055
0056 _LIBCPP_HIDE_FROM_ABI ~out_ptr_t() {
0057 if (!__p_) {
0058 return;
0059 }
0060
0061 using _SmartPtr = __pointer_of_or_t<_Smart, _Pointer>;
0062 if constexpr (__resettable_smart_pointer_with_args<_Smart, _Pointer, _Args...>) {
0063 std::apply([&](auto&&... __args) { __s_.reset(static_cast<_SmartPtr>(__p_), std::forward<_Args>(__args)...); },
0064 std::move(__a_));
0065 } else {
0066 static_assert(is_constructible_v<_Smart, _SmartPtr, _Args...>,
0067 "The smart pointer must be constructible from arguments of types _Smart, _Pointer, _Args...");
0068 std::apply([&](auto&&... __args) { __s_ = _Smart(static_cast<_SmartPtr>(__p_), std::forward<_Args>(__args)...); },
0069 std::move(__a_));
0070 }
0071 }
0072
0073 _LIBCPP_HIDE_FROM_ABI operator _Pointer*() const noexcept { return std::addressof(const_cast<_Pointer&>(__p_)); }
0074
0075 _LIBCPP_HIDE_FROM_ABI operator void**() const noexcept
0076 requires(!is_same_v<_Pointer, void*>)
0077 {
0078 static_assert(is_pointer_v<_Pointer>, "The conversion to void** requires _Pointer to be a raw pointer.");
0079
0080 return reinterpret_cast<void**>(static_cast<_Pointer*>(*this));
0081 }
0082
0083 private:
0084 _Smart& __s_;
0085 tuple<_Args...> __a_;
0086 _Pointer __p_ = _Pointer();
0087 };
0088
0089 template <class _Pointer = void, class _Smart, class... _Args>
0090 _LIBCPP_HIDE_FROM_ABI auto out_ptr(_Smart& __s, _Args&&... __args) {
0091 using _Ptr = conditional_t<is_void_v<_Pointer>, __pointer_of_t<_Smart>, _Pointer>;
0092 return std::out_ptr_t<_Smart, _Ptr, _Args&&...>(__s, std::forward<_Args>(__args)...);
0093 }
0094
0095 #endif
0096
0097 _LIBCPP_END_NAMESPACE_STD
0098
0099 _LIBCPP_POP_MACROS
0100
0101 #endif