Files
react-native/ReactCommon/react/renderer/core/propsConversions.h
T
Andrew Coates 4d87d8c6b2 Fix various c++ warnings (#31399)
Summary:
react-native-windows runs with a more strict set of warnings as errors.  This fixes a bunch of warnings being hit while compiling core react-native code as part of react-native-windows.  In particular warnings about mismatched signed/unsigned comparisons, lossy conversions, and variable names that conflict with names in outer scopes (yoga has a global for `leading` and `trailing` that conflicts with some local variable names)

## Changelog

[Internal] [Fixed] - Fix various C++ warnings

Pull Request resolved: https://github.com/facebook/react-native/pull/31399

Test Plan: I've run these changes in react-native-windows. -- Shouldn't have any functionality difference.

Reviewed By: sammy-SC

Differential Revision: D28290188

Pulled By: rozele

fbshipit-source-id: 2f7cf87f58d73a3f43510ac888dbcb9ab177d134
2021-05-12 12:35:33 -07:00

126 lines
3.1 KiB
C++

/*
* Copyright (c) Facebook, Inc. and its 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 <better/optional.h>
#include <folly/Likely.h>
#include <folly/dynamic.h>
#include <react/renderer/core/RawProps.h>
#include <react/renderer/graphics/Color.h>
#include <react/renderer/graphics/Geometry.h>
#include <react/renderer/graphics/conversions.h>
namespace facebook {
namespace react {
template <typename T>
void fromRawValue(RawValue const &rawValue, T &result) {
result = (T)rawValue;
}
template <typename T>
void fromRawValue(RawValue const &rawValue, std::vector<T> &result) {
if (rawValue.hasType<std::vector<RawValue>>()) {
auto items = (std::vector<RawValue>)rawValue;
auto length = items.size();
result.clear();
result.reserve(length);
for (size_t i = 0; i < length; i++) {
T itemResult;
fromRawValue(items.at(i), itemResult);
result.push_back(itemResult);
}
return;
}
// The case where `value` is not an array.
result.clear();
result.reserve(1);
T itemResult;
fromRawValue(rawValue, itemResult);
result.push_back(itemResult);
}
template <typename T>
void fromRawValue(
RawValue const &rawValue,
std::vector<std::vector<T>> &result) {
if (rawValue.hasType<std::vector<std::vector<RawValue>>>()) {
auto items = (std::vector<std::vector<RawValue>>)rawValue;
auto length = items.size();
result.clear();
result.reserve(length);
for (int i = 0; i < length; i++) {
T itemResult;
fromRawValue(items.at(i), itemResult);
result.push_back(itemResult);
}
return;
}
// The case where `value` is not an array.
result.clear();
result.reserve(1);
T itemResult;
fromRawValue(rawValue, itemResult);
result.push_back(itemResult);
}
template <typename T, typename U = T>
T convertRawProp(
RawProps const &rawProps,
char const *name,
T const &sourceValue,
U const &defaultValue,
char const *namePrefix = nullptr,
char const *nameSuffix = nullptr) {
const auto *rawValue = rawProps.at(name, namePrefix, nameSuffix);
if (LIKELY(rawValue == nullptr)) {
return sourceValue;
}
// Special case: `null` always means "the prop was removed, use default
// value".
if (UNLIKELY(!rawValue->hasValue())) {
return defaultValue;
}
T result;
fromRawValue(*rawValue, result);
return result;
}
template <typename T>
static better::optional<T> convertRawProp(
RawProps const &rawProps,
char const *name,
better::optional<T> const &sourceValue,
better::optional<T> const &defaultValue,
char const *namePrefix = nullptr,
char const *nameSuffix = nullptr) {
const auto *rawValue = rawProps.at(name, namePrefix, nameSuffix);
if (LIKELY(rawValue == nullptr)) {
return sourceValue;
}
// Special case: `null` always means `the prop was removed, use default
// value`.
if (UNLIKELY(!rawValue->hasValue())) {
return defaultValue;
}
T result;
fromRawValue(*rawValue, result);
return better::optional<T>{result};
}
} // namespace react
} // namespace facebook