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