55
55
using internal::WireFormat;
56
56
using internal::ReflectionOps;
58
static string InitializationErrorMessage(const char* action,
59
const Message& message) {
60
return strings::Substitute(
61
"Can't $0 message of type \"$1\" because it is missing required "
63
action, message.GetDescriptor()->full_name(),
64
message.InitializationErrorString());
67
58
Message::~Message() {}
69
60
void Message::MergeFrom(const Message& from) {
75
66
ReflectionOps::Merge(from, this);
69
void Message::CheckTypeAndMergeFrom(const MessageLite& other) {
70
MergeFrom(*down_cast<const Message*>(&other));
78
73
void Message::CopyFrom(const Message& from) {
79
74
const Descriptor* descriptor = GetDescriptor();
80
75
GOOGLE_CHECK_EQ(from.GetDescriptor(), descriptor)
116
115
return WireFormat::ParseAndMergePartial(input, this);
119
bool Message::MergeFromCodedStream(io::CodedInputStream* input) {
120
if (!MergePartialFromCodedStream(input)) return false;
121
if (!IsInitialized()) {
122
GOOGLE_LOG(ERROR) << InitializationErrorMessage("parse", *this);
128
bool Message::ParseFromCodedStream(io::CodedInputStream* input) {
130
return MergeFromCodedStream(input);
133
bool Message::ParsePartialFromCodedStream(io::CodedInputStream* input) {
135
return MergePartialFromCodedStream(input);
138
bool Message::ParseFromZeroCopyStream(io::ZeroCopyInputStream* input) {
139
io::CodedInputStream decoder(input);
140
return ParseFromCodedStream(&decoder) && decoder.ConsumedEntireMessage();
143
bool Message::ParsePartialFromZeroCopyStream(io::ZeroCopyInputStream* input) {
144
io::CodedInputStream decoder(input);
145
return ParsePartialFromCodedStream(&decoder) &&
146
decoder.ConsumedEntireMessage();
149
bool Message::ParseFromBoundedZeroCopyStream(
150
io::ZeroCopyInputStream* input, int size) {
151
io::CodedInputStream decoder(input);
152
decoder.PushLimit(size);
153
return ParseFromCodedStream(&decoder) &&
154
decoder.ConsumedEntireMessage() &&
155
decoder.BytesUntilLimit() == 0;
158
bool Message::ParsePartialFromBoundedZeroCopyStream(
159
io::ZeroCopyInputStream* input, int size) {
160
io::CodedInputStream decoder(input);
161
decoder.PushLimit(size);
162
return ParsePartialFromCodedStream(&decoder) &&
163
decoder.ConsumedEntireMessage() &&
164
decoder.BytesUntilLimit() == 0;
167
bool Message::ParseFromString(const string& data) {
168
io::ArrayInputStream input(data.data(), data.size());
169
return ParseFromBoundedZeroCopyStream(&input, data.size());
172
bool Message::ParsePartialFromString(const string& data) {
173
io::ArrayInputStream input(data.data(), data.size());
174
return ParsePartialFromBoundedZeroCopyStream(&input, data.size());
177
bool Message::ParseFromArray(const void* data, int size) {
178
io::ArrayInputStream input(data, size);
179
return ParseFromBoundedZeroCopyStream(&input, size);
182
bool Message::ParsePartialFromArray(const void* data, int size) {
183
io::ArrayInputStream input(data, size);
184
return ParsePartialFromBoundedZeroCopyStream(&input, size);
187
118
bool Message::ParseFromFileDescriptor(int file_descriptor) {
188
119
io::FileInputStream input(file_descriptor);
189
120
return ParseFromZeroCopyStream(&input) && input.GetErrno() == 0;
209
139
void Message::SerializeWithCachedSizes(
210
140
io::CodedOutputStream* output) const {
211
141
WireFormat::SerializeWithCachedSizes(*this, GetCachedSize(), output);
214
uint8* Message::SerializeWithCachedSizesToArray(uint8* target) const {
215
// We only optimize this when using optimize_for = SPEED.
216
int size = GetCachedSize();
217
io::ArrayOutputStream out(target, size);
218
io::CodedOutputStream coded_out(&out);
219
SerializeWithCachedSizes(&coded_out);
220
GOOGLE_CHECK(!coded_out.HadError());
221
return target + size;
224
144
int Message::ByteSize() const {
225
145
int size = WireFormat::ByteSize(*this);
226
146
SetCachedSize(size);
237
157
return GetReflection()->SpaceUsed(*this);
240
bool Message::SerializeToCodedStream(io::CodedOutputStream* output) const {
241
GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *this);
242
return SerializePartialToCodedStream(output);
245
bool Message::SerializePartialToCodedStream(
246
io::CodedOutputStream* output) const {
247
ByteSize(); // Force size to be cached.
248
SerializeWithCachedSizes(output);
249
return !output->HadError();
252
bool Message::SerializeToZeroCopyStream(
253
io::ZeroCopyOutputStream* output) const {
254
io::CodedOutputStream encoder(output);
255
return SerializeToCodedStream(&encoder);
258
bool Message::SerializePartialToZeroCopyStream(
259
io::ZeroCopyOutputStream* output) const {
260
io::CodedOutputStream encoder(output);
261
return SerializePartialToCodedStream(&encoder);
264
bool Message::AppendToString(string* output) const {
265
GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *this);
266
return AppendPartialToString(output);
269
bool Message::AppendPartialToString(string* output) const {
270
int old_size = output->size();
271
int byte_size = ByteSize();
272
STLStringResizeUninitialized(output, old_size + byte_size);
273
uint8* start = reinterpret_cast<uint8*>(string_as_array(output) + old_size);
274
uint8* end = SerializeWithCachedSizesToArray(start);
275
GOOGLE_CHECK_EQ(end, start + byte_size);
279
bool Message::SerializeToString(string* output) const {
281
return AppendToString(output);
284
bool Message::SerializePartialToString(string* output) const {
286
return AppendPartialToString(output);
289
bool Message::SerializeToArray(void* data, int size) const {
290
GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *this);
291
return SerializePartialToArray(data, size);
294
bool Message::SerializePartialToArray(void* data, int size) const {
295
int byte_size = ByteSize();
296
if (size < byte_size) return false;
298
SerializeWithCachedSizesToArray(reinterpret_cast<uint8*>(data));
299
GOOGLE_CHECK_EQ(end, reinterpret_cast<uint8*>(data) + byte_size);
303
160
bool Message::SerializeToFileDescriptor(int file_descriptor) const {
304
161
io::FileOutputStream output(file_descriptor);
305
162
return SerializeToZeroCopyStream(&output);
324
string Message::SerializeAsString() const {
325
// If the compiler implements the (Named) Return Value Optimization,
326
// the local variable 'result' will not actually reside on the stack
327
// of this function, but will be overlaid with the object that the
328
// caller supplied for the return value to be constructed in.
330
if (!AppendToString(&output))
335
string Message::SerializePartialAsString() const {
337
if (!AppendPartialToString(&output))
342
181
Reflection::~Reflection() {}
344
183
// ===================================================================
372
211
hash_map<const Descriptor*, const Message*> type_map_;
214
GeneratedMessageFactory* generated_message_factory_ = NULL;
215
GOOGLE_PROTOBUF_DECLARE_ONCE(generated_message_factory_once_init_);
217
void ShutdownGeneratedMessageFactory() {
218
delete generated_message_factory_;
221
void InitGeneratedMessageFactory() {
222
generated_message_factory_ = new GeneratedMessageFactory;
223
internal::OnShutdown(&ShutdownGeneratedMessageFactory);
375
226
GeneratedMessageFactory::GeneratedMessageFactory() {}
376
227
GeneratedMessageFactory::~GeneratedMessageFactory() {}
378
229
GeneratedMessageFactory* GeneratedMessageFactory::singleton() {
379
// No need for thread-safety here because this will be called at static
380
// initialization time. (And GCC4 makes this thread-safe anyway.)
381
static GeneratedMessageFactory singleton;
230
GoogleOnceInit(&generated_message_factory_once_init_,
231
&InitGeneratedMessageFactory);
232
return generated_message_factory_;
385
235
void GeneratedMessageFactory::RegisterFile(