You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

def.hpp 16 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. #ifndef UPB_DEF_HPP_
  26. #define UPB_DEF_HPP_
  27. #include <cstring>
  28. #include <memory>
  29. #include <string>
  30. #include <vector>
  31. #include "upb/def.h"
  32. #include "upb/reflection.h"
  33. #include "upb/upb.hpp"
  34. namespace upb {
  35. typedef upb_MessageValue MessageValue;
  36. class EnumDefPtr;
  37. class FileDefPtr;
  38. class MessageDefPtr;
  39. class OneofDefPtr;
  40. // A upb::FieldDefPtr describes a single field in a message. It is most often
  41. // found as a part of a upb_MessageDef, but can also stand alone to represent
  42. // an extension.
  43. class FieldDefPtr {
  44. public:
  45. FieldDefPtr() : ptr_(nullptr) {}
  46. explicit FieldDefPtr(const upb_FieldDef* ptr) : ptr_(ptr) {}
  47. const upb_FieldDef* ptr() const { return ptr_; }
  48. explicit operator bool() const { return ptr_ != nullptr; }
  49. typedef upb_CType Type;
  50. typedef upb_Label Label;
  51. typedef upb_FieldType DescriptorType;
  52. const char* full_name() const { return upb_FieldDef_FullName(ptr_); }
  53. Type type() const { return upb_FieldDef_CType(ptr_); }
  54. Label label() const { return upb_FieldDef_Label(ptr_); }
  55. const char* name() const { return upb_FieldDef_Name(ptr_); }
  56. const char* json_name() const { return upb_FieldDef_JsonName(ptr_); }
  57. uint32_t number() const { return upb_FieldDef_Number(ptr_); }
  58. bool is_extension() const { return upb_FieldDef_IsExtension(ptr_); }
  59. // For non-string, non-submessage fields, this indicates whether binary
  60. // protobufs are encoded in packed or non-packed format.
  61. //
  62. // Note: this accessor reflects the fact that "packed" has different defaults
  63. // depending on whether the proto is proto2 or proto3.
  64. bool packed() const { return upb_FieldDef_IsPacked(ptr_); }
  65. // An integer that can be used as an index into an array of fields for
  66. // whatever message this field belongs to. Guaranteed to be less than
  67. // f->containing_type()->field_count(). May only be accessed once the def has
  68. // been finalized.
  69. uint32_t index() const { return upb_FieldDef_Index(ptr_); }
  70. // The MessageDef to which this field belongs.
  71. //
  72. // If this field has been added to a MessageDef, that message can be retrieved
  73. // directly (this is always the case for frozen FieldDefs).
  74. //
  75. // If the field has not yet been added to a MessageDef, you can set the name
  76. // of the containing type symbolically instead. This is mostly useful for
  77. // extensions, where the extension is declared separately from the message.
  78. MessageDefPtr containing_type() const;
  79. // The OneofDef to which this field belongs, or NULL if this field is not part
  80. // of a oneof.
  81. OneofDefPtr containing_oneof() const;
  82. // The field's type according to the enum in descriptor.proto. This is not
  83. // the same as UPB_TYPE_*, because it distinguishes between (for example)
  84. // INT32 and SINT32, whereas our "type" enum does not. This return of
  85. // descriptor_type() is a function of type(), integer_format(), and
  86. // is_tag_delimited().
  87. DescriptorType descriptor_type() const { return upb_FieldDef_Type(ptr_); }
  88. // Convenient field type tests.
  89. bool IsSubMessage() const { return upb_FieldDef_IsSubMessage(ptr_); }
  90. bool IsString() const { return upb_FieldDef_IsString(ptr_); }
  91. bool IsSequence() const { return upb_FieldDef_IsRepeated(ptr_); }
  92. bool IsPrimitive() const { return upb_FieldDef_IsPrimitive(ptr_); }
  93. bool IsMap() const { return upb_FieldDef_IsMap(ptr_); }
  94. MessageValue default_value() const { return upb_FieldDef_Default(ptr_); }
  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() == kUpb_CType_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_FieldCount(ptr_); }
  116. FieldDefPtr field(int i) const {
  117. return FieldDefPtr(upb_OneofDef_Field(ptr_, i));
  118. }
  119. // Looks up by name.
  120. FieldDefPtr FindFieldByName(const char* name, size_t len) const {
  121. return FieldDefPtr(upb_OneofDef_LookupNameWithSize(ptr_, name, len));
  122. }
  123. FieldDefPtr FindFieldByName(const char* name) const {
  124. return FieldDefPtr(upb_OneofDef_LookupName(ptr_, name));
  125. }
  126. template <class T>
  127. FieldDefPtr FindFieldByName(const T& str) const {
  128. return FindFieldByName(str.c_str(), str.size());
  129. }
  130. // Looks up by tag number.
  131. FieldDefPtr FindFieldByNumber(uint32_t num) const {
  132. return FieldDefPtr(upb_OneofDef_LookupNumber(ptr_, num));
  133. }
  134. private:
  135. const upb_OneofDef* ptr_;
  136. };
  137. // Structure that describes a single .proto message type.
  138. class MessageDefPtr {
  139. public:
  140. MessageDefPtr() : ptr_(nullptr) {}
  141. explicit MessageDefPtr(const upb_MessageDef* ptr) : ptr_(ptr) {}
  142. const upb_MessageDef* ptr() const { return ptr_; }
  143. explicit operator bool() const { return ptr_ != nullptr; }
  144. FileDefPtr file() const;
  145. const char* full_name() const { return upb_MessageDef_FullName(ptr_); }
  146. const char* name() const { return upb_MessageDef_Name(ptr_); }
  147. // The number of fields that belong to the MessageDef.
  148. int field_count() const { return upb_MessageDef_FieldCount(ptr_); }
  149. FieldDefPtr field(int i) const {
  150. return FieldDefPtr(upb_MessageDef_Field(ptr_, i));
  151. }
  152. // The number of oneofs that belong to the MessageDef.
  153. int oneof_count() const { return upb_MessageDef_OneofCount(ptr_); }
  154. OneofDefPtr oneof(int i) const {
  155. return OneofDefPtr(upb_MessageDef_Oneof(ptr_, i));
  156. }
  157. upb_Syntax syntax() const { return upb_MessageDef_Syntax(ptr_); }
  158. // These return null pointers if the field is not found.
  159. FieldDefPtr FindFieldByNumber(uint32_t number) const {
  160. return FieldDefPtr(upb_MessageDef_FindFieldByNumber(ptr_, number));
  161. }
  162. FieldDefPtr FindFieldByName(const char* name, size_t len) const {
  163. return FieldDefPtr(upb_MessageDef_FindFieldByNameWithSize(ptr_, name, len));
  164. }
  165. FieldDefPtr FindFieldByName(const char* name) const {
  166. return FieldDefPtr(upb_MessageDef_FindFieldByName(ptr_, name));
  167. }
  168. template <class T>
  169. FieldDefPtr FindFieldByName(const T& str) const {
  170. return FindFieldByName(str.c_str(), str.size());
  171. }
  172. OneofDefPtr FindOneofByName(const char* name, size_t len) const {
  173. return OneofDefPtr(upb_MessageDef_FindOneofByNameWithSize(ptr_, name, len));
  174. }
  175. OneofDefPtr FindOneofByName(const char* name) const {
  176. return OneofDefPtr(upb_MessageDef_FindOneofByName(ptr_, name));
  177. }
  178. template <class T>
  179. OneofDefPtr FindOneofByName(const T& str) const {
  180. return FindOneofByName(str.c_str(), str.size());
  181. }
  182. // Is this message a map entry?
  183. bool mapentry() const { return upb_MessageDef_IsMapEntry(ptr_); }
  184. // Return the type of well known type message. kUpb_WellKnown_Unspecified for
  185. // non-well-known message.
  186. upb_WellKnown wellknowntype() const {
  187. return upb_MessageDef_WellKnownType(ptr_);
  188. }
  189. private:
  190. class FieldIter {
  191. public:
  192. explicit FieldIter(const upb_MessageDef* m, int i) : m_(m), i_(i) {}
  193. void operator++() { i_++; }
  194. FieldDefPtr operator*() {
  195. return FieldDefPtr(upb_MessageDef_Field(m_, i_));
  196. }
  197. bool operator!=(const FieldIter& other) { return i_ != other.i_; }
  198. bool operator==(const FieldIter& other) { return i_ == other.i_; }
  199. private:
  200. const upb_MessageDef* m_;
  201. int i_;
  202. };
  203. class FieldAccessor {
  204. public:
  205. explicit FieldAccessor(const upb_MessageDef* md) : md_(md) {}
  206. FieldIter begin() { return FieldIter(md_, 0); }
  207. FieldIter end() { return FieldIter(md_, upb_MessageDef_FieldCount(md_)); }
  208. private:
  209. const upb_MessageDef* md_;
  210. };
  211. class OneofIter {
  212. public:
  213. explicit OneofIter(const upb_MessageDef* m, int i) : m_(m), i_(i) {}
  214. void operator++() { i_++; }
  215. OneofDefPtr operator*() {
  216. return OneofDefPtr(upb_MessageDef_Oneof(m_, i_));
  217. }
  218. bool operator!=(const OneofIter& other) { return i_ != other.i_; }
  219. bool operator==(const OneofIter& other) { return i_ == other.i_; }
  220. private:
  221. const upb_MessageDef* m_;
  222. int i_;
  223. };
  224. class OneofAccessor {
  225. public:
  226. explicit OneofAccessor(const upb_MessageDef* md) : md_(md) {}
  227. OneofIter begin() { return OneofIter(md_, 0); }
  228. OneofIter end() { return OneofIter(md_, upb_MessageDef_OneofCount(md_)); }
  229. private:
  230. const upb_MessageDef* md_;
  231. };
  232. public:
  233. FieldAccessor fields() const { return FieldAccessor(ptr()); }
  234. OneofAccessor oneofs() const { return OneofAccessor(ptr()); }
  235. private:
  236. const upb_MessageDef* ptr_;
  237. };
  238. class EnumValDefPtr {
  239. public:
  240. EnumValDefPtr() : ptr_(nullptr) {}
  241. explicit EnumValDefPtr(const upb_EnumValueDef* ptr) : ptr_(ptr) {}
  242. int32_t number() const { return upb_EnumValueDef_Number(ptr_); }
  243. const char* full_name() const { return upb_EnumValueDef_FullName(ptr_); }
  244. const char* name() const { return upb_EnumValueDef_Name(ptr_); }
  245. private:
  246. const upb_EnumValueDef* ptr_;
  247. };
  248. class EnumDefPtr {
  249. public:
  250. EnumDefPtr() : ptr_(nullptr) {}
  251. explicit EnumDefPtr(const upb_EnumDef* ptr) : ptr_(ptr) {}
  252. const upb_EnumDef* ptr() const { return ptr_; }
  253. explicit operator bool() const { return ptr_ != nullptr; }
  254. const char* full_name() const { return upb_EnumDef_FullName(ptr_); }
  255. const char* name() const { return upb_EnumDef_Name(ptr_); }
  256. // The value that is used as the default when no field default is specified.
  257. // If not set explicitly, the first value that was added will be used.
  258. // The default value must be a member of the enum.
  259. // Requires that value_count() > 0.
  260. int32_t default_value() const { return upb_EnumDef_Default(ptr_); }
  261. // Returns the number of values currently defined in the enum. Note that
  262. // multiple names can refer to the same number, so this may be greater than
  263. // the total number of unique numbers.
  264. int value_count() const { return upb_EnumDef_ValueCount(ptr_); }
  265. // Lookups from name to integer, returning true if found.
  266. EnumValDefPtr FindValueByName(const char* name) const {
  267. return EnumValDefPtr(upb_EnumDef_FindValueByName(ptr_, name));
  268. }
  269. // Finds the name corresponding to the given number, or NULL if none was
  270. // found. If more than one name corresponds to this number, returns the
  271. // first one that was added.
  272. EnumValDefPtr FindValueByNumber(int32_t num) const {
  273. return EnumValDefPtr(upb_EnumDef_FindValueByNumber(ptr_, num));
  274. }
  275. private:
  276. const upb_EnumDef* ptr_;
  277. };
  278. // Class that represents a .proto file with some things defined in it.
  279. //
  280. // Many users won't care about FileDefs, but they are necessary if you want to
  281. // read the values of file-level options.
  282. class FileDefPtr {
  283. public:
  284. explicit FileDefPtr(const upb_FileDef* ptr) : ptr_(ptr) {}
  285. const upb_FileDef* ptr() const { return ptr_; }
  286. explicit operator bool() const { return ptr_ != nullptr; }
  287. // Get/set name of the file (eg. "foo/bar.proto").
  288. const char* name() const { return upb_FileDef_Name(ptr_); }
  289. // Package name for definitions inside the file (eg. "foo.bar").
  290. const char* package() const { return upb_FileDef_Package(ptr_); }
  291. // Syntax for the file. Defaults to proto2.
  292. upb_Syntax syntax() const { return upb_FileDef_Syntax(ptr_); }
  293. // Get the list of dependencies from the file. These are returned in the
  294. // order that they were added to the FileDefPtr.
  295. int dependency_count() const { return upb_FileDef_DependencyCount(ptr_); }
  296. const FileDefPtr dependency(int index) const {
  297. return FileDefPtr(upb_FileDef_Dependency(ptr_, index));
  298. }
  299. private:
  300. const upb_FileDef* ptr_;
  301. };
  302. // Non-const methods in upb::DefPool are NOT thread-safe.
  303. class DefPool {
  304. public:
  305. DefPool() : ptr_(upb_DefPool_New(), upb_DefPool_Free) {}
  306. explicit DefPool(upb_DefPool* s) : ptr_(s, upb_DefPool_Free) {}
  307. const upb_DefPool* ptr() const { return ptr_.get(); }
  308. upb_DefPool* ptr() { return ptr_.get(); }
  309. // Finds an entry in the symbol table with this exact name. If not found,
  310. // returns NULL.
  311. MessageDefPtr FindMessageByName(const char* sym) const {
  312. return MessageDefPtr(upb_DefPool_FindMessageByName(ptr_.get(), sym));
  313. }
  314. EnumDefPtr FindEnumByName(const char* sym) const {
  315. return EnumDefPtr(upb_DefPool_FindEnumByName(ptr_.get(), sym));
  316. }
  317. FileDefPtr FindFileByName(const char* name) const {
  318. return FileDefPtr(upb_DefPool_FindFileByName(ptr_.get(), name));
  319. }
  320. // TODO: iteration?
  321. // Adds the given serialized FileDescriptorProto to the pool.
  322. FileDefPtr AddFile(const google_protobuf_FileDescriptorProto* file_proto,
  323. Status* status) {
  324. return FileDefPtr(
  325. upb_DefPool_AddFile(ptr_.get(), file_proto, status->ptr()));
  326. }
  327. private:
  328. std::unique_ptr<upb_DefPool, decltype(&upb_DefPool_Free)> ptr_;
  329. };
  330. // TODO(b/236632406): This typedef is deprecated. Delete it.
  331. using SymbolTable = DefPool;
  332. inline FileDefPtr MessageDefPtr::file() const {
  333. return FileDefPtr(upb_MessageDef_File(ptr_));
  334. }
  335. inline MessageDefPtr FieldDefPtr::message_subdef() const {
  336. return MessageDefPtr(upb_FieldDef_MessageSubDef(ptr_));
  337. }
  338. inline MessageDefPtr FieldDefPtr::containing_type() const {
  339. return MessageDefPtr(upb_FieldDef_ContainingType(ptr_));
  340. }
  341. inline MessageDefPtr OneofDefPtr::containing_type() const {
  342. return MessageDefPtr(upb_OneofDef_ContainingType(ptr_));
  343. }
  344. inline OneofDefPtr FieldDefPtr::containing_oneof() const {
  345. return OneofDefPtr(upb_FieldDef_ContainingOneof(ptr_));
  346. }
  347. inline EnumDefPtr FieldDefPtr::enum_subdef() const {
  348. return EnumDefPtr(upb_FieldDef_EnumSubDef(ptr_));
  349. }
  350. } // namespace upb
  351. #endif // UPB_DEF_HPP_