Path: blob/main/contrib/llvm-project/libcxx/include/__memory/out_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___OUT_PTR_H10#define _LIBCPP___OUT_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_specialization.h>18#include <__type_traits/is_void.h>19#include <__utility/forward.h>20#include <__utility/move.h>21#include <tuple>2223#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)24# pragma GCC system_header25#endif2627_LIBCPP_PUSH_MACROS28#include <__undef_macros>2930_LIBCPP_BEGIN_NAMESPACE_STD3132#if _LIBCPP_STD_VER >= 233334template <class _Smart, class _Pointer, class... _Args>35class _LIBCPP_TEMPLATE_VIS out_ptr_t {36static_assert(!__is_specialization_v<_Smart, shared_ptr> || sizeof...(_Args) > 0,37"Using std::shared_ptr<> without a deleter in std::out_ptr is not supported.");3839public:40_LIBCPP_HIDE_FROM_ABI explicit out_ptr_t(_Smart& __smart, _Args... __args)41: __s_(__smart), __a_(std::forward<_Args>(__args)...), __p_() {42using _Ptr = decltype(__smart);43if constexpr (__resettable_smart_pointer<_Ptr>) {44__s_.reset();45} else if constexpr (is_constructible_v<_Smart>) {46__s_ = _Smart();47} else {48static_assert(__resettable_smart_pointer<_Ptr> || is_constructible_v<_Smart>,49"The adapted pointer type must have a reset() member function or be default constructible.");50}51}5253_LIBCPP_HIDE_FROM_ABI out_ptr_t(const out_ptr_t&) = delete;5455_LIBCPP_HIDE_FROM_ABI ~out_ptr_t() {56if (!__p_) {57return;58}5960using _SmartPtr = __pointer_of_or_t<_Smart, _Pointer>;61if constexpr (__resettable_smart_pointer_with_args<_Smart, _Pointer, _Args...>) {62std::apply([&](auto&&... __args) { __s_.reset(static_cast<_SmartPtr>(__p_), std::forward<_Args>(__args)...); },63std::move(__a_));64} else {65static_assert(is_constructible_v<_Smart, _SmartPtr, _Args...>,66"The smart pointer must be constructible from arguments of types _Smart, _Pointer, _Args...");67std::apply([&](auto&&... __args) { __s_ = _Smart(static_cast<_SmartPtr>(__p_), std::forward<_Args>(__args)...); },68std::move(__a_));69}70}7172_LIBCPP_HIDE_FROM_ABI operator _Pointer*() const noexcept { return std::addressof(const_cast<_Pointer&>(__p_)); }7374_LIBCPP_HIDE_FROM_ABI operator void**() const noexcept75requires(!is_same_v<_Pointer, void*>)76{77static_assert(is_pointer_v<_Pointer>, "The conversion to void** requires _Pointer to be a raw pointer.");7879return reinterpret_cast<void**>(static_cast<_Pointer*>(*this));80}8182private:83_Smart& __s_;84tuple<_Args...> __a_;85_Pointer __p_ = _Pointer();86};8788template <class _Pointer = void, class _Smart, class... _Args>89_LIBCPP_HIDE_FROM_ABI auto out_ptr(_Smart& __s, _Args&&... __args) {90using _Ptr = conditional_t<is_void_v<_Pointer>, __pointer_of_t<_Smart>, _Pointer>;91return std::out_ptr_t<_Smart, _Ptr, _Args&&...>(__s, std::forward<_Args>(__args)...);92}9394#endif // _LIBCPP_STD_VER >= 239596_LIBCPP_END_NAMESPACE_STD9798_LIBCPP_POP_MACROS99100#endif // _LIBCPP___OUT_PTR_H101102103