extension_set.h 77 KB

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