xref: /freebsd/contrib/llvm-project/libcxx/include/__memory/inout_ptr.h (revision 62987288060ff68c817b7056815aa9fb8ba8ecd7)
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___INOUT_PTR_H
110fca6ea1SDimitry Andric #define _LIBCPP___INOUT_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_same.h>
190fca6ea1SDimitry Andric #include <__type_traits/is_specialization.h>
200fca6ea1SDimitry Andric #include <__type_traits/is_void.h>
210fca6ea1SDimitry Andric #include <__utility/forward.h>
220fca6ea1SDimitry Andric #include <__utility/move.h>
230fca6ea1SDimitry Andric #include <tuple>
240fca6ea1SDimitry Andric 
250fca6ea1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
260fca6ea1SDimitry Andric #  pragma GCC system_header
270fca6ea1SDimitry Andric #endif
280fca6ea1SDimitry Andric 
290fca6ea1SDimitry Andric _LIBCPP_PUSH_MACROS
300fca6ea1SDimitry Andric #include <__undef_macros>
310fca6ea1SDimitry Andric 
320fca6ea1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
330fca6ea1SDimitry Andric 
340fca6ea1SDimitry Andric #if _LIBCPP_STD_VER >= 23
350fca6ea1SDimitry Andric 
360fca6ea1SDimitry Andric template <class _Smart, class _Pointer, class... _Args>
370fca6ea1SDimitry Andric class _LIBCPP_TEMPLATE_VIS inout_ptr_t {
380fca6ea1SDimitry Andric   static_assert(!__is_specialization_v<_Smart, shared_ptr>, "std::shared_ptr<> is not supported with std::inout_ptr.");
390fca6ea1SDimitry Andric 
400fca6ea1SDimitry Andric public:
inout_ptr_t(_Smart & __smart,_Args...__args)410fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI explicit inout_ptr_t(_Smart& __smart, _Args... __args)
420fca6ea1SDimitry Andric       : __s_(__smart), __a_(std::forward<_Args>(__args)...), __p_([&__smart] {
430fca6ea1SDimitry Andric           if constexpr (is_pointer_v<_Smart>) {
440fca6ea1SDimitry Andric             return __smart;
450fca6ea1SDimitry Andric           } else {
460fca6ea1SDimitry Andric             return __smart.get();
470fca6ea1SDimitry Andric           }
480fca6ea1SDimitry Andric         }()) {
490fca6ea1SDimitry Andric     if constexpr (requires { __s_.release(); }) {
500fca6ea1SDimitry Andric       __s_.release();
510fca6ea1SDimitry Andric     } else {
520fca6ea1SDimitry Andric       __s_ = _Smart();
530fca6ea1SDimitry Andric     }
540fca6ea1SDimitry Andric   }
550fca6ea1SDimitry Andric 
560fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI inout_ptr_t(const inout_ptr_t&) = delete;
570fca6ea1SDimitry Andric 
~inout_ptr_t()580fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI ~inout_ptr_t() {
590fca6ea1SDimitry Andric     // LWG-3897 inout_ptr will not update raw pointer to null
600fca6ea1SDimitry Andric     if constexpr (!is_pointer_v<_Smart>) {
610fca6ea1SDimitry Andric       if (!__p_) {
620fca6ea1SDimitry Andric         return;
630fca6ea1SDimitry Andric       }
640fca6ea1SDimitry Andric     }
650fca6ea1SDimitry Andric 
66*62987288SDimitry Andric     using _SmartPtr = __pointer_of_or_t<_Smart, _Pointer>;
670fca6ea1SDimitry Andric     if constexpr (is_pointer_v<_Smart>) {
68*62987288SDimitry Andric       std::apply([&](auto&&... __args) { __s_ = _Smart(static_cast<_SmartPtr>(__p_), std::forward<_Args>(__args)...); },
690fca6ea1SDimitry Andric                  std::move(__a_));
700fca6ea1SDimitry Andric     } else if constexpr (__resettable_smart_pointer_with_args<_Smart, _Pointer, _Args...>) {
71*62987288SDimitry Andric       std::apply([&](auto&&... __args) { __s_.reset(static_cast<_SmartPtr>(__p_), std::forward<_Args>(__args)...); },
720fca6ea1SDimitry Andric                  std::move(__a_));
730fca6ea1SDimitry Andric     } else {
74*62987288SDimitry Andric       static_assert(is_constructible_v<_Smart, _SmartPtr, _Args...>,
750fca6ea1SDimitry Andric                     "The smart pointer must be constructible from arguments of types _Smart, _Pointer, _Args...");
76*62987288SDimitry Andric       std::apply([&](auto&&... __args) { __s_ = _Smart(static_cast<_SmartPtr>(__p_), std::forward<_Args>(__args)...); },
770fca6ea1SDimitry Andric                  std::move(__a_));
780fca6ea1SDimitry Andric     }
790fca6ea1SDimitry Andric   }
800fca6ea1SDimitry Andric 
810fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI operator _Pointer*() const noexcept { return std::addressof(const_cast<_Pointer&>(__p_)); }
820fca6ea1SDimitry Andric 
830fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI operator void**() const noexcept
840fca6ea1SDimitry Andric     requires(!is_same_v<_Pointer, void*>)
850fca6ea1SDimitry Andric   {
860fca6ea1SDimitry Andric     static_assert(is_pointer_v<_Pointer>, "The conversion to void** requires _Pointer to be a raw pointer.");
870fca6ea1SDimitry Andric 
880fca6ea1SDimitry Andric     return reinterpret_cast<void**>(static_cast<_Pointer*>(*this));
890fca6ea1SDimitry Andric   }
900fca6ea1SDimitry Andric 
910fca6ea1SDimitry Andric private:
920fca6ea1SDimitry Andric   _Smart& __s_;
930fca6ea1SDimitry Andric   tuple<_Args...> __a_;
940fca6ea1SDimitry Andric   _Pointer __p_;
950fca6ea1SDimitry Andric };
960fca6ea1SDimitry Andric 
970fca6ea1SDimitry Andric template <class _Pointer = void, class _Smart, class... _Args>
inout_ptr(_Smart & __s,_Args &&...__args)980fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI auto inout_ptr(_Smart& __s, _Args&&... __args) {
990fca6ea1SDimitry Andric   using _Ptr = conditional_t<is_void_v<_Pointer>, __pointer_of_t<_Smart>, _Pointer>;
1000fca6ea1SDimitry Andric   return std::inout_ptr_t<_Smart, _Ptr, _Args&&...>(__s, std::forward<_Args>(__args)...);
1010fca6ea1SDimitry Andric }
1020fca6ea1SDimitry Andric 
1030fca6ea1SDimitry Andric #endif // _LIBCPP_STD_VER >= 23
1040fca6ea1SDimitry Andric 
1050fca6ea1SDimitry Andric _LIBCPP_END_NAMESPACE_STD
1060fca6ea1SDimitry Andric 
1070fca6ea1SDimitry Andric _LIBCPP_POP_MACROS
1080fca6ea1SDimitry Andric 
1090fca6ea1SDimitry Andric #endif // _LIBCPP___INOUT_PTR_H
110