Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/google/protobuf/implicit_weak_message.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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()),
0051         data_(Arena::Create<std::string>(arena)) {}
0052 
0053   ~ImplicitWeakMessage() PROTOBUF_FINAL { delete data_; }
0054 
0055   static const ImplicitWeakMessage& default_instance();
0056 
0057   const ClassData* GetClassData() const PROTOBUF_FINAL;
0058 
0059   void Clear() PROTOBUF_FINAL { data_->clear(); }
0060 
0061   size_t ByteSizeLong() const PROTOBUF_FINAL {
0062     size_t size = data_ == nullptr ? 0 : data_->size();
0063     cached_size_.Set(internal::ToCachedSize(size));
0064     return size;
0065   }
0066 
0067   uint8_t* _InternalSerialize(
0068       uint8_t* target, io::EpsCopyOutputStream* stream) const PROTOBUF_FINAL {
0069     if (data_ == nullptr) {
0070       return target;
0071     }
0072     return stream->WriteRaw(data_->data(), static_cast<int>(data_->size()),
0073                             target);
0074   }
0075 
0076   using InternalArenaConstructable_ = void;
0077   using DestructorSkippable_ = void;
0078 
0079   static PROTOBUF_CC const char* ParseImpl(ImplicitWeakMessage* msg,
0080                                            const char* ptr, ParseContext* ctx);
0081 
0082  private:
0083   static const TcParseTable<0> table_;
0084   static const ClassDataLite<1> class_data_;
0085 
0086   static void MergeImpl(MessageLite&, const MessageLite&);
0087 
0088   static void DestroyImpl(MessageLite& msg) {
0089     static_cast<ImplicitWeakMessage&>(msg).~ImplicitWeakMessage();
0090   }
0091   static size_t ByteSizeLongImpl(const MessageLite& msg) {
0092     return static_cast<const ImplicitWeakMessage&>(msg).ByteSizeLong();
0093   }
0094 
0095   static uint8_t* _InternalSerializeImpl(const MessageLite& msg,
0096                                          uint8_t* target,
0097                                          io::EpsCopyOutputStream* stream) {
0098     return static_cast<const ImplicitWeakMessage&>(msg)._InternalSerialize(
0099         target, stream);
0100   }
0101 
0102   // This std::string is allocated on the heap, but we use a raw pointer so that
0103   // the default instance can be constant-initialized. In the const methods, we
0104   // have to handle the possibility of data_ being null.
0105   std::string* data_;
0106   google::protobuf::internal::CachedSize cached_size_{};
0107 };
0108 
0109 struct ImplicitWeakMessageDefaultType;
0110 extern ImplicitWeakMessageDefaultType implicit_weak_message_default_instance;
0111 
0112 // A type handler for use with implicit weak repeated message fields.
0113 template <typename ImplicitWeakType>
0114 class ImplicitWeakTypeHandler {
0115  public:
0116   typedef MessageLite Type;
0117   static constexpr bool Moveable = false;
0118 
0119   static inline MessageLite* NewFromPrototype(const MessageLite* prototype,
0120                                               Arena* arena = nullptr) {
0121     return prototype->New(arena);
0122   }
0123 
0124   static inline void Delete(MessageLite* value, Arena* arena) {
0125     if (arena == nullptr) {
0126       delete value;
0127     }
0128   }
0129   static inline Arena* GetArena(MessageLite* value) {
0130     return value->GetArena();
0131   }
0132   static inline void Clear(MessageLite* value) { value->Clear(); }
0133   static void Merge(const MessageLite& from, MessageLite* to) {
0134     to->CheckTypeAndMergeFrom(from);
0135   }
0136 };
0137 
0138 }  // namespace internal
0139 
0140 template <typename T>
0141 struct WeakRepeatedPtrField {
0142   using InternalArenaConstructable_ = void;
0143   using DestructorSkippable_ = void;
0144 
0145   using TypeHandler = internal::ImplicitWeakTypeHandler<T>;
0146 
0147   constexpr WeakRepeatedPtrField() : weak() {}
0148   WeakRepeatedPtrField(const WeakRepeatedPtrField& rhs)
0149       : WeakRepeatedPtrField(nullptr, rhs) {}
0150 
0151   // Arena enabled constructors: for internal use only.
0152   WeakRepeatedPtrField(internal::InternalVisibility, Arena* arena)
0153       : WeakRepeatedPtrField(arena) {}
0154   WeakRepeatedPtrField(internal::InternalVisibility, Arena* arena,
0155                        const WeakRepeatedPtrField& rhs)
0156       : WeakRepeatedPtrField(arena, rhs) {}
0157 
0158   // TODO: make this constructor private
0159   explicit WeakRepeatedPtrField(Arena* arena) : weak(arena) {}
0160 
0161   ~WeakRepeatedPtrField() {
0162     if (weak.NeedsDestroy()) {
0163       weak.DestroyProtos();
0164     }
0165   }
0166 
0167   typedef internal::RepeatedPtrIterator<MessageLite> iterator;
0168   typedef internal::RepeatedPtrIterator<const MessageLite> const_iterator;
0169   typedef internal::RepeatedPtrOverPtrsIterator<MessageLite*, void*>
0170       pointer_iterator;
0171   typedef internal::RepeatedPtrOverPtrsIterator<const MessageLite* const,
0172                                                 const void* const>
0173       const_pointer_iterator;
0174 
0175   bool empty() const { return base().empty(); }
0176   iterator begin() { return iterator(base().raw_data()); }
0177   const_iterator begin() const { return iterator(base().raw_data()); }
0178   const_iterator cbegin() const { return begin(); }
0179   iterator end() { return begin() + base().size(); }
0180   const_iterator end() const { return begin() + base().size(); }
0181   const_iterator cend() const { return end(); }
0182   pointer_iterator pointer_begin() {
0183     return pointer_iterator(base().raw_mutable_data());
0184   }
0185   const_pointer_iterator pointer_begin() const {
0186     return const_pointer_iterator(base().raw_data());
0187   }
0188   pointer_iterator pointer_end() {
0189     return pointer_iterator(base().raw_mutable_data() + base().size());
0190   }
0191   const_pointer_iterator pointer_end() const {
0192     return const_pointer_iterator(base().raw_data() + base().size());
0193   }
0194 
0195   T* Add() { return weak.Add(); }
0196   void Clear() { base().template Clear<TypeHandler>(); }
0197   void MergeFrom(const WeakRepeatedPtrField& other) {
0198     if (other.empty()) return;
0199     base().template MergeFrom<MessageLite>(other.base());
0200   }
0201   void InternalSwap(WeakRepeatedPtrField* PROTOBUF_RESTRICT other) {
0202     base().InternalSwap(&other->base());
0203   }
0204 
0205   const internal::RepeatedPtrFieldBase& base() const { return weak; }
0206   internal::RepeatedPtrFieldBase& base() { return weak; }
0207   // Union disables running the destructor. Which would create a strong link.
0208   // Instead we explicitly destroy the underlying base through the virtual
0209   // destructor.
0210   union {
0211     RepeatedPtrField<T> weak;
0212   };
0213 
0214   static constexpr size_t InternalGetArenaOffset(
0215       internal::InternalVisibility visibility) {
0216     return decltype(weak)::InternalGetArenaOffset(visibility);
0217   }
0218 
0219  private:
0220   WeakRepeatedPtrField(Arena* arena, const WeakRepeatedPtrField& rhs)
0221       : WeakRepeatedPtrField(arena) {
0222     MergeFrom(rhs);
0223   }
0224 };
0225 
0226 }  // namespace protobuf
0227 }  // namespace google
0228 
0229 #include "google/protobuf/port_undef.inc"
0230 
0231 #endif  // GOOGLE_PROTOBUF_IMPLICIT_WEAK_MESSAGE_H__