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 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. {
  46. namespace protobuf
  47. {
  48. namespace internal
  49. {
  50. // An implementation of MessageLite that treats all data as unknown. This type
  51. // acts as a placeholder for an implicit weak field in the case where the true
  52. // message type does not get linked into the binary.
  53. class PROTOBUF_EXPORT ImplicitWeakMessage : public MessageLite
  54. {
  55. public:
  56. ImplicitWeakMessage() :
  57. data_(new std::string)
  58. {
  59. }
  60. explicit constexpr ImplicitWeakMessage(ConstantInitialized) :
  61. data_(nullptr)
  62. {
  63. }
  64. explicit ImplicitWeakMessage(Arena* arena) :
  65. MessageLite(arena),
  66. data_(new std::string)
  67. {
  68. }
  69. ~ImplicitWeakMessage() override
  70. {
  71. // data_ will be null in the default instance, but we can safely call delete
  72. // here because the default instance will never be destroyed.
  73. delete data_;
  74. }
  75. static const ImplicitWeakMessage* default_instance();
  76. std::string GetTypeName() const override
  77. {
  78. return "";
  79. }
  80. MessageLite* New(Arena* arena) const override
  81. {
  82. return Arena::CreateMessage<ImplicitWeakMessage>(arena);
  83. }
  84. void Clear() override
  85. {
  86. data_->clear();
  87. }
  88. bool IsInitialized() const override
  89. {
  90. return true;
  91. }
  92. void CheckTypeAndMergeFrom(const MessageLite& other) override
  93. {
  94. const std::string* other_data =
  95. static_cast<const ImplicitWeakMessage&>(other).data_;
  96. if (other_data != nullptr)
  97. {
  98. data_->append(*other_data);
  99. }
  100. }
  101. const char* _InternalParse(const char* ptr, ParseContext* ctx) final;
  102. size_t ByteSizeLong() const override
  103. {
  104. return data_ == nullptr ? 0 : data_->size();
  105. }
  106. uint8_t* _InternalSerialize(uint8_t* target, io::EpsCopyOutputStream* stream) const final
  107. {
  108. if (data_ == nullptr)
  109. {
  110. return target;
  111. }
  112. return stream->WriteRaw(data_->data(), static_cast<int>(data_->size()), target);
  113. }
  114. int GetCachedSize() const override
  115. {
  116. return data_ == nullptr ? 0 : static_cast<int>(data_->size());
  117. }
  118. typedef void InternalArenaConstructable_;
  119. private:
  120. // This std::string is allocated on the heap, but we use a raw pointer so that
  121. // the default instance can be constant-initialized. In the const methods, we
  122. // have to handle the possibility of data_ being null.
  123. std::string* data_;
  124. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImplicitWeakMessage);
  125. };
  126. struct ImplicitWeakMessageDefaultType;
  127. extern ImplicitWeakMessageDefaultType implicit_weak_message_default_instance;
  128. // A type handler for use with implicit weak repeated message fields.
  129. template<typename ImplicitWeakType>
  130. class ImplicitWeakTypeHandler
  131. {
  132. public:
  133. typedef MessageLite Type;
  134. static constexpr bool Moveable = false;
  135. static inline MessageLite* NewFromPrototype(const MessageLite* prototype, Arena* arena = nullptr)
  136. {
  137. return prototype->New(arena);
  138. }
  139. static inline void Delete(MessageLite* value, Arena* arena)
  140. {
  141. if (arena == nullptr)
  142. {
  143. delete value;
  144. }
  145. }
  146. static inline Arena* GetArena(MessageLite* value)
  147. {
  148. return value->GetArena();
  149. }
  150. static inline void Clear(MessageLite* value)
  151. {
  152. value->Clear();
  153. }
  154. static void Merge(const MessageLite& from, MessageLite* to)
  155. {
  156. to->CheckTypeAndMergeFrom(from);
  157. }
  158. };
  159. } // namespace internal
  160. template<typename T>
  161. struct WeakRepeatedPtrField
  162. {
  163. using TypeHandler = internal::ImplicitWeakTypeHandler<T>;
  164. constexpr WeakRepeatedPtrField() :
  165. weak()
  166. {
  167. }
  168. explicit WeakRepeatedPtrField(Arena* arena) :
  169. weak(arena)
  170. {
  171. }
  172. ~WeakRepeatedPtrField()
  173. {
  174. weak.template Destroy<TypeHandler>();
  175. }
  176. typedef internal::RepeatedPtrIterator<MessageLite> iterator;
  177. typedef internal::RepeatedPtrIterator<const MessageLite> const_iterator;
  178. typedef internal::RepeatedPtrOverPtrsIterator<MessageLite*, void*>
  179. pointer_iterator;
  180. typedef internal::RepeatedPtrOverPtrsIterator<const MessageLite* const, const void* const>
  181. const_pointer_iterator;
  182. iterator begin()
  183. {
  184. return iterator(base().raw_data());
  185. }
  186. const_iterator begin() const
  187. {
  188. return iterator(base().raw_data());
  189. }
  190. const_iterator cbegin() const
  191. {
  192. return begin();
  193. }
  194. iterator end()
  195. {
  196. return begin() + base().size();
  197. }
  198. const_iterator end() const
  199. {
  200. return begin() + base().size();
  201. }
  202. const_iterator cend() const
  203. {
  204. return end();
  205. }
  206. pointer_iterator pointer_begin()
  207. {
  208. return pointer_iterator(base().raw_mutable_data());
  209. }
  210. const_pointer_iterator pointer_begin() const
  211. {
  212. return const_pointer_iterator(base().raw_mutable_data());
  213. }
  214. pointer_iterator pointer_end()
  215. {
  216. return pointer_iterator(base().raw_mutable_data() + base().size());
  217. }
  218. const_pointer_iterator pointer_end() const
  219. {
  220. return const_pointer_iterator(base().raw_mutable_data() + base().size());
  221. }
  222. MessageLite* AddWeak(const MessageLite* prototype)
  223. {
  224. return base().AddWeak(prototype);
  225. }
  226. T* Add()
  227. {
  228. return weak.Add();
  229. }
  230. void Clear()
  231. {
  232. base().template Clear<TypeHandler>();
  233. }
  234. void MergeFrom(const WeakRepeatedPtrField& other)
  235. {
  236. base().template MergeFrom<TypeHandler>(other.base());
  237. }
  238. void InternalSwap(WeakRepeatedPtrField* other)
  239. {
  240. base().InternalSwap(&other->base());
  241. }
  242. const internal::RepeatedPtrFieldBase& base() const
  243. {
  244. return weak;
  245. }
  246. internal::RepeatedPtrFieldBase& base()
  247. {
  248. return weak;
  249. }
  250. // Union disables running the destructor. Which would create a strong link.
  251. // Instead we explicitly destroy the underlying base through the virtual
  252. // destructor.
  253. union
  254. {
  255. RepeatedPtrField<T> weak;
  256. };
  257. };
  258. } // namespace protobuf
  259. } // namespace google
  260. #include <google/protobuf/port_undef.inc>
  261. #endif // GOOGLE_PROTOBUF_IMPLICIT_WEAK_MESSAGE_H__