Go to the documentation of this file.
31 #ifndef GOOGLE_PROTOBUF_MAP_FIELD_H__
32 #define GOOGLE_PROTOBUF_MAP_FIELD_H__
37 #include <google/protobuf/arena.h>
38 #include <google/protobuf/descriptor.h>
39 #include <google/protobuf/generated_message_reflection.h>
40 #include <google/protobuf/generated_message_util.h>
41 #include <google/protobuf/map_entry.h>
42 #include <google/protobuf/map_field_lite.h>
43 #include <google/protobuf/map_type_handler.h>
44 #include <google/protobuf/message.h>
45 #include <google/protobuf/stubs/mutex.h>
46 #include <google/protobuf/port.h>
47 #include <google/protobuf/repeated_field.h>
48 #include <google/protobuf/unknown_field_set.h>
51 #include <google/protobuf/port_def.inc>
54 #error "You cannot SWIG proto headers"
62 #define TYPE_CHECK(EXPECTEDTYPE, METHOD) \
63 if (type() != EXPECTEDTYPE) { \
64 GOOGLE_LOG(FATAL) << "Protocol Buffer map usage error:\n" \
65 << METHOD << " type does not match\n" \
67 << FieldDescriptor::CppTypeName(EXPECTEDTYPE) << "\n" \
68 << " Actual : " << FieldDescriptor::CppTypeName(type()); \
73 class PROTOBUF_EXPORT MapKey {
85 val_.string_value_.Destruct();
92 <<
"MapKey::type MapKey is not initialized. "
93 <<
"Call set methods to initialize MapKey.";
125 return val_.int64_value_;
129 return val_.uint64_value_;
133 return val_.int32_value_;
137 return val_.uint32_value_;
141 return val_.bool_value_;
145 return val_.string_value_.get();
207 SetType(other.
type());
237 template <
typename K,
typename V>
239 friend class ::PROTOBUF_NAMESPACE_ID::MapIterator;
255 val_.string_value_.Destruct();
259 val_.string_value_.DefaultConstruct();
272 struct hash<::PROTOBUF_NAMESPACE_ID::MapKey> {
273 size_t operator()(const ::PROTOBUF_NAMESPACE_ID::MapKey& map_key)
const {
274 switch (map_key.type()) {
275 case ::PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_DOUBLE:
276 case ::PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_FLOAT:
277 case ::PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_ENUM:
278 case ::PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_MESSAGE:
281 case ::PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_STRING:
282 return hash<std::string>()(map_key.GetStringValue());
283 case ::PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_INT64: {
284 auto value = map_key.GetInt64Value();
285 return hash<decltype(value)>()(
value);
287 case ::PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_INT32: {
288 auto value = map_key.GetInt32Value();
289 return hash<decltype(value)>()(map_key.GetInt32Value());
291 case ::PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_UINT64: {
292 auto value = map_key.GetUInt64Value();
293 return hash<decltype(value)>()(map_key.GetUInt64Value());
295 case ::PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_UINT32: {
296 auto value = map_key.GetUInt32Value();
297 return hash<decltype(value)>()(map_key.GetUInt32Value());
299 case ::PROTOBUF_NAMESPACE_ID::FieldDescriptor::CPPTYPE_BOOL: {
300 return hash<bool>()(map_key.GetBoolValue());
306 bool operator()(const ::PROTOBUF_NAMESPACE_ID::MapKey& map_key1,
307 const ::PROTOBUF_NAMESPACE_ID::MapKey& map_key2)
const {
308 return map_key1 < map_key2;
317 class ContendedMapCleanTest;
318 class GeneratedMessageReflection;
319 class MapFieldAccessor;
324 class PROTOBUF_EXPORT MapFieldBase {
327 :
arena_(nullptr), repeated_field_(nullptr),
state_(STATE_MODIFIED_MAP) {}
335 repeated_field_(nullptr),
337 state_(STATE_MODIFIED_MAP) {}
351 virtual bool ContainsMapKey(
const MapKey& map_key)
const = 0;
352 virtual bool InsertOrLookupMapValue(
const MapKey& map_key,
354 virtual bool LookupMapValue(
const MapKey& map_key,
359 bool IsRepeatedFieldValid()
const;
361 bool IsMapValid()
const;
362 virtual bool DeleteMapValue(
const MapKey& map_key) = 0;
365 virtual void MapBegin(
MapIterator* map_iter)
const = 0;
366 virtual void MapEnd(
MapIterator* map_iter)
const = 0;
371 virtual int size()
const = 0;
372 virtual void Clear() = 0;
376 size_t SpaceUsedExcludingSelfLong()
const;
384 virtual size_t SpaceUsedExcludingSelfNoLock()
const;
388 void SyncRepeatedFieldWithMap()
const;
389 virtual void SyncRepeatedFieldWithMapNoLock()
const;
393 void SyncMapWithRepeatedField()
const;
400 void SetRepeatedDirty();
403 void* MutableRepeatedPtrField()
const;
412 #if defined(__SANITIZE_THREAD__) || defined(THREAD_SANITIZER)
414 void MutableAccess() {
421 unsigned int seq1_ = 0, seq2_ = 0;
427 STATE_MODIFIED_MAP = 0,
429 STATE_MODIFIED_REPEATED = 1,
437 mutable internal::WrappedMutex
440 mutable std::atomic<State>
state_;
443 friend class ContendedMapCleanTest;
444 friend class GeneratedMessageReflection;
446 friend class ::PROTOBUF_NAMESPACE_ID::Reflection;
447 friend class ::PROTOBUF_NAMESPACE_ID::DynamicMessage;
453 friend class ::PROTOBUF_NAMESPACE_ID::MapIterator;
455 virtual void InitializeIterator(
MapIterator* map_iter)
const = 0;
459 virtual void DeleteIterator(
MapIterator* map_iter)
const = 0;
463 virtual void CopyIterator(
MapIterator* this_iterator,
468 virtual void IncreaseIterator(
MapIterator* map_iter)
const = 0;
478 template <
typename Key,
typename T>
515 template <
typename Derived,
typename Key,
typename T,
518 class MapField :
public TypeDefinedMapFieldBase<Key, T> {
572 int size()
const override;
573 void Clear()
override;
596 template <
typename UnknownType>
598 bool (*is_valid)(
int),
uint32_t field_num,
600 return impl_.template ParseWithEnumValidation<UnknownType>(
622 template <
typename Derived,
typename Key,
typename T,
627 const auto& t =
field.GetMap();
630 if (!
it->second.IsInitialized())
return false;
635 template <
typename T,
typename Key,
typename Value,
651 bool ContainsMapKey(
const MapKey& map_key)
const override;
653 bool LookupMapValue(
const MapKey& map_key,
656 bool DeleteMapValue(
const MapKey& map_key)
override;
664 int size()
const override;
665 void Clear()
override;
674 void SyncRepeatedFieldWithMapNoLock()
const override;
675 void SyncMapWithRepeatedFieldNoLock()
const override;
676 size_t SpaceUsedExcludingSelfNoLock()
const override;
677 void SetMapIteratorValue(
MapIterator* map_iter)
const override;
691 "MapValueConstRef::GetInt64Value");
696 "MapValueConstRef::GetUInt64Value");
701 "MapValueConstRef::GetInt32Value");
706 "MapValueConstRef::GetUInt32Value");
711 return *
reinterpret_cast<bool*
>(
data_);
715 return *
reinterpret_cast<int*
>(
data_);
719 "MapValueConstRef::GetStringValue");
724 "MapValueConstRef::GetFloatValue");
725 return *
reinterpret_cast<float*
>(
data_);
729 "MapValueConstRef::GetDoubleValue");
730 return *
reinterpret_cast<double*
>(
data_);
735 "MapValueConstRef::GetMessageValue");
750 <<
"Protocol Buffer map usage error:\n"
751 <<
"MapValueConstRef::type MapValueConstRef is not initialized.";
757 template <
typename Derived,
typename K,
typename V,
761 template <
typename K,
typename V>
763 friend class ::PROTOBUF_NAMESPACE_ID::MapIterator;
777 class PROTOBUF_EXPORT MapValueRef final :
public MapValueConstRef {
821 "MapValueRef::MutableMessageValue");
831 #define HANDLE_TYPE(CPPTYPE, TYPE) \
832 case FieldDescriptor::CPPTYPE_##CPPTYPE: { \
833 delete reinterpret_cast<TYPE*>(data_); \
853 class PROTOBUF_EXPORT MapIterator {
858 key_.SetType(
field->message_type()->FindFieldByName(
"key")->cpp_type());
859 value_.SetType(
field->message_type()->FindFieldByName(
"value")->cpp_type());
860 map_->InitializeIterator(
this);
865 map_->CopyIterator(
this, other);
874 return a.map_->EqualIterator(
a,
b);
877 return !
a.map_->EqualIterator(
a,
b);
880 map_->IncreaseIterator(
this);
887 map_->IncreaseIterator(
this);
898 template <
typename Key,
typename T>
901 template <
typename Derived,
typename Key,
typename T,
921 #include <google/protobuf/port_undef.inc>
923 #endif // GOOGLE_PROTOBUF_MAP_FIELD_H__
bool InsertOrLookupMapValue(const MapKey &map_key, MapValueRef *val) override
void DestructorSkippable_
bool operator<(const MapKey &other) const
constexpr MapField(ConstantInitialized tag)
const std::string & GetStringValue() const
void SetBoolValue(bool value)
const MapValueRef & GetValueRef()
static const char MESSAGE[]
void Swap(T &lhs, T &rhs) noexcept(IsNothrowSwappable< T >::value)
const Descriptor::ReservedRange value
void MergeFrom(const MapFieldBase &other) override
internal::MapFieldBase * MutableMapData(Message *message, const FieldDescriptor *field) const
Map< Key, T > * MutableMap()
const char * _InternalParse(const char *ptr, ParseContext *ctx)
#define GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeName)
bool EqualIterator(const MapIterator &a, const MapIterator &b) const override
FieldDescriptor::CppType type_
void MapBegin(MapIterator *map_iter) const override
MapFieldLite< Derived, Key, T, kKeyFieldType, kValueFieldType, default_enum_value > MapFieldLiteType
void SetValue(const void *val)
void SetInt64Value(int64_t value)
MapIterator(const MapIterator &other)
FieldDescriptor::CppType type_
static PyObject * CopyFrom(CMessage *self, PyObject *arg)
MapIterator operator++(int)
#define GOOGLE_CHECK_EQ(A, B)
int size() const override
void UnsafeShallowSwap(MapFieldBase *other) override
bool AllAreInitialized(const Type &t)
MapIf< kIsValueEnum, T, const T & >::type CastValueType
bool ContainsMapKey(const MapKey &map_key) const override
int64_t GetInt64Value() const
HANDLE_TYPE(int32, FieldDescriptor::CPPTYPE_INT32, -1)
void SetType(FieldDescriptor::CppType type)
#define T(upbtypeconst, upbtype, ctype, default_value)
Map< Key, T > * MutableMap() override
void InternalSwap(MapField *other)
EntryType * NewEnumEntryWrapper(const Key &key, const T t) const
MapValueRef * MutableValueRef()
void DeleteIterator(MapIterator *map_iter) const override
void IncreaseIterator(MapIterator *map_iter) const override
int32_t GetInt32Value() const
grpc_core::ScopedArenaPtr arena
double GetDoubleValue() const
virtual void InitializeIterator(MapIterator *map_iter) const =0
constexpr MapFieldBase(ConstantInitialized)
MapTypeHandler< kKeyFieldType, Key > KeyTypeHandler
FieldDescriptor::CppType type() const
size_t operator()(const ::PROTOBUF_NAMESPACE_ID::MapKey &map_key) const
uint64_t GetUInt64Value() const
bool LookupMapValue(const MapKey &map_key, MapValueConstRef *val) const override
MapField< T, Key, Value, kKeyFieldType, kValueFieldType > MapFieldType
EntryType * NewEntryWrapper(const Key &key, const T &t) const
const char * _InternalParse(const char *ptr, ParseContext *ctx)
void CopyFrom(const MapKey &other)
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
static void * tag(intptr_t t)
#define TYPE_CHECK(EXPECTEDTYPE, METHOD)
void SyncRepeatedFieldWithMapNoLock() const override
static MapContainer * GetMap(PyObject *obj)
uint64_t GetUInt64Value() const
int ToIntSize(size_t size)
void SetMapIteratorValue(MapIterator *map_iter) const override
void SetStringValue(std::string val)
void SetFloatValue(float value)
EntryType * NewEntryWrapper(const Key &key, const T &t) const
MapIterator & operator=(const MapIterator &other)
friend bool operator!=(const MapIterator &a, const MapIterator &b)
uint32_t GetUInt32Value() const
unsigned __int64 uint64_t
int32_t GetInt32Value() const
void SetEnumValue(int value)
virtual void SetMapIteratorValue(MapIterator *map_iter) const =0
void InitializeIterator(MapIterator *map_iter) const override
MapKey & operator=(const MapKey &other)
TypeDefinedMapFieldBase(Arena *arena)
constexpr TypeDefinedMapFieldBase(ConstantInitialized tag)
~TypeDefinedMapFieldBase() override
MapIterator(Message *message, const FieldDescriptor *field)
EntryType * NewEnumEntryWrapper(const Key &key, const T t) const
EntryType * NewEntry() const
virtual Map< Key, T > * MutableMap()=0
const Message & GetMessageValue() const
MapKey(const MapKey &other)
bool operator==(const MapKey &other) const
float GetFloatValue() const
void SetType(FieldDescriptor::CppType type)
static const bool kIsValueEnum
virtual const Map< Key, T > & GetMap() const =0
Derived::SuperType EntryTypeTrait
bool DeleteMapValue(const MapKey &map_key) override
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MapField)
uint32_t GetUInt32Value() const
MapFieldBase(Arena *arena)
bool operator()(const ::PROTOBUF_NAMESPACE_ID::MapKey &map_key1, const ::PROTOBUF_NAMESPACE_ID::MapKey &map_key2) const
void Swap(MapFieldBase *other) override
const FieldDescriptor * field
std::string * string_value_
void CopyIterator(MapIterator *this_iteratorm, const MapIterator &that_iterator) const override
size_t SpaceUsedExcludingSelfNoLock() const override
union google::protobuf::MapKey::KeyValue val_
const char * ParseWithEnumValidation(const char *ptr, ParseContext *ctx, bool(*is_valid)(int), uint32_t field_num, InternalMetadata *metadata)
const Map< Key, T > & GetMap() const
TypeDefinedMapFieldBase()
internal::ExplicitlyConstructed< std::string > string_value_
internal::KeyMatcher< M > Key(M inner_matcher)
EntryType * NewEntry() const
void SetUInt32Value(uint32_t value)
void SetUInt64Value(uint64_t value)
internal::MapFieldBase * map_
const std::string & GetStringValue() const
int64_t GetInt64Value() const
virtual void SyncMapWithRepeatedFieldNoLock() const
void SetDoubleValue(double value)
void SyncMapWithRepeatedField() const
void SetStringValue(const std::string &value)
MapFieldLite< Derived, Key, T, kKeyFieldType, kValueFieldType > MapFieldLiteType
grpc_connectivity_state state_
void SetInt32Value(int32_t value)
int SpaceUsedExcludingSelf() const
void SetInt32Value(int32_t value)
Map< Key, T >::const_iterator & InternalGetIterator(const MapIterator *map_iter) const
MapTypeHandler< kValueFieldType, T > ValueTypeHandler
#define GOOGLE_PROTOBUF_LINKER_INITIALIZED
MapIterator & operator++()
void MapEnd(MapIterator *map_iter) const override
void SetUInt32Value(uint32_t value)
internal::WrappedMutex mutex_
friend bool operator==(const MapIterator &a, const MapIterator &b)
void InternalArenaConstructable_
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeDefinedMapFieldBase)
void SetUInt64Value(uint64_t value)
void SetBoolValue(bool value)
bool GetBoolValue() const
#define GOOGLE_LOG(LEVEL)
const Map< Key, T > & GetMap() const override
bool GetBoolValue() const
void CopyFrom(const MapValueConstRef &other)
void SetInt64Value(int64_t value)
void SyncMapWithRepeatedFieldNoLock() const override
FieldDescriptor::CppType type() const
virtual void CopyIterator(MapIterator *this_iterator, const MapIterator &other_iterator) const =0
void UnsafeShallowSwap(MapFieldBase *other) override
Message * MutableMessageValue()
grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:19