implicit_weak_message.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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() const override { return new ImplicitWeakMessage; }
  56. MessageLite* New(Arena* arena) const override {
  57. return Arena::CreateMessage<ImplicitWeakMessage>(arena);
  58. }
  59. void Clear() override { data_.clear(); }
  60. bool IsInitialized() const override { return true; }
  61. void CheckTypeAndMergeFrom(const MessageLite& other) override {
  62. data_.append(static_cast<const ImplicitWeakMessage&>(other).data_);
  63. }
  64. const char* _InternalParse(const char* ptr, ParseContext* ctx) final;
  65. size_t ByteSizeLong() const override { return data_.size(); }
  66. uint8_t* _InternalSerialize(uint8_t* target,
  67. io::EpsCopyOutputStream* stream) const final {
  68. return stream->WriteRaw(data_.data(), static_cast<int>(data_.size()),
  69. target);
  70. }
  71. int GetCachedSize() const override { return static_cast<int>(data_.size()); }
  72. typedef void InternalArenaConstructable_;
  73. private:
  74. std::string data_;
  75. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImplicitWeakMessage);
  76. };
  77. // A type handler for use with implicit weak repeated message fields.
  78. template <typename ImplicitWeakType>
  79. class ImplicitWeakTypeHandler {
  80. public:
  81. typedef MessageLite Type;
  82. static constexpr bool Moveable = false;
  83. static inline MessageLite* NewFromPrototype(const MessageLite* prototype,
  84. Arena* arena = NULL) {
  85. return prototype->New(arena);
  86. }
  87. static inline void Delete(MessageLite* value, Arena* arena) {
  88. if (arena == NULL) {
  89. delete value;
  90. }
  91. }
  92. static inline Arena* GetArena(MessageLite* value) {
  93. return value->GetArena();
  94. }
  95. static inline void* GetMaybeArenaPointer(MessageLite* value) {
  96. return value->GetArena();
  97. }
  98. static inline void Clear(MessageLite* value) { value->Clear(); }
  99. static void Merge(const MessageLite& from, MessageLite* to) {
  100. to->CheckTypeAndMergeFrom(from);
  101. }
  102. };
  103. } // namespace internal
  104. template <typename T>
  105. struct WeakRepeatedPtrField {
  106. using TypeHandler = internal::ImplicitWeakTypeHandler<T>;
  107. constexpr WeakRepeatedPtrField() : weak() {}
  108. explicit WeakRepeatedPtrField(Arena* arena) : weak(arena) {}
  109. ~WeakRepeatedPtrField() { weak.template Destroy<TypeHandler>(); }
  110. typedef internal::RepeatedPtrIterator<MessageLite> iterator;
  111. typedef internal::RepeatedPtrIterator<const MessageLite> const_iterator;
  112. typedef internal::RepeatedPtrOverPtrsIterator<MessageLite*, void*>
  113. pointer_iterator;
  114. typedef internal::RepeatedPtrOverPtrsIterator<const MessageLite* const,
  115. const void* const>
  116. const_pointer_iterator;
  117. iterator begin() { return iterator(base().raw_data()); }
  118. const_iterator begin() const { return iterator(base().raw_data()); }
  119. const_iterator cbegin() const { return begin(); }
  120. iterator end() { return begin() + base().size(); }
  121. const_iterator end() const { return begin() + base().size(); }
  122. const_iterator cend() const { return end(); }
  123. pointer_iterator pointer_begin() {
  124. return pointer_iterator(base().raw_mutable_data());
  125. }
  126. const_pointer_iterator pointer_begin() const {
  127. return const_pointer_iterator(base().raw_mutable_data());
  128. }
  129. pointer_iterator pointer_end() {
  130. return pointer_iterator(base().raw_mutable_data() + base().size());
  131. }
  132. const_pointer_iterator pointer_end() const {
  133. return const_pointer_iterator(base().raw_mutable_data() + base().size());
  134. }
  135. MessageLite* AddWeak(const MessageLite* prototype) {
  136. return base().AddWeak(prototype);
  137. }
  138. T* Add() { return weak.Add(); }
  139. void Clear() { base().template Clear<TypeHandler>(); }
  140. void MergeFrom(const WeakRepeatedPtrField& other) {
  141. base().template MergeFrom<TypeHandler>(other.base());
  142. }
  143. void InternalSwap(WeakRepeatedPtrField* other) {
  144. base().InternalSwap(&other->base());
  145. }
  146. const internal::RepeatedPtrFieldBase& base() const { return weak; }
  147. internal::RepeatedPtrFieldBase& base() { return weak; }
  148. // Union disables running the destructor. Which would create a strong link.
  149. // Instead we explicitly destroy the underlying base through the virtual
  150. // destructor.
  151. union {
  152. RepeatedPtrField<T> weak;
  153. };
  154. };
  155. } // namespace protobuf
  156. } // namespace google
  157. #include <google/protobuf/port_undef.inc>
  158. #endif // GOOGLE_PROTOBUF_IMPLICIT_WEAK_MESSAGE_H__