Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:56

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___INOUT_PTR_H
0011 #define _LIBCPP___INOUT_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_same.h>
0020 #include <__type_traits/is_specialization.h>
0021 #include <__type_traits/is_void.h>
0022 #include <__utility/forward.h>
0023 #include <__utility/move.h>
0024 #include <tuple>
0025 
0026 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0027 #  pragma GCC system_header
0028 #endif
0029 
0030 _LIBCPP_PUSH_MACROS
0031 #include <__undef_macros>
0032 
0033 _LIBCPP_BEGIN_NAMESPACE_STD
0034 
0035 #if _LIBCPP_STD_VER >= 23
0036 
0037 template <class _Smart, class _Pointer, class... _Args>
0038 class _LIBCPP_TEMPLATE_VIS inout_ptr_t {
0039   static_assert(!__is_specialization_v<_Smart, shared_ptr>, "std::shared_ptr<> is not supported with std::inout_ptr.");
0040 
0041 public:
0042   _LIBCPP_HIDE_FROM_ABI explicit inout_ptr_t(_Smart& __smart, _Args... __args)
0043       : __s_(__smart), __a_(std::forward<_Args>(__args)...), __p_([&__smart] {
0044           if constexpr (is_pointer_v<_Smart>) {
0045             return __smart;
0046           } else {
0047             return __smart.get();
0048           }
0049         }()) {
0050     if constexpr (requires { __s_.release(); }) {
0051       __s_.release();
0052     } else {
0053       __s_ = _Smart();
0054     }
0055   }
0056 
0057   _LIBCPP_HIDE_FROM_ABI inout_ptr_t(const inout_ptr_t&) = delete;
0058 
0059   _LIBCPP_HIDE_FROM_ABI ~inout_ptr_t() {
0060     // LWG-3897 inout_ptr will not update raw pointer to null
0061     if constexpr (!is_pointer_v<_Smart>) {
0062       if (!__p_) {
0063         return;
0064       }
0065     }
0066 
0067     using _SmartPtr = __pointer_of_or_t<_Smart, _Pointer>;
0068     if constexpr (is_pointer_v<_Smart>) {
0069       std::apply([&](auto&&... __args) { __s_ = _Smart(static_cast<_SmartPtr>(__p_), std::forward<_Args>(__args)...); },
0070                  std::move(__a_));
0071     } else if constexpr (__resettable_smart_pointer_with_args<_Smart, _Pointer, _Args...>) {
0072       std::apply([&](auto&&... __args) { __s_.reset(static_cast<_SmartPtr>(__p_), std::forward<_Args>(__args)...); },
0073                  std::move(__a_));
0074     } else {
0075       static_assert(is_constructible_v<_Smart, _SmartPtr, _Args...>,
0076                     "The smart pointer must be constructible from arguments of types _Smart, _Pointer, _Args...");
0077       std::apply([&](auto&&... __args) { __s_ = _Smart(static_cast<_SmartPtr>(__p_), std::forward<_Args>(__args)...); },
0078                  std::move(__a_));
0079     }
0080   }
0081 
0082   _LIBCPP_HIDE_FROM_ABI operator _Pointer*() const noexcept { return std::addressof(const_cast<_Pointer&>(__p_)); }
0083 
0084   _LIBCPP_HIDE_FROM_ABI operator void**() const noexcept
0085     requires(!is_same_v<_Pointer, void*>)
0086   {
0087     static_assert(is_pointer_v<_Pointer>, "The conversion to void** requires _Pointer to be a raw pointer.");
0088 
0089     return reinterpret_cast<void**>(static_cast<_Pointer*>(*this));
0090   }
0091 
0092 private:
0093   _Smart& __s_;
0094   tuple<_Args...> __a_;
0095   _Pointer __p_;
0096 };
0097 
0098 template <class _Pointer = void, class _Smart, class... _Args>
0099 _LIBCPP_HIDE_FROM_ABI auto inout_ptr(_Smart& __s, _Args&&... __args) {
0100   using _Ptr = conditional_t<is_void_v<_Pointer>, __pointer_of_t<_Smart>, _Pointer>;
0101   return std::inout_ptr_t<_Smart, _Ptr, _Args&&...>(__s, std::forward<_Args>(__args)...);
0102 }
0103 
0104 #endif // _LIBCPP_STD_VER >= 23
0105 
0106 _LIBCPP_END_NAMESPACE_STD
0107 
0108 _LIBCPP_POP_MACROS
0109 
0110 #endif // _LIBCPP___INOUT_PTR_H