|
| 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-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +// This header is unguarded on purpose. This header is an implementation detail of move_only_function.h |
| 10 | +// and generates multiple versions of std::move_only_function |
| 11 | + |
| 12 | +#include <__assert> |
| 13 | +#include <__config> |
| 14 | +#include <__cstddef/nullptr_t.h> |
| 15 | +#include <__cstddef/size_t.h> |
| 16 | +#include <__functional/invoke.h> |
| 17 | +#include <__functional/move_only_function_common.h> |
| 18 | +#include <__memory/addressof.h> |
| 19 | +#include <__memory/construct_at.h> |
| 20 | +#include <__type_traits/decay.h> |
| 21 | +#include <__type_traits/invoke.h> |
| 22 | +#include <__type_traits/is_constructible.h> |
| 23 | +#include <__type_traits/is_function.h> |
| 24 | +#include <__type_traits/is_member_pointer.h> |
| 25 | +#include <__type_traits/is_pointer.h> |
| 26 | +#include <__type_traits/is_same.h> |
| 27 | +#include <__type_traits/is_trivially_destructible.h> |
| 28 | +#include <__type_traits/remove_cvref.h> |
| 29 | +#include <__type_traits/remove_pointer.h> |
| 30 | +#include <__utility/exchange.h> |
| 31 | +#include <__utility/forward.h> |
| 32 | +#include <__utility/in_place.h> |
| 33 | +#include <__utility/move.h> |
| 34 | +#include <__utility/small_buffer.h> |
| 35 | +#include <__utility/swap.h> |
| 36 | +#include <initializer_list> |
| 37 | + |
| 38 | +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 39 | +# pragma GCC system_header |
| 40 | +#endif |
| 41 | + |
| 42 | +#ifndef _LIBCPP_IN_MOVE_ONLY_FUNCTION_H |
| 43 | +# error This header should only be included from move_only_function.h |
| 44 | +#endif |
| 45 | + |
| 46 | +#ifndef _LIBCPP_MOVE_ONLY_FUNCTION_CV |
| 47 | +# define _LIBCPP_MOVE_ONLY_FUNCTION_CV |
| 48 | +#endif |
| 49 | + |
| 50 | +#ifndef _LIBCPP_MOVE_ONLY_FUNCTION_REF |
| 51 | +# define _LIBCPP_MOVE_ONLY_FUNCTION_REF |
| 52 | +# define _LIBCPP_MOVE_ONLY_FUNCTION_INVOKE_QUALS _LIBCPP_MOVE_ONLY_FUNCTION_CV& |
| 53 | +#else |
| 54 | +# define _LIBCPP_MOVE_ONLY_FUNCTION_INVOKE_QUALS _LIBCPP_MOVE_ONLY_FUNCTION_CV _LIBCPP_MOVE_ONLY_FUNCTION_REF |
| 55 | +#endif |
| 56 | + |
| 57 | +#define _LIBCPP_MOVE_ONLY_FUNCTION_CVREF _LIBCPP_MOVE_ONLY_FUNCTION_CV _LIBCPP_MOVE_ONLY_FUNCTION_REF |
| 58 | + |
| 59 | +_LIBCPP_PUSH_MACROS |
| 60 | +#include <__undef_macros> |
| 61 | + |
| 62 | +_LIBCPP_BEGIN_NAMESPACE_STD |
| 63 | + |
| 64 | +template <class...> |
| 65 | +class move_only_function; |
| 66 | + |
| 67 | +template <class _ReturnT, class... _ArgTypes, bool __is_noexcept> |
| 68 | +class [[_Clang::__trivial_abi__]] |
| 69 | +move_only_function<_ReturnT(_ArgTypes...) _LIBCPP_MOVE_ONLY_FUNCTION_CVREF noexcept(__is_noexcept)> { |
| 70 | +private: |
| 71 | + static constexpr size_t __buffer_size_ = 3 * sizeof(void*); |
| 72 | + static constexpr size_t __buffer_alignment_ = alignof(void*); |
| 73 | + using _BufferT _LIBCPP_NODEBUG = __small_buffer<__buffer_size_, __buffer_alignment_>; |
| 74 | + |
| 75 | + using _VTable _LIBCPP_NODEBUG = _MoveOnlyFunctionVTable<_BufferT, _ReturnT, _ArgTypes...>; |
| 76 | + |
| 77 | + template <class _Functor> |
| 78 | + static constexpr _VTable __vtable_var_ = { |
| 79 | + .__call_ = [](_BufferT& __buffer, _ArgTypes... __args) noexcept(__is_noexcept) -> _ReturnT { |
| 80 | + return std::invoke_r<_ReturnT>( |
| 81 | + static_cast<_Functor _LIBCPP_MOVE_ONLY_FUNCTION_INVOKE_QUALS>(*__buffer.__get<_Functor>()), |
| 82 | + std::forward<_ArgTypes>(__args)...); |
| 83 | + }, |
| 84 | + .__destroy_ = (_BufferT::__fits_in_buffer<_Functor> && is_trivially_destructible_v<_Functor>) |
| 85 | + ? nullptr |
| 86 | + : [](_BufferT& __buffer) noexcept -> void { |
| 87 | + std::destroy_at(__buffer.__get<_Functor>()); |
| 88 | + __buffer.__dealloc<_Functor>(); |
| 89 | + }}; |
| 90 | + |
| 91 | + template <class _VT> |
| 92 | + static constexpr bool __is_callable_from = [] { |
| 93 | + using _DVT = decay_t<_VT>; |
| 94 | + if constexpr (__is_noexcept) { |
| 95 | + return is_nothrow_invocable_r_v<_ReturnT, _DVT _LIBCPP_MOVE_ONLY_FUNCTION_CVREF, _ArgTypes...> && |
| 96 | + is_nothrow_invocable_r_v<_ReturnT, _DVT _LIBCPP_MOVE_ONLY_FUNCTION_INVOKE_QUALS, _ArgTypes...>; |
| 97 | + } else { |
| 98 | + return is_invocable_r_v<_ReturnT, _DVT _LIBCPP_MOVE_ONLY_FUNCTION_CVREF, _ArgTypes...> && |
| 99 | + is_invocable_r_v<_ReturnT, _DVT _LIBCPP_MOVE_ONLY_FUNCTION_INVOKE_QUALS, _ArgTypes...>; |
| 100 | + } |
| 101 | + }(); |
| 102 | + |
| 103 | + template <class _Func, class... _Args> |
| 104 | + _LIBCPP_HIDE_FROM_ABI void __construct(_Args&&... __args) { |
| 105 | + static_assert(is_constructible_v<decay_t<_Func>, _Func>); |
| 106 | + |
| 107 | + using _StoredFunc = decay_t<_Func>; |
| 108 | + __vtable_ = std::addressof(__vtable_var_<_StoredFunc>); |
| 109 | + __buffer_.__construct<_StoredFunc>(std::forward<_Args>(__args)...); |
| 110 | + } |
| 111 | + |
| 112 | + _LIBCPP_HIDE_FROM_ABI void __reset() noexcept { |
| 113 | + if (__vtable_ && __vtable_->__destroy_) |
| 114 | + __vtable_->__destroy_(__buffer_); |
| 115 | + __vtable_ = nullptr; |
| 116 | + } |
| 117 | + |
| 118 | +public: |
| 119 | + using result_type = _ReturnT; |
| 120 | + |
| 121 | + // [func.wrap.move.ctor] |
| 122 | + move_only_function() noexcept = default; |
| 123 | + _LIBCPP_HIDE_FROM_ABI move_only_function(nullptr_t) noexcept {} |
| 124 | + _LIBCPP_HIDE_FROM_ABI move_only_function(move_only_function&& __other) noexcept |
| 125 | + : __vtable_(__other.__vtable_), __buffer_(std::move(__other.__buffer_)) { |
| 126 | + __other.__vtable_ = nullptr; |
| 127 | + } |
| 128 | + |
| 129 | + template <class _Func> |
| 130 | + requires(!is_same_v<remove_cvref_t<_Func>, move_only_function> && !__is_inplace_type<_Func>::value && |
| 131 | + __is_callable_from<_Func>) |
| 132 | + _LIBCPP_HIDE_FROM_ABI move_only_function(_Func&& __func) noexcept { |
| 133 | + using _StoredFunc = decay_t<_Func>; |
| 134 | + |
| 135 | + if constexpr ((is_pointer_v<_StoredFunc> && is_function_v<remove_pointer_t<_StoredFunc>>) || |
| 136 | + is_member_function_pointer_v<_StoredFunc>) { |
| 137 | + if (__func != nullptr) { |
| 138 | + __vtable_ = std::addressof(__vtable_var_<_StoredFunc>); |
| 139 | + static_assert(_BufferT::__fits_in_buffer<_StoredFunc>); |
| 140 | + __buffer_.__construct<_StoredFunc>(std::forward<_Func>(__func)); |
| 141 | + } |
| 142 | + } else if constexpr (__is_move_only_function_v<_StoredFunc>) { |
| 143 | + if (__func) { |
| 144 | + __vtable_ = std::exchange(__func.__vtable_, nullptr); |
| 145 | + __buffer_ = std::move(__func.__buffer_); |
| 146 | + } |
| 147 | + } else { |
| 148 | + __construct<_Func>(std::forward<_Func>(__func)); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + template <class _Func, class... _Args> |
| 153 | + requires is_constructible_v<decay_t<_Func>, _Args...> && __is_callable_from<_Func> |
| 154 | + _LIBCPP_HIDE_FROM_ABI explicit move_only_function(in_place_type_t<_Func>, _Args&&... __args) { |
| 155 | + static_assert(is_same_v<decay_t<_Func>, _Func>); |
| 156 | + __construct<_Func>(std::forward<_Args>(__args)...); |
| 157 | + } |
| 158 | + |
| 159 | + template <class _Func, class _InitListType, class... _Args> |
| 160 | + requires is_constructible_v<decay_t<_Func>, initializer_list<_InitListType>&, _Args...> && __is_callable_from<_Func> |
| 161 | + _LIBCPP_HIDE_FROM_ABI explicit move_only_function( |
| 162 | + in_place_type_t<_Func>, initializer_list<_InitListType> __il, _Args&&... __args) { |
| 163 | + static_assert(is_same_v<decay_t<_Func>, _Func>); |
| 164 | + __construct<_Func>(__il, std::forward<_Args>(__args)...); |
| 165 | + } |
| 166 | + |
| 167 | + _LIBCPP_HIDE_FROM_ABI move_only_function& operator=(move_only_function&& __other) noexcept { |
| 168 | + move_only_function(std::move(__other)).swap(*this); |
| 169 | + return *this; |
| 170 | + } |
| 171 | + |
| 172 | + _LIBCPP_HIDE_FROM_ABI move_only_function& operator=(nullptr_t) noexcept { |
| 173 | + __reset(); |
| 174 | + return *this; |
| 175 | + } |
| 176 | + |
| 177 | + template <class _Func> |
| 178 | + requires(!is_same_v<remove_cvref_t<_Func>, move_only_function> && !__is_inplace_type<_Func>::value && |
| 179 | + __is_callable_from<_Func>) |
| 180 | + _LIBCPP_HIDE_FROM_ABI move_only_function& operator=(_Func&& __func) { |
| 181 | + move_only_function(std::forward<_Func>(__func)).swap(*this); |
| 182 | + return *this; |
| 183 | + } |
| 184 | + |
| 185 | + _LIBCPP_HIDE_FROM_ABI ~move_only_function() { __reset(); } |
| 186 | + |
| 187 | + // [func.wrap.move.inv] |
| 188 | + _LIBCPP_HIDE_FROM_ABI explicit operator bool() const noexcept { return __vtable_; } |
| 189 | + |
| 190 | + _LIBCPP_HIDE_FROM_ABI _ReturnT operator()(_ArgTypes... __args) _LIBCPP_MOVE_ONLY_FUNCTION_CVREF |
| 191 | + noexcept(__is_noexcept) { |
| 192 | + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(static_cast<bool>(*this), "Tried to call a disengaged move_only_function"); |
| 193 | + const auto __call = static_cast<_ReturnT (*)(_BufferT&, _ArgTypes...)>(__vtable_->__call_); |
| 194 | + return __call(__buffer_, std::forward<_ArgTypes>(__args)...); |
| 195 | + } |
| 196 | + |
| 197 | + // [func.wrap.move.util] |
| 198 | + _LIBCPP_HIDE_FROM_ABI void swap(move_only_function& __other) noexcept { |
| 199 | + std::swap(__vtable_, __other.__vtable_); |
| 200 | + std::swap(__buffer_, __other.__buffer_); |
| 201 | + } |
| 202 | + |
| 203 | + _LIBCPP_HIDE_FROM_ABI friend void swap(move_only_function& __lhs, move_only_function& __rhs) noexcept { |
| 204 | + __lhs.swap(__rhs); |
| 205 | + } |
| 206 | + |
| 207 | + _LIBCPP_HIDE_FROM_ABI friend bool operator==(const move_only_function& __func, nullptr_t) noexcept { return !__func; } |
| 208 | + |
| 209 | +private: |
| 210 | + const _VTable* __vtable_ = nullptr; |
| 211 | + mutable _BufferT __buffer_; |
| 212 | + |
| 213 | + template <class...> |
| 214 | + friend class move_only_function; |
| 215 | +}; |
| 216 | + |
| 217 | +#undef _LIBCPP_MOVE_ONLY_FUNCTION_CV |
| 218 | +#undef _LIBCPP_MOVE_ONLY_FUNCTION_REF |
| 219 | +#undef _LIBCPP_MOVE_ONLY_FUNCTION_INVOKE_QUALS |
| 220 | +#undef _LIBCPP_MOVE_ONLY_FUNCTION_CVREF |
| 221 | + |
| 222 | +_LIBCPP_END_NAMESPACE_STD |
| 223 | + |
| 224 | +_LIBCPP_POP_MACROS |
0 commit comments