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.

generated_message_util.h 9.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. //
  34. // This file contains miscellaneous helper code used by generated code --
  35. // including lite types -- but which should not be used directly by users.
  36. #ifndef GOOGLE_PROTOBUF_GENERATED_MESSAGE_UTIL_H__
  37. #define GOOGLE_PROTOBUF_GENERATED_MESSAGE_UTIL_H__
  38. #include <assert.h>
  39. #include <atomic>
  40. #include <climits>
  41. #include <string>
  42. #include <vector>
  43. #include <google/protobuf/stubs/common.h>
  44. #include <google/protobuf/stubs/once.h> // Add direct dep on port for pb.cc
  45. #include <google/protobuf/port.h>
  46. #include <google/protobuf/stubs/strutil.h>
  47. #include <google/protobuf/any.h>
  48. #include <google/protobuf/has_bits.h>
  49. #include <google/protobuf/implicit_weak_message.h>
  50. #include <google/protobuf/message_lite.h>
  51. #include <google/protobuf/repeated_field.h>
  52. #include <google/protobuf/wire_format_lite.h>
  53. #include <google/protobuf/stubs/casts.h>
  54. // Must be included last.
  55. #include <google/protobuf/port_def.inc>
  56. #ifdef SWIG
  57. #error "You cannot SWIG proto headers"
  58. #endif
  59. namespace google
  60. {
  61. namespace protobuf
  62. {
  63. class Arena;
  64. class Message;
  65. namespace io
  66. {
  67. class CodedInputStream;
  68. }
  69. namespace internal
  70. {
  71. template<typename To, typename From>
  72. inline To DownCast(From* f)
  73. {
  74. return PROTOBUF_NAMESPACE_ID::internal::down_cast<To>(f);
  75. }
  76. template<typename To, typename From>
  77. inline To DownCast(From& f)
  78. {
  79. return PROTOBUF_NAMESPACE_ID::internal::down_cast<To>(f);
  80. }
  81. // This fastpath inlines a single branch instead of having to make the
  82. // InitProtobufDefaults function call.
  83. // It also generates less inlined code than a function-scope static initializer.
  84. PROTOBUF_EXPORT extern std::atomic<bool> init_protobuf_defaults_state;
  85. PROTOBUF_EXPORT void InitProtobufDefaultsSlow();
  86. PROTOBUF_EXPORT inline void InitProtobufDefaults()
  87. {
  88. if (PROTOBUF_PREDICT_FALSE(
  89. !init_protobuf_defaults_state.load(std::memory_order_acquire)
  90. ))
  91. {
  92. InitProtobufDefaultsSlow();
  93. }
  94. }
  95. // This used by proto1
  96. PROTOBUF_EXPORT inline const std::string& GetEmptyString()
  97. {
  98. InitProtobufDefaults();
  99. return GetEmptyStringAlreadyInited();
  100. }
  101. // True if IsInitialized() is true for all elements of t. Type is expected
  102. // to be a RepeatedPtrField<some message type>. It's useful to have this
  103. // helper here to keep the protobuf compiler from ever having to emit loops in
  104. // IsInitialized() methods. We want the C++ compiler to inline this or not
  105. // as it sees fit.
  106. template<typename Msg>
  107. bool AllAreInitialized(const RepeatedPtrField<Msg>& t)
  108. {
  109. for (int i = t.size(); --i >= 0;)
  110. {
  111. if (!t.Get(i).IsInitialized())
  112. return false;
  113. }
  114. return true;
  115. }
  116. // "Weak" variant of AllAreInitialized, used to implement implicit weak fields.
  117. // This version operates on MessageLite to avoid introducing a dependency on the
  118. // concrete message type.
  119. template<class T>
  120. bool AllAreInitializedWeak(const RepeatedPtrField<T>& t)
  121. {
  122. for (int i = t.size(); --i >= 0;)
  123. {
  124. if (!reinterpret_cast<const RepeatedPtrFieldBase&>(t)
  125. .Get<ImplicitWeakTypeHandler<T>>(i)
  126. .IsInitialized())
  127. {
  128. return false;
  129. }
  130. }
  131. return true;
  132. }
  133. inline bool IsPresent(const void* base, uint32_t hasbit)
  134. {
  135. const uint32_t* has_bits_array = static_cast<const uint32_t*>(base);
  136. return (has_bits_array[hasbit / 32] & (1u << (hasbit & 31))) != 0;
  137. }
  138. inline bool IsOneofPresent(const void* base, uint32_t offset, uint32_t tag)
  139. {
  140. const uint32_t* oneof = reinterpret_cast<const uint32_t*>(
  141. static_cast<const uint8_t*>(base) + offset
  142. );
  143. return *oneof == tag >> 3;
  144. }
  145. typedef void (*SpecialSerializer)(const uint8_t* base, uint32_t offset, uint32_t tag, uint32_t has_offset, io::CodedOutputStream* output);
  146. PROTOBUF_EXPORT void ExtensionSerializer(const MessageLite* extendee, const uint8_t* ptr, uint32_t offset, uint32_t tag, uint32_t has_offset, io::CodedOutputStream* output);
  147. PROTOBUF_EXPORT void UnknownFieldSerializerLite(const uint8_t* base, uint32_t offset, uint32_t tag, uint32_t has_offset, io::CodedOutputStream* output);
  148. PROTOBUF_EXPORT MessageLite* DuplicateIfNonNullInternal(MessageLite* message);
  149. PROTOBUF_EXPORT MessageLite* GetOwnedMessageInternal(Arena* message_arena, MessageLite* submessage, Arena* submessage_arena);
  150. PROTOBUF_EXPORT void GenericSwap(MessageLite* m1, MessageLite* m2);
  151. // We specialize GenericSwap for non-lite messages to benefit from reflection.
  152. PROTOBUF_EXPORT void GenericSwap(Message* m1, Message* m2);
  153. template<typename T>
  154. T* DuplicateIfNonNull(T* message)
  155. {
  156. // The casts must be reinterpret_cast<> because T might be a forward-declared
  157. // type that the compiler doesn't know is related to MessageLite.
  158. return reinterpret_cast<T*>(
  159. DuplicateIfNonNullInternal(reinterpret_cast<MessageLite*>(message))
  160. );
  161. }
  162. template<typename T>
  163. T* GetOwnedMessage(Arena* message_arena, T* submessage, Arena* submessage_arena)
  164. {
  165. // The casts must be reinterpret_cast<> because T might be a forward-declared
  166. // type that the compiler doesn't know is related to MessageLite.
  167. return reinterpret_cast<T*>(GetOwnedMessageInternal(
  168. message_arena, reinterpret_cast<MessageLite*>(submessage), submessage_arena
  169. ));
  170. }
  171. // Hide atomic from the public header and allow easy change to regular int
  172. // on platforms where the atomic might have a perf impact.
  173. class PROTOBUF_EXPORT CachedSize
  174. {
  175. public:
  176. int Get() const
  177. {
  178. return size_.load(std::memory_order_relaxed);
  179. }
  180. void Set(int size)
  181. {
  182. size_.store(size, std::memory_order_relaxed);
  183. }
  184. private:
  185. std::atomic<int> size_{0};
  186. };
  187. PROTOBUF_EXPORT void DestroyMessage(const void* message);
  188. PROTOBUF_EXPORT void DestroyString(const void* s);
  189. // Destroy (not delete) the message
  190. inline void OnShutdownDestroyMessage(const void* ptr)
  191. {
  192. OnShutdownRun(DestroyMessage, ptr);
  193. }
  194. // Destroy the string (call std::string destructor)
  195. inline void OnShutdownDestroyString(const std::string* ptr)
  196. {
  197. OnShutdownRun(DestroyString, ptr);
  198. }
  199. } // namespace internal
  200. } // namespace protobuf
  201. } // namespace google
  202. #include <google/protobuf/port_undef.inc>
  203. #endif // GOOGLE_PROTOBUF_GENERATED_MESSAGE_UTIL_H__