UNPKG

2.18 kBtext/x-cView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#pragma once
9
10#include <limits>
11#include <memory>
12#include <string>
13#include <vector>
14
15#include <folly/Conv.h>
16#include <folly/Optional.h>
17#include <react/debug/DebugStringConvertible.h>
18#include <react/debug/DebugStringConvertibleItem.h>
19
20namespace facebook {
21namespace react {
22
23#if RN_DEBUG_STRING_CONVERTIBLE
24
25inline std::string toString(const std::string &value) {
26 return value;
27}
28inline std::string toString(const int &value) {
29 return folly::to<std::string>(value);
30}
31inline std::string toString(const bool &value) {
32 return folly::to<std::string>(value);
33}
34inline std::string toString(const float &value) {
35 return folly::to<std::string>(value);
36}
37inline std::string toString(const double &value) {
38 return folly::to<std::string>(value);
39}
40
41template <typename T>
42inline SharedDebugStringConvertible
43debugStringConvertibleItem(std::string name, T value, T defaultValue = {}) {
44 if (value == defaultValue) {
45 return nullptr;
46 }
47
48 return std::make_shared<DebugStringConvertibleItem>(name, toString(value));
49}
50
51template <typename T>
52inline SharedDebugStringConvertible debugStringConvertibleItem(
53 std::string name,
54 folly::Optional<T> value,
55 T defaultValue = {}) {
56 if (!value.hasValue()) {
57 return nullptr;
58 }
59
60 return debugStringConvertibleItem(
61 name, value.value_or(defaultValue), defaultValue);
62}
63
64inline SharedDebugStringConvertibleList operator+(
65 const SharedDebugStringConvertibleList &lhs,
66 const SharedDebugStringConvertibleList &rhs) {
67 auto result = SharedDebugStringConvertibleList{};
68 std::move(lhs.begin(), lhs.end(), std::back_inserter(result));
69 std::move(rhs.begin(), rhs.end(), std::back_inserter(result));
70 return result;
71}
72
73inline SharedDebugStringConvertible debugStringConvertibleItem(
74 std::string name,
75 DebugStringConvertible value,
76 std::string defaultValue) {
77 return debugStringConvertibleItem(
78 name, value.getDebugDescription(), defaultValue);
79}
80
81#endif
82
83} // namespace react
84} // namespace facebook