aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/protobuf/util/traits.h
blob: 8f00b6d6e1c3b9b52f8e25ab499fe11fb9a23276 (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#pragma once

#include <util/generic/typetraits.h>

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

namespace NProtoBuf {
// this nasty windows.h macro interfers with protobuf::Reflection::GetMessage()
#if defined(GetMessage)
#undef GetMessage
#endif

    struct TCppTypeTraitsBase { 
        static inline bool Has(const Message& msg, const FieldDescriptor* field) { // non-repeated 
            return msg.GetReflection()->HasField(msg, field); 
        } 
        static inline size_t Size(const Message& msg, const FieldDescriptor* field) { // repeated 
            return msg.GetReflection()->FieldSize(msg, field); 
        } 

        static inline void Clear(Message& msg, const FieldDescriptor* field) { 
            msg.GetReflection()->ClearField(&msg, field); 
        } 

        static inline void RemoveLast(Message& msg, const FieldDescriptor* field) { 
            msg.GetReflection()->RemoveLast(&msg, field); 
        } 

        static inline void SwapElements(Message& msg, const FieldDescriptor* field, int index1, int index2) { 
            msg.GetReflection()->SwapElements(&msg, field, index1, index2); 
        } 
    }; 

    // default value accessor 
    template <FieldDescriptor::CppType cpptype> 
    struct TCppTypeTraitsDefault; 

#define DECLARE_CPPTYPE_DEFAULT(cpptype, method)          \ 
    template <>                                           \ 
    struct TCppTypeTraitsDefault<cpptype> {               \ 
        static auto GetDefault(const FieldDescriptor* fd) \ 
            -> decltype(fd->default_value_##method()) {   \ 
            Y_ASSERT(fd);                                 \ 
            return fd->default_value_##method();          \ 
        }                                                 \ 
    };

    DECLARE_CPPTYPE_DEFAULT(FieldDescriptor::CppType::CPPTYPE_INT32, int32); 
    DECLARE_CPPTYPE_DEFAULT(FieldDescriptor::CppType::CPPTYPE_INT64, int64); 
    DECLARE_CPPTYPE_DEFAULT(FieldDescriptor::CppType::CPPTYPE_UINT32, uint32); 
    DECLARE_CPPTYPE_DEFAULT(FieldDescriptor::CppType::CPPTYPE_UINT64, uint64); 
    DECLARE_CPPTYPE_DEFAULT(FieldDescriptor::CppType::CPPTYPE_FLOAT, float); 
    DECLARE_CPPTYPE_DEFAULT(FieldDescriptor::CppType::CPPTYPE_DOUBLE, double); 
    DECLARE_CPPTYPE_DEFAULT(FieldDescriptor::CppType::CPPTYPE_BOOL, bool); 
    DECLARE_CPPTYPE_DEFAULT(FieldDescriptor::CppType::CPPTYPE_ENUM, enum); 
    DECLARE_CPPTYPE_DEFAULT(FieldDescriptor::CppType::CPPTYPE_STRING, string); 

#undef DECLARE_CPPTYPE_DEFAULT

    // getters/setters of field with specified CppType 
    template <FieldDescriptor::CppType cpptype> 
    struct TCppTypeTraits : TCppTypeTraitsBase { 
        static const FieldDescriptor::CppType CppType = cpptype; 

        struct T {}; 
        static T Get(const Message& msg, const FieldDescriptor* field); 
        static T GetRepeated(const Message& msg, const FieldDescriptor* field, int index); 
        static T GetDefault(const FieldDescriptor* field); 

        static void Set(Message& msg, const FieldDescriptor* field, T value); 
        static void AddRepeated(Message& msg, const FieldDescriptor* field, T value); 
        static void SetRepeated(Message& msg, const FieldDescriptor* field, int index, T value); 
    }; 

    // any type T -> CppType 
    template <typename T> 
    struct TSelectCppType { 
        //static const FieldDescriptor::CppType Result = FieldDescriptor::MAX_CPPTYPE; 
    }; 

#define DECLARE_CPPTYPE_TRAITS(cpptype, type, method)                                                    \ 
    template <>                                                                                          \ 
    struct TCppTypeTraits<cpptype>: public TCppTypeTraitsBase {                                          \ 
        typedef type T;                                                                                  \ 
        static const FieldDescriptor::CppType CppType = cpptype;                                         \ 
                                                                                                         \ 
        static inline T Get(const Message& msg, const FieldDescriptor* field) {                          \ 
            return msg.GetReflection()->Get##method(msg, field);                                         \ 
        }                                                                                                \ 
        static inline T GetRepeated(const Message& msg, const FieldDescriptor* field, int index) {       \ 
            return msg.GetReflection()->GetRepeated##method(msg, field, index);                          \ 
        }                                                                                                \ 
        static inline T GetDefault(const FieldDescriptor* field) {                                       \ 
            return TCppTypeTraitsDefault<cpptype>::GetDefault(field);                                    \ 
        }                                                                                                \ 
        static inline void Set(Message& msg, const FieldDescriptor* field, T value) {                    \ 
            msg.GetReflection()->Set##method(&msg, field, value);                                        \ 
        }                                                                                                \ 
        static inline void AddRepeated(Message& msg, const FieldDescriptor* field, T value) {            \ 
            msg.GetReflection()->Add##method(&msg, field, value);                                        \ 
        }                                                                                                \ 
        static inline void SetRepeated(Message& msg, const FieldDescriptor* field, int index, T value) { \ 
            msg.GetReflection()->SetRepeated##method(&msg, field, index, value);                         \ 
        }                                                                                                \ 
    };                                                                                                   \ 
    template <>                                                                                          \ 
    struct TSelectCppType<type> {                                                                        \ 
        static const FieldDescriptor::CppType Result = cpptype;                                          \ 
        typedef type T;                                                                                  \ 
    };

    DECLARE_CPPTYPE_TRAITS(FieldDescriptor::CPPTYPE_INT32, i32, Int32); 
    DECLARE_CPPTYPE_TRAITS(FieldDescriptor::CPPTYPE_INT64, i64, Int64); 
    DECLARE_CPPTYPE_TRAITS(FieldDescriptor::CPPTYPE_UINT32, ui32, UInt32); 
    DECLARE_CPPTYPE_TRAITS(FieldDescriptor::CPPTYPE_UINT64, ui64, UInt64); 
    DECLARE_CPPTYPE_TRAITS(FieldDescriptor::CPPTYPE_DOUBLE, double, Double); 
    DECLARE_CPPTYPE_TRAITS(FieldDescriptor::CPPTYPE_FLOAT, float, Float); 
    DECLARE_CPPTYPE_TRAITS(FieldDescriptor::CPPTYPE_BOOL, bool, Bool); 
    DECLARE_CPPTYPE_TRAITS(FieldDescriptor::CPPTYPE_ENUM, const EnumValueDescriptor*, Enum); 
    DECLARE_CPPTYPE_TRAITS(FieldDescriptor::CPPTYPE_STRING, TString, String); 
    //DECLARE_CPPTYPE_TRAITS(FieldDescriptor::CPPTYPE_MESSAGE, const Message&, Message); 

#undef DECLARE_CPPTYPE_TRAITS

    // specialization for message pointer 
    template <> 
    struct TCppTypeTraits<FieldDescriptor::CPPTYPE_MESSAGE>: public TCppTypeTraitsBase { 
        typedef const Message* T; 
        static const FieldDescriptor::CppType CppType = FieldDescriptor::CPPTYPE_MESSAGE; 

        static inline T Get(const Message& msg, const FieldDescriptor* field) { 
            return &(msg.GetReflection()->GetMessage(msg, field)); 
        } 
        static inline T GetRepeated(const Message& msg, const FieldDescriptor* field, int index) { 
            return &(msg.GetReflection()->GetRepeatedMessage(msg, field, index)); 
        } 
        static inline Message* Set(Message& msg, const FieldDescriptor* field, const Message* value) { 
            Message* ret = msg.GetReflection()->MutableMessage(&msg, field); 
            ret->CopyFrom(*value); 
            return ret; 
        } 
        static inline Message* AddRepeated(Message& msg, const FieldDescriptor* field, const Message* value) { 
            Message* ret = msg.GetReflection()->AddMessage(&msg, field); 
            ret->CopyFrom(*value); 
            return ret; 
        } 
        static inline Message* SetRepeated(Message& msg, const FieldDescriptor* field, int index, const Message* value) { 
            Message* ret = msg.GetReflection()->MutableRepeatedMessage(&msg, field, index); 
            ret->CopyFrom(*value); 
            return ret; 
        } 
    }; 

    template <> 
    struct TSelectCppType<const Message*> { 
        static const FieldDescriptor::CppType Result = FieldDescriptor::CPPTYPE_MESSAGE; 
        typedef const Message* T; 
    }; 

    template <> 
    struct TSelectCppType<Message> { 
        static const FieldDescriptor::CppType Result = FieldDescriptor::CPPTYPE_MESSAGE; 
        typedef const Message* T; 
    }; 

    template <FieldDescriptor::CppType CppType, bool Repeated> 
    struct TFieldTraits { 
        typedef TCppTypeTraits<CppType> TBaseTraits; 
        typedef typename TBaseTraits::T T; 

        static inline T Get(const Message& msg, const FieldDescriptor* field, size_t index = 0) { 
            Y_ASSERT(index == 0); 
            return TBaseTraits::Get(msg, field); 
        } 

        static inline T GetDefault(const FieldDescriptor* field) { 
            return TBaseTraits::GetDefault(field); 
        } 

        static inline bool Has(const Message& msg, const FieldDescriptor* field) { 
            return TBaseTraits::Has(msg, field); 
        } 

        static inline size_t Size(const Message& msg, const FieldDescriptor* field) { 
            return Has(msg, field); 
        } 

        static inline void Set(Message& msg, const FieldDescriptor* field, T value, size_t index = 0) { 
            Y_ASSERT(index == 0); 
            TBaseTraits::Set(msg, field, value); 
        } 

        static inline void Add(Message& msg, const FieldDescriptor* field, T value) { 
            TBaseTraits::Set(msg, field, value); 
        } 
    }; 

    template <FieldDescriptor::CppType CppType> 
    struct TFieldTraits<CppType, true> { 
        typedef TCppTypeTraits<CppType> TBaseTraits; 
        typedef typename TBaseTraits::T T; 

        static inline T Get(const Message& msg, const FieldDescriptor* field, size_t index = 0) { 
            return TBaseTraits::GetRepeated(msg, field, index); 
        } 

        static inline T GetDefault(const FieldDescriptor* field) { 
            return TBaseTraits::GetDefault(field); 
        } 

        static inline size_t Size(const Message& msg, const FieldDescriptor* field) { 
            return TBaseTraits::Size(msg, field); 
        } 

        static inline bool Has(const Message& msg, const FieldDescriptor* field) { 
            return Size(msg, field) > 0; 
        } 

        static inline void Set(Message& msg, const FieldDescriptor* field, T value, size_t index = 0) { 
            TBaseTraits::SetRepeated(msg, field, index, value); 
        } 

        static inline void Add(Message& msg, const FieldDescriptor* field, T value) { 
            TBaseTraits::AddRepeated(msg, field, value); 
        } 
    }; 

    // Simpler interface at the cost of checking is_repeated() on each call 
    template <FieldDescriptor::CppType CppType> 
    struct TSimpleFieldTraits { 
        typedef TFieldTraits<CppType, true> TRepeated; 
        typedef TFieldTraits<CppType, false> TSingle; 
        typedef typename TRepeated::T T; 

        static inline size_t Size(const Message& msg, const FieldDescriptor* field) { 
            if (field->is_repeated()) 
                return TRepeated::Size(msg, field); 
            else 
                return TSingle::Size(msg, field); 
        } 

        static inline bool Has(const Message& msg, const FieldDescriptor* field) { 
            if (field->is_repeated()) 
                return TRepeated::Has(msg, field); 
            else 
                return TSingle::Has(msg, field); 
        } 

        static inline T Get(const Message& msg, const FieldDescriptor* field, size_t index = 0) { 
            Y_ASSERT(index < Size(msg, field) || !field->is_repeated() && index == 0); // Get for single fields is always allowed because of default values 
            if (field->is_repeated()) 
                return TRepeated::Get(msg, field, index); 
            else 
                return TSingle::Get(msg, field, index); 
        } 

        static inline T GetDefault(const FieldDescriptor* field) { 
            return TSingle::GetDefault(field); 
        } 

        static inline void Set(Message& msg, const FieldDescriptor* field, T value, size_t index = 0) { 
            Y_ASSERT(!field->is_repeated() && index == 0 || index < Size(msg, field)); 
            if (field->is_repeated()) 
                TRepeated::Set(msg, field, value, index); 
            else 
                TSingle::Set(msg, field, value, index); 
        } 

        static inline void Add(Message& msg, const FieldDescriptor* field, T value) { 
            if (field->is_repeated()) 
                TRepeated::Add(msg, field, value); 
            else 
                TSingle::Add(msg, field, value); 
        } 
    }; 

    // some cpp-type groups 

    template <FieldDescriptor::CppType CppType> 
    struct TIsIntegerCppType { 
        enum { 
            Result = CppType == FieldDescriptor::CPPTYPE_INT32 || 
                     CppType == FieldDescriptor::CPPTYPE_INT64 || 
                     CppType == FieldDescriptor::CPPTYPE_UINT32 || 
                     CppType == FieldDescriptor::CPPTYPE_UINT64 
        }; 
    };

    template <FieldDescriptor::CppType CppType> 
    struct TIsFloatCppType { 
        enum { 
            Result = CppType == FieldDescriptor::CPPTYPE_FLOAT || 
                     CppType == FieldDescriptor::CPPTYPE_DOUBLE 
        }; 
    };

    template <FieldDescriptor::CppType CppType> 
    struct TIsNumericCppType { 
        enum { 
            Result = CppType == FieldDescriptor::CPPTYPE_BOOL || 
                     TIsIntegerCppType<CppType>::Result || 
                     TIsFloatCppType<CppType>::Result 
        }; 
    };

    // a helper macro for splitting flow by cpp-type (e.g. in a switch) 

#define APPLY_TMP_MACRO_FOR_ALL_CPPTYPES()                            \ 
    TMP_MACRO_FOR_CPPTYPE(NProtoBuf::FieldDescriptor::CPPTYPE_INT32)  \
    TMP_MACRO_FOR_CPPTYPE(NProtoBuf::FieldDescriptor::CPPTYPE_INT64)  \
    TMP_MACRO_FOR_CPPTYPE(NProtoBuf::FieldDescriptor::CPPTYPE_UINT32) \
    TMP_MACRO_FOR_CPPTYPE(NProtoBuf::FieldDescriptor::CPPTYPE_UINT64) \
    TMP_MACRO_FOR_CPPTYPE(NProtoBuf::FieldDescriptor::CPPTYPE_DOUBLE) \
    TMP_MACRO_FOR_CPPTYPE(NProtoBuf::FieldDescriptor::CPPTYPE_FLOAT)  \
    TMP_MACRO_FOR_CPPTYPE(NProtoBuf::FieldDescriptor::CPPTYPE_BOOL)   \
    TMP_MACRO_FOR_CPPTYPE(NProtoBuf::FieldDescriptor::CPPTYPE_ENUM)   \
    TMP_MACRO_FOR_CPPTYPE(NProtoBuf::FieldDescriptor::CPPTYPE_STRING) \
    TMP_MACRO_FOR_CPPTYPE(NProtoBuf::FieldDescriptor::CPPTYPE_MESSAGE)
}