10fca6ea1SDimitry Andric // -*- C++ -*-
20fca6ea1SDimitry Andric //===----------------------------------------------------------------------===//
30fca6ea1SDimitry Andric //
40fca6ea1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
50fca6ea1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
60fca6ea1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
70fca6ea1SDimitry Andric //
80fca6ea1SDimitry Andric //===----------------------------------------------------------------------===//
90fca6ea1SDimitry Andric
100fca6ea1SDimitry Andric #ifndef _LIBCPP___OUT_PTR_H
110fca6ea1SDimitry Andric #define _LIBCPP___OUT_PTR_H
120fca6ea1SDimitry Andric
130fca6ea1SDimitry Andric #include <__config>
140fca6ea1SDimitry Andric #include <__memory/addressof.h>
150fca6ea1SDimitry Andric #include <__memory/pointer_traits.h>
160fca6ea1SDimitry Andric #include <__memory/shared_ptr.h>
170fca6ea1SDimitry Andric #include <__memory/unique_ptr.h>
180fca6ea1SDimitry Andric #include <__type_traits/is_specialization.h>
190fca6ea1SDimitry Andric #include <__type_traits/is_void.h>
200fca6ea1SDimitry Andric #include <__utility/forward.h>
210fca6ea1SDimitry Andric #include <__utility/move.h>
220fca6ea1SDimitry Andric #include <tuple>
230fca6ea1SDimitry Andric
240fca6ea1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
250fca6ea1SDimitry Andric # pragma GCC system_header
260fca6ea1SDimitry Andric #endif
270fca6ea1SDimitry Andric
280fca6ea1SDimitry Andric _LIBCPP_PUSH_MACROS
290fca6ea1SDimitry Andric #include <__undef_macros>
300fca6ea1SDimitry Andric
310fca6ea1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
320fca6ea1SDimitry Andric
330fca6ea1SDimitry Andric #if _LIBCPP_STD_VER >= 23
340fca6ea1SDimitry Andric
350fca6ea1SDimitry Andric template <class _Smart, class _Pointer, class... _Args>
360fca6ea1SDimitry Andric class _LIBCPP_TEMPLATE_VIS out_ptr_t {
370fca6ea1SDimitry Andric static_assert(!__is_specialization_v<_Smart, shared_ptr> || sizeof...(_Args) > 0,
380fca6ea1SDimitry Andric "Using std::shared_ptr<> without a deleter in std::out_ptr is not supported.");
390fca6ea1SDimitry Andric
400fca6ea1SDimitry Andric public:
out_ptr_t(_Smart & __smart,_Args...__args)410fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit out_ptr_t(_Smart& __smart, _Args... __args)
420fca6ea1SDimitry Andric : __s_(__smart), __a_(std::forward<_Args>(__args)...), __p_() {
430fca6ea1SDimitry Andric using _Ptr = decltype(__smart);
440fca6ea1SDimitry Andric if constexpr (__resettable_smart_pointer<_Ptr>) {
450fca6ea1SDimitry Andric __s_.reset();
460fca6ea1SDimitry Andric } else if constexpr (is_constructible_v<_Smart>) {
470fca6ea1SDimitry Andric __s_ = _Smart();
480fca6ea1SDimitry Andric } else {
490fca6ea1SDimitry Andric static_assert(__resettable_smart_pointer<_Ptr> || is_constructible_v<_Smart>,
500fca6ea1SDimitry Andric "The adapted pointer type must have a reset() member function or be default constructible.");
510fca6ea1SDimitry Andric }
520fca6ea1SDimitry Andric }
530fca6ea1SDimitry Andric
540fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI out_ptr_t(const out_ptr_t&) = delete;
550fca6ea1SDimitry Andric
~out_ptr_t()560fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI ~out_ptr_t() {
570fca6ea1SDimitry Andric if (!__p_) {
580fca6ea1SDimitry Andric return;
590fca6ea1SDimitry Andric }
600fca6ea1SDimitry Andric
61*62987288SDimitry Andric using _SmartPtr = __pointer_of_or_t<_Smart, _Pointer>;
620fca6ea1SDimitry Andric if constexpr (__resettable_smart_pointer_with_args<_Smart, _Pointer, _Args...>) {
63*62987288SDimitry Andric std::apply([&](auto&&... __args) { __s_.reset(static_cast<_SmartPtr>(__p_), std::forward<_Args>(__args)...); },
640fca6ea1SDimitry Andric std::move(__a_));
650fca6ea1SDimitry Andric } else {
66*62987288SDimitry Andric static_assert(is_constructible_v<_Smart, _SmartPtr, _Args...>,
670fca6ea1SDimitry Andric "The smart pointer must be constructible from arguments of types _Smart, _Pointer, _Args...");
68*62987288SDimitry Andric std::apply([&](auto&&... __args) { __s_ = _Smart(static_cast<_SmartPtr>(__p_), std::forward<_Args>(__args)...); },
690fca6ea1SDimitry Andric std::move(__a_));
700fca6ea1SDimitry Andric }
710fca6ea1SDimitry Andric }
720fca6ea1SDimitry Andric
730fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI operator _Pointer*() const noexcept { return std::addressof(const_cast<_Pointer&>(__p_)); }
740fca6ea1SDimitry Andric
750fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI operator void**() const noexcept
760fca6ea1SDimitry Andric requires(!is_same_v<_Pointer, void*>)
770fca6ea1SDimitry Andric {
780fca6ea1SDimitry Andric static_assert(is_pointer_v<_Pointer>, "The conversion to void** requires _Pointer to be a raw pointer.");
790fca6ea1SDimitry Andric
800fca6ea1SDimitry Andric return reinterpret_cast<void**>(static_cast<_Pointer*>(*this));
810fca6ea1SDimitry Andric }
820fca6ea1SDimitry Andric
830fca6ea1SDimitry Andric private:
840fca6ea1SDimitry Andric _Smart& __s_;
850fca6ea1SDimitry Andric tuple<_Args...> __a_;
860fca6ea1SDimitry Andric _Pointer __p_ = _Pointer();
870fca6ea1SDimitry Andric };
880fca6ea1SDimitry Andric
890fca6ea1SDimitry Andric template <class _Pointer = void, class _Smart, class... _Args>
out_ptr(_Smart & __s,_Args &&...__args)900fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI auto out_ptr(_Smart& __s, _Args&&... __args) {
910fca6ea1SDimitry Andric using _Ptr = conditional_t<is_void_v<_Pointer>, __pointer_of_t<_Smart>, _Pointer>;
920fca6ea1SDimitry Andric return std::out_ptr_t<_Smart, _Ptr, _Args&&...>(__s, std::forward<_Args>(__args)...);
930fca6ea1SDimitry Andric }
940fca6ea1SDimitry Andric
950fca6ea1SDimitry Andric #endif // _LIBCPP_STD_VER >= 23
960fca6ea1SDimitry Andric
970fca6ea1SDimitry Andric _LIBCPP_END_NAMESPACE_STD
980fca6ea1SDimitry Andric
990fca6ea1SDimitry Andric _LIBCPP_POP_MACROS
1000fca6ea1SDimitry Andric
1010fca6ea1SDimitry Andric #endif // _LIBCPP___OUT_PTR_H
102