OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 28 matching lines...) Expand all Loading... |
39 #include <google/protobuf/message.h> | 39 #include <google/protobuf/message.h> |
40 | 40 |
41 #include <google/protobuf/stubs/logging.h> | 41 #include <google/protobuf/stubs/logging.h> |
42 #include <google/protobuf/stubs/common.h> | 42 #include <google/protobuf/stubs/common.h> |
43 #include <google/protobuf/stubs/mutex.h> | 43 #include <google/protobuf/stubs/mutex.h> |
44 #include <google/protobuf/stubs/once.h> | 44 #include <google/protobuf/stubs/once.h> |
45 #include <google/protobuf/reflection_internal.h> | 45 #include <google/protobuf/reflection_internal.h> |
46 #include <google/protobuf/io/coded_stream.h> | 46 #include <google/protobuf/io/coded_stream.h> |
47 #include <google/protobuf/io/zero_copy_stream_impl.h> | 47 #include <google/protobuf/io/zero_copy_stream_impl.h> |
48 #include <google/protobuf/descriptor.pb.h> | 48 #include <google/protobuf/descriptor.pb.h> |
49 #include <google/protobuf/map_field.h> | |
50 #include <google/protobuf/descriptor.h> | 49 #include <google/protobuf/descriptor.h> |
51 #include <google/protobuf/generated_message_util.h> | 50 #include <google/protobuf/generated_message_util.h> |
| 51 #include <google/protobuf/map_field.h> |
52 #include <google/protobuf/reflection_ops.h> | 52 #include <google/protobuf/reflection_ops.h> |
53 #include <google/protobuf/wire_format.h> | 53 #include <google/protobuf/wire_format.h> |
54 #include <google/protobuf/stubs/strutil.h> | 54 #include <google/protobuf/stubs/strutil.h> |
55 #include <google/protobuf/stubs/map_util.h> | 55 #include <google/protobuf/stubs/map_util.h> |
56 #include <google/protobuf/stubs/singleton.h> | 56 #include <google/protobuf/stubs/singleton.h> |
57 #include <google/protobuf/stubs/stl_util.h> | 57 #include <google/protobuf/stubs/stl_util.h> |
58 | 58 |
59 namespace google { | 59 namespace google { |
60 namespace protobuf { | 60 namespace protobuf { |
61 | 61 |
62 using internal::WireFormat; | 62 using internal::WireFormat; |
63 using internal::ReflectionOps; | 63 using internal::ReflectionOps; |
64 | 64 |
65 Message::~Message() {} | |
66 | |
67 void Message::MergeFrom(const Message& from) { | 65 void Message::MergeFrom(const Message& from) { |
68 const Descriptor* descriptor = GetDescriptor(); | 66 const Descriptor* descriptor = GetDescriptor(); |
69 GOOGLE_CHECK_EQ(from.GetDescriptor(), descriptor) | 67 GOOGLE_CHECK_EQ(from.GetDescriptor(), descriptor) |
70 << ": Tried to merge from a message with a different type. " | 68 << ": Tried to merge from a message with a different type. " |
71 "to: " << descriptor->full_name() << ", " | 69 "to: " << descriptor->full_name() << ", " |
72 "from: " << from.GetDescriptor()->full_name(); | 70 "from: " << from.GetDescriptor()->full_name(); |
73 ReflectionOps::Merge(from, this); | 71 ReflectionOps::Merge(from, this); |
74 } | 72 } |
75 | 73 |
76 void Message::CheckTypeAndMergeFrom(const MessageLite& other) { | 74 void Message::CheckTypeAndMergeFrom(const MessageLite& other) { |
(...skipping 14 matching lines...) Expand all Loading... |
91 } | 89 } |
92 | 90 |
93 void Message::Clear() { | 91 void Message::Clear() { |
94 ReflectionOps::Clear(this); | 92 ReflectionOps::Clear(this); |
95 } | 93 } |
96 | 94 |
97 bool Message::IsInitialized() const { | 95 bool Message::IsInitialized() const { |
98 return ReflectionOps::IsInitialized(*this); | 96 return ReflectionOps::IsInitialized(*this); |
99 } | 97 } |
100 | 98 |
101 void Message::FindInitializationErrors(vector<string>* errors) const { | 99 void Message::FindInitializationErrors(std::vector<string>* errors) const { |
102 return ReflectionOps::FindInitializationErrors(*this, "", errors); | 100 return ReflectionOps::FindInitializationErrors(*this, "", errors); |
103 } | 101 } |
104 | 102 |
105 string Message::InitializationErrorString() const { | 103 string Message::InitializationErrorString() const { |
106 vector<string> errors; | 104 std::vector<string> errors; |
107 FindInitializationErrors(&errors); | 105 FindInitializationErrors(&errors); |
108 return Join(errors, ", "); | 106 return Join(errors, ", "); |
109 } | 107 } |
110 | 108 |
111 void Message::CheckInitialized() const { | 109 void Message::CheckInitialized() const { |
112 GOOGLE_CHECK(IsInitialized()) | 110 GOOGLE_CHECK(IsInitialized()) |
113 << "Message of type \"" << GetDescriptor()->full_name() | 111 << "Message of type \"" << GetDescriptor()->full_name() |
114 << "\" is missing required fields: " << InitializationErrorString(); | 112 << "\" is missing required fields: " << InitializationErrorString(); |
115 } | 113 } |
116 | 114 |
(...skipping 24 matching lines...) Expand all Loading... |
141 io::IstreamInputStream zero_copy_input(input); | 139 io::IstreamInputStream zero_copy_input(input); |
142 return ParsePartialFromZeroCopyStream(&zero_copy_input) && input->eof(); | 140 return ParsePartialFromZeroCopyStream(&zero_copy_input) && input->eof(); |
143 } | 141 } |
144 | 142 |
145 | 143 |
146 void Message::SerializeWithCachedSizes( | 144 void Message::SerializeWithCachedSizes( |
147 io::CodedOutputStream* output) const { | 145 io::CodedOutputStream* output) const { |
148 WireFormat::SerializeWithCachedSizes(*this, GetCachedSize(), output); | 146 WireFormat::SerializeWithCachedSizes(*this, GetCachedSize(), output); |
149 } | 147 } |
150 | 148 |
151 int Message::ByteSize() const { | 149 size_t Message::ByteSizeLong() const { |
152 int size = WireFormat::ByteSize(*this); | 150 size_t size = WireFormat::ByteSize(*this); |
153 SetCachedSize(size); | 151 SetCachedSize(internal::ToCachedSize(size)); |
154 return size; | 152 return size; |
155 } | 153 } |
156 | 154 |
157 void Message::SetCachedSize(int /* size */) const { | 155 void Message::SetCachedSize(int /* size */) const { |
158 GOOGLE_LOG(FATAL) << "Message class \"" << GetDescriptor()->full_name() | 156 GOOGLE_LOG(FATAL) << "Message class \"" << GetDescriptor()->full_name() |
159 << "\" implements neither SetCachedSize() nor ByteSize(). " | 157 << "\" implements neither SetCachedSize() nor ByteSize(). " |
160 "Must implement one or the other."; | 158 "Must implement one or the other."; |
161 } | 159 } |
162 | 160 |
163 int Message::SpaceUsed() const { | 161 int Message::SpaceUsed() const { |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 | 218 |
221 #undef HANDLE_TYPE | 219 #undef HANDLE_TYPE |
222 | 220 |
223 void* Reflection::MutableRawRepeatedString( | 221 void* Reflection::MutableRawRepeatedString( |
224 Message* message, const FieldDescriptor* field, bool is_string) const { | 222 Message* message, const FieldDescriptor* field, bool is_string) const { |
225 return MutableRawRepeatedField(message, field, | 223 return MutableRawRepeatedField(message, field, |
226 FieldDescriptor::CPPTYPE_STRING, FieldOptions::STRING, NULL); | 224 FieldDescriptor::CPPTYPE_STRING, FieldOptions::STRING, NULL); |
227 } | 225 } |
228 | 226 |
229 | 227 |
230 // Default EnumValue API implementations. Real reflection implementations should | |
231 // override these. However, there are several legacy implementations that do | |
232 // not, and cannot easily be changed at the same time as the Reflection API, so | |
233 // we provide these for now. | |
234 // TODO: Remove these once all Reflection implementations are updated. | |
235 int Reflection::GetEnumValue(const Message& message, | |
236 const FieldDescriptor* field) const { | |
237 GOOGLE_LOG(FATAL) << "Unimplemented EnumValue API."; | |
238 return 0; | |
239 } | |
240 void Reflection::SetEnumValue(Message* message, | |
241 const FieldDescriptor* field, | |
242 int value) const { | |
243 GOOGLE_LOG(FATAL) << "Unimplemented EnumValue API."; | |
244 } | |
245 int Reflection::GetRepeatedEnumValue( | |
246 const Message& message, | |
247 const FieldDescriptor* field, int index) const { | |
248 GOOGLE_LOG(FATAL) << "Unimplemented EnumValue API."; | |
249 return 0; | |
250 } | |
251 void Reflection::SetRepeatedEnumValue(Message* message, | |
252 const FieldDescriptor* field, int index, | |
253 int value) const { | |
254 GOOGLE_LOG(FATAL) << "Unimplemented EnumValue API."; | |
255 } | |
256 void Reflection::AddEnumValue(Message* message, | |
257 const FieldDescriptor* field, | |
258 int value) const { | |
259 GOOGLE_LOG(FATAL) << "Unimplemented EnumValue API."; | |
260 } | |
261 | |
262 MapIterator Reflection::MapBegin( | 228 MapIterator Reflection::MapBegin( |
263 Message* message, | 229 Message* message, |
264 const FieldDescriptor* field) const { | 230 const FieldDescriptor* field) const { |
265 GOOGLE_LOG(FATAL) << "Unimplemented Map Reflection API."; | 231 GOOGLE_LOG(FATAL) << "Unimplemented Map Reflection API."; |
266 MapIterator iter(message, field); | 232 MapIterator iter(message, field); |
267 return iter; | 233 return iter; |
268 } | 234 } |
269 | 235 |
270 MapIterator Reflection::MapEnd( | 236 MapIterator Reflection::MapEnd( |
271 Message* message, | 237 Message* message, |
(...skipping 22 matching lines...) Expand all Loading... |
294 void RegisterType(const Descriptor* descriptor, const Message* prototype); | 260 void RegisterType(const Descriptor* descriptor, const Message* prototype); |
295 | 261 |
296 // implements MessageFactory --------------------------------------- | 262 // implements MessageFactory --------------------------------------- |
297 const Message* GetPrototype(const Descriptor* type); | 263 const Message* GetPrototype(const Descriptor* type); |
298 | 264 |
299 private: | 265 private: |
300 // Only written at static init time, so does not require locking. | 266 // Only written at static init time, so does not require locking. |
301 hash_map<const char*, RegistrationFunc*, | 267 hash_map<const char*, RegistrationFunc*, |
302 hash<const char*>, streq> file_map_; | 268 hash<const char*>, streq> file_map_; |
303 | 269 |
| 270 Mutex mutex_; |
304 // Initialized lazily, so requires locking. | 271 // Initialized lazily, so requires locking. |
305 Mutex mutex_; | |
306 hash_map<const Descriptor*, const Message*> type_map_; | 272 hash_map<const Descriptor*, const Message*> type_map_; |
307 }; | 273 }; |
308 | 274 |
309 GeneratedMessageFactory* generated_message_factory_ = NULL; | 275 GeneratedMessageFactory* generated_message_factory_ = NULL; |
310 GOOGLE_PROTOBUF_DECLARE_ONCE(generated_message_factory_once_init_); | 276 GOOGLE_PROTOBUF_DECLARE_ONCE(generated_message_factory_once_init_); |
311 | 277 |
312 void ShutdownGeneratedMessageFactory() { | 278 void ShutdownGeneratedMessageFactory() { |
313 delete generated_message_factory_; | 279 delete generated_message_factory_; |
314 } | 280 } |
315 | 281 |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
508 GOOGLE_ATTRIBUTE_NOINLINE | 474 GOOGLE_ATTRIBUTE_NOINLINE |
509 #endif | 475 #endif |
510 void* GenericTypeHandler<Message>::GetMaybeArenaPointer( | 476 void* GenericTypeHandler<Message>::GetMaybeArenaPointer( |
511 Message* value) { | 477 Message* value) { |
512 return value->GetMaybeArenaPointer(); | 478 return value->GetMaybeArenaPointer(); |
513 } | 479 } |
514 } // namespace internal | 480 } // namespace internal |
515 | 481 |
516 } // namespace protobuf | 482 } // namespace protobuf |
517 } // namespace google | 483 } // namespace google |
OLD | NEW |