~ubuntu-branches/ubuntu/oneiric/protobuf/oneiric

« back to all changes in this revision

Viewing changes to src/google/protobuf/message.cc

  • Committer: Bazaar Package Importer
  • Author(s): Iustin Pop
  • Date: 2008-08-03 11:01:44 UTC
  • Revision ID: james.westby@ubuntu.com-20080803110144-uyiw41bf1m2oe17t
Tags: upstream-2.0.0~b
ImportĀ upstreamĀ versionĀ 2.0.0~b

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Protocol Buffers - Google's data interchange format
 
2
// Copyright 2008 Google Inc.
 
3
// http://code.google.com/p/protobuf/
 
4
//
 
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
 
8
//
 
9
//      http://www.apache.org/licenses/LICENSE-2.0
 
10
//
 
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.
 
16
 
 
17
// Author: kenton@google.com (Kenton Varda)
 
18
//  Based on original Protocol Buffers design by
 
19
//  Sanjay Ghemawat, Jeff Dean, and others.
 
20
 
 
21
#include <stack>
 
22
#include <google/protobuf/stubs/hash.h>
 
23
 
 
24
#include <google/protobuf/message.h>
 
25
 
 
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>
 
36
 
 
37
namespace google {
 
38
namespace protobuf {
 
39
 
 
40
using internal::WireFormat;
 
41
using internal::ReflectionOps;
 
42
 
 
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 "
 
47
    "fields: $2",
 
48
    action, message.GetDescriptor()->full_name(),
 
49
    message.InitializationErrorString());
 
50
}
 
51
 
 
52
Message::~Message() {}
 
53
Message::Reflection::~Reflection() {}
 
54
 
 
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());
 
62
}
 
63
 
 
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());
 
71
}
 
72
 
 
73
void Message::Clear() {
 
74
  ReflectionOps::Clear(GetDescriptor(), GetReflection());
 
75
}
 
76
 
 
77
bool Message::IsInitialized() const {
 
78
  return ReflectionOps::IsInitialized(GetDescriptor(), *GetReflection());
 
79
}
 
80
 
 
81
void Message::FindInitializationErrors(vector<string>* errors) const {
 
82
  return ReflectionOps::FindInitializationErrors(
 
83
    GetDescriptor(), *GetReflection(), "", errors);
 
84
}
 
85
 
 
86
string Message::InitializationErrorString() const {
 
87
  vector<string> errors;
 
88
  FindInitializationErrors(&errors);
 
89
  return JoinStrings(errors, ", ");
 
90
}
 
91
 
 
92
void Message::CheckInitialized() const {
 
93
  GOOGLE_CHECK(IsInitialized())
 
94
    << "Message of type \"" << GetDescriptor()->full_name()
 
95
    << "\" is missing required fields: " << InitializationErrorString();
 
96
}
 
97
 
 
98
void Message::DiscardUnknownFields() {
 
99
  return ReflectionOps::DiscardUnknownFields(GetDescriptor(), GetReflection());
 
100
}
 
101
 
 
102
bool Message::MergePartialFromCodedStream(io::CodedInputStream* input) {
 
103
  return WireFormat::ParseAndMergePartial(
 
104
    GetDescriptor(), input, GetReflection());
 
105
}
 
106
 
 
107
bool Message::MergeFromCodedStream(io::CodedInputStream* input) {
 
108
  if (!MergePartialFromCodedStream(input)) return false;
 
109
  if (!IsInitialized()) {
 
110
    GOOGLE_LOG(ERROR) << InitializationErrorMessage("parse", *this);
 
111
    return false;
 
112
  }
 
113
  return true;
 
114
}
 
115
 
 
116
bool Message::ParseFromCodedStream(io::CodedInputStream* input) {
 
117
  Clear();
 
118
  return MergeFromCodedStream(input);
 
119
}
 
120
 
 
121
bool Message::ParsePartialFromCodedStream(io::CodedInputStream* input) {
 
122
  Clear();
 
123
  return MergePartialFromCodedStream(input);
 
124
}
 
125
 
 
126
bool Message::ParseFromZeroCopyStream(io::ZeroCopyInputStream* input) {
 
127
  io::CodedInputStream decoder(input);
 
128
  return ParseFromCodedStream(&decoder) && decoder.ConsumedEntireMessage();
 
129
}
 
