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


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:26