1
// Protocol Buffers - Google's data interchange format
2
// Copyright 2008 Google Inc.
3
// http://code.google.com/p/protobuf/
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
9
// http://www.apache.org/licenses/LICENSE-2.0
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
17
// Author: kenton@google.com (Kenton Varda)
18
// Based on original Protocol Buffers design by
19
// Sanjay Ghemawat, Jeff Dean, and others.
22
#include <google/protobuf/stubs/hash.h>
24
#include <google/protobuf/message.h>
26
#include <google/protobuf/stubs/common.h>
27
#include <google/protobuf/io/coded_stream.h>
28
#include <google/protobuf/io/zero_copy_stream_impl.h>
29
#include <google/protobuf/descriptor.pb.h>
30
#include <google/protobuf/descriptor.h>
31
#include <google/protobuf/reflection_ops.h>
32
#include <google/protobuf/wire_format.h>
33
#include <google/protobuf/stubs/strutil.h>
34
#include <google/protobuf/stubs/substitute.h>
35
#include <google/protobuf/stubs/map-util.h>
40
using internal::WireFormat;
41
using internal::ReflectionOps;
43
static string InitializationErrorMessage(const char* action,
44
const Message& message) {
45
return strings::Substitute(
46
"Can't $0 message of type \"$1\" because it is missing required "
48
action, message.GetDescriptor()->full_name(),
49
message.InitializationErrorString());
52
Message::~Message() {}
53
Message::Reflection::~Reflection() {}
55
void Message::MergeFrom(const Message& from) {
56
const Descriptor* descriptor = GetDescriptor();
57
GOOGLE_CHECK_EQ(from.GetDescriptor(), descriptor)
58
<< ": Tried to merge from a message with a different type. "
59
"to: " << descriptor->full_name() << ", "
60
"from:" << from.GetDescriptor()->full_name();
61
ReflectionOps::Merge(descriptor, *from.GetReflection(), GetReflection());
64
void Message::CopyFrom(const Message& from) {
65
const Descriptor* descriptor = GetDescriptor();
66
GOOGLE_CHECK_EQ(from.GetDescriptor(), descriptor)
67
<< ": Tried to copy from a message with a different type."
68
"to: " << descriptor->full_name() << ", "
69
"from:" << from.GetDescriptor()->full_name();
70
ReflectionOps::Copy(descriptor, *from.GetReflection(), GetReflection());
73
void Message::Clear() {
74
ReflectionOps::Clear(GetDescriptor(), GetReflection());
77
bool Message::IsInitialized() const {
78
return ReflectionOps::IsInitialized(GetDescriptor(), *GetReflection());
81
void Message::FindInitializationErrors(vector<string>* errors) const {
82
return ReflectionOps::FindInitializationErrors(
83
GetDescriptor(), *GetReflection(), "", errors);
86
string Message::InitializationErrorString() const {
87
vector<string> errors;
88
FindInitializationErrors(&errors);
89
return JoinStrings(errors, ", ");
92
void Message::CheckInitialized() const {
93
GOOGLE_CHECK(IsInitialized())
94
<< "Message of type \"" << GetDescriptor()->full_name()
95
<< "\" is missing required fields: " << InitializationErrorString();
98
void Message::DiscardUnknownFields() {
99
return ReflectionOps::DiscardUnknownFields(GetDescriptor(), GetReflection());
102
bool Message::MergePartialFromCodedStream(io::CodedInputStream* input) {
103
return WireFormat::ParseAndMergePartial(
104
GetDescriptor(), input, GetReflection());
107
bool Message::MergeFromCodedStream(io::CodedInputStream* input) {
108
if (!MergePartialFromCodedStream(input)) return false;
109
if (!IsInitialized()) {
110
GOOGLE_LOG(ERROR) << InitializationErrorMessage("parse", *this);
116
bool Message::ParseFromCodedStream(io::CodedInputStream* input) {
118
return MergeFromCodedStream(input);
121
bool Message::ParsePartialFromCodedStream(io::CodedInputStream* input) {
123
return MergePartialFromCodedStream(input);
126
bool Message::ParseFromZeroCopyStream(io::ZeroCopyInputStream* input) {
127
io::CodedInputStream decoder(input);
128
return ParseFromCodedStream(&decoder) && decoder.ConsumedEntireMessage();
131
bool Message::ParsePartialFromZeroCopyStream(io::ZeroCopyInputStream* input) {
132
io::CodedInputStream decoder(input);
133
return ParsePartialFromCodedStream(&decoder) &&
134
decoder.ConsumedEntireMessage();
137
bool Message::ParseFromString(const string& data) {
138
io::ArrayInputStream input(data.data(), data.size());
139
return ParseFromZeroCopyStream(&input);
142
bool Message::ParsePartialFromString(const string& data) {
143
io::ArrayInputStream input(data.data(), data.size());
144
return ParsePartialFromZeroCopyStream(&input);
147
bool Message::ParseFromArray(const void* data, int size) {
148
io::ArrayInputStream input(data, size);
149
return ParseFromZeroCopyStream(&input);
152
bool Message::ParsePartialFromArray(const void* data, int size) {
153
io::ArrayInputStream input(data, size);
154
return ParsePartialFromZeroCopyStream(&input);
157
bool Message::ParseFromFileDescriptor(int file_descriptor) {
158
io::FileInputStream input(file_descriptor);
159
return ParseFromZeroCopyStream(&input) && input.GetErrno() == 0;
162
bool Message::ParsePartialFromFileDescriptor(int file_descriptor) {
163
io::FileInputStream input(file_descriptor);
164
return ParsePartialFromZeroCopyStream(&input) && input.GetErrno() == 0;
167
bool Message::ParseFromIstream(istream* input) {
168
io::IstreamInputStream zero_copy_input(input);
169
return ParseFromZeroCopyStream(&zero_copy_input) && input->eof();
172
bool Message::ParsePartialFromIstream(istream* input) {
173
io::IstreamInputStream zero_copy_input(input);
174
return ParsePartialFromZeroCopyStream(&zero_copy_input) && input->eof();
179
bool Message::SerializeWithCachedSizes(
180
io::CodedOutputStream* output) const {
181
return WireFormat::SerializeWithCachedSizes(
182
GetDescriptor(), GetReflection(), GetCachedSize(), output);
185
int Message::ByteSize() const {
186
int size = WireFormat::ByteSize(GetDescriptor(), GetReflection());
191
void Message::SetCachedSize(int size) const {
192
GOOGLE_LOG(FATAL) << "Message class \"" << GetDescriptor()->full_name()
193
<< "\" implements neither SetCachedSize() nor ByteSize(). "
194
"Must implement one or the other.";
197
bool Message::SerializeToCodedStream(io::CodedOutputStream* output) const {
198
GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *this);
199
return SerializePartialToCodedStream(output);
202
bool Message::SerializePartialToCodedStream(
203
io::CodedOutputStream* output) const {
204
ByteSize(); // Force size to be cached.
205
if (!SerializeWithCachedSizes(output)) return false;
209
bool Message::SerializeToZeroCopyStream(
210
io::ZeroCopyOutputStream* output) const {
211
io::CodedOutputStream encoder(output);
212
return SerializeToCodedStream(&encoder);
215
bool Message::SerializePartialToZeroCopyStream(
216
io::ZeroCopyOutputStream* output) const {
217
io::CodedOutputStream encoder(output);
218
return SerializePartialToCodedStream(&encoder);
221
bool Message::AppendToString(string* output) const {
222
GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *this);
223
return AppendPartialToString(output);
226
bool Message::AppendPartialToString(string* output) const {
227
// For efficiency, we'd like to reserve the exact amount of space we need
229
int total_size = output->size() + ByteSize();
230
output->reserve(total_size);
232
io::StringOutputStream output_stream(output);
235
io::CodedOutputStream encoder(&output_stream);
236
if (!SerializeWithCachedSizes(&encoder)) return false;
239
GOOGLE_CHECK_EQ(output_stream.ByteCount(), total_size);
243
bool Message::SerializeToString(string* output) const {
245
return AppendToString(output);
248
bool Message::SerializePartialToString(string* output) const {
250
return AppendPartialToString(output);
253
bool Message::SerializeToArray(void* data, int size) const {
254
io::ArrayOutputStream output_stream(data, size);
255
return SerializeToZeroCopyStream(&output_stream);
258
bool Message::SerializePartialToArray(void* data, int size) const {
259
io::ArrayOutputStream output_stream(data, size);
260
return SerializePartialToZeroCopyStream(&output_stream);
263
bool Message::SerializeToFileDescriptor(int file_descriptor) const {
264
io::FileOutputStream output(file_descriptor);
265
return SerializeToZeroCopyStream(&output);
268
bool Message::SerializePartialToFileDescriptor(int file_descriptor) const {
269
io::FileOutputStream output(file_descriptor);
270
return SerializePartialToZeroCopyStream(&output);
273
bool Message::SerializeToOstream(ostream* output) const {
274
io::OstreamOutputStream zero_copy_output(output);
275
return SerializeToZeroCopyStream(&zero_copy_output);
278
bool Message::SerializePartialToOstream(ostream* output) const {
279
io::OstreamOutputStream zero_copy_output(output);
280
return SerializePartialToZeroCopyStream(&zero_copy_output);
284
// ===================================================================
287
MessageFactory::~MessageFactory() {}
291
class GeneratedMessageFactory : public MessageFactory {
293
GeneratedMessageFactory();
294
~GeneratedMessageFactory();
296
static GeneratedMessageFactory* singleton();
298
void RegisterType(const Descriptor* descriptor, const Message* prototype);
300
// implements MessageFactory ---------------------------------------
301
const Message* GetPrototype(const Descriptor* type);
304
hash_map<const Descriptor*, const Message*> type_map_;
307
GeneratedMessageFactory::GeneratedMessageFactory() {}
308
GeneratedMessageFactory::~GeneratedMessageFactory() {}
310
GeneratedMessageFactory* GeneratedMessageFactory::singleton() {
311
// No need for thread-safety here because this will be called at static
312
// initialization time. (And GCC4 makes this thread-safe anyway.)
313
static GeneratedMessageFactory singleton;
317
void GeneratedMessageFactory::RegisterType(const Descriptor* descriptor,
318
const Message* prototype) {
319
GOOGLE_DCHECK_EQ(descriptor->file()->pool(), DescriptorPool::generated_pool())
320
<< "Tried to register a non-generated type with the generated "
323
if (!InsertIfNotPresent(&type_map_, descriptor, prototype)) {
324
GOOGLE_LOG(DFATAL) << "Type is already registered: " << descriptor->full_name();
328
const Message* GeneratedMessageFactory::GetPrototype(const Descriptor* type) {
329
return FindPtrOrNull(type_map_, type);
334
MessageFactory* MessageFactory::generated_factory() {
335
return GeneratedMessageFactory::singleton();
338
void MessageFactory::InternalRegisterGeneratedMessage(
339
const Descriptor* descriptor, const Message* prototype) {
340
GeneratedMessageFactory::singleton()->RegisterType(descriptor, prototype);
344
} // namespace protobuf
345
} // namespace google