130
 
 
131
bool Message::ParsePartialFromZeroCopyStream(io::ZeroCopyInputStream* input) {
 
132
  io::CodedInputStream decoder(input);
 
133
  return ParsePartialFromCodedStream(&decoder) &&
 
134
         decoder.ConsumedEntireMessage();
 
135
}
 
136
 
 
137
bool Message::ParseFromString(const string& data) {
 
138
  io::ArrayInputStream input(data.data(), data.size());
 
139
  return ParseFromZeroCopyStream(&input);
 
140
}
 
141
 
 
142
bool Message::ParsePartialFromString(const string& data) {
 
143
  io::ArrayInputStream input(data.data(), data.size());
 
144
  return ParsePartialFromZeroCopyStream(&input);
 
145
}
 
146
 
 
147
bool Message::ParseFromArray(const void* data, int size) {
 
148
  io::ArrayInputStream input(data, size);
 
149
  return ParseFromZeroCopyStream(&input);
 
150
}
 
151
 
 
152
bool Message::ParsePartialFromArray(const void* data, int size) {
 
153
  io::ArrayInputStream input(data, size);
 
154
  return ParsePartialFromZeroCopyStream(&input);
 
155
}
 
156
 
 
157
bool Message::ParseFromFileDescriptor(int file_descriptor) {
 
158
  io::FileInputStream input(file_descriptor);
 
159
  return ParseFromZeroCopyStream(&input) && input.GetErrno() == 0;
 
160
}
 
161
 
 
162
bool Message::ParsePartialFromFileDescriptor(int file_descriptor) {
 
163
  io::FileInputStream input(file_descriptor);
 
164
  return ParsePartialFromZeroCopyStream(&input) && input.GetErrno() == 0;
 
165
}
 
166
 
 
167
bool Message::ParseFromIstream(istream* input) {
 
168
  io::IstreamInputStream zero_copy_input(input);
 
169
  return ParseFromZeroCopyStream(&zero_copy_input) && input->eof();
 
170
}
 
171
 
 
172
bool Message::ParsePartialFromIstream(istream* input) {
 
173
  io::IstreamInputStream zero_copy_input(input);
 
174
  return ParsePartialFromZeroCopyStream(&zero_copy_input) && input->eof();
 
175
}
 
176
 
 
177
 
 
178
 
 
179
bool Message::SerializeWithCachedSizes(
 
180
    io::CodedOutputStream* output) const {
 
181
  return WireFormat::SerializeWithCachedSizes(
 
182
    GetDescriptor(), GetReflection(), GetCachedSize(), output);
 
183
}
 
184
 
 
185
int Message::ByteSize() const {
 
186
  int size = WireFormat::ByteSize(GetDescriptor(), GetReflection());
 
187
  SetCachedSize(size);
 
188
  return size;
 
189
}
 
190
 
 
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.";
 
195
}
 
196
 
 
197
bool Message::SerializeToCodedStream(io::CodedOutputStream* output) const {
 
198
  GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *this);
 
199
  return SerializePartialToCodedStream(output);
 
200
}
 
201
 
 
202
bool Message::SerializePartialToCodedStream(
 
203
    io::CodedOutputStream* output) const {
 
204
  ByteSize();  // Force size to be cached.
 
205
  if (!SerializeWithCachedSizes(output)) return false;
 
206
  return true;
 
207
}
 
208
 
 
209
bool Message::SerializeToZeroCopyStream(
 
210
    io::ZeroCopyOutputStream* output) const {
 
211
  io::CodedOutputStream encoder(output);
 
212
  return SerializeToCodedStream(&encoder);
 
213
}
 
214
 
 
215
bool Message::SerializePartialToZeroCopyStream(
 
216
    io::ZeroCopyOutputStream* output) const {
 
217
  io::CodedOutputStream encoder(output);
 
218
  return SerializePartialToCodedStream(&encoder);
 
219
}
 
220
 
 
221
bool Message::AppendToString(string* output) const {
 
222
  GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *this);
 
223
  return AppendPartialToString(output);
 
224
}
 
