MoveOnlyFunction.h (1865B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_MoveOnlyFunction_h 8 #define mozilla_MoveOnlyFunction_h 9 10 // Use stl-like empty propagation to avoid issues with wrapping closures which 11 // implicitly coerce to bool. 12 #define FU2_WITH_LIMITED_EMPTY_PROPAGATION 13 14 #include "function2/function2.hpp" 15 16 namespace mozilla { 17 18 /// A type like `std::function`, but with support for move-only callable 19 /// objects. 20 /// 21 /// A similar type is proposed to be added to the standard library as 22 /// `std::move_only_function` in C++23. 23 /// 24 /// Unlike `std::function`, the function signature may be given const or 25 /// reference qualifiers which will be applied to `operator()`. This can be used 26 /// to declare const qualified or move-only functions. 27 /// 28 /// The implementation this definition depends on (function2) also has support 29 /// for callables with overload sets, however support for this was not exposed 30 /// to align better with the proposed `std::move_only_function`, which does not 31 /// support overload sets. 32 /// 33 /// A custom typedef over `fu2::function_base` is used to control the size and 34 /// alignment of the inline storage to store 2 aligned pointers, and ensure the 35 /// type is compatible with `nsTArray`. 36 template <typename Signature> 37 using MoveOnlyFunction = fu2::function_base< 38 /* IsOwning */ true, 39 /* IsCopyable */ false, 40 /* Capacity */ fu2::capacity_fixed<2 * sizeof(void*), alignof(void*)>, 41 /* IsThrowing */ false, 42 /* HasStrongExceptionGuarantee */ false, 43 /* Signature */ Signature>; 44 45 } // namespace mozilla 46 47 #endif // mozilla_MoveOnlyFunction_h