1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
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 |
|
20 | namespace facebook {
|
21 | namespace react {
|
22 |
|
23 | #if RN_DEBUG_STRING_CONVERTIBLE
|
24 |
|
25 | inline std::string toString(const std::string &value) {
|
26 | return value;
|
27 | }
|
28 | inline std::string toString(const int &value) {
|
29 | return folly::to<std::string>(value);
|
30 | }
|
31 | inline std::string toString(const bool &value) {
|
32 | return folly::to<std::string>(value);
|
33 | }
|
34 | inline std::string toString(const float &value) {
|
35 | return folly::to<std::string>(value);
|
36 | }
|
37 | inline std::string toString(const double &value) {
|
38 | return folly::to<std::string>(value);
|
39 | }
|
40 |
|
41 | template <typename T>
|
42 | inline SharedDebugStringConvertible
|
43 | debugStringConvertibleItem(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 |
|
51 | template <typename T>
|
52 | inline 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 |
|
64 | inline 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 |
|
73 | inline 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 | }
|
84 | }
|