Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 10:12:24

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2008 Google Inc.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
0007 
0008 #ifndef GOOGLE_PROTOBUF_REFLECTION_INTERNAL_H__
0009 #define GOOGLE_PROTOBUF_REFLECTION_INTERNAL_H__
0010 
0011 #include "absl/strings/cord.h"
0012 #include "google/protobuf/map_field.h"
0013 #include "google/protobuf/reflection.h"
0014 #include "google/protobuf/repeated_field.h"
0015 
0016 namespace google {
0017 namespace protobuf {
0018 namespace internal {
0019 // A base class for RepeatedFieldAccessor implementations that can support
0020 // random-access efficiently. All iterator methods delegates the work to
0021 // corresponding random-access methods.
0022 class RandomAccessRepeatedFieldAccessor : public RepeatedFieldAccessor {
0023  public:
0024   Iterator* BeginIterator(const Field* /*data*/) const override {
0025     return PositionToIterator(0);
0026   }
0027   Iterator* EndIterator(const Field* data) const override {
0028     return PositionToIterator(this->Size(data));
0029   }
0030   Iterator* CopyIterator(const Field* /*data*/,
0031                          const Iterator* iterator) const override {
0032     return const_cast<Iterator*>(iterator);
0033   }
0034   Iterator* AdvanceIterator(const Field* /*data*/,
0035                             Iterator* iterator) const override {
0036     return PositionToIterator(IteratorToPosition(iterator) + 1);
0037   }
0038   bool EqualsIterator(const Field* /*data*/, const Iterator* a,
0039                       const Iterator* b) const override {
0040     return a == b;
0041   }
0042   void DeleteIterator(const Field* /*data*/,
0043                       Iterator* /*iterator*/) const override {}
0044   const Value* GetIteratorValue(const Field* data, const Iterator* iterator,
0045                                 Value* scratch_space) const override {
0046     return Get(data, static_cast<int>(IteratorToPosition(iterator)),
0047                scratch_space);
0048   }
0049 
0050  protected:
0051   ~RandomAccessRepeatedFieldAccessor() = default;
0052 
0053  private:
0054   static intptr_t IteratorToPosition(const Iterator* iterator) {
0055     return reinterpret_cast<intptr_t>(iterator);
0056   }
0057   static Iterator* PositionToIterator(intptr_t position) {
0058     return reinterpret_cast<Iterator*>(position);
0059   }
0060 };
0061 
0062 // Base class for RepeatedFieldAccessor implementations that manipulates
0063 // RepeatedField<T>.
0064 template <typename T>
0065 class RepeatedFieldWrapper : public RandomAccessRepeatedFieldAccessor {
0066  public:
0067   RepeatedFieldWrapper() {}
0068   bool IsEmpty(const Field* data) const override {
0069     return GetRepeatedField(data)->empty();
0070   }
0071   int Size(const Field* data) const override {
0072     return GetRepeatedField(data)->size();
0073   }
0074   const Value* Get(const Field* data, int index,
0075                    Value* scratch_space) const override {
0076     return ConvertFromT(GetRepeatedField(data)->Get(index), scratch_space);
0077   }
0078   void Clear(Field* data) const override {
0079     MutableRepeatedField(data)->Clear();
0080   }
0081   void Set(Field* data, int index, const Value* value) const override {
0082     MutableRepeatedField(data)->Set(index, ConvertToT(value));
0083   }
0084   void Add(Field* data, const Value* value) const override {
0085     MutableRepeatedField(data)->Add(ConvertToT(value));
0086   }
0087   void RemoveLast(Field* data) const override {
0088     MutableRepeatedField(data)->RemoveLast();
0089   }
0090   void Reserve(Field* data, int size) const override {
0091     MutableRepeatedField(data)->Reserve(size);
0092   }
0093   void SwapElements(Field* data, int index1, int index2) const override {
0094     MutableRepeatedField(data)->SwapElements(index1, index2);
0095   }
0096 
0097  protected:
0098   ~RepeatedFieldWrapper() = default;
0099   typedef RepeatedField<T> RepeatedFieldType;
0100   static const RepeatedFieldType* GetRepeatedField(const Field* data) {
0101     return reinterpret_cast<const RepeatedFieldType*>(data);
0102   }
0103   static RepeatedFieldType* MutableRepeatedField(Field* data) {
0104     return reinterpret_cast<RepeatedFieldType*>(data);
0105   }
0106 
0107   // Convert an object received by this accessor to an object to be stored in
0108   // the underlying RepeatedField.
0109   virtual T ConvertToT(const Value* value) const = 0;
0110 
0111   // Convert an object stored in RepeatedPtrField to an object that will be
0112   // returned by this accessor. If the two objects have the same type (true for
0113   // string fields with ctype=STRING), a pointer to the source object can be
0114   // returned directly. Otherwise, data should be copied from value to
0115   // scratch_space and scratch_space should be returned.
0116   virtual const Value* ConvertFromT(const T& value,
0117                                     Value* scratch_space) const = 0;
0118 };
0119 
0120 // Base class for RepeatedFieldAccessor implementations that manipulates
0121 // RepeatedPtrField<T>.
0122 template <typename T>
0123 class RepeatedPtrFieldWrapper : public RandomAccessRepeatedFieldAccessor {
0124  public:
0125   bool IsEmpty(const Field* data) const override {
0126     return GetRepeatedField(data)->empty();
0127   }
0128   int Size(const Field* data) const override {
0129     return GetRepeatedField(data)->size();
0130   }
0131   const Value* Get(const Field* data, int index,
0132                    Value* scratch_space) const override {
0133     return ConvertFromT(GetRepeatedField(data)->Get(index), scratch_space);
0134   }
0135   void Clear(Field* data) const override {
0136     MutableRepeatedField(data)->Clear();
0137   }
0138   void Set(Field* data, int index, const Value* value) const override {
0139     ConvertToT(value, MutableRepeatedField(data)->Mutable(index));
0140   }
0141   void Add(Field* data, const Value* value) const override {
0142     T* allocated = New(value);
0143     ConvertToT(value, allocated);
0144     MutableRepeatedField(data)->AddAllocated(allocated);
0145   }
0146   void RemoveLast(Field* data) const override {
0147     MutableRepeatedField(data)->RemoveLast();
0148   }
0149   void Reserve(Field* data, int size) const override {
0150     MutableRepeatedField(data)->Reserve(size);
0151   }
0152   void SwapElements(Field* data, int index1, int index2) const override {
0153     MutableRepeatedField(data)->SwapElements(index1, index2);
0154   }
0155 
0156  protected:
0157   ~RepeatedPtrFieldWrapper() = default;
0158   typedef RepeatedPtrField<T> RepeatedFieldType;
0159   static const RepeatedFieldType* GetRepeatedField(const Field* data) {
0160     return reinterpret_cast<const RepeatedFieldType*>(data);
0161   }
0162   static RepeatedFieldType* MutableRepeatedField(Field* data) {
0163     return reinterpret_cast<RepeatedFieldType*>(data);
0164   }
0165 
0166   // Create a new T instance. For repeated message fields, T can be specified
0167   // as google::protobuf::Message so we can't use "new T()" directly. In that case, value
0168   // should be a message of the same type (it's ensured by the caller) and a
0169   // new message object will be created using it.
0170   virtual T* New(const Value* value) const = 0;
0171 
0172   // Convert an object received by this accessor to an object that will be
0173   // stored in the underlying RepeatedPtrField.
0174   virtual void ConvertToT(const Value* value, T* result) const = 0;
0175 
0176   // Convert an object stored in RepeatedPtrField to an object that will be
0177   // returned by this accessor. If the two objects have the same type (true for
0178   // string fields with ctype=STRING), a pointer to the source object can be
0179   // returned directly. Otherwise, data should be copied from value to
0180   // scratch_space and scratch_space should be returned.
0181   virtual const Value* ConvertFromT(const T& value,
0182                                     Value* scratch_space) const = 0;
0183 };
0184 
0185 // An implementation of RandomAccessRepeatedFieldAccessor that manipulates
0186 // MapFieldBase.
0187 class MapFieldAccessor final : public RandomAccessRepeatedFieldAccessor {
0188  public:
0189   MapFieldAccessor() {}
0190   virtual ~MapFieldAccessor() {}
0191   bool IsEmpty(const Field* data) const override {
0192     return GetRepeatedField(data)->empty();
0193   }
0194   int Size(const Field* data) const override {
0195     return GetRepeatedField(data)->size();
0196   }
0197   const Value* Get(const Field* data, int index,
0198                    Value* scratch_space) const override {
0199     return ConvertFromEntry(GetRepeatedField(data)->Get(index), scratch_space);
0200   }
0201   void Clear(Field* data) const override {
0202     MutableRepeatedField(data)->Clear();
0203   }
0204   void Set(Field* data, int index, const Value* value) const override {
0205     ConvertToEntry(value, MutableRepeatedField(data)->Mutable(index));
0206   }
0207   void Add(Field* data, const Value* value) const override {
0208     Message* allocated = New(value);
0209     ConvertToEntry(value, allocated);
0210     MutableRepeatedField(data)->AddAllocated(allocated);
0211   }
0212   void RemoveLast(Field* data) const override {
0213     MutableRepeatedField(data)->RemoveLast();
0214   }
0215   void Reserve(Field* data, int size) const override {
0216     MutableRepeatedField(data)->Reserve(size);
0217   }
0218   void SwapElements(Field* data, int index1, int index2) const override {
0219     MutableRepeatedField(data)->SwapElements(index1, index2);
0220   }
0221   void Swap(Field* data, const internal::RepeatedFieldAccessor* other_mutator,
0222             Field* other_data) const override {
0223     ABSL_CHECK(this == other_mutator);
0224     MutableRepeatedField(data)->Swap(MutableRepeatedField(other_data));
0225   }
0226 
0227  protected:
0228   typedef RepeatedPtrField<Message> RepeatedFieldType;
0229   static const RepeatedFieldType* GetRepeatedField(const Field* data) {
0230     return reinterpret_cast<const RepeatedFieldType*>(
0231         (&reinterpret_cast<const MapFieldBase*>(data)->GetRepeatedField()));
0232   }
0233   static RepeatedFieldType* MutableRepeatedField(Field* data) {
0234     return reinterpret_cast<RepeatedFieldType*>(
0235         reinterpret_cast<MapFieldBase*>(data)->MutableRepeatedField());
0236   }
0237   virtual Message* New(const Value* value) const {
0238     return static_cast<const Message*>(value)->New();
0239   }
0240   // Convert an object received by this accessor to an MapEntry message to be
0241   // stored in the underlying MapFieldBase.
0242   virtual void ConvertToEntry(const Value* value, Message* result) const {
0243     result->CopyFrom(*static_cast<const Message*>(value));
0244   }
0245   // Convert a MapEntry message stored in the underlying MapFieldBase to an
0246   // object that will be returned by this accessor.
0247   virtual const Value* ConvertFromEntry(const Message& value,
0248                                         Value* /*scratch_space*/) const {
0249     return static_cast<const Value*>(&value);
0250   }
0251 };
0252 
0253 // Default implementations of RepeatedFieldAccessor for primitive types.
0254 template <typename T>
0255 class RepeatedFieldPrimitiveAccessor final : public RepeatedFieldWrapper<T> {
0256   typedef void Field;
0257   typedef void Value;
0258   using RepeatedFieldWrapper<T>::MutableRepeatedField;
0259 
0260  public:
0261   RepeatedFieldPrimitiveAccessor() {}
0262   void Swap(Field* data, const internal::RepeatedFieldAccessor* other_mutator,
0263             Field* other_data) const override {
0264     // Currently RepeatedFieldPrimitiveAccessor is the only implementation of
0265     // RepeatedFieldAccessor for primitive types. As we are using singletons
0266     // for these accessors, here "other_mutator" must be "this".
0267     ABSL_CHECK(this == other_mutator);
0268     MutableRepeatedField(data)->Swap(MutableRepeatedField(other_data));
0269   }
0270 
0271  protected:
0272   T ConvertToT(const Value* value) const override {
0273     return *static_cast<const T*>(value);
0274   }
0275   const Value* ConvertFromT(const T& value,
0276                             Value* /*scratch_space*/) const override {
0277     return static_cast<const Value*>(&value);
0278   }
0279 };
0280 
0281 // Default implementation of RepeatedFieldAccessor for string fields with
0282 // ctype=STRING.
0283 class RepeatedPtrFieldStringAccessor final
0284     : public RepeatedPtrFieldWrapper<std::string> {
0285   typedef void Field;
0286   typedef void Value;
0287   using RepeatedFieldAccessor::Add;
0288 
0289  public:
0290   RepeatedPtrFieldStringAccessor() {}
0291   void Swap(Field* data, const internal::RepeatedFieldAccessor* other_mutator,
0292             Field* other_data) const override {
0293     if (this == other_mutator) {
0294       MutableRepeatedField(data)->Swap(MutableRepeatedField(other_data));
0295     } else {
0296       RepeatedPtrField<std::string> tmp;
0297       tmp.Swap(MutableRepeatedField(data));
0298       int other_size = other_mutator->Size(other_data);
0299       for (int i = 0; i < other_size; ++i) {
0300         Add<std::string>(data, other_mutator->Get<std::string>(other_data, i));
0301       }
0302       int size = Size(data);
0303       other_mutator->Clear(other_data);
0304       for (int i = 0; i < size; ++i) {
0305         other_mutator->Add<std::string>(other_data, tmp.Get(i));
0306       }
0307     }
0308   }
0309 
0310  protected:
0311   std::string* New(const Value*) const override { return new std::string(); }
0312   void ConvertToT(const Value* value, std::string* result) const override {
0313     *result = *static_cast<const std::string*>(value);
0314   }
0315   const Value* ConvertFromT(const std::string& value,
0316                             Value* /*scratch_space*/) const override {
0317     return static_cast<const Value*>(&value);
0318   }
0319 };
0320 
0321 
0322 class RepeatedPtrFieldMessageAccessor final
0323     : public RepeatedPtrFieldWrapper<Message> {
0324   typedef void Field;
0325   typedef void Value;
0326 
0327  public:
0328   RepeatedPtrFieldMessageAccessor() {}
0329   void Swap(Field* data, const internal::RepeatedFieldAccessor* other_mutator,
0330             Field* other_data) const override {
0331     ABSL_CHECK(this == other_mutator);
0332     MutableRepeatedField(data)->Swap(MutableRepeatedField(other_data));
0333   }
0334 
0335  protected:
0336   Message* New(const Value* value) const override {
0337     return static_cast<const Message*>(value)->New();
0338   }
0339   void ConvertToT(const Value* value, Message* result) const override {
0340     result->CopyFrom(*static_cast<const Message*>(value));
0341   }
0342   const Value* ConvertFromT(const Message& value,
0343                             Value* /*scratch_space*/) const override {
0344     return static_cast<const Value*>(&value);
0345   }
0346 };
0347 }  // namespace internal
0348 }  // namespace protobuf
0349 }  // namespace google
0350 
0351 #endif  // GOOGLE_PROTOBUF_REFLECTION_INTERNAL_H__