1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 #ifndef UPB_REFLECTION_DEF_HPP_
9 #define UPB_REFLECTION_DEF_HPP_
10
11 #include <stdint.h>
12
13 #include <cstring>
14 #include <memory>
15 #include <string>
16
17 #include "upb/base/descriptor_constants.h"
18 #include "upb/base/status.hpp"
19 #include "upb/base/string_view.h"
20 #include "upb/mem/arena.hpp"
21 #include "upb/message/value.h"
22 #include "upb/mini_descriptor/decode.h"
23 #include "upb/mini_table/enum.h"
24 #include "upb/mini_table/field.h"
25 #include "upb/mini_table/message.h"
26 #include "upb/reflection/def.h"
27 #include "upb/reflection/internal/def_pool.h"
28 #include "upb/reflection/internal/enum_def.h"
29 #include "upb/reflection/message.h"
30
31 // Must be last
32 #include "upb/port/def.inc"
33
34 namespace upb {
35
36 typedef upb_MessageValue MessageValue;
37
38 class EnumDefPtr;
39 class FileDefPtr;
40 class MessageDefPtr;
41 class OneofDefPtr;
42
43 // A upb::FieldDefPtr describes a single field in a message. It is most often
44 // found as a part of a upb_MessageDef, but can also stand alone to represent
45 // an extension.
46 class FieldDefPtr {
47 public:
FieldDefPtr()48 FieldDefPtr() : ptr_(nullptr) {}
FieldDefPtr(const upb_FieldDef * ptr)49 explicit FieldDefPtr(const upb_FieldDef* ptr) : ptr_(ptr) {}
50
ptr() const51 const upb_FieldDef* ptr() const { return ptr_; }
52
53 typedef upb_FieldType Type;
54 typedef upb_CType CType;
55 typedef upb_Label Label;
56
57 FileDefPtr file() const;
full_name() const58 const char* full_name() const { return upb_FieldDef_FullName(ptr_); }
59
mini_table() const60 const upb_MiniTableField* mini_table() const {
61 return upb_FieldDef_MiniTable(ptr_);
62 }
63
MiniDescriptorEncode() const64 std::string MiniDescriptorEncode() const {
65 upb::Arena arena;
66 upb_StringView md;
67 upb_FieldDef_MiniDescriptorEncode(ptr_, arena.ptr(), &md);
68 return std::string(md.data, md.size);
69 }
70
UPB_DESC(FieldOptions)71 const UPB_DESC(FieldOptions) * options() const {
72 return upb_FieldDef_Options(ptr_);
73 }
74
type() const75 Type type() const { return upb_FieldDef_Type(ptr_); }
ctype() const76 CType ctype() const { return upb_FieldDef_CType(ptr_); }
label() const77 Label label() const { return upb_FieldDef_Label(ptr_); }
name() const78 const char* name() const { return upb_FieldDef_Name(ptr_); }
json_name() const79 const char* json_name() const { return upb_FieldDef_JsonName(ptr_); }
number() const80 uint32_t number() const { return upb_FieldDef_Number(ptr_); }
is_extension() const81 bool is_extension() const { return upb_FieldDef_IsExtension(ptr_); }
is_required() const82 bool is_required() const { return upb_FieldDef_IsRequired(ptr_); }
has_presence() const83 bool has_presence() const { return upb_FieldDef_HasPresence(ptr_); }
84
85 // For non-string, non-submessage fields, this indicates whether binary
86 // protobufs are encoded in packed or non-packed format.
87 //
88 // Note: this accessor reflects the fact that "packed" has different defaults
89 // depending on whether the proto is proto2 or proto3.
packed() const90 bool packed() const { return upb_FieldDef_IsPacked(ptr_); }
91
92 // An integer that can be used as an index into an array of fields for
93 // whatever message this field belongs to. Guaranteed to be less than
94 // f->containing_type()->field_count(). May only be accessed once the def has
95 // been finalized.
index() const96 uint32_t index() const { return upb_FieldDef_Index(ptr_); }
97
98 // Index into msgdef->layout->fields or file->exts
layout_index() const99 uint32_t layout_index() const { return upb_FieldDef_LayoutIndex(ptr_); }
100
101 // The MessageDef to which this field belongs (for extensions, the extended
102 // message).
103 MessageDefPtr containing_type() const;
104
105 // For extensions, the message the extension is declared inside, or NULL if
106 // none.
107 MessageDefPtr extension_scope() const;
108
109 // The OneofDef to which this field belongs, or NULL if this field is not part
110 // of a oneof.
111 OneofDefPtr containing_oneof() const;
112 OneofDefPtr real_containing_oneof() const;
113
114 // Convenient field type tests.
IsEnum() const115 bool IsEnum() const { return upb_FieldDef_IsEnum(ptr_); }
IsSubMessage() const116 bool IsSubMessage() const { return upb_FieldDef_IsSubMessage(ptr_); }
IsString() const117 bool IsString() const { return upb_FieldDef_IsString(ptr_); }
IsSequence() const118 bool IsSequence() const { return upb_FieldDef_IsRepeated(ptr_); }
IsPrimitive() const119 bool IsPrimitive() const { return upb_FieldDef_IsPrimitive(ptr_); }
IsMap() const120 bool IsMap() const { return upb_FieldDef_IsMap(ptr_); }
121
default_value() const122 MessageValue default_value() const { return upb_FieldDef_Default(ptr_); }
123
124 // Returns the enum or submessage def for this field, if any. The field's
125 // type must match (ie. you may only call enum_subdef() for fields where
126 // type() == kUpb_CType_Enum).
127 EnumDefPtr enum_subdef() const;
128 MessageDefPtr message_type() const;
129
operator bool() const130 explicit operator bool() const { return ptr_ != nullptr; }
131
operator ==(FieldDefPtr lhs,FieldDefPtr rhs)132 friend bool operator==(FieldDefPtr lhs, FieldDefPtr rhs) {
133 return lhs.ptr_ == rhs.ptr_;
134 }
135
operator !=(FieldDefPtr lhs,FieldDefPtr rhs)136 friend bool operator!=(FieldDefPtr lhs, FieldDefPtr rhs) {
137 return !(lhs == rhs);
138 }
139
140 private:
141 const upb_FieldDef* ptr_;
142 };
143
144 // Class that represents a oneof.
145 class OneofDefPtr {
146 public:
OneofDefPtr()147 OneofDefPtr() : ptr_(nullptr) {}
OneofDefPtr(const upb_OneofDef * ptr)148 explicit OneofDefPtr(const upb_OneofDef* ptr) : ptr_(ptr) {}
149
ptr() const150 const upb_OneofDef* ptr() const { return ptr_; }
operator bool() const151 explicit operator bool() const { return ptr_ != nullptr; }
152
UPB_DESC(OneofOptions)153 const UPB_DESC(OneofOptions) * options() const {
154 return upb_OneofDef_Options(ptr_);
155 }
156
157 // Returns the MessageDef that contains this OneofDef.
158 MessageDefPtr containing_type() const;
159
160 // Returns the name of this oneof.
name() const161 const char* name() const { return upb_OneofDef_Name(ptr_); }
full_name() const162 const char* full_name() const { return upb_OneofDef_FullName(ptr_); }
163
164 // Returns the number of fields in the oneof.
field_count() const165 int field_count() const { return upb_OneofDef_FieldCount(ptr_); }
field(int i) const166 FieldDefPtr field(int i) const {
167 return FieldDefPtr(upb_OneofDef_Field(ptr_, i));
168 }
169
170 // Looks up by name.
FindFieldByName(const char * name,size_t len) const171 FieldDefPtr FindFieldByName(const char* name, size_t len) const {
172 return FieldDefPtr(upb_OneofDef_LookupNameWithSize(ptr_, name, len));
173 }
FindFieldByName(const char * name) const174 FieldDefPtr FindFieldByName(const char* name) const {
175 return FieldDefPtr(upb_OneofDef_LookupName(ptr_, name));
176 }
177
178 template <class T>
FindFieldByName(const T & str) const179 FieldDefPtr FindFieldByName(const T& str) const {
180 return FindFieldByName(str.c_str(), str.size());
181 }
182
183 // Looks up by tag number.
FindFieldByNumber(uint32_t num) const184 FieldDefPtr FindFieldByNumber(uint32_t num) const {
185 return FieldDefPtr(upb_OneofDef_LookupNumber(ptr_, num));
186 }
187
188 private:
189 const upb_OneofDef* ptr_;
190 };
191
192 // Structure that describes a single .proto message type.
193 class MessageDefPtr {
194 public:
MessageDefPtr()195 MessageDefPtr() : ptr_(nullptr) {}
MessageDefPtr(const upb_MessageDef * ptr)196 explicit MessageDefPtr(const upb_MessageDef* ptr) : ptr_(ptr) {}
197
UPB_DESC(MessageOptions)198 const UPB_DESC(MessageOptions) * options() const {
199 return upb_MessageDef_Options(ptr_);
200 }
201
MiniDescriptorEncode() const202 std::string MiniDescriptorEncode() const {
203 upb::Arena arena;
204 upb_StringView md;
205 upb_MessageDef_MiniDescriptorEncode(ptr_, arena.ptr(), &md);
206 return std::string(md.data, md.size);
207 }
208
ptr() const209 const upb_MessageDef* ptr() const { return ptr_; }
210
211 FileDefPtr file() const;
212
full_name() const213 const char* full_name() const { return upb_MessageDef_FullName(ptr_); }
name() const214 const char* name() const { return upb_MessageDef_Name(ptr_); }
215
216 // Returns the MessageDef that contains this MessageDef (or null).
217 MessageDefPtr containing_type() const;
218
mini_table() const219 const upb_MiniTable* mini_table() const {
220 return upb_MessageDef_MiniTable(ptr_);
221 }
222
223 // The number of fields that belong to the MessageDef.
field_count() const224 int field_count() const { return upb_MessageDef_FieldCount(ptr_); }
field(int i) const225 FieldDefPtr field(int i) const {
226 return FieldDefPtr(upb_MessageDef_Field(ptr_, i));
227 }
228
229 // The number of oneofs that belong to the MessageDef.
oneof_count() const230 int oneof_count() const { return upb_MessageDef_OneofCount(ptr_); }
real_oneof_count() const231 int real_oneof_count() const { return upb_MessageDef_RealOneofCount(ptr_); }
oneof(int i) const232 OneofDefPtr oneof(int i) const {
233 return OneofDefPtr(upb_MessageDef_Oneof(ptr_, i));
234 }
235
enum_type_count() const236 int enum_type_count() const { return upb_MessageDef_NestedEnumCount(ptr_); }
237 EnumDefPtr enum_type(int i) const;
238
nested_message_count() const239 int nested_message_count() const {
240 return upb_MessageDef_NestedMessageCount(ptr_);
241 }
nested_message(int i) const242 MessageDefPtr nested_message(int i) const {
243 return MessageDefPtr(upb_MessageDef_NestedMessage(ptr_, i));
244 }
245
nested_extension_count() const246 int nested_extension_count() const {
247 return upb_MessageDef_NestedExtensionCount(ptr_);
248 }
nested_extension(int i) const249 FieldDefPtr nested_extension(int i) const {
250 return FieldDefPtr(upb_MessageDef_NestedExtension(ptr_, i));
251 }
252
extension_range_count() const253 int extension_range_count() const {
254 return upb_MessageDef_ExtensionRangeCount(ptr_);
255 }
256
syntax() const257 upb_Syntax syntax() const { return upb_MessageDef_Syntax(ptr_); }
258
259 // These return null pointers if the field is not found.
FindFieldByNumber(uint32_t number) const260 FieldDefPtr FindFieldByNumber(uint32_t number) const {
261 return FieldDefPtr(upb_MessageDef_FindFieldByNumber(ptr_, number));
262 }
FindFieldByName(const char * name,size_t len) const263 FieldDefPtr FindFieldByName(const char* name, size_t len) const {
264 return FieldDefPtr(upb_MessageDef_FindFieldByNameWithSize(ptr_, name, len));
265 }
FindFieldByName(const char * name) const266 FieldDefPtr FindFieldByName(const char* name) const {
267 return FieldDefPtr(upb_MessageDef_FindFieldByName(ptr_, name));
268 }
269
270 template <class T>
FindFieldByName(const T & str) const271 FieldDefPtr FindFieldByName(const T& str) const {
272 return FindFieldByName(str.c_str(), str.size());
273 }
274
FindOneofByName(const char * name,size_t len) const275 OneofDefPtr FindOneofByName(const char* name, size_t len) const {
276 return OneofDefPtr(upb_MessageDef_FindOneofByNameWithSize(ptr_, name, len));
277 }
278
FindOneofByName(const char * name) const279 OneofDefPtr FindOneofByName(const char* name) const {
280 return OneofDefPtr(upb_MessageDef_FindOneofByName(ptr_, name));
281 }
282
283 template <class T>
FindOneofByName(const T & str) const284 OneofDefPtr FindOneofByName(const T& str) const {
285 return FindOneofByName(str.c_str(), str.size());
286 }
287
288 // Is this message a map entry?
mapentry() const289 bool mapentry() const { return upb_MessageDef_IsMapEntry(ptr_); }
290
map_key() const291 FieldDefPtr map_key() const {
292 if (!mapentry()) return FieldDefPtr();
293 return FieldDefPtr(upb_MessageDef_Field(ptr_, 0));
294 }
295
map_value() const296 FieldDefPtr map_value() const {
297 if (!mapentry()) return FieldDefPtr();
298 return FieldDefPtr(upb_MessageDef_Field(ptr_, 1));
299 }
300
301 // Return the type of well known type message. kUpb_WellKnown_Unspecified for
302 // non-well-known message.
wellknowntype() const303 upb_WellKnown wellknowntype() const {
304 return upb_MessageDef_WellKnownType(ptr_);
305 }
306
operator bool() const307 explicit operator bool() const { return ptr_ != nullptr; }
308
operator ==(MessageDefPtr lhs,MessageDefPtr rhs)309 friend bool operator==(MessageDefPtr lhs, MessageDefPtr rhs) {
310 return lhs.ptr_ == rhs.ptr_;
311 }
312
operator !=(MessageDefPtr lhs,MessageDefPtr rhs)313 friend bool operator!=(MessageDefPtr lhs, MessageDefPtr rhs) {
314 return !(lhs == rhs);
315 }
316
317 private:
318 class FieldIter {
319 public:
FieldIter(const upb_MessageDef * m,int i)320 explicit FieldIter(const upb_MessageDef* m, int i) : m_(m), i_(i) {}
operator ++()321 void operator++() { i_++; }
322
operator *()323 FieldDefPtr operator*() {
324 return FieldDefPtr(upb_MessageDef_Field(m_, i_));
325 }
326
operator ==(FieldIter lhs,FieldIter rhs)327 friend bool operator==(FieldIter lhs, FieldIter rhs) {
328 return lhs.i_ == rhs.i_;
329 }
330
operator !=(FieldIter lhs,FieldIter rhs)331 friend bool operator!=(FieldIter lhs, FieldIter rhs) {
332 return !(lhs == rhs);
333 }
334
335 private:
336 const upb_MessageDef* m_;
337 int i_;
338 };
339
340 class FieldAccessor {
341 public:
FieldAccessor(const upb_MessageDef * md)342 explicit FieldAccessor(const upb_MessageDef* md) : md_(md) {}
begin()343 FieldIter begin() { return FieldIter(md_, 0); }
end()344 FieldIter end() { return FieldIter(md_, upb_MessageDef_FieldCount(md_)); }
345
346 private:
347 const upb_MessageDef* md_;
348 };
349
350 class OneofIter {
351 public:
OneofIter(const upb_MessageDef * m,int i)352 explicit OneofIter(const upb_MessageDef* m, int i) : m_(m), i_(i) {}
operator ++()353 void operator++() { i_++; }
354
operator *()355 OneofDefPtr operator*() {
356 return OneofDefPtr(upb_MessageDef_Oneof(m_, i_));
357 }
358
operator ==(OneofIter lhs,OneofIter rhs)359 friend bool operator==(OneofIter lhs, OneofIter rhs) {
360 return lhs.i_ == rhs.i_;
361 }
362
operator !=(OneofIter lhs,OneofIter rhs)363 friend bool operator!=(OneofIter lhs, OneofIter rhs) {
364 return !(lhs == rhs);
365 }
366
367 private:
368 const upb_MessageDef* m_;
369 int i_;
370 };
371
372 class OneofAccessor {
373 public:
OneofAccessor(const upb_MessageDef * md)374 explicit OneofAccessor(const upb_MessageDef* md) : md_(md) {}
begin()375 OneofIter begin() { return OneofIter(md_, 0); }
end()376 OneofIter end() { return OneofIter(md_, upb_MessageDef_OneofCount(md_)); }
377
378 private:
379 const upb_MessageDef* md_;
380 };
381
382 public:
fields() const383 FieldAccessor fields() const { return FieldAccessor(ptr()); }
oneofs() const384 OneofAccessor oneofs() const { return OneofAccessor(ptr()); }
385
386 private:
387 const upb_MessageDef* ptr_;
388 };
389
390 class EnumValDefPtr {
391 public:
EnumValDefPtr()392 EnumValDefPtr() : ptr_(nullptr) {}
EnumValDefPtr(const upb_EnumValueDef * ptr)393 explicit EnumValDefPtr(const upb_EnumValueDef* ptr) : ptr_(ptr) {}
394
UPB_DESC(EnumValueOptions)395 const UPB_DESC(EnumValueOptions) * options() const {
396 return upb_EnumValueDef_Options(ptr_);
397 }
398
number() const399 int32_t number() const { return upb_EnumValueDef_Number(ptr_); }
full_name() const400 const char* full_name() const { return upb_EnumValueDef_FullName(ptr_); }
name() const401 const char* name() const { return upb_EnumValueDef_Name(ptr_); }
402
403 private:
404 const upb_EnumValueDef* ptr_;
405 };
406
407 class EnumDefPtr {
408 public:
EnumDefPtr()409 EnumDefPtr() : ptr_(nullptr) {}
EnumDefPtr(const upb_EnumDef * ptr)410 explicit EnumDefPtr(const upb_EnumDef* ptr) : ptr_(ptr) {}
411
UPB_DESC(EnumOptions)412 const UPB_DESC(EnumOptions) * options() const {
413 return upb_EnumDef_Options(ptr_);
414 }
415
mini_table() const416 const upb_MiniTableEnum* mini_table() const {
417 return _upb_EnumDef_MiniTable(ptr_);
418 }
419
MiniDescriptorEncode() const420 std::string MiniDescriptorEncode() const {
421 upb::Arena arena;
422 upb_StringView md;
423 upb_EnumDef_MiniDescriptorEncode(ptr_, arena.ptr(), &md);
424 return std::string(md.data, md.size);
425 }
426
ptr() const427 const upb_EnumDef* ptr() const { return ptr_; }
operator bool() const428 explicit operator bool() const { return ptr_ != nullptr; }
429
430 FileDefPtr file() const;
full_name() const431 const char* full_name() const { return upb_EnumDef_FullName(ptr_); }
name() const432 const char* name() const { return upb_EnumDef_Name(ptr_); }
is_closed() const433 bool is_closed() const { return upb_EnumDef_IsClosed(ptr_); }
434
435 // Returns the MessageDef that contains this EnumDef (or null).
436 MessageDefPtr containing_type() const;
437
438 // The value that is used as the default when no field default is specified.
439 // If not set explicitly, the first value that was added will be used.
440 // The default value must be a member of the enum.
441 // Requires that value_count() > 0.
default_value() const442 int32_t default_value() const { return upb_EnumDef_Default(ptr_); }
443
444 // Returns the number of values currently defined in the enum. Note that
445 // multiple names can refer to the same number, so this may be greater than
446 // the total number of unique numbers.
value_count() const447 int value_count() const { return upb_EnumDef_ValueCount(ptr_); }
value(int i) const448 EnumValDefPtr value(int i) const {
449 return EnumValDefPtr(upb_EnumDef_Value(ptr_, i));
450 }
451
452 // Lookups from name to integer, returning true if found.
FindValueByName(const char * name) const453 EnumValDefPtr FindValueByName(const char* name) const {
454 return EnumValDefPtr(upb_EnumDef_FindValueByName(ptr_, name));
455 }
456
457 // Finds the name corresponding to the given number, or NULL if none was
458 // found. If more than one name corresponds to this number, returns the
459 // first one that was added.
FindValueByNumber(int32_t num) const460 EnumValDefPtr FindValueByNumber(int32_t num) const {
461 return EnumValDefPtr(upb_EnumDef_FindValueByNumber(ptr_, num));
462 }
463
464 private:
465 const upb_EnumDef* ptr_;
466 };
467
468 // Class that represents a .proto file with some things defined in it.
469 //
470 // Many users won't care about FileDefs, but they are necessary if you want to
471 // read the values of file-level options.
472 class FileDefPtr {
473 public:
FileDefPtr(const upb_FileDef * ptr)474 explicit FileDefPtr(const upb_FileDef* ptr) : ptr_(ptr) {}
475
UPB_DESC(FileOptions)476 const UPB_DESC(FileOptions) * options() const {
477 return upb_FileDef_Options(ptr_);
478 }
479
ptr() const480 const upb_FileDef* ptr() const { return ptr_; }
481
482 // Get/set name of the file (eg. "foo/bar.proto").
name() const483 const char* name() const { return upb_FileDef_Name(ptr_); }
484
485 // Package name for definitions inside the file (eg. "foo.bar").
package() const486 const char* package() const { return upb_FileDef_Package(ptr_); }
487
488 // Syntax for the file. Defaults to proto2.
syntax() const489 upb_Syntax syntax() const { return upb_FileDef_Syntax(ptr_); }
490
491 // Get the list of dependencies from the file. These are returned in the
492 // order that they were added to the FileDefPtr.
dependency_count() const493 int dependency_count() const { return upb_FileDef_DependencyCount(ptr_); }
dependency(int index) const494 FileDefPtr dependency(int index) const {
495 return FileDefPtr(upb_FileDef_Dependency(ptr_, index));
496 }
497
public_dependency_count() const498 int public_dependency_count() const {
499 return upb_FileDef_PublicDependencyCount(ptr_);
500 }
public_dependency(int index) const501 FileDefPtr public_dependency(int index) const {
502 return FileDefPtr(upb_FileDef_PublicDependency(ptr_, index));
503 }
504
toplevel_enum_count() const505 int toplevel_enum_count() const {
506 return upb_FileDef_TopLevelEnumCount(ptr_);
507 }
toplevel_enum(int index) const508 EnumDefPtr toplevel_enum(int index) const {
509 return EnumDefPtr(upb_FileDef_TopLevelEnum(ptr_, index));
510 }
511
toplevel_message_count() const512 int toplevel_message_count() const {
513 return upb_FileDef_TopLevelMessageCount(ptr_);
514 }
toplevel_message(int index) const515 MessageDefPtr toplevel_message(int index) const {
516 return MessageDefPtr(upb_FileDef_TopLevelMessage(ptr_, index));
517 }
518
toplevel_extension_count() const519 int toplevel_extension_count() const {
520 return upb_FileDef_TopLevelExtensionCount(ptr_);
521 }
toplevel_extension(int index) const522 FieldDefPtr toplevel_extension(int index) const {
523 return FieldDefPtr(upb_FileDef_TopLevelExtension(ptr_, index));
524 }
525
resolves(const char * path) const526 bool resolves(const char* path) const {
527 return upb_FileDef_Resolves(ptr_, path);
528 }
529
operator bool() const530 explicit operator bool() const { return ptr_ != nullptr; }
531
operator ==(FileDefPtr lhs,FileDefPtr rhs)532 friend bool operator==(FileDefPtr lhs, FileDefPtr rhs) {
533 return lhs.ptr_ == rhs.ptr_;
534 }
535
operator !=(FileDefPtr lhs,FileDefPtr rhs)536 friend bool operator!=(FileDefPtr lhs, FileDefPtr rhs) {
537 return !(lhs == rhs);
538 }
539
540 private:
541 const upb_FileDef* ptr_;
542 };
543
544 // Non-const methods in upb::DefPool are NOT thread-safe.
545 class DefPool {
546 public:
DefPool()547 DefPool() : ptr_(upb_DefPool_New(), upb_DefPool_Free) {}
DefPool(upb_DefPool * s)548 explicit DefPool(upb_DefPool* s) : ptr_(s, upb_DefPool_Free) {}
549
ptr() const550 const upb_DefPool* ptr() const { return ptr_.get(); }
ptr()551 upb_DefPool* ptr() { return ptr_.get(); }
552
553 // Finds an entry in the symbol table with this exact name. If not found,
554 // returns NULL.
FindMessageByName(const char * sym) const555 MessageDefPtr FindMessageByName(const char* sym) const {
556 return MessageDefPtr(upb_DefPool_FindMessageByName(ptr_.get(), sym));
557 }
558
FindEnumByName(const char * sym) const559 EnumDefPtr FindEnumByName(const char* sym) const {
560 return EnumDefPtr(upb_DefPool_FindEnumByName(ptr_.get(), sym));
561 }
562
FindFileByName(const char * name) const563 FileDefPtr FindFileByName(const char* name) const {
564 return FileDefPtr(upb_DefPool_FindFileByName(ptr_.get(), name));
565 }
566
FindExtensionByName(const char * name) const567 FieldDefPtr FindExtensionByName(const char* name) const {
568 return FieldDefPtr(upb_DefPool_FindExtensionByName(ptr_.get(), name));
569 }
570
_SetPlatform(upb_MiniTablePlatform platform)571 void _SetPlatform(upb_MiniTablePlatform platform) {
572 _upb_DefPool_SetPlatform(ptr_.get(), platform);
573 }
574
575 // TODO: iteration?
576
577 // Adds the given serialized FileDescriptorProto to the pool.
AddFile(const UPB_DESC (FileDescriptorProto)* file_proto,Status * status)578 FileDefPtr AddFile(const UPB_DESC(FileDescriptorProto) * file_proto,
579 Status* status) {
580 return FileDefPtr(
581 upb_DefPool_AddFile(ptr_.get(), file_proto, status->ptr()));
582 }
583
584 private:
585 std::unique_ptr<upb_DefPool, decltype(&upb_DefPool_Free)> ptr_;
586 };
587
file() const588 inline FileDefPtr EnumDefPtr::file() const {
589 return FileDefPtr(upb_EnumDef_File(ptr_));
590 }
591
file() const592 inline FileDefPtr FieldDefPtr::file() const {
593 return FileDefPtr(upb_FieldDef_File(ptr_));
594 }
595
file() const596 inline FileDefPtr MessageDefPtr::file() const {
597 return FileDefPtr(upb_MessageDef_File(ptr_));
598 }
599
containing_type() const600 inline MessageDefPtr MessageDefPtr::containing_type() const {
601 return MessageDefPtr(upb_MessageDef_ContainingType(ptr_));
602 }
603
containing_type() const604 inline MessageDefPtr EnumDefPtr::containing_type() const {
605 return MessageDefPtr(upb_EnumDef_ContainingType(ptr_));
606 }
607
enum_type(int i) const608 inline EnumDefPtr MessageDefPtr::enum_type(int i) const {
609 return EnumDefPtr(upb_MessageDef_NestedEnum(ptr_, i));
610 }
611
message_type() const612 inline MessageDefPtr FieldDefPtr::message_type() const {
613 return MessageDefPtr(upb_FieldDef_MessageSubDef(ptr_));
614 }
615
containing_type() const616 inline MessageDefPtr FieldDefPtr::containing_type() const {
617 return MessageDefPtr(upb_FieldDef_ContainingType(ptr_));
618 }
619
extension_scope() const620 inline MessageDefPtr FieldDefPtr::extension_scope() const {
621 return MessageDefPtr(upb_FieldDef_ExtensionScope(ptr_));
622 }
623
containing_type() const624 inline MessageDefPtr OneofDefPtr::containing_type() const {
625 return MessageDefPtr(upb_OneofDef_ContainingType(ptr_));
626 }
627
containing_oneof() const628 inline OneofDefPtr FieldDefPtr::containing_oneof() const {
629 return OneofDefPtr(upb_FieldDef_ContainingOneof(ptr_));
630 }
631
real_containing_oneof() const632 inline OneofDefPtr FieldDefPtr::real_containing_oneof() const {
633 return OneofDefPtr(upb_FieldDef_RealContainingOneof(ptr_));
634 }
635
enum_subdef() const636 inline EnumDefPtr FieldDefPtr::enum_subdef() const {
637 return EnumDefPtr(upb_FieldDef_EnumSubDef(ptr_));
638 }
639
640 } // namespace upb
641
642 #include "upb/port/undef.inc"
643
644 #endif // UPB_REFLECTION_DEF_HPP_
645