aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/google/protobuf/reflection_internal.h
diff options
context:
space:
mode:
authorGravatar Feng Xiao <xfxyjwf@gmail.com>2014-11-14 11:50:31 -0800
committerGravatar Feng Xiao <xfxyjwf@gmail.com>2014-11-14 11:50:31 -0800
commitf157a5651c79a7a36e242af216a5d5b383ba8af2 (patch)
tree368bb0ca0e89ab7514302e4df00435a4ef461a28 /src/google/protobuf/reflection_internal.h
parentfaf581d20866ad5e586b3e515f6c547d2dcec2c1 (diff)
Down-integrate from internal code base (C++ maps support).
Diffstat (limited to 'src/google/protobuf/reflection_internal.h')
-rw-r--r--src/google/protobuf/reflection_internal.h68
1 files changed, 68 insertions, 0 deletions
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 <google/protobuf/map_field.h>
#include <google/protobuf/reflection.h>
#include <google/protobuf/repeated_field.h>
@@ -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<Message> RepeatedFieldType;
+ static const RepeatedFieldType* GetRepeatedField(const Field* data) {
+ return reinterpret_cast<const RepeatedFieldType*>(
+ (&reinterpret_cast<const MapFieldBase*>(data)->GetRepeatedField()));
+ }
+ static RepeatedFieldType* MutableRepeatedField(Field* data) {
+ return reinterpret_cast<RepeatedFieldType*>(
+ reinterpret_cast<MapFieldBase*>(data)->MutableRepeatedField());
+ }
+ virtual Message* New(const Value* value) const {
+ return static_cast<const Message*>(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<const Message*>(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<const Value*>(&value);
+ }
+};
+
// Default implementations of RepeatedFieldAccessor for primitive types.
template<typename T>
class RepeatedFieldPrimitiveAccessor : public RepeatedFieldWrapper<T> {