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

#include "cast.h"
#include "path.h"
#include "traits.h"

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

#include <util/generic/maybe.h>
#include <util/generic/typetraits.h>
#include <util/generic/vector.h>
#include <util/system/defaults.h>

namespace NProtoBuf {
    class TConstField {
    public:
        TConstField(const Message& msg, const FieldDescriptor* fd)
            : Msg(msg)
            , Fd(fd)
        {
            Y_ASSERT(Fd && Fd->containing_type() == Msg.GetDescriptor());
        }

        static TMaybe<TConstField> ByPath(const Message& msg, const TStringBuf& path);
        static TMaybe<TConstField> ByPath(const Message& msg, const TVector<const FieldDescriptor*>& fieldsPath);
        static TMaybe<TConstField> ByPath(const Message& msg, const TFieldPath& fieldsPath);

        const Message& Parent() const {
            return Msg;
        }

        const FieldDescriptor* Field() const {
            return Fd;
        }

        bool HasValue() const {
            return IsRepeated() ? Refl().FieldSize(Msg, Fd) > 0
                                : Refl().HasField(Msg, Fd);
        }

        // deprecated, use HasValue() instead
        bool Has() const {
            return HasValue();
        }

        size_t Size() const {
            return IsRepeated() ? Refl().FieldSize(Msg, Fd)
                                : (Refl().HasField(Msg, Fd) ? 1 : 0);
        }

        template <typename T>
        inline typename TSelectCppType<T>::T Get(size_t index = 0) const;

        template <typename TMsg>
        inline const TMsg* GetAs(size_t index = 0) const {
            // casting version of Get
            return IsMessageInstance<TMsg>() ? CheckedCast<const TMsg*>(Get<const Message*>(index)) : nullptr;
        }

        template <typename T>
        bool IsInstance() const {
            return CppType() == TSelectCppType<T>::Result;
        }

        template <typename TMsg>
        bool IsMessageInstance() const {
            return IsMessage() && Fd->message_type() == TMsg::descriptor();
        }

        template <typename TMsg>
        bool IsInstance(std::enable_if_t<std::is_base_of<Message, TMsg>::value && !std::is_same<Message, TMsg>::value, void>* = NULL) const { // template will be selected when specifying Message children types
            return IsMessage() && Fd->message_type() == TMsg::descriptor();
        }

        bool IsString() const {
            return CppType() == FieldDescriptor::CPPTYPE_STRING;
        }

        bool IsMessage() const {
            return CppType() == FieldDescriptor::CPPTYPE_MESSAGE;
        }

        bool HasSameType(const TConstField& other) const {
            if (CppType() != other.CppType())
                return false;
            if (IsMessage() && Field()->message_type() != other.Field()->message_type())
                return false;
            if (CppType() == FieldDescriptor::CPPTYPE_ENUM && Field()->enum_type() != other.Field()->enum_type())
                return false;
            return true;
        }

    protected:
        bool IsRepeated() const {
            return Fd->is_repeated();
        }

        FieldDescriptor::CppType CppType() const {
            return Fd->cpp_type();
        }

        const Reflection& Refl() const {
            return *Msg.GetReflection();
        }

        [[noreturn]] void RaiseUnknown() const {
            ythrow yexception() << "Unknown field cpp-type: " << (size_t)CppType();
        }

        bool IsSameField(const TConstField& other) const {
            return &Parent() == &other.Parent() && Field() == other.Field();
        }

    protected:
        const Message& Msg;
        const FieldDescriptor* Fd;
    };

    class TMutableField: public TConstField {
    public:
        TMutableField(Message& msg, const FieldDescriptor* fd)
            : TConstField(msg, fd)
        {
        }

        static TMaybe<TMutableField> ByPath(Message& msg, const TStringBuf& path, bool createPath = false);
        static TMaybe<TMutableField> ByPath(Message& msg, const TVector<const FieldDescriptor*>& fieldsPath, bool createPath = false);
        static TMaybe<TMutableField> ByPath(Message& msg, const TFieldPath& fieldsPath, bool createPath = false);

        Message* MutableParent() {
            return Mut();
        }

        template <typename T>
        inline void Set(T value, size_t index = 0);

        template <typename T>
        inline void Add(T value);

        inline void MergeFrom(const TConstField& src);

        inline void Clear() {
            Refl().ClearField(Mut(), Fd);
        }
        /*
    void Swap(TMutableField& f) {
        Y_ASSERT(Field() == f.Field()); 

        // not implemented yet, TODO: implement when Reflection::Mutable(Ptr)RepeatedField
        // is ported into arcadia protobuf library from up-stream.
    }
*/
        inline void RemoveLast() {
            Y_ASSERT(HasValue());
            if (IsRepeated())
                Refl().RemoveLast(Mut(), Fd);
            else
                Clear();
        }

