/* * 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 #include #include #include #include #include #include namespace facebook { namespace react { template void fromRawValue(RawValue const &rawValue, T &result) { result = (T)rawValue; } template void fromRawValue(RawValue const &rawValue, std::vector &result) { if (rawValue.hasType>()) { auto items = (std::vector)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 void fromRawValue( RawValue const &rawValue, std::vector> &result) { if (rawValue.hasType>>()) { auto items = (std::vector>)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 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 static better::optional convertRawProp( RawProps const &rawProps, char const *name, better::optional const &sourceValue, better::optional 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{result}; } } // namespace react } // namespace facebook