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