implicit_weak_message.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #ifndef GOOGLE_PROTOBUF_IMPLICIT_WEAK_MESSAGE_H__
  31. #define GOOGLE_PROTOBUF_IMPLICIT_WEAK_MESSAGE_H__
  32. #include <string>
  33. #include <google/protobuf/io/coded_stream.h>
  34. #include <google/protobuf/arena.h>
  35. #include <google/protobuf/message_lite.h>
  36. #include <google/protobuf/repeated_field.h>
  37. #ifdef SWIG
  38. #error "You cannot SWIG proto headers"
  39. #endif
  40. #include <google/protobuf/port_def.inc>
  41. // This file is logically internal-only and should only be used by protobuf
  42. // generated code.
  43. namespace google {
  44. namespace protobuf {
  45. namespace internal {
  46. // An implementation of MessageLite that treats all data as unknown. This type
  47. // acts as a placeholder for an implicit weak field in the case where the true
  48. // message type does not get linked into the binary.
  49. class PROTOBUF_EXPORT ImplicitWeakMessage : public MessageLite {
  50. public:
  51. ImplicitWeakMessage() {}
  52. explicit ImplicitWeakMessage(Arena* arena) : MessageLite(arena) {}
  53. static const ImplicitWeakMessage* default_instance();
  54. std::string GetTypeName() const override { return ""; }
  55. MessageLite* New(Arena* arena) const override {
  56. return Arena::CreateMessage<ImplicitWeakMessage>(arena);
  57. }
  58. void Clear() override { data_.clear(); }
  59. bool IsInitialized() const override { return true; }
  60. void CheckTypeAndMergeFrom(const MessageLite& other) override {
  61. data_.append(static_cast<const ImplicitWeakMessage&>(other).data_);
  62. }
  63. const char* _InternalParse(const char* ptr, ParseContext* ctx) final;
  64. size_t ByteSizeLong() const override { return data_.size(); }
  65. uint8_t* _InternalSerialize(uint8_t* target,
  66. io::EpsCopyOutputStream* stream) const final {
  67. return stream->WriteRaw(data_.data(), static_cast<int>(data_.size()),
  68. target);
  69. }
  70. int GetCachedSize() const override { return static_cast<int>(data_.size()); }
  71. typedef void InternalArenaConstructable_;
  72. private:
  73. std::string data_;
  74. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImplicitWeakMessage);
  75. };
  76. // A type handler for use with implicit weak repeated message fields.
  77. template <typename ImplicitWeakType>
  78. class ImplicitWeakTypeHandler {
  79. public:
  80. typedef MessageLite Type;
  81. static constexpr bool Moveable = false;
  82. static inline MessageLite* NewFromPrototype(const MessageLite* prototype,
  83. Arena* arena = nullptr) {
  84. return prototype->New(arena);
  85. }
  86. static inline void Delete(MessageLite* value, Arena* arena) {
  87. if (arena == nullptr) {
  88. delete value;
  89. }
  90. }
  91. static inline Arena* GetArena(MessageLite* value) {
  92. return value->GetArena();
  93. }
  94. static inline void Clear(MessageLite* value) { value->Clear(); }
  95. static void Merge(const MessageLite& from, MessageLite* to) {
  96. to->CheckTypeAndMergeFrom(from);
  97. }
  98. };
  99. } // namespace internal
  100. template <typename T>
  101. struct WeakRepeatedPtrField {
  102. using TypeHandler = internal::ImplicitWeakTypeHandler<T>;
  103. constexpr WeakRepeatedPtrField() : weak() {}
  104. explicit WeakRepeatedPtrField(Arena* arena) : weak(arena) {}
  105. ~WeakRepeatedPtrField() { weak.template Destroy<TypeHandler>(); }
  106. typedef internal::RepeatedPtrIterator<MessageLite> iterator;
  107. typedef internal::RepeatedPtrIterator<const MessageLite> const_iterator;
  108. typedef internal::RepeatedPtrOverPtrsIterator<MessageLite*, void*>
  109. pointer_iterator;
  110. typedef internal::RepeatedPtrOverPtrsIterator<const MessageLite* const,
  111. const void* const>
  112. const_pointer_iterator;
  113. iterator begin() { return iterator(base().raw_data()); }
  114. const_iterator begin() const { return iterator(base().raw_data()); }
  115. const_iterator cbegin() const { return begin(); }
  116. iterator end() { return begin() + base().size(); }
  117. const_iterator end() const { return begin() + base().size(); }
  118. const_iterator cend() const { return end(); }
  119. pointer_iterator pointer_begin() {
  120. return pointer_iterator(base().raw_mutable_data());
  121. }
  122. const_pointer_iterator pointer_begin() const {
  123. return const_pointer_iterator(base().raw_mutable_data());
  124. }
  125. pointer_iterator pointer_end() {
  126. return pointer_iterator(base().raw_mutable_data() + base().size());
  127. }
  128. const_pointer_iterator pointer_end() const {
  129. return const_pointer_iterator(base().raw_mutable_data() + base().size());
  130. }
  131. MessageLite* AddWeak(const MessageLite* prototype) {
  132. return base().AddWeak(prototype);
  133. }
  134. T* Add() { return weak.Add(); }
  135. void Clear() { base().template Clear<TypeHandler>(); }
  136. void MergeFrom(const WeakRepeatedPtrField& other) {
  137. base().template MergeFrom<TypeHandler>(other.base());
  138. }
  139. void InternalSwap(WeakRepeatedPtrField* other) {
  140. base().InternalSwap(&other->base());
  141. }
  142. const internal::RepeatedPtrFieldBase& base() const { return weak; }
  143. internal::RepeatedPtrFieldBase& base() { return weak; }
  144. // Union disables running the destructor. Which would create a strong link.
  145. // Instead we explicitly destroy the underlying base through the virtual
  146. // destructor.
  147. union {
  148. RepeatedPtrField<T> weak;
  149. };
  150. };
  151. } // namespace protobuf
  152. } // namespace google
  153. #include <google/protobuf/port_undef.inc>
  154. #endif // GOOGLE_PROTOBUF_IMPLICIT_WEAK_MESSAGE_H__