• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
47 #include <google/protobuf/stubs/common.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/stubs/once.h>  // Add direct dep on port for pb.cc
52 #include <google/protobuf/port.h>
53 #include <google/protobuf/wire_format_lite.h>
54 #include <google/protobuf/stubs/strutil.h>
55 #include <google/protobuf/stubs/casts.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>
DownCast(From * f)75 inline To DownCast(From* f) {
76   return PROTOBUF_NAMESPACE_ID::internal::down_cast<To>(f);
77 }
78 template <typename To, typename From>
DownCast(From & f)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
GetEmptyString()87 PROTOBUF_EXPORT inline const std::string& GetEmptyString() {
88   InitProtobufDefaults();
89   return GetEmptyStringAlreadyInited();
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>
AllAreInitialized(const Type & t)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>
AllAreInitializedWeak(const RepeatedPtrField<T> & t)110 bool AllAreInitializedWeak(const RepeatedPtrField<T>& t) {
111   for (int i = t.size(); --i >= 0;) {
112     if (!reinterpret_cast<const RepeatedPtrFieldBase&>(t)
113              .Get<ImplicitWeakTypeHandler<T> >(i)
114              .IsInitialized()) {
115       return false;
116     }
117   }
118   return true;
119 }
120 
IsPresent(const void * base,uint32 hasbit)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 
IsOneofPresent(const void * base,uint32 offset,uint32 tag)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 
132 typedef void (*SpecialSerializer)(const uint8* base, uint32 offset, uint32 tag,
133                                   uint32 has_offset,
134                                   io::CodedOutputStream* output);
135 
136 PROTOBUF_EXPORT void ExtensionSerializer(const uint8* base, uint32 offset,
137                                          uint32 tag, uint32 has_offset,
138                                          io::CodedOutputStream* output);
139 PROTOBUF_EXPORT void UnknownFieldSerializerLite(const uint8* base,
140                                                 uint32 offset, uint32 tag,
141                                                 uint32 has_offset,
142                                                 io::CodedOutputStream* output);
143 
144 PROTOBUF_EXPORT MessageLite* DuplicateIfNonNullInternal(MessageLite* message);
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>
DuplicateIfNonNull(T * message)151 T* DuplicateIfNonNull(T* message) {
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>
GetOwnedMessage(Arena * message_arena,T * submessage,Arena * submessage_arena)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:
Get()172   int Get() const { return size_.load(std::memory_order_relaxed); }
Set(int size)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 {
207   SCCInfoBase base;
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 
InitSCC(SCCInfoBase * scc)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
OnShutdownDestroyMessage(const void * ptr)226 inline void OnShutdownDestroyMessage(const void* ptr) {
227   OnShutdownRun(DestroyMessage, ptr);
228 }
229 // Destroy the string (call std::string destructor)
OnShutdownDestroyString(const std::string * ptr)230 inline void OnShutdownDestroyString(const std::string* ptr) {
231   OnShutdownRun(DestroyString, 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__
241