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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. namespace protobuf {
  61. class Arena;
  62. class Message;
  63. namespace io {
  64. class CodedInputStream;
  65. }
  66. namespace internal {
  67. template <typename To, typename From>
  68. inline To DownCast(From* f) {
  69. return PROTOBUF_NAMESPACE_ID::internal::down_cast<To>(f);
  70. }
  71. template <typename To, typename From>
  72. inline To DownCast(From& f) {
  73. return PROTOBUF_NAMESPACE_ID::internal::down_cast<To>(f);
  74. }
  75. // This fastpath inlines a single branch instead of having to make the
  76. // InitProtobufDefaults function call.
  77. // It also generates less inlined code than a function-scope static initializer.
  78. PROTOBUF_EXPORT extern std::atomic<bool> init_protobuf_defaults_state;
  79. PROTOBUF_EXPORT void InitProtobufDefaultsSlow();
  80. PROTOBUF_EXPORT inline void InitProtobufDefaults() {
  81. if (PROTOBUF_PREDICT_FALSE(
  82. !init_protobuf_defaults_state.load(std::memory_order_acquire))) {
  83. InitProtobufDefaultsSlow();
  84. }
  85. }
  86. // This used by proto1
  87. PROTOBUF_EXPORT inline const std::string& GetEmptyString() {
  88. InitProtobufDefaults();
  89. return GetEmptyStringAlreadyInited();
  90. }
  91. // True if IsInitialized() is true for all elements of t. Type is expected
  92. // to be a RepeatedPtrField<some message type>. It's useful to have this
  93. // helper here to keep the protobuf compiler from ever having to emit loops in
  94. // IsInitialized() methods. We want the C++ compiler to inline this or not
  95. // as it sees fit.
  96. template <typename Msg>
  97. bool AllAreInitialized(const RepeatedPtrField<Msg>& t) {
  98. for (int i = t.size(); --i >= 0;) {
  99. if (!t.Get(i).IsInitialized()) return false;
  100. }
  101. return true;
  102. }
  103. // "Weak" variant of AllAreInitialized, used to implement implicit weak fields.
  104. // This version operates on MessageLite to avoid introducing a dependency on the
  105. // concrete message type.
  106. template <class T>
  107. bool AllAreInitializedWeak(const RepeatedPtrField<T>& t) {
  108. for (int i = t.size(); --i >= 0;) {
  109. if (!reinterpret_cast<const RepeatedPtrFieldBase&>(t)
  110. .Get<ImplicitWeakTypeHandler<T> >(i)
  111. .IsInitialized()) {
  112. return false;
  113. }
  114. }
  115. return true;
  116. }
  117. inline bool IsPresent(const void* base, uint32_t hasbit) {
  118. const uint32_t* has_bits_array = static_cast<const uint32_t*>(base);
  119. return (has_bits_array[hasbit / 32] & (1u << (hasbit & 31))) != 0;
  120. }
  121. inline bool IsOneofPresent(const void* base, uint32_t offset, uint32_t tag) {
  122. const uint32_t* oneof = reinterpret_cast<const uint32_t*>(
  123. static_cast<const uint8_t*>(base) + offset);
  124. return *oneof == tag >> 3;
  125. }
  126. typedef void (*SpecialSerializer)(const uint8_t* base, uint32_t offset,
  127. uint32_t tag, uint32_t has_offset,
  128. io::CodedOutputStream* output);
  129. PROTOBUF_EXPORT void ExtensionSerializer(const MessageLite* extendee,
  130. const uint8_t* ptr, uint32_t offset,
  131. uint32_t tag, uint32_t has_offset,
  132. io::CodedOutputStream* output);
  133. PROTOBUF_EXPORT void UnknownFieldSerializerLite(const uint8_t* base,
  134. uint32_t offset, uint32_t tag,
  135. uint32_t has_offset,
  136. io::CodedOutputStream* output);
  137. PROTOBUF_EXPORT MessageLite* DuplicateIfNonNullInternal(MessageLite* message);
  138. PROTOBUF_EXPORT MessageLite* GetOwnedMessageInternal(Arena* message_arena,
  139. MessageLite* submessage,
  140. Arena* submessage_arena);
  141. PROTOBUF_EXPORT void GenericSwap(MessageLite* m1, MessageLite* m2);
  142. // We specialize GenericSwap for non-lite messages to benefit from reflection.
  143. PROTOBUF_EXPORT void GenericSwap(Message* m1, Message* m2);
  144. template <typename T>
  145. T* DuplicateIfNonNull(T* message) {
  146. // The casts must be reinterpret_cast<> because T might be a forward-declared
  147. // type that the compiler doesn't know is related to MessageLite.
  148. return reinterpret_cast<T*>(
  149. DuplicateIfNonNullInternal(reinterpret_cast<MessageLite*>(message)));
  150. }
  151. template <typename T>
  152. T* GetOwnedMessage(Arena* message_arena, T* submessage,
  153. Arena* submessage_arena) {
  154. // The casts must be reinterpret_cast<> because T might be a forward-declared
  155. // type that the compiler doesn't know is related to MessageLite.
  156. return reinterpret_cast<T*>(GetOwnedMessageInternal(
  157. message_arena, reinterpret_cast<MessageLite*>(submessage),
  158. submessage_arena));
  159. }
  160. // Hide atomic from the public header and allow easy change to regular int
  161. // on platforms where the atomic might have a perf impact.
  162. class PROTOBUF_EXPORT CachedSize {
  163. public:
  164. int Get() const { return size_.load(std::memory_order_relaxed); }
  165. void Set(int size) { size_.store(size, std::memory_order_relaxed); }
  166. private:
  167. std::atomic<int> size_{0};
  168. };
  169. PROTOBUF_EXPORT void DestroyMessage(const void* message);
  170. PROTOBUF_EXPORT void DestroyString(const void* s);
  171. // Destroy (not delete) the message
  172. inline void OnShutdownDestroyMessage(const void* ptr) {
  173. OnShutdownRun(DestroyMessage, ptr);
  174. }
  175. // Destroy the string (call std::string destructor)
  176. inline void OnShutdownDestroyString(const std::string* ptr) {
  177. OnShutdownRun(DestroyString, ptr);
  178. }
  179. } // namespace internal
  180. } // namespace protobuf
  181. } // namespace google
  182. #include <google/protobuf/port_undef.inc>
  183. #endif // GOOGLE_PROTOBUF_GENERATED_MESSAGE_UTIL_H__