Files
gcc/libstdc++-v3/include/bits/out_ptr.h
Jason Merrill 63a598deb0 libstdc++: #ifdef out #pragma GCC system_header
In r15-3714-gd3a7302ec5985a I added -Wsystem-headers to the libstdc++ build
flags to help catch problems in the library.  This patch takes a different
approach, of disabling the #pragma system_header unless _GLIBCXX_SYSHDR is
defined.  As a result, the testsuites will treat them as non-system-headers
to get better warning coverage during regression testing of both gcc and
libstdc++, not just when building the library.

My rationale for the #ifdef instead of just removing the #pragma is the
three G++ tests that want to test libstdc++ system header behavior, so we
need a way to select it.

This doesn't affect installed libraries, as they get their
system-header status from the lookup path.  But testsuite_flags
--build-includes gives -I directives rather than -isystem.

This patch doesn't change the headers in config/ because I'm not compiling
with most of them, so won't see any warnings that need fixing.  Adjusting
them could happen later, or we can not bother.

libstdc++-v3/ChangeLog:

	* acinclude.m4 (WARN_FLAGS): Remove -Wsystem-headers.
	* configure: Regenerate.
	* include/bits/algorithmfwd.h: #ifdef out #pragma GCC system_header.
	* include/bits/atomic_base.h
	* include/bits/atomic_futex.h
	* include/bits/atomic_timed_wait.h
	* include/bits/atomic_wait.h
	* include/bits/basic_ios.h
	* include/bits/basic_string.h
	* include/bits/boost_concept_check.h
	* include/bits/char_traits.h
	* include/bits/charconv.h
	* include/bits/chrono.h
	* include/bits/chrono_io.h
	* include/bits/codecvt.h
	* include/bits/concept_check.h
	* include/bits/cpp_type_traits.h
	* include/bits/elements_of.h
	* include/bits/enable_special_members.h
	* include/bits/erase_if.h
	* include/bits/forward_list.h
	* include/bits/functional_hash.h
	* include/bits/gslice.h
	* include/bits/gslice_array.h
	* include/bits/hashtable.h
	* include/bits/indirect_array.h
	* include/bits/invoke.h
	* include/bits/ios_base.h
	* include/bits/iterator_concepts.h
	* include/bits/locale_classes.h
	* include/bits/locale_facets.h
	* include/bits/locale_facets_nonio.h
	* include/bits/localefwd.h
	* include/bits/mask_array.h
	* include/bits/max_size_type.h
	* include/bits/memory_resource.h
	* include/bits/memoryfwd.h
	* include/bits/move_only_function.h
	* include/bits/node_handle.h
	* include/bits/ostream_insert.h
	* include/bits/out_ptr.h
	* include/bits/parse_numbers.h
	* include/bits/postypes.h
	* include/bits/quoted_string.h
	* include/bits/range_access.h
	* include/bits/ranges_base.h
	* include/bits/refwrap.h
	* include/bits/sat_arith.h
	* include/bits/semaphore_base.h
	* include/bits/slice_array.h
	* include/bits/std_abs.h
	* include/bits/std_function.h
	* include/bits/std_mutex.h
	* include/bits/std_thread.h
	* include/bits/stl_iterator_base_funcs.h
	* include/bits/stl_iterator_base_types.h
	* include/bits/stl_tree.h
	* include/bits/stream_iterator.h
	* include/bits/streambuf_iterator.h
	* include/bits/stringfwd.h
	* include/bits/this_thread_sleep.h
	* include/bits/unique_lock.h
	* include/bits/uses_allocator_args.h
	* include/bits/utility.h
	* include/bits/valarray_after.h
	* include/bits/valarray_array.h
	* include/bits/valarray_before.h
	* include/bits/version.h
	* include/c_compatibility/fenv.h
	* include/c_compatibility/inttypes.h
	* include/c_compatibility/stdint.h
	* include/decimal/decimal.h
	* include/experimental/bits/net.h
	* include/experimental/bits/shared_ptr.h
	* include/ext/aligned_buffer.h
	* include/ext/alloc_traits.h
	* include/ext/atomicity.h
	* include/ext/concurrence.h
	* include/ext/numeric_traits.h
	* include/ext/pod_char_traits.h
	* include/ext/pointer.h
	* include/ext/stdio_filebuf.h
	* include/ext/stdio_sync_filebuf.h
	* include/ext/string_conversions.h
	* include/ext/type_traits.h
	* include/ext/vstring.h
	* include/ext/vstring_fwd.h
	* include/ext/vstring_util.h
	* include/parallel/algorithmfwd.h
	* include/parallel/numericfwd.h
	* include/tr1/functional_hash.h
	* include/tr1/hashtable.h
	* include/tr1/random.h
	* libsupc++/exception.h
	* libsupc++/hash_bytes.h
	* include/bits/basic_ios.tcc
	* include/bits/basic_string.tcc
	* include/bits/fstream.tcc
	* include/bits/istream.tcc
	* include/bits/locale_classes.tcc
	* include/bits/locale_facets.tcc
	* include/bits/locale_facets_nonio.tcc
	* include/bits/ostream.tcc
	* include/bits/sstream.tcc
	* include/bits/streambuf.tcc
	* include/bits/string_view.tcc
	* include/bits/version.tpl
	* include/experimental/bits/string_view.tcc
	* include/ext/pb_ds/detail/resize_policy/hash_prime_size_policy_imp.hpp
	* include/ext/random.tcc
	* include/ext/vstring.tcc
	* include/tr2/bool_set.tcc
	* include/tr2/dynamic_bitset.tcc
	* include/bits/c++config
	* include/c/cassert
	* include/c/cctype
	* include/c/cerrno
	* include/c/cfloat
	* include/c/ciso646
	* include/c/climits
	* include/c/clocale
	* include/c/cmath
	* include/c/csetjmp
	* include/c/csignal
	* include/c/cstdarg
	* include/c/cstddef
	* include/c/cstdio
	* include/c/cstdlib
	* include/c/cstring
	* include/c/ctime
	* include/c/cuchar
	* include/c/cwchar
	* include/c/cwctype
	* include/c_global/cassert
	* include/c_global/ccomplex
	* include/c_global/cctype
	* include/c_global/cerrno
	* include/c_global/cfenv
	* include/c_global/cfloat
	* include/c_global/cinttypes
	* include/c_global/ciso646
	* include/c_global/climits
	* include/c_global/clocale
	* include/c_global/cmath
	* include/c_global/csetjmp
	* include/c_global/csignal
	* include/c_global/cstdalign
	* include/c_global/cstdarg
	* include/c_global/cstdbool
	* include/c_global/cstddef
	* include/c_global/cstdint
	* include/c_global/cstdio
	* include/c_global/cstdlib
	* include/c_global/cstring
	* include/c_global/ctgmath
	* include/c_global/ctime
	* include/c_global/cuchar
	* include/c_global/cwchar
	* include/c_global/cwctype
	* include/c_std/cassert
	* include/c_std/cctype
	* include/c_std/cerrno
	* include/c_std/cfloat
	* include/c_std/ciso646
	* include/c_std/climits
	* include/c_std/clocale
	* include/c_std/cmath
	* include/c_std/csetjmp
	* include/c_std/csignal
	* include/c_std/cstdarg
	* include/c_std/cstddef
	* include/c_std/cstdio
	* include/c_std/cstdlib
	* include/c_std/cstring
	* include/c_std/ctime
	* include/c_std/cuchar
	* include/c_std/cwchar
	* include/c_std/cwctype
	* include/debug/array
	* include/debug/bitset
	* include/debug/deque
	* include/debug/forward_list
	* include/debug/list
	* include/debug/map
	* include/debug/set
	* include/debug/string
	* include/debug/unordered_map
	* include/debug/unordered_set
	* include/debug/vector
	* include/decimal/decimal
	* include/experimental/algorithm
	* include/experimental/any
	* include/experimental/array
	* include/experimental/buffer
	* include/experimental/chrono
	* include/experimental/contract
	* include/experimental/deque
	* include/experimental/executor
	* include/experimental/filesystem
	* include/experimental/forward_list
	* include/experimental/functional
	* include/experimental/internet
	* include/experimental/io_context
	* include/experimental/iterator
	* include/experimental/list
	* include/experimental/map
	* include/experimental/memory
	* include/experimental/memory_resource
	* include/experimental/net
	* include/experimental/netfwd
	* include/experimental/numeric
	* include/experimental/propagate_const
	* include/experimental/ratio
	* include/experimental/regex
	* include/experimental/scope
	* include/experimental/set
	* include/experimental/socket
	* include/experimental/string
	* include/experimental/string_view
	* include/experimental/synchronized_value
	* include/experimental/system_error
	* include/experimental/timer
	* include/experimental/tuple
	* include/experimental/type_traits
	* include/experimental/unordered_map
	* include/experimental/unordered_set
	* include/experimental/vector
	* include/ext/algorithm
	* include/ext/cmath
	* include/ext/functional
	* include/ext/iterator
	* include/ext/memory
	* include/ext/numeric
	* include/ext/random
	* include/ext/rb_tree
	* include/ext/rope
	* include/parallel/algorithm
	* include/std/algorithm
	* include/std/any
	* include/std/array
	* include/std/atomic
	* include/std/barrier
	* include/std/bit
	* include/std/bitset
	* include/std/charconv
	* include/std/chrono
	* include/std/codecvt
	* include/std/complex
	* include/std/concepts
	* include/std/condition_variable
	* include/std/coroutine
	* include/std/deque
	* include/std/execution
	* include/std/expected
	* include/std/filesystem
	* include/std/format
	* include/std/forward_list
	* include/std/fstream
	* include/std/functional
	* include/std/future
	* include/std/generator
	* include/std/iomanip
	* include/std/ios
	* include/std/iosfwd
	* include/std/iostream
	* include/std/istream
	* include/std/iterator
	* include/std/latch
	* include/std/limits
	* include/std/list
	* include/std/locale
	* include/std/map
	* include/std/memory
	* include/std/memory_resource
	* include/std/mutex
	* include/std/numbers
	* include/std/numeric
	* include/std/optional
	* include/std/ostream
	* include/std/print
	* include/std/queue
	* include/std/random
	* include/std/ranges
	* include/std/ratio
	* include/std/regex
	* include/std/scoped_allocator
	* include/std/semaphore
	* include/std/set
	* include/std/shared_mutex
	* include/std/span
	* include/std/spanstream
	* include/std/sstream
	* include/std/stack
	* include/std/stacktrace
	* include/std/stdexcept
	* include/std/streambuf
	* include/std/string
	* include/std/string_view
	* include/std/syncstream
	* include/std/system_error
	* include/std/text_encoding
	* include/std/thread
	* include/std/tuple
	* include/std/type_traits
	* include/std/typeindex
	* include/std/unordered_map
	* include/std/unordered_set
	* include/std/utility
	* include/std/valarray
	* include/std/variant
	* include/std/vector
	* include/std/version
	* include/tr1/array
	* include/tr1/cfenv
	* include/tr1/cinttypes
	* include/tr1/cmath
	* include/tr1/complex
	* include/tr1/cstdbool
	* include/tr1/cstdint
	* include/tr1/cstdio
	* include/tr1/cstdlib
	* include/tr1/cwchar
	* include/tr1/cwctype
	* include/tr1/functional
	* include/tr1/memory
	* include/tr1/random
	* include/tr1/regex
	* include/tr1/tuple
	* include/tr1/type_traits
	* include/tr1/unordered_map
	* include/tr1/unordered_set
	* include/tr1/utility
	* include/tr2/bool_set
	* include/tr2/dynamic_bitset
	* include/tr2/type_traits
	* libsupc++/atomic_lockfree_defines.h
	* libsupc++/compare
	* libsupc++/cxxabi.h
	* libsupc++/cxxabi_forced.h
	* libsupc++/cxxabi_init_exception.h
	* libsupc++/exception
	* libsupc++/initializer_list
	* libsupc++/new
	* libsupc++/typeinfo: Likewise.
	* testsuite/20_util/ratio/operations/ops_overflow_neg.cc
	* testsuite/23_containers/array/tuple_interface/get_neg.cc
	* testsuite/23_containers/vector/cons/destructible_debug_neg.cc
	* testsuite/24_iterators/operations/prev_neg.cc
	* testsuite/ext/type_traits/add_unsigned_floating_neg.cc
	* testsuite/ext/type_traits/add_unsigned_integer_neg.cc
	* testsuite/ext/type_traits/remove_unsigned_floating_neg.cc
	* testsuite/ext/type_traits/remove_unsigned_integer_neg.cc: Adjust
	line numbers.

