Back to home page

EIC code displayed by LXR

 
 

    


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

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 // Author: kenton@google.com (Kenton Varda)
0009 //  Based on original Protocol Buffers design by
0010 //  Sanjay Ghemawat, Jeff Dean, and others.
0011 //
0012 // Defines an implementation of Message which can emulate types which are not
0013 // known at compile-time.
0014 
0015 #ifndef GOOGLE_PROTOBUF_DYNAMIC_MESSAGE_H__
0016 #define GOOGLE_PROTOBUF_DYNAMIC_MESSAGE_H__
0017 
0018 #include <algorithm>
0019 #include <cstddef>
0020 #include <cstdint>
0021 #include <string>
0022 #include <vector>
0023 
0024 #include "absl/container/flat_hash_map.h"
0025 #include "absl/log/absl_log.h"
0026 #include "absl/synchronization/mutex.h"
0027 #include "google/protobuf/descriptor.h"
0028 #include "google/protobuf/message.h"
0029 #include "google/protobuf/reflection.h"
0030 #include "google/protobuf/repeated_field.h"
0031 
0032 #ifdef SWIG
0033 #error "You cannot SWIG proto headers"
0034 #endif
0035 
0036 // Must be included last.
0037 #include "google/protobuf/port_def.inc"
0038 
0039 namespace google {
0040 namespace protobuf {
0041 
0042 // Defined in other files.
0043 class Descriptor;      // descriptor.h
0044 class DescriptorPool;  // descriptor.h
0045 
0046 // Constructs implementations of Message which can emulate types which are not
0047 // known at compile-time.
0048 //
0049 // Sometimes you want to be able to manipulate protocol types that you don't
0050 // know about at compile time.  It would be nice to be able to construct
0051 // a Message object which implements the message type given by any arbitrary
0052 // Descriptor.  DynamicMessage provides this.
0053 //
0054 // As it turns out, a DynamicMessage needs to construct extra
0055 // information about its type in order to operate.  Most of this information
0056 // can be shared between all DynamicMessages of the same type.  But, caching
0057 // this information in some sort of global map would be a bad idea, since
0058 // the cached information for a particular descriptor could outlive the
0059 // descriptor itself.  To avoid this problem, DynamicMessageFactory
0060 // encapsulates this "cache".  All DynamicMessages of the same type created
0061 // from the same factory will share the same support data.  Any Descriptors
0062 // used with a particular factory must outlive the factory.
0063 //
0064 // The thread safety for this class is subtle, see comments around GetPrototype
0065 // for details
0066 class PROTOBUF_EXPORT DynamicMessageFactory : public MessageFactory {
0067  public:
0068   // Construct a DynamicMessageFactory that will search for extensions in
0069   // the DescriptorPool in which the extendee is defined.
0070   DynamicMessageFactory();
0071 
0072   // Construct a DynamicMessageFactory that will search for extensions in
0073   // the given DescriptorPool.
0074   //
0075   // DEPRECATED:  Use CodedInputStream::SetExtensionRegistry() to tell the
0076   //   parser to look for extensions in an alternate pool.  However, note that
0077   //   this is almost never what you want to do.  Almost all users should use
0078   //   the zero-arg constructor.
0079 #ifndef PROTOBUF_FUTURE_BREAKING_CHANGES
0080   explicit
0081 #endif
0082       DynamicMessageFactory(const DescriptorPool* pool);
0083   DynamicMessageFactory(const DynamicMessageFactory&) = delete;
0084   DynamicMessageFactory& operator=(const DynamicMessageFactory&) = delete;
0085 
0086   ~DynamicMessageFactory() override;
0087 
0088   // Call this to tell the DynamicMessageFactory that if it is given a
0089   // Descriptor d for which:
0090   //   d->file()->pool() == DescriptorPool::generated_pool(),
0091   // then it should delegate to MessageFactory::generated_factory() instead
0092   // of constructing a dynamic implementation of the message.  In theory there
0093   // is no down side to doing this, so it may become the default in the future.
0094   void SetDelegateToGeneratedFactory(bool enable) {
0095     delegate_to_generated_factory_ = enable;
0096   }
0097 
0098   // implements MessageFactory ---------------------------------------
0099 
0100   // Given a Descriptor, constructs the default (prototype) Message of that
0101   // type.  You can then call that message's New() method to construct a
0102   // mutable message of that type.
0103   //
0104   // Calling this method twice with the same Descriptor returns the same
0105   // object.  The returned object remains property of the factory and will
0106   // be destroyed when the factory is destroyed.  Also, any objects created
0107   // by calling the prototype's New() method share some data with the
0108   // prototype, so these must be destroyed before the DynamicMessageFactory
0109   // is destroyed.
0110   //
0111   // The given descriptor must be non-null and outlive the returned message, and
0112   // hence must outlive the DynamicMessageFactory.
0113   //
0114   // The method is thread-safe.
0115   const Message* GetPrototype(const Descriptor* type) override;
0116 
0117  private:
0118   const DescriptorPool* pool_;
0119   bool delegate_to_generated_factory_;
0120 
0121   struct TypeInfo;
0122   absl::flat_hash_map<const Descriptor*, const TypeInfo*> prototypes_;
0123   mutable absl::Mutex prototypes_mutex_;
0124 
0125   friend class DynamicMessage;
0126   const Message* GetPrototypeNoLock(const Descriptor* type);
0127 };
0128 
0129 // Helper for computing a sorted list of map entries via reflection.
0130 class PROTOBUF_EXPORT DynamicMapSorter {
0131  public:
0132   static std::vector<const Message*> Sort(const Message& message, int map_size,
0133                                           const Reflection* reflection,
0134                                           const FieldDescriptor* field) {
0135     std::vector<const Message*> result;
0136     result.reserve(map_size);
0137     RepeatedFieldRef<Message> map_field =
0138         reflection->GetRepeatedFieldRef<Message>(message, field);
0139     for (auto it = map_field.begin(); it != map_field.end(); ++it) {
0140       result.push_back(&*it);
0141     }
0142     MapEntryMessageComparator comparator(field->message_type());
0143     std::stable_sort(result.begin(), result.end(), comparator);
0144     // Complain if the keys aren't in ascending order.
0145 #ifndef NDEBUG
0146     for (size_t j = 1; j < static_cast<size_t>(map_size); ++j) {
0147       if (!comparator(result[j - 1], result[j])) {
0148         ABSL_LOG(ERROR) << (comparator(result[j], result[j - 1])
0149                                 ? "internal error in map key sorting"
0150                                 : "map keys are not unique");
0151       }
0152     }
0153 #endif
0154     return result;
0155   }
0156 
0157  private:
0158   class PROTOBUF_EXPORT MapEntryMessageComparator {
0159    public:
0160     explicit MapEntryMessageComparator(const Descriptor* descriptor)
0161         : field_(descriptor->field(0)) {}
0162 
0163     bool operator()(const Message* a, const Message* b) {
0164       const Reflection* reflection = a->GetReflection();
0165       switch (field_->cpp_type()) {
0166         case FieldDescriptor::CPPTYPE_BOOL: {
0167           bool first = reflection->GetBool(*a, field_);
0168           bool second = reflection->GetBool(*b, field_);
0169           return first < second;
0170         }
0171         case FieldDescriptor::CPPTYPE_INT32: {
0172           int32_t first = reflection->GetInt32(*a, field_);
0173           int32_t second = reflection->GetInt32(*b, field_);
0174           return first < second;
0175         }
0176         case FieldDescriptor::CPPTYPE_INT64: {
0177           int64_t first = reflection->GetInt64(*a, field_);
0178           int64_t second = reflection->GetInt64(*b, field_);
0179           return first < second;
0180         }
0181         case FieldDescriptor::CPPTYPE_UINT32: {
0182           uint32_t first = reflection->GetUInt32(*a, field_);
0183           uint32_t second = reflection->GetUInt32(*b, field_);
0184           return first < second;
0185         }
0186         case FieldDescriptor::CPPTYPE_UINT64: {
0187           uint64_t first = reflection->GetUInt64(*a, field_);
0188           uint64_t second = reflection->GetUInt64(*b, field_);
0189           return first < second;
0190         }
0191         case FieldDescriptor::CPPTYPE_STRING: {
0192           std::string first = reflection->GetString(*a, field_);
0193           std::string second = reflection->GetString(*b, field_);
0194           return first < second;
0195         }
0196         default:
0197           ABSL_DLOG(FATAL) << "Invalid key for map field.";
0198           return true;
0199       }
0200     }
0201 
0202    private:
0203     const FieldDescriptor* field_;
0204   };
0205 };
0206 
0207 }  // namespace protobuf
0208 }  // namespace google
0209 
0210 #include "google/protobuf/port_undef.inc"
0211 
0212 #endif  // GOOGLE_PROTOBUF_DYNAMIC_MESSAGE_H__