| 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: [email protected] (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 InternalMetadata; |
| 83 |
|
| 84 |
// Used to store values of type WireFormatLite::FieldType without having to |
| 85 |
// #include wire_format_lite.h. Also, ensures that we use only one byte to |
| 86 |
// store these values, which is important to keep the layout of |
| 87 |
// ExtensionSet::Extension small. |
| 88 |
typedef uint8 FieldType; |
| 89 |
|
| 90 |
// A function which, given an integer value, returns true if the number |
| 91 |
// matches one of the defined values for the corresponding enum type. This |
| 92 |
// is used with RegisterEnumExtension, below. |
| 93 |
typedef bool EnumValidityFunc(int number); |
| 94 |
|
| 95 |
// Version of the above which takes an argument. This is needed to deal with |
| 96 |
// extensions that are not compiled in. |
| 97 |
typedef bool EnumValidityFuncWithArg(const void* arg, int number); |
| 98 |
|
| 99 |
// Information about a registered extension. |
| 100 |
struct ExtensionInfo { |
| 101 |
inline ExtensionInfo() {} |
| 102 |
inline ExtensionInfo(FieldType type_param, bool isrepeated, bool ispacked) |
| 103 |
: type(type_param), |
| 104 |
is_repeated(isrepeated), |
| 105 |
is_packed(ispacked), |
| 106 |
descriptor(NULL) {} |
| 107 |
|
| 108 |
FieldType type; |
| 109 |
bool is_repeated; |
| 110 |
bool is_packed; |
| 111 |
|
| 112 |
struct EnumValidityCheck { |
| 113 |
EnumValidityFuncWithArg* func; |
| 114 |
const void* arg; |
| 115 |
}; |
| 116 |
|
| 117 |
struct MessageInfo { |
| 118 |
const MessageLite* prototype; |
| 119 |
}; |
| 120 |
|
| 121 |
union { |
| 122 |
EnumValidityCheck enum_validity_check; |
| 123 |
MessageInfo message_info; |
| 124 |
}; |
| 125 |
|
| 126 |
// The descriptor for this extension, if one exists and is known. May be |
| 127 |
// NULL. Must not be NULL if the descriptor for the extension does not |
| 128 |
// live in the same pool as the descriptor for the containing type. |
| 129 |
const FieldDescriptor* descriptor; |
| 130 |
}; |
| 131 |
|
| 132 |
// Abstract interface for an object which looks up extension definitions. Used |
| 133 |
// when parsing. |
| 134 |
class PROTOBUF_EXPORT ExtensionFinder { |
| 135 |
public: |
| 136 |
virtual ~ExtensionFinder(); |
| 137 |
|
| 138 |
// Find the extension with the given containing type and number. |
| 139 |
virtual bool Find(int number, ExtensionInfo* output) = 0; |
| 140 |
}; |
| 141 |
|
| 142 |
// Implementation of ExtensionFinder which finds extensions defined in .proto |
| 143 |
// files which have been compiled into the binary. |
| 144 |
class PROTOBUF_EXPORT GeneratedExtensionFinder : public ExtensionFinder { |
| 145 |
public: |
| 146 |
GeneratedExtensionFinder(const MessageLite* containing_type) |
| 147 |
: containing_type_(containing_type) {} |
| 148 |
~GeneratedExtensionFinder() override {} |
| 149 |
|
| 150 |
// Returns true and fills in *output if found, otherwise returns false. |
| 151 |
bool Find(int number, ExtensionInfo* output) override; |
| 152 |
|
| 153 |
private: |
| 154 |
const MessageLite* containing_type_; |
| 155 |
}; |
| 156 |
|
| 157 |
// A FieldSkipper used for parsing MessageSet. |
| 158 |
class MessageSetFieldSkipper; |
| 159 |
|
| 160 |
// Note: extension_set_heavy.cc defines DescriptorPoolExtensionFinder for |
| 161 |
// finding extensions from a DescriptorPool. |
| 162 |
|
| 163 |
// This is an internal helper class intended for use within the protocol buffer |
| 164 |
// library and generated classes. Clients should not use it directly. Instead, |
| 165 |
// use the generated accessors such as GetExtension() of the class being |
| 166 |
// extended. |
| 167 |
// |
| 168 |
// This class manages extensions for a protocol message object. The |
| 169 |
// message's HasExtension(), GetExtension(), MutableExtension(), and |
| 170 |
// ClearExtension() methods are just thin wrappers around the embedded |
| 171 |
// ExtensionSet. When parsing, if a tag number is encountered which is |
| 172 |
// inside one of the message type's extension ranges, the tag is passed |
| 173 |
// off to the ExtensionSet for parsing. Etc. |
| 174 |
class PROTOBUF_EXPORT ExtensionSet { |
| 175 |
public: |
| 176 |
constexpr ExtensionSet(); |
| 177 |
explicit ExtensionSet(Arena* arena); |
| 178 |
~ExtensionSet(); |
| 179 |
|
| 180 |
// These are called at startup by protocol-compiler-generated code to |
| 181 |
// register known extensions. The registrations are used by ParseField() |
| 182 |
// to look up extensions for parsed field numbers. Note that dynamic parsing |
| 183 |
// does not use ParseField(); only protocol-compiler-generated parsing |
| 184 |
// methods do. |
| 185 |
static void RegisterExtension(const MessageLite* containing_type, int number, |
| 186 |
FieldType type, bool is_repeated, |
| 187 |
bool is_packed); |
| 188 |
static void RegisterEnumExtension(const MessageLite* containing_type, |
| 189 |
int number, FieldType type, |
| 190 |
bool is_repeated, bool is_packed, |
| 191 |
EnumValidityFunc* is_valid); |
| 192 |
static void RegisterMessageExtension(const MessageLite* containing_type, |
| 193 |
int number, FieldType type, |
| 194 |
bool is_repeated, bool is_packed, |
| 195 |
const MessageLite* prototype); |
| 196 |
|
| 197 |
// ================================================================= |
| 198 |
|
| 199 |
// Add all fields which are currently present to the given vector. This |
| 200 |
// is useful to implement Reflection::ListFields(). |
| 201 |
void AppendToList(const Descriptor* containing_type, |
| 202 |
const DescriptorPool* pool, |
| 203 |
std::vector<const FieldDescriptor*>* output) const; |
| 204 |
|
| 205 |
// ================================================================= |
| 206 |
// Accessors |
| 207 |
// |
| 208 |
// Generated message classes include type-safe templated wrappers around |
| 209 |
// these methods. Generally you should use those rather than call these |
| 210 |
// directly, unless you are doing low-level memory management. |
| 211 |
// |
| 212 |
// When calling any of these accessors, the extension number requested |
| 213 |
// MUST exist in the DescriptorPool provided to the constructor. Otherwise, |
| 214 |
// the method will fail an assert. Normally, though, you would not call |
| 215 |
// these directly; you would either call the generated accessors of your |
| 216 |
// message class (e.g. GetExtension()) or you would call the accessors |
| 217 |
// of the reflection interface. In both cases, it is impossible to |
| 218 |
// trigger this assert failure: the generated accessors only accept |
| 219 |
// linked-in extension types as parameters, while the Reflection interface |
| 220 |
// requires you to provide the FieldDescriptor describing the extension. |
| 221 |
// |
| 222 |
// When calling any of these accessors, a protocol-compiler-generated |
| 223 |
// implementation of the extension corresponding to the number MUST |
| 224 |
// be linked in, and the FieldDescriptor used to refer to it MUST be |
| 225 |
// the one generated by that linked-in code. Otherwise, the method will |
| 226 |
// die on an assert failure. The message objects returned by the message |
| 227 |
// accessors are guaranteed to be of the correct linked-in type. |
| 228 |
// |
| 229 |
// These methods pretty much match Reflection except that: |
| 230 |
// - They're not virtual. |
| 231 |
// - They identify fields by number rather than FieldDescriptors. |
| 232 |
// - They identify enum values using integers rather than descriptors. |
| 233 |
// - Strings provide Mutable() in addition to Set() accessors. |
| 234 |
|
| 235 |
bool Has(int number) const; |
| 236 |
int ExtensionSize(int number) const; // Size of a repeated extension. |
| 237 |
int NumExtensions() const; // The number of extensions |
| 238 |
FieldType ExtensionType(int number) const; |
| 239 |
void ClearExtension(int number); |
| 240 |
|
| 241 |
// singular fields ------------------------------------------------- |
| 242 |
|
| 243 |
int32 GetInt32(int number, int32 default_value) const; |
| 244 |
int64 GetInt64(int number, int64 default_value) const; |
| 245 |
uint32 GetUInt32(int number, uint32 default_value) const; |
| 246 |
uint64 GetUInt64(int number, uint64 default_value) const; |
| 247 |
float GetFloat(int number, float default_value) const; |
| 248 |
double GetDouble(int number, double default_value) const; |
| 249 |
bool GetBool(int number, bool default_value) const; |
| 250 |
int GetEnum(int number, int default_value) const; |
| 251 |
const std::string& GetString(int number, |
| 252 |
const std::string& default_value) const; |
| 253 |
const MessageLite& GetMessage(int number, |
| 254 |
const MessageLite& default_value) const; |
| 255 |
const MessageLite& GetMessage(int number, const Descriptor* message_type, |
| 256 |
MessageFactory* factory) const; |
| 257 |
|
| 258 |
// |descriptor| may be NULL so long as it is known that the descriptor for |
| 259 |
// the extension lives in the same pool as the descriptor for the containing |
| 260 |
// type. |
| 261 |
#define desc const FieldDescriptor* descriptor // avoid line wrapping |
| 262 |
void SetInt32(int number, FieldType type, int32 value, desc); |
| 263 |
void SetInt64(int number, FieldType type, int64 value, desc); |
| 264 |
void SetUInt32(int number, FieldType type, uint32 value, desc); |
| 265 |
void SetUInt64(int number, FieldType type, uint64 value, desc); |
| 266 |
void SetFloat(int number, FieldType type, float value, desc); |
| 267 |
void SetDouble(int number, FieldType type, double value, desc); |
| 268 |
void SetBool(int number, FieldType type, bool value, desc); |
| 269 |
void SetEnum(int number, FieldType type, int value, desc); |
| 270 |
void SetString(int number, FieldType type, std::string value, desc); |
| 271 |
std::string* MutableString(int number, FieldType type, desc); |
| 272 |
MessageLite* MutableMessage(int number, FieldType type, |
| 273 |
const MessageLite& prototype, desc); |
| 274 |
MessageLite* MutableMessage(const FieldDescriptor* descriptor, |
| 275 |
MessageFactory* factory); |
| 276 |
// Adds the given message to the ExtensionSet, taking ownership of the |
| 277 |
// message object. Existing message with the same number will be deleted. |
| 278 |
// If "message" is NULL, this is equivalent to "ClearExtension(number)". |
| 279 |
void SetAllocatedMessage(int number, FieldType type, |
| 280 |
const FieldDescriptor* descriptor, |
| 281 |
MessageLite* message); |
| 282 |
void UnsafeArenaSetAllocatedMessage(int number, FieldType type, |
| 283 |
const FieldDescriptor* descriptor, |
| 284 |
MessageLite* message); |
| 285 |
MessageLite* ReleaseMessage(int number, const MessageLite& prototype); |
| 286 |
MessageLite* UnsafeArenaReleaseMessage(int number, |
| 287 |
const MessageLite& prototype); |
| 288 |
|
| 289 |
MessageLite* ReleaseMessage(const FieldDescriptor* descriptor, |
| 290 |
MessageFactory* factory); |
| 291 |
MessageLite* UnsafeArenaReleaseMessage(const FieldDescriptor* descriptor, |
| 292 |
MessageFactory* factory); |
| 293 |
#undef desc |
| 294 |
Arena* GetArena() const { return arena_; } |
| 295 |
|
| 296 |
// repeated fields ------------------------------------------------- |
| 297 |
|
| 298 |
// Fetches a RepeatedField extension by number; returns |default_value| |
| 299 |
// if no such extension exists. User should not touch this directly; it is |
| 300 |
// used by the GetRepeatedExtension() method. |
| 301 |
const void* GetRawRepeatedField(int number, const void* default_value) const; |
| 302 |
// Fetches a mutable version of a RepeatedField extension by number, |
| 303 |
// instantiating one if none exists. Similar to above, user should not use |
| 304 |
// this directly; it underlies MutableRepeatedExtension(). |
| 305 |
void* MutableRawRepeatedField(int number, FieldType field_type, bool packed, |
| 306 |
const FieldDescriptor* desc); |
| 307 |
|
| 308 |
// This is an overload of MutableRawRepeatedField to maintain compatibility |
| 309 |
// with old code using a previous API. This version of |
| 310 |
// MutableRawRepeatedField() will GOOGLE_CHECK-fail on a missing extension. |
| 311 |
// (E.g.: borg/clients/internal/proto1/proto2_reflection.cc.) |
| 312 |
void* MutableRawRepeatedField(int number); |
| 313 |
|
| 314 |
int32 GetRepeatedInt32(int number, int index) const; |
| 315 |
int64 GetRepeatedInt64(int number, int index) const; |
| 316 |
uint32 GetRepeatedUInt32(int number, int index) const; |
| 317 |
uint64 GetRepeatedUInt64(int number, int index) const; |
| 318 |
float GetRepeatedFloat(int number, int index) const; |
| 319 |
double GetRepeatedDouble(int number, int index) const; |
| 320 |
bool GetRepeatedBool(int number, int index) const; |
| 321 |
int GetRepeatedEnum(int number, int index) const; |
| 322 |
const std::string& GetRepeatedString(int number, int index) const; |
| 323 |
const MessageLite& GetRepeatedMessage(int number, int index) const; |
| 324 |
|
| 325 |
void SetRepeatedInt32(int number, int index, int32 value); |
| 326 |
void SetRepeatedInt64(int number, int index, int64 value); |
| 327 |
void SetRepeatedUInt32(int number, int index, uint32 value); |
| 328 |
void SetRepeatedUInt64(int number, int index, uint64 value); |
| 329 |
void SetRepeatedFloat(int number, int index, float value); |
| 330 |
void SetRepeatedDouble(int number, int index, double value); |
| 331 |
void SetRepeatedBool(int number, int index, bool value); |
| 332 |
void SetRepeatedEnum(int number, int index, int value); |
| 333 |
void SetRepeatedString(int number, int index, std::string value); |
| 334 |
std::string* MutableRepeatedString(int number, int index); |
| 335 |
MessageLite* MutableRepeatedMessage(int number, int index); |
| 336 |
|
| 337 |
#define desc const FieldDescriptor* descriptor // avoid line wrapping |
| 338 |
void AddInt32(int number, FieldType type, bool packed, int32 value, desc); |
| 339 |
void AddInt64(int number, FieldType type, bool packed, int64 value, desc); |
| 340 |
void AddUInt32(int number, FieldType type, bool packed, uint32 value, desc); |
| 341 |
void AddUInt64(int number, FieldType type, bool packed, uint64 value, desc); |
| 342 |
void AddFloat(int number, FieldType type, bool packed, float value, desc); |
| 343 |
void AddDouble(int number, FieldType type, bool packed, double value, desc); |
| 344 |
void AddBool(int number, FieldType type, bool packed, bool value, desc); |
| 345 |
void AddEnum(int number, FieldType type, bool packed, int value, desc); |
| 346 |
void AddString(int number, FieldType type, std::string value, desc); |
| 347 |
std::string* AddString(int number, FieldType type, desc); |
| 348 |
MessageLite* AddMessage(int number, FieldType type, |
| 349 |
const MessageLite& prototype, desc); |
| 350 |
MessageLite* AddMessage(const FieldDescriptor* descriptor, |
| 351 |
MessageFactory* factory); |
| 352 |
void AddAllocatedMessage(const FieldDescriptor* descriptor, |
| 353 |
MessageLite* new_entry); |
| 354 |
#undef desc |
| 355 |
|
| 356 |
void RemoveLast(int number); |
| 357 |
MessageLite* ReleaseLast(int number); |
| 358 |
void SwapElements(int number, int index1, int index2); |
| 359 |
|
| 360 |
// ----------------------------------------------------------------- |
| 361 |
// TODO(kenton): Hardcore memory management accessors |
| 362 |
|
| 363 |
// ================================================================= |
| 364 |
// convenience methods for implementing methods of Message |
| 365 |
// |
| 366 |
// These could all be implemented in terms of the other methods of this |
| 367 |
// class, but providing them here helps keep the generated code size down. |
| 368 |
|
| 369 |
void Clear(); |
| 370 |
void MergeFrom(const ExtensionSet& other); |
| 371 |
void Swap(ExtensionSet* other); |
| 372 |
void SwapExtension(ExtensionSet* other, int number); |
| 373 |
bool IsInitialized() const; |
| 374 |
|
| 375 |
// Parses a single extension from the input. The input should start out |
| 376 |
// positioned immediately after the tag. |
| 377 |
bool ParseField(uint32 tag, io::CodedInputStream* input, |
| 378 |
ExtensionFinder* extension_finder, |
| 379 |
FieldSkipper* field_skipper); |
| 380 |
|
| 381 |
// Specific versions for lite or full messages (constructs the appropriate |
| 382 |
// FieldSkipper automatically). |containing_type| is the default |
| 383 |
// instance for the containing message; it is used only to look up the |
| 384 |
// extension by number. See RegisterExtension(), above. Unlike the other |
| 385 |
// methods of ExtensionSet, this only works for generated message types -- |
| 386 |
// it looks up extensions registered using RegisterExtension(). |
| 387 |
bool ParseField(uint32 tag, io::CodedInputStream* input, |
| 388 |
const MessageLite* containing_type); |
| 389 |
bool ParseField(uint32 tag, io::CodedInputStream* input, |
| 390 |
const Message* containing_type, |
| 391 |
UnknownFieldSet* unknown_fields); |
| 392 |
bool ParseField(uint32 tag, io::CodedInputStream* input, |
| 393 |
const MessageLite* containing_type, |
| 394 |
io::CodedOutputStream* unknown_fields); |
| 395 |
|
| 396 |
// Lite parser |
| 397 |
const char* ParseField(uint64 tag, const char* ptr, |
| 398 |
const MessageLite* containing_type, |
| 399 |
internal::InternalMetadata* metadata, |
| 400 |
internal::ParseContext* ctx); |
| 401 |
// Full parser |
| 402 |
const char* ParseField(uint64 tag, const char* ptr, |
| 403 |
const Message* containing_type, |
| 404 |
internal::InternalMetadata* metadata, |
| 405 |
internal::ParseContext* ctx); |
| 406 |
template <typename Msg> |
| 407 |
const char* ParseMessageSet(const char* ptr, const Msg* containing_type, |
| 408 |
InternalMetadata* metadata, |
| 409 |
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 |
InternalMetadata* 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. |
| 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. |
| 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: |
| 516 |
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") |
| 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 { |
| 634 |
bool operator()(const KeyValue& lhs, const KeyValue& rhs) const { |
| 635 |
return lhs.first < rhs.first; |
| 636 |
} |
| 637 |
bool operator()(const KeyValue& lhs, int key) const { |
| 638 |
return lhs.first < key; |
| 639 |
} |
| 640 |
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; |
| 667 |
bool is_large() const { return flat_capacity_ > kMaximumFlatCapacity; } |
| 668 |
|
| 669 |
// Removes a key from the ExtensionSet. |
| 670 |
void Erase(int key); |
| 671 |
|
| 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> |
| 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> |
| 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> |
| 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 |
|
| 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 |
| 773 |
const char* ParseFieldMaybeLazily(uint64 tag, const char* ptr, |
| 774 |
const MessageLite* containing_type, |
| 775 |
internal::InternalMetadata* 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(uint64 tag, const char* ptr, |
| 781 |
const Message* containing_type, |
| 782 |
internal::InternalMetadata* metadata, |
| 783 |
internal::ParseContext* ctx); |
| 784 |
const char* ParseMessageSetItem(const char* ptr, |
| 785 |
const MessageLite* containing_type, |
| 786 |
internal::InternalMetadata* metadata, |
| 787 |
internal::ParseContext* ctx); |
| 788 |
const char* ParseMessageSetItem(const char* ptr, |
| 789 |
const Message* containing_type, |
| 790 |
internal::InternalMetadata* 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 |
internal::InternalMetadata* metadata, |
| 798 |
const char* ptr, |
| 799 |
internal::ParseContext* ctx); |
| 800 |
template <typename Msg, typename T> |
| 801 |
const char* ParseMessageSetItemTmpl(const char* ptr, |
| 802 |
const Msg* containing_type, |
| 803 |
internal::InternalMetadata* metadata, |
| 804 |
internal::ParseContext* ctx); |
| 805 |
|
| 806 |
// Hack: RepeatedPtrFieldBase declares ExtensionSet as a friend. This |
| 807 |
// friendship should automatically extend to ExtensionSet::Extension, but |
| 808 |
// unfortunately some older compilers (e.g. GCC 3.4.4) do not implement this |
| 809 |
// correctly. So, we must provide helpers for calling methods of that |
| 810 |
// class. |
| 811 |
|
| 812 |
// Defined in extension_set_heavy.cc. |
| 813 |
static inline size_t RepeatedMessage_SpaceUsedExcludingSelfLong( |
| 814 |
RepeatedPtrFieldBase* field); |
| 815 |
|
| 816 |
KeyValue* flat_begin() { |
| 817 |
assert(!is_large()); |
| 818 |
return map_.flat; |
| 819 |
} |
| 820 |
const KeyValue* flat_begin() const { |
| 821 |
assert(!is_large()); |
| 822 |
return map_.flat; |
| 823 |
} |
| 824 |
KeyValue* flat_end() { |
| 825 |
assert(!is_large()); |
| 826 |
return map_.flat + flat_size_; |
| 827 |
} |
| 828 |
const KeyValue* flat_end() const { |
| 829 |
assert(!is_large()); |
| 830 |
return map_.flat + flat_size_; |
| 831 |
} |
| 832 |
|
| 833 |
Arena* arena_; |
| 834 |
|
| 835 |
// Manual memory-management: |
| 836 |
// map_.flat is an allocated array of flat_capacity_ elements. |
| 837 |
// [map_.flat, map_.flat + flat_size_) is the currently-in-use prefix. |
| 838 |
uint16 flat_capacity_; |
| 839 |
uint16 flat_size_; |
| 840 |
union AllocatedData { |
| 841 |
KeyValue* flat; |
| 842 |
|
| 843 |
// If flat_capacity_ > kMaximumFlatCapacity, switch to LargeMap, |
| 844 |
// which guarantees O(n lg n) CPU but larger constant factors. |
| 845 |
LargeMap* large; |
| 846 |
} map_; |
| 847 |
|
| 848 |
static void DeleteFlatMap(const KeyValue* flat, uint16 flat_capacity); |
| 849 |
|
| 850 |
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ExtensionSet); |
| 851 |
}; |
| 852 |
|
| 853 |
constexpr ExtensionSet::ExtensionSet() |
| 854 |
: arena_(nullptr), flat_capacity_(0), flat_size_(0), map_{nullptr} {} |
| 855 |
|
| 856 |
// These are just for convenience... |
| 857 |
inline void ExtensionSet::SetString(int number, FieldType type, |
| 858 |
std::string value, |
| 859 |
const FieldDescriptor* descriptor) { |
| 860 |
MutableString(number, type, descriptor)->assign(std::move(value)); |
| 861 |
} |
| 862 |
inline void ExtensionSet::SetRepeatedString(int number, int index, |
| 863 |
std::string value) { |
| 864 |
MutableRepeatedString(number, index)->assign(std::move(value)); |
| 865 |
} |
| 866 |
inline void ExtensionSet::AddString(int number, FieldType type, |
| 867 |
std::string value, |
| 868 |
const FieldDescriptor* descriptor) { |
| 869 |
AddString(number, type, descriptor)->assign(std::move(value)); |
| 870 |
} |
| 871 |
// =================================================================== |
| 872 |
// Glue for generated extension accessors |
| 873 |
|
| 874 |
// ------------------------------------------------------------------- |
| 875 |
// Template magic |
| 876 |
|
| 877 |
// First we have a set of classes representing "type traits" for different |
| 878 |
// field types. A type traits class knows how to implement basic accessors |
| 879 |
// for extensions of a particular type given an ExtensionSet. The signature |
| 880 |
// for a type traits class looks like this: |
| 881 |
// |
| 882 |
// class TypeTraits { |
| 883 |
// public: |
| 884 |
// typedef ? ConstType; |
| 885 |
// typedef ? MutableType; |
| 886 |
// // TypeTraits for singular fields and repeated fields will define the |
| 887 |
// // symbol "Singular" or "Repeated" respectively. These two symbols will |
| 888 |
// // be used in extension accessors to distinguish between singular |
| 889 |
// // extensions and repeated extensions. If the TypeTraits for the passed |
| 890 |
// // in extension doesn't have the expected symbol defined, it means the |
| 891 |
// // user is passing a repeated extension to a singular accessor, or the |
| 892 |
// // opposite. In that case the C++ compiler will generate an error |
| 893 |
// // message "no matching member function" to inform the user. |
| 894 |
// typedef ? Singular |
| 895 |
// typedef ? Repeated |
| 896 |
// |
| 897 |
// static inline ConstType Get(int number, const ExtensionSet& set); |
| 898 |
// static inline void Set(int number, ConstType value, ExtensionSet* set); |
| 899 |
// static inline MutableType Mutable(int number, ExtensionSet* set); |
| 900 |
// |
| 901 |
// // Variants for repeated fields. |
| 902 |
// static inline ConstType Get(int number, const ExtensionSet& set, |
| 903 |
// int index); |
| 904 |
// static inline void Set(int number, int index, |
| 905 |
// ConstType value, ExtensionSet* set); |
| 906 |
// static inline MutableType Mutable(int number, int index, |
| 907 |
// ExtensionSet* set); |
| 908 |
// static inline void Add(int number, ConstType value, ExtensionSet* set); |
| 909 |
// static inline MutableType Add(int number, ExtensionSet* set); |
| 910 |
// This is used by the ExtensionIdentifier constructor to register |
| 911 |
// the extension at dynamic initialization. |
| 912 |
// template <typename ExtendeeT> |
| 913 |
// static void Register(int number, FieldType type, bool is_packed); |
| 914 |
// }; |
| 915 |
// |
| 916 |
// Not all of these methods make sense for all field types. For example, the |
| 917 |
// "Mutable" methods only make sense for strings and messages, and the |
| 918 |
// repeated methods only make sense for repeated types. So, each type |
| 919 |
// traits class implements only the set of methods from this signature that it |
| 920 |
// actually supports. This will cause a compiler error if the user tries to |
| 921 |
// access an extension using a method that doesn't make sense for its type. |
| 922 |
// For example, if "foo" is an extension of type "optional int32", then if you |
| 923 |
// try to write code like: |
| 924 |
// my_message.MutableExtension(foo) |
| 925 |
// you will get a compile error because PrimitiveTypeTraits<int32> does not |
| 926 |
// have a "Mutable()" method. |
| 927 |
|
| 928 |
// ------------------------------------------------------------------- |
| 929 |
// PrimitiveTypeTraits |
| 930 |
|
| 931 |
// Since the ExtensionSet has different methods for each primitive type, |
| 932 |
// we must explicitly define the methods of the type traits class for each |
| 933 |
// known type. |
| 934 |
template <typename Type> |
| 935 |
class PrimitiveTypeTraits { |
| 936 |
public: |
| 937 |
typedef Type ConstType; |
| 938 |
typedef Type MutableType; |
| 939 |
typedef PrimitiveTypeTraits<Type> Singular; |
| 940 |
|
| 941 |
static inline ConstType Get(int number, const ExtensionSet& set, |
| 942 |
ConstType default_value); |
| 943 |
static inline void Set(int number, FieldType field_type, ConstType value, |
| 944 |
ExtensionSet* set); |
| 945 |
template <typename ExtendeeT> |
| 946 |
static void Register(int number, FieldType type, bool is_packed) { |
| 947 |
ExtensionSet::RegisterExtension(&ExtendeeT::default_instance(), number, |
| 948 |
type, false, is_packed); |
| 949 |
} |
| 950 |
}; |
| 951 |
|
| 952 |
template <typename Type> |
| 953 |
class RepeatedPrimitiveTypeTraits { |
| 954 |
public: |
| 955 |
typedef Type ConstType; |
| 956 |
typedef Type MutableType; |
| 957 |
typedef RepeatedPrimitiveTypeTraits<Type> Repeated; |
| 958 |
|
| 959 |
typedef RepeatedField<Type> RepeatedFieldType; |
| 960 |
|
| 961 |
static inline Type Get(int number, const ExtensionSet& set, int index); |
| 962 |
static inline void Set(int number, int index, Type value, ExtensionSet* set); |
| 963 |
static inline void Add(int number, FieldType field_type, bool is_packed, |
| 964 |
Type value, ExtensionSet* set); |
| 965 |
|
| 966 |
static inline const RepeatedField<ConstType>& GetRepeated( |
| 967 |
int number, const ExtensionSet& set); |
| 968 |
static inline RepeatedField<Type>* MutableRepeated(int number, |
| 969 |
FieldType field_type, |
| 970 |
bool is_packed, |
| 971 |
ExtensionSet* set); |
| 972 |
|
| 973 |
static const RepeatedFieldType* GetDefaultRepeatedField(); |
| 974 |
template <typename ExtendeeT> |
| 975 |
static void Register(int number, FieldType type, bool is_packed) { |
| 976 |
ExtensionSet::RegisterExtension(&ExtendeeT::default_instance(), number, |
| 977 |
type, true, is_packed); |
| 978 |
} |
| 979 |
}; |
| 980 |
|
| 981 |
class PROTOBUF_EXPORT RepeatedPrimitiveDefaults { |
| 982 |
private: |
| 983 |
template <typename Type> |
| 984 |
friend class RepeatedPrimitiveTypeTraits; |
| 985 |
static const RepeatedPrimitiveDefaults* default_instance(); |
| 986 |
RepeatedField<int32> default_repeated_field_int32_; |
| 987 |
RepeatedField<int64> default_repeated_field_int64_; |
| 988 |
RepeatedField<uint32> default_repeated_field_uint32_; |
| 989 |
RepeatedField<uint64> default_repeated_field_uint64_; |
| 990 |
RepeatedField<double> default_repeated_field_double_; |
| 991 |
RepeatedField<float> default_repeated_field_float_; |
| 992 |
RepeatedField<bool> default_repeated_field_bool_; |
| 993 |
}; |
| 994 |
|
| 995 |
#define PROTOBUF_DEFINE_PRIMITIVE_TYPE(TYPE, METHOD) \ |
| 996 |
template <> \ |
| 997 |
inline TYPE PrimitiveTypeTraits<TYPE>::Get( \ |
| 998 |
int number, const ExtensionSet& set, TYPE default_value) { \ |
| 999 |
return set.Get##METHOD(number, default_value); \ |
| 1000 |
} \ |
| 1001 |
template <> \ |
| 1002 |
inline void PrimitiveTypeTraits<TYPE>::Set(int number, FieldType field_type, \ |
| 1003 |
TYPE value, ExtensionSet* set) { \ |
| 1004 |
set->Set##METHOD(number, field_type, value, NULL); \ |
| 1005 |
} \ |
| 1006 |
\ |
| 1007 |
template <> \ |
| 1008 |
inline TYPE RepeatedPrimitiveTypeTraits<TYPE>::Get( \ |
| 1009 |
int number, const ExtensionSet& set, int index) { \ |
| 1010 |
return set.GetRepeated##METHOD(number, index); \ |
| 1011 |
} \ |
| 1012 |
template <> \ |
| 1013 |
inline void RepeatedPrimitiveTypeTraits<TYPE>::Set( \ |
| 1014 |
int number, int index, TYPE value, ExtensionSet* set) { \ |
| 1015 |
set->SetRepeated##METHOD(number, index, value); \ |
| 1016 |
} \ |
| 1017 |
template <> \ |
| 1018 |
inline void RepeatedPrimitiveTypeTraits<TYPE>::Add( \ |
| 1019 |
int number, FieldType field_type, bool is_packed, TYPE value, \ |
| 1020 |
ExtensionSet* set) { \ |
| 1021 |
set->Add##METHOD(number, field_type, is_packed, value, NULL); \ |
| 1022 |
} \ |
| 1023 |
template <> \ |
| 1024 |
inline const RepeatedField<TYPE>* \ |
| 1025 |
RepeatedPrimitiveTypeTraits<TYPE>::GetDefaultRepeatedField() { \ |
| 1026 |
return &RepeatedPrimitiveDefaults::default_instance() \ |
| 1027 |
->default_repeated_field_##TYPE##_; \ |
| 1028 |
} \ |
| 1029 |
template <> \ |
| 1030 |
inline const RepeatedField<TYPE>& \ |
| 1031 |
RepeatedPrimitiveTypeTraits<TYPE>::GetRepeated(int number, \ |
| 1032 |
const ExtensionSet& set) { \ |
| 1033 |
return *reinterpret_cast<const RepeatedField<TYPE>*>( \ |
| 1034 |
set.GetRawRepeatedField(number, GetDefaultRepeatedField())); \ |
| 1035 |
} \ |
| 1036 |
template <> \ |
| 1037 |
inline RepeatedField<TYPE>* \ |
| 1038 |
RepeatedPrimitiveTypeTraits<TYPE>::MutableRepeated( \ |
| 1039 |
int number, FieldType field_type, bool is_packed, ExtensionSet* set) { \ |
| 1040 |
return reinterpret_cast<RepeatedField<TYPE>*>( \ |
| 1041 |
set->MutableRawRepeatedField(number, field_type, is_packed, NULL)); \ |
| 1042 |
} |
| 1043 |
|
| 1044 |
PROTOBUF_DEFINE_PRIMITIVE_TYPE(int32, Int32) |
| 1045 |
PROTOBUF_DEFINE_PRIMITIVE_TYPE(int64, Int64) |
| 1046 |
PROTOBUF_DEFINE_PRIMITIVE_TYPE(uint32, UInt32) |
| 1047 |
PROTOBUF_DEFINE_PRIMITIVE_TYPE(uint64, UInt64) |
| 1048 |
PROTOBUF_DEFINE_PRIMITIVE_TYPE(float, Float) |
| 1049 |
PROTOBUF_DEFINE_PRIMITIVE_TYPE(double, Double) |
| 1050 |
PROTOBUF_DEFINE_PRIMITIVE_TYPE(bool, Bool) |
| 1051 |
|
| 1052 |
#undef PROTOBUF_DEFINE_PRIMITIVE_TYPE |
| 1053 |
|
| 1054 |
// ------------------------------------------------------------------- |
| 1055 |
// StringTypeTraits |
| 1056 |
|
| 1057 |
// Strings support both Set() and Mutable(). |
| 1058 |
class PROTOBUF_EXPORT StringTypeTraits { |
| 1059 |
public: |
| 1060 |
typedef const std::string& ConstType; |
| 1061 |
typedef std::string* MutableType; |
| 1062 |
typedef StringTypeTraits Singular; |
| 1063 |
|
| 1064 |
static inline const std::string& Get(int number, const ExtensionSet& set, |
| 1065 |
ConstType default_value) { |
| 1066 |
return set.GetString(number, default_value); |
| 1067 |
} |
| 1068 |
static inline void Set(int number, FieldType field_type, |
| 1069 |
const std::string& value, ExtensionSet* set) { |
| 1070 |
set->SetString(number, field_type, value, NULL); |
| 1071 |
} |
| 1072 |
static inline std::string* Mutable(int number, FieldType field_type, |
| 1073 |
ExtensionSet* set) { |
| 1074 |
return set->MutableString(number, field_type, NULL); |
| 1075 |
} |
| 1076 |
template <typename ExtendeeT> |
| 1077 |
static void Register(int number, FieldType type, bool is_packed) { |
| 1078 |
ExtensionSet::RegisterExtension(&ExtendeeT::default_instance(), number, |
| 1079 |
type, false, is_packed); |
| 1080 |
} |
| 1081 |
}; |
| 1082 |
|
| 1083 |
class PROTOBUF_EXPORT RepeatedStringTypeTraits { |
| 1084 |
public: |
| 1085 |
typedef const std::string& ConstType; |
| 1086 |
typedef std::string* MutableType; |
| 1087 |
typedef RepeatedStringTypeTraits Repeated; |
| 1088 |
|
| 1089 |
typedef RepeatedPtrField<std::string> RepeatedFieldType; |
| 1090 |
|
| 1091 |
static inline const std::string& Get(int number, const ExtensionSet& set, |
| 1092 |
int index) { |
| 1093 |
return set.GetRepeatedString(number, index); |
| 1094 |
} |
| 1095 |
static inline void Set(int number, int index, const std::string& value, |
| 1096 |
ExtensionSet* set) { |
| 1097 |
set->SetRepeatedString(number, index, value); |
| 1098 |
} |
| 1099 |
static inline std::string* Mutable(int number, int index, ExtensionSet* set) { |
| 1100 |
return set->MutableRepeatedString(number, index); |
| 1101 |
} |
| 1102 |
static inline void Add(int number, FieldType field_type, bool /*is_packed*/, |
| 1103 |
const std::string& value, ExtensionSet* set) { |
| 1104 |
set->AddString(number, field_type, value, NULL); |
| 1105 |
} |
| 1106 |
static inline std::string* Add(int number, FieldType field_type, |
| 1107 |
ExtensionSet* set) { |
| 1108 |
return set->AddString(number, field_type, NULL); |
| 1109 |
} |
| 1110 |
static inline const RepeatedPtrField<std::string>& GetRepeated( |
| 1111 |
int number, const ExtensionSet& set) { |
| 1112 |
return *reinterpret_cast<const RepeatedPtrField<std::string>*>( |
| 1113 |
set.GetRawRepeatedField(number, GetDefaultRepeatedField())); |
| 1114 |
} |
| 1115 |
|
| 1116 |
static inline RepeatedPtrField<std::string>* MutableRepeated( |
| 1117 |
int number, FieldType field_type, bool is_packed, ExtensionSet* set) { |
| 1118 |
return reinterpret_cast<RepeatedPtrField<std::string>*>( |
| 1119 |
set->MutableRawRepeatedField(number, field_type, is_packed, NULL)); |
| 1120 |
} |
| 1121 |
|
| 1122 |
static const RepeatedFieldType* GetDefaultRepeatedField(); |
| 1123 |
|
| 1124 |
template <typename ExtendeeT> |
| 1125 |
static void Register(int number, FieldType type, bool is_packed) { |
| 1126 |
ExtensionSet::RegisterExtension(&ExtendeeT::default_instance(), number, |
| 1127 |
type, true, is_packed); |
| 1128 |
} |
| 1129 |
|
| 1130 |
private: |
| 1131 |
static void InitializeDefaultRepeatedFields(); |
| 1132 |
static void DestroyDefaultRepeatedFields(); |
| 1133 |
}; |
| 1134 |
|
| 1135 |
// ------------------------------------------------------------------- |
| 1136 |
// EnumTypeTraits |
| 1137 |
|
| 1138 |
// ExtensionSet represents enums using integers internally, so we have to |
| 1139 |
// static_cast around. |
| 1140 |
template <typename Type, bool IsValid(int)> |
| 1141 |
class EnumTypeTraits { |
| 1142 |
public: |
| 1143 |
typedef Type ConstType; |
| 1144 |
typedef Type MutableType; |
| 1145 |
typedef EnumTypeTraits<Type, IsValid> Singular; |
| 1146 |
|
| 1147 |
static inline ConstType Get(int number, const ExtensionSet& set, |
| 1148 |
ConstType default_value) { |
| 1149 |
return static_cast<Type>(set.GetEnum(number, default_value)); |
| 1150 |
} |
| 1151 |
static inline void Set(int number, FieldType field_type, ConstType value, |
| 1152 |
ExtensionSet* set) { |
| 1153 |
GOOGLE_DCHECK(IsValid(value)); |
| 1154 |
set->SetEnum(number, field_type, value, NULL); |
| 1155 |
} |
| 1156 |
template <typename ExtendeeT> |
| 1157 |
static void Register(int number, FieldType type, bool is_packed) { |
| 1158 |
ExtensionSet::RegisterEnumExtension(&ExtendeeT::default_instance(), number, |
| 1159 |
type, false, is_packed, IsValid); |
| 1160 |
} |
| 1161 |
}; |
| 1162 |
|
| 1163 |
template <typename Type, bool IsValid(int)> |
| 1164 |
class RepeatedEnumTypeTraits { |
| 1165 |
public: |
| 1166 |
typedef Type ConstType; |
| 1167 |
typedef Type MutableType; |
| 1168 |
typedef RepeatedEnumTypeTraits<Type, IsValid> Repeated; |
| 1169 |
|
| 1170 |
typedef RepeatedField<Type> RepeatedFieldType; |
| 1171 |
|
| 1172 |
static inline ConstType Get(int number, const ExtensionSet& set, int index) { |
| 1173 |
return static_cast<Type>(set.GetRepeatedEnum(number, index)); |
| 1174 |
} |
| 1175 |
static inline void Set(int number, int index, ConstType value, |
| 1176 |
ExtensionSet* set) { |
| 1177 |
GOOGLE_DCHECK(IsValid(value)); |
| 1178 |
set->SetRepeatedEnum(number, index, value); |
| 1179 |
} |
| 1180 |
static inline void Add(int number, FieldType field_type, bool is_packed, |
| 1181 |
ConstType value, ExtensionSet* set) { |
| 1182 |
GOOGLE_DCHECK(IsValid(value)); |
| 1183 |
set->AddEnum(number, field_type, is_packed, value, NULL); |
| 1184 |
} |
| 1185 |
static inline const RepeatedField<Type>& GetRepeated( |
| 1186 |
int number, const ExtensionSet& set) { |
| 1187 |
// Hack: the `Extension` struct stores a RepeatedField<int> for enums. |
| 1188 |
// RepeatedField<int> cannot implicitly convert to RepeatedField<EnumType> |
| 1189 |
// so we need to do some casting magic. See message.h for similar |
| 1190 |
// contortions for non-extension fields. |
| 1191 |
return *reinterpret_cast<const RepeatedField<Type>*>( |
| 1192 |
set.GetRawRepeatedField(number, GetDefaultRepeatedField())); |
| 1193 |
} |
| 1194 |
|
| 1195 |
static inline RepeatedField<Type>* MutableRepeated(int number, |
| 1196 |
FieldType field_type, |
| 1197 |
bool is_packed, |
| 1198 |
ExtensionSet* set) { |
| 1199 |
return reinterpret_cast<RepeatedField<Type>*>( |
| 1200 |
set->MutableRawRepeatedField(number, field_type, is_packed, NULL)); |
| 1201 |
} |
| 1202 |
|
| 1203 |
static const RepeatedFieldType* GetDefaultRepeatedField() { |
| 1204 |
// Hack: as noted above, repeated enum fields are internally stored as a |
| 1205 |
// RepeatedField<int>. We need to be able to instantiate global static |
| 1206 |
// objects to return as default (empty) repeated fields on non-existent |
| 1207 |
// extensions. We would not be able to know a-priori all of the enum types |
| 1208 |
// (values of |Type|) to instantiate all of these, so we just re-use int32's |
| 1209 |
// default repeated field object. |
| 1210 |
return reinterpret_cast<const RepeatedField<Type>*>( |
| 1211 |
RepeatedPrimitiveTypeTraits<int32>::GetDefaultRepeatedField()); |
| 1212 |
} |
| 1213 |
template <typename ExtendeeT> |
| 1214 |
static void Register(int number, FieldType type, bool is_packed) { |
| 1215 |
ExtensionSet::RegisterEnumExtension(&ExtendeeT::default_instance(), number, |
| 1216 |
type, true, is_packed, IsValid); |
| 1217 |
} |
| 1218 |
}; |
| 1219 |
|
| 1220 |
// ------------------------------------------------------------------- |
| 1221 |
// MessageTypeTraits |
| 1222 |
|
| 1223 |
// ExtensionSet guarantees that when manipulating extensions with message |
| 1224 |
// types, the implementation used will be the compiled-in class representing |
| 1225 |
// that type. So, we can static_cast down to the exact type we expect. |
| 1226 |
template <typename Type> |
| 1227 |
class MessageTypeTraits { |
| 1228 |
public: |
| 1229 |
typedef const Type& ConstType; |
| 1230 |
typedef Type* MutableType; |
| 1231 |
typedef MessageTypeTraits<Type> Singular; |
| 1232 |
|
| 1233 |
static inline ConstType Get(int number, const ExtensionSet& set, |
| 1234 |
ConstType default_value) { |
| 1235 |
return static_cast<const Type&>(set.GetMessage(number, default_value)); |
| 1236 |
} |
| 1237 |
static inline MutableType Mutable(int number, FieldType field_type, |
| 1238 |
ExtensionSet* set) { |
| 1239 |
return static_cast<Type*>(set->MutableMessage( |
| 1240 |
number, field_type, Type::default_instance(), NULL)); |
| 1241 |
} |
| 1242 |
static inline void SetAllocated(int number, FieldType field_type, |
| 1243 |
MutableType message, ExtensionSet* set) { |
| 1244 |
set->SetAllocatedMessage(number, field_type, NULL, message); |
| 1245 |
} |
| 1246 |
static inline void UnsafeArenaSetAllocated(int number, FieldType field_type, |
| 1247 |
MutableType message, |
| 1248 |
ExtensionSet* set) { |
| 1249 |
set->UnsafeArenaSetAllocatedMessage(number, field_type, NULL, message); |
| 1250 |
} |
| 1251 |
static inline MutableType Release(int number, FieldType /* field_type */, |
| 1252 |
ExtensionSet* set) { |
| 1253 |
return static_cast<Type*>( |
| 1254 |
set->ReleaseMessage(number, Type::default_instance())); |
| 1255 |
} |
| 1256 |
static inline MutableType UnsafeArenaRelease(int number, |
| 1257 |
FieldType /* field_type */, |
| 1258 |
ExtensionSet* set) { |
| 1259 |
return static_cast<Type*>( |
| 1260 |
set->UnsafeArenaReleaseMessage(number, Type::default_instance())); |
| 1261 |
} |
| 1262 |
template <typename ExtendeeT> |
| 1263 |
static void Register(int number, FieldType type, bool is_packed) { |
| 1264 |
ExtensionSet::RegisterMessageExtension(&ExtendeeT::default_instance(), |
| 1265 |
number, type, false, is_packed, |
| 1266 |
&Type::default_instance()); |
| 1267 |
} |
| 1268 |
}; |
| 1269 |
|
| 1270 |
// forward declaration |
| 1271 |
class RepeatedMessageGenericTypeTraits; |
| 1272 |
|
| 1273 |
template <typename Type> |
| 1274 |
class RepeatedMessageTypeTraits { |
| 1275 |
public: |
| 1276 |
typedef const Type& ConstType; |
| 1277 |
typedef Type* MutableType; |
| 1278 |
typedef RepeatedMessageTypeTraits<Type> Repeated; |
| 1279 |
|
| 1280 |
typedef RepeatedPtrField<Type> RepeatedFieldType; |
| 1281 |
|
| 1282 |
static inline ConstType Get(int number, const ExtensionSet& set, int index) { |
| 1283 |
return static_cast<const Type&>(set.GetRepeatedMessage(number, index)); |
| 1284 |
} |
| 1285 |
static inline MutableType Mutable(int number, int index, ExtensionSet* set) { |
| 1286 |
return static_cast<Type*>(set->MutableRepeatedMessage(number, index)); |
| 1287 |
} |
| 1288 |
static inline MutableType Add(int number, FieldType field_type, |
| 1289 |
ExtensionSet* set) { |
| 1290 |
return static_cast<Type*>( |
| 1291 |
set->AddMessage(number, field_type, Type::default_instance(), NULL)); |
| 1292 |
} |
| 1293 |
static inline const RepeatedPtrField<Type>& GetRepeated( |
| 1294 |
int number, const ExtensionSet& set) { |
| 1295 |
// See notes above in RepeatedEnumTypeTraits::GetRepeated(): same |
| 1296 |
// casting hack applies here, because a RepeatedPtrField<MessageLite> |
| 1297 |
// cannot naturally become a RepeatedPtrType<Type> even though Type is |
| 1298 |
// presumably a message. google::protobuf::Message goes through similar contortions |
| 1299 |
// with a reinterpret_cast<>. |
| 1300 |
return *reinterpret_cast<const RepeatedPtrField<Type>*>( |
| 1301 |
set.GetRawRepeatedField(number, GetDefaultRepeatedField())); |
| 1302 |
} |
| 1303 |
static inline RepeatedPtrField<Type>* MutableRepeated(int number, |
| 1304 |
FieldType field_type, |
| 1305 |
bool is_packed, |
| 1306 |
ExtensionSet* set) { |
| 1307 |
return reinterpret_cast<RepeatedPtrField<Type>*>( |
| 1308 |
set->MutableRawRepeatedField(number, field_type, is_packed, NULL)); |
| 1309 |
} |
| 1310 |
|
| 1311 |
static const RepeatedFieldType* GetDefaultRepeatedField(); |
| 1312 |
template <typename ExtendeeT> |
| 1313 |
static void Register(int number, FieldType type, bool is_packed) { |
| 1314 |
ExtensionSet::RegisterMessageExtension(&ExtendeeT::default_instance(), |
| 1315 |
number, type, true, is_packed, |
| 1316 |
&Type::default_instance()); |
| 1317 |
} |
| 1318 |
}; |
| 1319 |
|
| 1320 |
template <typename Type> |
| 1321 |
inline const typename RepeatedMessageTypeTraits<Type>::RepeatedFieldType* |
| 1322 |
RepeatedMessageTypeTraits<Type>::GetDefaultRepeatedField() { |
| 1323 |
static auto instance = OnShutdownDelete(new RepeatedFieldType); |
| 1324 |
return instance; |
| 1325 |
} |
| 1326 |
|
| 1327 |
// ------------------------------------------------------------------- |
| 1328 |
// ExtensionIdentifier |
| 1329 |
|
| 1330 |
// This is the type of actual extension objects. E.g. if you have: |
| 1331 |
// extends Foo with optional int32 bar = 1234; |
| 1332 |
// then "bar" will be defined in C++ as: |
| 1333 |
// ExtensionIdentifier<Foo, PrimitiveTypeTraits<int32>, 5, false> bar(1234); |
| 1334 |
// |
| 1335 |
// Note that we could, in theory, supply the field number as a template |
| 1336 |
// parameter, and thus make an instance of ExtensionIdentifier have no |
| 1337 |
// actual contents. However, if we did that, then using an extension |
| 1338 |
// identifier would not necessarily cause the compiler to output any sort |
| 1339 |
// of reference to any symbol defined in the extension's .pb.o file. Some |
| 1340 |
// linkers will actually drop object files that are not explicitly referenced, |
| 1341 |
// but that would be bad because it would cause this extension to not be |
| 1342 |
// registered at static initialization, and therefore using it would crash. |
| 1343 |
|
| 1344 |
template <typename ExtendeeType, typename TypeTraitsType, FieldType field_type, |
| 1345 |
bool is_packed> |
| 1346 |
class ExtensionIdentifier { |
| 1347 |
public: |
| 1348 |
typedef TypeTraitsType TypeTraits; |
| 1349 |
typedef ExtendeeType Extendee; |
| 1350 |
|
| 1351 |
ExtensionIdentifier(int number, typename TypeTraits::ConstType default_value) |
| 1352 |
: number_(number), default_value_(default_value) { |
| 1353 |
Register(number); |
| 1354 |
} |
| 1355 |
inline int number() const { return number_; } |
| 1356 |
typename TypeTraits::ConstType default_value() const { |
| 1357 |
return default_value_; |
| 1358 |
} |
| 1359 |
|
| 1360 |
static void Register(int number) { |
| 1361 |
TypeTraits::template Register<ExtendeeType>(number, field_type, is_packed); |
| 1362 |
} |
| 1363 |
|
| 1364 |
private: |
| 1365 |
const int number_; |
| 1366 |
typename TypeTraits::ConstType default_value_; |
| 1367 |
}; |
| 1368 |
|
| 1369 |
// ------------------------------------------------------------------- |
| 1370 |
// Generated accessors |
| 1371 |
|
| 1372 |
// This macro should be expanded in the context of a generated type which |
| 1373 |
// has extensions. |
| 1374 |
// |
| 1375 |
// We use "_proto_TypeTraits" as a type name below because "TypeTraits" |
| 1376 |
// causes problems if the class has a nested message or enum type with that |
| 1377 |
// name and "_TypeTraits" is technically reserved for the C++ library since |
| 1378 |
// it starts with an underscore followed by a capital letter. |
| 1379 |
// |
| 1380 |
// For similar reason, we use "_field_type" and "_is_packed" as parameter names |
| 1381 |
// below, so that "field_type" and "is_packed" can be used as field names. |
| 1382 |
#define GOOGLE_PROTOBUF_EXTENSION_ACCESSORS(CLASSNAME) \ |
| 1383 |
/* Has, Size, Clear */ \ |
| 1384 |
template <typename _proto_TypeTraits, \ |
| 1385 |
::PROTOBUF_NAMESPACE_ID::internal::FieldType _field_type, \ |
| 1386 |
bool _is_packed> \ |
| 1387 |
inline bool HasExtension( \ |
| 1388 |
const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ |
| 1389 |
CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) const { \ |
| 1390 |
return _extensions_.Has(id.number()); \ |
| 1391 |
} \ |
| 1392 |
\ |
| 1393 |
template <typename _proto_TypeTraits, \ |
| 1394 |
::PROTOBUF_NAMESPACE_ID::internal::FieldType _field_type, \ |
| 1395 |
bool _is_packed> \ |
| 1396 |
inline void ClearExtension( \ |
| 1397 |
const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ |
| 1398 |
CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) { \ |
| 1399 |
_extensions_.ClearExtension(id.number()); \ |
| 1400 |
} \ |
| 1401 |
\ |
| 1402 |
template <typename _proto_TypeTraits, \ |
| 1403 |
::PROTOBUF_NAMESPACE_ID::internal::FieldType _field_type, \ |
| 1404 |
bool _is_packed> \ |
| 1405 |
inline int ExtensionSize( \ |
| 1406 |
const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ |
| 1407 |
CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) const { \ |
| 1408 |
return _extensions_.ExtensionSize(id.number()); \ |
| 1409 |
} \ |
| 1410 |
\ |
| 1411 |
/* Singular accessors */ \ |
| 1412 |
template <typename _proto_TypeTraits, \ |
| 1413 |
::PROTOBUF_NAMESPACE_ID::internal::FieldType _field_type, \ |
| 1414 |
bool _is_packed> \ |
| 1415 |
inline typename _proto_TypeTraits::Singular::ConstType GetExtension( \ |
| 1416 |
const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ |
| 1417 |
CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) const { \ |
| 1418 |
return _proto_TypeTraits::Get(id.number(), _extensions_, \ |
| 1419 |
id.default_value()); \ |
| 1420 |
} \ |
| 1421 |
\ |
| 1422 |
template <typename _proto_TypeTraits, \ |
| 1423 |
::PROTOBUF_NAMESPACE_ID::internal::FieldType _field_type, \ |
| 1424 |
bool _is_packed> \ |
| 1425 |
inline typename _proto_TypeTraits::Singular::MutableType MutableExtension( \ |
| 1426 |
const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ |
| 1427 |
CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) { \ |
| 1428 |
return _proto_TypeTraits::Mutable(id.number(), _field_type, \ |
| 1429 |
&_extensions_); \ |
| 1430 |
} \ |
| 1431 |
\ |
| 1432 |
template <typename _proto_TypeTraits, \ |
| 1433 |
::PROTOBUF_NAMESPACE_ID::internal::FieldType _field_type, \ |
| 1434 |
bool _is_packed> \ |
| 1435 |
inline void SetExtension( \ |
| 1436 |
const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ |
| 1437 |
CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id, \ |
| 1438 |
typename _proto_TypeTraits::Singular::ConstType value) { \ |
| 1439 |
_proto_TypeTraits::Set(id.number(), _field_type, value, &_extensions_); \ |
| 1440 |
} \ |
| 1441 |
\ |
| 1442 |
template <typename _proto_TypeTraits, \ |
| 1443 |
::PROTOBUF_NAMESPACE_ID::internal::FieldType _field_type, \ |
| 1444 |
bool _is_packed> \ |
| 1445 |
inline void SetAllocatedExtension( \ |
| 1446 |
const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ |
| 1447 |
CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id, \ |
| 1448 |
typename _proto_TypeTraits::Singular::MutableType value) { \ |
| 1449 |
_proto_TypeTraits::SetAllocated(id.number(), _field_type, value, \ |
| 1450 |
&_extensions_); \ |
| 1451 |
} \ |
| 1452 |
template <typename _proto_TypeTraits, \ |
| 1453 |
::PROTOBUF_NAMESPACE_ID::internal::FieldType _field_type, \ |
| 1454 |
bool _is_packed> \ |
| 1455 |
inline void UnsafeArenaSetAllocatedExtension( \ |
| 1456 |
const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ |
| 1457 |
CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id, \ |
| 1458 |
typename _proto_TypeTraits::Singular::MutableType value) { \ |
| 1459 |
_proto_TypeTraits::UnsafeArenaSetAllocated(id.number(), _field_type, \ |
| 1460 |
value, &_extensions_); \ |
| 1461 |
} \ |
| 1462 |
template <typename _proto_TypeTraits, \ |
| 1463 |
::PROTOBUF_NAMESPACE_ID::internal::FieldType _field_type, \ |
| 1464 |
bool _is_packed> \ |
| 1465 |
inline typename _proto_TypeTraits::Singular::MutableType ReleaseExtension( \ |
| 1466 |
const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ |
| 1467 |
CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) { \ |
| 1468 |
return _proto_TypeTraits::Release(id.number(), _field_type, \ |
| 1469 |
&_extensions_); \ |
| 1470 |
} \ |
| 1471 |
template <typename _proto_TypeTraits, \ |
| 1472 |
::PROTOBUF_NAMESPACE_ID::internal::FieldType _field_type, \ |
| 1473 |
bool _is_packed> \ |
| 1474 |
inline typename _proto_TypeTraits::Singular::MutableType \ |
| 1475 |
UnsafeArenaReleaseExtension( \ |
| 1476 |
const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ |
| 1477 |
CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) { \ |
| 1478 |
return _proto_TypeTraits::UnsafeArenaRelease(id.number(), _field_type, \ |
| 1479 |
&_extensions_); \ |
| 1480 |
} \ |
| 1481 |
\ |
| 1482 |
/* Repeated accessors */ \ |
| 1483 |
template <typename _proto_TypeTraits, \ |
| 1484 |
::PROTOBUF_NAMESPACE_ID::internal::FieldType _field_type, \ |
| 1485 |
bool _is_packed> \ |
| 1486 |
inline typename _proto_TypeTraits::Repeated::ConstType GetExtension( \ |
| 1487 |
const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ |
| 1488 |
CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id, \ |
| 1489 |
int index) const { \ |
| 1490 |
return _proto_TypeTraits::Get(id.number(), _extensions_, index); \ |
| 1491 |
} \ |
| 1492 |
\ |
| 1493 |
template <typename _proto_TypeTraits, \ |
| 1494 |
::PROTOBUF_NAMESPACE_ID::internal::FieldType _field_type, \ |
| 1495 |
bool _is_packed> \ |
| 1496 |
inline typename _proto_TypeTraits::Repeated::MutableType MutableExtension( \ |
| 1497 |
const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ |
| 1498 |
CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id, \ |
| 1499 |
int index) { \ |
| 1500 |
return _proto_TypeTraits::Mutable(id.number(), index, &_extensions_); \ |
| 1501 |
} \ |
| 1502 |
\ |
| 1503 |
template <typename _proto_TypeTraits, \ |
| 1504 |
::PROTOBUF_NAMESPACE_ID::internal::FieldType _field_type, \ |
| 1505 |
bool _is_packed> \ |
| 1506 |
inline void SetExtension( \ |
| 1507 |
const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ |
| 1508 |
CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id, \ |
| 1509 |
int index, typename _proto_TypeTraits::Repeated::ConstType value) { \ |
| 1510 |
_proto_TypeTraits::Set(id.number(), index, value, &_extensions_); \ |
| 1511 |
} \ |
| 1512 |
\ |
| 1513 |
template <typename _proto_TypeTraits, \ |
| 1514 |
::PROTOBUF_NAMESPACE_ID::internal::FieldType _field_type, \ |
| 1515 |
bool _is_packed> \ |
| 1516 |
inline typename _proto_TypeTraits::Repeated::MutableType AddExtension( \ |
| 1517 |
const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ |
| 1518 |
CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) { \ |
| 1519 |
return _proto_TypeTraits::Add(id.number(), _field_type, &_extensions_); \ |
| 1520 |
} \ |
| 1521 |
\ |
| 1522 |
template <typename _proto_TypeTraits, \ |
| 1523 |
::PROTOBUF_NAMESPACE_ID::internal::FieldType _field_type, \ |
| 1524 |
bool _is_packed> \ |
| 1525 |
inline void AddExtension( \ |
| 1526 |
const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ |
| 1527 |
CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id, \ |
| 1528 |
typename _proto_TypeTraits::Repeated::ConstType value) { \ |
| 1529 |
_proto_TypeTraits::Add(id.number(), _field_type, _is_packed, value, \ |
| 1530 |
&_extensions_); \ |
| 1531 |
} \ |
| 1532 |
\ |
| 1533 |
template <typename _proto_TypeTraits, \ |
| 1534 |
::PROTOBUF_NAMESPACE_ID::internal::FieldType _field_type, \ |
| 1535 |
bool _is_packed> \ |
| 1536 |
inline const typename _proto_TypeTraits::Repeated::RepeatedFieldType& \ |
| 1537 |
GetRepeatedExtension( \ |
| 1538 |
const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ |
| 1539 |
CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) const { \ |
| 1540 |
return _proto_TypeTraits::GetRepeated(id.number(), _extensions_); \ |
| 1541 |
} \ |
| 1542 |
\ |
| 1543 |
template <typename _proto_TypeTraits, \ |
| 1544 |
::PROTOBUF_NAMESPACE_ID::internal::FieldType _field_type, \ |
| 1545 |
bool _is_packed> \ |
| 1546 |
inline typename _proto_TypeTraits::Repeated::RepeatedFieldType* \ |
| 1547 |
MutableRepeatedExtension( \ |
| 1548 |
const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< \ |
| 1549 |
CLASSNAME, _proto_TypeTraits, _field_type, _is_packed>& id) { \ |
| 1550 |
return _proto_TypeTraits::MutableRepeated(id.number(), _field_type, \ |
| 1551 |
_is_packed, &_extensions_); \ |
| 1552 |
} |
| 1553 |
|
| 1554 |
} // namespace internal |
| 1555 |
|
| 1556 |
// Call this function to ensure that this extensions's reflection is linked into |
| 1557 |
// the binary: |
| 1558 |
// |
| 1559 |
// google::protobuf::LinkExtensionReflection(Foo::my_extension); |
| 1560 |
// |
| 1561 |
// This will ensure that the following lookup will succeed: |
| 1562 |
// |
| 1563 |
// DescriptorPool::generated_pool()->FindExtensionByName("Foo.my_extension"); |
| 1564 |
// |
| 1565 |
// This is often relevant for parsing extensions in text mode. |
| 1566 |
// |
| 1567 |
// As a side-effect, it will also guarantee that anything else from the same |
| 1568 |
// .proto file will also be available for lookup in the generated pool. |
| 1569 |
// |
| 1570 |
// This function does not actually register the extension, so it does not need |
| 1571 |
// to be called before the lookup. However it does need to occur in a function |
| 1572 |
// that cannot be stripped from the binary (ie. it must be reachable from main). |
| 1573 |
// |
| 1574 |
// Best practice is to call this function as close as possible to where the |
| 1575 |
// reflection is actually needed. This function is very cheap to call, so you |
| 1576 |
// should not need to worry about its runtime overhead except in tight loops (on |
| 1577 |
// x86-64 it compiles into two "mov" instructions). |
| 1578 |
template <typename ExtendeeType, typename TypeTraitsType, |
| 1579 |
internal::FieldType field_type, bool is_packed> |
| 1580 |
void LinkExtensionReflection( |
| 1581 |
const google::protobuf::internal::ExtensionIdentifier< |
| 1582 |
ExtendeeType, TypeTraitsType, field_type, is_packed>& extension) { |
| 1583 |
internal::StrongReference(extension); |
| 1584 |
} |
| 1585 |
|
| 1586 |
} // namespace protobuf |
| 1587 |
} // namespace google |
| 1588 |
|
| 1589 |
#include <google/protobuf/port_undef.inc> |
| 1590 |
|
| 1591 |
#endif // GOOGLE_PROTOBUF_EXTENSION_SET_H__ |