File indexing completed on 2026-05-03 08:13:35
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LIBCPP___CXX03___INOUT_PTR_H
0011 #define _LIBCPP___CXX03___INOUT_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_same.h>
0019 #include <__cxx03/__type_traits/is_specialization.h>
0020 #include <__cxx03/__type_traits/is_void.h>
0021 #include <__cxx03/__utility/forward.h>
0022 #include <__cxx03/__utility/move.h>
0023 #include <__cxx03/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 <__cxx03/__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 inout_ptr_t {
0038 static_assert(!__is_specialization_v<_Smart, shared_ptr>, "std::shared_ptr<> is not supported with std::inout_ptr.");
0039
0040 public:
0041 _LIBCPP_HIDE_FROM_ABI explicit inout_ptr_t(_Smart& __smart, _Args... __args)
0042 : __s_(__smart), __a_(std::forward<_Args>(__args)...), __p_([&__smart] {
0043 if constexpr (is_pointer_v<_Smart>) {
0044 return __smart;
0045 } else {
0046 return __smart.get();
0047 }
0048 }()) {
0049 if constexpr (requires { __s_.release(); }) {
0050 __s_.release();
0051 } else {
0052 __s_ = _Smart();
0053 }
0054 }
0055
0056 _LIBCPP_HIDE_FROM_ABI inout_ptr_t(const inout_ptr_t&) = delete;
0057
0058 _LIBCPP_HIDE_FROM_ABI ~inout_ptr_t() {
0059
0060 if constexpr (!is_pointer_v<_Smart>) {
0061 if (!__p_) {
0062 return;
0063 }
0064 }
0065
0066 using _SmartPtr = __pointer_of_or_t<_Smart, _Pointer>;
0067 if constexpr (is_pointer_v<_Smart>) {
0068 std::apply([&](auto&&... __args) { __s_ = _Smart(static_cast<_SmartPtr>(__p_), std::forward<_Args>(__args)...); },
0069 std::move(__a_));
0070 } else if constexpr (__resettable_smart_pointer_with_args<_Smart, _Pointer, _Args...>) {
0071 std::apply([&](auto&&... __args) { __s_.reset(static_cast<_SmartPtr>(__p_), std::forward<_Args>(__args)...); },
0072 std::move(__a_));
0073 } else {
0074 static_assert(is_constructible_v<_Smart, _SmartPtr, _Args...>,
0075 "The smart pointer must be constructible from arguments of types _Smart, _Pointer, _Args...");
0076 std::apply([&](auto&&... __args) { __s_ = _Smart(static_cast<_SmartPtr>(__p_), std::forward<_Args>(__args)...); },
0077 std::move(__a_));
0078 }
0079 }
0080
0081 _LIBCPP_HIDE_FROM_ABI operator _Pointer*() const noexcept { return std::addressof(const_cast<_Pointer&>(__p_)); }
0082
0083 _LIBCPP_HIDE_FROM_ABI operator void**() const noexcept
0084 requires(!is_same_v<_Pointer, void*>)
0085 {
0086 static_assert(is_pointer_v<_Pointer>, "The conversion to void** requires _Pointer to be a raw pointer.");
0087
0088 return reinterpret_cast<void**>(static_cast<_Pointer*>(*this));
0089 }
0090
0091 private:
0092 _Smart& __s_;
0093 tuple<_Args...> __a_;
0094 _Pointer __p_;
0095 };
0096
0097 template <class _Pointer = void, class _Smart, class... _Args>
0098 _LIBCPP_HIDE_FROM_ABI auto inout_ptr(_Smart& __s, _Args&&... __args) {
0099 using _Ptr = conditional_t<is_void_v<_Pointer>, __pointer_of_t<_Smart>, _Pointer>;
0100 return std::inout_ptr_t<_Smart, _Ptr, _Args&&...>(__s, std::forward<_Args>(__args)...);
0101 }
0102
0103 #endif
0104
0105 _LIBCPP_END_NAMESPACE_STD
0106
0107 _LIBCPP_POP_MACROS
0108
0109 #endif