aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/protobuf/util/path.h
blob: 487f643a2d651cdde28238bde21d5400c517e103 (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
#pragma once

#include <google/protobuf/descriptor.h>
#include <google/protobuf/message.h>

#include <util/generic/vector.h>

namespace NProtoBuf {
    class TFieldPath {
    public:
        TFieldPath();
        TFieldPath(const Descriptor* msgType, const TStringBuf& path); // throws exception if path doesn't exist
        TFieldPath(const TVector<const FieldDescriptor*>& path);
        TFieldPath(const TFieldPath&) = default;
        TFieldPath& operator=(const TFieldPath&) = default;

        bool InitUnsafe(const Descriptor* msgType, const TStringBuf path); // noexcept
        void Init(const Descriptor* msgType, const TStringBuf& path);      // throws

        const TVector<const FieldDescriptor*>& Fields() const {
            return Path;
        }

        void AddField(const FieldDescriptor* field) {
            Path.push_back(field);
        }

        const Descriptor* ParentType() const {
            return Empty() ? nullptr : Path.front()->containing_type();
        }

        const FieldDescriptor* FieldDescr() const {
            return Empty() ? nullptr : Path.back();
        }

        bool Empty() const {
            return Path.empty();
        }

        explicit operator bool() const {
            return !Empty();
        }

        bool operator!() const {
            return Empty();
        }

    private:
        TVector<const FieldDescriptor*> Path;
    };

}