Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/contrib/llvm-project/libcxx/include/__memory/inout_ptr.h
35233 views
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___INOUT_PTR_H
11
#define _LIBCPP___INOUT_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_same.h>
19
#include <__type_traits/is_specialization.h>
20
#include <__type_traits/is_void.h>
21
#include <__utility/forward.h>
22
#include <__utility/move.h>
23
#include <tuple>
24
25
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26
# pragma GCC system_header
27
#endif
28
29
_LIBCPP_PUSH_MACROS
30
#include <__undef_macros>
31
32
_LIBCPP_BEGIN_NAMESPACE_STD
33
34
#if _LIBCPP_STD_VER >= 23
35
36
template <class _Smart, class _Pointer, class... _Args>
37
class _LIBCPP_TEMPLATE_VIS inout_ptr_t {
38
static_assert(!__is_specialization_v<_Smart, shared_ptr>, "std::shared_ptr<> is not supported with std::inout_ptr.");
39
40
public:
41
_LIBCPP_HIDE_FROM_ABI explicit inout_ptr_t(_Smart& __smart, _Args... __args)
42
: __s_(__smart), __a_(std::forward<_Args>(__args)...), __p_([&__smart] {
43
if constexpr (is_pointer_v<_Smart>) {
44
return __smart;
45
} else {
46
return __smart.get();
47
}
48
}()) {
49
if constexpr (requires { __s_.release(); }) {
50
__s_.release();
51
} else {
52
__s_ = _Smart();
53
}
54
}
55
56
_LIBCPP_HIDE_FROM_ABI inout_ptr_t(const inout_ptr_t&) = delete;
57
58
_LIBCPP_HIDE_FROM_ABI ~inout_ptr_t() {
59
// LWG-3897 inout_ptr will not update raw pointer to null
60
if constexpr (!is_pointer_v<_Smart>) {
61
if (!__p_) {
62
return;
63
}
64
}
65
66
using _SmartPtr = __pointer_of_or_t<_Smart, _Pointer>;
67
if constexpr (is_pointer_v<_Smart>) {
68
std::apply([&](auto&&... __args) { __s_ = _Smart(static_cast<_SmartPtr>(__p_), std::forward<_Args>(__args)...); },
69
std::move(__a_));
70
} else if constexpr (__resettable_smart_pointer_with_args<_Smart, _Pointer, _Args...>) {
71
std::apply([&](auto&&... __args) { __s_.reset(static_cast<_SmartPtr>(__p_), std::forward<_Args>(__args)...); },
72
std::move(__a_));
73
} else {
74
static_assert(is_constructible_v<_Smart, _SmartPtr, _Args...>,
75
"The smart pointer must be constructible from arguments of types _Smart, _Pointer, _Args...");
76
std::apply([&](auto&&... __args) { __s_ = _Smart(static_cast<_SmartPtr>(__p_), std::forward<_Args>(__args)...); },
77
std::move(__a_));
78
}
79
}
80
81
_LIBCPP_HIDE_FROM_ABI operator _Pointer*() const noexcept { return std::addressof(const_cast<_Pointer&>(__p_)); }
82
83
_LIBCPP_HIDE_FROM_ABI operator void**() const noexcept
84
requires(!is_same_v<_Pointer, void*>)
85
{
86
static_assert(is_pointer_v<_Pointer>, "The conversion to void** requires _Pointer to be a raw pointer.");
87
88
return reinterpret_cast<void**>(static_cast<_Pointer*>(*this));
89
}
90
91
private:
92
_Smart& __s_;
93
tuple<_Args...> __a_;
94
_Pointer __p_;
95
};
96
97
template <class _Pointer = void, class _Smart, class... _Args>
98
_LIBCPP_HIDE_FROM_ABI auto inout_ptr(_Smart& __s, _Args&&... __args) {
99
using _Ptr = conditional_t<is_void_v<_Pointer>, __pointer_of_t<_Smart>, _Pointer>;
100
return std::inout_ptr_t<_Smart, _Ptr, _Args&&...>(__s, std::forward<_Args>(__args)...);
101
}
102
103
#endif // _LIBCPP_STD_VER >= 23
104
105
_LIBCPP_END_NAMESPACE_STD
106
107
_LIBCPP_POP_MACROS
108
109
#endif // _LIBCPP___INOUT_PTR_H
110
111