Skip to content

Commit

Permalink
Add butter/function.h (facebook#35385)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#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=<functional/functor.h>
-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
  • Loading branch information
christophpurrer authored and facebook-github-bot committed Nov 21, 2022
1 parent f12b12c commit 6c315de
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
51 changes: 51 additions & 0 deletions ReactCommon/butter/function.h
Original file line number Diff line number Diff line change
@@ -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 <butter/butter.h>

#if ( \
defined(BUTTER_FUNCTION_OVERRIDE_INCLUDE) && \
defined(BUTTER_FUNCTION_OVERRIDE))

#include BUTTER_FUNCTION_OVERRIDE_INCLUDE

#elif defined(BUTTER_USE_FOLLY_CONTAINERS)

#include <folly/Function.h>

#else

#include <functional>

#endif

namespace facebook {
namespace butter {

#if ( \
defined(BUTTER_FUNCTION_OVERRIDE_INCLUDE) && \
defined(BUTTER_FUNCTION_OVERRIDE))

template <typename... Ts>
using function = BUTTER_FUNCTION_OVERRIDE<Ts...>;

#elif defined(BUTTER_USE_FOLLY_CONTAINERS)

template <typename... Ts>
using function = folly::Function<Ts...>;

#else

template <typename... Ts>
using function = std::function<Ts...>;

#endif

} // namespace butter
} // namespace facebook
6 changes: 3 additions & 3 deletions ReactCommon/react/bridging/Base.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <react/bridging/Convert.h>

#include <ReactCommon/CallInvoker.h>
#include <folly/Function.h>
#include <butter/function.h>
#include <jsi/jsi.h>

#include <cstdint>
Expand All @@ -36,12 +36,12 @@ struct function_wrapper;

template <typename C, typename R, typename... Args>
struct function_wrapper<R (C::*)(Args...)> {
using type = folly::Function<R(Args...)>;
using type = butter::function<R(Args...)>;
};

template <typename C, typename R, typename... Args>
struct function_wrapper<R (C::*)(Args...) const> {
using type = folly::Function<R(Args...)>;
using type = butter::function<R(Args...)>;
};

template <typename T, typename = void>
Expand Down
18 changes: 11 additions & 7 deletions ReactCommon/react/bridging/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <react/bridging/Base.h>
#include <react/bridging/CallbackWrapper.h>

#include <folly/Function.h>
#include <butter/function.h>

namespace facebook::react {

Expand Down Expand Up @@ -154,8 +154,8 @@ struct Bridging<SyncCallback<R(Args...)>> {
};

template <typename R, typename... Args>
struct Bridging<folly::Function<R(Args...)>> {
using Func = folly::Function<R(Args...)>;
struct Bridging<butter::function<R(Args...)>> {
using Func = butter::function<R(Args...)>;
using IndexSequence = std::index_sequence_for<Args...>;

static constexpr size_t kArgumentCount = sizeof...(Args);
Expand Down Expand Up @@ -202,13 +202,17 @@ struct Bridging<folly::Function<R(Args...)>> {
};

template <typename R, typename... Args>
struct Bridging<std::function<R(Args...)>>
: Bridging<folly::Function<R(Args...)>> {};
struct Bridging<
std::function<R(Args...)>,
std::enable_if_t<!std::is_same_v<
std::function<R(Args...)>,
butter::function<R(Args...)>>>>
: Bridging<butter::function<R(Args...)>> {};

template <typename R, typename... Args>
struct Bridging<R(Args...)> : Bridging<folly::Function<R(Args...)>> {};
struct Bridging<R(Args...)> : Bridging<butter::function<R(Args...)>> {};

template <typename R, typename... Args>
struct Bridging<R (*)(Args...)> : Bridging<folly::Function<R(Args...)>> {};
struct Bridging<R (*)(Args...)> : Bridging<butter::function<R(Args...)>> {};

} // namespace facebook::react

0 comments on commit 6c315de

Please sign in to comment.