Path: blob/main/contrib/llvm-project/libcxx/include/__memory/inout_ptr.h
35233 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___INOUT_PTR_H10#define _LIBCPP___INOUT_PTR_H1112#include <__config>13#include <__memory/addressof.h>14#include <__memory/pointer_traits.h>15#include <__memory/shared_ptr.h>16#include <__memory/unique_ptr.h>17#include <__type_traits/is_same.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>2324#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)25# pragma GCC system_header26#endif2728_LIBCPP_PUSH_MACROS29#include <__undef_macros>3031_LIBCPP_BEGIN_NAMESPACE_STD3233#if _LIBCPP_STD_VER >= 233435template <class _Smart, class _Pointer, class... _Args>36class _LIBCPP_TEMPLATE_VIS inout_ptr_t {37static_assert(!__is_specialization_v<_Smart, shared_ptr>, "std::shared_ptr<> is not supported with std::inout_ptr.");3839public:40_LIBCPP_HIDE_FROM_ABI explicit inout_ptr_t(_Smart& __smart, _Args... __args)41: __s_(__smart), __a_(std::forward<_Args>(__args)...), __p_([&__smart] {42if constexpr (is_pointer_v<_Smart>) {43return __smart;44} else {45return __smart.get();46}47}()) {48if constexpr (requires { __s_.release(); }) {49__s_.release();50} else {51__s_ = _Smart();52}53}5455_LIBCPP_HIDE_FROM_ABI inout_ptr_t(const inout_ptr_t&) = delete;5657_LIBCPP_HIDE_FROM_ABI ~inout_ptr_t() {58// LWG-3897 inout_ptr will not update raw pointer to null59if constexpr (!is_pointer_v<_Smart>) {60if (!__p_) {61return;62}63}6465using _SmartPtr = __pointer_of_or_t<_Smart, _Pointer>;66if constexpr (is_pointer_v<_Smart>) {67std::apply([&](auto&&... __args) { __s_ = _Smart(static_cast<_SmartPtr>(__p_), std::forward<_Args>(__args)...); },68std::move(__a_));69} else if constexpr (__resettable_smart_pointer_with_args<_Smart, _Pointer, _Args...>) {70std::apply([&](auto&&... __args) { __s_.reset(static_cast<_SmartPtr>(__p_), std::forward<_Args>(__args)...); },71std::move(__a_));72} else {73static_assert(is_constructible_v<_Smart, _SmartPtr, _Args...>,74"The smart pointer must be constructible from arguments of types _Smart, _Pointer, _Args...");75std::apply([&](auto&&... __args) { __s_ = _Smart(static_cast<_SmartPtr>(__p_), std::forward<_Args>(__args)...); },76std::move(__a_));77}78}7980_LIBCPP_HIDE_FROM_ABI operator _Pointer*() const noexcept { return std::addressof(const_cast<_Pointer&>(__p_)); }8182_LIBCPP_HIDE_FROM_ABI operator void**() const noexcept83requires(!is_same_v<_Pointer, void*>)84{85static_assert(is_pointer_v<_Pointer>, "The conversion to void** requires _Pointer to be a raw pointer.");8687return reinterpret_cast<void**>(static_cast<_Pointer*>(*this));88}8990private:91_Smart& __s_;92tuple<_Args...> __a_;93_Pointer __p_;94};9596template <class _Pointer = void, class _Smart, class... _Args>97_LIBCPP_HIDE_FROM_ABI auto inout_ptr(_Smart& __s, _Args&&... __args) {98using _Ptr = conditional_t<is_void_v<_Pointer>, __pointer_of_t<_Smart>, _Pointer>;99return std::inout_ptr_t<_Smart, _Ptr, _Args&&...>(__s, std::forward<_Args>(__args)...);100}101102#endif // _LIBCPP_STD_VER >= 23103104_LIBCPP_END_NAMESPACE_STD105106_LIBCPP_POP_MACROS107108#endif // _LIBCPP___INOUT_PTR_H109110111