From d36c0c538a545fac5d9db6ba65c525246d4efa95 Mon Sep 17 00:00:00 2001 From: Feng Xiao Date: Wed, 29 Mar 2017 14:32:48 -0700 Subject: Down-integrate from google3. --- src/google/protobuf/map_field.h | 529 +++++++++++++++++++++++++++++++++++----- 1 file changed, 470 insertions(+), 59 deletions(-) (limited to 'src/google/protobuf/map_field.h') diff --git a/src/google/protobuf/map_field.h b/src/google/protobuf/map_field.h index d6af8532..6d904076 100644 --- a/src/google/protobuf/map_field.h +++ b/src/google/protobuf/map_field.h @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -62,14 +63,10 @@ class LIBPROTOBUF_EXPORT MapFieldBase { MapFieldBase() : arena_(NULL), repeated_field_(NULL), - entry_descriptor_(NULL), - assign_descriptor_callback_(NULL), state_(STATE_MODIFIED_MAP) {} explicit MapFieldBase(Arena* arena) : arena_(arena), repeated_field_(NULL), - entry_descriptor_(NULL), - assign_descriptor_callback_(NULL), state_(STATE_MODIFIED_MAP) { // Mutex's destructor needs to be called explicitly to release resources // acquired in its constructor. @@ -99,11 +96,15 @@ class LIBPROTOBUF_EXPORT MapFieldBase { // Returns the number of bytes used by the repeated field, excluding // sizeof(*this) - int SpaceUsedExcludingSelf() const; + size_t SpaceUsedExcludingSelfLong() const; + + int SpaceUsedExcludingSelf() const { + return internal::ToIntSize(SpaceUsedExcludingSelfLong()); + } protected: // Gets the size of space used by map field. - virtual int SpaceUsedExcludingSelfNoLock() const; + virtual size_t SpaceUsedExcludingSelfNoLock() const; // Synchronizes the content in Map to RepeatedPtrField if there is any change // to Map after last synchronization. @@ -124,9 +125,6 @@ class LIBPROTOBUF_EXPORT MapFieldBase { // Provides derived class the access to repeated field. void* MutableRepeatedPtrField() const; - // Creates descriptor for only one time. - void InitMetadataOnce() const; - enum State { STATE_MODIFIED_MAP = 0, // map has newly added data that has not been // synchronized to repeated field @@ -137,13 +135,6 @@ class LIBPROTOBUF_EXPORT MapFieldBase { Arena* arena_; mutable RepeatedPtrField* repeated_field_; - // MapEntry can only be created from MapField. To create MapEntry, MapField - // needs to know its descriptor, because MapEntry is not generated class which - // cannot initialize its own descriptor by calling generated - // descriptor-assign-function. Thus, we need to register a callback to - // initialize MapEntry's descriptor. - const Descriptor** entry_descriptor_; - void (*assign_descriptor_callback_)(); mutable Mutex mutex_; // The thread to synchronize map and repeated field // needs to get lock first; @@ -211,27 +202,24 @@ class TypeDefinedMapFieldBase : public MapFieldBase { // This class provides access to map field using generated api. It is used for // internal generated message implentation only. Users should never use this // directly. -template -class MapField : public TypeDefinedMapFieldBase, - public MapFieldLite { + WireFormatLite::FieldType kValueFieldType, int default_enum_value = 0> +class MapField : public TypeDefinedMapFieldBase { // Provide utilities to parse/serialize key/value. Provide utilities to // manipulate internal stored type. typedef MapTypeHandler KeyTypeHandler; typedef MapTypeHandler ValueTypeHandler; // Define message type for internal repeated field. - typedef MapEntry - EntryType; + typedef Derived EntryType; typedef MapEntryLite EntryLiteType; // Define abbreviation for parent MapFieldLite - typedef MapFieldLite MapFieldLiteType; + typedef MapFieldLite + MapFieldLiteType; // Enum needs to be handled differently from other types because it has // different exposed type in google::protobuf::Map's api and repeated field's api. For @@ -241,58 +229,62 @@ class MapField : public TypeDefinedMapFieldBase, typedef typename MapIf::type CastValueType; public: - MapField(); - explicit MapField(Arena* arena); - // MapField doesn't own the default_entry, which means default_entry must - // outlive the lifetime of MapField. - MapField(const Message* default_entry); - // For tests only. - MapField(Arena* arena, const Message* default_entry); - ~MapField(); + MapField() {} + explicit MapField(Arena* arena) + : TypeDefinedMapFieldBase(arena), impl_(arena) {} // Implement MapFieldBase bool ContainsMapKey(const MapKey& map_key) const; bool InsertOrLookupMapValue(const MapKey& map_key, MapValueRef* val); bool DeleteMapValue(const MapKey& map_key); - // Accessors - const Map& GetMap() const; - Map* MutableMap(); + const Map& GetMap() const { + MapFieldBase::SyncMapWithRepeatedField(); + return impl_.GetMap(); + } + + Map* MutableMap() { + MapFieldBase::SyncMapWithRepeatedField(); + Map* result = impl_.MutableMap(); + MapFieldBase::SetMapDirty(); + return result; + } // Convenient methods for generated message implementation. int size() const; void Clear(); - void MergeFrom(const MapFieldLiteType& other); - void Swap(MapFieldLiteType* other); - - // Allocates metadata only if this MapField is part of a generated message. - void SetEntryDescriptor(const Descriptor** descriptor); - void SetAssignDescriptorCallback(void (*callback)()); + void MergeFrom(const MapField& other); + void Swap(MapField* other); + + // Used in the implementation of parsing. Caller should take the ownership iff + // arena_ is NULL. + EntryType* NewEntry() const { return impl_.NewEntry(); } + // Used in the implementation of serializing enum value type. Caller should + // take the ownership iff arena_ is NULL. + EntryType* NewEnumEntryWrapper(const Key& key, const T t) const { + return impl_.NewEnumEntryWrapper(key, t); + } + // Used in the implementation of serializing other value types. Caller should + // take the ownership iff arena_ is NULL. + EntryType* NewEntryWrapper(const Key& key, const T& t) const { + return impl_.NewEntryWrapper(key, t); + } private: + MapFieldLiteType impl_; + typedef void InternalArenaConstructable_; typedef void DestructorSkippable_; - // MapField needs MapEntry's default instance to create new MapEntry. - void InitDefaultEntryOnce() const; - - // Manually set default entry instance. For test only. - void SetDefaultEntryOnce(const EntryType* default_entry) const; - - // Convenient methods to get internal google::protobuf::Map - const Map& GetInternalMap() const; - Map* MutableInternalMap(); - // Implements MapFieldBase void SyncRepeatedFieldWithMapNoLock() const; void SyncMapWithRepeatedFieldNoLock() const; - int SpaceUsedExcludingSelfNoLock() const; + size_t SpaceUsedExcludingSelfNoLock() const; void SetMapIteratorValue(MapIterator* map_iter) const; - mutable const EntryType* default_entry_; - friend class ::google::protobuf::Arena; + friend class MapFieldStateTest; // For testing, it needs raw access to impl_ }; class LIBPROTOBUF_EXPORT DynamicMapField: public TypeDefinedMapFieldBase { @@ -318,12 +310,395 @@ class LIBPROTOBUF_EXPORT DynamicMapField: public TypeDefinedMapFieldBase + friend class internal::TypeDefinedMapFieldBase; + friend class MapIterator; + friend class internal::DynamicMapField; + + union KeyValue { + KeyValue() {} + string* string_value_; + int64 int64_value_; + int32 int32_value_; + uint64 uint64_value_; + uint32 uint32_value_; + bool bool_value_; + } val_; + + void SetType(FieldDescriptor::CppType type) { + if (type_ == type) return; + if (type_ == FieldDescriptor::CPPTYPE_STRING) { + delete val_.string_value_; + } + type_ = type; + if (type_ == FieldDescriptor::CPPTYPE_STRING) { + val_.string_value_ = new string; + } + } + + // type_ is 0 or a valid FieldDescriptor::CppType. + int type_; +}; + +// MapValueRef points to a map value. +class LIBPROTOBUF_EXPORT MapValueRef { + public: + MapValueRef() : data_(NULL), type_(0) {} + + void SetInt64Value(int64 value) { + TYPE_CHECK(FieldDescriptor::CPPTYPE_INT64, + "MapValueRef::SetInt64Value"); + *reinterpret_cast(data_) = value; + } + void SetUInt64Value(uint64 value) { + TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT64, + "MapValueRef::SetUInt64Value"); + *reinterpret_cast(data_) = value; + } + void SetInt32Value(int32 value) { + TYPE_CHECK(FieldDescriptor::CPPTYPE_INT32, + "MapValueRef::SetInt32Value"); + *reinterpret_cast(data_) = value; + } + void SetUInt32Value(uint32 value) { + TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT32, + "MapValueRef::SetUInt32Value"); + *reinterpret_cast(data_) = value; + } + void SetBoolValue(bool value) { + TYPE_CHECK(FieldDescriptor::CPPTYPE_BOOL, + "MapValueRef::SetBoolValue"); + *reinterpret_cast(data_) = value; + } + // TODO(jieluo) - Checks that enum is member. + void SetEnumValue(int value) { + TYPE_CHECK(FieldDescriptor::CPPTYPE_ENUM, + "MapValueRef::SetEnumValue"); + *reinterpret_cast(data_) = value; + } + void SetStringValue(const string& value) { + TYPE_CHECK(FieldDescriptor::CPPTYPE_STRING, + "MapValueRef::SetStringValue"); + *reinterpret_cast(data_) = value; + } + void SetFloatValue(float value) { + TYPE_CHECK(FieldDescriptor::CPPTYPE_FLOAT, + "MapValueRef::SetFloatValue"); + *reinterpret_cast(data_) = value; + } + void SetDoubleValue(double value) { + TYPE_CHECK(FieldDescriptor::CPPTYPE_DOUBLE, + "MapValueRef::SetDoubleValue"); + *reinterpret_cast(data_) = value; + } + + int64 GetInt64Value() const { + TYPE_CHECK(FieldDescriptor::CPPTYPE_INT64, + "MapValueRef::GetInt64Value"); + return *reinterpret_cast(data_); + } + uint64 GetUInt64Value() const { + TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT64, + "MapValueRef::GetUInt64Value"); + return *reinterpret_cast(data_); + } + int32 GetInt32Value() const { + TYPE_CHECK(FieldDescriptor::CPPTYPE_INT32, + "MapValueRef::GetInt32Value"); + return *reinterpret_cast(data_); + } + uint32 GetUInt32Value() const { + TYPE_CHECK(FieldDescriptor::CPPTYPE_UINT32, + "MapValueRef::GetUInt32Value"); + return *reinterpret_cast(data_); + } + bool GetBoolValue() const { + TYPE_CHECK(FieldDescriptor::CPPTYPE_BOOL, + "MapValueRef::GetBoolValue"); + return *reinterpret_cast(data_); + } + int GetEnumValue() const { + TYPE_CHECK(FieldDescriptor::CPPTYPE_ENUM, + "MapValueRef::GetEnumValue"); + return *reinterpret_cast(data_); + } + const string& GetStringValue() const { + TYPE_CHECK(FieldDescriptor::CPPTYPE_STRING, + "MapValueRef::GetStringValue"); + return *reinterpret_cast(data_); + } + float GetFloatValue() const { + TYPE_CHECK(FieldDescriptor::CPPTYPE_FLOAT, + "MapValueRef::GetFloatValue"); + return *reinterpret_cast(data_); + } + double GetDoubleValue() const { + TYPE_CHECK(FieldDescriptor::CPPTYPE_DOUBLE, + "MapValueRef::GetDoubleValue"); + return *reinterpret_cast(data_); + } + + const Message& GetMessageValue() const { + TYPE_CHECK(FieldDescriptor::CPPTYPE_MESSAGE, + "MapValueRef::GetMessageValue"); + return *reinterpret_cast(data_); + } + + Message* MutableMessageValue() { + TYPE_CHECK(FieldDescriptor::CPPTYPE_MESSAGE, + "MapValueRef::MutableMessageValue"); + return reinterpret_cast(data_); + } + + private: + template + friend class internal::MapField; + template + friend class internal::TypeDefinedMapFieldBase; + friend class MapIterator; + friend class internal::GeneratedMessageReflection; + friend class internal::DynamicMapField; + + void SetType(FieldDescriptor::CppType type) { + type_ = type; + } + + FieldDescriptor::CppType type() const { + if (type_ == 0 || data_ == NULL) { + GOOGLE_LOG(FATAL) + << "Protocol Buffer map usage error:\n" + << "MapValueRef::type MapValueRef is not initialized."; + } + return (FieldDescriptor::CppType)type_; + } + void SetValue(const void* val) { + data_ = const_cast(val); + } + void CopyFrom(const MapValueRef& other) { + type_ = other.type_; + data_ = other.data_; + } + // Only used in DynamicMapField + void DeleteData() { + switch (type_) { +#define HANDLE_TYPE(CPPTYPE, TYPE) \ + case google::protobuf::FieldDescriptor::CPPTYPE_##CPPTYPE: { \ + delete reinterpret_cast(data_); \ + break; \ + } + HANDLE_TYPE(INT32, int32); + HANDLE_TYPE(INT64, int64); + HANDLE_TYPE(UINT32, uint32); + HANDLE_TYPE(UINT64, uint64); + HANDLE_TYPE(DOUBLE, double); + HANDLE_TYPE(FLOAT, float); + HANDLE_TYPE(BOOL, bool); + HANDLE_TYPE(STRING, string); + HANDLE_TYPE(ENUM, int32); + HANDLE_TYPE(MESSAGE, Message); +#undef HANDLE_TYPE + } + } + // data_ point to a map value. MapValueRef does not + // own this value. + void* data_; + // type_ is 0 or a valid FieldDescriptor::CppType. + int type_; +}; + +#undef TYPE_CHECK + class LIBPROTOBUF_EXPORT MapIterator { public: MapIterator(Message* message, const FieldDescriptor* field) { @@ -373,7 +748,7 @@ class LIBPROTOBUF_EXPORT MapIterator { template friend class internal::TypeDefinedMapFieldBase; friend class internal::DynamicMapField; - template @@ -392,6 +767,42 @@ class LIBPROTOBUF_EXPORT MapIterator { }; } // namespace protobuf - } // namespace google + +GOOGLE_PROTOBUF_HASH_NAMESPACE_DECLARATION_START +template<> +struct hash { + size_t + operator()(const google::protobuf::MapKey& map_key) const { + switch (map_key.type()) { + case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE: + case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT: + case google::protobuf::FieldDescriptor::CPPTYPE_ENUM: + case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: + GOOGLE_LOG(FATAL) << "Unsupported"; + break; + case google::protobuf::FieldDescriptor::CPPTYPE_STRING: + return hash()(map_key.GetStringValue()); + case google::protobuf::FieldDescriptor::CPPTYPE_INT64: + return hash< ::google::protobuf::int64>()(map_key.GetInt64Value()); + case google::protobuf::FieldDescriptor::CPPTYPE_INT32: + return hash< ::google::protobuf::int32>()(map_key.GetInt32Value()); + case google::protobuf::FieldDescriptor::CPPTYPE_UINT64: + return hash< ::google::protobuf::uint64>()(map_key.GetUInt64Value()); + case google::protobuf::FieldDescriptor::CPPTYPE_UINT32: + return hash< ::google::protobuf::uint32>()(map_key.GetUInt32Value()); + case google::protobuf::FieldDescriptor::CPPTYPE_BOOL: + return hash()(map_key.GetBoolValue()); + } + GOOGLE_LOG(FATAL) << "Can't get here."; + return 0; + } + bool + operator()(const google::protobuf::MapKey& map_key1, + const google::protobuf::MapKey& map_key2) const { + return map_key1 < map_key2; + } +}; +GOOGLE_PROTOBUF_HASH_NAMESPACE_DECLARATION_END + #endif // GOOGLE_PROTOBUF_MAP_FIELD_H__ -- cgit v1.2.3