        inline void SwapElements(size_t index1, size_t index2) {
            Y_ASSERT(IsRepeated());
            Y_ASSERT(index1 < Size());
            Y_ASSERT(index2 < Size());
            if (index1 == index2)
                return;
            Refl().SwapElements(Mut(), Fd, index1, index2);
        }

        inline void Remove(size_t index) {
            if (index >= Size())
                return;

            // Move to the end
            for (size_t i = index, size = Size(); i < size - 1; ++i)
                SwapElements(i, i + 1);
            RemoveLast();
        }

        Message* MutableMessage(size_t index = 0) {
            Y_ASSERT(IsMessage());
            if (IsRepeated()) {
                Y_ASSERT(index < Size());
                return Refl().MutableRepeatedMessage(Mut(), Fd, index);
            } else {
                Y_ASSERT(index == 0);
                return Refl().MutableMessage(Mut(), Fd);
            }
        }

        template <typename TMsg>
        inline TMsg* AddMessage() {
            return CheckedCast<TMsg*>(AddMessage());
        }

        inline Message* AddMessage() {
            Y_ASSERT(IsMessage() && IsRepeated());
            return Refl().AddMessage(Mut(), Fd);
        }

    private:
        Message* Mut() {
            return const_cast<Message*>(&Msg);
        }

        template <typename T>
        inline void MergeValue(T srcValue);
    };

    // template implementations

    template <typename T>
    inline typename TSelectCppType<T>::T TConstField::Get(size_t index) const {
        Y_ASSERT(index < Size() || !Fd->is_repeated() && index == 0); // Get for single fields is always allowed because of default values
#define TMP_MACRO_FOR_CPPTYPE(CPPTYPE) \
    case CPPTYPE:                      \
        return CompatCast<CPPTYPE, TSelectCppType<T>::Result>(TSimpleFieldTraits<CPPTYPE>::Get(Msg, Fd, index));
        switch (CppType()) {
            APPLY_TMP_MACRO_FOR_ALL_CPPTYPES()
            default:
                RaiseUnknown();
        }
#undef TMP_MACRO_FOR_CPPTYPE
    }

    template <typename T>
    inline void TMutableField::Set(T value, size_t index) {
        Y_ASSERT(!IsRepeated() && index == 0 || index < Size());
#define TMP_MACRO_FOR_CPPTYPE(CPPTYPE)                                                                              \
    case CPPTYPE:                                                                                                   \
        TSimpleFieldTraits<CPPTYPE>::Set(*Mut(), Fd, CompatCast<TSelectCppType<T>::Result, CPPTYPE>(value), index); \
        break;
        switch (CppType()) {
            APPLY_TMP_MACRO_FOR_ALL_CPPTYPES()
            default:
                RaiseUnknown();
        }
#undef TMP_MACRO_FOR_CPPTYPE
    }

    template <typename T>
    inline void TMutableField::Add(T value) {
#define TMP_MACRO_FOR_CPPTYPE(CPPTYPE)                                                                       \
    case CPPTYPE:                                                                                            \
        TSimpleFieldTraits<CPPTYPE>::Add(*Mut(), Fd, CompatCast<TSelectCppType<T>::Result, CPPTYPE>(value)); \
        break;
        switch (CppType()) {
            APPLY_TMP_MACRO_FOR_ALL_CPPTYPES()
            default:
                RaiseUnknown();
        }
#undef TMP_MACRO_FOR_CPPTYPE
    }

    template <typename T>
    inline void TMutableField::MergeValue(T srcValue) {
        Add(srcValue);
    }

    template <>
    inline void TMutableField::MergeValue<const Message*>(const Message* srcValue) {
        if (IsRepeated()) {
            Add(srcValue);
        } else {
            MutableMessage()->MergeFrom(*srcValue);
        }
    }

    inline void TMutableField::MergeFrom(const TConstField& src) {
        Y_ASSERT(HasSameType(src));
        if (IsSameField(src))
            return;
#define TMP_MACRO_FOR_CPPTYPE(CPPTYPE)                                                        \
    case CPPTYPE: {                                                                           \
        for (size_t itemIdx = 0; itemIdx < src.Size(); ++itemIdx) {                           \
            MergeValue(TSimpleFieldTraits<CPPTYPE>::Get(src.Parent(), src.Field(), itemIdx)); \
        }                                                                                     \
        break;                                                                                \
    }
        switch (CppType()) {
            APPLY_TMP_MACRO_FOR_ALL_CPPTYPES()
            default:
                RaiseUnknown();
        }
#undef TMP_MACRO_FOR_CPPTYPE
    }

}