any_invocable.h 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. // Copyright 2022 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // Implementation details for `absl::AnyInvocable`
  16. #ifndef ABSL_FUNCTIONAL_INTERNAL_ANY_INVOCABLE_H_
  17. #define ABSL_FUNCTIONAL_INTERNAL_ANY_INVOCABLE_H_
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // This implementation of the proposed `any_invocable` uses an approach that //
  21. // chooses between local storage and remote storage for the contained target //
  22. // object based on the target object's size, alignment requirements, and //
  23. // whether or not it has a nothrow move constructor. Additional optimizations //
  24. // are performed when the object is a trivially copyable type [basic.types]. //
  25. // //
  26. // There are three datamembers per `AnyInvocable` instance //
  27. // //
  28. // 1) A union containing either //
  29. // - A pointer to the target object referred to via a void*, or //
  30. // - the target object, emplaced into a raw char buffer //
  31. // //
  32. // 2) A function pointer to a "manager" function operation that takes a //
  33. // discriminator and logically branches to either perform a move operation //
  34. // or destroy operation based on that discriminator. //
  35. // //
  36. // 3) A function pointer to an "invoker" function operation that invokes the //
  37. // target object, directly returning the result. //
  38. // //
  39. // When in the logically empty state, the manager function is an empty //
  40. // function and the invoker function is one that would be undefined-behavior //
  41. // to call. //
  42. // //
  43. // An additional optimization is performed when converting from one //
  44. // AnyInvocable to another where only the noexcept specification and/or the //
  45. // cv/ref qualifiers of the function type differ. In these cases, the //
  46. // conversion works by "moving the guts", similar to if they were the same //
  47. // exact type, as opposed to having to perform an additional layer of //
  48. // wrapping through remote storage. //
  49. // //
  50. ////////////////////////////////////////////////////////////////////////////////
  51. // IWYU pragma: private, include "absl/functional/any_invocable.h"
  52. #include <cassert>
  53. #include <cstddef>
  54. #include <cstring>
  55. #include <exception>
  56. #include <functional>
  57. #include <initializer_list>
  58. #include <memory>
  59. #include <new>
  60. #include <type_traits>
  61. #include <utility>
  62. #include "absl/base/config.h"
  63. #include "absl/base/internal/invoke.h"
  64. #include "absl/base/macros.h"
  65. #include "absl/base/optimization.h"
  66. #include "absl/meta/type_traits.h"
  67. #include "absl/utility/utility.h"
  68. namespace absl {
  69. ABSL_NAMESPACE_BEGIN
  70. // Helper macro used to prevent spelling `noexcept` in language versions older
  71. // than C++17, where it is not part of the type system, in order to avoid
  72. // compilation failures and internal compiler errors.
  73. #if ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L
  74. #define ABSL_INTERNAL_NOEXCEPT_SPEC(noex) noexcept(noex)
  75. #else
  76. #define ABSL_INTERNAL_NOEXCEPT_SPEC(noex)
  77. #endif
  78. // Defined in functional/any_invocable.h
  79. template <class Sig>
  80. class AnyInvocable;
  81. namespace internal_any_invocable {
  82. // Constants relating to the small-object-storage for AnyInvocable
  83. enum StorageProperty : std::size_t {
  84. kAlignment = alignof(std::max_align_t), // The alignment of the storage
  85. kStorageSize = sizeof(void*) * 2 // The size of the storage
  86. };
  87. ////////////////////////////////////////////////////////////////////////////////
  88. //
  89. // A metafunction for checking if a type is an AnyInvocable instantiation.
  90. // This is used during conversion operations.
  91. template <class T>
  92. struct IsAnyInvocable : std::false_type {};
  93. template <class Sig>
  94. struct IsAnyInvocable<AnyInvocable<Sig>> : std::true_type {};
  95. //
  96. ////////////////////////////////////////////////////////////////////////////////
  97. // A type trait that tells us whether or not a target function type should be
  98. // stored locally in the small object optimization storage
  99. template <class T>
  100. using IsStoredLocally = std::integral_constant<
  101. bool, sizeof(T) <= kStorageSize && alignof(T) <= kAlignment &&
  102. kAlignment % alignof(T) == 0 &&
  103. std::is_nothrow_move_constructible<T>::value>;
  104. // An implementation of std::remove_cvref_t of C++20.
  105. template <class T>
  106. using RemoveCVRef =
  107. typename std::remove_cv<typename std::remove_reference<T>::type>::type;
  108. ////////////////////////////////////////////////////////////////////////////////
  109. //
  110. // An implementation of the C++ standard INVOKE<R> pseudo-macro, operation is
  111. // equivalent to std::invoke except that it forces an implicit conversion to the
  112. // specified return type. If "R" is void, the function is executed and the
  113. // return value is simply ignored.
  114. template <class ReturnType, class F, class... P,
  115. typename = absl::enable_if_t<std::is_void<ReturnType>::value>>
  116. void InvokeR(F&& f, P&&... args) {
  117. absl::base_internal::invoke(std::forward<F>(f), std::forward<P>(args)...);
  118. }
  119. template <class ReturnType, class F, class... P,
  120. absl::enable_if_t<!std::is_void<ReturnType>::value, int> = 0>
  121. ReturnType InvokeR(F&& f, P&&... args) {
  122. // GCC 12 has a false-positive -Wmaybe-uninitialized warning here.
  123. #if ABSL_INTERNAL_HAVE_MIN_GNUC_VERSION(12, 0)
  124. #pragma GCC diagnostic push
  125. #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
  126. #endif
  127. return absl::base_internal::invoke(std::forward<F>(f),
  128. std::forward<P>(args)...);
  129. #if ABSL_INTERNAL_HAVE_MIN_GNUC_VERSION(12, 0)
  130. #pragma GCC diagnostic pop
  131. #endif
  132. }
  133. //
  134. ////////////////////////////////////////////////////////////////////////////////
  135. ////////////////////////////////////////////////////////////////////////////////
  136. ///
  137. // A metafunction that takes a "T" corresponding to a parameter type of the
  138. // user's specified function type, and yields the parameter type to use for the
  139. // type-erased invoker. In order to prevent observable moves, this must be
  140. // either a reference or, if the type is trivial, the original parameter type
  141. // itself. Since the parameter type may be incomplete at the point that this
  142. // metafunction is used, we can only do this optimization for scalar types
  143. // rather than for any trivial type.
  144. template <typename T>
  145. T ForwardImpl(std::true_type);
  146. template <typename T>
  147. T&& ForwardImpl(std::false_type);
  148. // NOTE: We deliberately use an intermediate struct instead of a direct alias,
  149. // as a workaround for b/206991861 on MSVC versions < 1924.
  150. template <class T>
  151. struct ForwardedParameter {
  152. using type = decltype((
  153. ForwardImpl<T>)(std::integral_constant<bool,
  154. std::is_scalar<T>::value>()));
  155. };
  156. template <class T>
  157. using ForwardedParameterType = typename ForwardedParameter<T>::type;
  158. //
  159. ////////////////////////////////////////////////////////////////////////////////
  160. // A discriminator when calling the "manager" function that describes operation
  161. // type-erased operation should be invoked.
  162. //
  163. // "relocate_from_to" specifies that the manager should perform a move.
  164. //
  165. // "dispose" specifies that the manager should perform a destroy.
  166. enum class FunctionToCall : bool { relocate_from_to, dispose };
  167. // The portion of `AnyInvocable` state that contains either a pointer to the
  168. // target object or the object itself in local storage
  169. union TypeErasedState {
  170. struct {
  171. // A pointer to the type-erased object when remotely stored
  172. void* target;
  173. // The size of the object for `RemoteManagerTrivial`
  174. std::size_t size;
  175. } remote;
  176. // Local-storage for the type-erased object when small and trivial enough
  177. alignas(kAlignment) char storage[kStorageSize];
  178. };
  179. // A typed accessor for the object in `TypeErasedState` storage
  180. template <class T>
  181. T& ObjectInLocalStorage(TypeErasedState* const state) {
  182. // We launder here because the storage may be reused with the same type.
  183. #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606L
  184. return *std::launder(reinterpret_cast<T*>(&state->storage));
  185. #elif ABSL_HAVE_BUILTIN(__builtin_launder)
  186. return *__builtin_launder(reinterpret_cast<T*>(&state->storage));
  187. #else
  188. // When `std::launder` or equivalent are not available, we rely on undefined
  189. // behavior, which works as intended on Abseil's officially supported
  190. // platforms as of Q2 2022.
  191. #if !defined(__clang__) && defined(__GNUC__)
  192. #pragma GCC diagnostic push
  193. #pragma GCC diagnostic ignored "-Wstrict-aliasing"
  194. #endif
  195. return *reinterpret_cast<T*>(&state->storage);
  196. #if !defined(__clang__) && defined(__GNUC__)
  197. #pragma GCC diagnostic pop
  198. #endif
  199. #endif
  200. }
  201. // The type for functions issuing lifetime-related operations: move and dispose
  202. // A pointer to such a function is contained in each `AnyInvocable` instance.
  203. // NOTE: When specifying `FunctionToCall::`dispose, the same state must be
  204. // passed as both "from" and "to".
  205. using ManagerType = void(FunctionToCall /*operation*/,
  206. TypeErasedState* /*from*/, TypeErasedState* /*to*/)
  207. ABSL_INTERNAL_NOEXCEPT_SPEC(true);
  208. // The type for functions issuing the actual invocation of the object
  209. // A pointer to such a function is contained in each AnyInvocable instance.
  210. template <bool SigIsNoexcept, class ReturnType, class... P>
  211. using InvokerType = ReturnType(TypeErasedState*, ForwardedParameterType<P>...)
  212. ABSL_INTERNAL_NOEXCEPT_SPEC(SigIsNoexcept);
  213. // The manager that is used when AnyInvocable is empty
  214. inline void EmptyManager(FunctionToCall /*operation*/,
  215. TypeErasedState* /*from*/,
  216. TypeErasedState* /*to*/) noexcept {}
  217. // The manager that is used when a target function is in local storage and is
  218. // a trivially copyable type.
  219. inline void LocalManagerTrivial(FunctionToCall /*operation*/,
  220. TypeErasedState* const from,
  221. TypeErasedState* const to) noexcept {
  222. // This single statement without branching handles both possible operations.
  223. //
  224. // For FunctionToCall::dispose, "from" and "to" point to the same state, and
  225. // so this assignment logically would do nothing.
  226. //
  227. // Note: Correctness here relies on http://wg21.link/p0593, which has only
  228. // become standard in C++20, though implementations do not break it in
  229. // practice for earlier versions of C++.
  230. //
  231. // The correct way to do this without that paper is to first placement-new a
  232. // default-constructed T in "to->storage" prior to the memmove, but doing so
  233. // requires a different function to be created for each T that is stored
  234. // locally, which can cause unnecessary bloat and be less cache friendly.
  235. *to = *from;
  236. // Note: Because the type is trivially copyable, the destructor does not need
  237. // to be called ("trivially copyable" requires a trivial destructor).
  238. }
  239. // The manager that is used when a target function is in local storage and is
  240. // not a trivially copyable type.
  241. template <class T>
  242. void LocalManagerNontrivial(FunctionToCall operation,
  243. TypeErasedState* const from,
  244. TypeErasedState* const to) noexcept {
  245. static_assert(IsStoredLocally<T>::value,
  246. "Local storage must only be used for supported types.");
  247. static_assert(!std::is_trivially_copyable<T>::value,
  248. "Locally stored types must be trivially copyable.");
  249. T& from_object = (ObjectInLocalStorage<T>)(from);
  250. switch (operation) {
  251. case FunctionToCall::relocate_from_to:
  252. // NOTE: Requires that the left-hand operand is already empty.
  253. ::new (static_cast<void*>(&to->storage)) T(std::move(from_object));
  254. ABSL_FALLTHROUGH_INTENDED;
  255. case FunctionToCall::dispose:
  256. from_object.~T(); // Must not throw. // NOLINT
  257. return;
  258. }
  259. ABSL_UNREACHABLE();
  260. }
  261. // The invoker that is used when a target function is in local storage
  262. // Note: QualTRef here is the target function type along with cv and reference
  263. // qualifiers that must be used when calling the function.
  264. template <bool SigIsNoexcept, class ReturnType, class QualTRef, class... P>
  265. ReturnType LocalInvoker(
  266. TypeErasedState* const state,
  267. ForwardedParameterType<P>... args) noexcept(SigIsNoexcept) {
  268. using RawT = RemoveCVRef<QualTRef>;
  269. static_assert(
  270. IsStoredLocally<RawT>::value,
  271. "Target object must be in local storage in order to be invoked from it.");
  272. auto& f = (ObjectInLocalStorage<RawT>)(state);
  273. return (InvokeR<ReturnType>)(static_cast<QualTRef>(f),
  274. static_cast<ForwardedParameterType<P>>(args)...);
  275. }
  276. // The manager that is used when a target function is in remote storage and it
  277. // has a trivial destructor
  278. inline void RemoteManagerTrivial(FunctionToCall operation,
  279. TypeErasedState* const from,
  280. TypeErasedState* const to) noexcept {
  281. switch (operation) {
  282. case FunctionToCall::relocate_from_to:
  283. // NOTE: Requires that the left-hand operand is already empty.
  284. to->remote = from->remote;
  285. return;
  286. case FunctionToCall::dispose:
  287. #if defined(__cpp_sized_deallocation)
  288. ::operator delete(from->remote.target, from->remote.size);
  289. #else // __cpp_sized_deallocation
  290. ::operator delete(from->remote.target);
  291. #endif // __cpp_sized_deallocation
  292. return;
  293. }
  294. ABSL_UNREACHABLE();
  295. }
  296. // The manager that is used when a target function is in remote storage and the
  297. // destructor of the type is not trivial
  298. template <class T>
  299. void RemoteManagerNontrivial(FunctionToCall operation,
  300. TypeErasedState* const from,
  301. TypeErasedState* const to) noexcept {
  302. static_assert(!IsStoredLocally<T>::value,
  303. "Remote storage must only be used for types that do not "
  304. "qualify for local storage.");
  305. switch (operation) {
  306. case FunctionToCall::relocate_from_to:
  307. // NOTE: Requires that the left-hand operand is already empty.
  308. to->remote.target = from->remote.target;
  309. return;
  310. case FunctionToCall::dispose:
  311. ::delete static_cast<T*>(from->remote.target); // Must not throw.
  312. return;
  313. }
  314. ABSL_UNREACHABLE();
  315. }
  316. // The invoker that is used when a target function is in remote storage
  317. template <bool SigIsNoexcept, class ReturnType, class QualTRef, class... P>
  318. ReturnType RemoteInvoker(
  319. TypeErasedState* const state,
  320. ForwardedParameterType<P>... args) noexcept(SigIsNoexcept) {
  321. using RawT = RemoveCVRef<QualTRef>;
  322. static_assert(!IsStoredLocally<RawT>::value,
  323. "Target object must be in remote storage in order to be "
  324. "invoked from it.");
  325. auto& f = *static_cast<RawT*>(state->remote.target);
  326. return (InvokeR<ReturnType>)(static_cast<QualTRef>(f),
  327. static_cast<ForwardedParameterType<P>>(args)...);
  328. }
  329. ////////////////////////////////////////////////////////////////////////////////
  330. //
  331. // A metafunction that checks if a type T is an instantiation of
  332. // absl::in_place_type_t (needed for constructor constraints of AnyInvocable).
  333. template <class T>
  334. struct IsInPlaceType : std::false_type {};
  335. template <class T>
  336. struct IsInPlaceType<absl::in_place_type_t<T>> : std::true_type {};
  337. //
  338. ////////////////////////////////////////////////////////////////////////////////
  339. // A constructor name-tag used with CoreImpl (below) to request the
  340. // conversion-constructor. QualDecayedTRef is the decayed-type of the object to
  341. // wrap, along with the cv and reference qualifiers that must be applied when
  342. // performing an invocation of the wrapped object.
  343. template <class QualDecayedTRef>
  344. struct TypedConversionConstruct {};
  345. // A helper base class for all core operations of AnyInvocable. Most notably,
  346. // this class creates the function call operator and constraint-checkers so that
  347. // the top-level class does not have to be a series of partial specializations.
  348. //
  349. // Note: This definition exists (as opposed to being a declaration) so that if
  350. // the user of the top-level template accidentally passes a template argument
  351. // that is not a function type, they will get a static_assert in AnyInvocable's
  352. // class body rather than an error stating that Impl is not defined.
  353. template <class Sig>
  354. class Impl {}; // Note: This is partially-specialized later.
  355. // A std::unique_ptr deleter that deletes memory allocated via ::operator new.
  356. #if defined(__cpp_sized_deallocation)
  357. class TrivialDeleter {
  358. public:
  359. explicit TrivialDeleter(std::size_t size) : size_(size) {}
  360. void operator()(void* target) const {
  361. ::operator delete(target, size_);
  362. }
  363. private:
  364. std::size_t size_;
  365. };
  366. #else // __cpp_sized_deallocation
  367. class TrivialDeleter {
  368. public:
  369. explicit TrivialDeleter(std::size_t) {}
  370. void operator()(void* target) const { ::operator delete(target); }
  371. };
  372. #endif // __cpp_sized_deallocation
  373. template <bool SigIsNoexcept, class ReturnType, class... P>
  374. class CoreImpl;
  375. constexpr bool IsCompatibleConversion(void*, void*) { return false; }
  376. template <bool NoExceptSrc, bool NoExceptDest, class... T>
  377. constexpr bool IsCompatibleConversion(CoreImpl<NoExceptSrc, T...>*,
  378. CoreImpl<NoExceptDest, T...>*) {
  379. return !NoExceptDest || NoExceptSrc;
  380. }
  381. // A helper base class for all core operations of AnyInvocable that do not
  382. // depend on the cv/ref qualifiers of the function type.
  383. template <bool SigIsNoexcept, class ReturnType, class... P>
  384. class CoreImpl {
  385. public:
  386. using result_type = ReturnType;
  387. CoreImpl() noexcept : manager_(EmptyManager), invoker_(nullptr) {}
  388. enum class TargetType {
  389. kPointer,
  390. kCompatibleAnyInvocable,
  391. kIncompatibleAnyInvocable,
  392. kOther,
  393. };
  394. // Note: QualDecayedTRef here includes the cv-ref qualifiers associated with
  395. // the invocation of the Invocable. The unqualified type is the target object
  396. // type to be stored.
  397. template <class QualDecayedTRef, class F>
  398. explicit CoreImpl(TypedConversionConstruct<QualDecayedTRef>, F&& f) {
  399. using DecayedT = RemoveCVRef<QualDecayedTRef>;
  400. constexpr TargetType kTargetType =
  401. (std::is_pointer<DecayedT>::value ||
  402. std::is_member_pointer<DecayedT>::value)
  403. ? TargetType::kPointer
  404. : IsCompatibleAnyInvocable<DecayedT>::value
  405. ? TargetType::kCompatibleAnyInvocable
  406. : IsAnyInvocable<DecayedT>::value
  407. ? TargetType::kIncompatibleAnyInvocable
  408. : TargetType::kOther;
  409. // NOTE: We only use integers instead of enums as template parameters in
  410. // order to work around a bug on C++14 under MSVC 2017.
  411. // See b/236131881.
  412. Initialize<kTargetType, QualDecayedTRef>(std::forward<F>(f));
  413. }
  414. // Note: QualTRef here includes the cv-ref qualifiers associated with the
  415. // invocation of the Invocable. The unqualified type is the target object
  416. // type to be stored.
  417. template <class QualTRef, class... Args>
  418. explicit CoreImpl(absl::in_place_type_t<QualTRef>, Args&&... args) {
  419. InitializeStorage<QualTRef>(std::forward<Args>(args)...);
  420. }
  421. CoreImpl(CoreImpl&& other) noexcept {
  422. other.manager_(FunctionToCall::relocate_from_to, &other.state_, &state_);
  423. manager_ = other.manager_;
  424. invoker_ = other.invoker_;
  425. other.manager_ = EmptyManager;
  426. other.invoker_ = nullptr;
  427. }
  428. CoreImpl& operator=(CoreImpl&& other) noexcept {
  429. // Put the left-hand operand in an empty state.
  430. //
  431. // Note: A full reset that leaves us with an object that has its invariants
  432. // intact is necessary in order to handle self-move. This is required by
  433. // types that are used with certain operations of the standard library, such
  434. // as the default definition of std::swap when both operands target the same
  435. // object.
  436. Clear();
  437. // Perform the actual move/destroy operation on the target function.
  438. other.manager_(FunctionToCall::relocate_from_to, &other.state_, &state_);
  439. manager_ = other.manager_;
  440. invoker_ = other.invoker_;
  441. other.manager_ = EmptyManager;
  442. other.invoker_ = nullptr;
  443. return *this;
  444. }
  445. ~CoreImpl() { manager_(FunctionToCall::dispose, &state_, &state_); }
  446. // Check whether or not the AnyInvocable is in the empty state.
  447. bool HasValue() const { return invoker_ != nullptr; }
  448. // Effects: Puts the object into its empty state.
  449. void Clear() {
  450. manager_(FunctionToCall::dispose, &state_, &state_);
  451. manager_ = EmptyManager;
  452. invoker_ = nullptr;
  453. }
  454. template <TargetType target_type, class QualDecayedTRef, class F,
  455. absl::enable_if_t<target_type == TargetType::kPointer, int> = 0>
  456. void Initialize(F&& f) {
  457. // This condition handles types that decay into pointers, which includes
  458. // function references. Since function references cannot be null, GCC warns
  459. // against comparing their decayed form with nullptr.
  460. // Since this is template-heavy code, we prefer to disable these warnings
  461. // locally instead of adding yet another overload of this function.
  462. #if !defined(__clang__) && defined(__GNUC__)
  463. #pragma GCC diagnostic push
  464. #pragma GCC diagnostic ignored "-Wpragmas"
  465. #pragma GCC diagnostic ignored "-Waddress"
  466. #pragma GCC diagnostic ignored "-Wnonnull-compare"
  467. #endif
  468. if (static_cast<RemoveCVRef<QualDecayedTRef>>(f) == nullptr) {
  469. #if !defined(__clang__) && defined(__GNUC__)
  470. #pragma GCC diagnostic pop
  471. #endif
  472. manager_ = EmptyManager;
  473. invoker_ = nullptr;
  474. return;
  475. }
  476. InitializeStorage<QualDecayedTRef>(std::forward<F>(f));
  477. }
  478. template <TargetType target_type, class QualDecayedTRef, class F,
  479. absl::enable_if_t<
  480. target_type == TargetType::kCompatibleAnyInvocable, int> = 0>
  481. void Initialize(F&& f) {
  482. // In this case we can "steal the guts" of the other AnyInvocable.
  483. f.manager_(FunctionToCall::relocate_from_to, &f.state_, &state_);
  484. manager_ = f.manager_;
  485. invoker_ = f.invoker_;
  486. f.manager_ = EmptyManager;
  487. f.invoker_ = nullptr;
  488. }
  489. template <TargetType target_type, class QualDecayedTRef, class F,
  490. absl::enable_if_t<
  491. target_type == TargetType::kIncompatibleAnyInvocable, int> = 0>
  492. void Initialize(F&& f) {
  493. if (f.HasValue()) {
  494. InitializeStorage<QualDecayedTRef>(std::forward<F>(f));
  495. } else {
  496. manager_ = EmptyManager;
  497. invoker_ = nullptr;
  498. }
  499. }
  500. template <TargetType target_type, class QualDecayedTRef, class F,
  501. typename = absl::enable_if_t<target_type == TargetType::kOther>>
  502. void Initialize(F&& f) {
  503. InitializeStorage<QualDecayedTRef>(std::forward<F>(f));
  504. }
  505. // Use local (inline) storage for applicable target object types.
  506. template <class QualTRef, class... Args,
  507. typename = absl::enable_if_t<
  508. IsStoredLocally<RemoveCVRef<QualTRef>>::value>>
  509. void InitializeStorage(Args&&... args) {
  510. using RawT = RemoveCVRef<QualTRef>;
  511. ::new (static_cast<void*>(&state_.storage))
  512. RawT(std::forward<Args>(args)...);
  513. invoker_ = LocalInvoker<SigIsNoexcept, ReturnType, QualTRef, P...>;
  514. // We can simplify our manager if we know the type is trivially copyable.
  515. InitializeLocalManager<RawT>();
  516. }
  517. // Use remote storage for target objects that cannot be stored locally.
  518. template <class QualTRef, class... Args,
  519. absl::enable_if_t<!IsStoredLocally<RemoveCVRef<QualTRef>>::value,
  520. int> = 0>
  521. void InitializeStorage(Args&&... args) {
  522. InitializeRemoteManager<RemoveCVRef<QualTRef>>(std::forward<Args>(args)...);
  523. // This is set after everything else in case an exception is thrown in an
  524. // earlier step of the initialization.
  525. invoker_ = RemoteInvoker<SigIsNoexcept, ReturnType, QualTRef, P...>;
  526. }
  527. template <class T,
  528. typename = absl::enable_if_t<std::is_trivially_copyable<T>::value>>
  529. void InitializeLocalManager() {
  530. manager_ = LocalManagerTrivial;
  531. }
  532. template <class T,
  533. absl::enable_if_t<!std::is_trivially_copyable<T>::value, int> = 0>
  534. void InitializeLocalManager() {
  535. manager_ = LocalManagerNontrivial<T>;
  536. }
  537. template <class T>
  538. using HasTrivialRemoteStorage =
  539. std::integral_constant<bool, std::is_trivially_destructible<T>::value &&
  540. alignof(T) <=
  541. ABSL_INTERNAL_DEFAULT_NEW_ALIGNMENT>;
  542. template <class T, class... Args,
  543. typename = absl::enable_if_t<HasTrivialRemoteStorage<T>::value>>
  544. void InitializeRemoteManager(Args&&... args) {
  545. // unique_ptr is used for exception-safety in case construction throws.
  546. std::unique_ptr<void, TrivialDeleter> uninitialized_target(
  547. ::operator new(sizeof(T)), TrivialDeleter(sizeof(T)));
  548. ::new (uninitialized_target.get()) T(std::forward<Args>(args)...);
  549. state_.remote.target = uninitialized_target.release();
  550. state_.remote.size = sizeof(T);
  551. manager_ = RemoteManagerTrivial;
  552. }
  553. template <class T, class... Args,
  554. absl::enable_if_t<!HasTrivialRemoteStorage<T>::value, int> = 0>
  555. void InitializeRemoteManager(Args&&... args) {
  556. state_.remote.target = ::new T(std::forward<Args>(args)...);
  557. manager_ = RemoteManagerNontrivial<T>;
  558. }
  559. //////////////////////////////////////////////////////////////////////////////
  560. //
  561. // Type trait to determine if the template argument is an AnyInvocable whose
  562. // function type is compatible enough with ours such that we can
  563. // "move the guts" out of it when moving, rather than having to place a new
  564. // object into remote storage.
  565. template <typename Other>
  566. struct IsCompatibleAnyInvocable {
  567. static constexpr bool value = false;
  568. };
  569. template <typename Sig>
  570. struct IsCompatibleAnyInvocable<AnyInvocable<Sig>> {
  571. static constexpr bool value =
  572. (IsCompatibleConversion)(static_cast<
  573. typename AnyInvocable<Sig>::CoreImpl*>(
  574. nullptr),
  575. static_cast<CoreImpl*>(nullptr));
  576. };
  577. //
  578. //////////////////////////////////////////////////////////////////////////////
  579. TypeErasedState state_;
  580. ManagerType* manager_;
  581. InvokerType<SigIsNoexcept, ReturnType, P...>* invoker_;
  582. };
  583. // A constructor name-tag used with Impl to request the
  584. // conversion-constructor
  585. struct ConversionConstruct {};
  586. ////////////////////////////////////////////////////////////////////////////////
  587. //
  588. // A metafunction that is normally an identity metafunction except that when
  589. // given a std::reference_wrapper<T>, it yields T&. This is necessary because
  590. // currently std::reference_wrapper's operator() is not conditionally noexcept,
  591. // so when checking if such an Invocable is nothrow-invocable, we must pull out
  592. // the underlying type.
  593. template <class T>
  594. struct UnwrapStdReferenceWrapperImpl {
  595. using type = T;
  596. };
  597. template <class T>
  598. struct UnwrapStdReferenceWrapperImpl<std::reference_wrapper<T>> {
  599. using type = T&;
  600. };
  601. template <class T>
  602. using UnwrapStdReferenceWrapper =
  603. typename UnwrapStdReferenceWrapperImpl<T>::type;
  604. //
  605. ////////////////////////////////////////////////////////////////////////////////
  606. // An alias that always yields std::true_type (used with constraints) where
  607. // substitution failures happen when forming the template arguments.
  608. template <class... T>
  609. using TrueAlias =
  610. std::integral_constant<bool, sizeof(absl::void_t<T...>*) != 0>;
  611. /*SFINAE constraints for the conversion-constructor.*/
  612. template <class Sig, class F,
  613. class = absl::enable_if_t<
  614. !std::is_same<RemoveCVRef<F>, AnyInvocable<Sig>>::value>>
  615. using CanConvert = TrueAlias<
  616. absl::enable_if_t<!IsInPlaceType<RemoveCVRef<F>>::value>,
  617. absl::enable_if_t<Impl<Sig>::template CallIsValid<F>::value>,
  618. absl::enable_if_t<
  619. Impl<Sig>::template CallIsNoexceptIfSigIsNoexcept<F>::value>,
  620. absl::enable_if_t<std::is_constructible<absl::decay_t<F>, F>::value>>;
  621. /*SFINAE constraints for the std::in_place constructors.*/
  622. template <class Sig, class F, class... Args>
  623. using CanEmplace = TrueAlias<
  624. absl::enable_if_t<Impl<Sig>::template CallIsValid<F>::value>,
  625. absl::enable_if_t<
  626. Impl<Sig>::template CallIsNoexceptIfSigIsNoexcept<F>::value>,
  627. absl::enable_if_t<std::is_constructible<absl::decay_t<F>, Args...>::value>>;
  628. /*SFINAE constraints for the conversion-assign operator.*/
  629. template <class Sig, class F,
  630. class = absl::enable_if_t<
  631. !std::is_same<RemoveCVRef<F>, AnyInvocable<Sig>>::value>>
  632. using CanAssign = TrueAlias<
  633. absl::enable_if_t<Impl<Sig>::template CallIsValid<F>::value>,
  634. absl::enable_if_t<
  635. Impl<Sig>::template CallIsNoexceptIfSigIsNoexcept<F>::value>,
  636. absl::enable_if_t<std::is_constructible<absl::decay_t<F>, F>::value>>;
  637. /*SFINAE constraints for the reference-wrapper conversion-assign operator.*/
  638. template <class Sig, class F>
  639. using CanAssignReferenceWrapper = TrueAlias<
  640. absl::enable_if_t<
  641. Impl<Sig>::template CallIsValid<std::reference_wrapper<F>>::value>,
  642. absl::enable_if_t<Impl<Sig>::template CallIsNoexceptIfSigIsNoexcept<
  643. std::reference_wrapper<F>>::value>>;
  644. ////////////////////////////////////////////////////////////////////////////////
  645. //
  646. // The constraint for checking whether or not a call meets the noexcept
  647. // callability requirements. This is a preprocessor macro because specifying it
  648. // this way as opposed to a disjunction/branch can improve the user-side error
  649. // messages and avoids an instantiation of std::is_nothrow_invocable_r in the
  650. // cases where the user did not specify a noexcept function type.
  651. //
  652. #define ABSL_INTERNAL_ANY_INVOCABLE_NOEXCEPT_CONSTRAINT(inv_quals, noex) \
  653. ABSL_INTERNAL_ANY_INVOCABLE_NOEXCEPT_CONSTRAINT_##noex(inv_quals)
  654. // The disjunction below is because we can't rely on std::is_nothrow_invocable_r
  655. // to give the right result when ReturnType is non-moveable in toolchains that
  656. // don't treat non-moveable result types correctly. For example this was the
  657. // case in libc++ before commit c3a24882 (2022-05).
  658. #define ABSL_INTERNAL_ANY_INVOCABLE_NOEXCEPT_CONSTRAINT_true(inv_quals) \
  659. absl::enable_if_t<absl::disjunction< \
  660. std::is_nothrow_invocable_r< \
  661. ReturnType, UnwrapStdReferenceWrapper<absl::decay_t<F>> inv_quals, \
  662. P...>, \
  663. std::conjunction< \
  664. std::is_nothrow_invocable< \
  665. UnwrapStdReferenceWrapper<absl::decay_t<F>> inv_quals, P...>, \
  666. std::is_same< \
  667. ReturnType, \
  668. absl::base_internal::invoke_result_t< \
  669. UnwrapStdReferenceWrapper<absl::decay_t<F>> inv_quals, \
  670. P...>>>>::value>
  671. #define ABSL_INTERNAL_ANY_INVOCABLE_NOEXCEPT_CONSTRAINT_false(inv_quals)
  672. //
  673. ////////////////////////////////////////////////////////////////////////////////
  674. // A macro to generate partial specializations of Impl with the different
  675. // combinations of supported cv/reference qualifiers and noexcept specifier.
  676. //
  677. // Here, `cv` are the cv-qualifiers if any, `ref` is the ref-qualifier if any,
  678. // inv_quals is the reference type to be used when invoking the target, and
  679. // noex is "true" if the function type is noexcept, or false if it is not.
  680. //
  681. // The CallIsValid condition is more complicated than simply using
  682. // absl::base_internal::is_invocable_r because we can't rely on it to give the
  683. // right result when ReturnType is non-moveable in toolchains that don't treat
  684. // non-moveable result types correctly. For example this was the case in libc++
  685. // before commit c3a24882 (2022-05).
  686. #define ABSL_INTERNAL_ANY_INVOCABLE_IMPL_(cv, ref, inv_quals, noex) \
  687. template <class ReturnType, class... P> \
  688. class Impl<ReturnType(P...) cv ref ABSL_INTERNAL_NOEXCEPT_SPEC(noex)> \
  689. : public CoreImpl<noex, ReturnType, P...> { \
  690. public: \
  691. /*The base class, which contains the datamembers and core operations*/ \
  692. using Core = CoreImpl<noex, ReturnType, P...>; \
  693. \
  694. /*SFINAE constraint to check if F is invocable with the proper signature*/ \
  695. template <class F> \
  696. using CallIsValid = TrueAlias<absl::enable_if_t<absl::disjunction< \
  697. absl::base_internal::is_invocable_r<ReturnType, \
  698. absl::decay_t<F> inv_quals, P...>, \
  699. std::is_same<ReturnType, \
  700. absl::base_internal::invoke_result_t< \
  701. absl::decay_t<F> inv_quals, P...>>>::value>>; \
  702. \
  703. /*SFINAE constraint to check if F is nothrow-invocable when necessary*/ \
  704. template <class F> \
  705. using CallIsNoexceptIfSigIsNoexcept = \
  706. TrueAlias<ABSL_INTERNAL_ANY_INVOCABLE_NOEXCEPT_CONSTRAINT(inv_quals, \
  707. noex)>; \
  708. \
  709. /*Put the AnyInvocable into an empty state.*/ \
  710. Impl() = default; \
  711. \
  712. /*The implementation of a conversion-constructor from "f*/ \
  713. /*This forwards to Core, attaching inv_quals so that the base class*/ \
  714. /*knows how to properly type-erase the invocation.*/ \
  715. template <class F> \
  716. explicit Impl(ConversionConstruct, F&& f) \
  717. : Core(TypedConversionConstruct< \
  718. typename std::decay<F>::type inv_quals>(), \
  719. std::forward<F>(f)) {} \
  720. \
  721. /*Forward along the in-place construction parameters.*/ \
  722. template <class T, class... Args> \
  723. explicit Impl(absl::in_place_type_t<T>, Args&&... args) \
  724. : Core(absl::in_place_type<absl::decay_t<T> inv_quals>, \
  725. std::forward<Args>(args)...) {} \
  726. \
  727. /*Raises a fatal error when the AnyInvocable is invoked after a move*/ \
  728. static ReturnType InvokedAfterMove( \
  729. TypeErasedState*, \
  730. ForwardedParameterType<P>...) noexcept(noex) { \
  731. ABSL_HARDENING_ASSERT(false && "AnyInvocable use-after-move"); \
  732. std::terminate(); \
  733. } \
  734. \
  735. InvokerType<noex, ReturnType, P...>* ExtractInvoker() cv { \
  736. using QualifiedTestType = int cv ref; \
  737. auto* invoker = this->invoker_; \
  738. if (!std::is_const<QualifiedTestType>::value && \
  739. std::is_rvalue_reference<QualifiedTestType>::value) { \
  740. ABSL_ASSERT([this]() { \
  741. /* We checked that this isn't const above, so const_cast is safe */ \
  742. const_cast<Impl*>(this)->invoker_ = InvokedAfterMove; \
  743. return this->HasValue(); \
  744. }()); \
  745. } \
  746. return invoker; \
  747. } \
  748. \
  749. /*The actual invocation operation with the proper signature*/ \
  750. ReturnType operator()(P... args) cv ref noexcept(noex) { \
  751. assert(this->invoker_ != nullptr); \
  752. return this->ExtractInvoker()( \
  753. const_cast<TypeErasedState*>(&this->state_), \
  754. static_cast<ForwardedParameterType<P>>(args)...); \
  755. } \
  756. }
  757. // Define the `noexcept(true)` specialization only for C++17 and beyond, when
  758. // `noexcept` is part of the type system.
  759. #if ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L
  760. // A convenience macro that defines specializations for the noexcept(true) and
  761. // noexcept(false) forms, given the other properties.
  762. #define ABSL_INTERNAL_ANY_INVOCABLE_IMPL(cv, ref, inv_quals) \
  763. ABSL_INTERNAL_ANY_INVOCABLE_IMPL_(cv, ref, inv_quals, false); \
  764. ABSL_INTERNAL_ANY_INVOCABLE_IMPL_(cv, ref, inv_quals, true)
  765. #else
  766. #define ABSL_INTERNAL_ANY_INVOCABLE_IMPL(cv, ref, inv_quals) \
  767. ABSL_INTERNAL_ANY_INVOCABLE_IMPL_(cv, ref, inv_quals, false)
  768. #endif
  769. // Non-ref-qualified partial specializations
  770. ABSL_INTERNAL_ANY_INVOCABLE_IMPL(, , &);
  771. ABSL_INTERNAL_ANY_INVOCABLE_IMPL(const, , const&);
  772. // Lvalue-ref-qualified partial specializations
  773. ABSL_INTERNAL_ANY_INVOCABLE_IMPL(, &, &);
  774. ABSL_INTERNAL_ANY_INVOCABLE_IMPL(const, &, const&);
  775. // Rvalue-ref-qualified partial specializations
  776. ABSL_INTERNAL_ANY_INVOCABLE_IMPL(, &&, &&);
  777. ABSL_INTERNAL_ANY_INVOCABLE_IMPL(const, &&, const&&);
  778. // Undef the detail-only macros.
  779. #undef ABSL_INTERNAL_ANY_INVOCABLE_IMPL
  780. #undef ABSL_INTERNAL_ANY_INVOCABLE_IMPL_
  781. #undef ABSL_INTERNAL_ANY_INVOCABLE_NOEXCEPT_CONSTRAINT_false
  782. #undef ABSL_INTERNAL_ANY_INVOCABLE_NOEXCEPT_CONSTRAINT_true
  783. #undef ABSL_INTERNAL_ANY_INVOCABLE_NOEXCEPT_CONSTRAINT
  784. #undef ABSL_INTERNAL_NOEXCEPT_SPEC
  785. } // namespace internal_any_invocable
  786. ABSL_NAMESPACE_END
  787. } // namespace absl
  788. #endif // ABSL_FUNCTIONAL_INTERNAL_ANY_INVOCABLE_H_