extension_set.h 71 KB

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