File indexing completed on 2026-08-27 09:40:20
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef UPB_REFLECTION_DEF_HPP_
0009 #define UPB_REFLECTION_DEF_HPP_
0010
0011 #include <stdint.h>
0012
0013 #include <cstring>
0014 #include <memory>
0015 #include <string>
0016
0017 #include "upb/base/descriptor_constants.h"
0018 #include "upb/base/status.hpp"
0019 #include "upb/base/string_view.h"
0020 #include "upb/mem/arena.hpp"
0021 #include "upb/message/value.h"
0022 #include "upb/mini_descriptor/decode.h"
0023 #include "upb/mini_table/enum.h"
0024 #include "upb/mini_table/field.h"
0025 #include "upb/mini_table/message.h"
0026 #include "upb/reflection/def.h"
0027 #include "upb/reflection/internal/def_pool.h"
0028 #include "upb/reflection/internal/enum_def.h"
0029 #include "upb/reflection/message.h"
0030
0031
0032 #include "upb/port/def.inc"
0033
0034 namespace upb {
0035
0036 typedef upb_MessageValue MessageValue;
0037
0038 class EnumDefPtr;
0039 class FileDefPtr;
0040 class MessageDefPtr;
0041 class OneofDefPtr;
0042
0043
0044
0045
0046 class FieldDefPtr {
0047 public:
0048 FieldDefPtr() : ptr_(nullptr) {}
0049 explicit FieldDefPtr(const upb_FieldDef* ptr) : ptr_(ptr) {}
0050
0051 const upb_FieldDef* ptr() const { return ptr_; }
0052
0053 typedef upb_FieldType Type;
0054 typedef upb_CType CType;
0055 typedef upb_Label Label;
0056
0057 FileDefPtr file() const;
0058 const char* full_name() const { return upb_FieldDef_FullName(ptr_); }
0059
0060 const upb_MiniTableField* mini_table() const {
0061 return upb_FieldDef_MiniTable(ptr_);
0062 }
0063
0064 std::string MiniDescriptorEncode() const {
0065 upb::Arena arena;
0066 upb_StringView md;
0067 upb_FieldDef_MiniDescriptorEncode(ptr_, arena.ptr(), &md);
0068 return std::string(md.data, md.size);
0069 }
0070
0071 const UPB_DESC(FieldOptions) * options() const {
0072 return upb_FieldDef_Options(ptr_);
0073 }
0074
0075 Type type() const { return upb_FieldDef_Type(ptr_); }
0076 CType ctype() const { return upb_FieldDef_CType(ptr_); }
0077 Label label() const { return upb_FieldDef_Label(ptr_); }
0078 const char* name() const { return upb_FieldDef_Name(ptr_); }
0079 const char* json_name() const { return upb_FieldDef_JsonName(ptr_); }
0080 uint32_t number() const { return upb_FieldDef_Number(ptr_); }
0081 bool is_extension() const { return upb_FieldDef_IsExtension(ptr_); }
0082 bool is_required() const { return upb_FieldDef_IsRequired(ptr_); }
0083 bool has_presence() const { return upb_FieldDef_HasPresence(ptr_); }
0084
0085
0086
0087
0088
0089
0090 bool packed() const { return upb_FieldDef_IsPacked(ptr_); }
0091
0092
0093
0094
0095
0096
0097
0098 uint32_t index() const { return upb_FieldDef_Index(ptr_); }
0099
0100
0101
0102
0103 uint32_t layout_index() const { return upb_FieldDef_LayoutIndex(ptr_); }
0104
0105
0106
0107 MessageDefPtr containing_type() const;
0108
0109
0110
0111 MessageDefPtr extension_scope() const;
0112
0113
0114
0115 OneofDefPtr containing_oneof() const;
0116 OneofDefPtr real_containing_oneof() const;
0117
0118
0119 bool IsEnum() const { return upb_FieldDef_IsEnum(ptr_); }
0120 bool IsSubMessage() const { return upb_FieldDef_IsSubMessage(ptr_); }
0121 bool IsString() const { return upb_FieldDef_IsString(ptr_); }
0122 bool IsSequence() const { return upb_FieldDef_IsRepeated(ptr_); }
0123 bool IsPrimitive() const { return upb_FieldDef_IsPrimitive(ptr_); }
0124 bool IsMap() const { return upb_FieldDef_IsMap(ptr_); }
0125
0126 MessageValue default_value() const { return upb_FieldDef_Default(ptr_); }
0127
0128
0129
0130
0131 EnumDefPtr enum_subdef() const;
0132 MessageDefPtr message_type() const;
0133
0134 explicit operator bool() const { return ptr_ != nullptr; }
0135
0136 friend bool operator==(FieldDefPtr lhs, FieldDefPtr rhs) {
0137 return lhs.ptr_ == rhs.ptr_;
0138 }
0139
0140 friend bool operator!=(FieldDefPtr lhs, FieldDefPtr rhs) {
0141 return !(lhs == rhs);
0142 }
0143
0144 private:
0145 const upb_FieldDef* ptr_;
0146 };
0147
0148
0149 class OneofDefPtr {
0150 public:
0151 OneofDefPtr() : ptr_(nullptr) {}
0152 explicit OneofDefPtr(const upb_OneofDef* ptr) : ptr_(ptr) {}
0153
0154 const upb_OneofDef* ptr() const { return ptr_; }
0155 explicit operator bool() const { return ptr_ != nullptr; }
0156
0157 const UPB_DESC(OneofOptions) * options() const {
0158 return upb_OneofDef_Options(ptr_);
0159 }
0160
0161
0162 MessageDefPtr containing_type() const;
0163
0164
0165 const char* name() const { return upb_OneofDef_Name(ptr_); }
0166 const char* full_name() const { return upb_OneofDef_FullName(ptr_); }
0167
0168
0169 int field_count() const { return upb_OneofDef_FieldCount(ptr_); }
0170 FieldDefPtr field(int i) const {
0171 return FieldDefPtr(upb_OneofDef_Field(ptr_, i));
0172 }
0173
0174
0175 FieldDefPtr FindFieldByName(const char* name, size_t len) const {
0176 return FieldDefPtr(upb_OneofDef_LookupNameWithSize(ptr_, name, len));
0177 }
0178 FieldDefPtr FindFieldByName(const char* name) const {
0179 return FieldDefPtr(upb_OneofDef_LookupName(ptr_, name));
0180 }
0181
0182 template <class T>
0183 FieldDefPtr FindFieldByName(const T& str) const {
0184 return FindFieldByName(str.c_str(), str.size());
0185 }
0186
0187
0188 FieldDefPtr FindFieldByNumber(uint32_t num) const {
0189 return FieldDefPtr(upb_OneofDef_LookupNumber(ptr_, num));
0190 }
0191
0192 private:
0193 const upb_OneofDef* ptr_;
0194 };
0195
0196
0197 class MessageDefPtr {
0198 public:
0199 MessageDefPtr() : ptr_(nullptr) {}
0200 explicit MessageDefPtr(const upb_MessageDef* ptr) : ptr_(ptr) {}
0201
0202 const UPB_DESC(MessageOptions) * options() const {
0203 return upb_MessageDef_Options(ptr_);
0204 }
0205
0206 std::string MiniDescriptorEncode() const {
0207 upb::Arena arena;
0208 upb_StringView md;
0209 upb_MessageDef_MiniDescriptorEncode(ptr_, arena.ptr(), &md);
0210 return std::string(md.data, md.size);
0211 }
0212
0213 const upb_MessageDef* ptr() const { return ptr_; }
0214
0215 FileDefPtr file() const;
0216
0217 const char* full_name() const { return upb_MessageDef_FullName(ptr_); }
0218 const char* name() const { return upb_MessageDef_Name(ptr_); }
0219
0220
0221 MessageDefPtr containing_type() const;
0222
0223 const upb_MiniTable* mini_table() const {
0224 return upb_MessageDef_MiniTable(ptr_);
0225 }
0226
0227
0228 int field_count() const { return upb_MessageDef_FieldCount(ptr_); }
0229 FieldDefPtr field(int i) const {
0230 return FieldDefPtr(upb_MessageDef_Field(ptr_, i));
0231 }
0232
0233
0234 int oneof_count() const { return upb_MessageDef_OneofCount(ptr_); }
0235 int real_oneof_count() const { return upb_MessageDef_RealOneofCount(ptr_); }
0236 OneofDefPtr oneof(int i) const {
0237 return OneofDefPtr(upb_MessageDef_Oneof(ptr_, i));
0238 }
0239
0240 int enum_type_count() const { return upb_MessageDef_NestedEnumCount(ptr_); }
0241 EnumDefPtr enum_type(int i) const;
0242
0243 int nested_message_count() const {
0244 return upb_MessageDef_NestedMessageCount(ptr_);
0245 }
0246 MessageDefPtr nested_message(int i) const {
0247 return MessageDefPtr(upb_MessageDef_NestedMessage(ptr_, i));
0248 }
0249
0250 int nested_extension_count() const {
0251 return upb_MessageDef_NestedExtensionCount(ptr_);
0252 }
0253 FieldDefPtr nested_extension(int i) const {
0254 return FieldDefPtr(upb_MessageDef_NestedExtension(ptr_, i));
0255 }
0256
0257 int extension_range_count() const {
0258 return upb_MessageDef_ExtensionRangeCount(ptr_);
0259 }
0260
0261 upb_Syntax syntax() const { return upb_MessageDef_Syntax(ptr_); }
0262
0263
0264 FieldDefPtr FindFieldByNumber(uint32_t number) const {
0265 return FieldDefPtr(upb_MessageDef_FindFieldByNumber(ptr_, number));
0266 }
0267 FieldDefPtr FindFieldByName(const char* name, size_t len) const {
0268 return FieldDefPtr(upb_MessageDef_FindFieldByNameWithSize(ptr_, name, len));
0269 }
0270 FieldDefPtr FindFieldByName(const char* name) const {
0271 return FieldDefPtr(upb_MessageDef_FindFieldByName(ptr_, name));
0272 }
0273
0274 template <class T>
0275 FieldDefPtr FindFieldByName(const T& str) const {
0276 return FindFieldByName(str.c_str(), str.size());
0277 }
0278
0279 OneofDefPtr FindOneofByName(const char* name, size_t len) const {
0280 return OneofDefPtr(upb_MessageDef_FindOneofByNameWithSize(ptr_, name, len));
0281 }
0282
0283 OneofDefPtr FindOneofByName(const char* name) const {
0284 return OneofDefPtr(upb_MessageDef_FindOneofByName(ptr_, name));
0285 }
0286
0287 template <class T>
0288 OneofDefPtr FindOneofByName(const T& str) const {
0289 return FindOneofByName(str.c_str(), str.size());
0290 }
0291
0292
0293 bool mapentry() const { return upb_MessageDef_IsMapEntry(ptr_); }
0294
0295 FieldDefPtr map_key() const {
0296 if (!mapentry()) return FieldDefPtr();
0297 return FieldDefPtr(upb_MessageDef_Field(ptr_, 0));
0298 }
0299
0300 FieldDefPtr map_value() const {
0301 if (!mapentry()) return FieldDefPtr();
0302 return FieldDefPtr(upb_MessageDef_Field(ptr_, 1));
0303 }
0304
0305
0306
0307 upb_WellKnown wellknowntype() const {
0308 return upb_MessageDef_WellKnownType(ptr_);
0309 }
0310
0311 explicit operator bool() const { return ptr_ != nullptr; }
0312
0313 friend bool operator==(MessageDefPtr lhs, MessageDefPtr rhs) {
0314 return lhs.ptr_ == rhs.ptr_;
0315 }
0316
0317 friend bool operator!=(MessageDefPtr lhs, MessageDefPtr rhs) {
0318 return !(lhs == rhs);
0319 }
0320
0321 private:
0322 class FieldIter {
0323 public:
0324 explicit FieldIter(const upb_MessageDef* m, int i) : m_(m), i_(i) {}
0325 void operator++() { i_++; }
0326
0327 FieldDefPtr operator*() {
0328 return FieldDefPtr(upb_MessageDef_Field(m_, i_));
0329 }
0330
0331 friend bool operator==(FieldIter lhs, FieldIter rhs) {
0332 return lhs.i_ == rhs.i_;
0333 }
0334
0335 friend bool operator!=(FieldIter lhs, FieldIter rhs) {
0336 return !(lhs == rhs);
0337 }
0338
0339 private:
0340 const upb_MessageDef* m_;
0341 int i_;
0342 };
0343
0344 class FieldAccessor {
0345 public:
0346 explicit FieldAccessor(const upb_MessageDef* md) : md_(md) {}
0347 FieldIter begin() { return FieldIter(md_, 0); }
0348 FieldIter end() { return FieldIter(md_, upb_MessageDef_FieldCount(md_)); }
0349
0350 private:
0351 const upb_MessageDef* md_;
0352 };
0353
0354 class OneofIter {
0355 public:
0356 explicit OneofIter(const upb_MessageDef* m, int i) : m_(m), i_(i) {}
0357 void operator++() { i_++; }
0358
0359 OneofDefPtr operator*() {
0360 return OneofDefPtr(upb_MessageDef_Oneof(m_, i_));
0361 }
0362
0363 friend bool operator==(OneofIter lhs, OneofIter rhs) {
0364 return lhs.i_ == rhs.i_;
0365 }
0366
0367 friend bool operator!=(OneofIter lhs, OneofIter rhs) {
0368 return !(lhs == rhs);
0369 }
0370
0371 private:
0372 const upb_MessageDef* m_;
0373 int i_;
0374 };
0375
0376 class OneofAccessor {
0377 public:
0378 explicit OneofAccessor(const upb_MessageDef* md) : md_(md) {}
0379 OneofIter begin() { return OneofIter(md_, 0); }
0380 OneofIter end() { return OneofIter(md_, upb_MessageDef_OneofCount(md_)); }
0381
0382 private:
0383 const upb_MessageDef* md_;
0384 };
0385
0386 public:
0387 FieldAccessor fields() const { return FieldAccessor(ptr()); }
0388 OneofAccessor oneofs() const { return OneofAccessor(ptr()); }
0389
0390 private:
0391 const upb_MessageDef* ptr_;
0392 };
0393
0394 class EnumValDefPtr {
0395 public:
0396 EnumValDefPtr() : ptr_(nullptr) {}
0397 explicit EnumValDefPtr(const upb_EnumValueDef* ptr) : ptr_(ptr) {}
0398
0399 const UPB_DESC(EnumValueOptions) * options() const {
0400 return upb_EnumValueDef_Options(ptr_);
0401 }
0402
0403 int32_t number() const { return upb_EnumValueDef_Number(ptr_); }
0404 const char* full_name() const { return upb_EnumValueDef_FullName(ptr_); }
0405 const char* name() const { return upb_EnumValueDef_Name(ptr_); }
0406
0407 private:
0408 const upb_EnumValueDef* ptr_;
0409 };
0410
0411 class EnumDefPtr {
0412 public:
0413 EnumDefPtr() : ptr_(nullptr) {}
0414 explicit EnumDefPtr(const upb_EnumDef* ptr) : ptr_(ptr) {}
0415
0416 const UPB_DESC(EnumOptions) * options() const {
0417 return upb_EnumDef_Options(ptr_);
0418 }
0419
0420 const upb_MiniTableEnum* mini_table() const {
0421 return _upb_EnumDef_MiniTable(ptr_);
0422 }
0423
0424 std::string MiniDescriptorEncode() const {
0425 upb::Arena arena;
0426 upb_StringView md;
0427 upb_EnumDef_MiniDescriptorEncode(ptr_, arena.ptr(), &md);
0428 return std::string(md.data, md.size);
0429 }
0430
0431 const upb_EnumDef* ptr() const { return ptr_; }
0432 explicit operator bool() const { return ptr_ != nullptr; }
0433
0434 FileDefPtr file() const;
0435 const char* full_name() const { return upb_EnumDef_FullName(ptr_); }
0436 const char* name() const { return upb_EnumDef_Name(ptr_); }
0437 bool is_closed() const { return upb_EnumDef_IsClosed(ptr_); }
0438
0439
0440 MessageDefPtr containing_type() const;
0441
0442
0443
0444
0445
0446 int32_t default_value() const { return upb_EnumDef_Default(ptr_); }
0447
0448
0449
0450
0451 int value_count() const { return upb_EnumDef_ValueCount(ptr_); }
0452 EnumValDefPtr value(int i) const {
0453 return EnumValDefPtr(upb_EnumDef_Value(ptr_, i));
0454 }
0455
0456
0457 EnumValDefPtr FindValueByName(const char* name) const {
0458 return EnumValDefPtr(upb_EnumDef_FindValueByName(ptr_, name));
0459 }
0460
0461
0462
0463
0464 EnumValDefPtr FindValueByNumber(int32_t num) const {
0465 return EnumValDefPtr(upb_EnumDef_FindValueByNumber(ptr_, num));
0466 }
0467
0468 private:
0469 const upb_EnumDef* ptr_;
0470 };
0471
0472
0473
0474
0475
0476 class FileDefPtr {
0477 public:
0478 explicit FileDefPtr(const upb_FileDef* ptr) : ptr_(ptr) {}
0479
0480 const UPB_DESC(FileOptions) * options() const {
0481 return upb_FileDef_Options(ptr_);
0482 }
0483
0484 const upb_FileDef* ptr() const { return ptr_; }
0485
0486
0487 const char* name() const { return upb_FileDef_Name(ptr_); }
0488
0489
0490 const char* package() const { return upb_FileDef_Package(ptr_); }
0491
0492
0493 upb_Syntax syntax() const { return upb_FileDef_Syntax(ptr_); }
0494
0495
0496
0497 int dependency_count() const { return upb_FileDef_DependencyCount(ptr_); }
0498 FileDefPtr dependency(int index) const {
0499 return FileDefPtr(upb_FileDef_Dependency(ptr_, index));
0500 }
0501
0502 int public_dependency_count() const {
0503 return upb_FileDef_PublicDependencyCount(ptr_);
0504 }
0505 FileDefPtr public_dependency(int index) const {
0506 return FileDefPtr(upb_FileDef_PublicDependency(ptr_, index));
0507 }
0508
0509 int toplevel_enum_count() const {
0510 return upb_FileDef_TopLevelEnumCount(ptr_);
0511 }
0512 EnumDefPtr toplevel_enum(int index) const {
0513 return EnumDefPtr(upb_FileDef_TopLevelEnum(ptr_, index));
0514 }
0515
0516 int toplevel_message_count() const {
0517 return upb_FileDef_TopLevelMessageCount(ptr_);
0518 }
0519 MessageDefPtr toplevel_message(int index) const {
0520 return MessageDefPtr(upb_FileDef_TopLevelMessage(ptr_, index));
0521 }
0522
0523 int toplevel_extension_count() const {
0524 return upb_FileDef_TopLevelExtensionCount(ptr_);
0525 }
0526 FieldDefPtr toplevel_extension(int index) const {
0527 return FieldDefPtr(upb_FileDef_TopLevelExtension(ptr_, index));
0528 }
0529
0530 bool resolves(const char* path) const {
0531 return upb_FileDef_Resolves(ptr_, path);
0532 }
0533
0534 explicit operator bool() const { return ptr_ != nullptr; }
0535
0536 friend bool operator==(FileDefPtr lhs, FileDefPtr rhs) {
0537 return lhs.ptr_ == rhs.ptr_;
0538 }
0539
0540 friend bool operator!=(FileDefPtr lhs, FileDefPtr rhs) {
0541 return !(lhs == rhs);
0542 }
0543
0544 private:
0545 const upb_FileDef* ptr_;
0546 };
0547
0548
0549 class DefPool {
0550 public:
0551 DefPool() : ptr_(upb_DefPool_New(), upb_DefPool_Free) {}
0552 explicit DefPool(upb_DefPool* s) : ptr_(s, upb_DefPool_Free) {}
0553
0554 const upb_DefPool* ptr() const { return ptr_.get(); }
0555 upb_DefPool* ptr() { return ptr_.get(); }
0556
0557
0558
0559 MessageDefPtr FindMessageByName(const char* sym) const {
0560 return MessageDefPtr(upb_DefPool_FindMessageByName(ptr_.get(), sym));
0561 }
0562
0563 EnumDefPtr FindEnumByName(const char* sym) const {
0564 return EnumDefPtr(upb_DefPool_FindEnumByName(ptr_.get(), sym));
0565 }
0566
0567 FileDefPtr FindFileByName(const char* name) const {
0568 return FileDefPtr(upb_DefPool_FindFileByName(ptr_.get(), name));
0569 }
0570
0571 FieldDefPtr FindExtensionByName(const char* name) const {
0572 return FieldDefPtr(upb_DefPool_FindExtensionByName(ptr_.get(), name));
0573 }
0574
0575 void _SetPlatform(upb_MiniTablePlatform platform) {
0576 _upb_DefPool_SetPlatform(ptr_.get(), platform);
0577 }
0578
0579
0580
0581
0582 FileDefPtr AddFile(const UPB_DESC(FileDescriptorProto) * file_proto,
0583 Status* status) {
0584 return FileDefPtr(
0585 upb_DefPool_AddFile(ptr_.get(), file_proto, status->ptr()));
0586 }
0587
0588 private:
0589 std::unique_ptr<upb_DefPool, decltype(&upb_DefPool_Free)> ptr_;
0590 };
0591
0592 inline FileDefPtr EnumDefPtr::file() const {
0593 return FileDefPtr(upb_EnumDef_File(ptr_));
0594 }
0595
0596 inline FileDefPtr FieldDefPtr::file() const {
0597 return FileDefPtr(upb_FieldDef_File(ptr_));
0598 }
0599
0600 inline FileDefPtr MessageDefPtr::file() const {
0601 return FileDefPtr(upb_MessageDef_File(ptr_));
0602 }
0603
0604 inline MessageDefPtr MessageDefPtr::containing_type() const {
0605 return MessageDefPtr(upb_MessageDef_ContainingType(ptr_));
0606 }
0607
0608 inline MessageDefPtr EnumDefPtr::containing_type() const {
0609 return MessageDefPtr(upb_EnumDef_ContainingType(ptr_));
0610 }
0611
0612 inline EnumDefPtr MessageDefPtr::enum_type(int i) const {
0613 return EnumDefPtr(upb_MessageDef_NestedEnum(ptr_, i));
0614 }
0615
0616 inline MessageDefPtr FieldDefPtr::message_type() const {
0617 return MessageDefPtr(upb_FieldDef_MessageSubDef(ptr_));
0618 }
0619
0620 inline MessageDefPtr FieldDefPtr::containing_type() const {
0621 return MessageDefPtr(upb_FieldDef_ContainingType(ptr_));
0622 }
0623
0624 inline MessageDefPtr FieldDefPtr::extension_scope() const {
0625 return MessageDefPtr(upb_FieldDef_ExtensionScope(ptr_));
0626 }
0627
0628 inline MessageDefPtr OneofDefPtr::containing_type() const {
0629 return MessageDefPtr(upb_OneofDef_ContainingType(ptr_));
0630 }
0631
0632 inline OneofDefPtr FieldDefPtr::containing_oneof() const {
0633 return OneofDefPtr(upb_FieldDef_ContainingOneof(ptr_));
0634 }
0635
0636 inline OneofDefPtr FieldDefPtr::real_containing_oneof() const {
0637 return OneofDefPtr(upb_FieldDef_RealContainingOneof(ptr_));
0638 }
0639
0640 inline EnumDefPtr FieldDefPtr::enum_subdef() const {
0641 return EnumDefPtr(upb_FieldDef_EnumSubDef(ptr_));
0642 }
0643
0644 }
0645
0646 #include "upb/port/undef.inc"
0647
0648 #endif