Coverage Report

Created: 2026-10-19 14:15

src/zserio/Walker.h
Line
Count
Source
1
#ifndef ZSERIO_WALKER_H_INC
2
#define ZSERIO_WALKER_H_INC
3
4
#include <algorithm>
5
#include <functional>
6
#include <regex>
7
8
#include "zserio/IReflectable.h"
9
#include "zserio/ITypeInfo.h"
10
#include "zserio/IWalkFilter.h"
11
#include "zserio/IWalkObserver.h"
12
#include "zserio/String.h"
13
#include "zserio/StringConvertUtil.h"
14
#include "zserio/TypeInfoUtil.h"
15
#include "zserio/Types.h"
16
#include "zserio/Vector.h"
17
#include "zserio/WalkerConst.h"
18
19
namespace zserio
20
{
21
22
template <typename ALLOC>
23
class BasicDefaultWalkFilter;
24
25
/**
26
 * Walker through zserio objects, based on generated type info (see -withTypeInfoCode) and
27
 * reflectable interface (see -withReflectionCode).
28
 */
29
template <typename ALLOC = std::allocator<uint8_t>>
30
class BasicWalker
31
{
32
public:
33
    /**
34
     * Constructor using default walk filter.
35
     *
36
     * \param walkObserver Observer to use during walking.
37
     */
38
    explicit BasicWalker(IBasicWalkObserver<ALLOC>& walkObserver);
39
40
    /**
41
     * Constructor.
42
     *
43
     * \param walkObserver Observer to use during walking.
44
     * \param walkFilter Walk filter to use.
45
     */
46
    BasicWalker(IBasicWalkObserver<ALLOC>& walkObserver, IBasicWalkFilter<ALLOC>& walkFilter);
47
48
    /**
49
     * Method generated by default.
50
     */
51
56
    ~BasicWalker() = default;
52
53
    /**
54
     * Copying and moving is disallowed!
55
     * \{
56
     */
57
    BasicWalker(const BasicWalker& other) = delete;
58
    BasicWalker& operator=(const BasicWalker& other) = delete;
59
60
    BasicWalker(BasicWalker&& other) = delete;
61
    BasicWalker& operator=(BasicWalker&& other) = delete;
62
    /**
63
     * \}
64
     */
65
66
    /**
67
     * Walks given reflectable zserio compound object.
68
     *
69
     * \param compound Zserio compound object to walk.
70
     */
71
    void walk(const IBasicReflectableConstPtr<ALLOC>& compound);
72
73
private:
74
    void walkFields(const IBasicReflectableConstPtr<ALLOC>& compound, const IBasicTypeInfo<ALLOC>& typeInfo);
75
    bool walkField(const IBasicReflectableConstPtr<ALLOC>& reflectable, const BasicFieldInfo<ALLOC>& fieldInfo);
76
    bool walkFieldValue(const IBasicReflectableConstPtr<ALLOC>& reflectable,
77
            const BasicFieldInfo<ALLOC>& fieldInfo, size_t elementIndex = WALKER_NOT_ELEMENT);
78
79
    IBasicWalkObserver<ALLOC>& m_walkObserver;
80
    BasicDefaultWalkFilter<ALLOC> m_defaultWalkFilter;
81
    IBasicWalkFilter<ALLOC>& m_walkFilter;
82
};
83
84
/**
85
 * Default walk observer which just does nothing.
86
 */
87
template <typename ALLOC = std::allocator<uint8_t>>
88
class BasicDefaultWalkObserver : public IBasicWalkObserver<ALLOC>
89
{
90
public:
91
    /**
92
     * Method generated by default.
93
     * \{
94
     */
95
14
    BasicDefaultWalkObserver() = default;
96
14
    ~BasicDefaultWalkObserver() override = default;
97
    /**
98
     * \}
99
     */
100
101
    /**
102
     * Copying and moving is disallowed!
103
     * \{
104
     */
105
    BasicDefaultWalkObserver(const BasicDefaultWalkObserver& other) = delete;
106
    BasicDefaultWalkObserver& operator=(const BasicDefaultWalkObserver& other) = delete;
107
108
    BasicDefaultWalkObserver(BasicDefaultWalkObserver&& other) = delete;
109
    BasicDefaultWalkObserver& operator=(BasicDefaultWalkObserver&& other) = delete;
110
    /**
111
     * \}
112
     */
113
114
    void beginRoot(const IBasicReflectableConstPtr<ALLOC>&) override
115
12
    {}
116
    void endRoot(const IBasicReflectableConstPtr<ALLOC>&) override
117
12
    {}
118
119
    void beginArray(const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>&) override
120
3
    {}
121
    void endArray(const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>&) override
122
3
    {}
123
124
    void beginCompound(const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>&, size_t) override
125
3
    {}
126
    void endCompound(const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>&, size_t) override
127
3
    {}
128
129
    void visitValue(const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>&, size_t) override
130
9
    {}
131
};
132
133
/**
134
 * Default walk filter which filters nothing.
135
 */
136
template <typename ALLOC = std::allocator<uint8_t>>
137
class BasicDefaultWalkFilter : public IBasicWalkFilter<ALLOC>
138
{
139
public:
140
    /**
141
     * Method generated by default.
142
     * \{
143
     */
144
92
    BasicDefaultWalkFilter() = default;
145
92
    ~BasicDefaultWalkFilter() override = default;
146
    /**
147
     * \}
148
     */
149
150
    /**
151
     * Copying and moving is disallowed!
152
     * \{
153
     */
154
    BasicDefaultWalkFilter(const BasicDefaultWalkFilter& other) = delete;
155
    BasicDefaultWalkFilter& operator=(const BasicDefaultWalkFilter& other) = delete;
156
157
    BasicDefaultWalkFilter(BasicDefaultWalkFilter&& other) = delete;
158
    BasicDefaultWalkFilter& operator=(BasicDefaultWalkFilter&& other) = delete;
159
    /**
160
     * \}
161
     */
162
163
    bool beforeArray(const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>&) override
164
5
    {
165
5
        return true;
166
5
    }
167
168
    bool afterArray(const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>&) override
169
5
    {
170
5
        return true;
171
5
    }
172
173
    bool beforeCompound(const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>&, size_t) override
174
12
    {
175
12
        return true;
176
12
    }
177
178
    bool afterCompound(const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>&, size_t) override
179
12
    {
180
12
        return true;
181
12
    }
182
183
    bool beforeValue(const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>&, size_t) override
184
48
    {
185
48
        return true;
186
48
    }
187
188
    bool afterValue(const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>&, size_t) override
189
48
    {
190
48
        return true;
191
48
    }
192
};
193
194
/**
195
 * Walk filter which allows to walk only to the given maximum depth.
196
 */
197
template <typename ALLOC = std::allocator<uint8_t>>
198
class BasicDepthWalkFilter : public IBasicWalkFilter<ALLOC>
199
{
200
public:
201
    /**
202
     * Constructor.
203
     *
204
     * \param maxDepth Maximum depth to walk to.
205
     */
206
    explicit BasicDepthWalkFilter(size_t maxDepth);
207
208
    /**
209
     * Method generated by default.
210
     */
211
7
    ~BasicDepthWalkFilter() override = default;
212
213
    /**
214
     * Copying is disallowed!
215
     * \{
216
     */
217
    BasicDepthWalkFilter(const BasicDepthWalkFilter& other) = delete;
218
    BasicDepthWalkFilter& operator=(const BasicDepthWalkFilter& other) = delete;
219
220
    BasicDepthWalkFilter(BasicDepthWalkFilter&& other) = delete;
221
    BasicDepthWalkFilter& operator=(BasicDepthWalkFilter&& other) = delete;
222
    /**
223
     * \}
224
     */
225
226
    bool beforeArray(
227
            const IBasicReflectableConstPtr<ALLOC>& array, const BasicFieldInfo<ALLOC>& fieldInfo) override;
228
    bool afterArray(
229
            const IBasicReflectableConstPtr<ALLOC>& array, const BasicFieldInfo<ALLOC>& fieldInfo) override;
230
231
    bool beforeCompound(const IBasicReflectableConstPtr<ALLOC>& compound,
232
            const BasicFieldInfo<ALLOC>& fieldInfo, size_t elementIndex) override;
233
    bool afterCompound(const IBasicReflectableConstPtr<ALLOC>& compound, const BasicFieldInfo<ALLOC>& fieldInfo,
234
            size_t elementIndex) override;
235
236
    bool beforeValue(const IBasicReflectableConstPtr<ALLOC>& value, const BasicFieldInfo<ALLOC>& fieldInfo,
237
            size_t elementIndex) override;
238
    bool afterValue(const IBasicReflectableConstPtr<ALLOC>& value, const BasicFieldInfo<ALLOC>& fieldInfo,
239
            size_t elementIndex) override;
240
241
private:
242
    bool enterDepthLevel();
243
    bool leaveDepthLevel();
244
245
    size_t m_maxDepth;
246
    size_t m_depth;
247
};
248
249
/**
250
 * Walk filter which allows to walk only paths matching the given regex.
251
 *
252
 * The path is constructed from field names within the root object, thus the root object itself
253
 * is not part of the path.
254
 *
255
 * Array elements have the index appended to the path so that e.g. "compound.arrayField[0]" will match
256
 * only the first element in the array "arrayField".
257
 */
258
template <typename ALLOC = std::allocator<uint8_t>>
259
class BasicRegexWalkFilter : public IBasicWalkFilter<ALLOC>
260
{
261
public:
262
    /**
263
     * Constructor.
264
     *
265
     * \param pathRegex Path regex to use for filtering.
266
     */
267
    explicit BasicRegexWalkFilter(const char* pathRegex, const ALLOC& allocator = ALLOC());
268
269
    /**
270
     * Method generated by default.
271
     */
272
8
    ~BasicRegexWalkFilter() override = default;
273
274
    /**
275
     * Copying is disallowed!
276
     * \{
277
     */
278
    BasicRegexWalkFilter(const BasicRegexWalkFilter& other) = delete;
279
    BasicRegexWalkFilter& operator=(const BasicRegexWalkFilter& other) = delete;
280
281
    BasicRegexWalkFilter(BasicRegexWalkFilter&& other) = delete;
282
    BasicRegexWalkFilter& operator=(BasicRegexWalkFilter&& other) = delete;
283
    /**
284
     * \}
285
     */
286
287
    bool beforeArray(
288
            const IBasicReflectableConstPtr<ALLOC>& array, const BasicFieldInfo<ALLOC>& fieldInfo) override;
289
    bool afterArray(
290
            const IBasicReflectableConstPtr<ALLOC>& array, const BasicFieldInfo<ALLOC>& fieldInfo) override;
291
292
    bool beforeCompound(const IBasicReflectableConstPtr<ALLOC>& compound,
293
            const BasicFieldInfo<ALLOC>& fieldInfo, size_t elementIndex) override;
294
    bool afterCompound(const IBasicReflectableConstPtr<ALLOC>& compound, const BasicFieldInfo<ALLOC>& fieldInfo,
295
            size_t elementIndex) override;
296
297
    bool beforeValue(const IBasicReflectableConstPtr<ALLOC>& value, const BasicFieldInfo<ALLOC>& fieldInfo,
298
            size_t elementIndex) override;
299
    bool afterValue(const IBasicReflectableConstPtr<ALLOC>& value, const BasicFieldInfo<ALLOC>& fieldInfo,
300
            size_t elementIndex) override;
301
302
private:
303
    void appendPath(const BasicFieldInfo<ALLOC>& fieldInfo, size_t elementIndex);
304
    void popPath(const BasicFieldInfo<ALLOC>& fieldInfo, size_t elementIndex);
305
    string<ALLOC> getCurrentPath() const;
306
    bool matchSubtree(
307
            const IBasicReflectableConstPtr<ALLOC>& value, const BasicFieldInfo<ALLOC>& fieldInfo) const;
308
309
    vector<string<ALLOC>, ALLOC> m_currentPath;
310
    std::regex m_pathRegex;
311
    ALLOC m_allocator; // TODO[Mi-L@]: Check how std::regex_match allocates when results are omitted!
312
};
313
314
/**
315
 * Walk filter which allows to walk only to the given maximum array length.
316
 */
317
template <typename ALLOC = std::allocator<uint8_t>>
318
class BasicArrayLengthWalkFilter : public IBasicWalkFilter<ALLOC>
319
{
320
public:
321
    /**
322
     * Constructor.
323
     *
324
     * \param maxArrayLength Maximum array length to walk to.
325
     */
326
    explicit BasicArrayLengthWalkFilter(size_t maxArrayLength);
327
328
    /**
329
     * Method generated by default.
330
     */
331
1
    ~BasicArrayLengthWalkFilter() override = default;
332
333
    /**
334
     * Copying and moving is disallowed!
335
     * \{
336
     */
337
    BasicArrayLengthWalkFilter(const BasicArrayLengthWalkFilter& other) = delete;
338
    BasicArrayLengthWalkFilter& operator=(const BasicArrayLengthWalkFilter& other) = delete;
339
340
    BasicArrayLengthWalkFilter(BasicArrayLengthWalkFilter&& other) = delete;
341
    BasicArrayLengthWalkFilter& operator=(BasicArrayLengthWalkFilter&& other) = delete;
342
    /**
343
     * \}
344
     */
345
346
    bool beforeArray(
347
            const IBasicReflectableConstPtr<ALLOC>& array, const BasicFieldInfo<ALLOC>& fieldInfo) override;
348
    bool afterArray(
349
            const IBasicReflectableConstPtr<ALLOC>& array, const BasicFieldInfo<ALLOC>& fieldInfo) override;
350
351
    bool beforeCompound(const IBasicReflectableConstPtr<ALLOC>& compound,
352
            const BasicFieldInfo<ALLOC>& fieldInfo, size_t elementIndex) override;
353
    bool afterCompound(const IBasicReflectableConstPtr<ALLOC>& compound, const BasicFieldInfo<ALLOC>& fieldInfo,
354
            size_t elementIndex) override;
355
356
    bool beforeValue(const IBasicReflectableConstPtr<ALLOC>& value, const BasicFieldInfo<ALLOC>& fieldInfo,
357
            size_t elementIndex) override;
358
    bool afterValue(const IBasicReflectableConstPtr<ALLOC>& value, const BasicFieldInfo<ALLOC>& fieldInfo,
359
            size_t elementIndex) override;
360
361
private:
362
    bool filterArrayElement(size_t elementIndex);
363
364
    size_t m_maxArrayLength;
365
};
366
367
/**
368
 * Walk filter which implements composition of particular filters.
369
 *
370
 * The filters are called sequentially and logical and is applied on theirs results.
371
 * Note that all filters are always called.
372
 */
373
template <typename ALLOC = std::allocator<uint8_t>>
374
class BasicAndWalkFilter : public IBasicWalkFilter<ALLOC>
375
{
376
public:
377
    using WalkFilterRef = std::reference_wrapper<IBasicWalkFilter<ALLOC>>;
378
    using WalkFilters = vector<WalkFilterRef, ALLOC>;
379
380
    /**
381
     * Constructor.
382
     *
383
     * \param walkFilters List of filters to use in composition.
384
     */
385
    explicit BasicAndWalkFilter(const WalkFilters& walkFilters);
386
387
    /**
388
     * Method generated by default.
389
     */
390
4
    ~BasicAndWalkFilter() override = default;
391
392
    /**
393
     * Copying and moving is disallowed!
394
     * \{
395
     */
396
    BasicAndWalkFilter(const BasicAndWalkFilter& other) = delete;
397
    BasicAndWalkFilter& operator=(const BasicAndWalkFilter& other) = delete;
398
399
    BasicAndWalkFilter(BasicAndWalkFilter&& other) = delete;
400
    BasicAndWalkFilter& operator=(BasicAndWalkFilter&& other) = delete;
401
    /**
402
     * \}
403
     */
404
405
    bool beforeArray(
406
            const IBasicReflectableConstPtr<ALLOC>& array, const BasicFieldInfo<ALLOC>& fieldInfo) override;
407
    bool afterArray(
408
            const IBasicReflectableConstPtr<ALLOC>& array, const BasicFieldInfo<ALLOC>& fieldInfo) override;
409
410
    bool beforeCompound(const IBasicReflectableConstPtr<ALLOC>& compound,
411
            const BasicFieldInfo<ALLOC>& fieldInfo, size_t elementIndex) override;
412
    bool afterCompound(const IBasicReflectableConstPtr<ALLOC>& compound, const BasicFieldInfo<ALLOC>& fieldInfo,
413
            size_t elementIndex) override;
414
415
    bool beforeValue(const IBasicReflectableConstPtr<ALLOC>& value, const BasicFieldInfo<ALLOC>& fieldInfo,
416
            size_t elementIndex) override;
417
    bool afterValue(const IBasicReflectableConstPtr<ALLOC>& value, const BasicFieldInfo<ALLOC>& fieldInfo,
418
            size_t elementIndex) override;
419
420
private:
421
    template <typename FILTER_FUNC, typename... ARGS>
422
    bool applyFilters(FILTER_FUNC filterFunc, ARGS... args)
423
24
    {
424
24
        bool result = true;
425
24
        for (IBasicWalkFilter<ALLOC>& walkFilter : m_walkFilters)
426
36
        {
427
36
            result &= (walkFilter.*filterFunc)(args...);
428
36
        }
429
24
        return result;
430
24
    }
431
432
    WalkFilters m_walkFilters;
433
};
434
435
/** Typedefs to walker related classes provided for convenience - using default std::allocator<uint8_t>. */
436
/** \{ */
437
using Walker = BasicWalker<>;
438
using DefaultWalkObserver = BasicDefaultWalkObserver<>;
439
using DefaultWalkFilter = BasicDefaultWalkFilter<>;
440
using DepthWalkFilter = BasicDepthWalkFilter<>;
441
using RegexWalkFilter = BasicRegexWalkFilter<>;
442
using ArrayLengthWalkFilter = BasicArrayLengthWalkFilter<>;
443
using AndWalkFilter = BasicAndWalkFilter<>;
444
/** \} */
445
446
template <typename ALLOC>
447
BasicWalker<ALLOC>::BasicWalker(IBasicWalkObserver<ALLOC>& walkObserver) :
448
        m_walkObserver(walkObserver),
449
        m_walkFilter(m_defaultWalkFilter)
450
2
{}
451
452
template <typename ALLOC>
453
BasicWalker<ALLOC>::BasicWalker(IBasicWalkObserver<ALLOC>& walkObserver, IBasicWalkFilter<ALLOC>& walkFilter) :
454
        m_walkObserver(walkObserver),
455
        m_walkFilter(walkFilter)
456
54
{}
457
458
template <typename ALLOC>
459
void BasicWalker<ALLOC>::walk(const IBasicReflectableConstPtr<ALLOC>& compound)
460
56
{
461
56
    if (!compound)
462
1
    {
463
1
        throw CppRuntimeException("Walker: Root object cannot be NULL!");
464
1
    }
465
466
55
    const IBasicTypeInfo<ALLOC>& typeInfo = compound->getTypeInfo();
467
55
    if (!TypeInfoUtil::isCompound(typeInfo.getSchemaType()))
468
1
    {
469
1
        throw CppRuntimeException("Walker: Root object '")
470
1
                << typeInfo.getSchemaName() << "' is not a compound type!";
471
1
    }
472
473
54
    m_walkObserver.beginRoot(compound);
474
54
    walkFields(compound, typeInfo);
475
54
    m_walkObserver.endRoot(compound);
476
54
}
477
478
template <typename ALLOC>
479
void BasicWalker<ALLOC>::walkFields(
480
        const IBasicReflectableConstPtr<ALLOC>& compound, const IBasicTypeInfo<ALLOC>& typeInfo)
481
71
{
482
71
    if (TypeInfoUtil::hasChoice(typeInfo.getSchemaType()))
483
21
    {
484
21
        StringView compoundChoice = compound->getChoice();
485
21
        if (!compoundChoice.empty())
486
18
        {
487
18
            Span<const BasicFieldInfo<ALLOC>> fields = typeInfo.getFields();
488
18
            auto fieldsIt = std::find_if(
489
38
                    fields.begin(), fields.end(), [compoundChoice](const BasicFieldInfo<ALLOC>& fieldInfo) {
490
38
                        return fieldInfo.schemaName == compoundChoice;
491
38
                    });
492
18
            if (fieldsIt != fields.end())
493
17
            {
494
17
                walkField(compound->getField(compoundChoice), *fieldsIt);
495
17
            }
496
18
        }
497
        // else uninitialized or empty branch
498
21
    }
499
50
    else
500
50
    {
501
50
        for (const BasicFieldInfo<ALLOC>& fieldInfo : typeInfo.getFields())
502
75
        {
503
75
            if (!walkField(compound->getField(fieldInfo.schemaName), fieldInfo))
504
3
            {
505
3
                break;
506
3
            }
507
75
        }
508
50
    }
509
71
}
510
511
template <typename ALLOC>
512
bool BasicWalker<ALLOC>::walkField(
513
        const IBasicReflectableConstPtr<ALLOC>& reflectable, const BasicFieldInfo<ALLOC>& fieldInfo)
514
92
{
515
92
    if (reflectable && 
fieldInfo.isArray87
)
516
10
    {
517
10
        if (m_walkFilter.beforeArray(reflectable, fieldInfo))
518
8
        {
519
8
            m_walkObserver.beginArray(reflectable, fieldInfo);
520
21
            for (size_t i = 0; i < reflectable->size(); 
++i13
)
521
14
            {
522
14
                if (!walkFieldValue(reflectable->at(i), fieldInfo, i))
523
1
                {
524
1
                    break;
525
1
                }
526
14
            }
527
8
            m_walkObserver.endArray(reflectable, fieldInfo);
528
8
        }
529
10
        return m_walkFilter.afterArray(reflectable, fieldInfo);
530
10
    }
531
82
    else
532
82
    {
533
82
        return walkFieldValue(reflectable, fieldInfo);
534
82
    }
535
92
}
536
537
template <typename ALLOC>
538
bool BasicWalker<ALLOC>::walkFieldValue(const IBasicReflectableConstPtr<ALLOC>& reflectable,
539
        const BasicFieldInfo<ALLOC>& fieldInfo, size_t elementIndex)
540
96
{
541
96
    const IBasicTypeInfo<ALLOC>& typeInfo = fieldInfo.typeInfo;
542
96
    if (reflectable && 
TypeInfoUtil::isCompound(typeInfo.getSchemaType())91
)
543
22
    {
544
22
        if (m_walkFilter.beforeCompound(reflectable, fieldInfo, elementIndex))
545
17
        {
546
17
            m_walkObserver.beginCompound(reflectable, fieldInfo, elementIndex);
547
17
            walkFields(reflectable, typeInfo);
548
17
            m_walkObserver.endCompound(reflectable, fieldInfo, elementIndex);
549
17
        }
550
22
        return m_walkFilter.afterCompound(reflectable, fieldInfo, elementIndex);
551
22
    }
552
74
    else
553
74
    {
554
74
        if (m_walkFilter.beforeValue(reflectable, fieldInfo, elementIndex))
555
68
        {
556
68
            m_walkObserver.visitValue(reflectable, fieldInfo, elementIndex);
557
68
        }
558
74
        return m_walkFilter.afterValue(reflectable, fieldInfo, elementIndex);
559
74
    }
560
96
}
561
562
template <typename ALLOC>
563
BasicDepthWalkFilter<ALLOC>::BasicDepthWalkFilter(size_t maxDepth) :
564
        m_maxDepth(maxDepth),
565
        m_depth(1)
566
7
{}
567
568
template <typename ALLOC>
569
bool BasicDepthWalkFilter<ALLOC>::beforeArray(
570
        const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>&)
571
4
{
572
4
    return enterDepthLevel();
573
4
}
574
575
template <typename ALLOC>
576
bool BasicDepthWalkFilter<ALLOC>::afterArray(
577
        const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>&)
578
4
{
579
4
    return leaveDepthLevel();
580
4
}
581
582
template <typename ALLOC>
583
bool BasicDepthWalkFilter<ALLOC>::beforeCompound(
584
        const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>&, size_t)
585
4
{
586
4
    return enterDepthLevel();
587
4
}
588
589
template <typename ALLOC>
590
bool BasicDepthWalkFilter<ALLOC>::afterCompound(
591
        const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>&, size_t)
592
4
{
593
4
    return leaveDepthLevel();
594
4
}
595
596
template <typename ALLOC>
597
bool BasicDepthWalkFilter<ALLOC>::beforeValue(
598
        const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>&, size_t)
599
9
{
600
9
    return m_depth <= m_maxDepth;
601
9
}
602
603
template <typename ALLOC>
604
bool BasicDepthWalkFilter<ALLOC>::afterValue(
605
        const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>&, size_t)
606
9
{
607
9
    return true;
608
9
}
609
610
template <typename ALLOC>
611
bool BasicDepthWalkFilter<ALLOC>::enterDepthLevel()
612
8
{
613
8
    const bool enter = (m_depth <= m_maxDepth);
614
8
    m_depth += 1;
615
8
    return enter;
616
8
}
617
618
template <typename ALLOC>
619
bool BasicDepthWalkFilter<ALLOC>::leaveDepthLevel()
620
8
{
621
8
    m_depth -= 1;
622
8
    return true;
623
8
}
624
625
namespace detail
626
{
627
628
template <typename ALLOC>
629
string<ALLOC> getCurrentPathImpl(const vector<string<ALLOC>, ALLOC>& currentPath, const ALLOC& allocator)
630
31
{
631
31
    string<ALLOC> currentPathStr(allocator);
632
80
    for (auto it = currentPath.begin(); it != currentPath.end(); 
++it49
)
633
49
    {
634
49
        if (!currentPathStr.empty())
635
18
        {
636
18
            currentPathStr += ".";
637
18
        }
638
49
        currentPathStr += *it;
639
49
    }
640
31
    return currentPathStr;
641
31
}
642
643
template <typename ALLOC>
644
void appendPathImpl(vector<string<ALLOC>, ALLOC>& currentPath, const BasicFieldInfo<ALLOC>& fieldInfo,
645
        size_t elementIndex, const ALLOC& allocator)
646
23
{
647
23
    if (elementIndex == WALKER_NOT_ELEMENT)
648
18
    {
649
18
        currentPath.emplace_back(fieldInfo.schemaName.data(), fieldInfo.schemaName.size(), allocator);
650
18
    }
651
5
    else
652
5
    {
653
5
        currentPath.back() =
654
5
                toString(fieldInfo.schemaName, allocator) + "[" + toString(elementIndex, allocator) + "]";
655
5
    }
656
23
}
657
658
template <typename ALLOC>
659
void popPathImpl(vector<string<ALLOC>, ALLOC>& currentPath, const BasicFieldInfo<ALLOC>& fieldInfo,
660
        size_t elementIndex, const ALLOC& allocator)
661
23
{
662
23
    if (elementIndex == WALKER_NOT_ELEMENT)
663
18
    {
664
18
        currentPath.pop_back();
665
18
    }
666
5
    else
667
5
    {
668
5
        currentPath.back() = toString(fieldInfo.schemaName, allocator);
669
5
    }
670
23
}
671
672
template <typename ALLOC>
673
class SubtreeRegexWalkFilter : public IBasicWalkFilter<ALLOC>
674
{
675
public:
676
    SubtreeRegexWalkFilter(const vector<string<ALLOC>, ALLOC>& currentPath, const std::regex& pathRegex,
677
            const ALLOC& allocator) :
678
            m_currentPath(currentPath),
679
            m_pathRegex(pathRegex),
680
            m_allocator(allocator)
681
11
    {}
682
683
    bool matches()
684
11
    {
685
11
        return m_matches;
686
11
    }
687
688
    bool beforeArray(const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>& fieldInfo) override
689
4
    {
690
4
        m_currentPath.emplace_back(fieldInfo.schemaName.data(), fieldInfo.schemaName.size());
691
4
        m_matches = std::regex_match(getCurrentPath(), m_pathRegex);
692
693
        // terminate when the match is already found (note that array is never null here)
694
4
        return !m_matches;
695
4
    }
696
697
    bool afterArray(const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>&) override
698
4
    {
699
4
        m_currentPath.pop_back();
700
4
        return !m_matches; // terminate when the match is already found
701
4
    }
702
703
    bool beforeCompound(const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>& fieldInfo,
704
            size_t elementIndex) override
705
2
    {
706
2
        appendPath(fieldInfo, elementIndex);
707
2
        m_matches = std::regex_match(getCurrentPath(), m_pathRegex);
708
709
        // terminate when the match is already found (note that compound is never null here)
710
2
        return !m_matches;
711
2
    }
712
713
    bool afterCompound(const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>& fieldInfo,
714
            size_t elementIndex) override
715
2
    {
716
2
        popPath(fieldInfo, elementIndex);
717
2
        return !m_matches; // terminate when the match is already found
718
2
    }
719
720
    bool beforeValue(const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>& fieldInfo,
721
            size_t elementIndex) override
722
9
    {
723
9
        appendPath(fieldInfo, elementIndex);
724
9
        m_matches = std::regex_match(getCurrentPath(), m_pathRegex);
725
726
9
        return !m_matches; // terminate when the match is already found
727
9
    }
728
729
    bool afterValue(const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>& fieldInfo,
730
            size_t elementIndex) override
731
9
    {
732
9
        popPath(fieldInfo, elementIndex);
733
9
        return !m_matches; // terminate when the match is already found
734
9
    }
735
736
private:
737
    string<ALLOC> getCurrentPath() const
738
15
    {
739
15
        return detail::getCurrentPathImpl(m_currentPath, m_allocator);
740
15
    }
741
742
    void appendPath(const BasicFieldInfo<ALLOC>& fieldInfo, size_t elementIndex)
743
11
    {
744
11
        detail::appendPathImpl(m_currentPath, fieldInfo, elementIndex, m_allocator);
745
11
    }
746
747
    void popPath(const BasicFieldInfo<ALLOC>& fieldInfo, size_t elementIndex)
748
11
    {
749
11
        detail::popPathImpl(m_currentPath, fieldInfo, elementIndex, m_allocator);
750
11
    }
751
752
    vector<string<ALLOC>, ALLOC> m_currentPath;
753
    std::regex m_pathRegex;
754
    ALLOC m_allocator;
755
    bool m_matches = false;
756
};
757
758
} // namespace detail
759
760
template <typename ALLOC>
761
BasicRegexWalkFilter<ALLOC>::BasicRegexWalkFilter(const char* pathRegex, const ALLOC& allocator) :
762
        m_pathRegex(pathRegex),
763
        m_allocator(allocator)
764
8
{}
765
766
template <typename ALLOC>
767
bool BasicRegexWalkFilter<ALLOC>::beforeArray(
768
        const IBasicReflectableConstPtr<ALLOC>& array, const BasicFieldInfo<ALLOC>& fieldInfo)
769
4
{
770
4
    m_currentPath.emplace_back(fieldInfo.schemaName.data(), fieldInfo.schemaName.size(), m_allocator);
771
772
4
    if (std::regex_match(getCurrentPath(), m_pathRegex))
773
1
    {
774
1
        return true; // the array itself matches
775
1
    }
776
777
9
    
for (size_t i = 0; 3
i < array->size();
++i6
)
778
7
    {
779
7
        m_currentPath.back() =
780
7
                toString(fieldInfo.schemaName, m_allocator) + "[" + toString(i, m_allocator) + "]";
781
782
7
        if (matchSubtree(array->at(i), fieldInfo))
783
1
        {
784
1
            return true;
785
1
        }
786
7
    }
787
788
2
    m_currentPath.back() = toString(fieldInfo.schemaName, m_allocator);
789
790
2
    return false;
791
3
}
792
793
template <typename ALLOC>
794
bool BasicRegexWalkFilter<ALLOC>::afterArray(
795
        const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>&)
796
4
{
797
4
    m_currentPath.pop_back();
798
4
    return true;
799
4
}
800
801
template <typename ALLOC>
802
bool BasicRegexWalkFilter<ALLOC>::beforeCompound(const IBasicReflectableConstPtr<ALLOC>& compound,
803
        const BasicFieldInfo<ALLOC>& fieldInfo, size_t elementIndex)
804
5
{
805
5
    appendPath(fieldInfo, elementIndex);
806
5
    if (std::regex_match(getCurrentPath(), m_pathRegex))
807
1
    {
808
1
        return true; // the compound itself matches
809
1
    }
810
811
4
    return matchSubtree(compound, fieldInfo);
812
5
}
813
814
template <typename ALLOC>
815
bool BasicRegexWalkFilter<ALLOC>::afterCompound(
816
        const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>& fieldInfo, size_t elementIndex)
817
5
{
818
5
    popPath(fieldInfo, elementIndex);
819
5
    return true;
820
5
}
821
822
template <typename ALLOC>
823
bool BasicRegexWalkFilter<ALLOC>::beforeValue(const IBasicReflectableConstPtr<ALLOC>& value,
824
        const BasicFieldInfo<ALLOC>& fieldInfo, size_t elementIndex)
825
7
{
826
7
    appendPath(fieldInfo, elementIndex);
827
7
    return matchSubtree(value, fieldInfo);
828
7
}
829
830
template <typename ALLOC>
831
bool BasicRegexWalkFilter<ALLOC>::afterValue(
832
        const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>& fieldInfo, size_t elementIndex)
833
7
{
834
7
    popPath(fieldInfo, elementIndex);
835
7
    return true;
836
7
}
837
838
template <typename ALLOC>
839
void BasicRegexWalkFilter<ALLOC>::appendPath(const BasicFieldInfo<ALLOC>& fieldInfo, size_t elementIndex)
840
12
{
841
12
    detail::appendPathImpl(m_currentPath, fieldInfo, elementIndex, m_allocator);
842
12
}
843
844
template <typename ALLOC>
845
void BasicRegexWalkFilter<ALLOC>::popPath(const BasicFieldInfo<ALLOC>& fieldInfo, size_t elementIndex)
846
12
{
847
12
    detail::popPathImpl(m_currentPath, fieldInfo, elementIndex, m_allocator);
848
12
}
849
850
template <typename ALLOC>
851
string<ALLOC> BasicRegexWalkFilter<ALLOC>::getCurrentPath() const
852
16
{
853
16
    return detail::getCurrentPathImpl(m_currentPath, m_allocator);
854
16
}
855
856
template <typename ALLOC>
857
bool BasicRegexWalkFilter<ALLOC>::matchSubtree(
858
        const IBasicReflectableConstPtr<ALLOC>& value, const BasicFieldInfo<ALLOC>& fieldInfo) const
859
18
{
860
18
    if (value != nullptr && 
TypeInfoUtil::isCompound(fieldInfo.typeInfo.getSchemaType())13
)
861
11
    {
862
        // is a not null compound, try to find match within its subtree
863
11
        BasicDefaultWalkObserver<ALLOC> defaultObserver;
864
11
        detail::SubtreeRegexWalkFilter<ALLOC> subtreeFilter(m_currentPath, m_pathRegex, m_allocator);
865
11
        BasicWalker<ALLOC> walker(defaultObserver, subtreeFilter);
866
11
        walker.walk(value);
867
11
        return subtreeFilter.matches();
868
11
    }
869
7
    else
870
7
    {
871
        // try to match a simple value or null compound
872
7
        return std::regex_match(getCurrentPath(), m_pathRegex);
873
7
    }
874
18
}
875
876
template <typename ALLOC>
877
BasicArrayLengthWalkFilter<ALLOC>::BasicArrayLengthWalkFilter(size_t maxArrayLength) :
878
        m_maxArrayLength(maxArrayLength)
879
1
{}
880
881
template <typename ALLOC>
882
bool BasicArrayLengthWalkFilter<ALLOC>::beforeArray(
883
        const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>&)
884
2
{
885
2
    return true;
886
2
}
887
888
template <typename ALLOC>
889
bool BasicArrayLengthWalkFilter<ALLOC>::afterArray(
890
        const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>&)
891
2
{
892
2
    return true;
893
2
}
894
895
template <typename ALLOC>
896
bool BasicArrayLengthWalkFilter<ALLOC>::beforeCompound(
897
        const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>&, size_t elementIndex)
898
2
{
899
2
    return filterArrayElement(elementIndex);
900
2
}
901
902
template <typename ALLOC>
903
bool BasicArrayLengthWalkFilter<ALLOC>::afterCompound(
904
        const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>&, size_t elementIndex)
905
2
{
906
2
    return filterArrayElement(elementIndex);
907
2
}
908
909
template <typename ALLOC>
910
bool BasicArrayLengthWalkFilter<ALLOC>::beforeValue(
911
        const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>&, size_t elementIndex)
912
3
{
913
3
    return filterArrayElement(elementIndex);
914
3
}
915
916
template <typename ALLOC>
917
bool BasicArrayLengthWalkFilter<ALLOC>::afterValue(
918
        const IBasicReflectableConstPtr<ALLOC>&, const BasicFieldInfo<ALLOC>&, size_t elementIndex)
919
3
{
920
3
    return filterArrayElement(elementIndex);
921
3
}
922
923
template <typename ALLOC>
924
bool BasicArrayLengthWalkFilter<ALLOC>::filterArrayElement(size_t elementIndex)
925
10
{
926
10
    return elementIndex == WALKER_NOT_ELEMENT ? 
true4
:
elementIndex < m_maxArrayLength6
;
927
10
}
928
929
template <typename ALLOC>
930
BasicAndWalkFilter<ALLOC>::BasicAndWalkFilter(const WalkFilters& walkFilters) :
931
        m_walkFilters(walkFilters)
932
4
{}
933
934
template <typename ALLOC>
935
bool BasicAndWalkFilter<ALLOC>::beforeArray(
936
        const IBasicReflectableConstPtr<ALLOC>& array, const BasicFieldInfo<ALLOC>& fieldInfo)
937
4
{
938
4
    return applyFilters(&IBasicWalkFilter<ALLOC>::beforeArray, array, fieldInfo);
939
4
}
940
941
template <typename ALLOC>
942
bool BasicAndWalkFilter<ALLOC>::afterArray(
943
        const IBasicReflectableConstPtr<ALLOC>& array, const BasicFieldInfo<ALLOC>& fieldInfo)
944
4
{
945
4
    return applyFilters(&IBasicWalkFilter<ALLOC>::afterArray, array, fieldInfo);
946
4
}
947
948
template <typename ALLOC>
949
bool BasicAndWalkFilter<ALLOC>::beforeCompound(const IBasicReflectableConstPtr<ALLOC>& compound,
950
        const BasicFieldInfo<ALLOC>& fieldInfo, size_t elementIndex)
951
4
{
952
4
    return applyFilters(&IBasicWalkFilter<ALLOC>::beforeCompound, compound, fieldInfo, elementIndex);
953
4
}
954
955
template <typename ALLOC>
956
bool BasicAndWalkFilter<ALLOC>::afterCompound(const IBasicReflectableConstPtr<ALLOC>& compound,
957
        const BasicFieldInfo<ALLOC>& fieldInfo, size_t elementIndex)
958
4
{
959
4
    return applyFilters(&IBasicWalkFilter<ALLOC>::afterCompound, compound, fieldInfo, elementIndex);
960
4
}
961
962
template <typename ALLOC>
963
bool BasicAndWalkFilter<ALLOC>::beforeValue(const IBasicReflectableConstPtr<ALLOC>& value,
964
        const BasicFieldInfo<ALLOC>& fieldInfo, size_t elementIndex)
965
4
{
966
4
    return applyFilters(&IBasicWalkFilter<ALLOC>::beforeValue, value, fieldInfo, elementIndex);
967
4
}
968
969
template <typename ALLOC>
970
bool BasicAndWalkFilter<ALLOC>::afterValue(const IBasicReflectableConstPtr<ALLOC>& value,
971
        const BasicFieldInfo<ALLOC>& fieldInfo, size_t elementIndex)
972
4
{
973
4
    return applyFilters(&IBasicWalkFilter<ALLOC>::afterValue, value, fieldInfo, elementIndex);
974
4
}
975
976
} // namespace zserio
977
978
#endif // ZSERIO_WALKER_H_INC