You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

implicit_weak_message.h 7.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. // Must be included last.
  41. #include <google/protobuf/port_def.inc>
  42. // This file is logically internal-only and should only be used by protobuf
  43. // generated code.
  44. namespace google {
  45. namespace protobuf {
  46. namespace internal {
  47. // An implementation of MessageLite that treats all data as unknown. This type
  48. // acts as a placeholder for an implicit weak field in the case where the true
  49. // message type does not get linked into the binary.
  50. class PROTOBUF_EXPORT ImplicitWeakMessage : public MessageLite {
  51. public:
  52. ImplicitWeakMessage() : data_(new std::string) {}
  53. explicit constexpr ImplicitWeakMessage(ConstantInitialized)
  54. : data_(nullptr) {}
  55. explicit ImplicitWeakMessage(Arena* arena)
  56. : MessageLite(arena), data_(new std::string) {}
  57. ~ImplicitWeakMessage() override {
  58. // data_ will be null in the default instance, but we can safely call delete
  59. // here because the default instance will never be destroyed.
  60. delete data_;
  61. }
  62. static const ImplicitWeakMessage* default_instance();
  63. std::string GetTypeName() const override { return ""; }
  64. MessageLite* New(Arena* arena) const override {
  65. return Arena::CreateMessage<ImplicitWeakMessage>(arena);
  66. }
  67. void Clear() override { data_->clear(); }
  68. bool IsInitialized() const override { return true; }
  69. void CheckTypeAndMergeFrom(const MessageLite& other) override {
  70. const std::string* other_data =
  71. static_cast<const ImplicitWeakMessage&>(other).data_;
  72. if (other_data != nullptr) {
  73. data_->append(*other_data);
  74. }
  75. }
  76. const char* _InternalParse(const char* ptr, ParseContext* ctx) final;
  77. size_t ByteSizeLong() const override {
  78. return data_ == nullptr ? 0 : data_->size();
  79. }
  80. uint8_t* _InternalSerialize(uint8_t* target,
  81. io::EpsCopyOutputStream* stream) const final {
  82. if (data_ == nullptr) {
  83. return target;
  84. }
  85. return stream->WriteRaw(data_->data(), static_cast<int>(data_->size()),
  86. target);
  87. }
  88. int GetCachedSize() const override {
  89. return data_ == nullptr ? 0 : static_cast<int>(data_->size());
  90. }
  91. typedef void InternalArenaConstructable_;
  92. private:
  93. // This std::string is allocated on the heap, but we use a raw pointer so that
  94. // the default instance can be constant-initialized. In the const methods, we
  95. // have to handle the possibility of data_ being null.
  96. std::string* data_;
  97. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImplicitWeakMessage);
  98. };
  99. struct ImplicitWeakMessageDefaultType;
  100. extern ImplicitWeakMessageDefaultType implicit_weak_message_default_instance;
  101. // A type handler for use with implicit weak repeated message fields.
  102. template <typename ImplicitWeakType>
  103. class ImplicitWeakTypeHandler {
  104. public:
  105. typedef MessageLite Type;
  106. static constexpr bool Moveable = false;
  107. static inline MessageLite* NewFromPrototype(const MessageLite* prototype,
  108. Arena* arena = nullptr) {
  109. return prototype->New(arena);
  110. }
  111. static inline void Delete(MessageLite* value, Arena* arena) {
  112. if (arena == nullptr) {
  113. delete value;
  114. }
  115. }
  116. static inline Arena* GetArena(MessageLite* value) {
  117. return value->GetArena();
  118. }
  119. static inline void Clear(MessageLite* value) { value->Clear(); }
  120. static void Merge(const MessageLite& from, MessageLite* to) {
  121. to->CheckTypeAndMergeFrom(from);
  122. }
  123. };
  124. } // namespace internal
  125. template <typename T>
  126. struct WeakRepeatedPtrField {
  127. using TypeHandler = internal::ImplicitWeakTypeHandler<T>;
  128. constexpr WeakRepeatedPtrField() : weak() {}
  129. explicit WeakRepeatedPtrField(Arena* arena) : weak(arena) {}
  130. ~WeakRepeatedPtrField() { weak.template Destroy<TypeHandler>(); }
  131. typedef internal::RepeatedPtrIterator<MessageLite> iterator;
  132. typedef internal::RepeatedPtrIterator<const MessageLite> const_iterator;
  133. typedef internal::RepeatedPtrOverPtrsIterator<MessageLite*, void*>
  134. pointer_iterator;
  135. typedef internal::RepeatedPtrOverPtrsIterator<const MessageLite* const,
  136. const void* const>
  137. const_pointer_iterator;
  138. iterator begin() { return iterator(base().raw_data()); }
  139. const_iterator begin() const { return iterator(base().raw_data()); }
  140. const_iterator cbegin() const { return begin(); }
  141. iterator end() { return begin() + base().size(); }
  142. const_iterator end() const { return begin() + base().size(); }
  143. const_iterator cend() const { return end(); }
  144. pointer_iterator pointer_begin() {
  145. return pointer_iterator(base().raw_mutable_data());
  146. }
  147. const_pointer_iterator pointer_begin() const {
  148. return const_pointer_iterator(base().raw_mutable_data());
  149. }
  150. pointer_iterator pointer_end() {
  151. return pointer_iterator(base().raw_mutable_data() + base().size());
  152. }
  153. const_pointer_iterator pointer_end() const {
  154. return const_pointer_iterator(base().raw_mutable_data() + base().size());
  155. }
  156. MessageLite* AddWeak(const MessageLite* prototype) {
  157. return base().AddWeak(prototype);
  158. }
  159. T* Add() { return weak.Add(); }
  160. void Clear() { base().template Clear<TypeHandler>(); }
  161. void MergeFrom(const WeakRepeatedPtrField& other) {
  162. base().template MergeFrom<TypeHandler>(other.base());
  163. }
  164. void InternalSwap(WeakRepeatedPtrField* other) {
  165. base().InternalSwap(&other->base());
  166. }
  167. const internal::RepeatedPtrFieldBase& base() const { return weak; }
  168. internal::RepeatedPtrFieldBase& base() { return weak; }
  169. // Union disables running the destructor. Which would create a strong link.
  170. // Instead we explicitly destroy the underlying base through the virtual
  171. // destructor.
  172. union {
  173. RepeatedPtrField<T> weak;
  174. };
  175. };
  176. } // namespace protobuf
  177. } // namespace google
  178. #include <google/protobuf/port_undef.inc>
  179. #endif // GOOGLE_PROTOBUF_IMPLICIT_WEAK_MESSAGE_H__