Back to home page

EIC code displayed by LXR

 
 

    


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

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_IMPLICIT_WEAK_MESSAGE_H__
0009 #define GOOGLE_PROTOBUF_IMPLICIT_WEAK_MESSAGE_H__
0010 
0011 #include <cstddef>
0012 #include <string>
0013 
0014 #include "google/protobuf/arena.h"
0015 #include "google/protobuf/generated_message_tctable_decl.h"
0016 #include "google/protobuf/io/coded_stream.h"
0017 #include "google/protobuf/message_lite.h"
0018 #include "google/protobuf/repeated_field.h"
0019 
0020 #ifdef SWIG
0021 #error "You cannot SWIG proto headers"
0022 #endif
0023 
0024 // Must be included last.
0025 #include "google/protobuf/port_def.inc"
0026 
0027 // This file is logically internal-only and should only be used by protobuf
0028 // generated code.
0029 
0030 namespace google {
0031 namespace protobuf {
0032 namespace internal {
0033 
0034 // An implementation of MessageLite that treats all data as unknown. This type
0035 // acts as a placeholder for an implicit weak field in the case where the true
0036 // message type does not get linked into the binary.
0037 class PROTOBUF_EXPORT ImplicitWeakMessage final : public MessageLite {
0038  public:
0039   ImplicitWeakMessage() : ImplicitWeakMessage(nullptr) {}
0040   explicit constexpr ImplicitWeakMessage(ConstantInitialized);
0041   ImplicitWeakMessage(const ImplicitWeakMessage&) = delete;
0042   ImplicitWeakMessage& operator=(const ImplicitWeakMessage&) = delete;
0043 
0044   // Arena enabled constructors: for internal use only.
0045   ImplicitWeakMessage(internal::InternalVisibility, Arena* arena)
0046       : ImplicitWeakMessage(arena) {}
0047 
0048   // TODO: make this constructor private
0049   explicit ImplicitWeakMessage(Arena* arena)
0050       : MessageLite(arena, class_data_.base()), data_(new std::string) {}
0051 
0052   ~ImplicitWeakMessage() PROTOBUF_FINAL {
0053     // data_ will be null in the default instance, but we can safely call delete
0054     // here because the default instance will never be destroyed.
0055     delete data_;
0056   }
0057 
0058   static const ImplicitWeakMessage& default_instance();
0059 
0060   const ClassData* GetClassData() const PROTOBUF_FINAL;
0061 
0062   MessageLite* New(Arena* arena) const PROTOBUF_FINAL {
0063     return Arena::Create<ImplicitWeakMessage>(arena);
0064   }
0065 
0066   void Clear() PROTOBUF_FINAL { data_->clear(); }
0067 
0068   size_t ByteSizeLong() const PROTOBUF_FINAL {
0069     size_t size = data_ == nullptr ? 0 : data_->size();
0070     cached_size_.Set(internal::ToCachedSize(size));
0071     return size;
0072   }
0073 
0074   uint8_t* _InternalSerialize(
0075       uint8_t* target, io::EpsCopyOutputStream* stream) const PROTOBUF_FINAL {
0076     if (data_ == nullptr) {
0077       return target;
0078     }
0079     return stream->WriteRaw(data_->data(), static_cast<int>(data_->size()),
0080                             target);
0081   }
0082 
0083   typedef void InternalArenaConstructable_;
0084 
0085   static PROTOBUF_CC const char* ParseImpl(ImplicitWeakMessage* msg,
0086                                            const char* ptr, ParseContext* ctx);
0087 
0088  private:
0089   static const TcParseTable<0> table_;
0090   static const ClassDataLite<1> class_data_;
0091 
0092   static void MergeImpl(MessageLite&, const MessageLite&);
0093 
0094   static void ClearImpl(MessageLite& msg) {
0095     static_cast<ImplicitWeakMessage&>(msg).Clear();
0096   }
0097 
0098   static size_t ByteSizeLongImpl(const MessageLite& msg) {
0099     return static_cast<const ImplicitWeakMessage&>(msg).ByteSizeLong();
0100   }
0101 
0102   static uint8_t* _InternalSerializeImpl(const MessageLite& msg,
0103                                          uint8_t* target,
0104                                          io::EpsCopyOutputStream* stream) {
0105     return static_cast<const ImplicitWeakMessage&>(msg)._InternalSerialize(
0106         target, stream);
0107   }
0108 
0109   // This std::string is allocated on the heap, but we use a raw pointer so that
0110   // the default instance can be constant-initialized. In the const methods, we
0111   // have to handle the possibility of data_ being null.
0112   std::string* data_;
0113   mutable google::protobuf::internal::CachedSize cached_size_{};
0114 };
0115 
0116 struct ImplicitWeakMessageDefaultType;
0117 extern ImplicitWeakMessageDefaultType implicit_weak_message_default_instance;
0118 
0119 // A type handler for use with implicit weak repeated message fields.
0120 template <typename ImplicitWeakType>
0121 class ImplicitWeakTypeHandler {
0122  public:
0123   typedef MessageLite Type;
0124   static constexpr bool Moveable = false;
0125 
0126   static inline MessageLite* NewFromPrototype(const MessageLite* prototype,
0127                                               Arena* arena = nullptr) {
0128     return prototype->New(arena);
0129   }
0130 
0131   static inline void Delete(MessageLite* value, Arena* arena) {
0132     if (arena == nullptr) {
0133       delete value;
0134     }
0135   }
0136   static inline Arena* GetArena(MessageLite* value) {
0137     return value->GetArena();
0138   }
0139   static inline void Clear(MessageLite* value) { value->Clear(); }
0140   static void Merge(const MessageLite& from, MessageLite* to) {
0141     to->CheckTypeAndMergeFrom(from);
0142   }
0143 };
0144 
0145 }  // namespace internal
0146 
0147 template <typename T>
0148 struct WeakRepeatedPtrField {
0149   using InternalArenaConstructable_ = void;
0150   using DestructorSkippable_ = void;
0151 
0152   using TypeHandler = internal::ImplicitWeakTypeHandler<T>;
0153 
0154   constexpr WeakRepeatedPtrField() : weak() {}
0155   WeakRepeatedPtrField(const WeakRepeatedPtrField& rhs)
0156       : WeakRepeatedPtrField(nullptr, rhs) {}
0157 
0158   // Arena enabled constructors: for internal use only.
0159   WeakRepeatedPtrField(internal::InternalVisibility, Arena* arena)
0160       : WeakRepeatedPtrField(arena) {}
0161   WeakRepeatedPtrField(internal::InternalVisibility, Arena* arena,
0162                        const WeakRepeatedPtrField& rhs)
0163       : WeakRepeatedPtrField(arena, rhs) {}
0164 
0165   // TODO: make this constructor private
0166   explicit WeakRepeatedPtrField(Arena* arena) : weak(arena) {}
0167 
0168   ~WeakRepeatedPtrField() {
0169     if (weak.NeedsDestroy()) {
0170       weak.DestroyProtos();
0171     }
0172   }
0173 
0174   typedef internal::RepeatedPtrIterator<MessageLite> iterator;
0175   typedef internal::RepeatedPtrIterator<const MessageLite> const_iterator;
0176   typedef internal::RepeatedPtrOverPtrsIterator<MessageLite*, void*>
0177       pointer_iterator;
0178   typedef internal::RepeatedPtrOverPtrsIterator<const MessageLite* const,
0179                                                 const void* const>
0180       const_pointer_iterator;
0181 
0182   bool empty() const { return base().empty(); }
0183   iterator begin() { return iterator(base().raw_data()); }
0184   const_iterator begin() const { return iterator(base().raw_data()); }
0185   const_iterator cbegin() const { return begin(); }
0186   iterator end() { return begin() + base().size(); }
0187   const_iterator end() const { return begin() + base().size(); }
0188   const_iterator cend() const { return end(); }
0189   pointer_iterator pointer_begin() {
0190     return pointer_iterator(base().raw_mutable_data());
0191   }
0192   const_pointer_iterator pointer_begin() const {
0193     return const_pointer_iterator(base().raw_data());
0194   }
0195   pointer_iterator pointer_end() {
0196     return pointer_iterator(base().raw_mutable_data() + base().size());
0197   }
0198   const_pointer_iterator pointer_end() const {
0199     return const_pointer_iterator(base().raw_data() + base().size());
0200   }
0201 
0202   T* Add() { return weak.Add(); }
0203   void Clear() { base().template Clear<TypeHandler>(); }
0204   void MergeFrom(const WeakRepeatedPtrField& other) {
0205     if (other.empty()) return;
0206     base().template MergeFrom<MessageLite>(other.base());
0207   }
0208   void InternalSwap(WeakRepeatedPtrField* PROTOBUF_RESTRICT other) {
0209     base().InternalSwap(&other->base());
0210   }
0211 
0212   const internal::RepeatedPtrFieldBase& base() const { return weak; }
0213   internal::RepeatedPtrFieldBase& base() { return weak; }
0214   // Union disables running the destructor. Which would create a strong link.
0215   // Instead we explicitly destroy the underlying base through the virtual
0216   // destructor.
0217   union {
0218     RepeatedPtrField<T> weak;
0219   };
0220 
0221  private:
0222   WeakRepeatedPtrField(Arena* arena, const WeakRepeatedPtrField& rhs)
0223       : WeakRepeatedPtrField(arena) {
0224     MergeFrom(rhs);
0225   }
0226 };
0227 
0228 }  // namespace protobuf
0229 }  // namespace google
0230 
0231 #include "google/protobuf/port_undef.inc"
0232 
0233 #endif  // GOOGLE_PROTOBUF_IMPLICIT_WEAK_MESSAGE_H__