forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugStringConvertible.h
More file actions
390 lines (326 loc) · 10.8 KB
/
DebugStringConvertible.h
File metadata and controls
390 lines (326 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
* 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 <climits>
#include <memory>
#include <string>
#include <unordered_set>
#include <vector>
namespace facebook {
namespace react {
#ifndef NDEBUG
#define RN_DEBUG_STRING_CONVERTIBLE 1
#endif
// To Debug Yoga layout, uncomment the following line.
// #define RN_DEBUG_YOGA_LOGGER 1
//
// Additional logging can be enabled editing yoga.cpp (e.g. gPrintChanges,
// gPrintSkips)
// To Debug introspection of RN Shadow tree, uncomment the following line:
// #define RN_SHADOW_TREE_INTROSPECTION 1
#if RN_DEBUG_STRING_CONVERTIBLE
class DebugStringConvertible;
using SharedDebugStringConvertible =
std::shared_ptr<const DebugStringConvertible>;
using SharedDebugStringConvertibleList =
std::vector<SharedDebugStringConvertible>;
struct DebugStringConvertibleOptions {
bool format{true};
int depth{0};
int maximumDepth{INT_MAX};
};
/*
* Abstract class describes conformance to DebugStringConvertible concept
* and implements basic recursive debug string assembly algorithm.
* Use this as a base class for providing a debugging textual representation
* of your class.
*
* The `DebugStringConvertible` *class* is obsolete. Whenever possible prefer
* implementing standalone functions that conform to the informal
* `DebugStringConvertible`-like interface instead of extending this class.
*/
class DebugStringConvertible {
public:
virtual ~DebugStringConvertible() = default;
// Returns a name of the object.
// Default implementation returns "Node".
virtual std::string getDebugName() const;
// Returns a value associate with the object.
// Default implementation returns an empty string.
virtual std::string getDebugValue() const;
// Returns a list of `DebugStringConvertible` objects which can be considered
// as *children* of the object.
// Default implementation returns an empty list.
virtual SharedDebugStringConvertibleList getDebugChildren() const;
// Returns a list of `DebugStringConvertible` objects which can be considered
// as *properties* of the object.
// Default implementation returns an empty list.
virtual SharedDebugStringConvertibleList getDebugProps() const;
// Returns a string which represents the object in a human-readable way.
// Default implementation returns a description of the subtree
// rooted at this node, represented in XML-like format.
virtual std::string getDebugDescription(
DebugStringConvertibleOptions options = {}) const;
// Do same as `getDebugDescription` but return only *children* and
// *properties* parts (which are used in `getDebugDescription`).
virtual std::string getDebugPropsDescription(
DebugStringConvertibleOptions options = {}) const;
virtual std::string getDebugChildrenDescription(
DebugStringConvertibleOptions options = {}) const;
};
#else
class DebugStringConvertible {};
#endif
#if RN_DEBUG_STRING_CONVERTIBLE
/*
* Set of particular-format-opinionated functions that convert base types to
* `std::string`; practically incapsulate `folly:to<>` and `folly::format`.
*/
std::string toString(std::string const &value);
std::string toString(int const &value);
std::string toString(bool const &value);
std::string toString(float const &value);
std::string toString(double const &value);
std::string toString(void const *value);
/*
* *Informal* `DebugStringConvertible` interface.
*
* The interface consts of several functions which are designed to be composable
* and reusable relying on C++ overloading mechanism. Implement appropriate
* versions of those functions for your custom type to enable conformance to the
* interface:
*
* - `getDebugName`: Returns a name of the object. Default implementation
* returns "Node".
*
* - `getDebugValue`: Returns a value associate with the object. Default
* implementation returns an empty string.
*
* - `getDebugChildren`: Returns a list of `DebugStringConvertible`-compatible
* objects which can be considered as *children* of the object. Default
* implementation returns an empty list.
*
* - `getDebugProps`: Returns a list of `DebugStringConvertible` objects which
* can be considered as *properties* of the object. Default implementation
* returns an empty list.
*
* - `getDebugDescription`: Returns a string which represents the object in a
* human-readable way. Default implementation returns a description of the
* subtree rooted at this node, represented in XML-like format using functions
* above to form the tree.
*/
/*
* Universal implementation of `getDebugDescription`-family functions for all
* types.
*/
template <typename T>
std::string getDebugName(T const &object) {
return "Node";
}
template <typename T>
std::string getDebugValue(T const &object) {
return "";
}
template <typename T>
std::vector<T> getDebugChildren(
T const &object,
DebugStringConvertibleOptions options) {
return {};
}
template <typename T>
std::vector<T> getDebugProps(
T const &object,
DebugStringConvertibleOptions options) {
return {};
}
template <typename T>
std::string getDebugPropsDescription(
T const &object,
DebugStringConvertibleOptions options) {
if (options.depth >= options.maximumDepth) {
return "";
}
std::string propsString = "";
options.depth++;
for (auto prop : getDebugProps(object, options)) {
auto name = getDebugName(prop);
auto value = getDebugValue(prop);
auto children = getDebugPropsDescription(prop, options);
auto valueAndChildren =
value + (children.empty() ? "" : "(" + children + ")");
propsString +=
" " + name + (valueAndChildren.empty() ? "" : "=" + valueAndChildren);
}
if (!propsString.empty()) {
// Removing leading space character.
propsString.erase(propsString.begin());
}
return propsString;
}
template <typename T>
std::string getDebugChildrenDescription(
T const &object,
DebugStringConvertibleOptions options) {
if (options.depth >= options.maximumDepth) {
return "";
}
auto trailing = options.format ? std::string{"\n"} : std::string{""};
auto childrenString = std::string{""};
options.depth++;
for (auto child : getDebugChildren(object, options)) {
childrenString += getDebugDescription(child, options) + trailing;
}
if (!childrenString.empty() && !trailing.empty()) {
// Removing trailing fragment.
childrenString.erase(childrenString.end() - 1);
}
return childrenString;
}
template <typename T>
std::string getDebugDescription(
T const &object,
DebugStringConvertibleOptions options) {
auto nameString = getDebugName(object);
auto valueString = getDebugValue(object);
// Convention:
// If `name` and `value` are empty, `description` is also empty.
if (nameString.empty() && valueString.empty()) {
return "";
}
// Convention:
// If `name` is empty and `value` isn't empty, `description` equals `value`.
if (nameString.empty()) {
return valueString;
}
auto childrenString = getDebugChildrenDescription(object, options);
auto propsString = getDebugPropsDescription(object, options);
auto leading =
options.format ? std::string(options.depth * 2, ' ') : std::string{""};
auto trailing = options.format ? std::string{"\n"} : std::string{""};
return leading + "<" + nameString +
(valueString.empty() ? "" : "=" + valueString) +
(propsString.empty() ? "" : " " + propsString) +
(childrenString.empty() ? "/>"
: ">" + trailing + childrenString + trailing +
leading + "</" + nameString + ">");
}
/*
* Functions of `getDebugDescription`-family for primitive types.
*/
// `int`
inline std::string getDebugDescription(
int number,
DebugStringConvertibleOptions options) {
return toString(number);
}
// `float`
inline std::string getDebugDescription(
float number,
DebugStringConvertibleOptions options) {
return toString(number);
}
// `double`
inline std::string getDebugDescription(
double number,
DebugStringConvertibleOptions options) {
return toString(number);
}
// `bool`
inline std::string getDebugDescription(
bool boolean,
DebugStringConvertibleOptions options) {
return toString(boolean);
}
// `void *`
inline std::string getDebugDescription(
void *pointer,
DebugStringConvertibleOptions options) {
return toString(pointer);
}
// `std::string`
inline std::string getDebugDescription(
std::string const &string,
DebugStringConvertibleOptions options) {
return string;
}
// `std::vector<T>`
template <typename T, typename... Ts>
std::string getDebugName(std::vector<T, Ts...> const &vector) {
return "List";
}
template <typename T, typename... Ts>
std::vector<T, Ts...> getDebugChildren(
std::vector<T, Ts...> const &vector,
DebugStringConvertibleOptions options) {
return vector;
}
// `std::array<T, Size>`
template <typename T, size_t Size>
std::string getDebugName(std::array<T, Size> const &array) {
return "List";
}
template <typename T, size_t Size>
std::vector<T> getDebugChildren(
std::array<T, Size> const &array,
DebugStringConvertibleOptions options) {
auto vector = std::vector<T>{};
for (auto const &value : array) {
vector.push_back(value);
}
return vector;
}
// `std::unordered_set<T>`
template <typename T, typename... Ts>
std::string getDebugName(std::unordered_set<T, Ts...> const &set) {
return "Set";
}
template <typename T, typename... Ts>
std::vector<T> getDebugChildren(
std::unordered_set<T, Ts...> const &set,
DebugStringConvertibleOptions options) {
auto vector = std::vector<T>{};
vector.insert(vector.end(), set.begin(), set.end());
return vector;
}
// `std::shared_ptr<T>`
template <typename T>
inline std::string getDebugDescription(
std::shared_ptr<T> const &pointer,
DebugStringConvertibleOptions options) {
return getDebugDescription((void *)pointer.get(), options) + "(shared)";
}
// `std::weak_ptr<T>`
template <typename T>
inline std::string getDebugDescription(
std::weak_ptr<T> const &pointer,
DebugStringConvertibleOptions options) {
return getDebugDescription((void *)pointer.lock().get(), options) + "(weak)";
}
// `std::unique_ptr<T>`
template <typename T>
inline std::string getDebugDescription(
std::unique_ptr<T const> const &pointer,
DebugStringConvertibleOptions options) {
return getDebugDescription((void *)pointer.get(), options) + "(unique)";
}
/*
* Trivial container for `name` and `value` pair that supports
* static `DebugStringConvertible` informal interface.
*/
struct DebugStringConvertibleObject {
std::string name;
std::string value;
};
inline std::string getDebugName(DebugStringConvertibleObject const &object) {
return object.name;
}
inline std::string getDebugValue(DebugStringConvertibleObject const &object) {
return object.value;
}
#endif
} // namespace react
} // namespace facebook