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