xref: /freebsd/contrib/llvm-project/libcxx/include/__memory/out_ptr.h (revision 7ab1a32cd43cbae61ad4dd435d6a482bbf61cb52)
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 
10 #ifndef _LIBCPP___OUT_PTR_H
11 #define _LIBCPP___OUT_PTR_H
12 
13 #include <__config>
14 #include <__memory/addressof.h>
15 #include <__memory/pointer_traits.h>
16 #include <__memory/shared_ptr.h>
17 #include <__memory/unique_ptr.h>
18 #include <__type_traits/is_specialization.h>
19 #include <__type_traits/is_void.h>
20 #include <__utility/forward.h>
21 #include <__utility/move.h>
22 #include <tuple>
23 
24 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
25 #  pragma GCC system_header
26 #endif
27 
28 _LIBCPP_PUSH_MACROS
29 #include <__undef_macros>
30 
31 _LIBCPP_BEGIN_NAMESPACE_STD
32 
33 #if _LIBCPP_STD_VER >= 23
34 
35 template <class _Smart, class _Pointer, class... _Args>
36 class _LIBCPP_TEMPLATE_VIS out_ptr_t {
37   static_assert(!__is_specialization_v<_Smart, shared_ptr> || sizeof...(_Args) > 0,
38                 "Using std::shared_ptr<> without a deleter in std::out_ptr is not supported.");
39 
40 public:
41   _LIBCPP_HIDE_FROM_ABI explicit out_ptr_t(_Smart& __smart, _Args... __args)
42       : __s_(__smart), __a_(std::forward<_Args>(__args)...), __p_() {
43     using _Ptr = decltype(__smart);
44     if constexpr (__resettable_smart_pointer<_Ptr>) {
45       __s_.reset();
46     } else if constexpr (is_constructible_v<_Smart>) {
47       __s_ = _Smart();
48     } else {
49       static_assert(__resettable_smart_pointer<_Ptr> || is_constructible_v<_Smart>,
50                     "The adapted pointer type must have a reset() member function or be default constructible.");
51     }
52   }
53 
54   _LIBCPP_HIDE_FROM_ABI out_ptr_t(const out_ptr_t&) = delete;
55 
56   _LIBCPP_HIDE_FROM_ABI ~out_ptr_t() {
57     if (!__p_) {
58       return;
59     }
60 
61     using _SmartPtr = __pointer_of_or_t<_Smart, _Pointer>;
62     if constexpr (__resettable_smart_pointer_with_args<_Smart, _Pointer, _Args...>) {
63       std::apply([&](auto&&... __args) { __s_.reset(static_cast<_SmartPtr>(__p_), std::forward<_Args>(__args)...); },
64                  std::move(__a_));
65     } else {
66       static_assert(is_constructible_v<_Smart, _SmartPtr, _Args...>,
67                     "The smart pointer must be constructible from arguments of types _Smart, _Pointer, _Args...");
68       std::apply([&](auto&&... __args) { __s_ = _Smart(static_cast<_SmartPtr>(__p_), std::forward<_Args>(__args)...); },
69                  std::move(__a_));
70     }
71   }
72 
73   _LIBCPP_HIDE_FROM_ABI operator _Pointer*() const noexcept { return std::addressof(const_cast<_Pointer&>(__p_)); }
74 
75   _LIBCPP_HIDE_FROM_ABI operator void**() const noexcept
76     requires(!is_same_v<_Pointer, void*>)
77   {
78     static_assert(is_pointer_v<_Pointer>, "The conversion to void** requires _Pointer to be a raw pointer.");
79 
80     return reinterpret_cast<void**>(static_cast<_Pointer*>(*this));
81   }
82 
83 private:
84   _Smart& __s_;
85   tuple<_Args...> __a_;
86   _Pointer __p_ = _Pointer();
87 };
88 
89 template <class _Pointer = void, class _Smart, class... _Args>
90 _LIBCPP_HIDE_FROM_ABI auto out_ptr(_Smart& __s, _Args&&... __args) {
91   using _Ptr = conditional_t<is_void_v<_Pointer>, __pointer_of_t<_Smart>, _Pointer>;
92   return std::out_ptr_t<_Smart, _Ptr, _Args&&...>(__s, std::forward<_Args>(__args)...);
93 }
94 
95 #endif // _LIBCPP_STD_VER >= 23
96 
97 _LIBCPP_END_NAMESPACE_STD
98 
99 _LIBCPP_POP_MACROS
100 
101 #endif // _LIBCPP___OUT_PTR_H
102