| 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 |
#ifndef GOOGLE_PROTOBUF_MAP_ENTRY_LITE_H__ |
| 32 |
#define GOOGLE_PROTOBUF_MAP_ENTRY_LITE_H__ |
| 33 |
|
| 34 |
#include <assert.h> |
| 35 |
#include <string> |
| 36 |
|
| 37 |
#include <google/protobuf/stubs/casts.h> |
| 38 |
#include <google/protobuf/parse_context.h> |
| 39 |
#include <google/protobuf/io/coded_stream.h> |
| 40 |
#include <google/protobuf/arena.h> |
| 41 |
#include <google/protobuf/arenastring.h> |
| 42 |
#include <google/protobuf/generated_message_util.h> |
| 43 |
#include <google/protobuf/map.h> |
| 44 |
#include <google/protobuf/map_type_handler.h> |
| 45 |
#include <google/protobuf/port.h> |
| 46 |
#include <google/protobuf/wire_format_lite.h> |
| 47 |
|
| 48 |
#include <google/protobuf/port_def.inc> |
| 49 |
#ifdef SWIG |
| 50 |
#error "You cannot SWIG proto headers" |
| 51 |
#endif |
| 52 |
|
| 53 |
namespace google { |
| 54 |
namespace protobuf { |
| 55 |
namespace internal { |
| 56 |
template <typename Derived, typename Key, typename Value, |
| 57 |
WireFormatLite::FieldType kKeyFieldType, |
| 58 |
WireFormatLite::FieldType kValueFieldType> |
| 59 |
class MapEntry; |
| 60 |
template <typename Derived, typename Key, typename Value, |
| 61 |
WireFormatLite::FieldType kKeyFieldType, |
| 62 |
WireFormatLite::FieldType kValueFieldType> |
| 63 |
class MapFieldLite; |
| 64 |
} // namespace internal |
| 65 |
} // namespace protobuf |
| 66 |
} // namespace google |
| 67 |
|
| 68 |
namespace google { |
| 69 |
namespace protobuf { |
| 70 |
namespace internal { |
| 71 |
|
| 72 |
// MoveHelper::Move is used to set *dest. It copies *src, or moves it (in |
| 73 |
// the C++11 sense), or swaps it. *src is left in a sane state for |
| 74 |
// subsequent destruction, but shouldn't be used for anything. |
| 75 |
template <bool is_enum, bool is_message, bool is_stringlike, typename T> |
| 76 |
struct MoveHelper { // primitives |
| 77 |
static void Move(T* src, T* dest) { *dest = *src; } |
| 78 |
}; |
| 79 |
|
| 80 |
template <bool is_message, bool is_stringlike, typename T> |
| 81 |
struct MoveHelper<true, is_message, is_stringlike, T> { // enums |
| 82 |
static void Move(T* src, T* dest) { *dest = *src; } |
| 83 |
// T is an enum here, so allow conversions to and from int. |
| 84 |
static void Move(T* src, int* dest) { *dest = static_cast<int>(*src); } |
| 85 |
static void Move(int* src, T* dest) { *dest = static_cast<T>(*src); } |
| 86 |
}; |
| 87 |
|
| 88 |
template <bool is_stringlike, typename T> |
| 89 |
struct MoveHelper<false, true, is_stringlike, T> { // messages |
| 90 |
static void Move(T* src, T* dest) { dest->Swap(src); } |
| 91 |
}; |
| 92 |
|
| 93 |
template <typename T> |
| 94 |
struct MoveHelper<false, false, true, T> { // strings and similar |
| 95 |
static void Move(T* src, T* dest) { |
| 96 |
*dest = std::move(*src); |
| 97 |
} |
| 98 |
}; |
| 99 |
|
| 100 |
// Functions for operating on a map entry. Does not contain any representation |
| 101 |
// (this class is not intended to be instantiated). |
| 102 |
template <typename Key, typename Value, WireFormatLite::FieldType kKeyFieldType, |
| 103 |
WireFormatLite::FieldType kValueFieldType> |
| 104 |
struct MapEntryFuncs { |
| 105 |
typedef MapTypeHandler<kKeyFieldType, Key> KeyTypeHandler; |
| 106 |
typedef MapTypeHandler<kValueFieldType, Value> ValueTypeHandler; |
| 107 |
static const int kKeyFieldNumber = 1; |
| 108 |
static const int kValueFieldNumber = 2; |
| 109 |
|
| 110 |
static uint8* InternalSerialize(int field_number, const Key& key, |
| 111 |
const Value& value, uint8* ptr, |
| 112 |
io::EpsCopyOutputStream* stream) { |
| 113 |
ptr = stream->EnsureSpace(ptr); |
| 114 |
ptr = WireFormatLite::WriteTagToArray( |
| 115 |
field_number, WireFormatLite::WIRETYPE_LENGTH_DELIMITED, ptr); |
| 116 |
ptr = io::CodedOutputStream::WriteVarint32ToArray(GetCachedSize(key, value), |
| 117 |
ptr); |
| 118 |
|
| 119 |
ptr = KeyTypeHandler::Write(kKeyFieldNumber, key, ptr, stream); |
| 120 |
return ValueTypeHandler::Write(kValueFieldNumber, value, ptr, stream); |
| 121 |
} |
| 122 |
|
| 123 |
static size_t ByteSizeLong(const Key& key, const Value& value) { |
| 124 |
// Tags for key and value will both be one byte (field numbers 1 and 2). |
| 125 |
size_t inner_length = |
| 126 |
2 + KeyTypeHandler::ByteSize(key) + ValueTypeHandler::ByteSize(value); |
| 127 |
return inner_length + io::CodedOutputStream::VarintSize32( |
| 128 |
static_cast<uint32>(inner_length)); |
| 129 |
} |
| 130 |
|
| 131 |
static int GetCachedSize(const Key& key, const Value& value) { |
| 132 |
// Tags for key and value will both be one byte (field numbers 1 and 2). |
| 133 |
return 2 + KeyTypeHandler::GetCachedSize(key) + |
| 134 |
ValueTypeHandler::GetCachedSize(value); |
| 135 |
} |
| 136 |
}; |
| 137 |
|
| 138 |
// MapEntryImpl is used to implement parsing and serialization of map entries. |
| 139 |
// It uses Curious Recursive Template Pattern (CRTP) to provide the type of |
| 140 |
// the eventual code to the template code. |
| 141 |
template <typename Derived, typename Base, typename Key, typename Value, |
| 142 |
WireFormatLite::FieldType kKeyFieldType, |
| 143 |
WireFormatLite::FieldType kValueFieldType> |
| 144 |
class MapEntryImpl : public Base { |
| 145 |
public: |
| 146 |
typedef MapEntryFuncs<Key, Value, kKeyFieldType, kValueFieldType> Funcs; |
| 147 |
|
| 148 |
protected: |
| 149 |
// Provide utilities to parse/serialize key/value. Provide utilities to |
| 150 |
// manipulate internal stored type. |
| 151 |
typedef MapTypeHandler<kKeyFieldType, Key> KeyTypeHandler; |
| 152 |
typedef MapTypeHandler<kValueFieldType, Value> ValueTypeHandler; |
| 153 |
|
| 154 |
// Define internal memory layout. Strings and messages are stored as |
| 155 |
// pointers, while other types are stored as values. |
| 156 |
typedef typename KeyTypeHandler::TypeOnMemory KeyOnMemory; |
| 157 |
typedef typename ValueTypeHandler::TypeOnMemory ValueOnMemory; |
| 158 |
|
| 159 |
// Enum type cannot be used for MapTypeHandler::Read. Define a type |
| 160 |
// which will replace Enum with int. |
| 161 |
typedef typename KeyTypeHandler::MapEntryAccessorType KeyMapEntryAccessorType; |
| 162 |
typedef |
| 163 |
typename ValueTypeHandler::MapEntryAccessorType ValueMapEntryAccessorType; |
| 164 |
|
| 165 |
// Constants for field number. |
| 166 |
static const int kKeyFieldNumber = 1; |
| 167 |
static const int kValueFieldNumber = 2; |
| 168 |
|
| 169 |
// Constants for field tag. |
| 170 |
static const uint8 kKeyTag = |
| 171 |
GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(kKeyFieldNumber, KeyTypeHandler::kWireType); |
| 172 |
static const uint8 kValueTag = GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG( |
| 173 |
kValueFieldNumber, ValueTypeHandler::kWireType); |
| 174 |
static const size_t kTagSize = 1; |
| 175 |
|
| 176 |
public: |
| 177 |
// Work-around for a compiler bug (see repeated_field.h). |
| 178 |
typedef void MapEntryHasMergeTypeTrait; |
| 179 |
typedef Derived EntryType; |
| 180 |
typedef Key EntryKeyType; |
| 181 |
typedef Value EntryValueType; |
| 182 |
static const WireFormatLite::FieldType kEntryKeyFieldType = kKeyFieldType; |
| 183 |
static const WireFormatLite::FieldType kEntryValueFieldType = kValueFieldType; |
| 184 |
|
| 185 |
constexpr MapEntryImpl() |
| 186 |
: key_(KeyTypeHandler::Constinit()), |
| 187 |
value_(ValueTypeHandler::Constinit()), |
| 188 |
_has_bits_{} {} |
| 189 |
|
| 190 |
explicit MapEntryImpl(Arena* arena) |
| 191 |
: Base(arena), |
| 192 |
key_(KeyTypeHandler::Constinit()), |
| 193 |
value_(ValueTypeHandler::Constinit()), |
| 194 |
_has_bits_{} {} |
| 195 |
|
| 196 |
~MapEntryImpl() { |
| 197 |
if (Base::GetArena() != NULL) return; |
| 198 |
KeyTypeHandler::DeleteNoArena(key_); |
| 199 |
ValueTypeHandler::DeleteNoArena(value_); |
| 200 |
} |
| 201 |
|
| 202 |
// accessors ====================================================== |
| 203 |
|
| 204 |
virtual inline const KeyMapEntryAccessorType& key() const { |
| 205 |
return KeyTypeHandler::GetExternalReference(key_); |
| 206 |
} |
| 207 |
virtual inline const ValueMapEntryAccessorType& value() const { |
| 208 |
return ValueTypeHandler::DefaultIfNotInitialized(value_); |
| 209 |
} |
| 210 |
inline KeyMapEntryAccessorType* mutable_key() { |
| 211 |
set_has_key(); |
| 212 |
return KeyTypeHandler::EnsureMutable(&key_, Base::GetArena()); |
| 213 |
} |
| 214 |
inline ValueMapEntryAccessorType* mutable_value() { |
| 215 |
set_has_value(); |
| 216 |
return ValueTypeHandler::EnsureMutable(&value_, Base::GetArena()); |
| 217 |
} |
| 218 |
|
| 219 |
// implements MessageLite ========================================= |
| 220 |
|
| 221 |
// MapEntryImpl is for implementation only and this function isn't called |
| 222 |
// anywhere. Just provide a fake implementation here for MessageLite. |
| 223 |
std::string GetTypeName() const override { return ""; } |
| 224 |
|
| 225 |
void CheckTypeAndMergeFrom(const MessageLite& other) override { |
| 226 |
MergeFromInternal(*::google::protobuf::internal::DownCast<const Derived*>(&other)); |
| 227 |
} |
| 228 |
|
| 229 |
const char* _InternalParse(const char* ptr, ParseContext* ctx) final { |
| 230 |
while (!ctx->Done(&ptr)) { |
| 231 |
uint32 tag; |
| 232 |
ptr = ReadTag(ptr, &tag); |
| 233 |
GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); |
| 234 |
if (tag == kKeyTag) { |
| 235 |
set_has_key(); |
| 236 |
KeyMapEntryAccessorType* key = mutable_key(); |
| 237 |
ptr = KeyTypeHandler::Read(ptr, ctx, key); |
| 238 |
if (!Derived::ValidateKey(key)) return nullptr; |
| 239 |
} else if (tag == kValueTag) { |
| 240 |
set_has_value(); |
| 241 |
ValueMapEntryAccessorType* value = mutable_value(); |
| 242 |
ptr = ValueTypeHandler::Read(ptr, ctx, value); |
| 243 |
if (!Derived::ValidateValue(value)) return nullptr; |
| 244 |
} else { |
| 245 |
if (tag == 0 || WireFormatLite::GetTagWireType(tag) == |
| 246 |
WireFormatLite::WIRETYPE_END_GROUP) { |
| 247 |
ctx->SetLastTag(tag); |
| 248 |
return ptr; |
| 249 |
} |
| 250 |
ptr = UnknownFieldParse(tag, static_cast<std::string*>(nullptr), ptr, |
| 251 |
ctx); |
| 252 |
} |
| 253 |
GOOGLE_PROTOBUF_PARSER_ASSERT(ptr); |
| 254 |
} |
| 255 |
return ptr; |
| 256 |
} |
| 257 |
|
| 258 |
size_t ByteSizeLong() const override { |
| 259 |
size_t size = 0; |
| 260 |
size += kTagSize + static_cast<size_t>(KeyTypeHandler::ByteSize(key())); |
| 261 |
size += kTagSize + static_cast<size_t>(ValueTypeHandler::ByteSize(value())); |
| 262 |
return size; |
| 263 |
} |
| 264 |
|
| 265 |
::google::protobuf::uint8* _InternalSerialize(::google::protobuf::uint8* ptr, |
| 266 |
io::EpsCopyOutputStream* stream) const override { |
| 267 |
ptr = KeyTypeHandler::Write(kKeyFieldNumber, key(), ptr, stream); |
| 268 |
return ValueTypeHandler::Write(kValueFieldNumber, value(), ptr, stream); |
| 269 |
} |
| 270 |
|
| 271 |
// Don't override SerializeWithCachedSizesToArray. Use MessageLite's. |
| 272 |
|
| 273 |
int GetCachedSize() const override { |
| 274 |
int size = 0; |
| 275 |
size += has_key() ? static_cast<int>(kTagSize) + |
| 276 |
KeyTypeHandler::GetCachedSize(key()) |
| 277 |
: 0; |
| 278 |
size += has_value() ? static_cast<int>(kTagSize) + |
| 279 |
ValueTypeHandler::GetCachedSize(value()) |
| 280 |
: 0; |
| 281 |
return size; |
| 282 |
} |
| 283 |
|
| 284 |
bool IsInitialized() const override { |
| 285 |
return ValueTypeHandler::IsInitialized(value_); |
| 286 |
} |
| 287 |
|
| 288 |
Base* New() const override { |
| 289 |
Derived* entry = new Derived; |
| 290 |
return entry; |
| 291 |
} |
| 292 |
|
| 293 |
Base* New(Arena* arena) const override { |
| 294 |
Derived* entry = Arena::CreateMessage<Derived>(arena); |
| 295 |
return entry; |
| 296 |
} |
| 297 |
|
| 298 |
protected: |
| 299 |
// We can't declare this function directly here as it would hide the other |
| 300 |
// overload (const Message&). |
| 301 |
void MergeFromInternal(const MapEntryImpl& from) { |
| 302 |
if (from._has_bits_[0]) { |
| 303 |
if (from.has_key()) { |
| 304 |
KeyTypeHandler::EnsureMutable(&key_, Base::GetArena()); |
| 305 |
KeyTypeHandler::Merge(from.key(), &key_, Base::GetArena()); |
| 306 |
set_has_key(); |
| 307 |
} |
| 308 |
if (from.has_value()) { |
| 309 |
ValueTypeHandler::EnsureMutable(&value_, Base::GetArena()); |
| 310 |
ValueTypeHandler::Merge(from.value(), &value_, Base::GetArena()); |
| 311 |
set_has_value(); |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
public: |
| 317 |
void Clear() override { |
| 318 |
KeyTypeHandler::Clear(&key_, Base::GetArena()); |
| 319 |
ValueTypeHandler::Clear(&value_, Base::GetArena()); |
| 320 |
clear_has_key(); |
| 321 |
clear_has_value(); |
| 322 |
} |
| 323 |
|
| 324 |
// Parsing using MergePartialFromCodedStream, above, is not as |
| 325 |
// efficient as it could be. This helper class provides a speedier way. |
| 326 |
template <typename MapField, typename Map> |
| 327 |
class Parser { |
| 328 |
public: |
| 329 |
explicit Parser(MapField* mf) : mf_(mf), map_(mf->MutableMap()) {} |
| 330 |
~Parser() { |
| 331 |
if (entry_ != nullptr && entry_->GetArena() == nullptr) delete entry_; |
| 332 |
} |
| 333 |
|
| 334 |
// This does what the typical MergePartialFromCodedStream() is expected to |
| 335 |
// do, with the additional side-effect that if successful (i.e., if true is |
| 336 |
// going to be its return value) it inserts the key-value pair into map_. |
| 337 |
bool MergePartialFromCodedStream(io::CodedInputStream* input) { |
| 338 |
// Look for the expected thing: a key and then a value. If it fails, |
| 339 |
// invoke the enclosing class's MergePartialFromCodedStream, or return |
| 340 |
// false if that would be pointless. |
| 341 |
if (input->ExpectTag(kKeyTag)) { |
| 342 |
if (!KeyTypeHandler::Read(input, &key_)) { |
| 343 |
return false; |
| 344 |
} |
| 345 |
// Peek at the next byte to see if it is kValueTag. If not, bail out. |
| 346 |
const void* data; |
| 347 |
int size; |
| 348 |
input->GetDirectBufferPointerInline(&data, &size); |
| 349 |
// We could use memcmp here, but we don't bother. The tag is one byte. |
| 350 |
static_assert(kTagSize == 1, "tag size must be 1"); |
| 351 |
if (size > 0 && *reinterpret_cast<const char*>(data) == kValueTag) { |
| 352 |
typename Map::size_type map_size = map_->size(); |
| 353 |
value_ptr_ = &(*map_)[key_]; |
| 354 |
if (PROTOBUF_PREDICT_TRUE(map_size != map_->size())) { |
| 355 |
// We created a new key-value pair. Fill in the value. |
| 356 |
typedef |
| 357 |
typename MapIf<ValueTypeHandler::kIsEnum, int*, Value*>::type T; |
| 358 |
input->Skip(kTagSize); // Skip kValueTag. |
| 359 |
if (!ValueTypeHandler::Read(input, |
| 360 |
reinterpret_cast<T>(value_ptr_))) { |
| 361 |
map_->erase(key_); // Failure! Undo insertion. |
| 362 |
return false; |
| 363 |
} |
| 364 |
if (input->ExpectAtEnd()) return true; |
| 365 |
return ReadBeyondKeyValuePair(input); |
| 366 |
} |
| 367 |
} |
| 368 |
} else { |
| 369 |
key_ = Key(); |
| 370 |
} |
| 371 |
|
| 372 |
NewEntry(); |
| 373 |
*entry_->mutable_key() = key_; |
| 374 |
const bool result = entry_->MergePartialFromCodedStream(input); |
| 375 |
if (result) UseKeyAndValueFromEntry(); |
| 376 |
return result; |
| 377 |
} |
| 378 |
|
| 379 |
const char* _InternalParse(const char* ptr, ParseContext* ctx) { |
| 380 |
if (PROTOBUF_PREDICT_TRUE(!ctx->Done(&ptr) && *ptr == kKeyTag)) { |
| 381 |
ptr = KeyTypeHandler::Read(ptr + 1, ctx, &key_); |
| 382 |
if (PROTOBUF_PREDICT_FALSE(!ptr || !Derived::ValidateKey(&key_))) { |
| 383 |
return nullptr; |
| 384 |
} |
| 385 |
if (PROTOBUF_PREDICT_TRUE(!ctx->Done(&ptr) && *ptr == kValueTag)) { |
| 386 |
typename Map::size_type map_size = map_->size(); |
| 387 |
value_ptr_ = &(*map_)[key_]; |
| 388 |
if (PROTOBUF_PREDICT_TRUE(map_size != map_->size())) { |
| 389 |
using T = |
| 390 |
typename MapIf<ValueTypeHandler::kIsEnum, int*, Value*>::type; |
| 391 |
ptr = ValueTypeHandler::Read(ptr + 1, ctx, |
| 392 |
reinterpret_cast<T>(value_ptr_)); |
| 393 |
if (PROTOBUF_PREDICT_FALSE(!ptr || |
| 394 |
!Derived::ValidateValue(value_ptr_))) { |
| 395 |
map_->erase(key_); // Failure! Undo insertion. |
| 396 |
return nullptr; |
| 397 |
} |
| 398 |
if (PROTOBUF_PREDICT_TRUE(ctx->Done(&ptr))) return ptr; |
| 399 |
if (!ptr) return nullptr; |
| 400 |
NewEntry(); |
| 401 |
ValueMover::Move(value_ptr_, entry_->mutable_value()); |
| 402 |
map_->erase(key_); |
| 403 |
goto move_key; |
| 404 |
} |
| 405 |
} else { |
| 406 |
if (!ptr) return nullptr; |
| 407 |
} |
| 408 |
NewEntry(); |
| 409 |
move_key: |
| 410 |
KeyMover::Move(&key_, entry_->mutable_key()); |
| 411 |
} else { |
| 412 |
if (!ptr) return nullptr; |
| 413 |
NewEntry(); |
| 414 |
} |
| 415 |
ptr = entry_->_InternalParse(ptr, ctx); |
| 416 |
if (ptr) UseKeyAndValueFromEntry(); |
| 417 |
return ptr; |
| 418 |
} |
| 419 |
|
| 420 |
template <typename UnknownType> |
| 421 |
const char* ParseWithEnumValidation(const char* ptr, ParseContext* ctx, |
| 422 |
bool (*is_valid)(int), uint32 field_num, |
| 423 |
InternalMetadata* metadata) { |
| 424 |
auto entry = NewEntry(); |
| 425 |
ptr = entry->_InternalParse(ptr, ctx); |
| 426 |
if (!ptr) return nullptr; |
| 427 |
if (is_valid(entry->value())) { |
| 428 |
UseKeyAndValueFromEntry(); |
| 429 |
} else { |
| 430 |
WriteLengthDelimited(field_num, entry->SerializeAsString(), |
| 431 |
metadata->mutable_unknown_fields<UnknownType>()); |
| 432 |
} |
| 433 |
return ptr; |
| 434 |
} |
| 435 |
|
| 436 |
MapEntryImpl* NewEntry() { return entry_ = mf_->NewEntry(); } |
| 437 |
|
| 438 |
const Key& key() const { return key_; } |
| 439 |
const Value& value() const { return *value_ptr_; } |
| 440 |
|
| 441 |
const Key& entry_key() const { return entry_->key(); } |
| 442 |
const Value& entry_value() const { return entry_->value(); } |
| 443 |
|
| 444 |
private: |
| 445 |
void UseKeyAndValueFromEntry() { |
| 446 |
// Update key_ in case we need it later (because key() is called). |
| 447 |
// This is potentially inefficient, especially if the key is |
| 448 |
// expensive to copy (e.g., a long string), but this is a cold |
| 449 |
// path, so it's not a big deal. |
| 450 |
key_ = entry_->key(); |
| 451 |
value_ptr_ = &(*map_)[key_]; |
| 452 |
ValueMover::Move(entry_->mutable_value(), value_ptr_); |
| 453 |
} |
| 454 |
|
| 455 |
// After reading a key and value successfully, and inserting that data |
| 456 |
// into map_, we are not at the end of the input. This is unusual, but |
| 457 |
// allowed by the spec. |
| 458 |
bool ReadBeyondKeyValuePair(io::CodedInputStream* input) PROTOBUF_COLD { |
| 459 |
NewEntry(); |
| 460 |
ValueMover::Move(value_ptr_, entry_->mutable_value()); |
| 461 |
map_->erase(key_); |
| 462 |
KeyMover::Move(&key_, entry_->mutable_key()); |
| 463 |
const bool result = entry_->MergePartialFromCodedStream(input); |
| 464 |
if (result) UseKeyAndValueFromEntry(); |
| 465 |
return result; |
| 466 |
} |
| 467 |
|
| 468 |
typedef MoveHelper<KeyTypeHandler::kIsEnum, KeyTypeHandler::kIsMessage, |
| 469 |
KeyTypeHandler::kWireType == |
| 470 |
WireFormatLite::WIRETYPE_LENGTH_DELIMITED, |
| 471 |
Key> |
| 472 |
KeyMover; |
| 473 |
typedef MoveHelper<ValueTypeHandler::kIsEnum, ValueTypeHandler::kIsMessage, |
| 474 |
ValueTypeHandler::kWireType == |
| 475 |
WireFormatLite::WIRETYPE_LENGTH_DELIMITED, |
| 476 |
Value> |
| 477 |
ValueMover; |
| 478 |
|
| 479 |
MapField* const mf_; |
| 480 |
Map* const map_; |
| 481 |
Key key_; |
| 482 |
Value* value_ptr_; |
| 483 |
MapEntryImpl* entry_ = nullptr; |
| 484 |
}; |
| 485 |
|
| 486 |
protected: |
| 487 |
void set_has_key() { _has_bits_[0] |= 0x00000001u; } |
| 488 |
bool has_key() const { return (_has_bits_[0] & 0x00000001u) != 0; } |
| 489 |
void clear_has_key() { _has_bits_[0] &= ~0x00000001u; } |
| 490 |
void set_has_value() { _has_bits_[0] |= 0x00000002u; } |
| 491 |
bool has_value() const { return (_has_bits_[0] & 0x00000002u) != 0; } |
| 492 |
void clear_has_value() { _has_bits_[0] &= ~0x00000002u; } |
| 493 |
|
| 494 |
public: |
| 495 |
inline Arena* GetArena() const { return Base::GetArena(); } |
| 496 |
|
| 497 |
public: // Needed for constructing tables |
| 498 |
KeyOnMemory key_; |
| 499 |
ValueOnMemory value_; |
| 500 |
uint32 _has_bits_[1]; |
| 501 |
|
| 502 |
private: |
| 503 |
friend class ::PROTOBUF_NAMESPACE_ID::Arena; |
| 504 |
typedef void InternalArenaConstructable_; |
| 505 |
typedef void DestructorSkippable_; |
| 506 |
template <typename C, typename K, typename V, WireFormatLite::FieldType, |
| 507 |
WireFormatLite::FieldType> |
| 508 |
friend class internal::MapEntry; |
| 509 |
template <typename C, typename K, typename V, WireFormatLite::FieldType, |
| 510 |
WireFormatLite::FieldType> |
| 511 |
friend class internal::MapFieldLite; |
| 512 |
|
| 513 |
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MapEntryImpl); |
| 514 |
}; |
| 515 |
|
| 516 |
template <typename T, typename Key, typename Value, |
| 517 |
WireFormatLite::FieldType kKeyFieldType, |
| 518 |
WireFormatLite::FieldType kValueFieldType> |
| 519 |
class MapEntryLite : public MapEntryImpl<T, MessageLite, Key, Value, |
| 520 |
kKeyFieldType, kValueFieldType> { |
| 521 |
public: |
| 522 |
typedef MapEntryImpl<T, MessageLite, Key, Value, kKeyFieldType, |
| 523 |
kValueFieldType> |
| 524 |
SuperType; |
| 525 |
constexpr MapEntryLite() {} |
| 526 |
explicit MapEntryLite(Arena* arena) : SuperType(arena) {} |
| 527 |
~MapEntryLite() { MessageLite::_internal_metadata_.Delete<std::string>(); } |
| 528 |
void MergeFrom(const MapEntryLite& other) { MergeFromInternal(other); } |
| 529 |
|
| 530 |
private: |
| 531 |
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MapEntryLite); |
| 532 |
}; |
| 533 |
// The completely unprincipled and unwieldy use of template parameters in |
| 534 |
// the map code necessitates wrappers to make the code a little bit more |
| 535 |
// manageable. |
| 536 |
template <typename Derived> |
| 537 |
struct DeconstructMapEntry; |
| 538 |
|
| 539 |
template <typename T, typename K, typename V, WireFormatLite::FieldType key, |
| 540 |
WireFormatLite::FieldType value> |
| 541 |
struct DeconstructMapEntry<MapEntryLite<T, K, V, key, value> > { |
| 542 |
typedef K Key; |
| 543 |
typedef V Value; |
| 544 |
static const WireFormatLite::FieldType kKeyFieldType = key; |
| 545 |
static const WireFormatLite::FieldType kValueFieldType = value; |
| 546 |
}; |
| 547 |
|
| 548 |
// Helpers for deterministic serialization ============================= |
| 549 |
|
| 550 |
// This struct can be used with any generic sorting algorithm. If the Key |
| 551 |
// type is relatively small and easy to copy then copying Keys into an |
| 552 |
// array of SortItems can be beneficial. Then all the data the sorting |
| 553 |
// algorithm needs to touch is in that one array. |
| 554 |
template <typename Key, typename PtrToKeyValuePair> |
| 555 |
struct SortItem { |
| 556 |
SortItem() {} |
| 557 |
explicit SortItem(PtrToKeyValuePair p) : first(p->first), second(p) {} |
| 558 |
|
| 559 |
Key first; |
| 560 |
PtrToKeyValuePair second; |
| 561 |
}; |
| 562 |
|
| 563 |
template <typename T> |
| 564 |
struct CompareByFirstField { |
| 565 |
bool operator()(const T& a, const T& b) const { return a.first < b.first; } |
| 566 |
}; |
| 567 |
|
| 568 |
template <typename T> |
| 569 |
struct CompareByDerefFirst { |
| 570 |
bool operator()(const T& a, const T& b) const { return a->first < b->first; } |
| 571 |
}; |
| 572 |
|
| 573 |
// Helper for table driven serialization |
| 574 |
|
| 575 |
template <WireFormatLite::FieldType FieldType> |
| 576 |
struct FromHelper { |
| 577 |
template <typename T> |
| 578 |
static const T& From(const T& x) { |
| 579 |
return x; |
| 580 |
} |
| 581 |
}; |
| 582 |
|
| 583 |
template <> |
| 584 |
struct FromHelper<WireFormatLite::TYPE_STRING> { |
| 585 |
static ArenaStringPtr From(const std::string& x) { |
| 586 |
ArenaStringPtr res; |
| 587 |
TaggedPtr<std::string> ptr; |
| 588 |
ptr.Set(const_cast<std::string*>(&x)); |
| 589 |
res.UnsafeSetTaggedPointer(ptr); |
| 590 |
return res; |
| 591 |
} |
| 592 |
}; |
| 593 |
template <> |
| 594 |
struct FromHelper<WireFormatLite::TYPE_BYTES> { |
| 595 |
static ArenaStringPtr From(const std::string& x) { |
| 596 |
ArenaStringPtr res; |
| 597 |
TaggedPtr<std::string> ptr; |
| 598 |
ptr.Set(const_cast<std::string*>(&x)); |
| 599 |
res.UnsafeSetTaggedPointer(ptr); |
| 600 |
return res; |
| 601 |
} |
| 602 |
}; |
| 603 |
template <> |
| 604 |
struct FromHelper<WireFormatLite::TYPE_MESSAGE> { |
| 605 |
template <typename T> |
| 606 |
static T* From(const T& x) { |
| 607 |
return const_cast<T*>(&x); |
| 608 |
} |
| 609 |
}; |
| 610 |
|
| 611 |
template <typename MapEntryType> |
| 612 |
struct MapEntryHelper; |
| 613 |
|
| 614 |
template <typename T, typename Key, typename Value, |
| 615 |
WireFormatLite::FieldType kKeyFieldType, |
| 616 |
WireFormatLite::FieldType kValueFieldType> |
| 617 |
struct MapEntryHelper< |
| 618 |
MapEntryLite<T, Key, Value, kKeyFieldType, kValueFieldType> > { |
| 619 |
// Provide utilities to parse/serialize key/value. Provide utilities to |
| 620 |
// manipulate internal stored type. |
| 621 |
typedef MapTypeHandler<kKeyFieldType, Key> KeyTypeHandler; |
| 622 |
typedef MapTypeHandler<kValueFieldType, Value> ValueTypeHandler; |
| 623 |
|
| 624 |
// Define internal memory layout. Strings and messages are stored as |
| 625 |
// pointers, while other types are stored as values. |
| 626 |
typedef typename KeyTypeHandler::TypeOnMemory KeyOnMemory; |
| 627 |
typedef typename ValueTypeHandler::TypeOnMemory ValueOnMemory; |
| 628 |
|
| 629 |
explicit MapEntryHelper(const MapPair<Key, Value>& map_pair) |
| 630 |
: _has_bits_(3), |
| 631 |
_cached_size_(2 + KeyTypeHandler::GetCachedSize(map_pair.first) + |
| 632 |
ValueTypeHandler::GetCachedSize(map_pair.second)), |
| 633 |
key_(FromHelper<kKeyFieldType>::From(map_pair.first)), |
| 634 |
value_(FromHelper<kValueFieldType>::From(map_pair.second)) {} |
| 635 |
|
| 636 |
// Purposely not following the style guide naming. These are the names |
| 637 |
// the proto compiler would generate given the map entry descriptor. |
| 638 |
// The proto compiler generates the offsets in this struct as if this was |
| 639 |
// a regular message. This way the table driven code barely notices it's |
| 640 |
// dealing with a map field. |
| 641 |
uint32 _has_bits_; // NOLINT |
| 642 |
uint32 _cached_size_; // NOLINT |
| 643 |
KeyOnMemory key_; // NOLINT |
| 644 |
ValueOnMemory value_; // NOLINT |
| 645 |
}; |
| 646 |
|
| 647 |
} // namespace internal |
| 648 |
} // namespace protobuf |
| 649 |
} // namespace google |
| 650 |
|
| 651 |
#include <google/protobuf/port_undef.inc> |
| 652 |
|
| 653 |
#endif // GOOGLE_PROTOBUF_MAP_ENTRY_LITE_H__ |