
This change moves the definitions of feature test macros (or strictly speaking, the requests for <bits/version.h> to define them) so that only standard headers define them. For example, <bits/shared_ptr.h> will no longer define macros related to std::shared_ptr, only <memory> and <version> will define them. This means that __cpp_lib_shared_ptr_arrays will not be defined by <future> or by other headers that include <bits/shared_ptr.h>. It will only be defined when <memory> has been included. This will discourage users from relying on transitive includes. As a result, internal headers that need to query the macros should use the internal macros like __glibcxx_shared_ptr_arrays instead of __cpp_lib_shared_ptr_arrays, as those internal macros are defined by the internal headers after icluding <bits/version.h>. There are some exceptions to this rule, because __cpp_lib_is_constant_evaluated is defined by bits/c++config.h and so is available everywhere, and __cpp_lib_three_way_comparison is defined by <compare> which several headers are explicitly specified to include, so its macro is guaranteed to be usable too. N.B. not many internal headers actually need an explicit include of <bits/version.h>, because most of them include <type_traits> and so get all the __glibcxx_foo internal macros from there. libstdc++-v3/ChangeLog: * include/bits/algorithmfwd.h: Do not define standard feature test macro here. * include/bits/align.h: Likewise. Test internal macros instead of standard macros. * include/bits/alloc_traits.h: Likewise. * include/bits/allocator.h: Likewise. * include/bits/atomic_base.h: Likewise. * include/bits/atomic_timed_wait.h: Likewise. * include/bits/atomic_wait.h: Likewise. * include/bits/basic_string.h: Likewise. * include/bits/basic_string.tcc: Likewise. * include/bits/char_traits.h: Likewise. * include/bits/chrono.h: Likewise. * include/bits/cow_string.h: Likewise. * include/bits/forward_list.h: Likewise. * include/bits/hashtable.h: Likewise. * include/bits/ios_base.h: Likewise. * include/bits/memory_resource.h: Likewise. * include/bits/move.h: Likewise. * include/bits/move_only_function.h: Likewise. * include/bits/node_handle.h: Likewise. * include/bits/ptr_traits.h: Likewise. * include/bits/range_access.h: Likewise. * include/bits/ranges_algo.h: Likewise. * include/bits/ranges_cmp.h: Likewise. * include/bits/ranges_util.h: Likewise. * include/bits/semaphore_base.h: Likewise. * include/bits/shared_ptr.h: Likewise. * include/bits/shared_ptr_atomic.h: Likewise. * include/bits/shared_ptr_base.h: Likewise. * include/bits/stl_algo.h: Likewise. * include/bits/stl_algobase.h: Likewise. * include/bits/stl_function.h: Likewise. * include/bits/stl_iterator.h: Likewise. * include/bits/stl_list.h: Likewise. * include/bits/stl_map.h: Likewise. * include/bits/stl_pair.h: Likewise. * include/bits/stl_queue.h: Likewise. * include/bits/stl_stack.h: Likewise. * include/bits/stl_tree.h: Likewise. * include/bits/stl_uninitialized.h: Likewise. * include/bits/stl_vector.h: Likewise. * include/bits/unique_ptr.h: Likewise. * include/bits/unordered_map.h: Likewise. * include/bits/uses_allocator_args.h: Likewise. * include/bits/utility.h: Likewise. * include/bits/erase_if.h: Add comment. * include/std/algorithm: Define standard feature test macros here. * include/std/atomic: Likewise. * include/std/array: Likewise. * include/std/chrono: Likewise. * include/std/condition_variable: Likewise. * include/std/deque: Likewise. * include/std/format: Likewise. * include/std/functional: Likewise. * include/std/forward_list: Likewise. * include/std/ios: Likewise. * include/std/iterator: Likewise. * include/std/list: Likewise. * include/std/map: Likewise. * include/std/memory: Likewise. * include/std/numeric: Likewise. * include/std/queue: Likewise. * include/std/ranges: Likewise. * include/std/regex: Likewise. * include/std/set: Likewise. * include/std/stack: Likewise. * include/std/stop_token: Likewise. * include/std/string: Likewise. * include/std/string_view: * include/std/tuple: Likewise. * include/std/unordered_map: * include/std/unordered_set: * include/std/utility: Likewise. * include/std/vector: Likewise. * include/std/scoped_allocator: Query internal macros instead of standard macros.
216 lines
6.2 KiB
C++
216 lines
6.2 KiB
C++
// Implementation of std::move_only_function -*- 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/move_only_function.h
|
|
* This is an internal header file, included by other library headers.
|
|
* Do not attempt to use it directly. @headername{functional}
|
|
*/
|
|
|
|
#ifndef _GLIBCXX_MOVE_ONLY_FUNCTION_H
|
|
#define _GLIBCXX_MOVE_ONLY_FUNCTION_H 1
|
|
|
|
#pragma GCC system_header
|
|
|
|
#include <bits/version.h>
|
|
|
|
#ifdef __glibcxx_move_only_function // C++ >= 23 && HOSTED
|
|
|
|
#include <bits/invoke.h>
|
|
#include <bits/utility.h>
|
|
|
|
namespace std _GLIBCXX_VISIBILITY(default)
|
|
{
|
|
_GLIBCXX_BEGIN_NAMESPACE_VERSION
|
|
|
|
template<typename... _Signature>
|
|
class move_only_function; // not defined
|
|
|
|
/// @cond undocumented
|
|
class _Mofunc_base
|
|
{
|
|
protected:
|
|
_Mofunc_base() noexcept
|
|
: _M_manage(_S_empty)
|
|
{ }
|
|
|
|
_Mofunc_base(_Mofunc_base&& __x) noexcept
|
|
{
|
|
_M_manage = std::__exchange(__x._M_manage, _S_empty);
|
|
_M_manage(_M_storage, &__x._M_storage);
|
|
}
|
|
|
|
template<typename _Tp, typename... _Args>
|
|
static constexpr bool
|
|
_S_nothrow_init() noexcept
|
|
{
|
|
if constexpr (__stored_locally<_Tp>)
|
|
return is_nothrow_constructible_v<_Tp, _Args...>;
|
|
return false;
|
|
}
|
|
|
|
template<typename _Tp, typename... _Args>
|
|
void
|
|
_M_init(_Args&&... __args) noexcept(_S_nothrow_init<_Tp, _Args...>())
|
|
{
|
|
if constexpr (__stored_locally<_Tp>)
|
|
::new (_M_storage._M_addr()) _Tp(std::forward<_Args>(__args)...);
|
|
else
|
|
_M_storage._M_p = new _Tp(std::forward<_Args>(__args)...);
|
|
|
|
_M_manage = &_S_manage<_Tp>;
|
|
}
|
|
|
|
_Mofunc_base&
|
|
operator=(_Mofunc_base&& __x) noexcept
|
|
{
|
|
_M_manage(_M_storage, nullptr);
|
|
_M_manage = std::__exchange(__x._M_manage, _S_empty);
|
|
_M_manage(_M_storage, &__x._M_storage);
|
|
return *this;
|
|
}
|
|
|
|
_Mofunc_base&
|
|
operator=(nullptr_t) noexcept
|
|
{
|
|
_M_manage(_M_storage, nullptr);
|
|
_M_manage = _S_empty;
|
|
return *this;
|
|
}
|
|
|
|
~_Mofunc_base() { _M_manage(_M_storage, nullptr); }
|
|
|
|
void
|
|
swap(_Mofunc_base& __x) noexcept
|
|
{
|
|
// Order of operations here is more efficient if __x is empty.
|
|
_Storage __s;
|
|
__x._M_manage(__s, &__x._M_storage);
|
|
_M_manage(__x._M_storage, &_M_storage);
|
|
__x._M_manage(_M_storage, &__s);
|
|
std::swap(_M_manage, __x._M_manage);
|
|
}
|
|
|
|
template<typename _Tp, typename _Self>
|
|
static _Tp*
|
|
_S_access(_Self* __self) noexcept
|
|
{
|
|
if constexpr (__stored_locally<remove_const_t<_Tp>>)
|
|
return static_cast<_Tp*>(__self->_M_storage._M_addr());
|
|
else
|
|
return static_cast<_Tp*>(__self->_M_storage._M_p);
|
|
}
|
|
|
|
private:
|
|
struct _Storage
|
|
{
|
|
void* _M_addr() noexcept { return &_M_bytes[0]; }
|
|
const void* _M_addr() const noexcept { return &_M_bytes[0]; }
|
|
|
|
// We want to have enough space to store a simple delegate type.
|
|
struct _Delegate { void (_Storage::*__pfm)(); _Storage* __obj; };
|
|
union {
|
|
void* _M_p;
|
|
alignas(_Delegate) alignas(void(*)())
|
|
unsigned char _M_bytes[sizeof(_Delegate)];
|
|
};
|
|
};
|
|
|
|
template<typename _Tp>
|
|
static constexpr bool __stored_locally
|
|
= sizeof(_Tp) <= sizeof(_Storage) && alignof(_Tp) <= alignof(_Storage)
|
|
&& is_nothrow_move_constructible_v<_Tp>;
|
|
|
|
// A function that either destroys the target object stored in __target,
|
|
// or moves the target object from *__src to __target.
|
|
using _Manager = void (*)(_Storage& __target, _Storage* __src) noexcept;
|
|
|
|
// The no-op manager function for objects with no target.
|
|
static void _S_empty(_Storage&, _Storage*) noexcept { }
|
|
|
|
// The real manager function for a target object of type _Tp.
|
|
template<typename _Tp>
|
|
static void
|
|
_S_manage(_Storage& __target, _Storage* __src) noexcept
|
|
{
|
|
if constexpr (__stored_locally<_Tp>)
|
|
{
|
|
if (__src)
|
|
{
|
|
_Tp* __rval = static_cast<_Tp*>(__src->_M_addr());
|
|
::new (__target._M_addr()) _Tp(std::move(*__rval));
|
|
__rval->~_Tp();
|
|
}
|
|
else
|
|
static_cast<_Tp*>(__target._M_addr())->~_Tp();
|
|
}
|
|
else
|
|
{
|
|
if (__src)
|
|
__target._M_p = __src->_M_p;
|
|
else
|
|
delete static_cast<_Tp*>(__target._M_p);
|
|
}
|
|
}
|
|
|
|
_Storage _M_storage;
|
|
_Manager _M_manage;
|
|
};
|
|
|
|
template<typename _Tp>
|
|
inline constexpr bool __is_move_only_function_v = false;
|
|
template<typename _Tp>
|
|
constexpr bool __is_move_only_function_v<move_only_function<_Tp>> = true;
|
|
/// @endcond
|
|
|
|
namespace __detail::__variant
|
|
{
|
|
template<typename> struct _Never_valueless_alt; // see <variant>
|
|
|
|
// Provide the strong exception-safety guarantee when emplacing a
|
|
// move_only_function into a variant.
|
|
template<typename... _Signature>
|
|
struct _Never_valueless_alt<std::move_only_function<_Signature...>>
|
|
: true_type
|
|
{ };
|
|
} // namespace __detail::__variant
|
|
|
|
_GLIBCXX_END_NAMESPACE_VERSION
|
|
} // namespace std
|
|
|
|
#include "mofunc_impl.h"
|
|
#define _GLIBCXX_MOF_CV const
|
|
#include "mofunc_impl.h"
|
|
#define _GLIBCXX_MOF_REF &
|
|
#include "mofunc_impl.h"
|
|
#define _GLIBCXX_MOF_REF &&
|
|
#include "mofunc_impl.h"
|
|
#define _GLIBCXX_MOF_CV const
|
|
#define _GLIBCXX_MOF_REF &
|
|
#include "mofunc_impl.h"
|
|
#define _GLIBCXX_MOF_CV const
|
|
#define _GLIBCXX_MOF_REF &&
|
|
#include "mofunc_impl.h"
|
|
|
|
#endif // __glibcxx_move_only_function
|
|
#endif // _GLIBCXX_MOVE_ONLY_FUNCTION_H
|