extension_set.h 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. //
  34. // This header is logically internal, but is made public because it is used
  35. // from protocol-compiler-generated code, which may reside in other components.
  36. #ifndef GOOGLE_PROTOBUF_EXTENSION_SET_H__
  37. #define GOOGLE_PROTOBUF_EXTENSION_SET_H__
  38. #include <algorithm>
  39. #include <cassert>
  40. #include <map>
  41. #include <string>
  42. #include <utility>
  43. #include <vector>
  44. #include <google/protobuf/stubs/common.h>
  45. #include <google/protobuf/stubs/logging.h>
  46. #include <google/protobuf/parse_context.h>
  47. #include <google/protobuf/io/coded_stream.h>
  48. #include <google/protobuf/port.h>
  49. #include <google/protobuf/repeated_field.h>
  50. #include <google/protobuf/wire_format_lite.h>
  51. // clang-format off
  52. #include <google/protobuf/port_def.inc> // Must be last
  53. // clang-format on
  54. #ifdef SWIG
  55. #error "You cannot SWIG proto headers"
  56. #endif
  57. namespace google {
  58. namespace protobuf {
  59. class Arena;
  60. class Descriptor; // descriptor.h
  61. class FieldDescriptor; // descriptor.h
  62. class DescriptorPool; // descriptor.h
  63. class MessageLite; // message_lite.h
  64. class Message; // message.h
  65. class MessageFactory; // message.h
  66. class UnknownFieldSet; // unknown_field_set.h
  67. namespace internal {
  68. class FieldSkipper; // wire_format_lite.h
  69. } // namespace internal
  70. } // namespace protobuf
  71. } // namespace google
  72. namespace google {
  73. namespace protobuf {
  74. namespace internal {
  75. class InternalMetadata;
  76. // Used to store values of type WireFormatLite::FieldType without having to
  77. // #include wire_format_lite.h. Also, ensures that we use only one byte to
  78. // store these values, which is important to keep the layout of
  79. // ExtensionSet::Extension small.
  80. typedef uint8_t FieldType;
  81. // A function which, given an integer value, returns true if the number
  82. // matches one of the defined values for the corresponding enum type. This
  83. // is used with RegisterEnumExtension, below.
  84. typedef bool EnumValidityFunc(int number);
  85. // Version of the above which takes an argument. This is needed to deal with
  86. // extensions that are not compiled in.
  87. typedef bool EnumValidityFuncWithArg(const void* arg, int number);
  88. // Information about a registered extension.
  89. struct ExtensionInfo {
  90. inline ExtensionInfo() {}
  91. inline ExtensionInfo(const MessageLite* extendee, int param_number,
  92. FieldType type_param, bool isrepeated, bool ispacked)
  93. : message(extendee),
  94. number(param_number),
  95. type(type_param),
  96. is_repeated(isrepeated),
  97. is_packed(ispacked),
  98. descriptor(NULL) {}
  99. const MessageLite* message;
  100. int number;
  101. FieldType type;
  102. bool is_repeated;
  103. bool is_packed;
  104. struct EnumValidityCheck {
  105. EnumValidityFuncWithArg* func;
  106. const void* arg;
  107. };
  108. struct MessageInfo {
  109. const MessageLite* prototype;
  110. };
  111. union {
  112. EnumValidityCheck enum_validity_check;
  113. MessageInfo message_info;
  114. };
  115. // The descriptor for this extension, if one exists and is known. May be
  116. // NULL. Must not be NULL if the descriptor for the extension does not
  117. // live in the same pool as the descriptor for the containing type.
  118. const FieldDescriptor* descriptor;
  119. };
  120. // Abstract interface for an object which looks up extension definitions. Used
  121. // when parsing.
  122. class PROTOBUF_EXPORT ExtensionFinder {
  123. public:
  124. virtual ~ExtensionFinder();
  125. // Find the extension with the given containing type and number.
  126. virtual bool Find(int number, ExtensionInfo* output) = 0;
  127. };
  128. // Implementation of ExtensionFinder which finds extensions defined in .proto
  129. // files which have been compiled into the binary.
  130. class PROTOBUF_EXPORT GeneratedExtensionFinder : public ExtensionFinder {
  131. public:
  132. explicit GeneratedExtensionFinder(const MessageLite* extendee)
  133. : extendee_(extendee) {}
  134. ~GeneratedExtensionFinder() override {}
  135. // Returns true and fills in *output if found, otherwise returns false.
  136. bool Find(int number, ExtensionInfo* output) override;
  137. private:
  138. const MessageLite* extendee_;
  139. };
  140. // A FieldSkipper used for parsing MessageSet.
  141. class MessageSetFieldSkipper;
  142. // Note: extension_set_heavy.cc defines DescriptorPoolExtensionFinder for
  143. // finding extensions from a DescriptorPool.
  144. // This is an internal helper class intended for use within the protocol buffer
  145. // library and generated classes. Clients should not use it directly. Instead,
  146. // use the generated accessors such as GetExtension() of the class being
  147. // extended.
  148. //
  149. // This class manages extensions for a protocol message object. The
  150. // message's HasExtension(), GetExtension(), MutableExtension(), and
  151. // ClearExtension() methods are just thin wrappers around the embedded
  152. // ExtensionSet. When parsing, if a tag number is encountered which is
  153. // inside one of the message type's extension ranges, the tag is passed
  154. // off to the ExtensionSet for parsing. Etc.
  155. class PROTOBUF_EXPORT ExtensionSet {
  156. public:
  157. constexpr ExtensionSet();
  158. explicit ExtensionSet(Arena* arena);
  159. ~ExtensionSet();
  160. // These are called at startup by protocol-compiler-generated code to
  161. // register known extensions. The registrations are used by ParseField()
  162. // to look up extensions for parsed field numbers. Note that dynamic parsing
  163. // does not use ParseField(); only protocol-compiler-generated parsing
  164. // methods do.
  165. static void RegisterExtension(const MessageLite* extendee, int number,
  166. FieldType type, bool is_repeated,
  167. bool is_packed);
  168. static void RegisterEnumExtension(const MessageLite* extendee, int number,
  169. FieldType type, bool is_repeated,
  170. bool is_packed, EnumValidityFunc* is_valid);
  171. static void RegisterMessageExtension(const MessageLite* extendee, int number,
  172. FieldType type, bool is_repeated,
  173. bool is_packed,
  174. const MessageLite* prototype);
  175. // =================================================================
  176. // Add all fields which are currently present to the given vector. This
  177. // is useful to implement Reflection::ListFields().
  178. void AppendToList(const Descriptor* extendee, const DescriptorPool* pool,
  179. std::vector<const FieldDescriptor*>* output) const;
  180. // =================================================================
  181. // Accessors
  182. //
  183. // Generated message classes include type-safe templated wrappers around
  184. // these methods. Generally you should use those rather than call these
  185. // directly, unless you are doing low-level memory management.
  186. //
  187. // When calling any of these accessors, the extension number requested
  188. // MUST exist in the DescriptorPool provided to the constructor. Otherwise,
  189. // the method will fail an assert. Normally, though, you would not call
  190. // these directly; you would either call the generated accessors of your
  191. // message class (e.g. GetExtension()) or you would call the accessors
  192. // of the reflection interface. In both cases, it is impossible to
  193. // trigger this assert failure: the generated accessors only accept
  194. // linked-in extension types as parameters, while the Reflection interface
  195. // requires you to provide the FieldDescriptor describing the extension.
  196. //
  197. // When calling any of these accessors, a protocol-compiler-generated
  198. // implementation of the extension corresponding to the number MUST
  199. // be linked in, and the FieldDescriptor used to refer to it MUST be
  200. // the one generated by that linked-in code. Otherwise, the method will
  201. // die on an assert failure. The message objects returned by the message
  202. // accessors are guaranteed to be of the correct linked-in type.
  203. //
  204. // These methods pretty much match Reflection except that:
  205. // - They're not virtual.
  206. // - They identify fields by number rather than FieldDescriptors.
  207. // - They identify enum values using integers rather than descriptors.
  208. // - Strings provide Mutable() in addition to Set() accessors.
  209. bool Has(int number) const;
  210. int ExtensionSize(int number) const; // Size of a repeated extension.
  211. int NumExtensions() const; // The number of extensions
  212. FieldType ExtensionType(int number) const;
  213. void ClearExtension(int number);
  214. // singular fields -------------------------------------------------
  215. int32_t GetInt32(int number, int32_t default_value) const;
  216. int64_t GetInt64(int number, int64_t default_value) const;
  217. uint32_t GetUInt32(int number, uint32_t default_value) const;
  218. uint64_t GetUInt64(int number, uint64_t default_value) const;
  219. float GetFloat(int number, float default_value) const;
  220. double GetDouble(int number, double default_value) const;
  221. bool GetBool(int number, bool default_value) const;
  222. int GetEnum(int number, int default_value) const;
  223. const std::string& GetString(int number,
  224. const std::string& default_value) const;
  225. const MessageLite& GetMessage(int number,
  226. const MessageLite& default_value) const;
  227. const MessageLite& GetMessage(int number, const Descriptor* message_type,
  228. MessageFactory* factory) const;
  229. // |descriptor| may be NULL so long as it is known that the descriptor for
  230. // the extension lives in the same pool as the descriptor for the containing
  231. // type.
  232. #define desc const FieldDescriptor* descriptor // avoid line wrapping
  233. void SetInt32(int number, FieldType type, int32_t value, desc);
  234. void SetInt64(int number, FieldType type, int64_t value, desc);
  235. void SetUInt32(int number, FieldType type, uint32_t value, desc);
  236. void SetUInt64(int number, FieldType type, uint64_t value, desc);
  237. void SetFloat(int number, FieldType type, float value, desc);
  238. void SetDouble(int number, FieldType type, double value, desc);
  239. void SetBool(int number, FieldType type, bool value, desc);
  240. void SetEnum(int number, FieldType type, int value, desc);
  241. void SetString(int number, FieldType type, std::string value, desc);
  242. std::string* MutableString(int number, FieldType type, desc);
  243. MessageLite* MutableMessage(int number, FieldType type,
  244. const MessageLite& prototype, desc);
  245. MessageLite* MutableMessage(const FieldDescriptor* descriptor,
  246. MessageFactory* factory);
  247. // Adds the given message to the ExtensionSet, taking ownership of the
  248. // message object. Existing message with the same number will be deleted.
  249. // If "message" is NULL, this is equivalent to "ClearExtension(number)".
  250. void SetAllocatedMessage(int number, FieldType type,
  251. const FieldDescriptor* descriptor,
  252. MessageLite* message);
  253. void UnsafeArenaSetAllocatedMessage(int number, FieldType type,
  254. const FieldDescriptor* descriptor,
  255. MessageLite* message);
  256. PROTOBUF_MUST_USE_RESULT MessageLite* ReleaseMessage(
  257. int number, const MessageLite& prototype);
  258. MessageLite* UnsafeArenaReleaseMessage(int number,
  259. const MessageLite& prototype);
  260. PROTOBUF_MUST_USE_RESULT MessageLite* ReleaseMessage(
  261. const FieldDescriptor* descriptor, MessageFactory* factory);
  262. MessageLite* UnsafeArenaReleaseMessage(const FieldDescriptor* descriptor,
  263. MessageFactory* factory);
  264. #undef desc
  265. Arena* GetArena() const { return arena_; }
  266. // repeated fields -------------------------------------------------
  267. // Fetches a RepeatedField extension by number; returns |default_value|
  268. // if no such extension exists. User should not touch this directly; it is
  269. // used by the GetRepeatedExtension() method.
  270. const void* GetRawRepeatedField(int number, const void* default_value) const;
  271. // Fetches a mutable version of a RepeatedField extension by number,
  272. // instantiating one if none exists. Similar to above, user should not use
  273. // this directly; it underlies MutableRepeatedExtension().
  274. void* MutableRawRepeatedField(int number, FieldType field_type, bool packed,
  275. const FieldDescriptor* desc);
  276. // This is an overload of MutableRawRepeatedField to maintain compatibility
  277. // with old code using a previous API. This version of
  278. // MutableRawRepeatedField() will GOOGLE_CHECK-fail on a missing extension.
  279. // (E.g.: borg/clients/internal/proto1/proto2_reflection.cc.)
  280. void* MutableRawRepeatedField(int number);
  281. int32_t GetRepeatedInt32(int number, int index) const;
  282. int64_t GetRepeatedInt64(int number, int index) const;
  283. uint32_t GetRepeatedUInt32(int number, int index) const;
  284. uint64_t GetRepeatedUInt64(int number, int index) const;
  285. float GetRepeatedFloat(int number, int index) const;
  286. double GetRepeatedDouble(int number, int index) const;
  287. bool GetRepeatedBool(int number, int index) const;
  288. int GetRepeatedEnum(int number, int index) const;
  289. const std::string& GetRepeatedString(int number, int index) const;
  290. const MessageLite& GetRepeatedMessage(int number, int index) const;
  291. void SetRepeatedInt32(int number, int index, int32_t value);
  292. void SetRepeatedInt64(int number, int index, int64_t value);
  293. void SetRepeatedUInt32(int number, int index, uint32_t value);
  294. void SetRepeatedUInt64(int number, int index, uint64_t value);
  295. void SetRepeatedFloat(int number, int index, float value);
  296. void SetRepeatedDouble(int number, int index, double value);
  297. void SetRepeatedBool(int number, int index, bool value);
  298. void SetRepeatedEnum(int number, int index, int value);
  299. void SetRepeatedString(int number, int index, std::string value);
  300. std::string* MutableRepeatedString(int number, int index);
  301. MessageLite* MutableRepeatedMessage(int number, int index);
  302. #define desc const FieldDescriptor* descriptor // avoid line wrapping
  303. void AddInt32(int number, FieldType type, bool packed, int32_t value, desc);
  304. void AddInt64(int number, FieldType type, bool packed, int64_t value, desc);
  305. void AddUInt32(int number, FieldType type, bool packed, uint32_t value, desc);
  306. void AddUInt64(int number, FieldType type, bool packed, uint64_t value, desc);
  307. void AddFloat(int number, FieldType type, bool packed, float value, desc);
  308. void AddDouble(int number, FieldType type, bool packed, double value, desc);
  309. void AddBool(int number, FieldType type, bool packed, bool value, desc);
  310. void AddEnum(int number, FieldType type, bool packed, int value, desc);
  311. void AddString(int number, FieldType type, std::string value, desc);
  312. std::string* AddString(int number, FieldType type, desc);
  313. MessageLite* AddMessage(int number, FieldType type,
  314. const MessageLite& prototype, desc);
  315. MessageLite* AddMessage(const FieldDescriptor* descriptor,
  316. MessageFactory* factory);
  317. void AddAllocatedMessage(const FieldDescriptor* descriptor,
  318. MessageLite* new_entry);
  319. void UnsafeArenaAddAllocatedMessage(const FieldDescriptor* descriptor,
  320. MessageLite* new_entry);
  321. #undef desc
  322. void RemoveLast(int number);
  323. PROTOBUF_MUST_USE_RESULT MessageLite* ReleaseLast(int number);
  324. MessageLite* UnsafeArenaReleaseLast(int number);
  325. void SwapElements(int number, int index1, int index2);
  326. // -----------------------------------------------------------------
  327. // TODO(kenton): Hardcore memory management accessors
  328. // =================================================================
  329. // convenience methods for implementing methods of Message
  330. //
  331. // These could all be implemented in terms of the other methods of this
  332. // class, but providing them here helps keep the generated code size down.
  333. void Clear();
  334. void MergeFrom(const ExtensionSet& other);
  335. void Swap(ExtensionSet* other);
  336. void InternalSwap(ExtensionSet* other);
  337. void SwapExtension(ExtensionSet* other, int number);
  338. void UnsafeShallowSwapExtension(ExtensionSet* other, int number);
  339. bool IsInitialized() const;
  340. // Parses a single extension from the input. The input should start out
  341. // positioned immediately after the tag.
  342. bool ParseField(uint32_t tag, io::CodedInputStream* input,
  343. ExtensionFinder* extension_finder,
  344. FieldSkipper* field_skipper);
  345. // Specific versions for lite or full messages (constructs the appropriate
  346. // FieldSkipper automatically). |extendee| is the default
  347. // instance for the containing message; it is used only to look up the
  348. // extension by number. See RegisterExtension(), above. Unlike the other
  349. // methods of ExtensionSet, this only works for generated message types --
  350. // it looks up extensions registered using RegisterExtension().
  351. bool ParseField(uint32_t tag, io::CodedInputStream* input,
  352. const MessageLite* extendee);
  353. bool ParseField(uint32_t tag, io::CodedInputStream* input,
  354. const Message* extendee, UnknownFieldSet* unknown_fields);
  355. bool ParseField(uint32_t tag, io::CodedInputStream* input,
  356. const MessageLite* extendee,
  357. io::CodedOutputStream* unknown_fields);
  358. // Lite parser
  359. const char* ParseField(uint64_t tag, const char* ptr,
  360. const MessageLite* extendee,
  361. internal::InternalMetadata* metadata,
  362. internal::ParseContext* ctx);
  363. // Full parser
  364. const char* ParseField(uint64_t tag, const char* ptr, const Message* extendee,
  365. internal::InternalMetadata* metadata,
  366. internal::ParseContext* ctx);
  367. template <typename Msg>
  368. const char* ParseMessageSet(const char* ptr, const Msg* extendee,
  369. InternalMetadata* metadata,
  370. internal::ParseContext* ctx) {
  371. struct MessageSetItem {
  372. const char* _InternalParse(const char* ptr, ParseContext* ctx) {
  373. return me->ParseMessageSetItem(ptr, extendee, metadata, ctx);
  374. }
  375. ExtensionSet* me;
  376. const Msg* extendee;
  377. InternalMetadata* metadata;
  378. } item{this, extendee, metadata};
  379. while (!ctx->Done(&ptr)) {
  380. uint32_t tag;
  381. ptr = ReadTag(ptr, &tag);
  382. GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
  383. if (tag == WireFormatLite::kMessageSetItemStartTag) {
  384. ptr = ctx->ParseGroup(&item, ptr, tag);
  385. GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
  386. } else {
  387. if (tag == 0 || (tag & 7) == 4) {
  388. ctx->SetLastTag(tag);
  389. return ptr;
  390. }
  391. ptr = ParseField(tag, ptr, extendee, metadata, ctx);
  392. GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
  393. }
  394. }
  395. return ptr;
  396. }
  397. // Parse an entire message in MessageSet format. Such messages have no
  398. // fields, only extensions.
  399. bool ParseMessageSetLite(io::CodedInputStream* input,
  400. ExtensionFinder* extension_finder,
  401. FieldSkipper* field_skipper);
  402. bool ParseMessageSet(io::CodedInputStream* input,
  403. ExtensionFinder* extension_finder,
  404. MessageSetFieldSkipper* field_skipper);
  405. // Specific versions for lite or full messages (constructs the appropriate
  406. // FieldSkipper automatically).
  407. bool ParseMessageSet(io::CodedInputStream* input, const MessageLite* extendee,
  408. std::string* unknown_fields);
  409. bool ParseMessageSet(io::CodedInputStream* input, const Message* extendee,
  410. UnknownFieldSet* unknown_fields);
  411. // Write all extension fields with field numbers in the range
  412. // [start_field_number, end_field_number)
  413. // to the output stream, using the cached sizes computed when ByteSize() was
  414. // last called. Note that the range bounds are inclusive-exclusive.
  415. void SerializeWithCachedSizes(const MessageLite* extendee,
  416. int start_field_number, int end_field_number,
  417. io::CodedOutputStream* output) const {
  418. output->SetCur(_InternalSerialize(extendee, start_field_number,
  419. end_field_number, output->Cur(),
  420. output->EpsCopy()));
  421. }
  422. // Same as SerializeWithCachedSizes, but without any bounds checking.
  423. // The caller must ensure that target has sufficient capacity for the
  424. // serialized extensions.
  425. //
  426. // Returns a pointer past the last written byte.
  427. uint8_t* _InternalSerialize(const MessageLite* extendee,
  428. int start_field_number, int end_field_number,
  429. uint8_t* target,
  430. io::EpsCopyOutputStream* stream) const {
  431. if (flat_size_ == 0) {
  432. assert(!is_large());
  433. return target;
  434. }
  435. return _InternalSerializeImpl(extendee, start_field_number,
  436. end_field_number, target, stream);
  437. }
  438. // Like above but serializes in MessageSet format.
  439. void SerializeMessageSetWithCachedSizes(const MessageLite* extendee,
  440. io::CodedOutputStream* output) const {
  441. output->SetCur(InternalSerializeMessageSetWithCachedSizesToArray(
  442. extendee, output->Cur(), output->EpsCopy()));
  443. }
  444. uint8_t* InternalSerializeMessageSetWithCachedSizesToArray(
  445. const MessageLite* extendee, uint8_t* target,
  446. io::EpsCopyOutputStream* stream) const;
  447. // For backward-compatibility, versions of two of the above methods that
  448. // serialize deterministically iff SetDefaultSerializationDeterministic()
  449. // has been called.
  450. uint8_t* SerializeWithCachedSizesToArray(int start_field_number,
  451. int end_field_number,
  452. uint8_t* target) const;
  453. uint8_t* SerializeMessageSetWithCachedSizesToArray(
  454. const MessageLite* extendee, uint8_t* target) const;
  455. // Returns the total serialized size of all the extensions.
  456. size_t ByteSize() const;
  457. // Like ByteSize() but uses MessageSet format.
  458. size_t MessageSetByteSize() const;
  459. // Returns (an estimate of) the total number of bytes used for storing the
  460. // extensions in memory, excluding sizeof(*this). If the ExtensionSet is
  461. // for a lite message (and thus possibly contains lite messages), the results
  462. // are undefined (might work, might crash, might corrupt data, might not even
  463. // be linked in). It's up to the protocol compiler to avoid calling this on
  464. // such ExtensionSets (easy enough since lite messages don't implement
  465. // SpaceUsed()).
  466. size_t SpaceUsedExcludingSelfLong() const;
  467. // This method just calls SpaceUsedExcludingSelfLong() but it can not be
  468. // inlined because the definition of SpaceUsedExcludingSelfLong() is not
  469. // included in lite runtime and when an inline method refers to it MSVC
  470. // will complain about unresolved symbols when building the lite runtime
  471. // as .dll.
  472. int SpaceUsedExcludingSelf() const;
  473. private:
  474. template <typename Type>
  475. friend class PrimitiveTypeTraits;
  476. template <typename Type>
  477. friend class RepeatedPrimitiveTypeTraits;
  478. template <typename Type, bool IsValid(int)>
  479. friend class EnumTypeTraits;
  480. template <typename Type, bool IsValid(int)>
  481. friend class RepeatedEnumTypeTraits;
  482. const int32_t& GetRefInt32(int number, const int32_t& default_value) const;
  483. const int64_t& GetRefInt64(int number, const int64_t& default_value) const;
  484. const uint32_t& GetRefUInt32(int number, const uint32_t& default_value) const;
  485. const uint64_t& GetRefUInt64(int number, const uint64_t& default_value) const;
  486. const float& GetRefFloat(int number, const float& default_value) const;
  487. const double& GetRefDouble(int number, const double& default_value) const;
  488. const bool& GetRefBool(int number, const bool& default_value) const;
  489. const int& GetRefEnum(int number, const int& default_value) const;
  490. const int32_t& GetRefRepeatedInt32(int number, int index) const;
  491. const int64_t& GetRefRepeatedInt64(int number, int index) const;
  492. const uint32_t& GetRefRepeatedUInt32(int number, int index) const;
  493. const uint64_t& GetRefRepeatedUInt64(int number, int index) const;
  494. const float& GetRefRepeatedFloat(int number, int index) const;
  495. const double& GetRefRepeatedDouble(int number, int index) const;
  496. const bool& GetRefRepeatedBool(int number, int index) const;
  497. const int& GetRefRepeatedEnum(int number, int index) const;
  498. // Implementation of _InternalSerialize for non-empty map_.
  499. uint8_t* _InternalSerializeImpl(const MessageLite* extendee,
  500. int start_field_number, int end_field_number,
  501. uint8_t* target,
  502. io::EpsCopyOutputStream* stream) const;
  503. // Interface of a lazily parsed singular message extension.
  504. class PROTOBUF_EXPORT LazyMessageExtension {
  505. public:
  506. LazyMessageExtension() {}
  507. virtual ~LazyMessageExtension() {}
  508. virtual LazyMessageExtension* New(Arena* arena) const = 0;
  509. virtual const MessageLite& GetMessage(
  510. const MessageLite& prototype) const = 0;
  511. virtual MessageLite* MutableMessage(const MessageLite& prototype,
  512. Arena* arena) = 0;
  513. virtual void SetAllocatedMessage(MessageLite* message) = 0;
  514. virtual void UnsafeArenaSetAllocatedMessage(MessageLite* message) = 0;
  515. virtual PROTOBUF_MUST_USE_RESULT MessageLite* ReleaseMessage(
  516. const MessageLite& prototype) = 0;
  517. virtual MessageLite* UnsafeArenaReleaseMessage(
  518. const MessageLite& prototype) = 0;
  519. virtual bool IsInitialized() const = 0;
  520. PROTOBUF_DEPRECATED_MSG("Please use ByteSizeLong() instead")
  521. virtual int ByteSize() const { return internal::ToIntSize(ByteSizeLong()); }
  522. virtual size_t ByteSizeLong() const = 0;
  523. virtual size_t SpaceUsedLong() const = 0;
  524. virtual void MergeFrom(const LazyMessageExtension& other, Arena* arena) = 0;
  525. virtual void MergeFromMessage(const MessageLite& msg, Arena* arena) = 0;
  526. virtual void Clear() = 0;
  527. virtual bool ReadMessage(const MessageLite& prototype,
  528. io::CodedInputStream* input) = 0;
  529. virtual const char* _InternalParse(const char* ptr, ParseContext* ctx) = 0;
  530. virtual uint8_t* WriteMessageToArray(
  531. const MessageLite* prototype, int number, uint8_t* target,
  532. io::EpsCopyOutputStream* stream) const = 0;
  533. private:
  534. virtual void UnusedKeyMethod(); // Dummy key method to avoid weak vtable.
  535. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(LazyMessageExtension);
  536. };
  537. // Give access to function defined below to see LazyMessageExtension.
  538. friend LazyMessageExtension* MaybeCreateLazyExtension(Arena* arena);
  539. struct Extension {
  540. // The order of these fields packs Extension into 24 bytes when using 8
  541. // byte alignment. Consider this when adding or removing fields here.
  542. union {
  543. int32_t int32_t_value;
  544. int64_t int64_t_value;
  545. uint32_t uint32_t_value;
  546. uint64_t uint64_t_value;
  547. float float_value;
  548. double double_value;
  549. bool bool_value;
  550. int enum_value;
  551. std::string* string_value;
  552. MessageLite* message_value;
  553. LazyMessageExtension* lazymessage_value;
  554. RepeatedField<int32_t>* repeated_int32_t_value;
  555. RepeatedField<int64_t>* repeated_int64_t_value;
  556. RepeatedField<uint32_t>* repeated_uint32_t_value;
  557. RepeatedField<uint64_t>* repeated_uint64_t_value;
  558. RepeatedField<float>* repeated_float_value;
  559. RepeatedField<double>* repeated_double_value;
  560. RepeatedField<bool>* repeated_bool_value;
  561. RepeatedField<int>* repeated_enum_value;
  562. RepeatedPtrField<std::string>* repeated_string_value;
  563. RepeatedPtrField<MessageLite>* repeated_message_value;
  564. };
  565. FieldType type;
  566. bool is_repeated;
  567. // For singular types, indicates if the extension is "cleared". This
  568. // happens when an extension is set and then later cleared by the caller.
  569. // We want to keep the Extension object around for reuse, so instead of
  570. // removing it from the map, we just set is_cleared = true. This has no
  571. // meaning for repeated types; for those, the size of the RepeatedField
  572. // simply becomes zero when cleared.
  573. bool is_cleared : 4;
  574. // For singular message types, indicates whether lazy parsing is enabled
  575. // for this extension. This field is only valid when type == TYPE_MESSAGE
  576. // and !is_repeated because we only support lazy parsing for singular
  577. // message types currently. If is_lazy = true, the extension is stored in
  578. // lazymessage_value. Otherwise, the extension will be message_value.
  579. bool is_lazy : 4;
  580. // For repeated types, this indicates if the [packed=true] option is set.
  581. bool is_packed;
  582. // For packed fields, the size of the packed data is recorded here when
  583. // ByteSize() is called then used during serialization.
  584. // TODO(kenton): Use atomic<int> when C++ supports it.
  585. mutable int cached_size;
  586. // The descriptor for this extension, if one exists and is known. May be
  587. // NULL. Must not be NULL if the descriptor for the extension does not
  588. // live in the same pool as the descriptor for the containing type.
  589. const FieldDescriptor* descriptor;
  590. // Some helper methods for operations on a single Extension.
  591. uint8_t* InternalSerializeFieldWithCachedSizesToArray(
  592. const MessageLite* extendee, const ExtensionSet* extension_set,
  593. int number, uint8_t* target, io::EpsCopyOutputStream* stream) const;
  594. uint8_t* InternalSerializeMessageSetItemWithCachedSizesToArray(
  595. const MessageLite* extendee, const ExtensionSet* extension_set,
  596. int number, uint8_t* target, io::EpsCopyOutputStream* stream) const;
  597. size_t ByteSize(int number) const;
  598. size_t MessageSetItemByteSize(int number) const;
  599. void Clear();
  600. int GetSize() const;
  601. void Free();
  602. size_t SpaceUsedExcludingSelfLong() const;
  603. bool IsInitialized() const;
  604. };
  605. // The Extension struct is small enough to be passed by value, so we use it
  606. // directly as the value type in mappings rather than use pointers. We use
  607. // sorted maps rather than hash-maps because we expect most ExtensionSets will
  608. // only contain a small number of extension. Also, we want AppendToList and
  609. // deterministic serialization to order fields by field number.
  610. struct KeyValue {
  611. int first;
  612. Extension second;
  613. struct FirstComparator {
  614. bool operator()(const KeyValue& lhs, const KeyValue& rhs) const {
  615. return lhs.first < rhs.first;
  616. }
  617. bool operator()(const KeyValue& lhs, int key) const {
  618. return lhs.first < key;
  619. }
  620. bool operator()(int key, const KeyValue& rhs) const {
  621. return key < rhs.first;
  622. }
  623. };
  624. };
  625. typedef std::map<int, Extension> LargeMap;
  626. // Wrapper API that switches between flat-map and LargeMap.
  627. // Finds a key (if present) in the ExtensionSet.
  628. const Extension* FindOrNull(int key) const;
  629. Extension* FindOrNull(int key);
  630. // Helper-functions that only inspect the LargeMap.
  631. const Extension* FindOrNullInLargeMap(int key) const;
  632. Extension* FindOrNullInLargeMap(int key);
  633. // Inserts a new (key, Extension) into the ExtensionSet (and returns true), or
  634. // finds the already-existing Extension for that key (returns false).
  635. // The Extension* will point to the new-or-found Extension.
  636. std::pair<Extension*, bool> Insert(int key);
  637. // Grows the flat_capacity_.
  638. // If flat_capacity_ > kMaximumFlatCapacity, converts to LargeMap.
  639. void GrowCapacity(size_t minimum_new_capacity);
  640. static constexpr uint16_t kMaximumFlatCapacity = 256;
  641. bool is_large() const { return static_cast<int16_t>(flat_size_) < 0; }
  642. // Removes a key from the ExtensionSet.
  643. void Erase(int key);
  644. size_t Size() const {
  645. return PROTOBUF_PREDICT_FALSE(is_large()) ? map_.large->size() : flat_size_;
  646. }
  647. // Similar to std::for_each.
  648. // Each Iterator is decomposed into ->first and ->second fields, so
  649. // that the KeyValueFunctor can be agnostic vis-a-vis KeyValue-vs-std::pair.
  650. template <typename Iterator, typename KeyValueFunctor>
  651. static KeyValueFunctor ForEach(Iterator begin, Iterator end,
  652. KeyValueFunctor func) {
  653. for (Iterator it = begin; it != end; ++it) func(it->first, it->second);
  654. return std::move(func);
  655. }
  656. // Applies a functor to the <int, Extension&> pairs in sorted order.
  657. template <typename KeyValueFunctor>
  658. KeyValueFunctor ForEach(KeyValueFunctor func) {
  659. if (PROTOBUF_PREDICT_FALSE(is_large())) {
  660. return ForEach(map_.large->begin(), map_.large->end(), std::move(func));
  661. }
  662. return ForEach(flat_begin(), flat_end(), std::move(func));
  663. }
  664. // Applies a functor to the <int, const Extension&> pairs in sorted order.
  665. template <typename KeyValueFunctor>
  666. KeyValueFunctor ForEach(KeyValueFunctor func) const {
  667. if (PROTOBUF_PREDICT_FALSE(is_large())) {
  668. return ForEach(map_.large->begin(), map_.large->end(), std::move(func));
  669. }
  670. return ForEach(flat_begin(), flat_end(), std::move(func));
  671. }
  672. // Merges existing Extension from other_extension
  673. void InternalExtensionMergeFrom(int number, const Extension& other_extension);
  674. // Returns true and fills field_number and extension if extension is found.
  675. // Note to support packed repeated field compatibility, it also fills whether
  676. // the tag on wire is packed, which can be different from
  677. // extension->is_packed (whether packed=true is specified).
  678. bool FindExtensionInfoFromTag(uint32_t tag, ExtensionFinder* extension_finder,
  679. int* field_number, ExtensionInfo* extension,
  680. bool* was_packed_on_wire);
  681. // Returns true and fills extension if extension is found.
  682. // Note to support packed repeated field compatibility, it also fills whether
  683. // the tag on wire is packed, which can be different from
  684. // extension->is_packed (whether packed=true is specified).
  685. bool FindExtensionInfoFromFieldNumber(int wire_type, int field_number,
  686. ExtensionFinder* extension_finder,
  687. ExtensionInfo* extension,
  688. bool* was_packed_on_wire) const;
  689. // Find the prototype for a LazyMessage from the extension registry. Returns
  690. // null if the extension is not found.
  691. const MessageLite* GetPrototypeForLazyMessage(const MessageLite* extendee,
  692. int number) const;
  693. // Parses a single extension from the input. The input should start out
  694. // positioned immediately after the wire tag. This method is called in
  695. // ParseField() after field number and was_packed_on_wire is extracted from
  696. // the wire tag and ExtensionInfo is found by the field number.
  697. bool ParseFieldWithExtensionInfo(int field_number, bool was_packed_on_wire,
  698. const ExtensionInfo& extension,
  699. io::CodedInputStream* input,
  700. FieldSkipper* field_skipper);
  701. // Like ParseField(), but this method may parse singular message extensions
  702. // lazily depending on the value of FLAGS_eagerly_parse_message_sets.
  703. bool ParseFieldMaybeLazily(int wire_type, int field_number,
  704. io::CodedInputStream* input,
  705. ExtensionFinder* extension_finder,
  706. MessageSetFieldSkipper* field_skipper);
  707. // Gets the extension with the given number, creating it if it does not
  708. // already exist. Returns true if the extension did not already exist.
  709. bool MaybeNewExtension(int number, const FieldDescriptor* descriptor,
  710. Extension** result);
  711. // Gets the repeated extension for the given descriptor, creating it if
  712. // it does not exist.
  713. Extension* MaybeNewRepeatedExtension(const FieldDescriptor* descriptor);
  714. // Parse a single MessageSet item -- called just after the item group start
  715. // tag has been read.
  716. bool ParseMessageSetItemLite(io::CodedInputStream* input,
  717. ExtensionFinder* extension_finder,
  718. FieldSkipper* field_skipper);
  719. // Parse a single MessageSet item -- called just after the item group start
  720. // tag has been read.
  721. bool ParseMessageSetItem(io::CodedInputStream* input,
  722. ExtensionFinder* extension_finder,
  723. MessageSetFieldSkipper* field_skipper);
  724. bool FindExtension(int wire_type, uint32_t field, const MessageLite* extendee,
  725. const internal::ParseContext* /*ctx*/,
  726. ExtensionInfo* extension, bool* was_packed_on_wire) {
  727. GeneratedExtensionFinder finder(extendee);
  728. return FindExtensionInfoFromFieldNumber(wire_type, field, &finder,
  729. extension, was_packed_on_wire);
  730. }
  731. inline bool FindExtension(int wire_type, uint32_t field,
  732. const Message* extendee,
  733. const internal::ParseContext* ctx,
  734. ExtensionInfo* extension, bool* was_packed_on_wire);
  735. // Used for MessageSet only
  736. const char* ParseFieldMaybeLazily(uint64_t tag, const char* ptr,
  737. const MessageLite* extendee,
  738. internal::InternalMetadata* metadata,
  739. internal::ParseContext* ctx) {
  740. // Lite MessageSet doesn't implement lazy.
  741. return ParseField(tag, ptr, extendee, metadata, ctx);
  742. }
  743. const char* ParseFieldMaybeLazily(uint64_t tag, const char* ptr,
  744. const Message* extendee,
  745. internal::InternalMetadata* metadata,
  746. internal::ParseContext* ctx);
  747. const char* ParseMessageSetItem(const char* ptr, const MessageLite* extendee,
  748. internal::InternalMetadata* metadata,
  749. internal::ParseContext* ctx);
  750. const char* ParseMessageSetItem(const char* ptr, const Message* extendee,
  751. internal::InternalMetadata* metadata,
  752. internal::ParseContext* ctx);
  753. // Implemented in extension_set_inl.h to keep code out of the header file.
  754. template <typename T>
  755. const char* ParseFieldWithExtensionInfo(int number, bool was_packed_on_wire,
  756. const ExtensionInfo& info,
  757. internal::InternalMetadata* metadata,
  758. const char* ptr,
  759. internal::ParseContext* ctx);
  760. template <typename Msg, typename T>
  761. const char* ParseMessageSetItemTmpl(const char* ptr, const Msg* extendee,
  762. internal::InternalMetadata* metadata,
  763. internal::ParseContext* ctx);
  764. // Hack: RepeatedPtrFieldBase declares ExtensionSet as a friend. This
  765. // friendship should automatically extend to ExtensionSet::Extension, but
  766. // unfortunately some older compilers (e.g. GCC 3.4.4) do not implement this
  767. // correctly. So, we must provide helpers for calling methods of that
  768. // class.
  769. // Defined in extension_set_heavy.cc.
  770. static inline size_t RepeatedMessage_SpaceUsedExcludingSelfLong(
  771. RepeatedPtrFieldBase* field);
  772. KeyValue* flat_begin() {
  773. assert(!is_large());
  774. return map_.flat;
  775. }
  776. const KeyValue* flat_begin() const {
  777. assert(!is_large());
  778. return map_.flat;
  779. }
  780. KeyValue* flat_end() {
  781. assert(!is_large());
  782. return map_.flat + flat_size_;
  783. }
  784. const KeyValue* flat_end() const {
  785. assert(!is_large());
  786. return map_.flat + flat_size_;
  787. }
  788. Arena* arena_;
  789. // Manual memory-management:
  790. // map_.flat is an allocated array of flat_capacity_ elements.
  791. // [map_.flat, map_.flat + flat_size_) is the currently-in-use prefix.
  792. uint16_t flat_capacity_;
  793. uint16_t flat_size_; // negative int16_t(flat_size_) indicates is_large()
  794. union AllocatedData {
  795. KeyValue* flat;
  796. // If flat_capacity_ > kMaximumFlatCapacity, switch to LargeMap,
  797. // which guarantees O(n lg n) CPU but larger constant factors.
  798. LargeMap* large;
  799. } map_;
  800. static void DeleteFlatMap(const KeyValue* flat, uint16_t flat_capacity);
  801. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ExtensionSet);
  802. };
  803. constexpr ExtensionSet::ExtensionSet()
  804. : arena_(nullptr), flat_capacity_(0), flat_size_(0), map_{nullptr} {}
  805. // These are just for convenience...
  806. inline void ExtensionSet::SetString(int number, FieldType type,
  807. std::string value,
  808. const FieldDescriptor* descriptor) {
  809. MutableString(number, type, descriptor)->assign(std::move(value));
  810. }
  811. inline void ExtensionSet::SetRepeatedString(int number, int index,
  812. std::string value) {
  813. MutableRepeatedString(number, index)->assign(std::move(value));
  814. }
  815. inline void ExtensionSet::AddString(int number, FieldType type,
  816. std::string value,
  817. const FieldDescriptor* descriptor) {
  818. AddString(number, type, descriptor)->assign(std::move(value));
  819. }
  820. // ===================================================================
  821. // Glue for generated extension accessors
  822. // -------------------------------------------------------------------
  823. // Template magic
  824. // First we have a set of classes representing "type traits" for different
  825. // field types. A type traits class knows how to implement basic accessors
  826. // for extensions of a particular type given an ExtensionSet. The signature
  827. // for a type traits class looks like this:
  828. //
  829. // class TypeTraits {
  830. // public:
  831. // typedef ? ConstType;
  832. // typedef ? MutableType;
  833. // // TypeTraits for singular fields and repeated fields will define the
  834. // // symbol "Singular" or "Repeated" respectively. These two symbols will
  835. // // be used in extension accessors to distinguish between singular
  836. // // extensions and repeated extensions. If the TypeTraits for the passed
  837. // // in extension doesn't have the expected symbol defined, it means the
  838. // // user is passing a repeated extension to a singular accessor, or the
  839. // // opposite. In that case the C++ compiler will generate an error
  840. // // message "no matching member function" to inform the user.
  841. // typedef ? Singular
  842. // typedef ? Repeated
  843. //
  844. // static inline ConstType Get(int number, const ExtensionSet& set);
  845. // static inline void Set(int number, ConstType value, ExtensionSet* set);
  846. // static inline MutableType Mutable(int number, ExtensionSet* set);
  847. //
  848. // // Variants for repeated fields.
  849. // static inline ConstType Get(int number, const ExtensionSet& set,
  850. // int index);
  851. // static inline void Set(int number, int index,
  852. // ConstType value, ExtensionSet* set);
  853. // static inline MutableType Mutable(int number, int index,
  854. // ExtensionSet* set);
  855. // static inline void Add(int number, ConstType value, ExtensionSet* set);
  856. // static inline MutableType Add(int number, ExtensionSet* set);
  857. // This is used by the ExtensionIdentifier constructor to register
  858. // the extension at dynamic initialization.
  859. // template <typename ExtendeeT>
  860. // static void Register(int number, FieldType type, bool is_packed);
  861. // };
  862. //
  863. // Not all of these methods make sense for all field types. For example, the
  864. // "Mutable" methods only make sense for strings and messages, and the
  865. // repeated methods only make sense for repeated types. So, each type
  866. // traits class implements only the set of methods from this signature that it
  867. // actually supports. This will cause a compiler error if the user tries to
  868. // access an extension using a method that doesn't make sense for its type.
  869. // For example, if "foo" is an extension of type "optional int32", then if you
  870. // try to write code like:
  871. // my_message.MutableExtension(foo)
  872. // you will get a compile error because PrimitiveTypeTraits<int32_t> does not
  873. // have a "Mutable()" method.
  874. // -------------------------------------------------------------------
  875. // PrimitiveTypeTraits
  876. // Since the ExtensionSet has different methods for each primitive type,
  877. // we must explicitly define the methods of the type traits class for each
  878. // known type.
  879. template <typename Type>
  880. class PrimitiveTypeTraits {
  881. public:
  882. typedef Type ConstType;
  883. typedef Type MutableType;
  884. typedef PrimitiveTypeTraits<Type> Singular;
  885. static inline ConstType Get(int number, const ExtensionSet& set,
  886. ConstType default_value);
  887. static inline const ConstType* GetPtr(int number, const ExtensionSet& set,
  888. const ConstType& default_value);
  889. static inline void Set(int number, FieldType field_type, ConstType value,
  890. ExtensionSet* set);
  891. template <typename ExtendeeT>
  892. static void Register(int number, FieldType type, bool is_packed) {
  893. ExtensionSet::RegisterExtension(&ExtendeeT::default_instance(), number,
  894. type, false, is_packed);
  895. }
  896. };
  897. template <typename Type>
  898. class RepeatedPrimitiveTypeTraits {
  899. public:
  900. typedef Type ConstType;
  901. typedef Type MutableType;
  902. typedef RepeatedPrimitiveTypeTraits<Type> Repeated;
  903. typedef RepeatedField<Type> RepeatedFieldType;
  904. static inline Type Get(int number, const ExtensionSet& set, int index);
  905. static inline const Type* GetPtr(int number, const ExtensionSet& set,
  906. int index);
  907. static inline const RepeatedField<ConstType>* GetRepeatedPtr(
  908. int number, const ExtensionSet& set);
  909. static inline void Set(int number, int index, Type value, ExtensionSet* set);
  910. static inline void Add(int number, FieldType field_type, bool is_packed,
  911. Type value, ExtensionSet* set);
  912. static inline const RepeatedField<ConstType>& GetRepeated(
  913. int number, const ExtensionSet& set);
  914. static inline RepeatedField<Type>* MutableRepeated(int number,
  915. FieldType field_type,
  916. bool is_packed,
  917. ExtensionSet* set);
  918. static const RepeatedFieldType* GetDefaultRepeatedField();
  919. template <typename ExtendeeT>
  920. static void Register(int number, FieldType type, bool is_packed) {
  921. ExtensionSet::RegisterExtension(&ExtendeeT::default_instance(), number,
  922. type, true, is_packed);
  923. }
  924. };
  925. class PROTOBUF_EXPORT RepeatedPrimitiveDefaults {
  926. private:
  927. template <typename Type>
  928. friend class RepeatedPrimitiveTypeTraits;
  929. static const RepeatedPrimitiveDefaults* default_instance();
  930. RepeatedField<int32_t> default_repeated_field_int32_t_;
  931. RepeatedField<int64_t> default_repeated_field_int64_t_;
  932. RepeatedField<uint32_t> default_repeated_field_uint32_t_;
  933. RepeatedField<uint64_t> default_repeated_field_uint64_t_;
  934. RepeatedField<double> default_repeated_field_double_;
  935. RepeatedField<float> default_repeated_field_float_;
  936. RepeatedField<bool> default_repeated_field_bool_;
  937. };
  938. #define PROTOBUF_DEFINE_PRIMITIVE_TYPE(TYPE, METHOD) \
  939. template <> \
  940. inline TYPE PrimitiveTypeTraits<TYPE>::Get( \
  941. int number, const ExtensionSet& set, TYPE default_value) { \
  942. return set.Get##METHOD(number, default_value); \
  943. } \
  944. template <> \
  945. inline const TYPE* PrimitiveTypeTraits<TYPE>::GetPtr( \
  946. int number, const ExtensionSet& set, const TYPE& default_value) { \
  947. return &set.GetRef##METHOD(number, default_value); \
  948. } \
  949. template <> \
  950. inline void PrimitiveTypeTraits<TYPE>::Set(int number, FieldType field_type, \
  951. TYPE value, ExtensionSet* set) { \
  952. set->Set##METHOD(number, field_type, value, NULL); \
  953. } \
  954. \
  955. template <> \
  956. inline TYPE RepeatedPrimitiveTypeTraits<TYPE>::Get( \
  957. int number, const ExtensionSet& set, int index) { \
  958. return set.GetRepeated##METHOD(number, index); \
  959. } \
  960. template <> \
  961. inline const TYPE* RepeatedPrimitiveTypeTraits<TYPE>::GetPtr( \
  962. int number, const ExtensionSet& set, int index) { \
  963. return &set.GetRefRepeated##METHOD(number, index); \
  964. } \
  965. template <> \
  966. inline void RepeatedPrimitiveTypeTraits<TYPE>::Set( \
  967. int number, int index, TYPE value, ExtensionSet* set) { \
  968. set->SetRepeated##METHOD(number, index, value); \
  969. } \
  970. template <> \
  971. inline void RepeatedPrimitiveTypeTraits<TYPE>::Add( \
  972. int number, FieldType field_type, bool is_packed, TYPE value, \
  973. ExtensionSet* set) { \
  974. set->Add##METHOD(number, field_type, is_packed, value, NULL); \
  975. } \
  976. template <> \
  977. inline const RepeatedField<TYPE>* \
  978. RepeatedPrimitiveTypeTraits<TYPE>::GetDefaultRepeatedField() { \
  979. return &RepeatedPrimitiveDefaults::default_instance() \
  980. ->default_repeated_field_##TYPE##_; \
  981. } \
  982. template <> \
  983. inline const RepeatedField<TYPE>& \
  984. RepeatedPrimitiveTypeTraits<TYPE>::GetRepeated(int number, \
  985. const ExtensionSet& set) { \
  986. return *reinterpret_cast<const RepeatedField<TYPE>*>( \
  987. set.GetRawRepeatedField(number, GetDefaultRepeatedField())); \
  988. } \
  989. template <> \
  990. inline const RepeatedField<TYPE>* \
  991. RepeatedPrimitiveTypeTraits<TYPE>::GetRepeatedPtr(int number, \
  992. const ExtensionSet& set) { \
  993. return &GetRepeated(number, set); \
  994. } \
  995. template <> \
  996. inline RepeatedField<TYPE>* \
  997. RepeatedPrimitiveTypeTraits<TYPE>::MutableRepeated( \
  998. int number, FieldType field_type, bool is_packed, ExtensionSet* set) { \
  999. return reinterpret_cast<RepeatedField<TYPE>*>( \
  1000. set->MutableRawRepeatedField(number, field_type, is_packed, NULL)); \
  1001. }
  1002. PROTOBUF_DEFINE_PRIMITIVE_TYPE(int32_t, Int32)
  1003. PROTOBUF_DEFINE_PRIMITIVE_TYPE(int64_t, Int64)
  1004. PROTOBUF_DEFINE_PRIMITIVE_TYPE(uint32_t, UInt32)
  1005. PROTOBUF_DEFINE_PRIMITIVE_TYPE(uint64_t, UInt64)
  1006. PROTOBUF_DEFINE_PRIMITIVE_TYPE(float, Float)
  1007. PROTOBUF_DEFINE_PRIMITIVE_TYPE(double, Double)
  1008. PROTOBUF_DEFINE_PRIMITIVE_TYPE(bool, Bool)
  1009. #undef PROTOBUF_DEFINE_PRIMITIVE_TYPE
  1010. // -------------------------------------------------------------------
  1011. // StringTypeTraits
  1012. // Strings support both Set() and Mutable().
  1013. class PROTOBUF_EXPORT StringTypeTraits {
  1014. public:
  1015. typedef const std::string& ConstType;
  1016. typedef std::string* MutableType;
  1017. typedef StringTypeTraits Singular;
  1018. static inline const std::string& Get(int number, const ExtensionSet& set,
  1019. ConstType default_value) {
  1020. return set.GetString(number, default_value);
  1021. }
  1022. static inline const std::string* GetPtr(int number, const ExtensionSet& set,
  1023. ConstType default_value) {
  1024. return &Get(number, set, default_value);
  1025. }
  1026. static inline void Set(int number, FieldType field_type,
  1027. const std::string& value, ExtensionSet* set) {
  1028. set->SetString(number, field_type, value, NULL);
  1029. }
  1030. static inline std::string* Mutable(int number, FieldType field_type,
  1031. ExtensionSet* set) {
  1032. return set->MutableString(number, field_type, NULL);
  1033. }
  1034. template <typename ExtendeeT>
  1035. static void Register(int number, FieldType type, bool is_packed) {
  1036. ExtensionSet::RegisterExtension(&ExtendeeT::default_instance(), number,
  1037. type, false, is_packed);
  1038. }
  1039. };
  1040. class PROTOBUF_EXPORT RepeatedStringTypeTraits {
  1041. public:
  1042. typedef const std::string& ConstType;
  1043. typedef std::string* MutableType;
  1044. typedef RepeatedStringTypeTraits Repeated;
  1045. typedef RepeatedPtrField<std::string> RepeatedFieldType;
  1046. static inline const std::string& Get(int number, const ExtensionSet& set,
  1047. int index) {
  1048. return set.GetRepeatedString(number, index);
  1049. }
  1050. static inline const std::string* GetPtr(int number, const ExtensionSet& set,
  1051. int index) {
  1052. return &Get(number, set, index);
  1053. }
  1054. static inline const RepeatedPtrField<std::string>* GetRepeatedPtr(
  1055. int number, const ExtensionSet& set) {
  1056. return &GetRepeated(number, set);
  1057. }
  1058. static inline void Set(int number, int index, const std::string& value,
  1059. ExtensionSet* set) {
  1060. set->SetRepeatedString(number, index, value);
  1061. }
  1062. static inline std::string* Mutable(int number, int index, ExtensionSet* set) {
  1063. return set->MutableRepeatedString(number, index);
  1064. }
  1065. static inline void Add(int number, FieldType field_type, bool /*is_packed*/,
  1066. const std::string& value, ExtensionSet* set) {
  1067. set->AddString(number, field_type, value, NULL);
  1068. }
  1069. static inline std::string* Add(int number, FieldType field_type,
  1070. ExtensionSet* set) {
  1071. return set->AddString(number, field_type, NULL);
  1072. }
  1073. static inline const RepeatedPtrField<std::string>& GetRepeated(
  1074. int number, const ExtensionSet& set) {
  1075. return *reinterpret_cast<const RepeatedPtrField<std::string>*>(
  1076. set.GetRawRepeatedField(number, GetDefaultRepeatedField()));
  1077. }
  1078. static inline RepeatedPtrField<std::string>* MutableRepeated(
  1079. int number, FieldType field_type, bool is_packed, ExtensionSet* set) {
  1080. return reinterpret_cast<RepeatedPtrField<std::string>*>(
  1081. set->MutableRawRepeatedField(number, field_type, is_packed, NULL));
  1082. }
  1083. static const RepeatedFieldType* GetDefaultRepeatedField();
  1084. template <typename ExtendeeT>
  1085. static void Register(int number, FieldType type, bool is_packed) {
  1086. ExtensionSet::RegisterExtension(&ExtendeeT::default_instance(), number,
  1087. type, true, is_packed);
  1088. }
  1089. private:
  1090. static void InitializeDefaultRepeatedFields();
  1091. static void DestroyDefaultRepeatedFields();
  1092. };
  1093. // -------------------------------------------------------------------
  1094. // EnumTypeTraits
  1095. // ExtensionSet represents enums using integers internally, so we have to
  1096. // static_cast around.
  1097. template <typename Type, bool IsValid(int)>
  1098. class EnumTypeTraits {
  1099. public:
  1100. typedef Type ConstType;
  1101. typedef Type MutableType;
  1102. typedef EnumTypeTraits<Type, IsValid> Singular;
  1103. static inline ConstType Get(int number, const ExtensionSet& set,
  1104. ConstType default_value) {
  1105. return static_cast<Type>(set.GetEnum(number, default_value));
  1106. }
  1107. static inline const ConstType* GetPtr(int number, const ExtensionSet& set,
  1108. const ConstType& default_value) {
  1109. return reinterpret_cast<const Type*>(
  1110. &set.GetRefEnum(number, default_value));
  1111. }
  1112. static inline void Set(int number, FieldType field_type, ConstType value,
  1113. ExtensionSet* set) {
  1114. GOOGLE_DCHECK(IsValid(value));
  1115. set->SetEnum(number, field_type, value, NULL);
  1116. }
  1117. template <typename ExtendeeT>
  1118. static void Register(int number, FieldType type, bool is_packed) {
  1119. ExtensionSet::RegisterEnumExtension(&ExtendeeT::default_instance(), number,
  1120. type, false, is_packed, IsValid);
  1121. }
  1122. };
  1123. template <typename Type, bool IsValid(int)>
  1124. class RepeatedEnumTypeTraits {
  1125. public:
  1126. typedef Type ConstType;
  1127. typedef Type MutableType;
  1128. typedef RepeatedEnumTypeTraits<Type, IsValid> Repeated;
  1129. typedef RepeatedField<Type> RepeatedFieldType;
  1130. static inline ConstType Get(int number, const ExtensionSet& set, int index) {
  1131. return static_cast<Type>(set.GetRepeatedEnum(number, index));
  1132. }
  1133. static inline const ConstType* GetPtr(int number, const ExtensionSet& set,
  1134. int index) {
  1135. return reinterpret_cast<const Type*>(
  1136. &set.GetRefRepeatedEnum(number, index));
  1137. }
  1138. static inline void Set(int number, int index, ConstType value,
  1139. ExtensionSet* set) {
  1140. GOOGLE_DCHECK(IsValid(value));
  1141. set->SetRepeatedEnum(number, index, value);
  1142. }
  1143. static inline void Add(int number, FieldType field_type, bool is_packed,
  1144. ConstType value, ExtensionSet* set) {
  1145. GOOGLE_DCHECK(IsValid(value));
  1146. set->AddEnum(number, field_type, is_packed, value, NULL);
  1147. }
  1148. static inline const RepeatedField<Type>& GetRepeated(
  1149. int number, const ExtensionSet& set) {
  1150. // Hack: the `Extension` struct stores a RepeatedField<int> for enums.
  1151. // RepeatedField<int> cannot implicitly convert to RepeatedField<EnumType>
  1152. // so we need to do some casting magic. See message.h for similar
  1153. // contortions for non-extension fields.
  1154. return *reinterpret_cast<const RepeatedField<Type>*>(
  1155. set.GetRawRepeatedField(number, GetDefaultRepeatedField()));
  1156. }
  1157. static inline const RepeatedField<Type>* GetRepeatedPtr(
  1158. int number, const ExtensionSet& set) {
  1159. return &GetRepeated(number, set);
  1160. }
  1161. static inline RepeatedField<Type>* MutableRepeated(int number,
  1162. FieldType field_type,
  1163. bool is_packed,
  1164. ExtensionSet* set) {
  1165. return reinterpret_cast<RepeatedField<Type>*>(
  1166. set->MutableRawRepeatedField(number, field_type, is_packed, NULL));
  1167. }
  1168. static const RepeatedFieldType* GetDefaultRepeatedField() {
  1169. // Hack: as noted above, repeated enum fields are internally stored as a
  1170. // RepeatedField<int>. We need to be able to instantiate global static
  1171. // objects to return as default (empty) repeated fields on non-existent
  1172. // extensions. We would not be able to know a-priori all of the enum types
  1173. // (values of |Type|) to instantiate all of these, so we just re-use
  1174. // int32_t's default repeated field object.
  1175. return reinterpret_cast<const RepeatedField<Type>*>(
  1176. RepeatedPrimitiveTypeTraits<int32_t>::GetDefaultRepeatedField());
  1177. }
  1178. template <typename ExtendeeT>
  1179. static void Register(int number, FieldType type, bool is_packed) {
  1180. ExtensionSet::RegisterEnumExtension(&ExtendeeT::default_instance(), number,
  1181. type, true, is_packed, IsValid);
  1182. }
  1183. };
  1184. // -------------------------------------------------------------------
  1185. // MessageTypeTraits
  1186. // ExtensionSet guarantees that when manipulating extensions with message
  1187. // types, the implementation used will be the compiled-in class representing
  1188. // that type. So, we can static_cast down to the exact type we expect.
  1189. template <typename Type>
  1190. class MessageTypeTraits {
  1191. public:
  1192. typedef const Type& ConstType;
  1193. typedef Type* MutableType;
  1194. typedef MessageTypeTraits<Type> Singular;
  1195. static inline ConstType Get(int number, const ExtensionSet& set,
  1196. ConstType default_value) {
  1197. return static_cast<const Type&>(set.GetMessage(number, default_value));
  1198. }
  1199. static inline std::nullptr_t GetPtr(int number, const ExtensionSet& set,
  1200. ConstType default_value) {
  1201. // Cannot be implemented because of forward declared messages?
  1202. return nullptr;
  1203. }
  1204. static inline MutableType Mutable(int number, FieldType field_type,
  1205. ExtensionSet* set) {
  1206. return static_cast<Type*>(set->MutableMessage(
  1207. number, field_type, Type::default_instance(), NULL));
  1208. }
  1209. static inline void SetAllocated(int number, FieldType field_type,
  1210. MutableType message, ExtensionSet* set) {
  1211. set->SetAllocatedMessage(number, field_type, NULL, message);
  1212. }
  1213. static inline void UnsafeArenaSetAllocated(int number, FieldType field_type,
  1214. MutableType message,
  1215. ExtensionSet* set) {
  1216. set->UnsafeArenaSetAllocatedMessage(number, field_type, NULL, message);
  1217. }
  1218. static inline PROTOBUF_MUST_USE_RESULT MutableType
  1219. Release(int number, FieldType /* field_type */, ExtensionSet* set) {
  1220. return static_cast<Type*>(
  1221. set->ReleaseMessage(number, Type::default_instance()));
  1222. }
  1223. static inline MutableType UnsafeArenaRelease(int number,
  1224. FieldType /* field_type */,
  1225. ExtensionSet* set) {
  1226. return static_cast<Type*>(
  1227. set->UnsafeArenaReleaseMessage(number, Type::default_instance()));
  1228. }
  1229. template <typename ExtendeeT>
  1230. static void Register(int number, FieldType type, bool is_packed) {
  1231. ExtensionSet::RegisterMessageExtension(&ExtendeeT::default_instance(),
  1232. number, type, false, is_packed,
  1233. &Type::default_instance());
  1234. }
  1235. };
  1236. // forward declaration.
  1237. class RepeatedMessageGenericTypeTraits;
  1238. template <typename Type>
  1239. class RepeatedMessageTypeTraits {
  1240. public:
  1241. typedef const Type& ConstType;
  1242. typedef Type* MutableType;
  1243. typedef RepeatedMessageTypeTraits<Type> Repeated;
  1244. typedef RepeatedPtrField<Type> RepeatedFieldType;
  1245. static inline ConstType Get(int number, const ExtensionSet& set, int index) {
  1246. return static_cast<const Type&>(set.GetRepeatedMessage(number, index));
  1247. }
  1248. static inline std::nullptr_t GetPtr(int number, const ExtensionSet& set,
  1249. int index) {
  1250. // Cannot be implemented because of forward declared messages?
  1251. return nullptr;
  1252. }
  1253. static inline std::nullptr_t GetRepeatedPtr(int number,
  1254. const ExtensionSet& set) {
  1255. // Cannot be implemented because of forward declared messages?
  1256. return nullptr;
  1257. }
  1258. static inline MutableType Mutable(int number, int index, ExtensionSet* set) {
  1259. return static_cast<Type*>(set->MutableRepeatedMessage(number, index));
  1260. }
  1261. static inline MutableType Add(int number, FieldType field_type,
  1262. ExtensionSet* set) {
  1263. return static_cast<Type*>(
  1264. set->AddMessage(number, field_type, Type::default_instance(), NULL));
  1265. }
  1266. static inline const RepeatedPtrField<Type>& GetRepeated(
  1267. int number, const ExtensionSet& set) {
  1268. // See notes above in RepeatedEnumTypeTraits::GetRepeated(): same
  1269. // casting hack applies here, because a RepeatedPtrField<MessageLite>
  1270. // cannot naturally become a RepeatedPtrType<Type> even though Type is
  1271. // presumably a message. google::protobuf::Message goes through similar contortions
  1272. // with a reinterpret_cast<>.
  1273. return *reinterpret_cast<const RepeatedPtrField<Type>*>(
  1274. set.GetRawRepeatedField(number, GetDefaultRepeatedField()));
  1275. }
  1276. static inline RepeatedPtrField<Type>* MutableRepeated(int number,
  1277. FieldType field_type,
  1278. bool is_packed,
  1279. ExtensionSet* set) {
  1280. return reinterpret_cast<RepeatedPtrField<Type>*>(
  1281. set->MutableRawRepeatedField(number, field_type, is_packed, NULL));
  1282. }
  1283. static const RepeatedFieldType* GetDefaultRepeatedField();
  1284. template <typename ExtendeeT>
  1285. static void Register(int number, FieldType type, bool is_packed) {
  1286. ExtensionSet::RegisterMessageExtension(&ExtendeeT::default_instance(),
  1287. number, type, true, is_packed,
  1288. &Type::default_instance());
  1289. }
  1290. };
  1291. template <typename Type>
  1292. inline const typename RepeatedMessageTypeTraits<Type>::RepeatedFieldType*
  1293. RepeatedMessageTypeTraits<Type>::GetDefaultRepeatedField() {
  1294. static auto instance = OnShutdownDelete(new RepeatedFieldType);
  1295. return instance;
  1296. }
  1297. // -------------------------------------------------------------------
  1298. // ExtensionIdentifier
  1299. // This is the type of actual extension objects. E.g. if you have:
  1300. // extend Foo {
  1301. // optional int32 bar = 1234;
  1302. // }
  1303. // then "bar" will be defined in C++ as:
  1304. // ExtensionIdentifier<Foo, PrimitiveTypeTraits<int32_t>, 5, false> bar(1234);
  1305. //
  1306. // Note that we could, in theory, supply the field number as a template
  1307. // parameter, and thus make an instance of ExtensionIdentifier have no
  1308. // actual contents. However, if we did that, then using an extension
  1309. // identifier would not necessarily cause the compiler to output any sort
  1310. // of reference to any symbol defined in the extension's .pb.o file. Some
  1311. // linkers will actually drop object files that are not explicitly referenced,
  1312. // but that would be bad because it would cause this extension to not be
  1313. // registered at static initialization, and therefore using it would crash.
  1314. template <typename ExtendeeType, typename TypeTraitsType, FieldType field_type,
  1315. bool is_packed>
  1316. class ExtensionIdentifier {
  1317. public:
  1318. typedef TypeTraitsType TypeTraits;
  1319. typedef ExtendeeType Extendee;
  1320. ExtensionIdentifier(int number, typename TypeTraits::ConstType default_value)
  1321. : number_(number), default_value_(default_value) {
  1322. Register(number);
  1323. }
  1324. inline int number() const { return number_; }
  1325. typename TypeTraits::ConstType default_value() const {
  1326. return default_value_;
  1327. }
  1328. static void Register(int number) {
  1329. TypeTraits::template Register<ExtendeeType>(number, field_type, is_packed);
  1330. }
  1331. typename TypeTraits::ConstType const& default_value_ref() const {
  1332. return default_value_;
  1333. }
  1334. private:
  1335. const int number_;
  1336. typename TypeTraits::ConstType default_value_;
  1337. };
  1338. // -------------------------------------------------------------------
  1339. // Generated accessors
  1340. // Used to retrieve a lazy extension, may return nullptr in some environments.
  1341. extern PROTOBUF_ATTRIBUTE_WEAK ExtensionSet::LazyMessageExtension*
  1342. MaybeCreateLazyExtension(Arena* arena);
  1343. } // namespace internal
  1344. // Call this function to ensure that this extensions's reflection is linked into
  1345. // the binary:
  1346. //
  1347. // google::protobuf::LinkExtensionReflection(Foo::my_extension);
  1348. //
  1349. // This will ensure that the following lookup will succeed:
  1350. //
  1351. // DescriptorPool::generated_pool()->FindExtensionByName("Foo.my_extension");
  1352. //
  1353. // This is often relevant for parsing extensions in text mode.
  1354. //
  1355. // As a side-effect, it will also guarantee that anything else from the same
  1356. // .proto file will also be available for lookup in the generated pool.
  1357. //
  1358. // This function does not actually register the extension, so it does not need
  1359. // to be called before the lookup. However it does need to occur in a function
  1360. // that cannot be stripped from the binary (ie. it must be reachable from main).
  1361. //
  1362. // Best practice is to call this function as close as possible to where the
  1363. // reflection is actually needed. This function is very cheap to call, so you
  1364. // should not need to worry about its runtime overhead except in tight loops (on
  1365. // x86-64 it compiles into two "mov" instructions).
  1366. template <typename ExtendeeType, typename TypeTraitsType,
  1367. internal::FieldType field_type, bool is_packed>
  1368. void LinkExtensionReflection(
  1369. const google::protobuf::internal::ExtensionIdentifier<
  1370. ExtendeeType, TypeTraitsType, field_type, is_packed>& extension) {
  1371. internal::StrongReference(extension);
  1372. }
  1373. } // namespace protobuf
  1374. } // namespace google
  1375. #include <google/protobuf/port_undef.inc>
  1376. #endif // GOOGLE_PROTOBUF_EXTENSION_SET_H__