extension_set.h 78 KB

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