Skip to content

Commit

Permalink
react-native bridging > Add support for std::set
Browse files Browse the repository at this point in the history
Summary:
There are use cases in which we want to pass a set of unique elements from C++ to JS and back. We can do this simple by re-using a plain JS array for these purposes.

Changelog:
[Internal][Added] react-native bridging > Add support for std::set

Reviewed By: cipolleschi

Differential Revision: D40668244

fbshipit-source-id: d06603440569e5f760c2859a54cf6e4232c7d97a
  • Loading branch information
christophpurrer authored and facebook-github-bot committed Oct 25, 2022
1 parent 3b0c655 commit 1d6b732
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
19 changes: 19 additions & 0 deletions ReactCommon/react/bridging/Array.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <deque>
#include <initializer_list>
#include <list>
#include <set>
#include <tuple>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -105,4 +106,22 @@ struct Bridging<std::vector<T>>
}
};

template <typename T>
struct Bridging<std::set<T>> : array_detail::BridgingDynamic<std::set<T>> {
static std::set<T> fromJs(
facebook::jsi::Runtime &rt,
const jsi::Array &array,
const std::shared_ptr<CallInvoker> &jsInvoker) {
size_t length = array.length(rt);

std::set<T> set;
for (size_t i = 0; i < length; i++) {
set.insert(
bridging::fromJs<T>(rt, array.getValueAtIndex(rt, i), jsInvoker));
}

return set;
}
};

} // namespace facebook::react
6 changes: 6 additions & 0 deletions ReactCommon/react/bridging/tests/BridgingTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,8 @@ TEST_F(BridgingTest, supportTest) {
EXPECT_TRUE((bridging::supportsFromJs<std::string>));
EXPECT_TRUE((bridging::supportsFromJs<std::string, jsi::String>));
EXPECT_TRUE((bridging::supportsFromJs<std::string, jsi::String &>));
EXPECT_TRUE((bridging::supportsFromJs<std::set<int>, jsi::Array>));
EXPECT_TRUE((bridging::supportsFromJs<std::set<int>, jsi::Array &>));
EXPECT_TRUE((bridging::supportsFromJs<std::vector<int>, jsi::Array>));
EXPECT_TRUE((bridging::supportsFromJs<std::vector<int>, jsi::Array &>));
EXPECT_TRUE(
Expand All @@ -453,6 +455,8 @@ TEST_F(BridgingTest, supportTest) {
EXPECT_FALSE((bridging::supportsFromJs<double, jsi::Object &>));
EXPECT_FALSE((bridging::supportsFromJs<std::string, jsi::Object>));
EXPECT_FALSE((bridging::supportsFromJs<std::string, jsi::Object &>));
EXPECT_FALSE((bridging::supportsFromJs<std::set<int>, jsi::String>));
EXPECT_FALSE((bridging::supportsFromJs<std::set<int>, jsi::String &>));
EXPECT_FALSE((bridging::supportsFromJs<std::vector<int>, jsi::String>));
EXPECT_FALSE((bridging::supportsFromJs<std::vector<int>, jsi::String &>));

Expand Down Expand Up @@ -494,6 +498,8 @@ TEST_F(BridgingTest, supportTest) {
EXPECT_TRUE((bridging::supportsToJs<double>));
EXPECT_TRUE((bridging::supportsToJs<std::string>));
EXPECT_TRUE((bridging::supportsToJs<std::string, jsi::String>));
EXPECT_TRUE((bridging::supportsToJs<std::set<int>>));
EXPECT_TRUE((bridging::supportsToJs<std::set<int>, jsi::Array>));
EXPECT_TRUE((bridging::supportsToJs<std::vector<int>>));
EXPECT_TRUE((bridging::supportsToJs<std::vector<int>, jsi::Array>));
EXPECT_TRUE((bridging::supportsToJs<std::map<std::string, int>>));
Expand Down

0 comments on commit 1d6b732

Please sign in to comment.