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