gcc/testsuite/ChangeLog

	* g++.dg/analyzer/fanalyzer-show-events-in-system-headers-default.C
	* g++.dg/analyzer/fanalyzer-show-events-in-system-headers-no.C
	* g++.dg/diagnostic/disable.C: #define _GLIBCXX_SYSHDR.
2024-09-25 08:20:45 -04:00

476 lines
14 KiB
C++

// Smart pointer adaptors -*- C++ -*-
// Copyright The GNU Toolchain Authors.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/** @file include/bits/out_ptr.h
* This is an internal header file, included by other library headers.
* Do not attempt to use it directly. @headername{memory}
*/
#ifndef _GLIBCXX_OUT_PTR_H
#define _GLIBCXX_OUT_PTR_H 1
#ifdef _GLIBCXX_SYSHDR
#pragma GCC system_header
#endif
#include <bits/version.h>
#ifdef __glibcxx_out_ptr // C++ >= 23
#include <tuple>
#include <bits/ptr_traits.h>
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
/// Smart pointer adaptor for functions taking an output pointer parameter.
/**
* @tparam _Smart The type of pointer to adapt.
* @tparam _Pointer The type of pointer to convert to.
* @tparam _Args... Argument types used when resetting the smart pointer.
* @since C++23
* @headerfile <memory>
*/
template<typename _Smart, typename _Pointer, typename... _Args>
class out_ptr_t
{
#if _GLIBCXX_HOSTED
static_assert(!__is_shared_ptr<_Smart> || sizeof...(_Args) != 0,
"a deleter must be used when adapting std::shared_ptr "
"with std::out_ptr");
#endif
public:
explicit
out_ptr_t(_Smart& __smart, _Args... __args)
: _M_impl{__smart, std::forward<_Args>(__args)...}
{
if constexpr (requires { _M_impl._M_out_init(); })
_M_impl._M_out_init();
}
out_ptr_t(const out_ptr_t&) = delete;
~out_ptr_t() = default;
operator _Pointer*() const noexcept
{ return _M_impl._M_get(); }
operator void**() const noexcept requires (!same_as<_Pointer, void*>)
{
static_assert(is_pointer_v<_Pointer>);
_Pointer* __p = *this;
return static_cast<void**>(static_cast<void*>(__p));
}
private:
// TODO: Move this to namespace scope? e.g. __detail::_Ptr_adapt_impl
template<typename, typename, typename...>
struct _Impl
{
// This constructor must not modify __s because out_ptr_t and
// inout_ptr_t want to do different things. After construction
// they call _M_out_init() or _M_inout_init() respectively.
_Impl(_Smart& __s, _Args&&... __args)
: _M_smart(__s), _M_args(std::forward<_Args>(__args)...)
{ }
// Called by out_ptr_t to clear the smart pointer before using it.
void
_M_out_init()
{
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 3734. Inconsistency in inout_ptr and out_ptr for empty case
if constexpr (requires { _M_smart.reset(); })
_M_smart.reset();
else
_M_smart = _Smart();
}
// Called by inout_ptr_t to copy the smart pointer's value
// to the pointer that is returned from _M_get().
void
_M_inout_init()
{ _M_ptr = _M_smart.release(); }
// The pointer value returned by operator Pointer*().
_Pointer*
_M_get() const
{ return __builtin_addressof(const_cast<_Pointer&>(_M_ptr)); }
// Finalize the effects on the smart pointer.
~_Impl() noexcept(false);
_Smart& _M_smart;
[[no_unique_address]] _Pointer _M_ptr{};
[[no_unique_address]] tuple<_Args...> _M_args;
};
// Partial specialization for raw pointers.
template<typename _Tp>
struct _Impl<_Tp*, _Tp*>
{
void
_M_out_init()
{ _M_p = nullptr; }
void
_M_inout_init()
{ }
_Tp**
_M_get() const
{ return __builtin_addressof(const_cast<_Tp*&>(_M_p)); }
_Tp*& _M_p;
};
// Partial specialization for raw pointers, with conversion.
template<typename _Tp, typename _Ptr> requires (!is_same_v<_Ptr, _Tp*>)
struct _Impl<_Tp*, _Ptr>
{
explicit
_Impl(_Tp*& __p)
: _M_p(__p)
{ }
void
_M_out_init()
{ _M_p = nullptr; }
void
_M_inout_init()
{ _M_ptr = _M_p; }
_Pointer*
_M_get() const
{ return __builtin_addressof(const_cast<_Pointer&>(_M_ptr)); }
~_Impl() { _M_p = static_cast<_Tp*>(_M_ptr); }
_Tp*& _M_p;
_Pointer _M_ptr{};
};
// Partial specialization for std::unique_ptr.
// This specialization gives direct access to the private member
// of the unique_ptr, avoiding the overhead of storing a separate
// pointer and then resetting the unique_ptr in the destructor.
// FIXME: constrain to only match the primary template,
// not program-defined specializations of unique_ptr.
template<typename _Tp, typename _Del>
struct _Impl<unique_ptr<_Tp, _Del>,
typename unique_ptr<_Tp, _Del>::pointer>
{
void
_M_out_init()
{ _M_smart.reset(); }
_Pointer*
_M_get() const noexcept
{ return __builtin_addressof(_M_smart._M_t._M_ptr()); }
_Smart& _M_smart;
};
// Partial specialization for std::unique_ptr with replacement deleter.
// FIXME: constrain to only match the primary template,
// not program-defined specializations of unique_ptr.
template<typename _Tp, typename _Del, typename _Del2>
struct _Impl<unique_ptr<_Tp, _Del>,
typename unique_ptr<_Tp, _Del>::pointer, _Del2>
{
void
_M_out_init()
{ _M_smart.reset(); }
_Pointer*
_M_get() const noexcept
{ return __builtin_addressof(_M_smart._M_t._M_ptr()); }
~_Impl()
{
if (_M_smart.get())
_M_smart._M_t._M_deleter() = std::forward<_Del2>(_M_del);
}
_Smart& _M_smart;
[[no_unique_address]] _Del2 _M_del;
};
#if _GLIBCXX_HOSTED
// Partial specialization for std::shared_ptr.
// This specialization gives direct access to the private member
// of the shared_ptr, avoiding the overhead of storing a separate
// pointer and then resetting the shared_ptr in the destructor.
// A new control block is allocated in the constructor, so that if
// allocation fails it doesn't throw an exception from the destructor.
template<typename _Tp, typename _Del, typename _Alloc>
requires (is_base_of_v<__shared_ptr<_Tp>, shared_ptr<_Tp>>)
struct _Impl<shared_ptr<_Tp>,
typename shared_ptr<_Tp>::element_type*, _Del, _Alloc>
{
_Impl(_Smart& __s, _Del __d, _Alloc __a = _Alloc())
: _M_smart(__s)
{
// We know shared_ptr cannot be used with inout_ptr_t
// so we can do all set up here, instead of in _M_out_init().
_M_smart.reset();
// Similar to the shared_ptr(Y*, D, A) constructor, except that if
// the allocation throws we do not need (or want) to call deleter.
typename _Scd::__allocator_type __a2(__a);
auto __mem = __a2.allocate(1);
::new (__mem) _Scd(nullptr, std::forward<_Del>(__d),
std::forward<_Alloc>(__a));
_M_smart._M_refcount._M_pi = __mem;
}
_Pointer*
_M_get() const noexcept
{ return __builtin_addressof(_M_smart._M_ptr); }
~_Impl()
{
auto& __pi = _M_smart._M_refcount._M_pi;
if (_Sp __ptr = _M_smart.get())
static_cast<_Scd*>(__pi)->_M_impl._M_ptr = __ptr;
else // Destroy the control block manually without invoking deleter.
std::__exchange(__pi, nullptr)->_M_destroy();
}
_Smart& _M_smart;
using _Sp = typename _Smart::element_type*;
using _Scd = _Sp_counted_deleter<_Sp, decay_t<_Del>,
remove_cvref_t<_Alloc>,
__default_lock_policy>;
};
// Partial specialization for std::shared_ptr, without custom allocator.
template<typename _Tp, typename _Del>
requires (is_base_of_v<__shared_ptr<_Tp>, shared_ptr<_Tp>>)
struct _Impl<shared_ptr<_Tp>,
typename shared_ptr<_Tp>::element_type*, _Del>
: _Impl<_Smart, _Pointer, _Del, allocator<void>>
{
using _Impl<_Smart, _Pointer, _Del, allocator<void>>::_Impl;
};
#endif
using _Impl_t = _Impl<_Smart, _Pointer, _Args...>;
_Impl_t _M_impl;
template<typename, typename, typename...> friend class inout_ptr_t;
};
/// Smart pointer adaptor for functions taking an inout pointer parameter.
/**
* @tparam _Smart The type of pointer to adapt.
* @tparam _Pointer The type of pointer to convert to.
* @tparam _Args... Argument types used when resetting the smart pointer.
* @since C++23
* @headerfile <memory>
*/
template<typename _Smart, typename _Pointer, typename... _Args>
class inout_ptr_t
{
#if _GLIBCXX_HOSTED
static_assert(!__is_shared_ptr<_Smart>,
"std::inout_ptr can not be used to wrap std::shared_ptr");
#endif
public:
explicit
inout_ptr_t(_Smart& __smart, _Args... __args)
: _M_impl{__smart, std::forward<_Args>(__args)...}
{
if constexpr (requires { _M_impl._M_inout_init(); })
_M_impl._M_inout_init();
}
inout_ptr_t(const inout_ptr_t&) = delete;
~inout_ptr_t() = default;
operator _Pointer*() const noexcept
{ return _M_impl._M_get(); }
operator void**() const noexcept requires (!same_as<_Pointer, void*>)
{
static_assert(is_pointer_v<_Pointer>);
_Pointer* __p = *this;
return static_cast<void**>(static_cast<void*>(__p));
}
private:
#if _GLIBCXX_HOSTED
// Avoid an invalid instantiation of out_ptr_t<shared_ptr<T>, ...>
using _Out_ptr_t
= __conditional_t<__is_shared_ptr<_Smart>,
out_ptr_t<void*, void*>,
out_ptr_t<_Smart, _Pointer, _Args...>>;
#else
using _Out_ptr_t = out_ptr_t<_Smart, _Pointer, _Args...>;
#endif
using _Impl_t = typename _Out_ptr_t::_Impl_t;
_Impl_t _M_impl;
};
/// @cond undocumented
namespace __detail
{
// POINTER_OF metafunction
template<typename _Tp>
consteval auto
__pointer_of()
{
if constexpr (requires { typename _Tp::pointer; })
return type_identity<typename _Tp::pointer>{};
else if constexpr (requires { typename _Tp::element_type; })
return type_identity<typename _Tp::element_type*>{};
else
{
using _Traits = pointer_traits<_Tp>;
if constexpr (requires { typename _Traits::element_type; })
return type_identity<typename _Traits::element_type*>{};
}
// else POINTER_OF(S) is not a valid type, return void.
}
// POINTER_OF_OR metafunction
template<typename _Smart, typename _Ptr>
consteval auto
__pointer_of_or()
{
using _TypeId = decltype(__detail::__pointer_of<_Smart>());
if constexpr (is_void_v<_TypeId>)
return type_identity<_Ptr>{};
else
return _TypeId{};
}
// Returns Pointer if !is_void_v<Pointer>, otherwise POINTER_OF(Smart).
template<typename _Ptr, typename _Smart>
consteval auto
__choose_ptr()
{
if constexpr (!is_void_v<_Ptr>)
return type_identity<_Ptr>{};
else
return __detail::__pointer_of<_Smart>();
}
template<typename _Smart, typename _Sp, typename... _Args>
concept __resettable = requires (_Smart& __s) {
__s.reset(std::declval<_Sp>(), std::declval<_Args>()...);
};
}
/// @endcond
/// Adapt a smart pointer for functions taking an output pointer parameter.
/**
* @tparam _Pointer The type of pointer to convert to.
* @param __s The pointer that should take ownership of the result.
* @param __args... Arguments to use when resetting the smart pointer.
* @return A std::inout_ptr_t referring to `__s`.
* @since C++23
* @headerfile <memory>
*/
template<typename _Pointer = void, typename _Smart, typename... _Args>
inline auto
out_ptr(_Smart& __s, _Args&&... __args)
{
using _TypeId = decltype(__detail::__choose_ptr<_Pointer, _Smart>());
static_assert(!is_void_v<_TypeId>, "first argument to std::out_ptr "
"must be a pointer-like type");
using _Ret = out_ptr_t<_Smart, typename _TypeId::type, _Args&&...>;
return _Ret(__s, std::forward<_Args>(__args)...);
}
/// Adapt a smart pointer for functions taking an inout pointer parameter.
/**
* @tparam _Pointer The type of pointer to convert to.
* @param __s The pointer that should take ownership of the result.
* @param __args... Arguments to use when resetting the smart pointer.
* @return A std::inout_ptr_t referring to `__s`.
* @since C++23
* @headerfile <memory>
*/
template<typename _Pointer = void, typename _Smart, typename... _Args>
inline auto
inout_ptr(_Smart& __s, _Args&&... __args)
{
using _TypeId = decltype(__detail::__choose_ptr<_Pointer, _Smart>());
static_assert(!is_void_v<_TypeId>, "first argument to std::inout_ptr "
"must be a pointer-like type");
using _Ret = inout_ptr_t<_Smart, typename _TypeId::type, _Args&&...>;
return _Ret(__s, std::forward<_Args>(__args)...);
}
/// @cond undocumented
template<typename _Smart, typename _Pointer, typename... _Args>
template<typename _Smart2, typename _Pointer2, typename... _Args2>
inline
out_ptr_t<_Smart, _Pointer, _Args...>::
_Impl<_Smart2, _Pointer2, _Args2...>::~_Impl()
{
using _TypeId = decltype(__detail::__pointer_of_or<_Smart, _Pointer>());
using _Sp = typename _TypeId::type;
if (!_M_ptr)
return;
_Smart& __s = _M_smart;
_Pointer& __p = _M_ptr;
auto __reset = [&](auto&&... __args) {
if constexpr (__detail::__resettable<_Smart, _Sp, _Args...>)
__s.reset(static_cast<_Sp>(__p), std::forward<_Args>(__args)...);
else if constexpr (is_constructible_v<_Smart, _Sp, _Args...>)
__s = _Smart(static_cast<_Sp>(__p), std::forward<_Args>(__args)...);
else
static_assert(is_constructible_v<_Smart, _Sp, _Args...>);
};
if constexpr (sizeof...(_Args) >= 2)
std::apply(__reset, std::move(_M_args));
else if constexpr (sizeof...(_Args) == 1)
__reset(std::get<0>(std::move(_M_args)));
else
__reset();
}
/// @endcond
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace
#endif // __glibcxx_out_ptr
#endif /* _GLIBCXX_OUT_PTR_H */