| 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 // http://code.google.com/p/protobuf/ | 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. |
| 11 // * Redistributions in binary form must reproduce the above | 11 // * Redistributions in binary form must reproduce the above |
| 12 // copyright notice, this list of conditions and the following disclaimer | 12 // copyright notice, this list of conditions and the following disclaimer |
| 13 // in the documentation and/or other materials provided with the | 13 // in the documentation and/or other materials provided with the |
| (...skipping 11 matching lines...) Expand all Loading... |
| 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | 30 |
| 31 // Author: kenton@google.com (Kenton Varda) | 31 // Author: kenton@google.com (Kenton Varda) |
| 32 // Based on original Protocol Buffers design by | 32 // Based on original Protocol Buffers design by |
| 33 // Sanjay Ghemawat, Jeff Dean, and others. | 33 // Sanjay Ghemawat, Jeff Dean, and others. |
| 34 | 34 |
| 35 #include <istream> | 35 #include <iostream> |
| 36 #include <stack> | 36 #include <stack> |
| 37 #include <google/protobuf/stubs/hash.h> | 37 #include <google/protobuf/stubs/hash.h> |
| 38 | 38 |
| 39 #include <google/protobuf/message.h> | 39 #include <google/protobuf/message.h> |
| 40 | 40 |
| 41 #include <google/protobuf/stubs/common.h> | 41 #include <google/protobuf/stubs/common.h> |
| 42 #include <google/protobuf/stubs/once.h> | 42 #include <google/protobuf/stubs/once.h> |
| 43 #include <google/protobuf/reflection_internal.h> |
| 43 #include <google/protobuf/io/coded_stream.h> | 44 #include <google/protobuf/io/coded_stream.h> |
| 44 #include <google/protobuf/io/zero_copy_stream_impl.h> | 45 #include <google/protobuf/io/zero_copy_stream_impl.h> |
| 45 #include <google/protobuf/descriptor.pb.h> | 46 #include <google/protobuf/descriptor.pb.h> |
| 46 #include <google/protobuf/descriptor.h> | 47 #include <google/protobuf/descriptor.h> |
| 47 #include <google/protobuf/generated_message_util.h> | 48 #include <google/protobuf/generated_message_util.h> |
| 48 #include <google/protobuf/reflection_ops.h> | 49 #include <google/protobuf/reflection_ops.h> |
| 49 #include <google/protobuf/wire_format.h> | 50 #include <google/protobuf/wire_format.h> |
| 50 #include <google/protobuf/stubs/strutil.h> | 51 #include <google/protobuf/stubs/strutil.h> |
| 51 #include <google/protobuf/stubs/map-util.h> | 52 #include <google/protobuf/stubs/map_util.h> |
| 53 #include <google/protobuf/stubs/singleton.h> |
| 52 #include <google/protobuf/stubs/stl_util.h> | 54 #include <google/protobuf/stubs/stl_util.h> |
| 53 | 55 |
| 54 namespace google { | 56 namespace google { |
| 55 namespace protobuf { | 57 namespace protobuf { |
| 56 | 58 |
| 57 using internal::WireFormat; | 59 using internal::WireFormat; |
| 58 using internal::ReflectionOps; | 60 using internal::ReflectionOps; |
| 59 | 61 |
| 60 Message::~Message() {} | 62 Message::~Message() {} |
| 61 | 63 |
| 62 void Message::MergeFrom(const Message& from) { | 64 void Message::MergeFrom(const Message& from) { |
| 63 const Descriptor* descriptor = GetDescriptor(); | 65 const Descriptor* descriptor = GetDescriptor(); |
| 64 GOOGLE_CHECK_EQ(from.GetDescriptor(), descriptor) | 66 GOOGLE_CHECK_EQ(from.GetDescriptor(), descriptor) |
| 65 << ": Tried to merge from a message with a different type. " | 67 << ": Tried to merge from a message with a different type. " |
| 66 "to: " << descriptor->full_name() << ", " | 68 "to: " << descriptor->full_name() << ", " |
| 67 "from:" << from.GetDescriptor()->full_name(); | 69 "from:" << from.GetDescriptor()->full_name(); |
| 68 ReflectionOps::Merge(from, this); | 70 ReflectionOps::Merge(from, this); |
| 69 } | 71 } |
| 70 | 72 |
| 71 void Message::CheckTypeAndMergeFrom(const MessageLite& other) { | 73 void Message::CheckTypeAndMergeFrom(const MessageLite& other) { |
| 72 MergeFrom(*down_cast<const Message*>(&other)); | 74 MergeFrom(*down_cast<const Message*>(&other)); |
| 73 } | 75 } |
| 74 | 76 |
| 75 void Message::CopyFrom(const Message& from) { | 77 void Message::CopyFrom(const Message& from) { |
| 76 const Descriptor* descriptor = GetDescriptor(); | 78 const Descriptor* descriptor = GetDescriptor(); |
| 77 GOOGLE_CHECK_EQ(from.GetDescriptor(), descriptor) | 79 GOOGLE_CHECK_EQ(from.GetDescriptor(), descriptor) |
| 78 << ": Tried to copy from a message with a different type." | 80 << ": Tried to copy from a message with a different type. " |
| 79 "to: " << descriptor->full_name() << ", " | 81 "to: " << descriptor->full_name() << ", " |
| 80 "from:" << from.GetDescriptor()->full_name(); | 82 "from:" << from.GetDescriptor()->full_name(); |
| 81 ReflectionOps::Copy(from, this); | 83 ReflectionOps::Copy(from, this); |
| 82 } | 84 } |
| 83 | 85 |
| 84 string Message::GetTypeName() const { | 86 string Message::GetTypeName() const { |
| 85 return GetDescriptor()->full_name(); | 87 return GetDescriptor()->full_name(); |
| 86 } | 88 } |
| 87 | 89 |
| 88 void Message::Clear() { | 90 void Message::Clear() { |
| 89 ReflectionOps::Clear(this); | 91 ReflectionOps::Clear(this); |
| 90 } | 92 } |
| 91 | 93 |
| 92 bool Message::IsInitialized() const { | 94 bool Message::IsInitialized() const { |
| 93 return ReflectionOps::IsInitialized(*this); | 95 return ReflectionOps::IsInitialized(*this); |
| 94 } | 96 } |
| 95 | 97 |
| 96 void Message::FindInitializationErrors(vector<string>* errors) const { | 98 void Message::FindInitializationErrors(vector<string>* errors) const { |
| 97 return ReflectionOps::FindInitializationErrors(*this, "", errors); | 99 return ReflectionOps::FindInitializationErrors(*this, "", errors); |
| 98 } | 100 } |
| 99 | 101 |
| 100 string Message::InitializationErrorString() const { | 102 string Message::InitializationErrorString() const { |
| 101 vector<string> errors; | 103 vector<string> errors; |
| 102 FindInitializationErrors(&errors); | 104 FindInitializationErrors(&errors); |
| 103 return JoinStrings(errors, ", "); | 105 return Join(errors, ", "); |
| 104 } | 106 } |
| 105 | 107 |
| 106 void Message::CheckInitialized() const { | 108 void Message::CheckInitialized() const { |
| 107 GOOGLE_CHECK(IsInitialized()) | 109 GOOGLE_CHECK(IsInitialized()) |
| 108 << "Message of type \"" << GetDescriptor()->full_name() | 110 << "Message of type \"" << GetDescriptor()->full_name() |
| 109 << "\" is missing required fields: " << InitializationErrorString(); | 111 << "\" is missing required fields: " << InitializationErrorString(); |
| 110 } | 112 } |
| 111 | 113 |
| 112 void Message::DiscardUnknownFields() { | 114 void Message::DiscardUnknownFields() { |
| 113 return ReflectionOps::DiscardUnknownFields(this); | 115 return ReflectionOps::DiscardUnknownFields(this); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 142 io::CodedOutputStream* output) const { | 144 io::CodedOutputStream* output) const { |
| 143 WireFormat::SerializeWithCachedSizes(*this, GetCachedSize(), output); | 145 WireFormat::SerializeWithCachedSizes(*this, GetCachedSize(), output); |
| 144 } | 146 } |
| 145 | 147 |
| 146 int Message::ByteSize() const { | 148 int Message::ByteSize() const { |
| 147 int size = WireFormat::ByteSize(*this); | 149 int size = WireFormat::ByteSize(*this); |
| 148 SetCachedSize(size); | 150 SetCachedSize(size); |
| 149 return size; | 151 return size; |
| 150 } | 152 } |
| 151 | 153 |
| 152 void Message::SetCachedSize(int size) const { | 154 void Message::SetCachedSize(int /* size */) const { |
| 153 GOOGLE_LOG(FATAL) << "Message class \"" << GetDescriptor()->full_name() | 155 GOOGLE_LOG(FATAL) << "Message class \"" << GetDescriptor()->full_name() |
| 154 << "\" implements neither SetCachedSize() nor ByteSize(). " | 156 << "\" implements neither SetCachedSize() nor ByteSize(). " |
| 155 "Must implement one or the other."; | 157 "Must implement one or the other."; |
| 156 } | 158 } |
| 157 | 159 |
| 158 int Message::SpaceUsed() const { | 160 int Message::SpaceUsed() const { |
| 159 return GetReflection()->SpaceUsed(*this); | 161 return GetReflection()->SpaceUsed(*this); |
| 160 } | 162 } |
| 161 | 163 |
| 162 bool Message::SerializeToFileDescriptor(int file_descriptor) const { | 164 bool Message::SerializeToFileDescriptor(int file_descriptor) const { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 | 217 |
| 216 #undef HANDLE_TYPE | 218 #undef HANDLE_TYPE |
| 217 | 219 |
| 218 void* Reflection::MutableRawRepeatedString( | 220 void* Reflection::MutableRawRepeatedString( |
| 219 Message* message, const FieldDescriptor* field, bool is_string) const { | 221 Message* message, const FieldDescriptor* field, bool is_string) const { |
| 220 return MutableRawRepeatedField(message, field, | 222 return MutableRawRepeatedField(message, field, |
| 221 FieldDescriptor::CPPTYPE_STRING, FieldOptions::STRING, NULL); | 223 FieldDescriptor::CPPTYPE_STRING, FieldOptions::STRING, NULL); |
| 222 } | 224 } |
| 223 | 225 |
| 224 | 226 |
| 227 // Default EnumValue API implementations. Real reflection implementations should |
| 228 // override these. However, there are several legacy implementations that do |
| 229 // not, and cannot easily be changed at the same time as the Reflection API, so |
| 230 // we provide these for now. |
| 231 // TODO: Remove these once all Reflection implementations are updated. |
| 232 int Reflection::GetEnumValue(const Message& message, |
| 233 const FieldDescriptor* field) const { |
| 234 GOOGLE_LOG(FATAL) << "Unimplemented EnumValue API."; |
| 235 return 0; |
| 236 } |
| 237 void Reflection::SetEnumValue(Message* message, |
| 238 const FieldDescriptor* field, |
| 239 int value) const { |
| 240 GOOGLE_LOG(FATAL) << "Unimplemented EnumValue API."; |
| 241 } |
| 242 int Reflection::GetRepeatedEnumValue( |
| 243 const Message& message, |
| 244 const FieldDescriptor* field, int index) const { |
| 245 GOOGLE_LOG(FATAL) << "Unimplemented EnumValue API."; |
| 246 return 0; |
| 247 } |
| 248 void Reflection::SetRepeatedEnumValue(Message* message, |
| 249 const FieldDescriptor* field, int index, |
| 250 int value) const { |
| 251 GOOGLE_LOG(FATAL) << "Unimplemented EnumValue API."; |
| 252 } |
| 253 void Reflection::AddEnumValue(Message* message, |
| 254 const FieldDescriptor* field, |
| 255 int value) const { |
| 256 GOOGLE_LOG(FATAL) << "Unimplemented EnumValue API."; |
| 257 } |
| 258 |
| 225 // ============================================================================= | 259 // ============================================================================= |
| 226 // MessageFactory | 260 // MessageFactory |
| 227 | 261 |
| 228 MessageFactory::~MessageFactory() {} | 262 MessageFactory::~MessageFactory() {} |
| 229 | 263 |
| 230 namespace { | 264 namespace { |
| 231 | 265 |
| 232 class GeneratedMessageFactory : public MessageFactory { | 266 class GeneratedMessageFactory : public MessageFactory { |
| 233 public: | 267 public: |
| 234 GeneratedMessageFactory(); | 268 GeneratedMessageFactory(); |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 347 GeneratedMessageFactory::singleton()->RegisterFile(filename, | 381 GeneratedMessageFactory::singleton()->RegisterFile(filename, |
| 348 register_messages); | 382 register_messages); |
| 349 } | 383 } |
| 350 | 384 |
| 351 void MessageFactory::InternalRegisterGeneratedMessage( | 385 void MessageFactory::InternalRegisterGeneratedMessage( |
| 352 const Descriptor* descriptor, const Message* prototype) { | 386 const Descriptor* descriptor, const Message* prototype) { |
| 353 GeneratedMessageFactory::singleton()->RegisterType(descriptor, prototype); | 387 GeneratedMessageFactory::singleton()->RegisterType(descriptor, prototype); |
| 354 } | 388 } |
| 355 | 389 |
| 356 | 390 |
| 391 MessageFactory* Reflection::GetMessageFactory() const { |
| 392 GOOGLE_LOG(FATAL) << "Not implemented."; |
| 393 return NULL; |
| 394 } |
| 395 |
| 396 void* Reflection::RepeatedFieldData( |
| 397 Message* message, const FieldDescriptor* field, |
| 398 FieldDescriptor::CppType cpp_type, |
| 399 const Descriptor* message_type) const { |
| 400 GOOGLE_LOG(FATAL) << "Not implemented."; |
| 401 return NULL; |
| 402 } |
| 403 |
| 404 namespace internal { |
| 405 RepeatedFieldAccessor::~RepeatedFieldAccessor() { |
| 406 } |
| 407 } // namespace internal |
| 408 |
| 409 const internal::RepeatedFieldAccessor* Reflection::RepeatedFieldAccessor( |
| 410 const FieldDescriptor* field) const { |
| 411 GOOGLE_CHECK(field->is_repeated()); |
| 412 switch (field->cpp_type()) { |
| 413 #define HANDLE_PRIMITIVE_TYPE(TYPE, type) \ |
| 414 case FieldDescriptor::CPPTYPE_ ## TYPE: \ |
| 415 return internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<type>
>::get(); |
| 416 HANDLE_PRIMITIVE_TYPE(INT32, int32) |
| 417 HANDLE_PRIMITIVE_TYPE(UINT32, uint32) |
| 418 HANDLE_PRIMITIVE_TYPE(INT64, int64) |
| 419 HANDLE_PRIMITIVE_TYPE(UINT64, uint64) |
| 420 HANDLE_PRIMITIVE_TYPE(FLOAT, float) |
| 421 HANDLE_PRIMITIVE_TYPE(DOUBLE, double) |
| 422 HANDLE_PRIMITIVE_TYPE(BOOL, bool) |
| 423 HANDLE_PRIMITIVE_TYPE(ENUM, int32) |
| 424 #undef HANDLE_PRIMITIVE_TYPE |
| 425 case FieldDescriptor::CPPTYPE_STRING: |
| 426 switch (field->options().ctype()) { |
| 427 default: |
| 428 case FieldOptions::STRING: |
| 429 return internal::Singleton<internal::RepeatedPtrFieldStringAccessor>::
get(); |
| 430 } |
| 431 break; |
| 432 case FieldDescriptor::CPPTYPE_MESSAGE: |
| 433 if (field->is_map()) { |
| 434 return internal::Singleton<internal::MapFieldAccessor>::get(); |
| 435 } else { |
| 436 return internal::Singleton<internal::RepeatedPtrFieldMessageAccessor>::g
et(); |
| 437 } |
| 438 } |
| 439 GOOGLE_LOG(FATAL) << "Should not reach here."; |
| 440 return NULL; |
| 441 } |
| 442 |
| 443 namespace internal { |
| 444 namespace { |
| 445 void ShutdownRepeatedFieldAccessor() { |
| 446 internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<int32> >::ShutDow
n(); |
| 447 internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<uint32> >::ShutDo
wn(); |
| 448 internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<int64> >::ShutDow
n(); |
| 449 internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<uint64> >::ShutDo
wn(); |
| 450 internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<float> >::ShutDow
n(); |
| 451 internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<double> >::ShutDo
wn(); |
| 452 internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<bool> >::ShutDown
(); |
| 453 internal::Singleton<internal::RepeatedPtrFieldStringAccessor>::ShutDown(); |
| 454 internal::Singleton<internal::RepeatedPtrFieldMessageAccessor>::ShutDown(); |
| 455 internal::Singleton<internal::MapFieldAccessor>::ShutDown(); |
| 456 } |
| 457 |
| 458 struct ShutdownRepeatedFieldRegister { |
| 459 ShutdownRepeatedFieldRegister() { |
| 460 OnShutdown(&ShutdownRepeatedFieldAccessor); |
| 461 } |
| 462 } shutdown_; |
| 463 |
| 464 } // namespace |
| 465 } // namespace internal |
| 466 |
| 467 namespace internal { |
| 468 template<> |
| 469 Message* GenericTypeHandler<Message>::NewFromPrototype( |
| 470 const Message* prototype, google::protobuf::Arena* arena) { |
| 471 return prototype->New(arena); |
| 472 } |
| 473 template<> |
| 474 google::protobuf::Arena* GenericTypeHandler<Message>::GetArena( |
| 475 Message* value) { |
| 476 return value->GetArena(); |
| 477 } |
| 478 template<> |
| 479 void* GenericTypeHandler<Message>::GetMaybeArenaPointer( |
| 480 Message* value) { |
| 481 return value->GetMaybeArenaPointer(); |
| 482 } |
| 483 } // namespace internal |
| 484 |
| 357 } // namespace protobuf | 485 } // namespace protobuf |
| 358 } // namespace google | 486 } // namespace google |
| OLD | NEW |