/proc/self/cwd/external/upb/upb/reflection/def.hpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2009-2021, Google LLC |
2 | | // All rights reserved. |
3 | | // |
4 | | // Redistribution and use in source and binary forms, with or without |
5 | | // modification, are permitted provided that the following conditions are met: |
6 | | // * Redistributions of source code must retain the above copyright |
7 | | // notice, this list of conditions and the following disclaimer. |
8 | | // * Redistributions in binary form must reproduce the above copyright |
9 | | // notice, this list of conditions and the following disclaimer in the |
10 | | // documentation and/or other materials provided with the distribution. |
11 | | // * Neither the name of Google LLC nor the |
12 | | // names of its contributors may be used to endorse or promote products |
13 | | // derived from this software without specific prior written permission. |
14 | | // |
15 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
16 | | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | | // ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT, |
19 | | // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
20 | | // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
21 | | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
22 | | // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 | | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
24 | | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | | |
26 | | #ifndef UPB_REFLECTION_DEF_HPP_ |
27 | | #define UPB_REFLECTION_DEF_HPP_ |
28 | | |
29 | | #include <cstring> |
30 | | #include <memory> |
31 | | #include <string> |
32 | | #include <vector> |
33 | | |
34 | | #include "upb/reflection/def.h" |
35 | | #include "upb/reflection/def_pool_internal.h" |
36 | | #include "upb/reflection/enum_def_internal.h" |
37 | | #include "upb/reflection/message.h" |
38 | | #include "upb/upb.hpp" |
39 | | |
40 | | // Must be last |
41 | | #include "upb/port/def.inc" |
42 | | |
43 | | namespace upb { |
44 | | |
45 | | typedef upb_MessageValue MessageValue; |
46 | | |
47 | | class EnumDefPtr; |
48 | | class FileDefPtr; |
49 | | class MessageDefPtr; |
50 | | class OneofDefPtr; |
51 | | |
52 | | // A upb::FieldDefPtr describes a single field in a message. It is most often |
53 | | // found as a part of a upb_MessageDef, but can also stand alone to represent |
54 | | // an extension. |
55 | | class FieldDefPtr { |
56 | | public: |
57 | 0 | FieldDefPtr() : ptr_(nullptr) {} |
58 | 0 | explicit FieldDefPtr(const upb_FieldDef* ptr) : ptr_(ptr) {} |
59 | | |
60 | 0 | const upb_FieldDef* ptr() const { return ptr_; } |
61 | | |
62 | | typedef upb_FieldType Type; |
63 | | typedef upb_CType CType; |
64 | | typedef upb_Label Label; |
65 | | |
66 | | FileDefPtr file() const; |
67 | 0 | const char* full_name() const { return upb_FieldDef_FullName(ptr_); } |
68 | | |
69 | 0 | const upb_MiniTableField* mini_table() const { |
70 | 0 | return upb_FieldDef_MiniTable(ptr_); |
71 | 0 | } |
72 | | |
73 | 0 | const UPB_DESC(FieldOptions) * options() const { |
74 | 0 | return upb_FieldDef_Options(ptr_); |
75 | 0 | } |
76 | | |
77 | 0 | Type type() const { return upb_FieldDef_Type(ptr_); } |
78 | 0 | CType ctype() const { return upb_FieldDef_CType(ptr_); } |
79 | 0 | Label label() const { return upb_FieldDef_Label(ptr_); } |
80 | 0 | const char* name() const { return upb_FieldDef_Name(ptr_); } |
81 | 0 | const char* json_name() const { return upb_FieldDef_JsonName(ptr_); } |
82 | 0 | uint32_t number() const { return upb_FieldDef_Number(ptr_); } |
83 | 0 | bool is_extension() const { return upb_FieldDef_IsExtension(ptr_); } |
84 | 0 | bool is_required() const { return upb_FieldDef_IsRequired(ptr_); } |
85 | 0 | bool has_presence() const { return upb_FieldDef_HasPresence(ptr_); } |
86 | | |
87 | | // For non-string, non-submessage fields, this indicates whether binary |
88 | | // protobufs are encoded in packed or non-packed format. |
89 | | // |
90 | | // Note: this accessor reflects the fact that "packed" has different defaults |
91 | | // depending on whether the proto is proto2 or proto3. |
92 | 0 | bool packed() const { return upb_FieldDef_IsPacked(ptr_); } |
93 | | |
94 | | // An integer that can be used as an index into an array of fields for |
95 | | // whatever message this field belongs to. Guaranteed to be less than |
96 | | // f->containing_type()->field_count(). May only be accessed once the def has |
97 | | // been finalized. |
98 | 0 | uint32_t index() const { return upb_FieldDef_Index(ptr_); } |
99 | | |
100 | | // The MessageDef to which this field belongs (for extensions, the extended |
101 | | // message). |
102 | | MessageDefPtr containing_type() const; |
103 | | |
104 | | // For extensions, the message the extension is declared inside, or NULL if |
105 | | // none. |
106 | | MessageDefPtr extension_scope() const; |
107 | | |
108 | | // The OneofDef to which this field belongs, or NULL if this field is not part |
109 | | // of a oneof. |
110 | | OneofDefPtr containing_oneof() const; |
111 | | OneofDefPtr real_containing_oneof() const; |
112 | | |
113 | | // Convenient field type tests. |
114 | 0 | bool IsSubMessage() const { return upb_FieldDef_IsSubMessage(ptr_); } |
115 | 0 | bool IsString() const { return upb_FieldDef_IsString(ptr_); } |
116 | 0 | bool IsSequence() const { return upb_FieldDef_IsRepeated(ptr_); } |
117 | 0 | bool IsPrimitive() const { return upb_FieldDef_IsPrimitive(ptr_); } |
118 | 0 | bool IsMap() const { return upb_FieldDef_IsMap(ptr_); } |
119 | | |
120 | 0 | MessageValue default_value() const { return upb_FieldDef_Default(ptr_); } |
121 | | |
122 | | // Returns the enum or submessage def for this field, if any. The field's |
123 | | // type must match (ie. you may only call enum_subdef() for fields where |
124 | | // type() == kUpb_CType_Enum). |
125 | | EnumDefPtr enum_subdef() const; |
126 | | MessageDefPtr message_type() const; |
127 | | |
128 | 0 | explicit operator bool() const { return ptr_ != nullptr; } |
129 | | |
130 | 0 | friend bool operator==(FieldDefPtr lhs, FieldDefPtr rhs) { |
131 | 0 | return lhs.ptr_ == rhs.ptr_; |
132 | 0 | } |
133 | | |
134 | 0 | friend bool operator!=(FieldDefPtr lhs, FieldDefPtr rhs) { |
135 | 0 | return !(lhs == rhs); |
136 | 0 | } |
137 | | |
138 | | private: |
139 | | const upb_FieldDef* ptr_; |
140 | | }; |
141 | | |
142 | | // Class that represents a oneof. |
143 | | class OneofDefPtr { |
144 | | public: |
145 | 0 | OneofDefPtr() : ptr_(nullptr) {} |
146 | 0 | explicit OneofDefPtr(const upb_OneofDef* ptr) : ptr_(ptr) {} |
147 | | |
148 | 0 | const upb_OneofDef* ptr() const { return ptr_; } |
149 | 0 | explicit operator bool() const { return ptr_ != nullptr; } |
150 | | |
151 | 0 | const UPB_DESC(OneofOptions) * options() const { |
152 | 0 | return upb_OneofDef_Options(ptr_); |
153 | 0 | } |
154 | | |
155 | | // Returns the MessageDef that contains this OneofDef. |
156 | | MessageDefPtr containing_type() const; |
157 | | |
158 | | // Returns the name of this oneof. |
159 | 0 | const char* name() const { return upb_OneofDef_Name(ptr_); } |
160 | 0 | const char* full_name() const { return upb_OneofDef_FullName(ptr_); } |
161 | | |
162 | | // Returns the number of fields in the oneof. |
163 | 0 | int field_count() const { return upb_OneofDef_FieldCount(ptr_); } |
164 | 0 | FieldDefPtr field(int i) const { |
165 | 0 | return FieldDefPtr(upb_OneofDef_Field(ptr_, i)); |
166 | 0 | } |
167 | | |
168 | | // Looks up by name. |
169 | 0 | FieldDefPtr FindFieldByName(const char* name, size_t len) const { |
170 | 0 | return FieldDefPtr(upb_OneofDef_LookupNameWithSize(ptr_, name, len)); |
171 | 0 | } |
172 | 0 | FieldDefPtr FindFieldByName(const char* name) const { |
173 | 0 | return FieldDefPtr(upb_OneofDef_LookupName(ptr_, name)); |
174 | 0 | } |
175 | | |
176 | | template <class T> |
177 | | FieldDefPtr FindFieldByName(const T& str) const { |
178 | | return FindFieldByName(str.c_str(), str.size()); |
179 | | } |
180 | | |
181 | | // Looks up by tag number. |
182 | 0 | FieldDefPtr FindFieldByNumber(uint32_t num) const { |
183 | 0 | return FieldDefPtr(upb_OneofDef_LookupNumber(ptr_, num)); |
184 | 0 | } |
185 | | |
186 | | private: |
187 | | const upb_OneofDef* ptr_; |
188 | | }; |
189 | | |
190 | | // Structure that describes a single .proto message type. |
191 | | class MessageDefPtr { |
192 | | public: |
193 | 0 | MessageDefPtr() : ptr_(nullptr) {} |
194 | 0 | explicit MessageDefPtr(const upb_MessageDef* ptr) : ptr_(ptr) {} |
195 | | |
196 | 0 | const UPB_DESC(MessageOptions) * options() const { |
197 | 0 | return upb_MessageDef_Options(ptr_); |
198 | 0 | } |
199 | | |
200 | 0 | std::string MiniDescriptorEncode() const { |
201 | 0 | upb::Arena arena; |
202 | 0 | upb_StringView md; |
203 | 0 | upb_MessageDef_MiniDescriptorEncode(ptr_, arena.ptr(), &md); |
204 | 0 | return std::string(md.data, md.size); |
205 | 0 | } |
206 | | |
207 | 0 | const upb_MessageDef* ptr() const { return ptr_; } |
208 | | |
209 | | FileDefPtr file() const; |
210 | | |
211 | 0 | const char* full_name() const { return upb_MessageDef_FullName(ptr_); } |
212 | 0 | const char* name() const { return upb_MessageDef_Name(ptr_); } |
213 | | |
214 | 0 | const upb_MiniTable* mini_table() const { |
215 | 0 | return upb_MessageDef_MiniTable(ptr_); |
216 | 0 | } |
217 | | |
218 | | // The number of fields that belong to the MessageDef. |
219 | 0 | int field_count() const { return upb_MessageDef_FieldCount(ptr_); } |
220 | 0 | FieldDefPtr field(int i) const { |
221 | 0 | return FieldDefPtr(upb_MessageDef_Field(ptr_, i)); |
222 | 0 | } |
223 | | |
224 | | // The number of oneofs that belong to the MessageDef. |
225 | 0 | int oneof_count() const { return upb_MessageDef_OneofCount(ptr_); } |
226 | 0 | int real_oneof_count() const { return upb_MessageDef_RealOneofCount(ptr_); } |
227 | 0 | OneofDefPtr oneof(int i) const { |
228 | 0 | return OneofDefPtr(upb_MessageDef_Oneof(ptr_, i)); |
229 | 0 | } |
230 | | |
231 | 0 | int enum_type_count() const { return upb_MessageDef_NestedEnumCount(ptr_); } |
232 | | EnumDefPtr enum_type(int i) const; |
233 | | |
234 | 0 | int nested_message_count() const { |
235 | 0 | return upb_MessageDef_NestedMessageCount(ptr_); |
236 | 0 | } |
237 | 0 | MessageDefPtr nested_message(int i) const { |
238 | 0 | return MessageDefPtr(upb_MessageDef_NestedMessage(ptr_, i)); |
239 | 0 | } |
240 | | |
241 | 0 | int nested_extension_count() const { |
242 | 0 | return upb_MessageDef_NestedExtensionCount(ptr_); |
243 | 0 | } |
244 | 0 | FieldDefPtr nested_extension(int i) const { |
245 | 0 | return FieldDefPtr(upb_MessageDef_NestedExtension(ptr_, i)); |
246 | 0 | } |
247 | | |
248 | 0 | int extension_range_count() const { |
249 | 0 | return upb_MessageDef_ExtensionRangeCount(ptr_); |
250 | 0 | } |
251 | | |
252 | 0 | upb_Syntax syntax() const { return upb_MessageDef_Syntax(ptr_); } |
253 | | |
254 | | // These return null pointers if the field is not found. |
255 | 0 | FieldDefPtr FindFieldByNumber(uint32_t number) const { |
256 | 0 | return FieldDefPtr(upb_MessageDef_FindFieldByNumber(ptr_, number)); |
257 | 0 | } |
258 | 0 | FieldDefPtr FindFieldByName(const char* name, size_t len) const { |
259 | 0 | return FieldDefPtr(upb_MessageDef_FindFieldByNameWithSize(ptr_, name, len)); |
260 | 0 | } |
261 | 0 | FieldDefPtr FindFieldByName(const char* name) const { |
262 | 0 | return FieldDefPtr(upb_MessageDef_FindFieldByName(ptr_, name)); |
263 | 0 | } |
264 | | |
265 | | template <class T> |
266 | | FieldDefPtr FindFieldByName(const T& str) const { |
267 | | return FindFieldByName(str.c_str(), str.size()); |
268 | | } |
269 | | |
270 | 0 | OneofDefPtr FindOneofByName(const char* name, size_t len) const { |
271 | 0 | return OneofDefPtr(upb_MessageDef_FindOneofByNameWithSize(ptr_, name, len)); |
272 | 0 | } |
273 | | |
274 | 0 | OneofDefPtr FindOneofByName(const char* name) const { |
275 | 0 | return OneofDefPtr(upb_MessageDef_FindOneofByName(ptr_, name)); |
276 | 0 | } |
277 | | |
278 | | template <class T> |
279 | | OneofDefPtr FindOneofByName(const T& str) const { |
280 | | return FindOneofByName(str.c_str(), str.size()); |
281 | | } |
282 | | |
283 | | // Is this message a map entry? |
284 | 0 | bool mapentry() const { return upb_MessageDef_IsMapEntry(ptr_); } |
285 | | |
286 | 0 | FieldDefPtr map_key() const { |
287 | 0 | if (!mapentry()) return FieldDefPtr(); |
288 | 0 | return FieldDefPtr(upb_MessageDef_Field(ptr_, 0)); |
289 | 0 | } |
290 | | |
291 | 0 | FieldDefPtr map_value() const { |
292 | 0 | if (!mapentry()) return FieldDefPtr(); |
293 | 0 | return FieldDefPtr(upb_MessageDef_Field(ptr_, 1)); |
294 | 0 | } |
295 | | |
296 | | // Return the type of well known type message. kUpb_WellKnown_Unspecified for |
297 | | // non-well-known message. |
298 | 0 | upb_WellKnown wellknowntype() const { |
299 | 0 | return upb_MessageDef_WellKnownType(ptr_); |
300 | 0 | } |
301 | | |
302 | 0 | explicit operator bool() const { return ptr_ != nullptr; } |
303 | | |
304 | 0 | friend bool operator==(MessageDefPtr lhs, MessageDefPtr rhs) { |
305 | 0 | return lhs.ptr_ == rhs.ptr_; |
306 | 0 | } |
307 | | |
308 | 0 | friend bool operator!=(MessageDefPtr lhs, MessageDefPtr rhs) { |
309 | 0 | return !(lhs == rhs); |
310 | 0 | } |
311 | | |
312 | | private: |
313 | | class FieldIter { |
314 | | public: |
315 | 0 | explicit FieldIter(const upb_MessageDef* m, int i) : m_(m), i_(i) {} |
316 | 0 | void operator++() { i_++; } |
317 | | |
318 | 0 | FieldDefPtr operator*() { |
319 | 0 | return FieldDefPtr(upb_MessageDef_Field(m_, i_)); |
320 | 0 | } |
321 | | |
322 | 0 | friend bool operator==(FieldIter lhs, FieldIter rhs) { |
323 | 0 | return lhs.i_ == rhs.i_; |
324 | 0 | } |
325 | | |
326 | 0 | friend bool operator!=(FieldIter lhs, FieldIter rhs) { |
327 | 0 | return !(lhs == rhs); |
328 | 0 | } |
329 | | |
330 | | private: |
331 | | const upb_MessageDef* m_; |
332 | | int i_; |
333 | | }; |
334 | | |
335 | | class FieldAccessor { |
336 | | public: |
337 | 0 | explicit FieldAccessor(const upb_MessageDef* md) : md_(md) {} |
338 | 0 | FieldIter begin() { return FieldIter(md_, 0); } |
339 | 0 | FieldIter end() { return FieldIter(md_, upb_MessageDef_FieldCount(md_)); } |
340 | | |
341 | | private: |
342 | | const upb_MessageDef* md_; |
343 | | }; |
344 | | |
345 | | class OneofIter { |
346 | | public: |
347 | 0 | explicit OneofIter(const upb_MessageDef* m, int i) : m_(m), i_(i) {} |
348 | 0 | void operator++() { i_++; } |
349 | | |
350 | 0 | OneofDefPtr operator*() { |
351 | 0 | return OneofDefPtr(upb_MessageDef_Oneof(m_, i_)); |
352 | 0 | } |
353 | | |
354 | 0 | friend bool operator==(OneofIter lhs, OneofIter rhs) { |
355 | 0 | return lhs.i_ == rhs.i_; |
356 | 0 | } |
357 | | |
358 | 0 | friend bool operator!=(OneofIter lhs, OneofIter rhs) { |
359 | 0 | return !(lhs == rhs); |
360 | 0 | } |
361 | | |
362 | | private: |
363 | | const upb_MessageDef* m_; |
364 | | int i_; |
365 | | }; |
366 | | |
367 | | class OneofAccessor { |
368 | | public: |
369 | 0 | explicit OneofAccessor(const upb_MessageDef* md) : md_(md) {} |
370 | 0 | OneofIter begin() { return OneofIter(md_, 0); } |
371 | 0 | OneofIter end() { return OneofIter(md_, upb_MessageDef_OneofCount(md_)); } |
372 | | |
373 | | private: |
374 | | const upb_MessageDef* md_; |
375 | | }; |
376 | | |
377 | | public: |
378 | 0 | FieldAccessor fields() const { return FieldAccessor(ptr()); } |
379 | 0 | OneofAccessor oneofs() const { return OneofAccessor(ptr()); } |
380 | | |
381 | | private: |
382 | | const upb_MessageDef* ptr_; |
383 | | }; |
384 | | |
385 | | class EnumValDefPtr { |
386 | | public: |
387 | 0 | EnumValDefPtr() : ptr_(nullptr) {} |
388 | 0 | explicit EnumValDefPtr(const upb_EnumValueDef* ptr) : ptr_(ptr) {} |
389 | | |
390 | 0 | const UPB_DESC(EnumValueOptions) * options() const { |
391 | 0 | return upb_EnumValueDef_Options(ptr_); |
392 | 0 | } |
393 | | |
394 | 0 | int32_t number() const { return upb_EnumValueDef_Number(ptr_); } |
395 | 0 | const char* full_name() const { return upb_EnumValueDef_FullName(ptr_); } |
396 | 0 | const char* name() const { return upb_EnumValueDef_Name(ptr_); } |
397 | | |
398 | | private: |
399 | | const upb_EnumValueDef* ptr_; |
400 | | }; |
401 | | |
402 | | class EnumDefPtr { |
403 | | public: |
404 | 0 | EnumDefPtr() : ptr_(nullptr) {} |
405 | 0 | explicit EnumDefPtr(const upb_EnumDef* ptr) : ptr_(ptr) {} |
406 | | |
407 | 0 | const UPB_DESC(EnumOptions) * options() const { |
408 | 0 | return upb_EnumDef_Options(ptr_); |
409 | 0 | } |
410 | | |
411 | 0 | const upb_MiniTableEnum* mini_table() const { |
412 | 0 | return _upb_EnumDef_MiniTable(ptr_); |
413 | 0 | } |
414 | | |
415 | 0 | std::string MiniDescriptorEncode() const { |
416 | 0 | upb::Arena arena; |
417 | 0 | upb_StringView md; |
418 | 0 | upb_EnumDef_MiniDescriptorEncode(ptr_, arena.ptr(), &md); |
419 | 0 | return std::string(md.data, md.size); |
420 | 0 | } |
421 | | |
422 | 0 | const upb_EnumDef* ptr() const { return ptr_; } |
423 | 0 | explicit operator bool() const { return ptr_ != nullptr; } |
424 | | |
425 | 0 | const char* full_name() const { return upb_EnumDef_FullName(ptr_); } |
426 | 0 | const char* name() const { return upb_EnumDef_Name(ptr_); } |
427 | 0 | bool is_closed() const { return upb_EnumDef_IsClosed(ptr_); } |
428 | | |
429 | | // The value that is used as the default when no field default is specified. |
430 | | // If not set explicitly, the first value that was added will be used. |
431 | | // The default value must be a member of the enum. |
432 | | // Requires that value_count() > 0. |
433 | 0 | int32_t default_value() const { return upb_EnumDef_Default(ptr_); } |
434 | | |
435 | | // Returns the number of values currently defined in the enum. Note that |
436 | | // multiple names can refer to the same number, so this may be greater than |
437 | | // the total number of unique numbers. |
438 | 0 | int value_count() const { return upb_EnumDef_ValueCount(ptr_); } |
439 | 0 | EnumValDefPtr value(int i) const { |
440 | 0 | return EnumValDefPtr(upb_EnumDef_Value(ptr_, i)); |
441 | 0 | } |
442 | | |
443 | | // Lookups from name to integer, returning true if found. |
444 | 0 | EnumValDefPtr FindValueByName(const char* name) const { |
445 | 0 | return EnumValDefPtr(upb_EnumDef_FindValueByName(ptr_, name)); |
446 | 0 | } |
447 | | |
448 | | // Finds the name corresponding to the given number, or NULL if none was |
449 | | // found. If more than one name corresponds to this number, returns the |
450 | | // first one that was added. |
451 | 0 | EnumValDefPtr FindValueByNumber(int32_t num) const { |
452 | 0 | return EnumValDefPtr(upb_EnumDef_FindValueByNumber(ptr_, num)); |
453 | 0 | } |
454 | | |
455 | | private: |
456 | | const upb_EnumDef* ptr_; |
457 | | }; |
458 | | |
459 | | // Class that represents a .proto file with some things defined in it. |
460 | | // |
461 | | // Many users won't care about FileDefs, but they are necessary if you want to |
462 | | // read the values of file-level options. |
463 | | class FileDefPtr { |
464 | | public: |
465 | 0 | explicit FileDefPtr(const upb_FileDef* ptr) : ptr_(ptr) {} |
466 | | |
467 | 0 | const UPB_DESC(FileOptions) * options() const { |
468 | 0 | return upb_FileDef_Options(ptr_); |
469 | 0 | } |
470 | | |
471 | 0 | const upb_FileDef* ptr() const { return ptr_; } |
472 | | |
473 | | // Get/set name of the file (eg. "foo/bar.proto"). |
474 | 0 | const char* name() const { return upb_FileDef_Name(ptr_); } |
475 | | |
476 | | // Package name for definitions inside the file (eg. "foo.bar"). |
477 | 0 | const char* package() const { return upb_FileDef_Package(ptr_); } |
478 | | |
479 | | // Syntax for the file. Defaults to proto2. |
480 | 0 | upb_Syntax syntax() const { return upb_FileDef_Syntax(ptr_); } |
481 | | |
482 | | // Get the list of dependencies from the file. These are returned in the |
483 | | // order that they were added to the FileDefPtr. |
484 | 0 | int dependency_count() const { return upb_FileDef_DependencyCount(ptr_); } |
485 | 0 | FileDefPtr dependency(int index) const { |
486 | 0 | return FileDefPtr(upb_FileDef_Dependency(ptr_, index)); |
487 | 0 | } |
488 | | |
489 | 0 | int public_dependency_count() const { |
490 | 0 | return upb_FileDef_PublicDependencyCount(ptr_); |
491 | 0 | } |
492 | 0 | FileDefPtr public_dependency(int index) const { |
493 | 0 | return FileDefPtr(upb_FileDef_PublicDependency(ptr_, index)); |
494 | 0 | } |
495 | | |
496 | 0 | int toplevel_enum_count() const { |
497 | 0 | return upb_FileDef_TopLevelEnumCount(ptr_); |
498 | 0 | } |
499 | 0 | EnumDefPtr toplevel_enum(int index) const { |
500 | 0 | return EnumDefPtr(upb_FileDef_TopLevelEnum(ptr_, index)); |
501 | 0 | } |
502 | | |
503 | 0 | int toplevel_message_count() const { |
504 | 0 | return upb_FileDef_TopLevelMessageCount(ptr_); |
505 | 0 | } |
506 | 0 | MessageDefPtr toplevel_message(int index) const { |
507 | 0 | return MessageDefPtr(upb_FileDef_TopLevelMessage(ptr_, index)); |
508 | 0 | } |
509 | | |
510 | 0 | int toplevel_extension_count() const { |
511 | 0 | return upb_FileDef_TopLevelExtensionCount(ptr_); |
512 | 0 | } |
513 | 0 | FieldDefPtr toplevel_extension(int index) const { |
514 | 0 | return FieldDefPtr(upb_FileDef_TopLevelExtension(ptr_, index)); |
515 | 0 | } |
516 | | |
517 | 0 | explicit operator bool() const { return ptr_ != nullptr; } |
518 | | |
519 | 0 | friend bool operator==(FileDefPtr lhs, FileDefPtr rhs) { |
520 | 0 | return lhs.ptr_ == rhs.ptr_; |
521 | 0 | } |
522 | | |
523 | 0 | friend bool operator!=(FileDefPtr lhs, FileDefPtr rhs) { |
524 | 0 | return !(lhs == rhs); |
525 | 0 | } |
526 | | |
527 | | private: |
528 | | const upb_FileDef* ptr_; |
529 | | }; |
530 | | |
531 | | // Non-const methods in upb::DefPool are NOT thread-safe. |
532 | | class DefPool { |
533 | | public: |
534 | 0 | DefPool() : ptr_(upb_DefPool_New(), upb_DefPool_Free) {} |
535 | 0 | explicit DefPool(upb_DefPool* s) : ptr_(s, upb_DefPool_Free) {} |
536 | | |
537 | 0 | const upb_DefPool* ptr() const { return ptr_.get(); } |
538 | 0 | upb_DefPool* ptr() { return ptr_.get(); } |
539 | | |
540 | | // Finds an entry in the symbol table with this exact name. If not found, |
541 | | // returns NULL. |
542 | 0 | MessageDefPtr FindMessageByName(const char* sym) const { |
543 | 0 | return MessageDefPtr(upb_DefPool_FindMessageByName(ptr_.get(), sym)); |
544 | 0 | } |
545 | | |
546 | 0 | EnumDefPtr FindEnumByName(const char* sym) const { |
547 | 0 | return EnumDefPtr(upb_DefPool_FindEnumByName(ptr_.get(), sym)); |
548 | 0 | } |
549 | | |
550 | 0 | FileDefPtr FindFileByName(const char* name) const { |
551 | 0 | return FileDefPtr(upb_DefPool_FindFileByName(ptr_.get(), name)); |
552 | 0 | } |
553 | | |
554 | 0 | FieldDefPtr FindExtensionByName(const char* name) const { |
555 | 0 | return FieldDefPtr(upb_DefPool_FindExtensionByName(ptr_.get(), name)); |
556 | 0 | } |
557 | | |
558 | 0 | void _SetPlatform(upb_MiniTablePlatform platform) { |
559 | 0 | _upb_DefPool_SetPlatform(ptr_.get(), platform); |
560 | 0 | } |
561 | | |
562 | | // TODO: iteration? |
563 | | |
564 | | // Adds the given serialized FileDescriptorProto to the pool. |
565 | | FileDefPtr AddFile(const UPB_DESC(FileDescriptorProto) * file_proto, |
566 | 0 | Status* status) { |
567 | 0 | return FileDefPtr( |
568 | 0 | upb_DefPool_AddFile(ptr_.get(), file_proto, status->ptr())); |
569 | 0 | } |
570 | | |
571 | | private: |
572 | | std::unique_ptr<upb_DefPool, decltype(&upb_DefPool_Free)> ptr_; |
573 | | }; |
574 | | |
575 | | // TODO(b/236632406): This typedef is deprecated. Delete it. |
576 | | using SymbolTable = DefPool; |
577 | | |
578 | 0 | inline FileDefPtr FieldDefPtr::file() const { |
579 | 0 | return FileDefPtr(upb_FieldDef_File(ptr_)); |
580 | 0 | } |
581 | | |
582 | 0 | inline FileDefPtr MessageDefPtr::file() const { |
583 | 0 | return FileDefPtr(upb_MessageDef_File(ptr_)); |
584 | 0 | } |
585 | | |
586 | 0 | inline EnumDefPtr MessageDefPtr::enum_type(int i) const { |
587 | 0 | return EnumDefPtr(upb_MessageDef_NestedEnum(ptr_, i)); |
588 | 0 | } |
589 | | |
590 | 0 | inline MessageDefPtr FieldDefPtr::message_type() const { |
591 | 0 | return MessageDefPtr(upb_FieldDef_MessageSubDef(ptr_)); |
592 | 0 | } |
593 | | |
594 | 0 | inline MessageDefPtr FieldDefPtr::containing_type() const { |
595 | 0 | return MessageDefPtr(upb_FieldDef_ContainingType(ptr_)); |
596 | 0 | } |
597 | | |
598 | 0 | inline MessageDefPtr FieldDefPtr::extension_scope() const { |
599 | 0 | return MessageDefPtr(upb_FieldDef_ExtensionScope(ptr_)); |
600 | 0 | } |
601 | | |
602 | 0 | inline MessageDefPtr OneofDefPtr::containing_type() const { |
603 | 0 | return MessageDefPtr(upb_OneofDef_ContainingType(ptr_)); |
604 | 0 | } |
605 | | |
606 | 0 | inline OneofDefPtr FieldDefPtr::containing_oneof() const { |
607 | 0 | return OneofDefPtr(upb_FieldDef_ContainingOneof(ptr_)); |
608 | 0 | } |
609 | | |
610 | 0 | inline OneofDefPtr FieldDefPtr::real_containing_oneof() const { |
611 | 0 | return OneofDefPtr(upb_FieldDef_RealContainingOneof(ptr_)); |
612 | 0 | } |
613 | | |
614 | 0 | inline EnumDefPtr FieldDefPtr::enum_subdef() const { |
615 | 0 | return EnumDefPtr(upb_FieldDef_EnumSubDef(ptr_)); |
616 | 0 | } |
617 | | |
618 | | } // namespace upb |
619 | | |
620 | | #include "upb/port/undef.inc" |
621 | | |
622 | | #endif // UPB_REFLECTION_DEF_HPP_ |