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 #include <atomic>
43 #include <climits>
44 #include <string>
45 #include <vector>
46 
51 #include <google/protobuf/stubs/once.h> // Add direct dep on port for pb.cc
52 #include <google/protobuf/port.h>
56 
57 #include <google/protobuf/port_def.inc>
58 
59 #ifdef SWIG
60 #error "You cannot SWIG proto headers"
61 #endif
62 
63 namespace google {
64 namespace protobuf {
65 
66 class Arena;
67 
68 namespace io {
69 class CodedInputStream;
70 }
71 
72 namespace internal {
73 
74 template <typename To, typename From>
75 inline To DownCast(From* f) {
76  return PROTOBUF_NAMESPACE_ID::internal::down_cast<To>(f);
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 
83 
84 PROTOBUF_EXPORT void InitProtobufDefaults();
85 
86 // This used by proto1
87 PROTOBUF_EXPORT inline const std::string& GetEmptyString() {
90 }
91 
92 
93 // True if IsInitialized() is true for all elements of t. Type is expected
94 // to be a RepeatedPtrField<some message type>. It's useful to have this
95 // helper here to keep the protobuf compiler from ever having to emit loops in
96 // IsInitialized() methods. We want the C++ compiler to inline this or not
97 // as it sees fit.
98 template <class Type>
99 bool AllAreInitialized(const Type& t) {
100  for (int i = t.size(); --i >= 0;) {
101  if (!t.Get(i).IsInitialized()) return false;
102  }
103  return true;
104 }
105 
106 // "Weak" variant of AllAreInitialized, used to implement implicit weak fields.
107 // This version operates on MessageLite to avoid introducing a dependency on the
108 // concrete message type.
109 template <class T>
111  for (int i = t.size(); --i >= 0;) {
112  if (!reinterpret_cast<const RepeatedPtrFieldBase&>(t)
114  .IsInitialized()) {
115  return false;
116  }
117  }
118  return true;
119 }
120 
121 inline bool IsPresent(const void* base, uint32 hasbit) {
122  const uint32* has_bits_array = static_cast<const uint32*>(base);
123  return (has_bits_array[hasbit / 32] & (1u << (hasbit & 31))) != 0;
124 }
125 
126 inline bool IsOneofPresent(const void* base, uint32 offset, uint32 tag) {
127  const uint32* oneof =
128  reinterpret_cast<const uint32*>(static_cast<const uint8*>(base) + offset);
129  return *oneof == tag >> 3;
130 }
131 
133  uint32 has_offset,
135 
136 PROTOBUF_EXPORT void ExtensionSerializer(const uint8* base, uint32 offset,
137  uint32 tag, uint32 has_offset,
139 PROTOBUF_EXPORT void UnknownFieldSerializerLite(const uint8* base,
140  uint32 offset, uint32 tag,
141  uint32 has_offset,
143 
145 PROTOBUF_EXPORT MessageLite* GetOwnedMessageInternal(Arena* message_arena,
146  MessageLite* submessage,
147  Arena* submessage_arena);
148 PROTOBUF_EXPORT void GenericSwap(MessageLite* m1, MessageLite* m2);
149 
150 template <typename T>
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*>(
155  DuplicateIfNonNullInternal(reinterpret_cast<MessageLite*>(message)));
156 }
157 
158 template <typename T>
159 T* GetOwnedMessage(Arena* message_arena, T* submessage,
160  Arena* submessage_arena) {
161  // The casts must be reinterpret_cast<> because T might be a forward-declared
162  // type that the compiler doesn't know is related to MessageLite.
163  return reinterpret_cast<T*>(GetOwnedMessageInternal(
164  message_arena, reinterpret_cast<MessageLite*>(submessage),
165  submessage_arena));
166 }
167 
168 // Hide atomic from the public header and allow easy change to regular int
169 // on platforms where the atomic might have a perf impact.
170 class PROTOBUF_EXPORT CachedSize {
171  public:
172  int Get() const { return size_.load(std::memory_order_relaxed); }
173  void Set(int size) { size_.store(size, std::memory_order_relaxed); }
174 
175  private:
176  std::atomic<int> size_{0};
177 };
178 
179 // SCCInfo represents information of a strongly connected component of
180 // mutual dependent messages.
181 struct PROTOBUF_EXPORT SCCInfoBase {
182  // We use 0 for the Initialized state, because test eax,eax, jnz is smaller
183  // and is subject to macro fusion.
184  enum {
185  kInitialized = 0, // final state
186  kRunning = 1,
187  kUninitialized = -1, // initial state
188  };
189 #if defined(_MSC_VER) && !defined(__clang__)
190  // MSVC doesnt make std::atomic constant initialized. This union trick
191  // makes it so.
192  union {
193  int visit_status_to_make_linker_init;
194  std::atomic<int> visit_status;
195  };
196 #else
197  std::atomic<int> visit_status;
198 #endif
199  int num_deps;
200  void (*init_func)();
201  // This is followed by an array of num_deps
202  // const SCCInfoBase* deps[];
203 };
204 
205 template <int N>
206 struct SCCInfo {
208  // Semantically this is const SCCInfo<T>* which is is a templated type.
209  // The obvious inheriting from SCCInfoBase mucks with struct initialization.
210  // Attempts showed the compiler was generating dynamic initialization code.
211  // Zero length arrays produce warnings with MSVC.
212  SCCInfoBase* deps[N ? N : 1];
213 };
214 
215 PROTOBUF_EXPORT void InitSCCImpl(SCCInfoBase* scc);
216 
217 inline void InitSCC(SCCInfoBase* scc) {
218  auto status = scc->visit_status.load(std::memory_order_acquire);
219  if (PROTOBUF_PREDICT_FALSE(status != SCCInfoBase::kInitialized))
220  InitSCCImpl(scc);
221 }
222 
223 PROTOBUF_EXPORT void DestroyMessage(const void* message);
224 PROTOBUF_EXPORT void DestroyString(const void* s);
225 // Destroy (not delete) the message
226 inline void OnShutdownDestroyMessage(const void* ptr) {
228 }
229 // Destroy the string (call std::string destructor)
230 inline void OnShutdownDestroyString(const std::string* ptr) {
232 }
233 
234 } // namespace internal
235 } // namespace protobuf
236 } // namespace google
237 
238 #include <google/protobuf/port_undef.inc>
239 
240 #endif // GOOGLE_PROTOBUF_GENERATED_MESSAGE_UTIL_H__
google::protobuf::RepeatedPtrField< T >
wire_format_lite.h
google::protobuf.internal::InitSCCImpl
void InitSCCImpl(SCCInfoBase *scc)
Definition: generated_message_util.cc:797
google::protobuf.internal::SpecialSerializer
void(* SpecialSerializer)(const uint8 *base, uint32 offset, uint32 tag, uint32 has_offset, io::CodedOutputStream *output)
Definition: generated_message_util.h:132
google::protobuf.internal::Get
const T & Get(const void *ptr)
Definition: generated_message_util.cc:98
base
Definition: logging.cc:2162
Type::IsInitialized
bool IsInitialized() const final
Definition: type.pb.cc:920
google::protobuf::uint8
uint8_t uint8
Definition: protobuf/src/google/protobuf/stubs/port.h:153
google::protobuf.internal::SCCInfoBase::num_deps
int num_deps
Definition: generated_message_util.h:199
google::protobuf.internal::OnShutdownDestroyMessage
void OnShutdownDestroyMessage(const void *ptr)
Definition: generated_message_util.h:226
google::protobuf::uint32
uint32_t uint32
Definition: protobuf/src/google/protobuf/stubs/port.h:155
google::protobuf.internal::SCCInfoBase
Definition: generated_message_util.h:181
google::protobuf.internal::ExtensionSerializer
void ExtensionSerializer(const uint8 *ptr, uint32 offset, uint32 tag, uint32 has_offset, io::CodedOutputStream *output)
Definition: generated_message_util.cc:726
google::protobuf.internal::IsPresent
bool IsPresent(const void *base, uint32 hasbit)
Definition: generated_message_util.h:121
google::protobuf.internal::InitSCC
void InitSCC(SCCInfoBase *scc)
Definition: generated_message_util.h:217
google::protobuf.internal::AllAreInitialized
bool AllAreInitialized(const Type &t)
Definition: generated_message_util.h:99
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
google::protobuf.internal::GenericSwap
void GenericSwap(MessageLite *m1, MessageLite *m2)
Definition: generated_message_util.cc:750
google::protobuf::MessageLite
Definition: message_lite.h:183
google::protobuf.internal::AllAreInitializedWeak
bool AllAreInitializedWeak(const RepeatedPtrField< T > &t)
Definition: generated_message_util.h:110
port.h
T
#define T(upbtypeconst, upbtype, ctype, default_value)
google::protobuf.internal::DuplicateIfNonNull
T * DuplicateIfNonNull(T *message)
Definition: generated_message_util.h:151
google::protobuf.internal::InitProtobufDefaults
void InitProtobufDefaults()
Definition: generated_message_util.cc:81
google::protobuf.internal::IsOneofPresent
bool IsOneofPresent(const void *base, uint32 offset, uint32 tag)
Definition: generated_message_util.h:126
google::protobuf.internal::DestroyString
void DestroyString(const void *s)
Definition: generated_message_util.cc:68
google::protobuf.internal::GetOwnedMessage
T * GetOwnedMessage(Arena *message_arena, T *submessage, Arena *submessage_arena)
Definition: generated_message_util.h:159
google::protobuf.internal::OnShutdownRun
void OnShutdownRun(void(*f)(const void *), const void *arg)
Definition: common.cc:348
strutil.h
Type
Definition: type.pb.h:182
offset
GLintptr offset
Definition: glcorearb.h:2944
google::protobuf.internal::DownCast
To DownCast(From *f)
Definition: generated_message_util.h:75
google::protobuf.internal::SCCInfo::base
SCCInfoBase base
Definition: generated_message_util.h:207
google::protobuf.internal::SCCInfoBase::kInitialized
@ kInitialized
Definition: generated_message_util.h:185
google::protobuf.internal::GetOwnedMessageInternal
MessageLite * GetOwnedMessageInternal(Arena *message_arena, MessageLite *submessage, Arena *submessage_arena)
Definition: generated_message_util.cc:761
google::protobuf.internal::OnShutdownDestroyString
void OnShutdownDestroyString(const std::string *ptr)
Definition: generated_message_util.h:230
casts.h
google::protobuf.internal::DuplicateIfNonNullInternal
MessageLite * DuplicateIfNonNullInternal(MessageLite *message)
Definition: generated_message_util.cc:740
google::protobuf.internal::SCCInfo::deps
SCCInfoBase * deps[N ? N :1]
Definition: generated_message_util.h:212
void
typedef void(APIENTRY *GLDEBUGPROCARB)(GLenum source
google::protobuf::io::CodedOutputStream
Definition: coded_stream.h:693
i
int i
Definition: gmock-matchers_test.cc:764
google::protobuf::RepeatedPtrField::size
int size() const
Definition: repeated_field.h:2009
google::protobuf.internal::SCCInfoBase::visit_status
std::atomic< int > visit_status
Definition: generated_message_util.h:197
google::protobuf.internal::GetEmptyString
const PROTOBUF_EXPORT std::string & GetEmptyString()
Definition: generated_message_util.h:87
has_bits.h
google::protobuf.internal::DestroyMessage
void DestroyMessage(const void *message)
Definition: generated_message_util.cc:65
google::protobuf.internal::ImplicitWeakTypeHandler
Definition: implicit_weak_message.h:103
common.h
google::protobuf.internal::CachedSize
Definition: generated_message_util.h:170
size
GLsizeiptr size
Definition: glcorearb.h:2943
message_lite.h
once.h
implicit_weak_message.h
google::protobuf.internal::GetEmptyStringAlreadyInited
const PROTOBUF_EXPORT std::string & GetEmptyStringAlreadyInited()
Definition: message_lite.h:152
google::protobuf.internal::CachedSize::Set
void Set(int size)
Definition: generated_message_util.h:173
google::protobuf.internal::RepeatedPtrFieldBase
Definition: repeated_field.h:459
google::protobuf.internal.python_message.IsInitialized
IsInitialized
Definition: python_message.py:1246
internal
Definition: any.pb.h:40
f
GLfloat f
Definition: glcorearb.h:3964
assert.h
output
const upb_json_parsermethod const upb_symtab upb_sink * output
Definition: ruby/ext/google/protobuf_c/upb.h:10503
google::protobuf.internal::SCCInfo
Definition: generated_message_util.h:206
google::protobuf.internal::UnknownFieldSerializerLite
void UnknownFieldSerializerLite(const uint8 *ptr, uint32 offset, uint32 tag, uint32 has_offset, io::CodedOutputStream *output)
Definition: generated_message_util.cc:732
google
Definition: data_proto2_to_proto3_util.h:11
message
GLenum GLuint GLenum GLsizei const GLchar * message
Definition: glcorearb.h:2695
google::protobuf.internal::CachedSize::Get
int Get() const
Definition: generated_message_util.h:172


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:51