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