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