aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/protobuf/util/iterators.h
blob: 6d53ac71b15bab0678a70938f528db258ec74b02 (plain) (blame)
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
#pragma once

#include <google/protobuf/descriptor.h>

namespace NProtoBuf {
    class TFieldsIterator {
    public:
        explicit TFieldsIterator(const NProtoBuf::Descriptor* descriptor, int position = 0)
        : Descriptor(descriptor)
        , Position(position)
        { }

        TFieldsIterator& operator++() {
            ++Position;
            return *this;
        }

        TFieldsIterator& operator++(int) {
            auto& ret = *this;
            ++*this;
            return ret;
        }

        const NProtoBuf::FieldDescriptor* operator*() const {
            return Descriptor->field(Position);
        }

        bool operator== (const TFieldsIterator& other) const {
            return Position == other.Position && Descriptor == other.Descriptor;
        }

        bool operator!= (const TFieldsIterator& other) const {
            return !(*this == other);
        }

    private:
        const NProtoBuf::Descriptor* Descriptor = nullptr;
        int Position = 0;
    };
}

// Namespaces required by `range-based for` ADL:
namespace google {
    namespace protobuf {
        NProtoBuf::TFieldsIterator begin(const NProtoBuf::Descriptor& descriptor) {
            return NProtoBuf::TFieldsIterator(&descriptor);
        }

        NProtoBuf::TFieldsIterator end(const NProtoBuf::Descriptor& descriptor) {
            return NProtoBuf::TFieldsIterator(&descriptor, descriptor.field_count());
        }
    }
}