From f157a5651c79a7a36e242af216a5d5b383ba8af2 Mon Sep 17 00:00:00 2001 From: Feng Xiao Date: Fri, 14 Nov 2014 11:50:31 -0800 Subject: Down-integrate from internal code base (C++ maps support). --- src/google/protobuf/reflection_internal.h | 68 +++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) (limited to 'src/google/protobuf/reflection_internal.h') diff --git a/src/google/protobuf/reflection_internal.h b/src/google/protobuf/reflection_internal.h index e25e193c..fcb42471 100644 --- a/src/google/protobuf/reflection_internal.h +++ b/src/google/protobuf/reflection_internal.h @@ -31,6 +31,7 @@ #ifndef GOOGLE_PROTOBUF_REFLECTION_INTERNAL_H__ #define GOOGLE_PROTOBUF_REFLECTION_INTERNAL_H__ +#include #include #include @@ -199,6 +200,73 @@ class RepeatedPtrFieldWrapper : public RandomAccessRepeatedFieldAccessor { Value* scratch_space) const = 0; }; +// An implementation of RandomAccessRepeatedFieldAccessor that manipulates +// MapFieldBase. +class MapFieldAccessor : public RandomAccessRepeatedFieldAccessor { + public: + MapFieldAccessor() {} + virtual ~MapFieldAccessor() {} + virtual bool IsEmpty(const Field* data) const { + return GetRepeatedField(data)->empty(); + } + virtual int Size(const Field* data) const { + return GetRepeatedField(data)->size(); + } + virtual const Value* Get(const Field* data, int index, + Value* scratch_space) const { + return ConvertFromEntry(GetRepeatedField(data)->Get(index), scratch_space); + } + virtual void Clear(Field* data) const { + MutableRepeatedField(data)->Clear(); + } + virtual void Set(Field* data, int index, const Value* value) const { + ConvertToEntry(value, MutableRepeatedField(data)->Mutable(index)); + } + virtual void Add(Field* data, const Value* value) const { + Message* allocated = New(value); + ConvertToEntry(value, allocated); + MutableRepeatedField(data)->AddAllocated(allocated); + } + virtual void RemoveLast(Field* data) const { + MutableRepeatedField(data)->RemoveLast(); + } + virtual void SwapElements(Field* data, int index1, int index2) const { + MutableRepeatedField(data)->SwapElements(index1, index2); + } + virtual void Swap( + Field* data, + const internal::RepeatedFieldAccessor* other_mutator, + Field* other_data) const { + GOOGLE_CHECK(this == other_mutator); + MutableRepeatedField(data)->Swap(MutableRepeatedField(other_data)); + } + + protected: + typedef RepeatedPtrField RepeatedFieldType; + static const RepeatedFieldType* GetRepeatedField(const Field* data) { + return reinterpret_cast( + (&reinterpret_cast(data)->GetRepeatedField())); + } + static RepeatedFieldType* MutableRepeatedField(Field* data) { + return reinterpret_cast( + reinterpret_cast(data)->MutableRepeatedField()); + } + virtual Message* New(const Value* value) const { + return static_cast(value)->New(); + } + // Convert an object received by this accessor to an MapEntry message to be + // stored in the underlying MapFieldBase. + virtual void ConvertToEntry(const Value* value, Message* result) const { + result->CopyFrom(*static_cast(value)); + } + // Convert a MapEntry message stored in the underlying MapFieldBase to an + // object that will be returned by this accessor. + virtual const Value* ConvertFromEntry(const Message& value, + Value* scratch_space) const { + return static_cast(&value); + } +}; + // Default implementations of RepeatedFieldAccessor for primitive types. template class RepeatedFieldPrimitiveAccessor : public RepeatedFieldWrapper { -- cgit v1.2.3