extension_set.h 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  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 <vector>
  39. #include <map>
  40. #include <utility>
  41. #include <string>
  42. #include <google/protobuf/stubs/common.h>
  43. #include <google/protobuf/stubs/logging.h>
  44. #include <google/protobuf/repeated_field.h>
  45. namespace google {
  46. namespace protobuf {
  47. class Arena;
  48. class Descriptor; // descriptor.h
  49. class FieldDescriptor; // descriptor.h
  50. class DescriptorPool; // descriptor.h
  51. class MessageLite; // message_lite.h
  52. class Message; // message.h
  53. class MessageFactory; // message.h
  54. class UnknownFieldSet; // unknown_field_set.h
  55. namespace io {
  56. class CodedInputStream; // coded_stream.h
  57. class CodedOutputStream; // coded_stream.h
  58. }
  59. namespace internal {
  60. class FieldSkipper; // wire_format_lite.h
  61. }
  62. }
  63. namespace protobuf {
  64. namespace internal {
  65. // Used to store values of type WireFormatLite::FieldType without having to
  66. // #include wire_format_lite.h. Also, ensures that we use only one byte to
  67. // store these values, which is important to keep the layout of
  68. // ExtensionSet::Extension small.
  69. typedef uint8 FieldType;
  70. // A function which, given an integer value, returns true if the number
  71. // matches one of the defined values for the corresponding enum type. This
  72. // is used with RegisterEnumExtension, below.
  73. typedef bool EnumValidityFunc(int number);
  74. // Version of the above which takes an argument. This is needed to deal with
  75. // extensions that are not compiled in.
  76. typedef bool EnumValidityFuncWithArg(const void* arg, int number);
  77. // Information about a registered extension.
  78. struct ExtensionInfo {
  79. inline ExtensionInfo() {}
  80. inline ExtensionInfo(FieldType type_param, bool isrepeated, bool ispacked)
  81. : type(type_param), is_repeated(isrepeated), is_packed(ispacked),
  82. descriptor(NULL) {}
  83. FieldType type;
  84. bool is_repeated;
  85. bool is_packed;
  86. struct EnumValidityCheck {
  87. EnumValidityFuncWithArg* func;
  88. const void* arg;
  89. };
  90. union {
  91. EnumValidityCheck enum_validity_check;
  92. const MessageLite* message_prototype;
  93. };
  94. // The descriptor for this extension, if one exists and is known. May be
  95. // NULL. Must not be NULL if the descriptor for the extension does not
  96. // live in the same pool as the descriptor for the containing type.
  97. const FieldDescriptor* descriptor;
  98. };
  99. // Abstract interface for an object which looks up extension definitions. Used
  100. // when parsing.
  101. class LIBPROTOBUF_EXPORT ExtensionFinder {
  102. public:
  103. virtual ~ExtensionFinder();
  104. // Find the extension with the given containing type and number.
  105. virtual bool Find(int number, ExtensionInfo* output) = 0;
  106. };
  107. // Implementation of ExtensionFinder which finds extensions defined in .proto
  108. // files which have been compiled into the binary.
  109. class LIBPROTOBUF_EXPORT GeneratedExtensionFinder : public ExtensionFinder {
  110. public:
  111. GeneratedExtensionFinder(const MessageLite* containing_type)
  112. : containing_type_(containing_type) {}
  113. virtual ~GeneratedExtensionFinder() {}
  114. // Returns true and fills in *output if found, otherwise returns false.
  115. virtual bool Find(int number, ExtensionInfo* output);
  116. private:
  117. const MessageLite* containing_type_;
  118. };
  119. // A FieldSkipper used for parsing MessageSet.
  120. class MessageSetFieldSkipper;
  121. // Note: extension_set_heavy.cc defines DescriptorPoolExtensionFinder for
  122. // finding extensions from a DescriptorPool.
  123. // This is an internal helper class intended for use within the protocol buffer
  124. // library and generated classes. Clients should not use it directly. Instead,
  125. // use the generated accessors such as GetExtension() of the class being
  126. // extended.
  127. //
  128. // This class manages extensions for a protocol message object. The
  129. // message's HasExtension(), GetExtension(), MutableExtension(), and
  130. // ClearExtension() methods are just thin wrappers around the embedded
  131. // ExtensionSet. When parsing, if a tag number is encountered which is
  132. // inside one of the message type's extension ranges, the tag is passed
  133. // off to the ExtensionSet for parsing. Etc.
  134. class LIBPROTOBUF_EXPORT ExtensionSet {
  135. public:
  136. ExtensionSet();
  137. explicit ExtensionSet(::google::protobuf::Arena* arena);
  138. ~ExtensionSet();
  139. // These are called at startup by protocol-compiler-generated code to
  140. // register known extensions. The registrations are used by ParseField()
  141. // to look up extensions for parsed field numbers. Note that dynamic parsing
  142. // does not use ParseField(); only protocol-compiler-generated parsing
  143. // methods do.
  144. static void RegisterExtension(const MessageLite* containing_type,
  145. int number, FieldType type,
  146. bool is_repeated, bool is_packed);
  147. static void RegisterEnumExtension(const MessageLite* containing_type,
  148. int number, FieldType type,
  149. bool is_repeated, bool is_packed,
  150. EnumValidityFunc* is_valid);
  151. static void RegisterMessageExtension(const MessageLite* containing_type,
  152. int number, FieldType type,
  153. bool is_repeated, bool is_packed,
  154. const MessageLite* prototype);
  155. // =================================================================
  156. // Add all fields which are currently present to the given vector. This
  157. // is useful to implement Reflection::ListFields().
  158. void AppendToList(const Descriptor* containing_type,
  159. const DescriptorPool* pool,
  160. std::vector<const FieldDescriptor*>* output) const;
  161. // =================================================================
  162. // Accessors
  163. //
  164. // Generated message classes include type-safe templated wrappers around
  165. // these methods. Generally you should use those rather than call these
  166. // directly, unless you are doing low-level memory management.
  167. //
  168. // When calling any of these accessors, the extension number requested
  169. // MUST exist in the DescriptorPool provided to the constructor. Otherwise,
  170. // the method will fail an assert. Normally, though, you would not call
  171. // these directly; you would either call the generated accessors of your
  172. // message class (e.g. GetExtension()) or you would call the accessors
  173. // of the reflection interface. In both cases, it is impossible to
  174. // trigger this assert failure: the generated accessors only accept
  175. // linked-in extension types as parameters, while the Reflection interface
  176. // requires you to provide the FieldDescriptor describing the extension.
  177. //
  178. // When calling any of these accessors, a protocol-compiler-generated
  179. // implementation of the extension corresponding to the number MUST
  180. // be linked in, and the FieldDescriptor used to refer to it MUST be
  181. // the one generated by that linked-in code. Otherwise, the method will
  182. // die on an assert failure. The message objects returned by the message
  183. // accessors are guaranteed to be of the correct linked-in type.
  184. //
  185. // These methods pretty much match Reflection except that:
  186. // - They're not virtual.
  187. // - They identify fields by number rather than FieldDescriptors.
  188. // - They identify enum values using integers rather than descriptors.
  189. // - Strings provide Mutable() in addition to Set() accessors.
  190. bool Has(int number) const;
  191. int ExtensionSize(int number) const; // Size of a repeated extension.
  192. int NumExtensions() const; // The number of extensions
  193. FieldType ExtensionType(int number) const;
  194. void ClearExtension(int number);
  195. // singular fields -------------------------------------------------
  196. int32 GetInt32 (int number, int32 default_value) const;
  197. int64 GetInt64 (int number, int64 default_value) const;
  198. uint32 GetUInt32(int number, uint32 default_value) const;
  199. uint64 GetUInt64(int number, uint64 default_value) const;
  200. float GetFloat (int number, float default_value) const;
  201. double GetDouble(int number, double default_value) const;
  202. bool GetBool (int number, bool default_value) const;
  203. int GetEnum (int number, int default_value) const;
  204. const string & GetString (int number, const string& default_value) const;
  205. const MessageLite& GetMessage(int number,
  206. const MessageLite& default_value) const;
  207. const MessageLite& GetMessage(int number, const Descriptor* message_type,
  208. MessageFactory* factory) const;
  209. // |descriptor| may be NULL so long as it is known that the descriptor for
  210. // the extension lives in the same pool as the descriptor for the containing
  211. // type.
  212. #define desc const FieldDescriptor* descriptor // avoid line wrapping
  213. void SetInt32 (int number, FieldType type, int32 value, desc);
  214. void SetInt64 (int number, FieldType type, int64 value, desc);
  215. void SetUInt32(int number, FieldType type, uint32 value, desc);
  216. void SetUInt64(int number, FieldType type, uint64 value, desc);
  217. void SetFloat (int number, FieldType type, float value, desc);
  218. void SetDouble(int number, FieldType type, double value, desc);
  219. void SetBool (int number, FieldType type, bool value, desc);
  220. void SetEnum (int number, FieldType type, int value, desc);
  221. void SetString(int number, FieldType type, const string& value, desc);
  222. string * MutableString (int number, FieldType type, desc);
  223. MessageLite* MutableMessage(int number, FieldType type,
  224. const MessageLite& prototype, desc);
  225. MessageLite* MutableMessage(const FieldDescriptor* decsriptor,
  226. MessageFactory* factory);
  227. // Adds the given message to the ExtensionSet, taking ownership of the
  228. // message object. Existing message with the same number will be deleted.
  229. // If "message" is NULL, this is equivalent to "ClearExtension(number)".
  230. void SetAllocatedMessage(int number, FieldType type,
  231. const FieldDescriptor* descriptor,
  232. MessageLite* message);
  233. void UnsafeArenaSetAllocatedMessage(int number, FieldType type,
  234. const FieldDescriptor* descriptor,
  235. MessageLite* message);
  236. MessageLite* ReleaseMessage(int number, const MessageLite& prototype);
  237. MessageLite* UnsafeArenaReleaseMessage(
  238. int number, const MessageLite& prototype);
  239. MessageLite* ReleaseMessage(const FieldDescriptor* descriptor,
  240. MessageFactory* factory);
  241. #undef desc
  242. ::google::protobuf::Arena* GetArenaNoVirtual() const { return arena_; }
  243. // repeated fields -------------------------------------------------
  244. // Fetches a RepeatedField extension by number; returns |default_value|
  245. // if no such extension exists. User should not touch this directly; it is
  246. // used by the GetRepeatedExtension() method.
  247. const void* GetRawRepeatedField(int number, const void* default_value) const;
  248. // Fetches a mutable version of a RepeatedField extension by number,
  249. // instantiating one if none exists. Similar to above, user should not use
  250. // this directly; it underlies MutableRepeatedExtension().
  251. void* MutableRawRepeatedField(int number, FieldType field_type,
  252. bool packed, const FieldDescriptor* desc);
  253. // This is an overload of MutableRawRepeatedField to maintain compatibility
  254. // with old code using a previous API. This version of
  255. // MutableRawRepeatedField() will GOOGLE_CHECK-fail on a missing extension.
  256. // (E.g.: borg/clients/internal/proto1/proto2_reflection.cc.)
  257. void* MutableRawRepeatedField(int number);
  258. int32 GetRepeatedInt32 (int number, int index) const;
  259. int64 GetRepeatedInt64 (int number, int index) const;
  260. uint32 GetRepeatedUInt32(int number, int index) const;
  261. uint64 GetRepeatedUInt64(int number, int index) const;
  262. float GetRepeatedFloat (int number, int index) const;
  263. double GetRepeatedDouble(int number, int index) const;
  264. bool GetRepeatedBool (int number, int index) const;
  265. int GetRepeatedEnum (int number, int index) const;
  266. const string & GetRepeatedString (int number, int index) const;
  267. const MessageLite& GetRepeatedMessage(int number, int index) const;
  268. void SetRepeatedInt32 (int number, int index, int32 value);
  269. void SetRepeatedInt64 (int number, int index, int64 value);
  270. void SetRepeatedUInt32(int number, int index, uint32 value);
  271. void SetRepeatedUInt64(int number, int index, uint64 value);
  272. void SetRepeatedFloat (int number, int index, float value);
  273. void SetRepeatedDouble(int number, int index, double value);
  274. void SetRepeatedBool (int number, int index, bool value);
  275. void SetRepeatedEnum (int number, int index, int value);
  276. void SetRepeatedString(int number, int index, const string& value);
  277. string * MutableRepeatedString (int number, int index);
  278. MessageLite* MutableRepeatedMessage(int number, int index);
  279. #define desc const FieldDescriptor* descriptor // avoid line wrapping
  280. void AddInt32 (int number, FieldType type, bool packed, int32 value, desc);
  281. void AddInt64 (int number, FieldType type, bool packed, int64 value, desc);
  282. void AddUInt32(int number, FieldType type, bool packed, uint32 value, desc);
  283. void AddUInt64(int number, FieldType type, bool packed, uint64 value, desc);
  284. void AddFloat (int number, FieldType type, bool packed, float value, desc);
  285. void AddDouble(int number, FieldType type, bool packed, double value, desc);
  286. void AddBool (int number, FieldType type, bool packed, bool value, desc);
  287. void AddEnum (int number, FieldType type, bool packed, int value, desc);
  288. void AddString(int number, FieldType type, const string& value, desc);
  289. string * AddString (int number, FieldType type, desc);
  290. MessageLite* AddMessage(int number, FieldType type,
  291. const MessageLite& prototype, desc);
  292. MessageLite* AddMessage(const FieldDescriptor* descriptor,
  293. MessageFactory* factory);
  294. void AddAllocatedMessage(const FieldDescriptor* descriptor,
  295. MessageLite* new_entry);
  296. #undef desc
  297. void RemoveLast(int number);
  298. MessageLite* ReleaseLast(int number);
  299. void SwapElements(int number, int index1, int index2);
  300. // -----------------------------------------------------------------
  301. // TODO(kenton): Hardcore memory management accessors
  302. // =================================================================
  303. // convenience methods for implementing methods of Message
  304. //
  305. // These could all be implemented in terms of the other methods of this
  306. // class, but providing them here helps keep the generated code size down.
  307. void Clear();
  308. void MergeFrom(const ExtensionSet& other);
  309. void Swap(ExtensionSet* other);
  310. void SwapExtension(ExtensionSet* other, int number);
  311. bool IsInitialized() const;
  312. // Parses a single extension from the input. The input should start out
  313. // positioned immediately after the tag.
  314. bool ParseField(uint32 tag, io::CodedInputStream* input,
  315. ExtensionFinder* extension_finder,
  316. FieldSkipper* field_skipper);
  317. // Specific versions for lite or full messages (constructs the appropriate
  318. // FieldSkipper automatically). |containing_type| is the default
  319. // instance for the containing message; it is used only to look up the
  320. // extension by number. See RegisterExtension(), above. Unlike the other
  321. // methods of ExtensionSet, this only works for generated message types --
  322. // it looks up extensions registered using RegisterExtension().
  323. bool ParseField(uint32 tag, io::CodedInputStream* input,
  324. const MessageLite* containing_type);
  325. bool ParseField(uint32 tag, io::CodedInputStream* input,
  326. const Message* containing_type,
  327. UnknownFieldSet* unknown_fields);
  328. bool ParseField(uint32 tag, io::CodedInputStream* input,
  329. const MessageLite* containing_type,
  330. io::CodedOutputStream* unknown_fields);
  331. // Parse an entire message in MessageSet format. Such messages have no
  332. // fields, only extensions.
  333. bool ParseMessageSet(io::CodedInputStream* input,
  334. ExtensionFinder* extension_finder,
  335. MessageSetFieldSkipper* field_skipper);
  336. // Specific versions for lite or full messages (constructs the appropriate
  337. // FieldSkipper automatically).
  338. bool ParseMessageSet(io::CodedInputStream* input,
  339. const MessageLite* containing_type);
  340. bool ParseMessageSet(io::CodedInputStream* input,
  341. const Message* containing_type,
  342. UnknownFieldSet* unknown_fields);
  343. // Write all extension fields with field numbers in the range
  344. // [start_field_number, end_field_number)
  345. // to the output stream, using the cached sizes computed when ByteSize() was
  346. // last called. Note that the range bounds are inclusive-exclusive.
  347. void SerializeWithCachedSizes(int start_field_number,
  348. int end_field_number,
  349. io::CodedOutputStream* output) const;
  350. // Same as SerializeWithCachedSizes, but without any bounds checking.
  351. // The caller must ensure that target has sufficient capacity for the
  352. // serialized extensions.
  353. //
  354. // Returns a pointer past the last written byte.
  355. uint8* SerializeWithCachedSizesToArray(int start_field_number,
  356. int end_field_number,
  357. uint8* target) const;
  358. // Like above but serializes in MessageSet format.
  359. void SerializeMessageSetWithCachedSizes(io::CodedOutputStream* output) const;
  360. uint8* SerializeMessageSetWithCachedSizesToArray(uint8* target) const;
  361. // Returns the total serialized size of all the extensions.
  362. int ByteSize() const;
  363. // Like ByteSize() but uses MessageSet format.
  364. int MessageSetByteSize() const;
  365. // Returns (an estimate of) the total number of bytes used for storing the
  366. // extensions in memory, excluding sizeof(*this). If the ExtensionSet is
  367. // for a lite message (and thus possibly contains lite messages), the results
  368. // are undefined (might work, might crash, might corrupt data, might not even
  369. // be linked in). It's up to the protocol compiler to avoid calling this on
  370. // such ExtensionSets (easy enough since lite messages don't implement
  371. // SpaceUsed()).
  372. int SpaceUsedExcludingSelf() const;
  373. private:
  374. // Interface of a lazily parsed singular message extension.
  375. class LIBPROTOBUF_EXPORT LazyMessageExtension {
  376. public:
  377. LazyMessageExtension() {}
  378. virtual ~LazyMessageExtension() {}
  379. virtual LazyMessageExtension* New(::google::protobuf::Arena* arena) const = 0;
  380. virtual const MessageLite& GetMessage(
  381. const MessageLite& prototype) const = 0;
  382. virtual MessageLite* MutableMessage(const MessageLite& prototype) = 0;
  383. virtual void SetAllocatedMessage(MessageLite *message) = 0;
  384. virtual void UnsafeArenaSetAllocatedMessage(MessageLite *message) = 0;
  385. virtual MessageLite* ReleaseMessage(const MessageLite& prototype) = 0;
  386. virtual MessageLite* UnsafeArenaReleaseMessage(
  387. const MessageLite& prototype) = 0;
  388. virtual bool IsInitialized() const = 0;
  389. virtual int ByteSize() const = 0;
  390. virtual int SpaceUsed() const = 0;
  391. virtual void MergeFrom(const LazyMessageExtension& other) = 0;
  392. virtual void Clear() = 0;
  393. virtual bool ReadMessage(const MessageLite& prototype,
  394. io::CodedInputStream* input) = 0;
  395. virtual void WriteMessage(int number,
  396. io::CodedOutputStream* output) const = 0;
  397. virtual uint8* WriteMessageToArray(int number, uint8* target) const = 0;
  398. private:
  399. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(LazyMessageExtension);
  400. };
  401. struct Extension {
  402. // The order of these fields packs Extension into 24 bytes when using 8
  403. // byte alignment. Consider this when adding or removing fields here.
  404. union {
  405. int32 int32_value;
  406. int64 int64_value;
  407. uint32 uint32_value;
  408. uint64 uint64_value;
  409. float float_value;
  410. double double_value;
  411. bool bool_value;
  412. int enum_value;
  413. string* string_value;
  414. MessageLite* message_value;
  415. LazyMessageExtension* lazymessage_value;
  416. RepeatedField <int32 >* repeated_int32_value;
  417. RepeatedField <int64 >* repeated_int64_value;
  418. RepeatedField <uint32 >* repeated_uint32_value;
  419. RepeatedField <uint64 >* repeated_uint64_value;
  420. RepeatedField <float >* repeated_float_value;
  421. RepeatedField <double >* repeated_double_value;
  422. RepeatedField <bool >* repeated_bool_value;
  423. RepeatedField <int >* repeated_enum_value;
  424. RepeatedPtrField<string >* repeated_string_value;
  425. RepeatedPtrField<MessageLite>* repeated_message_value;
  426. };
  427. FieldType type;
  428. bool is_repeated;
  429. // For singular types, indicates if the extension is "cleared". This
  430. // happens when an extension is set and then later cleared by the caller.
  431. // We want to keep the Extension object around for reuse, so instead of
  432. // removing it from the map, we just set is_cleared = true. This has no
  433. // meaning for repeated types; for those, the size of the RepeatedField
  434. // simply becomes zero when cleared.
  435. bool is_cleared : 4;
  436. // For singular message types, indicates whether lazy parsing is enabled
  437. // for this extension. This field is only valid when type == TYPE_MESSAGE
  438. // and !is_repeated because we only support lazy parsing for singular
  439. // message types currently. If is_lazy = true, the extension is stored in
  440. // lazymessage_value. Otherwise, the extension will be message_value.
  441. bool is_lazy : 4;
  442. // For repeated types, this indicates if the [packed=true] option is set.
  443. bool is_packed;
  444. // For packed fields, the size of the packed data is recorded here when
  445. // ByteSize() is called then used during serialization.
  446. // TODO(kenton): Use atomic<int> when C++ supports it.
  447. mutable int cached_size;
  448. // The descriptor for this extension, if one exists and is known. May be
  449. // NULL. Must not be NULL if the descriptor for the extension does not
  450. // live in the same pool as the descriptor for the containing type.
  451. const FieldDescriptor* descriptor;
  452. // Some helper methods for operations on a single Extension.
  453. void SerializeFieldWithCachedSizes(
  454. int number,
  455. io::CodedOutputStream* output) const;
  456. uint8* SerializeFieldWithCachedSizesToArray(
  457. int number,
  458. uint8* target) const;
  459. void SerializeMessageSetItemWithCachedSizes(
  460. int number,
  461. io::CodedOutputStream* output) const;
  462. uint8* SerializeMessageSetItemWithCachedSizesToArray(
  463. int number,
  464. uint8* target) const;
  465. int ByteSize(int number) const;
  466. int MessageSetItemByteSize(int number) const;
  467. void Clear();
  468. int GetSize() const;
  469. void Free();
  470. int SpaceUsedExcludingSelf() const;
  471. };
  472. // Merges existing Extension from other_extension
  473. void InternalExtensionMergeFrom(int number, const Extension& other_extension);
  474. // Returns true and fills field_number and extension if extension is found.
  475. // Note to support packed repeated field compatibility, it also fills whether
  476. // the tag on wire is packed, which can be different from
  477. // extension->is_packed (whether packed=true is specified).
  478. bool FindExtensionInfoFromTag(uint32 tag, ExtensionFinder* extension_finder,
  479. int* field_number, ExtensionInfo* extension,
  480. bool* was_packed_on_wire);
  481. // Returns true and fills extension if extension is found.
  482. // Note to support packed repeated field compatibility, it also fills whether
  483. // the tag on wire is packed, which can be different from
  484. // extension->is_packed (whether packed=true is specified).
  485. bool FindExtensionInfoFromFieldNumber(int wire_type, int field_number,
  486. ExtensionFinder* extension_finder,
  487. ExtensionInfo* extension,
  488. bool* was_packed_on_wire);
  489. // Parses a single extension from the input. The input should start out
  490. // positioned immediately after the wire tag. This method is called in
  491. // ParseField() after field number and was_packed_on_wire is extracted from
  492. // the wire tag and ExtensionInfo is found by the field number.
  493. bool ParseFieldWithExtensionInfo(int field_number,
  494. bool was_packed_on_wire,
  495. const ExtensionInfo& extension,
  496. io::CodedInputStream* input,
  497. FieldSkipper* field_skipper);
  498. // Like ParseField(), but this method may parse singular message extensions
  499. // lazily depending on the value of FLAGS_eagerly_parse_message_sets.
  500. bool ParseFieldMaybeLazily(int wire_type, int field_number,
  501. io::CodedInputStream* input,
  502. ExtensionFinder* extension_finder,
  503. MessageSetFieldSkipper* field_skipper);
  504. // Gets the extension with the given number, creating it if it does not
  505. // already exist. Returns true if the extension did not already exist.
  506. bool MaybeNewExtension(int number, const FieldDescriptor* descriptor,
  507. Extension** result);
  508. // Gets the repeated extension for the given descriptor, creating it if
  509. // it does not exist.
  510. Extension* MaybeNewRepeatedExtension(const FieldDescriptor* descriptor);
  511. // Parse a single MessageSet item -- called just after the item group start
  512. // tag has been read.
  513. bool ParseMessageSetItem(io::CodedInputStream* input,
  514. ExtensionFinder* extension_finder,
  515. MessageSetFieldSkipper* field_skipper);
  516. // Hack: RepeatedPtrFieldBase declares ExtensionSet as a friend. This
  517. // friendship should automatically extend to ExtensionSet::Extension, but
  518. // unfortunately some older compilers (e.g. GCC 3.4.4) do not implement this
  519. // correctly. So, we must provide helpers for calling methods of that
  520. // class.
  521. // Defined in extension_set_heavy.cc.
  522. static inline int RepeatedMessage_SpaceUsedExcludingSelf(
  523. RepeatedPtrFieldBase* field);
  524. // The Extension struct is small enough to be passed by value, so we use it
  525. // directly as the value type in the map rather than use pointers. We use
  526. // a map rather than hash_map here because we expect most ExtensionSets will
  527. // only contain a small number of extensions whereas hash_map is optimized
  528. // for 100 elements or more. Also, we want AppendToList() to order fields
  529. // by field number.
  530. std::map<int, Extension> extensions_;
  531. ::google::protobuf::Arena* arena_;
  532. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ExtensionSet);
  533. };
  534. // These are just for convenience...
  535. inline void ExtensionSet::SetString(int number, FieldType type,
  536. const string& value,
  537. const FieldDescriptor* descriptor) {
  538. MutableString(number, type, descriptor)->assign(value);
  539. }
  540. inline void ExtensionSet::SetRepeatedString(int number, int index,
  541. const string& value) {
  542. MutableRepeatedString(number, index)->assign(value);
  543. }
  544. inline void ExtensionSet::AddString(int number, FieldType type,
  545. const string& value,
  546. const FieldDescriptor* descriptor) {
  547. AddString(number, type, descriptor)->assign(value);
  548. }
  549. // ===================================================================
  550. // Glue for generated extension accessors
  551. // -------------------------------------------------------------------
  552. // Template magic
  553. // First we have a set of classes representing "type traits" for different
  554. // field types. A type traits class knows how to implement basic accessors
  555. // for extensions of a particular type given an ExtensionSet. The signature
  556. // for a type traits class looks like this:
  557. //
  558. // class TypeTraits {
  559. // public:
  560. // typedef ? ConstType;
  561. // typedef ? MutableType;
  562. // // TypeTraits for singular fields and repeated fields will define the
  563. // // symbol "Singular" or "Repeated" respectively. These two symbols will
  564. // // be used in extension accessors to distinguish between singular
  565. // // extensions and repeated extensions. If the TypeTraits for the passed
  566. // // in extension doesn't have the expected symbol defined, it means the
  567. // // user is passing a repeated extension to a singular accessor, or the
  568. // // opposite. In that case the C++ compiler will generate an error
  569. // // message "no matching member function" to inform the user.
  570. // typedef ? Singular
  571. // typedef ? Repeated
  572. //
  573. // static inline ConstType Get(int number, const ExtensionSet& set);
  574. // static inline void Set(int number, ConstType value, ExtensionSet* set);
  575. // static inline MutableType Mutable(int number, ExtensionSet* set);
  576. //
  577. // // Variants for repeated fields.
  578. // static inline ConstType Get(int number, const ExtensionSet& set,
  579. // int index);
  580. // static inline void Set(int number, int index,
  581. // ConstType value, ExtensionSet* set);
  582. // static inline MutableType Mutable(int number, int index,
  583. // ExtensionSet* set);
  584. // static inline void Add(int number, ConstType value, ExtensionSet* set);
  585. // static inline MutableType Add(int number, ExtensionSet* set);
  586. // };
  587. //
  588. // Not all of these methods make sense for all field types. For example, the
  589. // "Mutable" methods only make sense for strings and messages, and the
  590. // repeated methods only make sense for repeated types. So, each type
  591. // traits class implements only the set of methods from this signature that it
  592. // actually supports. This will cause a compiler error if the user tries to
  593. // access an extension using a method that doesn't make sense for its type.
  594. // For example, if "foo" is an extension of type "optional int32", then if you
  595. // try to write code like:
  596. // my_message.MutableExtension(foo)
  597. // you will get a compile error because PrimitiveTypeTraits<int32> does not
  598. // have a "Mutable()" method.
  599. // -------------------------------------------------------------------
  600. // PrimitiveTypeTraits
  601. // Since the ExtensionSet has different methods for each primitive type,
  602. // we must explicitly define the methods of the type traits class for each
  603. // known type.
  604. template <typename Type>
  605. class PrimitiveTypeTraits {
  606. public:
  607. typedef Type ConstType;
  608. typedef Type MutableType;
  609. typedef PrimitiveTypeTraits<Type> Singular;
  610. static inline ConstType Get(int number, const ExtensionSet& set,
  611. ConstType default_value);
  612. static inline void Set(int number, FieldType field_type,
  613. ConstType value, ExtensionSet* set);
  614. };
  615. template <typename Type>
  616. class RepeatedPrimitiveTypeTraits {
  617. public:
  618. typedef Type ConstType;
  619. typedef Type MutableType;
  620. typedef RepeatedPrimitiveTypeTraits<Type> Repeated;
  621. typedef RepeatedField<Type> RepeatedFieldType;
  622. static inline Type Get(int number, const ExtensionSet& set, int index);
  623. static inline void Set(int number, int index, Type value, ExtensionSet* set);
  624. static inline void Add(int number, FieldType field_type,
  625. bool is_packed, Type value, ExtensionSet* set);
  626. static inline const RepeatedField<ConstType>&
  627. GetRepeated(int number, const ExtensionSet& set);
  628. static inline RepeatedField<Type>*
  629. MutableRepeated(int number, FieldType field_type,
  630. bool is_packed, ExtensionSet* set);
  631. static const RepeatedFieldType* GetDefaultRepeatedField();
  632. };
  633. // Declared here so that this can be friended below.
  634. void InitializeDefaultRepeatedFields();
  635. void DestroyDefaultRepeatedFields();
  636. class LIBPROTOBUF_EXPORT RepeatedPrimitiveGenericTypeTraits {
  637. private:
  638. template<typename Type> friend class RepeatedPrimitiveTypeTraits;
  639. friend void InitializeDefaultRepeatedFields();
  640. friend void DestroyDefaultRepeatedFields();
  641. static const RepeatedField<int32>* default_repeated_field_int32_;
  642. static const RepeatedField<int64>* default_repeated_field_int64_;
  643. static const RepeatedField<uint32>* default_repeated_field_uint32_;
  644. static const RepeatedField<uint64>* default_repeated_field_uint64_;
  645. static const RepeatedField<double>* default_repeated_field_double_;
  646. static const RepeatedField<float>* default_repeated_field_float_;
  647. static const RepeatedField<bool>* default_repeated_field_bool_;
  648. };
  649. #define PROTOBUF_DEFINE_PRIMITIVE_TYPE(TYPE, METHOD) \
  650. template<> inline TYPE PrimitiveTypeTraits<TYPE>::Get( \
  651. int number, const ExtensionSet& set, TYPE default_value) { \
  652. return set.Get##METHOD(number, default_value); \
  653. } \
  654. template<> inline void PrimitiveTypeTraits<TYPE>::Set( \
  655. int number, FieldType field_type, TYPE value, ExtensionSet* set) { \
  656. set->Set##METHOD(number, field_type, value, NULL); \
  657. } \
  658. \
  659. template<> inline TYPE RepeatedPrimitiveTypeTraits<TYPE>::Get( \
  660. int number, const ExtensionSet& set, int index) { \
  661. return set.GetRepeated##METHOD(number, index); \
  662. } \
  663. template<> inline void RepeatedPrimitiveTypeTraits<TYPE>::Set( \
  664. int number, int index, TYPE value, ExtensionSet* set) { \
  665. set->SetRepeated##METHOD(number, index, value); \
  666. } \
  667. template<> inline void RepeatedPrimitiveTypeTraits<TYPE>::Add( \
  668. int number, FieldType field_type, bool is_packed, \
  669. TYPE value, ExtensionSet* set) { \
  670. set->Add##METHOD(number, field_type, is_packed, value, NULL); \
  671. } \
  672. template<> inline const RepeatedField<TYPE>* \
  673. RepeatedPrimitiveTypeTraits<TYPE>::GetDefaultRepeatedField() { \
  674. return RepeatedPrimitiveGenericTypeTraits:: \
  675. default_repeated_field_##TYPE##_; \
  676. } \
  677. template<> inline const RepeatedField<TYPE>& \
  678. RepeatedPrimitiveTypeTraits<TYPE>::GetRepeated(int number, \
  679. const ExtensionSet& set) { \
  680. return *reinterpret_cast<const RepeatedField<TYPE>*>( \
  681. set.GetRawRepeatedField( \
  682. number, GetDefaultRepeatedField())); \
  683. } \
  684. template<> inline RepeatedField<TYPE>* \
  685. RepeatedPrimitiveTypeTraits<TYPE>::MutableRepeated(int number, \
  686. FieldType field_type, \
  687. bool is_packed, \
  688. ExtensionSet* set) { \
  689. return reinterpret_cast<RepeatedField<TYPE>*>( \
  690. set->MutableRawRepeatedField(number, field_type, is_packed, NULL)); \
  691. }
  692. PROTOBUF_DEFINE_PRIMITIVE_TYPE( int32, Int32)
  693. PROTOBUF_DEFINE_PRIMITIVE_TYPE( int64, Int64)
  694. PROTOBUF_DEFINE_PRIMITIVE_TYPE(uint32, UInt32)
  695. PROTOBUF_DEFINE_PRIMITIVE_TYPE(uint64, UInt64)
  696. PROTOBUF_DEFINE_PRIMITIVE_TYPE( float, Float)
  697. PROTOBUF_DEFINE_PRIMITIVE_TYPE(double, Double)
  698. PROTOBUF_DEFINE_PRIMITIVE_TYPE( bool, Bool)
  699. #undef PROTOBUF_DEFINE_PRIMITIVE_TYPE
  700. // -------------------------------------------------------------------
  701. // StringTypeTraits
  702. // Strings support both Set() and Mutable().
  703. class LIBPROTOBUF_EXPORT StringTypeTraits {
  704. public:
  705. typedef const string& ConstType;
  706. typedef string* MutableType;
  707. typedef StringTypeTraits Singular;
  708. static inline const string& Get(int number, const ExtensionSet& set,
  709. ConstType default_value) {
  710. return set.GetString(number, default_value);
  711. }
  712. static inline void Set(int number, FieldType field_type,
  713. const string& value, ExtensionSet* set) {
  714. set->SetString(number, field_type, value, NULL);
  715. }
  716. static inline string* Mutable(int number, FieldType field_type,
  717. ExtensionSet* set) {
  718. return set->MutableString(number, field_type, NULL);
  719. }
  720. };
  721. class LIBPROTOBUF_EXPORT RepeatedStringTypeTraits {
  722. public:
  723. typedef const string& ConstType;
  724. typedef string* MutableType;
  725. typedef RepeatedStringTypeTraits Repeated;
  726. typedef RepeatedPtrField<string> RepeatedFieldType;
  727. static inline const string& Get(int number, const ExtensionSet& set,
  728. int index) {
  729. return set.GetRepeatedString(number, index);
  730. }
  731. static inline void Set(int number, int index,
  732. const string& value, ExtensionSet* set) {
  733. set->SetRepeatedString(number, index, value);
  734. }
  735. static inline string* Mutable(int number, int index, ExtensionSet* set) {
  736. return set->MutableRepeatedString(number, index);
  737. }
  738. static inline void Add(int number, FieldType field_type,
  739. bool /*is_packed*/, const string& value,
  740. ExtensionSet* set) {
  741. set->AddString(number, field_type, value, NULL);
  742. }
  743. static inline string* Add(int number, FieldType field_type,
  744. ExtensionSet* set) {
  745. return set->AddString(number, field_type, NULL);
  746. }
  747. static inline const RepeatedPtrField<string>&
  748. GetRepeated(int number, const ExtensionSet& set) {
  749. return *reinterpret_cast<const RepeatedPtrField<string>*>(
  750. set.GetRawRepeatedField(number, GetDefaultRepeatedField()));
  751. }
  752. static inline RepeatedPtrField<string>*
  753. MutableRepeated(int number, FieldType field_type,
  754. bool is_packed, ExtensionSet* set) {
  755. return reinterpret_cast<RepeatedPtrField<string>*>(
  756. set->MutableRawRepeatedField(number, field_type,
  757. is_packed, NULL));
  758. }
  759. static const RepeatedFieldType* GetDefaultRepeatedField() {
  760. return default_repeated_field_;
  761. }
  762. private:
  763. friend void InitializeDefaultRepeatedFields();
  764. friend void DestroyDefaultRepeatedFields();
  765. static const RepeatedFieldType *default_repeated_field_;
  766. };
  767. // -------------------------------------------------------------------
  768. // EnumTypeTraits
  769. // ExtensionSet represents enums using integers internally, so we have to
  770. // static_cast around.
  771. template <typename Type, bool IsValid(int)>
  772. class EnumTypeTraits {
  773. public:
  774. typedef Type ConstType;
  775. typedef Type MutableType;
  776. typedef EnumTypeTraits<Type, IsValid> Singular;
  777. static inline ConstType Get(int number, const ExtensionSet& set,
  778. ConstType default_value) {
  779. return static_cast<Type>(set.GetEnum(number, default_value));
  780. }
  781. static inline void Set(int number, FieldType field_type,
  782. ConstType value, ExtensionSet* set) {
  783. GOOGLE_DCHECK(IsValid(value));
  784. set->SetEnum(number, field_type, value, NULL);
  785. }
  786. };
  787. template <typename Type, bool IsValid(int)>
  788. class RepeatedEnumTypeTraits {
  789. public:
  790. typedef Type ConstType;
  791. typedef Type MutableType;
  792. typedef RepeatedEnumTypeTraits<Type, IsValid> Repeated;
  793. typedef RepeatedField<Type> RepeatedFieldType;
  794. static inline ConstType Get(int number, const ExtensionSet& set, int index) {
  795. return static_cast<Type>(set.GetRepeatedEnum(number, index));
  796. }
  797. static inline void Set(int number, int index,
  798. ConstType value, ExtensionSet* set) {
  799. GOOGLE_DCHECK(IsValid(value));
  800. set->SetRepeatedEnum(number, index, value);
  801. }
  802. static inline void Add(int number, FieldType field_type,
  803. bool is_packed, ConstType value, ExtensionSet* set) {
  804. GOOGLE_DCHECK(IsValid(value));
  805. set->AddEnum(number, field_type, is_packed, value, NULL);
  806. }
  807. static inline const RepeatedField<Type>& GetRepeated(int number,
  808. const ExtensionSet&
  809. set) {
  810. // Hack: the `Extension` struct stores a RepeatedField<int> for enums.
  811. // RepeatedField<int> cannot implicitly convert to RepeatedField<EnumType>
  812. // so we need to do some casting magic. See message.h for similar
  813. // contortions for non-extension fields.
  814. return *reinterpret_cast<const RepeatedField<Type>*>(
  815. set.GetRawRepeatedField(number, GetDefaultRepeatedField()));
  816. }
  817. static inline RepeatedField<Type>* MutableRepeated(int number,
  818. FieldType field_type,
  819. bool is_packed,
  820. ExtensionSet* set) {
  821. return reinterpret_cast<RepeatedField<Type>*>(
  822. set->MutableRawRepeatedField(number, field_type, is_packed, NULL));
  823. }
  824. static const RepeatedFieldType* GetDefaultRepeatedField() {
  825. // Hack: as noted above, repeated enum fields are internally stored as a
  826. // RepeatedField<int>. We need to be able to instantiate global static
  827. // objects to return as default (empty) repeated fields on non-existent
  828. // extensions. We would not be able to know a-priori all of the enum types
  829. // (values of |Type|) to instantiate all of these, so we just re-use int32's
  830. // default repeated field object.
  831. return reinterpret_cast<const RepeatedField<Type>*>(
  832. RepeatedPrimitiveTypeTraits<int32>::GetDefaultRepeatedField());
  833. }
  834. };
  835. // -------------------------------------------------------------------
  836. // MessageTypeTraits
  837. // ExtensionSet guarantees that when manipulating extensions with message
  838. // types, the implementation used will be the compiled-in class representing
  839. // that type. So, we can static_cast down to the exact type we expect.
  840. template <typename Type>
  841. class MessageTypeTraits {
  842. public:
  843. typedef const Type& ConstType;
  844. typedef Type* MutableType;
  845. typedef MessageTypeTraits<Type> Singular;
  846. static inline ConstType Get(int number, const ExtensionSet& set,
  847. ConstType default_value) {
  848. return static_cast<const Type&>(
  849. set.GetMessage(number, default_value));
  850. }
  851. static inline MutableType Mutable(int number, FieldType field_type,
  852. ExtensionSet* set) {
  853. return static_cast<Type*>(
  854. set->MutableMessage(number, field_type, Type::default_instance(), NULL));
  855. }
  856. static inline void SetAllocated(int number, FieldType field_type,
  857. MutableType message, ExtensionSet* set) {
  858. set->SetAllocatedMessage(number, field_type, NULL, message);
  859. }
  860. static inline MutableType Release(int number, FieldType /* field_type */,
  861. ExtensionSet* set) {
  862. return static_cast<Type*>(set->ReleaseMessage(
  863. number, Type::default_instance()));
  864. }
  865. };
  866. // forward declaration
  867. class RepeatedMessageGenericTypeTraits;
  868. template <typename Type>
  869. class RepeatedMessageTypeTraits {
  870. public:
  871. typedef const Type& ConstType;
  872. typedef Type* MutableType;
  873. typedef RepeatedMessageTypeTraits<Type> Repeated;
  874. typedef RepeatedPtrField<Type> RepeatedFieldType;
  875. static inline ConstType Get(int number, const ExtensionSet& set, int index) {
  876. return static_cast<const Type&>(set.GetRepeatedMessage(number, index));
  877. }
  878. static inline MutableType Mutable(int number, int index, ExtensionSet* set) {
  879. return static_cast<Type*>(set->MutableRepeatedMessage(number, index));
  880. }
  881. static inline MutableType Add(int number, FieldType field_type,
  882. ExtensionSet* set) {
  883. return static_cast<Type*>(
  884. set->AddMessage(number, field_type, Type::default_instance(), NULL));
  885. }
  886. static inline const RepeatedPtrField<Type>& GetRepeated(int number,
  887. const ExtensionSet&
  888. set) {
  889. // See notes above in RepeatedEnumTypeTraits::GetRepeated(): same
  890. // casting hack applies here, because a RepeatedPtrField<MessageLite>
  891. // cannot naturally become a RepeatedPtrType<Type> even though Type is
  892. // presumably a message. google::protobuf::Message goes through similar contortions
  893. // with a reinterpret_cast<>.
  894. return *reinterpret_cast<const RepeatedPtrField<Type>*>(
  895. set.GetRawRepeatedField(number, GetDefaultRepeatedField()));
  896. }
  897. static inline RepeatedPtrField<Type>* MutableRepeated(int number,
  898. FieldType field_type,
  899. bool is_packed,
  900. ExtensionSet* set) {
  901. return reinterpret_cast<RepeatedPtrField<Type>*>(
  902. set->MutableRawRepeatedField(number, field_type, is_packed, NULL));
  903. }
  904. static const RepeatedFieldType* GetDefaultRepeatedField();
  905. };
  906. // This class exists only to hold a generic default empty repeated field for all
  907. // message-type repeated field extensions.
  908. class LIBPROTOBUF_EXPORT RepeatedMessageGenericTypeTraits {
  909. public:
  910. typedef RepeatedPtrField< ::google::protobuf::MessageLite*> RepeatedFieldType;
  911. private:
  912. template<typename Type> friend class RepeatedMessageTypeTraits;
  913. friend void InitializeDefaultRepeatedFields();
  914. friend void DestroyDefaultRepeatedFields();
  915. static const RepeatedFieldType* default_repeated_field_;
  916. };
  917. template<typename Type> inline
  918. const typename RepeatedMessageTypeTraits<Type>::RepeatedFieldType*
  919. RepeatedMessageTypeTraits<Type>::GetDefaultRepeatedField() {
  920. return reinterpret_cast<const RepeatedFieldType*>(
  921. RepeatedMessageGenericTypeTraits::default_repeated_field_);
  922. }
  923. // -------------------------------------------------------------------
  924. // ExtensionIdentifier
  925. // This is the type of actual extension objects. E.g. if you have:
  926. // extends Foo with optional int32 bar = 1234;
  927. // then "bar" will be defined in C++ as:
  928. // ExtensionIdentifier<Foo, PrimitiveTypeTraits<int32>, 1, false> bar(1234);
  929. //
  930. // Note that we could, in theory, supply the field number as a template
  931. // parameter, and thus make an instance of ExtensionIdentifier have no
  932. // actual contents. However, if we did that, then using at extension
  933. // identifier would not necessarily cause the compiler to output any sort
  934. // of reference to any simple defined in the extension's .pb.o file. Some
  935. // linkers will actually drop object files that are not explicitly referenced,
  936. // but that would be bad because it would cause this extension to not be
  937. // registered at static initialization, and therefore using it would crash.
  938. template <typename ExtendeeType, typename TypeTraitsType,
  939. FieldType field_type, bool is_packed>
  940. class ExtensionIdentifier {
  941. public:
  942. typedef TypeTraitsType TypeTraits;
  943. typedef ExtendeeType Extendee;
  944. ExtensionIdentifier(int number, typename TypeTraits::ConstType default_value)
  945. : number_(number), default_value_(default_value) {}
  946. inline int number() const { return number_; }
  947. typename TypeTraits::ConstType default_value() const {
  948. return default_value_;
  949. }
  950. private:
  951. const int number_;
  952. typename TypeTraits::ConstType default_value_;
  953. };
  954. // -------------------------------------------------------------------
  955. // Generated accessors
  956. // This macro should be expanded in the context of a generated type which
  957. // has extensions.
  958. //
  959. // We use "_proto_TypeTraits" as a type name below because "TypeTraits"
  960. // causes problems if the class has a nested message or enum type with that
  961. // name and "_TypeTraits" is technically reserved for the C++ library since
  962. // it starts with an underscore followed by a capital letter.
  963. //
  964. // For similar reason, we use "_field_type" and "_is_packed" as parameter names
  965. // below, so that "field_type" and "is_packed" can be used as field names.
  966. #define GOOGLE_PROTOBUF_EXTENSION_ACCESSORS(CLASSNAME) \
  967. /* Has, Size, Clear */ \
  968. template <typename _proto_TypeTraits, \
  969. ::google::protobuf::internal::FieldType _field_type, \
  970. bool _is_packed> \
  971. inline bool HasExtension( \
  972. const ::google::protobuf::internal::ExtensionIdentifier< \
  973. CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) const { \
  974. return _extensions_.Has(id.number()); \
  975. } \
  976. \
  977. template <typename _proto_TypeTraits, \
  978. ::google::protobuf::internal::FieldType _field_type, \
  979. bool _is_packed> \
  980. inline void ClearExtension( \
  981. const ::google::protobuf::internal::ExtensionIdentifier< \
  982. CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) { \
  983. _extensions_.ClearExtension(id.number()); \
  984. } \
  985. \
  986. template <typename _proto_TypeTraits, \
  987. ::google::protobuf::internal::FieldType _field_type, \
  988. bool _is_packed> \
  989. inline int ExtensionSize( \
  990. const ::google::protobuf::internal::ExtensionIdentifier< \
  991. CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) const { \
  992. return _extensions_.ExtensionSize(id.number()); \
  993. } \
  994. \
  995. /* Singular accessors */ \
  996. template <typename _proto_TypeTraits, \
  997. ::google::protobuf::internal::FieldType _field_type, \
  998. bool _is_packed> \
  999. inline typename _proto_TypeTraits::Singular::ConstType GetExtension( \
  1000. const ::google::protobuf::internal::ExtensionIdentifier< \
  1001. CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) const { \
  1002. return _proto_TypeTraits::Get(id.number(), _extensions_, \
  1003. id.default_value()); \
  1004. } \
  1005. \
  1006. template <typename _proto_TypeTraits, \
  1007. ::google::protobuf::internal::FieldType _field_type, \
  1008. bool _is_packed> \
  1009. inline typename _proto_TypeTraits::Singular::MutableType MutableExtension( \
  1010. const ::google::protobuf::internal::ExtensionIdentifier< \
  1011. CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) { \
  1012. return _proto_TypeTraits::Mutable(id.number(), _field_type, \
  1013. &_extensions_); \
  1014. } \
  1015. \
  1016. template <typename _proto_TypeTraits, \
  1017. ::google::protobuf::internal::FieldType _field_type, \
  1018. bool _is_packed> \
  1019. inline void SetExtension( \
  1020. const ::google::protobuf::internal::ExtensionIdentifier< \
  1021. CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id, \
  1022. typename _proto_TypeTraits::Singular::ConstType value) { \
  1023. _proto_TypeTraits::Set(id.number(), _field_type, value, &_extensions_); \
  1024. } \
  1025. \
  1026. template <typename _proto_TypeTraits, \
  1027. ::google::protobuf::internal::FieldType _field_type, \
  1028. bool _is_packed> \
  1029. inline void SetAllocatedExtension( \
  1030. const ::google::protobuf::internal::ExtensionIdentifier< \
  1031. CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id, \
  1032. typename _proto_TypeTraits::Singular::MutableType value) { \
  1033. _proto_TypeTraits::SetAllocated(id.number(), _field_type, \
  1034. value, &_extensions_); \
  1035. } \
  1036. template <typename _proto_TypeTraits, \
  1037. ::google::protobuf::internal::FieldType _field_type, \
  1038. bool _is_packed> \
  1039. inline typename _proto_TypeTraits::Singular::MutableType ReleaseExtension( \
  1040. const ::google::protobuf::internal::ExtensionIdentifier< \
  1041. CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) { \
  1042. return _proto_TypeTraits::Release(id.number(), _field_type, \
  1043. &_extensions_); \
  1044. } \
  1045. \
  1046. /* Repeated accessors */ \
  1047. template <typename _proto_TypeTraits, \
  1048. ::google::protobuf::internal::FieldType _field_type, \
  1049. bool _is_packed> \
  1050. inline typename _proto_TypeTraits::Repeated::ConstType GetExtension( \
  1051. const ::google::protobuf::internal::ExtensionIdentifier< \
  1052. CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id, \
  1053. int index) const { \
  1054. return _proto_TypeTraits::Get(id.number(), _extensions_, index); \
  1055. } \
  1056. \
  1057. template <typename _proto_TypeTraits, \
  1058. ::google::protobuf::internal::FieldType _field_type, \
  1059. bool _is_packed> \
  1060. inline typename _proto_TypeTraits::Repeated::MutableType MutableExtension( \
  1061. const ::google::protobuf::internal::ExtensionIdentifier< \
  1062. CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id, \
  1063. int index) { \
  1064. return _proto_TypeTraits::Mutable(id.number(), index, &_extensions_); \
  1065. } \
  1066. \
  1067. template <typename _proto_TypeTraits, \
  1068. ::google::protobuf::internal::FieldType _field_type, \
  1069. bool _is_packed> \
  1070. inline void SetExtension( \
  1071. const ::google::protobuf::internal::ExtensionIdentifier< \
  1072. CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id, \
  1073. int index, typename _proto_TypeTraits::Repeated::ConstType value) { \
  1074. _proto_TypeTraits::Set(id.number(), index, value, &_extensions_); \
  1075. } \
  1076. \
  1077. template <typename _proto_TypeTraits, \
  1078. ::google::protobuf::internal::FieldType _field_type, \
  1079. bool _is_packed> \
  1080. inline typename _proto_TypeTraits::Repeated::MutableType AddExtension( \
  1081. const ::google::protobuf::internal::ExtensionIdentifier< \
  1082. CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) { \
  1083. return _proto_TypeTraits::Add(id.number(), _field_type, &_extensions_); \
  1084. } \
  1085. \
  1086. template <typename _proto_TypeTraits, \
  1087. ::google::protobuf::internal::FieldType _field_type, \
  1088. bool _is_packed> \
  1089. inline void AddExtension( \
  1090. const ::google::protobuf::internal::ExtensionIdentifier< \
  1091. CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id, \
  1092. typename _proto_TypeTraits::Repeated::ConstType value) { \
  1093. _proto_TypeTraits::Add(id.number(), _field_type, _is_packed, \
  1094. value, &_extensions_); \
  1095. } \
  1096. \
  1097. template <typename _proto_TypeTraits, \
  1098. ::google::protobuf::internal::FieldType _field_type, \
  1099. bool _is_packed> \
  1100. inline const typename _proto_TypeTraits::Repeated::RepeatedFieldType& \
  1101. GetRepeatedExtension( \
  1102. const ::google::protobuf::internal::ExtensionIdentifier< \
  1103. CLASSNAME, _proto_TypeTraits, _field_type, \
  1104. _is_packed>& id) const { \
  1105. return _proto_TypeTraits::GetRepeated(id.number(), _extensions_); \
  1106. } \
  1107. \
  1108. template <typename _proto_TypeTraits, \
  1109. ::google::protobuf::internal::FieldType _field_type, \
  1110. bool _is_packed> \
  1111. inline typename _proto_TypeTraits::Repeated::RepeatedFieldType* \
  1112. MutableRepeatedExtension( \
  1113. const ::google::protobuf::internal::ExtensionIdentifier< \
  1114. CLASSNAME, _proto_TypeTraits, _field_type, \
  1115. _is_packed>& id) { \
  1116. return _proto_TypeTraits::MutableRepeated(id.number(), _field_type, \
  1117. _is_packed, &_extensions_); \
  1118. }
  1119. } // namespace internal
  1120. } // namespace protobuf
  1121. } // namespace google
  1122. #endif // GOOGLE_PROTOBUF_EXTENSION_SET_H__