def.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. #ifndef UPB_DEF_HPP_
  2. #define UPB_DEF_HPP_
  3. #include <cstring>
  4. #include <memory>
  5. #include <string>
  6. #include <vector>
  7. #include "upb/def.h"
  8. #include "upb/upb.hpp"
  9. namespace upb {
  10. class EnumDefPtr;
  11. class MessageDefPtr;
  12. class OneofDefPtr;
  13. // A upb::FieldDefPtr describes a single field in a message. It is most often
  14. // found as a part of a upb_msgdef, but can also stand alone to represent
  15. // an extension.
  16. class FieldDefPtr {
  17. public:
  18. FieldDefPtr() : ptr_(nullptr) {}
  19. explicit FieldDefPtr(const upb_fielddef* ptr) : ptr_(ptr) {}
  20. const upb_fielddef* ptr() const { return ptr_; }
  21. explicit operator bool() const { return ptr_ != nullptr; }
  22. typedef upb_fieldtype_t Type;
  23. typedef upb_label_t Label;
  24. typedef upb_descriptortype_t DescriptorType;
  25. const char* full_name() const { return upb_fielddef_fullname(ptr_); }
  26. Type type() const { return upb_fielddef_type(ptr_); }
  27. Label label() const { return upb_fielddef_label(ptr_); }
  28. const char* name() const { return upb_fielddef_name(ptr_); }
  29. const char* json_name() const { return upb_fielddef_jsonname(ptr_); }
  30. uint32_t number() const { return upb_fielddef_number(ptr_); }
  31. bool is_extension() const { return upb_fielddef_isextension(ptr_); }
  32. // For UPB_TYPE_MESSAGE fields only where is_tag_delimited() == false,
  33. // indicates whether this field should have lazy parsing handlers that yield
  34. // the unparsed string for the submessage.
  35. //
  36. // TODO(haberman): I think we want to move this into a FieldOptions container
  37. // when we add support for custom options (the FieldOptions struct will
  38. // contain both regular FieldOptions like "lazy" *and* custom options).
  39. bool lazy() const { return upb_fielddef_lazy(ptr_); }
  40. // For non-string, non-submessage fields, this indicates whether binary
  41. // protobufs are encoded in packed or non-packed format.
  42. //
  43. // TODO(haberman): see note above about putting options like this into a
  44. // FieldOptions container.
  45. bool packed() const { return upb_fielddef_packed(ptr_); }
  46. // An integer that can be used as an index into an array of fields for
  47. // whatever message this field belongs to. Guaranteed to be less than
  48. // f->containing_type()->field_count(). May only be accessed once the def has
  49. // been finalized.
  50. uint32_t index() const { return upb_fielddef_index(ptr_); }
  51. // The MessageDef to which this field belongs.
  52. //
  53. // If this field has been added to a MessageDef, that message can be retrieved
  54. // directly (this is always the case for frozen FieldDefs).
  55. //
  56. // If the field has not yet been added to a MessageDef, you can set the name
  57. // of the containing type symbolically instead. This is mostly useful for
  58. // extensions, where the extension is declared separately from the message.
  59. MessageDefPtr containing_type() const;
  60. // The OneofDef to which this field belongs, or NULL if this field is not part
  61. // of a oneof.
  62. OneofDefPtr containing_oneof() const;
  63. // The field's type according to the enum in descriptor.proto. This is not
  64. // the same as UPB_TYPE_*, because it distinguishes between (for example)
  65. // INT32 and SINT32, whereas our "type" enum does not. This return of
  66. // descriptor_type() is a function of type(), integer_format(), and
  67. // is_tag_delimited().
  68. DescriptorType descriptor_type() const {
  69. return upb_fielddef_descriptortype(ptr_);
  70. }
  71. // Convenient field type tests.
  72. bool IsSubMessage() const { return upb_fielddef_issubmsg(ptr_); }
  73. bool IsString() const { return upb_fielddef_isstring(ptr_); }
  74. bool IsSequence() const { return upb_fielddef_isseq(ptr_); }
  75. bool IsPrimitive() const { return upb_fielddef_isprimitive(ptr_); }
  76. bool IsMap() const { return upb_fielddef_ismap(ptr_); }
  77. // Returns the non-string default value for this fielddef, which may either
  78. // be something the client set explicitly or the "default default" (0 for
  79. // numbers, empty for strings). The field's type indicates the type of the
  80. // returned value, except for enum fields that are still mutable.
  81. //
  82. // Requires that the given function matches the field's current type.
  83. int64_t default_int64() const { return upb_fielddef_defaultint64(ptr_); }
  84. int32_t default_int32() const { return upb_fielddef_defaultint32(ptr_); }
  85. uint64_t default_uint64() const { return upb_fielddef_defaultuint64(ptr_); }
  86. uint32_t default_uint32() const { return upb_fielddef_defaultuint32(ptr_); }
  87. bool default_bool() const { return upb_fielddef_defaultbool(ptr_); }
  88. float default_float() const { return upb_fielddef_defaultfloat(ptr_); }
  89. double default_double() const { return upb_fielddef_defaultdouble(ptr_); }
  90. // The resulting string is always NULL-terminated. If non-NULL, the length
  91. // will be stored in *len.
  92. const char* default_string(size_t* len) const {
  93. return upb_fielddef_defaultstr(ptr_, len);
  94. }
  95. // Returns the enum or submessage def for this field, if any. The field's
  96. // type must match (ie. you may only call enum_subdef() for fields where
  97. // type() == UPB_TYPE_ENUM).
  98. EnumDefPtr enum_subdef() const;
  99. MessageDefPtr message_subdef() const;
  100. private:
  101. const upb_fielddef* ptr_;
  102. };
  103. // Class that represents a oneof.
  104. class OneofDefPtr {
  105. public:
  106. OneofDefPtr() : ptr_(nullptr) {}
  107. explicit OneofDefPtr(const upb_oneofdef* ptr) : ptr_(ptr) {}
  108. const upb_oneofdef* ptr() const { return ptr_; }
  109. explicit operator bool() const { return ptr_ != nullptr; }
  110. // Returns the MessageDef that contains this OneofDef.
  111. MessageDefPtr containing_type() const;
  112. // Returns the name of this oneof.
  113. const char* name() const { return upb_oneofdef_name(ptr_); }
  114. // Returns the number of fields in the oneof.
  115. int field_count() const { return upb_oneofdef_numfields(ptr_); }
  116. FieldDefPtr field(int i) const { return FieldDefPtr(upb_oneofdef_field(ptr_, i)); }
  117. // Looks up by name.
  118. FieldDefPtr FindFieldByName(const char* name, size_t len) const {
  119. return FieldDefPtr(upb_oneofdef_ntof(ptr_, name, len));
  120. }
  121. FieldDefPtr FindFieldByName(const char* name) const {
  122. return FieldDefPtr(upb_oneofdef_ntofz(ptr_, name));
  123. }
  124. template <class T>
  125. FieldDefPtr FindFieldByName(const T& str) const {
  126. return FindFieldByName(str.c_str(), str.size());
  127. }
  128. // Looks up by tag number.
  129. FieldDefPtr FindFieldByNumber(uint32_t num) const {
  130. return FieldDefPtr(upb_oneofdef_itof(ptr_, num));
  131. }
  132. private:
  133. const upb_oneofdef* ptr_;
  134. };
  135. // Structure that describes a single .proto message type.
  136. class MessageDefPtr {
  137. public:
  138. MessageDefPtr() : ptr_(nullptr) {}
  139. explicit MessageDefPtr(const upb_msgdef* ptr) : ptr_(ptr) {}
  140. const upb_msgdef* ptr() const { return ptr_; }
  141. explicit operator bool() const { return ptr_ != nullptr; }
  142. const char* full_name() const { return upb_msgdef_fullname(ptr_); }
  143. const char* name() const { return upb_msgdef_name(ptr_); }
  144. // The number of fields that belong to the MessageDef.
  145. int field_count() const { return upb_msgdef_numfields(ptr_); }
  146. FieldDefPtr field(int i) const { return FieldDefPtr(upb_msgdef_field(ptr_, i)); }
  147. // The number of oneofs that belong to the MessageDef.
  148. int oneof_count() const { return upb_msgdef_numoneofs(ptr_); }
  149. OneofDefPtr oneof(int i) const { return OneofDefPtr(upb_msgdef_oneof(ptr_, i)); }
  150. upb_syntax_t syntax() const { return upb_msgdef_syntax(ptr_); }
  151. // These return null pointers if the field is not found.
  152. FieldDefPtr FindFieldByNumber(uint32_t number) const {
  153. return FieldDefPtr(upb_msgdef_itof(ptr_, number));
  154. }
  155. FieldDefPtr FindFieldByName(const char* name, size_t len) const {
  156. return FieldDefPtr(upb_msgdef_ntof(ptr_, name, len));
  157. }
  158. FieldDefPtr FindFieldByName(const char* name) const {
  159. return FieldDefPtr(upb_msgdef_ntofz(ptr_, name));
  160. }
  161. template <class T>
  162. FieldDefPtr FindFieldByName(const T& str) const {
  163. return FindFieldByName(str.c_str(), str.size());
  164. }
  165. OneofDefPtr FindOneofByName(const char* name, size_t len) const {
  166. return OneofDefPtr(upb_msgdef_ntoo(ptr_, name, len));
  167. }
  168. OneofDefPtr FindOneofByName(const char* name) const {
  169. return OneofDefPtr(upb_msgdef_ntooz(ptr_, name));
  170. }
  171. template <class T>
  172. OneofDefPtr FindOneofByName(const T& str) const {
  173. return FindOneofByName(str.c_str(), str.size());
  174. }
  175. // Is this message a map entry?
  176. bool mapentry() const { return upb_msgdef_mapentry(ptr_); }
  177. // Return the type of well known type message. UPB_WELLKNOWN_UNSPECIFIED for
  178. // non-well-known message.
  179. upb_wellknowntype_t wellknowntype() const {
  180. return upb_msgdef_wellknowntype(ptr_);
  181. }
  182. // Whether is a number wrapper.
  183. bool isnumberwrapper() const { return upb_msgdef_isnumberwrapper(ptr_); }
  184. private:
  185. class FieldIter {
  186. public:
  187. explicit FieldIter(const upb_msgdef *m, int i) : m_(m), i_(i) {}
  188. void operator++() { i_++; }
  189. FieldDefPtr operator*() { return FieldDefPtr(upb_msgdef_field(m_, i_)); }
  190. bool operator!=(const FieldIter& other) { return i_ != other.i_; }
  191. bool operator==(const FieldIter& other) { return i_ == other.i_; }
  192. private:
  193. const upb_msgdef *m_;
  194. int i_;
  195. };
  196. class FieldAccessor {
  197. public:
  198. explicit FieldAccessor(const upb_msgdef* md) : md_(md) {}
  199. FieldIter begin() { return FieldIter(md_, 0); }
  200. FieldIter end() { return FieldIter(md_, upb_msgdef_fieldcount(md_)); }
  201. private:
  202. const upb_msgdef* md_;
  203. };
  204. class OneofIter {
  205. public:
  206. explicit OneofIter(const upb_msgdef *m, int i) : m_(m), i_(i) {}
  207. void operator++() { i_++; }
  208. OneofDefPtr operator*() { return OneofDefPtr(upb_msgdef_oneof(m_, i_)); }
  209. bool operator!=(const OneofIter& other) { return i_ != other.i_; }
  210. bool operator==(const OneofIter& other) { return i_ == other.i_; }
  211. private:
  212. const upb_msgdef *m_;
  213. int i_;
  214. };
  215. class OneofAccessor {
  216. public:
  217. explicit OneofAccessor(const upb_msgdef* md) : md_(md) {}
  218. OneofIter begin() { return OneofIter(md_, 0); }
  219. OneofIter end() { return OneofIter(md_, upb_msgdef_oneofcount(md_)); }
  220. private:
  221. const upb_msgdef* md_;
  222. };
  223. public:
  224. FieldAccessor fields() const { return FieldAccessor(ptr()); }
  225. OneofAccessor oneofs() const { return OneofAccessor(ptr()); }
  226. private:
  227. const upb_msgdef* ptr_;
  228. };
  229. class EnumDefPtr {
  230. public:
  231. EnumDefPtr() : ptr_(nullptr) {}
  232. explicit EnumDefPtr(const upb_enumdef* ptr) : ptr_(ptr) {}
  233. const upb_enumdef* ptr() const { return ptr_; }
  234. explicit operator bool() const { return ptr_ != nullptr; }
  235. const char* full_name() const { return upb_enumdef_fullname(ptr_); }
  236. const char* name() const { return upb_enumdef_name(ptr_); }
  237. // The value that is used as the default when no field default is specified.
  238. // If not set explicitly, the first value that was added will be used.
  239. // The default value must be a member of the enum.
  240. // Requires that value_count() > 0.
  241. int32_t default_value() const { return upb_enumdef_default(ptr_); }
  242. // Returns the number of values currently defined in the enum. Note that
  243. // multiple names can refer to the same number, so this may be greater than
  244. // the total number of unique numbers.
  245. int value_count() const { return upb_enumdef_numvals(ptr_); }
  246. // Lookups from name to integer, returning true if found.
  247. bool FindValueByName(const char* name, int32_t* num) const {
  248. return upb_enumdef_ntoiz(ptr_, name, num);
  249. }
  250. // Finds the name corresponding to the given number, or NULL if none was
  251. // found. If more than one name corresponds to this number, returns the
  252. // first one that was added.
  253. const char* FindValueByNumber(int32_t num) const {
  254. return upb_enumdef_iton(ptr_, num);
  255. }
  256. // Iteration over name/value pairs. The order is undefined.
  257. // Adding an enum val invalidates any iterators.
  258. //
  259. // TODO: make compatible with range-for, with elements as pairs?
  260. class Iterator {
  261. public:
  262. explicit Iterator(EnumDefPtr e) { upb_enum_begin(&iter_, e.ptr()); }
  263. int32_t number() { return upb_enum_iter_number(&iter_); }
  264. const char* name() { return upb_enum_iter_name(&iter_); }
  265. bool Done() { return upb_enum_done(&iter_); }
  266. void Next() { return upb_enum_next(&iter_); }
  267. private:
  268. upb_enum_iter iter_;
  269. };
  270. private:
  271. const upb_enumdef* ptr_;
  272. };
  273. // Class that represents a .proto file with some things defined in it.
  274. //
  275. // Many users won't care about FileDefs, but they are necessary if you want to
  276. // read the values of file-level options.
  277. class FileDefPtr {
  278. public:
  279. explicit FileDefPtr(const upb_filedef* ptr) : ptr_(ptr) {}
  280. const upb_filedef* ptr() const { return ptr_; }
  281. explicit operator bool() const { return ptr_ != nullptr; }
  282. // Get/set name of the file (eg. "foo/bar.proto").
  283. const char* name() const { return upb_filedef_name(ptr_); }
  284. // Package name for definitions inside the file (eg. "foo.bar").
  285. const char* package() const { return upb_filedef_package(ptr_); }
  286. // Sets the php class prefix which is prepended to all php generated classes
  287. // from this .proto. Default is empty.
  288. const char* phpprefix() const { return upb_filedef_phpprefix(ptr_); }
  289. // Use this option to change the namespace of php generated classes. Default
  290. // is empty. When this option is empty, the package name will be used for
  291. // determining the namespace.
  292. const char* phpnamespace() const { return upb_filedef_phpnamespace(ptr_); }
  293. // Syntax for the file. Defaults to proto2.
  294. upb_syntax_t syntax() const { return upb_filedef_syntax(ptr_); }
  295. // Get the list of dependencies from the file. These are returned in the
  296. // order that they were added to the FileDefPtr.
  297. int dependency_count() const { return upb_filedef_depcount(ptr_); }
  298. const FileDefPtr dependency(int index) const {
  299. return FileDefPtr(upb_filedef_dep(ptr_, index));
  300. }
  301. private:
  302. const upb_filedef* ptr_;
  303. };
  304. // Non-const methods in upb::SymbolTable are NOT thread-safe.
  305. class SymbolTable {
  306. public:
  307. SymbolTable() : ptr_(upb_symtab_new(), upb_symtab_free) {}
  308. explicit SymbolTable(upb_symtab* s) : ptr_(s, upb_symtab_free) {}
  309. const upb_symtab* ptr() const { return ptr_.get(); }
  310. upb_symtab* ptr() { return ptr_.get(); }
  311. // Finds an entry in the symbol table with this exact name. If not found,
  312. // returns NULL.
  313. MessageDefPtr LookupMessage(const char* sym) const {
  314. return MessageDefPtr(upb_symtab_lookupmsg(ptr_.get(), sym));
  315. }
  316. EnumDefPtr LookupEnum(const char* sym) const {
  317. return EnumDefPtr(upb_symtab_lookupenum(ptr_.get(), sym));
  318. }
  319. FileDefPtr LookupFile(const char* name) const {
  320. return FileDefPtr(upb_symtab_lookupfile(ptr_.get(), name));
  321. }
  322. // TODO: iteration?
  323. // Adds the given serialized FileDescriptorProto to the pool.
  324. FileDefPtr AddFile(const google_protobuf_FileDescriptorProto* file_proto,
  325. Status* status) {
  326. return FileDefPtr(
  327. upb_symtab_addfile(ptr_.get(), file_proto, status->ptr()));
  328. }
  329. private:
  330. std::unique_ptr<upb_symtab, decltype(&upb_symtab_free)> ptr_;
  331. };
  332. inline MessageDefPtr FieldDefPtr::message_subdef() const {
  333. return MessageDefPtr(upb_fielddef_msgsubdef(ptr_));
  334. }
  335. inline MessageDefPtr FieldDefPtr::containing_type() const {
  336. return MessageDefPtr(upb_fielddef_containingtype(ptr_));
  337. }
  338. inline MessageDefPtr OneofDefPtr::containing_type() const {
  339. return MessageDefPtr(upb_oneofdef_containingtype(ptr_));
  340. }
  341. inline OneofDefPtr FieldDefPtr::containing_oneof() const {
  342. return OneofDefPtr(upb_fielddef_containingoneof(ptr_));
  343. }
  344. inline EnumDefPtr FieldDefPtr::enum_subdef() const {
  345. return EnumDefPtr(upb_fielddef_enumsubdef(ptr_));
  346. }
  347. } // namespace upb
  348. #endif // UPB_DEF_HPP_