mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
e325c9505a
Summary: this is a trivial change that allocates the NSMutableArray with the correct capacity before it is filled ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. For an example, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [Internal] [Changed] - RCTConvertVecToArray - allocate array with capacity Pull Request resolved: https://github.com/facebook/react-native/pull/35490 Test Plan: tested locally: the code builds and runs as expected Reviewed By: cipolleschi Differential Revision: D41548050 Pulled By: GijsWeterings fbshipit-source-id: a5b947331d6c5fffcfecc7c20c827f42442b1ab8
86 lines
2.5 KiB
Objective-C
86 lines
2.5 KiB
Objective-C
/*
|
|
* 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.
|
|
*/
|
|
|
|
#import <optional>
|
|
|
|
#import <vector>
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
#import <FBLazyVector/FBLazyVector.h>
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
template <typename T>
|
|
using LazyVector = FB::LazyVector<T, id>;
|
|
}
|
|
}
|
|
|
|
template <typename ContainerT>
|
|
NSArray *RCTConvertVecToArray(const ContainerT &vec, id (^convertor)(typename ContainerT::value_type element))
|
|
{
|
|
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:vec.size()];
|
|
for (size_t i = 0, size = vec.size(); i < size; ++i) {
|
|
id object = convertor(vec[i]);
|
|
array[i] = object ?: (id)kCFNull;
|
|
}
|
|
return array;
|
|
}
|
|
template <typename ContainerT>
|
|
NSArray *RCTConvertVecToArray(const ContainerT &vec)
|
|
{
|
|
return RCTConvertVecToArray(vec, ^id(typename ContainerT::value_type element) {
|
|
return element;
|
|
});
|
|
}
|
|
|
|
template <typename ContainerT>
|
|
NSArray *RCTConvertOptionalVecToArray(
|
|
const std::optional<ContainerT> &vec,
|
|
id (^convertor)(typename ContainerT::value_type element))
|
|
{
|
|
return vec.has_value() ? RCTConvertVecToArray(vec.value(), convertor) : nil;
|
|
}
|
|
|
|
template <typename ContainerT>
|
|
NSArray *RCTConvertOptionalVecToArray(const std::optional<ContainerT> &vec)
|
|
{
|
|
return vec.has_value() ? RCTConvertVecToArray(
|
|
vec.value(),
|
|
^id(typename ContainerT::value_type element) {
|
|
return element;
|
|
})
|
|
: nil;
|
|
}
|
|
|
|
bool RCTBridgingToBool(id value);
|
|
std::optional<bool> RCTBridgingToOptionalBool(id value);
|
|
NSString *RCTBridgingToString(id value);
|
|
NSString *RCTBridgingToOptionalString(id value);
|
|
std::optional<double> RCTBridgingToOptionalDouble(id value);
|
|
double RCTBridgingToDouble(id value);
|
|
NSArray *RCTBridgingToArray(id value);
|
|
|
|
template <typename T>
|
|
facebook::react::LazyVector<T> RCTBridgingToVec(id value, T (^ctor)(id element))
|
|
{
|
|
NSArray *array = RCTBridgingToArray(value);
|
|
typedef typename facebook::react::LazyVector<T>::size_type _size_t;
|
|
_size_t size = static_cast<_size_t>(array.count);
|
|
return facebook::react::LazyVector<T>::fromUnsafeRawValue(array, size, ctor);
|
|
}
|
|
|
|
template <typename T>
|
|
std::optional<facebook::react::LazyVector<T>> RCTBridgingToOptionalVec(id value, T (^ctor)(id element))
|
|
{
|
|
if (value == nil || value == (id)kCFNull) {
|
|
return std::nullopt;
|
|
} else {
|
|
return RCTBridgingToVec(value, ctor);
|
|
}
|
|
}
|