From 6c315de2260318134ebfbe84aac65ae08085c198 Mon Sep 17 00:00:00 2001 From: Christoph Purrer Date: Mon, 21 Nov 2022 11:17:01 -0800 Subject: [PATCH] Add butter/function.h (#35385) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/35385 In OpenSource builds folly:: types are not always easy to consume. With butter:: we actually allow consumers to opt-into other/non folly:: types (by providing a custom butter:: implementation). This change adds a butter::function for that purpose. It is especially useful for react-native-windows which prefers Mso::Functor over folly::Function. You can use it by setting those compiler flags: -DBUTTER_FUNCTION_OVERRIDE_INCLUDE= -DBUTTER_FUNCTION_OVERRIDE=Mso::Functor std::function is no option as it is not move-only and we can't wait till 2025 > https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p0288r9.html Changelog: [Internal] Reviewed By: sammy-SC Differential Revision: D41388193 fbshipit-source-id: 56f58b9ddc602aa4b13000031d50de5228b4a16b --- ReactCommon/butter/function.h | 51 +++++++++++++++++++++++++++ ReactCommon/react/bridging/Base.h | 6 ++-- ReactCommon/react/bridging/Function.h | 18 ++++++---- 3 files changed, 65 insertions(+), 10 deletions(-) create mode 100644 ReactCommon/butter/function.h diff --git a/ReactCommon/butter/function.h b/ReactCommon/butter/function.h new file mode 100644 index 0000000000..724752498f --- /dev/null +++ b/ReactCommon/butter/function.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#pragma once + +#include + +#if ( \ + defined(BUTTER_FUNCTION_OVERRIDE_INCLUDE) && \ + defined(BUTTER_FUNCTION_OVERRIDE)) + +#include BUTTER_FUNCTION_OVERRIDE_INCLUDE + +#elif defined(BUTTER_USE_FOLLY_CONTAINERS) + +#include + +#else + +#include + +#endif + +namespace facebook { +namespace butter { + +#if ( \ + defined(BUTTER_FUNCTION_OVERRIDE_INCLUDE) && \ + defined(BUTTER_FUNCTION_OVERRIDE)) + +template +using function = BUTTER_FUNCTION_OVERRIDE; + +#elif defined(BUTTER_USE_FOLLY_CONTAINERS) + +template +using function = folly::Function; + +#else + +template +using function = std::function; + +#endif + +} // namespace butter +} // namespace facebook diff --git a/ReactCommon/react/bridging/Base.h b/ReactCommon/react/bridging/Base.h index 4a65364753..4ff8ef3a8e 100644 --- a/ReactCommon/react/bridging/Base.h +++ b/ReactCommon/react/bridging/Base.h @@ -10,7 +10,7 @@ #include #include -#include +#include #include #include @@ -36,12 +36,12 @@ struct function_wrapper; template struct function_wrapper { - using type = folly::Function; + using type = butter::function; }; template struct function_wrapper { - using type = folly::Function; + using type = butter::function; }; template diff --git a/ReactCommon/react/bridging/Function.h b/ReactCommon/react/bridging/Function.h index fdac94bb6f..a1d555acdd 100644 --- a/ReactCommon/react/bridging/Function.h +++ b/ReactCommon/react/bridging/Function.h @@ -10,7 +10,7 @@ #include #include -#include +#include namespace facebook::react { @@ -154,8 +154,8 @@ struct Bridging> { }; template -struct Bridging> { - using Func = folly::Function; +struct Bridging> { + using Func = butter::function; using IndexSequence = std::index_sequence_for; static constexpr size_t kArgumentCount = sizeof...(Args); @@ -202,13 +202,17 @@ struct Bridging> { }; template -struct Bridging> - : Bridging> {}; +struct Bridging< + std::function, + std::enable_if_t, + butter::function>>> + : Bridging> {}; template -struct Bridging : Bridging> {}; +struct Bridging : Bridging> {}; template -struct Bridging : Bridging> {}; +struct Bridging : Bridging> {}; } // namespace facebook::react