generated_message_util.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 hasbit) {
  117. const uint32* has_bits_array = static_cast<const uint32*>(base);
  118. return (has_bits_array[hasbit / 32] & (1u << (hasbit & 31))) != 0;
  119. }
  120. inline bool IsOneofPresent(const void* base, uint32 offset, uint32 tag) {
  121. const uint32* oneof =
  122. reinterpret_cast<const uint32*>(static_cast<const uint8*>(base) + offset);
  123. return *oneof == tag >> 3;
  124. }
  125. typedef void (*SpecialSerializer)(const uint8* base, uint32 offset, uint32 tag,
  126. uint32 has_offset,
  127. io::CodedOutputStream* output);
  128. PROTOBUF_EXPORT void ExtensionSerializer(const uint8* base, uint32 offset,
  129. uint32 tag, uint32 has_offset,
  130. io::CodedOutputStream* output);
  131. PROTOBUF_EXPORT void UnknownFieldSerializerLite(const uint8* base,
  132. uint32 offset, uint32 tag,
  133. uint32 has_offset,
  134. io::CodedOutputStream* output);
  135. PROTOBUF_EXPORT MessageLite* DuplicateIfNonNullInternal(MessageLite* message);
  136. PROTOBUF_EXPORT MessageLite* GetOwnedMessageInternal(Arena* message_arena,
  137. MessageLite* submessage,
  138. Arena* submessage_arena);
  139. PROTOBUF_EXPORT void GenericSwap(MessageLite* m1, MessageLite* m2);
  140. // We specialize GenericSwap for non-lite messages to benefit from reflection.
  141. PROTOBUF_EXPORT void GenericSwap(Message* m1, Message* m2);
  142. template <typename T>
  143. T* DuplicateIfNonNull(T* message) {
  144. // The casts must be reinterpret_cast<> because T might be a forward-declared
  145. // type that the compiler doesn't know is related to MessageLite.
  146. return reinterpret_cast<T*>(
  147. DuplicateIfNonNullInternal(reinterpret_cast<MessageLite*>(message)));
  148. }
  149. template <typename T>
  150. T* GetOwnedMessage(Arena* message_arena, T* submessage,
  151. Arena* submessage_arena) {
  152. // The casts must be reinterpret_cast<> because T might be a forward-declared
  153. // type that the compiler doesn't know is related to MessageLite.
  154. return reinterpret_cast<T*>(GetOwnedMessageInternal(
  155. message_arena, reinterpret_cast<MessageLite*>(submessage),
  156. submessage_arena));
  157. }
  158. // Hide atomic from the public header and allow easy change to regular int
  159. // on platforms where the atomic might have a perf impact.
  160. class PROTOBUF_EXPORT CachedSize {
  161. public:
  162. int Get() const { return size_.load(std::memory_order_relaxed); }
  163. void Set(int size) { size_.store(size, std::memory_order_relaxed); }
  164. private:
  165. std::atomic<int> size_{0};
  166. };
  167. PROTOBUF_EXPORT void DestroyMessage(const void* message);
  168. PROTOBUF_EXPORT void DestroyString(const void* s);
  169. // Destroy (not delete) the message
  170. inline void OnShutdownDestroyMessage(const void* ptr) {
  171. OnShutdownRun(DestroyMessage, ptr);
  172. }
  173. // Destroy the string (call std::string destructor)
  174. inline void OnShutdownDestroyString(const std::string* ptr) {
  175. OnShutdownRun(DestroyString, ptr);
  176. }
  177. } // namespace internal
  178. } // namespace protobuf
  179. } // namespace google
  180. #include <google/protobuf/port_undef.inc>
  181. #endif // GOOGLE_PROTOBUF_GENERATED_MESSAGE_UTIL_H__