xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
libxrpl
beast
utility
beast_PropertyStream.cpp
1
#include <xrpl/beast/core/List.h>
2
#include <xrpl/beast/utility/PropertyStream.h>
3
#include <xrpl/beast/utility/instrumentation.h>
4
5
#include <
algorithm
>
6
#include <
iostream
>
7
#include <
mutex
>
8
#include <
string
>
9
#include <
utility
>
10
11
namespace
beast
{
12
13
//------------------------------------------------------------------------------
14
//
15
// Item
16
//
17
//------------------------------------------------------------------------------
18
19
PropertyStream::Item::Item
(
Source
*
source
) :
source_
(
source
)
20
{
21
}
22
23
PropertyStream::Source
&
24
PropertyStream::Item::source
()
const
25
{
26
return
*
source_
;
27
}
28
29
PropertyStream::Source
*
30
PropertyStream::Item::operator->
()
const
31
{
32
return
&
source
();
33
}
34
35
PropertyStream::Source
&
36
PropertyStream::Item::operator*
()
const
37
{
38
return
source
();
39
}
40
41
//------------------------------------------------------------------------------
42
//
43
// Proxy
44
//
45
//------------------------------------------------------------------------------
46
47
PropertyStream::Proxy::Proxy
(
Map
const
& map,
std::string
key) :
map_
(&map),
key_
(
std
::move(key))
48
{
49
}
50
51
PropertyStream::Proxy::Proxy
(
Proxy
const
& other) :
map_
(other.
map_
),
key_
(other.
key_
)
52
{
53
}
54
55
PropertyStream::Proxy::~Proxy
()
56
{
57
std::string
const
s(
ostream_
.str());
58
if
(!s.
empty
())
59
map_
->add(
key_
, s);
60
}
61
62
std::ostream
&
63
PropertyStream::Proxy::operator<<
(
std::ostream
& manip(
std::ostream
&))
const
64
{
65
return
ostream_
<< manip;
66
}
67
68
//------------------------------------------------------------------------------
69
//
70
// Map
71
//
72
//------------------------------------------------------------------------------
73
74
PropertyStream::Map::Map
(
PropertyStream
&
stream
) :
stream_
(
stream
)
75
{
76
}
77
78
PropertyStream::Map::Map
(
Set
& parent) :
stream_
(parent.
stream
())
79
{
80
stream_
.mapBegin();
81
}
82
83
PropertyStream::Map::Map
(
std::string
const
& key,
Map
& map) :
stream_
(map.
stream
())
84
{
85
stream_
.mapBegin(key);
86
}
87
88
PropertyStream::Map::Map
(
std::string
const
& key,
PropertyStream
&
stream
) :
stream_
(
stream
)
89
{
90
stream_
.mapBegin(key);
91
}
92
93
PropertyStream::Map::~Map
()
94
{
95
stream_
.mapEnd();
96
}
97
98
PropertyStream
&
99
PropertyStream::Map::stream
()
100
{
101
return
stream_
;
102
}
103
104
PropertyStream
const
&
105
PropertyStream::Map::stream
()
const
106
{
107
return
stream_
;
108
}
109
110
PropertyStream::Proxy
111
PropertyStream::Map::operator[]
(
std::string
const
& key)
112
{
113
return
Proxy
(*
this
, key);
114
}
115
116
//------------------------------------------------------------------------------
117
//
118
// Set
119
//
120
//------------------------------------------------------------------------------
121
122
PropertyStream::Set::Set
(
std::string
const
& key,
Map
& map) :
stream_
(map.
stream
())
123
{
124
stream_
.arrayBegin(key);
125
}
126
127
PropertyStream::Set::Set
(
std::string
const
& key,
PropertyStream
&
stream
) :
stream_
(
stream
)
128
{
129
stream_
.arrayBegin(key);
130
}
131
132
PropertyStream::Set::~Set
()
133
{
134
stream_
.arrayEnd();
135
}
136
137
PropertyStream
&
138
PropertyStream::Set::stream
()
139
{
140
return
stream_
;
141
}
142
143
PropertyStream
const
&
144
PropertyStream::Set::stream
()
const
145
{
146
return
stream_
;
147
}
148
149
//------------------------------------------------------------------------------
150
//
151
// Source
152
//
153
//------------------------------------------------------------------------------
154
155
PropertyStream::Source::Source
(
std::string
name
) :
name_
(
std
::move(
name
)),
item_
(this)
156
{
157
}
158
159
PropertyStream::Source::~Source
()
160
{
161
std::scoped_lock
const
_(
lock_
);
162
if
(
parent_
!=
nullptr
)
163
parent_
->remove(*
this
);
164
removeAll
();
165
}
166
167
std::string
const
&
168
PropertyStream::Source::name
()
const
169
{
170
return
name_
;
171
}
172
173
void
174
PropertyStream::Source::add
(
Source
& source)
175
{
176
std::scoped_lock
const
lock(
lock_
, source.
lock_
);
177
178
XRPL_ASSERT(
179
source.
parent_
==
nullptr
,
"beast::PropertyStream::Source::add : null source parent"
);
180
children_
.pushBack(source.
item_
);
181
source.
parent_
=
this
;
182
}
183
184
void
185
PropertyStream::Source::remove
(
Source
& child)
186
{
187
std::scoped_lock
const
lock(
lock_
, child.lock_);
188
189
XRPL_ASSERT(
190
child.parent_ ==
this
,
"beast::PropertyStream::Source::remove : child parent match"
);
191
children_
.erase(
children_
.iteratorTo(child.item_));
192
child.parent_ =
nullptr
;
193
}
194
195
void
196
PropertyStream::Source::removeAll
()
197
{
198
std::scoped_lock
const
_(
lock_
);
199
for
(
auto
iter =
children_
.begin(); iter !=
children_
.end();)
200
{
201
std::scoped_lock
const
cl((*iter)->lock_);
202
remove
(*(*iter));
203
}
204
}
205
206
//------------------------------------------------------------------------------
207
208
void
209
PropertyStream::Source::writeOne
(
PropertyStream
& stream)
210
{
211
Map
map(
name_
, stream);
212
onWrite
(map);
213
}
214
215
void
216
PropertyStream::Source::write
(
PropertyStream
& stream)
217
{
218
Map
map(
name_
, stream);
219
onWrite
(map);
220
221
std::scoped_lock
const
_(
lock_
);
222
223
for
(
auto
& child :
children_
)
224
child.source().write(stream);
225
}
226
227
void
228
PropertyStream::Source::write
(
PropertyStream
& stream,
std::string
const
& path)
229
{
230
std::pair<Source*, bool>
result(
find
(path));
231
232
if
(result.
first
==
nullptr
)
233
return
;
234
235
if
(result.
second
)
236
{
237
result.
first
->write(stream);
238
}
239
else
240
{
241
result.
first
->writeOne(stream);
242
}
243
}
244
245
std::pair<PropertyStream::Source*, bool>
246
PropertyStream::Source::find
(
std::string
path)
247
{
248
bool
const
deep(
peelTrailingSlashstar
(&path));
249
bool
const
rooted(
peelLeadingSlash
(&path));
250
Source
* source(
this
);
251
if
(!path.
empty
())
252
{
253
if
(!rooted)
254
{
255
std::string
const
name
(
peelName
(&path));
256
source =
findOneDeep
(
name
);
257
if
(source ==
nullptr
)
258
return
std::make_pair
(
nullptr
, deep);
259
}
260
source = source->
findPath
(path);
261
}
262
return
std::make_pair
(source, deep);
263
}
264
265
bool
266
PropertyStream::Source::peelLeadingSlash
(
std::string
* path)
267
{
268
if
(!path->
empty
() && path->
front
() ==
'/'
)
269
{
270
*path =
std::string
(path->
begin
() + 1, path->
end
());
271
return
true
;
272
}
273
return
false
;
274
}
275
276
bool
277
PropertyStream::Source::peelTrailingSlashstar
(
std::string
* path)
278
{
279
bool
found(
false
);
280
if
(path->
empty
())
281
return
false
;
282
if
(path->
back
() ==
'*'
)
283
{
284
found =
true
;
285
path->
pop_back
();
286
}
287
if
(!path->
empty
() && path->
back
() ==
'/'
)
288
path->
pop_back
();
289
return
found;
290
}
291
292
std::string
293
PropertyStream::Source::peelName
(
std::string
* path)
294
{
295
if
(path->
empty
())
296
return
""
;
297
298
std::string::const_iterator
const
first = (*path).
begin
();
299
std::string::const_iterator
const
last = (*path).end();
300
std::string::const_iterator
const
pos =
std::find
(first, last,
'/'
);
301
std::string
s(first, pos);
302
303
if
(pos != last)
304
{
305
*path =
std::string
(pos + 1, last);
306
}
307
else
308
{
309
*path =
std::string
();
310
}
311
312
return
s;
313
}
314
315
// Recursive search through the whole tree until name is found
316
PropertyStream::Source
*
317
PropertyStream::Source::findOneDeep
(
std::string
const
&
name
)
318
{
319
Source
* found =
findOne
(
name
);
// NOLINT TODO
320
if
(found !=
nullptr
)
321
return
found;
322
323
std::scoped_lock
const
_(
lock_
);
324
for
(
auto
& s :
children_
)
325
{
326
found = s.source().
findOneDeep
(
name
);
327
if
(found !=
nullptr
)
328
return
found;
329
}
330
return
nullptr
;
331
}
332
333
PropertyStream::Source
*
334
PropertyStream::Source::findPath
(
std::string
path)
335
{
336
if
(path.
empty
())
337
return
this
;
338
Source
* source(
this
);
339
do
340
{
341
std::string
const
name
(
peelName
(&path));
342
if
(
name
.empty())
343
break
;
344
source = source->
findOne
(
name
);
345
}
while
(source !=
nullptr
);
346
return
source;
347
}
348
349
// This function only looks at immediate children
350
// If no immediate children match, then return nullptr
351
PropertyStream::Source
*
352
PropertyStream::Source::findOne
(
std::string
const
&
name
)
353
{
354
std::scoped_lock
const
_(
lock_
);
355
for
(
auto
& s :
children_
)
356
{
357
if
(s.source().name_ ==
name
)
358
return
&s.source();
359
}
360
return
nullptr
;
361
}
362
363
void
364
PropertyStream::Source::onWrite
(
Map
&)
365
{
366
}
367
368
//------------------------------------------------------------------------------
369
//
370
// PropertyStream
371
//
372
//------------------------------------------------------------------------------
373
374
void
375
PropertyStream::add
(
std::string
const
& key,
bool
value)
376
{
377
if
(value)
378
{
379
add
(key,
"true"
);
380
}
381
else
382
{
383
add
(key,
"false"
);
384
}
385
}
386
387
void
388
PropertyStream::add
(
std::string
const
& key,
char
value)
389
{
390
lexicalAdd
(key, value);
391
}
392
393
void
394
PropertyStream::add
(
std::string
const
& key,
signed
char
value)
395
{
396
lexicalAdd
(key, value);
397
}
398
399
void
400
PropertyStream::add
(
std::string
const
& key,
unsigned
char
value)
401
{
402
lexicalAdd
(key, value);
403
}
404
405
void
406
PropertyStream::add
(
std::string
const
& key,
short
value)
407
{
408
lexicalAdd
(key, value);
409
}
410
411
void
412
PropertyStream::add
(
std::string
const
& key,
unsigned
short
value)
413
{
414
lexicalAdd
(key, value);
415
}
416
417
void
418
PropertyStream::add
(
std::string
const
& key,
int
value)
419
{
420
lexicalAdd
(key, value);
421
}
422
423
void
424
PropertyStream::add
(
std::string
const
& key,
unsigned
int
value)
425
{
426
lexicalAdd
(key, value);
427
}
428
429
void
430
PropertyStream::add
(
std::string
const
& key,
long
value)
431
{
432
lexicalAdd
(key, value);
433
}
434
435
void
436
PropertyStream::add
(
std::string
const
& key,
unsigned
long
value)
437
{
438
lexicalAdd
(key, value);
439
}
440
441
void
442
PropertyStream::add
(
std::string
const
& key,
long
long
value)
443
{
444
lexicalAdd
(key, value);
445
}
446
447
void
448
PropertyStream::add
(
std::string
const
& key,
unsigned
long
long
value)
449
{
450
lexicalAdd
(key, value);
451
}
452
453
void
454
PropertyStream::add
(
std::string
const
& key,
float
value)
455
{
456
lexicalAdd
(key, value);
457
}
458
459
void
460
PropertyStream::add
(
std::string
const
& key,
double
value)
461
{
462
lexicalAdd
(key, value);
463
}
464
465
void
466
PropertyStream::add
(
std::string
const
& key,
long
double
value)
467
{
468
lexicalAdd
(key, value);
469
}
470
471
void
472
PropertyStream::add
(
bool
value)
473
{
474
if
(value)
475
{
476
add
(
"true"
);
477
}
478
else
479
{
480
add
(
"false"
);
481
}
482
}
483
484
void
485
PropertyStream::add
(
char
value)
486
{
487
lexicalAdd
(value);
488
}
489
490
void
491
PropertyStream::add
(
signed
char
value)
492
{
493
lexicalAdd
(value);
494
}
495
496
void
497
PropertyStream::add
(
unsigned
char
value)
498
{
499
lexicalAdd
(value);
500
}
501
502
void
503
PropertyStream::add
(
short
value)
504
{
505
lexicalAdd
(value);
506
}
507
508
void
509
PropertyStream::add
(
unsigned
short
value)
510
{
511
lexicalAdd
(value);
512
}
513
514
void
515
PropertyStream::add
(
int
value)
516
{
517
lexicalAdd
(value);
518
}
519
520
void
521
PropertyStream::add
(
unsigned
int
value)
522
{
523
lexicalAdd
(value);
524
}
525
526
void
527
PropertyStream::add
(
long
value)
528
{
529
lexicalAdd
(value);
530
}
531
532
void
533
PropertyStream::add
(
unsigned
long
value)
534
{
535
lexicalAdd
(value);
536
}
537
538
void
539
PropertyStream::add
(
long
long
value)
540
{
541
lexicalAdd
(value);
542
}
543
544
void
545
PropertyStream::add
(
unsigned
long
long
value)
546
{
547
lexicalAdd
(value);
548
}
549
550
void
551
PropertyStream::add
(
float
value)
552
{
553
lexicalAdd
(value);
554
}
555
556
void
557
PropertyStream::add
(
double
value)
558
{
559
lexicalAdd
(value);
560
}
561
562
void
563
PropertyStream::add
(
long
double
value)
564
{
565
lexicalAdd
(value);
566
}
567
568
}
// namespace beast
algorithm
std::string::back
T back(T... args)
std::ostream
std::string
std::string::begin
T begin(T... args)
beast::PropertyStream::Item::source_
Source * source_
Definition
PropertyStream.h:164
beast::PropertyStream::Item::operator*
Source & operator*() const
Definition
beast_PropertyStream.cpp:36
beast::PropertyStream::Item::Item
Item(Source *source)
Definition
beast_PropertyStream.cpp:19
beast::PropertyStream::Item::source
Source & source() const
Definition
beast_PropertyStream.cpp:24
beast::PropertyStream::Item::operator->
Source * operator->() const
Definition
beast_PropertyStream.cpp:30
beast::PropertyStream::Map
Definition
PropertyStream.h:207
beast::PropertyStream::Map::~Map
~Map()
Definition
beast_PropertyStream.cpp:93
beast::PropertyStream::Map::Map
Map(PropertyStream &stream)
Definition
beast_PropertyStream.cpp:74
beast::PropertyStream::Map::operator[]
Proxy operator[](std::string const &key)
Definition
beast_PropertyStream.cpp:111
beast::PropertyStream::Map::stream
PropertyStream & stream()
Definition
beast_PropertyStream.cpp:99
beast::PropertyStream::Map::stream_
PropertyStream & stream_
Definition
PropertyStream.h:209
beast::PropertyStream::Proxy
Definition
PropertyStream.h:174
beast::PropertyStream::Proxy::Proxy
Proxy(Map const &map, std::string key)
Definition
beast_PropertyStream.cpp:47
beast::PropertyStream::Proxy::ostream_
std::ostringstream ostream_
Definition
PropertyStream.h:178
beast::PropertyStream::Proxy::operator<<
std::ostream & operator<<(std::ostream &manip(std::ostream &)) const
Definition
beast_PropertyStream.cpp:63
beast::PropertyStream::Proxy::~Proxy
~Proxy()
Definition
beast_PropertyStream.cpp:55
beast::PropertyStream::Proxy::key_
std::string key_
Definition
PropertyStream.h:177
beast::PropertyStream::Proxy::map_
Map const * map_
Definition
PropertyStream.h:176
beast::PropertyStream::Set
Definition
PropertyStream.h:279
beast::PropertyStream::Set::stream_
PropertyStream & stream_
Definition
PropertyStream.h:281
beast::PropertyStream::Set::stream
PropertyStream & stream()
Definition
beast_PropertyStream.cpp:138
beast::PropertyStream::Set::~Set
~Set()
Definition
beast_PropertyStream.cpp:132
beast::PropertyStream::Set::Set
Set(std::string const &key, Map &map)
Definition
beast_PropertyStream.cpp:122
beast::PropertyStream::Source
Subclasses can be called to write to a stream and have children.
Definition
PropertyStream.h:315
beast::PropertyStream::Source::removeAll
void removeAll()
Remove all child sources from this Source.
Definition
beast_PropertyStream.cpp:196
beast::PropertyStream::Source::peelTrailingSlashstar
static bool peelTrailingSlashstar(std::string *path)
Definition
beast_PropertyStream.cpp:277
beast::PropertyStream::Source::children_
List< Item > children_
Definition
PropertyStream.h:321
beast::PropertyStream::Source::peelName
static std::string peelName(std::string *path)
Definition
beast_PropertyStream.cpp:293
beast::PropertyStream::Source::lock_
std::recursive_mutex lock_
Definition
PropertyStream.h:318
beast::PropertyStream::Source::find
std::pair< Source *, bool > find(std::string path)
Parse the dot-delimited Source path and return the result.
Definition
beast_PropertyStream.cpp:246
beast::PropertyStream::Source::findOne
PropertyStream::Source * findOne(std::string const &name)
Definition
beast_PropertyStream.cpp:352
beast::PropertyStream::Source::findPath
PropertyStream::Source * findPath(std::string path)
Definition
beast_PropertyStream.cpp:334
beast::PropertyStream::Source::name_
std::string const name_
Definition
PropertyStream.h:317
beast::PropertyStream::Source::remove
void remove(Source &child)
Remove a child source from this Source.
Definition
beast_PropertyStream.cpp:185
beast::PropertyStream::Source::name
std::string const & name() const
Returns the name of this source.
Definition
beast_PropertyStream.cpp:168
beast::PropertyStream::Source::findOneDeep
Source * findOneDeep(std::string const &name)
Definition
beast_PropertyStream.cpp:317
beast::PropertyStream::Source::add
void add(Source &source)
Add a child source.
Definition
beast_PropertyStream.cpp:174
beast::PropertyStream::Source::peelLeadingSlash
static bool peelLeadingSlash(std::string *path)
Definition
beast_PropertyStream.cpp:266
beast::PropertyStream::Source::Source
Source(std::string name)
Definition
beast_PropertyStream.cpp:155
beast::PropertyStream::Source::writeOne
void writeOne(PropertyStream &stream)
Write only this Source to the stream.
Definition
beast_PropertyStream.cpp:209
beast::PropertyStream::Source::write
void write(PropertyStream &stream)
write this source and all its children recursively to the stream.
Definition
beast_PropertyStream.cpp:216
beast::PropertyStream::Source::parent_
Source * parent_
Definition
PropertyStream.h:320
beast::PropertyStream::Source::item_
Item item_
Definition
PropertyStream.h:319
beast::PropertyStream::Source::onWrite
virtual void onWrite(Map &)
Subclass override.
Definition
beast_PropertyStream.cpp:364
beast::PropertyStream::Source::~Source
virtual ~Source()
Definition
beast_PropertyStream.cpp:159
beast::PropertyStream::add
virtual void add(std::string const &key, std::string const &value)=0
beast::PropertyStream::PropertyStream
PropertyStream()=default
beast::PropertyStream::lexicalAdd
void lexicalAdd(std::string const &key, Value value)
Definition
PropertyStream.h:47
std::string::empty
T empty(T... args)
std::string::end
T end(T... args)
std::find
T find(T... args)
std::string::front
T front(T... args)
iostream
std::make_pair
T make_pair(T... args)
mutex
beast
Definition
base_uint.h:697
std
STL namespace.
std::pair
std::string::pop_back
T pop_back(T... args)
std::scoped_lock
string
utility
Generated by
1.17.0