1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
|
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Author: kenton@google.com (Kenton Varda)
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
#include <google/protobuf/message.h>
#include <iostream>
#include <stack>
#include <unordered_map>
#include <google/protobuf/stubs/casts.h>
#include <google/protobuf/stubs/logging.h>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/parse_context.h>
#include <google/protobuf/reflection_internal.h>
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/generated_message_reflection.h>
#include <google/protobuf/generated_message_util.h>
#include <google/protobuf/map_field.h>
#include <google/protobuf/map_field_inl.h>
#include <google/protobuf/reflection_ops.h>
#include <google/protobuf/unknown_field_set.h>
#include <google/protobuf/wire_format.h>
#include <google/protobuf/wire_format_lite.h>
#include <google/protobuf/stubs/strutil.h>
#include <google/protobuf/stubs/map_util.h>
#include <google/protobuf/stubs/stl_util.h>
#include <google/protobuf/stubs/hash.h>
#include <google/protobuf/port_def.inc>
namespace google {
namespace protobuf {
namespace internal {
// TODO(gerbens) make this factorized better. This should not have to hop
// to reflection. Currently uses GeneratedMessageReflection and thus is
// defined in generated_message_reflection.cc
void RegisterFileLevelMetadata(const DescriptorTable* descriptor_table);
} // namespace internal
using internal::ReflectionOps;
using internal::WireFormat;
using internal::WireFormatLite;
void Message::MergeFrom(const Message& from) {
auto* class_to = GetClassData();
auto* class_from = from.GetClassData();
auto* merge_to_from = class_to ? class_to->merge_to_from : nullptr;
if (class_to == nullptr || class_to != class_from) {
merge_to_from = [](Message* to, const Message& from) {
ReflectionOps::Merge(from, to);
};
}
merge_to_from(this, from);
}
void Message::CheckTypeAndMergeFrom(const MessageLite& other) {
MergeFrom(*down_cast<const Message*>(&other));
}
void Message::CopyFrom(const Message& from) {
if (&from == this) return;
auto* class_to = GetClassData();
auto* class_from = from.GetClassData();
auto* copy_to_from = class_to ? class_to->copy_to_from : nullptr;
if (class_to == nullptr || class_to != class_from) {
const Descriptor* descriptor = GetDescriptor();
GOOGLE_CHECK_EQ(from.GetDescriptor(), descriptor)
<< ": Tried to copy from a message with a different type. "
"to: "
<< descriptor->full_name()
<< ", "
"from: "
<< from.GetDescriptor()->full_name();
copy_to_from = [](Message* to, const Message& from) {
ReflectionOps::Copy(from, to);
};
}
copy_to_from(this, from);
}
void Message::CopyWithSizeCheck(Message* to, const Message& from) {
#ifndef NDEBUG
size_t from_size = from.ByteSizeLong();
#endif
to->Clear();
#ifndef NDEBUG
GOOGLE_CHECK_EQ(from_size, from.ByteSizeLong())
<< "Source of CopyFrom changed when clearing target. Either "
"source is a nested message in target (not allowed), or "
"another thread is modifying the source.";
#endif
to->GetClassData()->merge_to_from(to, from);
}
TProtoStringType Message::GetTypeName() const {
return GetDescriptor()->full_name();
}
void Message::Clear() { ReflectionOps::Clear(this); }
bool Message::IsInitialized() const {
return ReflectionOps::IsInitialized(*this);
}
void Message::FindInitializationErrors(std::vector<TProtoStringType>* errors) const {
return ReflectionOps::FindInitializationErrors(*this, "", errors);
}
TProtoStringType Message::InitializationErrorString() const {
std::vector<TProtoStringType> errors;
FindInitializationErrors(&errors);
return Join(errors, ", ");
}
void Message::CheckInitialized() const {
GOOGLE_CHECK(IsInitialized()) << "Message of type \"" << GetDescriptor()->full_name()
<< "\" is missing required fields: "
<< InitializationErrorString();
}
void Message::DiscardUnknownFields() {
return ReflectionOps::DiscardUnknownFields(this);
}
const char* Message::_InternalParse(const char* ptr,
internal::ParseContext* ctx) {
return WireFormat::_InternalParse(this, ptr, ctx);
}
uint8* Message::_InternalSerialize(uint8* target,
io::EpsCopyOutputStream* stream) const {
return WireFormat::_InternalSerialize(*this, target, stream);
}
// Yandex-specific
void Message::PrintJSON(IOutputStream& out) const {
out << "(Something went wrong: no PrintJSON() override provided - are you using a non-styleguided .pb.h?)";
}
bool Message::ParseFromArcadiaStream(IInputStream* input) {
bool res = false;
io::TInputStreamProxy proxy(input);
{
io::CopyingInputStreamAdaptor stream(&proxy);
res = ParseFromZeroCopyStream(&stream);
}
return res && !proxy.HasError();
}
bool Message::ParsePartialFromArcadiaStream(IInputStream* input) {
bool res = false;
io::TInputStreamProxy proxy(input);
{
io::CopyingInputStreamAdaptor stream(&proxy);
res = ParsePartialFromZeroCopyStream(&stream);
}
return res && !proxy.HasError();
}
bool Message::SerializeToArcadiaStream(IOutputStream* output) const {
bool res = false;
io::TOutputStreamProxy proxy(output);
{
io::CopyingOutputStreamAdaptor stream(&proxy);
res = SerializeToZeroCopyStream(&stream);
}
return res && !proxy.HasError();
}
bool Message::SerializePartialToArcadiaStream(IOutputStream* output) const {
bool res = false;
io::TOutputStreamProxy proxy(output);
{
io::CopyingOutputStreamAdaptor stream(&proxy);
res = SerializePartialToZeroCopyStream(&stream);
}
return res && !proxy.HasError();
}
// End of Yandex-specific
size_t Message::ByteSizeLong() const {
size_t size = WireFormat::ByteSize(*this);
SetCachedSize(internal::ToCachedSize(size));
return size;
}
void Message::SetCachedSize(int /* size */) const {
GOOGLE_LOG(FATAL) << "Message class \"" << GetDescriptor()->full_name()
<< "\" implements neither SetCachedSize() nor ByteSize(). "
"Must implement one or the other.";
}
size_t Message::SpaceUsedLong() const {
return GetReflection()->SpaceUsedLong(*this);
}
uint64 Message::GetInvariantPerBuild(uint64 salt) {
return salt;
}
// =============================================================================
// MessageFactory
MessageFactory::~MessageFactory() {}
namespace {
#define HASH_MAP std::unordered_map
#define STR_HASH_FXN hash<::google::protobuf::StringPiece>
class GeneratedMessageFactory final : public MessageFactory {
public:
static GeneratedMessageFactory* singleton();
void RegisterFile(const google::protobuf::internal::DescriptorTable* table);
void RegisterType(const Descriptor* descriptor, const Message* prototype);
// implements MessageFactory ---------------------------------------
const Message* GetPrototype(const Descriptor* type) override;
private:
// Only written at static init time, so does not require locking.
HASH_MAP<StringPiece, const google::protobuf::internal::DescriptorTable*,
STR_HASH_FXN>
file_map_;
internal::WrappedMutex mutex_;
// Initialized lazily, so requires locking.
std::unordered_map<const Descriptor*, const Message*> type_map_;
};
GeneratedMessageFactory* GeneratedMessageFactory::singleton() {
static auto instance =
internal::OnShutdownDelete(new GeneratedMessageFactory);
return instance;
}
void GeneratedMessageFactory::RegisterFile(
const google::protobuf::internal::DescriptorTable* table) {
if (!InsertIfNotPresent(&file_map_, table->filename, table)) {
GOOGLE_LOG(FATAL) << "File is already registered: " << table->filename;
}
}
void GeneratedMessageFactory::RegisterType(const Descriptor* descriptor,
const Message* prototype) {
GOOGLE_DCHECK_EQ(descriptor->file()->pool(), DescriptorPool::generated_pool())
<< "Tried to register a non-generated type with the generated "
"type registry.";
// This should only be called as a result of calling a file registration
// function during GetPrototype(), in which case we already have locked
// the mutex.
mutex_.AssertHeld();
if (!InsertIfNotPresent(&type_map_, descriptor, prototype)) {
GOOGLE_LOG(DFATAL) << "Type is already registered: " << descriptor->full_name();
}
}
const Message* GeneratedMessageFactory::GetPrototype(const Descriptor* type) {
{
ReaderMutexLock lock(&mutex_);
const Message* result = FindPtrOrNull(type_map_, type);
if (result != NULL) return result;
}
// If the type is not in the generated pool, then we can't possibly handle
// it.
if (type->file()->pool() != DescriptorPool::generated_pool()) return NULL;
// Apparently the file hasn't been registered yet. Let's do that now.
const internal::DescriptorTable* registration_data =
FindPtrOrNull(file_map_, type->file()->name().c_str());
if (registration_data == NULL) {
GOOGLE_LOG(DFATAL) << "File appears to be in generated pool but wasn't "
"registered: "
<< type->file()->name();
return NULL;
}
WriterMutexLock lock(&mutex_);
// Check if another thread preempted us.
const Message* result = FindPtrOrNull(type_map_, type);
if (result == NULL) {
// Nope. OK, register everything.
internal::RegisterFileLevelMetadata(registration_data);
// Should be here now.
result = FindPtrOrNull(type_map_, type);
}
if (result == NULL) {
GOOGLE_LOG(DFATAL) << "Type appears to be in generated pool but wasn't "
<< "registered: " << type->full_name();
}
return result;
}
} // namespace
MessageFactory* MessageFactory::generated_factory() {
return GeneratedMessageFactory::singleton();
}
void MessageFactory::InternalRegisterGeneratedFile(
const google::protobuf::internal::DescriptorTable* table) {
GeneratedMessageFactory::singleton()->RegisterFile(table);
}
void MessageFactory::InternalRegisterGeneratedMessage(
const Descriptor* descriptor, const Message* prototype) {
GeneratedMessageFactory::singleton()->RegisterType(descriptor, prototype);
}
namespace {
template <typename T>
T* GetSingleton() {
static T singleton;
return &singleton;
}
} // namespace
const internal::RepeatedFieldAccessor* Reflection::RepeatedFieldAccessor(
const FieldDescriptor* field) const {
GOOGLE_CHECK(field->is_repeated());
switch (field->cpp_type()) {
#define HANDLE_PRIMITIVE_TYPE(TYPE, type) \
case FieldDescriptor::CPPTYPE_##TYPE: \
return GetSingleton<internal::RepeatedFieldPrimitiveAccessor<type> >();
HANDLE_PRIMITIVE_TYPE(INT32, int32)
HANDLE_PRIMITIVE_TYPE(UINT32, uint32)
HANDLE_PRIMITIVE_TYPE(INT64, int64)
HANDLE_PRIMITIVE_TYPE(UINT64, uint64)
HANDLE_PRIMITIVE_TYPE(FLOAT, float)
HANDLE_PRIMITIVE_TYPE(DOUBLE, double)
HANDLE_PRIMITIVE_TYPE(BOOL, bool)
HANDLE_PRIMITIVE_TYPE(ENUM, int32)
#undef HANDLE_PRIMITIVE_TYPE
case FieldDescriptor::CPPTYPE_STRING:
switch (field->options().ctype()) {
default:
case FieldOptions::STRING:
return GetSingleton<internal::RepeatedPtrFieldStringAccessor>();
}
break;
case FieldDescriptor::CPPTYPE_MESSAGE:
if (field->is_map()) {
return GetSingleton<internal::MapFieldAccessor>();
} else {
return GetSingleton<internal::RepeatedPtrFieldMessageAccessor>();
}
}
GOOGLE_LOG(FATAL) << "Should not reach here.";
return NULL;
}
namespace internal {
template <>
#if defined(_MSC_VER) && (_MSC_VER >= 1800)
// Note: force noinline to workaround MSVC compiler bug with /Zc:inline, issue
// #240
PROTOBUF_NOINLINE
#endif
Message*
GenericTypeHandler<Message>::NewFromPrototype(const Message* prototype,
Arena* arena) {
return prototype->New(arena);
}
template <>
#if defined(_MSC_VER) && (_MSC_VER >= 1800)
// Note: force noinline to workaround MSVC compiler bug with /Zc:inline, issue
// #240
PROTOBUF_NOINLINE
#endif
Arena*
GenericTypeHandler<Message>::GetOwningArena(Message* value) {
return value->GetOwningArena();
}
} // namespace internal
} // namespace protobuf
} // namespace google
#include <google/protobuf/port_undef.inc>
|