225
 
 
226
bool Message::AppendPartialToString(string* output) const {
 
227
  // For efficiency, we'd like to reserve the exact amount of space we need
 
228
  // in the string.
 
229
  int total_size = output->size() + ByteSize();
 
230
  output->reserve(total_size);
 
231
 
 
232
  io::StringOutputStream output_stream(output);
 
233
 
 
234
  {
 
235
    io::CodedOutputStream encoder(&output_stream);
 
236
    if (!SerializeWithCachedSizes(&encoder)) return false;
 
237
  }
 
238
 
 
239
  GOOGLE_CHECK_EQ(output_stream.ByteCount(), total_size);
 
240
  return true;
 
241
}
 
242
 
 
243
bool Message::SerializeToString(string* output) const {
 
244
  output->clear();
 
245
  return AppendToString(output);
 
246
}
 
247
 
 
248
bool Message::SerializePartialToString(string* output) const {
 
249
  output->clear();
 
250
  return AppendPartialToString(output);
 
251
}
 
252
 
 
253
bool Message::SerializeToArray(void* data, int size) const {
 
254
  io::ArrayOutputStream output_stream(data, size);
 
255
  return SerializeToZeroCopyStream(&output_stream);
 
256
}
 
257
 
 
258
bool Message::SerializePartialToArray(void* data, int size) const {
 
259
  io::ArrayOutputStream output_stream(data, size);
 
260
  return SerializePartialToZeroCopyStream(&output_stream);
 
261
}
 
262
 
 
263
bool Message::SerializeToFileDescriptor(int file_descriptor) const {
 
264
  io::FileOutputStream output(file_descriptor);
 
265
  return SerializeToZeroCopyStream(&output);
 
266
}
 
267
 
 
268
bool Message::SerializePartialToFileDescriptor(int file_descriptor) const {
 
269
  io::FileOutputStream output(file_descriptor);
 
270
  return SerializePartialToZeroCopyStream(&output);
 
271
}
 
272
 
 
273
bool Message::SerializeToOstream(ostream* output) const {
 
274
  io::OstreamOutputStream zero_copy_output(output);
 
275
  return SerializeToZeroCopyStream(&zero_copy_output);
 
276
}
 
277
 
 
278
bool Message::SerializePartialToOstream(ostream* output) const {
 
279
  io::OstreamOutputStream zero_copy_output(output);
 
280
  return SerializePartialToZeroCopyStream(&zero_copy_output);
 
281
}
 
282
 
 
283
 
 
284
// ===================================================================
 
285
// MessageFactory
 
286
 
 
287
MessageFactory::~MessageFactory() {}
 
288
 
 
289
namespace {
 
290
 
 
291
class GeneratedMessageFactory : public MessageFactory {
 
292
 public:
 
293
  GeneratedMessageFactory();
 
294
  ~GeneratedMessageFactory();
 
295
 
 
296
  static GeneratedMessageFactory* singleton();
 
297
 
 
298
  void RegisterType(const Descriptor* descriptor, const Message* prototype);
 
299
 
 
300
  // implements MessageFactory ---------------------------------------
 
301
  const Message* GetPrototype(const Descriptor* type);
 
302
 
 
303
 private:
 
304
  hash_map<const Descriptor*, const Message*> type_map_;
 
305
};
 
306
 
 
307
GeneratedMessageFactory::GeneratedMessageFactory() {}
 
308
GeneratedMessageFactory::~GeneratedMessageFactory() {}
 
309
 
 
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;
 
314
  return &singleton;
 
315
}
 
316
 
 
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 "
 
321
       "type registry.";
 
322
 
 
323
  if (!InsertIfNotPresent(&type_map_, descriptor, prototype)) {
 
324
    GOOGLE_LOG(DFATAL) << "Type is already registered: " << descriptor->full_name();
 
325
  }
 
326
}
 
327
 
 
328
const Message* GeneratedMessageFactory::GetPrototype(const Descriptor* type) {
 
329
  return FindPtrOrNull(type_map_, type);
 
330
}
 
331
 
 
332
}  // namespace
 
333
 
 
334
MessageFactory* MessageFactory::generated_factory() {
 
335
  return GeneratedMessageFactory::singleton();
 
336
}
 
337
 
 
338
void MessageFactory::InternalRegisterGeneratedMessage(
 
339
    const Descriptor* descriptor, const Message* prototype) {
 
340
  GeneratedMessageFactory::singleton()->RegisterType(descriptor, prototype);
 
341
}
 
342
 
 
343
 
 
344
}  // namespace protobuf
 
345
}  // namespace google