| 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/logging.h> |
| 41 #include <google/protobuf/stubs/common.h> | 42 #include <google/protobuf/stubs/common.h> |
| 43 #include <google/protobuf/stubs/mutex.h> |
| 42 #include <google/protobuf/stubs/once.h> | 44 #include <google/protobuf/stubs/once.h> |
| 45 #include <google/protobuf/reflection_internal.h> |
| 43 #include <google/protobuf/io/coded_stream.h> | 46 #include <google/protobuf/io/coded_stream.h> |
| 44 #include <google/protobuf/io/zero_copy_stream_impl.h> | 47 #include <google/protobuf/io/zero_copy_stream_impl.h> |
| 45 #include <google/protobuf/descriptor.pb.h> | 48 #include <google/protobuf/descriptor.pb.h> |
| 49 #include <google/protobuf/map_field.h> |
| 46 #include <google/protobuf/descriptor.h> | 50 #include <google/protobuf/descriptor.h> |
| 47 #include <google/protobuf/generated_message_util.h> | 51 #include <google/protobuf/generated_message_util.h> |
| 48 #include <google/protobuf/reflection_ops.h> | 52 #include <google/protobuf/reflection_ops.h> |
| 49 #include <google/protobuf/wire_format.h> | 53 #include <google/protobuf/wire_format.h> |
| 50 #include <google/protobuf/stubs/strutil.h> | 54 #include <google/protobuf/stubs/strutil.h> |
| 51 #include <google/protobuf/stubs/map-util.h> | 55 #include <google/protobuf/stubs/map_util.h> |
| 56 #include <google/protobuf/stubs/singleton.h> |
| 52 #include <google/protobuf/stubs/stl_util.h> | 57 #include <google/protobuf/stubs/stl_util.h> |
| 53 | 58 |
| 54 namespace google { | 59 namespace google { |
| 55 namespace protobuf { | 60 namespace protobuf { |
| 56 | 61 |
| 57 using internal::WireFormat; | 62 using internal::WireFormat; |
| 58 using internal::ReflectionOps; | 63 using internal::ReflectionOps; |
| 59 | 64 |
| 60 Message::~Message() {} | 65 Message::~Message() {} |
| 61 | 66 |
| 62 void Message::MergeFrom(const Message& from) { | 67 void Message::MergeFrom(const Message& from) { |
| 63 const Descriptor* descriptor = GetDescriptor(); | 68 const Descriptor* descriptor = GetDescriptor(); |
| 64 GOOGLE_CHECK_EQ(from.GetDescriptor(), descriptor) | 69 GOOGLE_CHECK_EQ(from.GetDescriptor(), descriptor) |
| 65 << ": Tried to merge from a message with a different type. " | 70 << ": Tried to merge from a message with a different type. " |
| 66 "to: " << descriptor->full_name() << ", " | 71 "to: " << descriptor->full_name() << ", " |
| 67 "from:" << from.GetDescriptor()->full_name(); | 72 "from:" << from.GetDescriptor()->full_name(); |
| 68 ReflectionOps::Merge(from, this); | 73 ReflectionOps::Merge(from, this); |
| 69 } | 74 } |
| 70 | 75 |
| 71 void Message::CheckTypeAndMergeFrom(const MessageLite& other) { | 76 void Message::CheckTypeAndMergeFrom(const MessageLite& other) { |
| 72 MergeFrom(*down_cast<const Message*>(&other)); | 77 MergeFrom(*down_cast<const Message*>(&other)); |
| 73 } | 78 } |
| 74 | 79 |
| 75 void Message::CopyFrom(const Message& from) { | 80 void Message::CopyFrom(const Message& from) { |
| 76 const Descriptor* descriptor = GetDescriptor(); | 81 const Descriptor* descriptor = GetDescriptor(); |
| 77 GOOGLE_CHECK_EQ(from.GetDescriptor(), descriptor) | 82 GOOGLE_CHECK_EQ(from.GetDescriptor(), descriptor) |
| 78 << ": Tried to copy from a message with a different type." | 83 << ": Tried to copy from a message with a different type. " |
| 79 "to: " << descriptor->full_name() << ", " | 84 "to: " << descriptor->full_name() << ", " |
| 80 "from:" << from.GetDescriptor()->full_name(); | 85 "from:" << from.GetDescriptor()->full_name(); |
| 81 ReflectionOps::Copy(from, this); | 86 ReflectionOps::Copy(from, this); |
| 82 } | 87 } |
| 83 | 88 |
| 84 string Message::GetTypeName() const { | 89 string Message::GetTypeName() const { |
| 85 return GetDescriptor()->full_name(); | 90 return GetDescriptor()->full_name(); |
| 86 } | 91 } |
| 87 | 92 |
| 88 void Message::Clear() { | 93 void Message::Clear() { |
| 89 ReflectionOps::Clear(this); | 94 ReflectionOps::Clear(this); |
| 90 } | 95 } |
| 91 | 96 |
| 92 bool Message::IsInitialized() const { | 97 bool Message::IsInitialized() const { |
| 93 return ReflectionOps::IsInitialized(*this); | 98 return ReflectionOps::IsInitialized(*this); |
| 94 } | 99 } |
| 95 | 100 |
| 96 void Message::FindInitializationErrors(vector<string>* errors) const { | 101 void Message::FindInitializationErrors(vector<string>* errors) const { |
| 97 return ReflectionOps::FindInitializationErrors(*this, "", errors); | 102 return ReflectionOps::FindInitializationErrors(*this, "", errors); |
| 98 } | 103 } |
| 99 | 104 |
| 100 string Message::InitializationErrorString() const { | 105 string Message::InitializationErrorString() const { |
| 101 vector<string> errors; | 106 vector<string> errors; |
| 102 FindInitializationErrors(&errors); | 107 FindInitializationErrors(&errors); |
| 103 return JoinStrings(errors, ", "); | 108 return Join(errors, ", "); |
| 104 } | 109 } |
| 105 | 110 |
| 106 void Message::CheckInitialized() const { | 111 void Message::CheckInitialized() const { |
| 107 GOOGLE_CHECK(IsInitialized()) | 112 GOOGLE_CHECK(IsInitialized()) |
| 108 << "Message of type \"" << GetDescriptor()->full_name() | 113 << "Message of type \"" << GetDescriptor()->full_name() |
| 109 << "\" is missing required fields: " << InitializationErrorString(); | 114 << "\" is missing required fields: " << InitializationErrorString(); |
| 110 } | 115 } |
| 111 | 116 |
| 112 void Message::DiscardUnknownFields() { | 117 void Message::DiscardUnknownFields() { |
| 113 return ReflectionOps::DiscardUnknownFields(this); | 118 return ReflectionOps::DiscardUnknownFields(this); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 142 io::CodedOutputStream* output) const { | 147 io::CodedOutputStream* output) const { |
| 143 WireFormat::SerializeWithCachedSizes(*this, GetCachedSize(), output); | 148 WireFormat::SerializeWithCachedSizes(*this, GetCachedSize(), output); |
| 144 } | 149 } |
| 145 | 150 |
| 146 int Message::ByteSize() const { | 151 int Message::ByteSize() const { |
| 147 int size = WireFormat::ByteSize(*this); | 152 int size = WireFormat::ByteSize(*this); |
| 148 SetCachedSize(size); | 153 SetCachedSize(size); |
| 149 return size; | 154 return size; |
| 150 } | 155 } |
| 151 | 156 |
| 152 void Message::SetCachedSize(int size) const { | 157 void Message::SetCachedSize(int /* size */) const { |
| 153 GOOGLE_LOG(FATAL) << "Message class \"" << GetDescriptor()->full_name() | 158 GOOGLE_LOG(FATAL) << "Message class \"" << GetDescriptor()->full_name() |
| 154 << "\" implements neither SetCachedSize() nor ByteSize(). " | 159 << "\" implements neither SetCachedSize() nor ByteSize(). " |
| 155 "Must implement one or the other."; | 160 "Must implement one or the other."; |
| 156 } | 161 } |
| 157 | 162 |
| 158 int Message::SpaceUsed() const { | 163 int Message::SpaceUsed() const { |
| 159 return GetReflection()->SpaceUsed(*this); | 164 return GetReflection()->SpaceUsed(*this); |
| 160 } | 165 } |
| 161 | 166 |
| 162 bool Message::SerializeToFileDescriptor(int file_descriptor) const { | 167 bool Message::SerializeToFileDescriptor(int file_descriptor) const { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 | 220 |
| 216 #undef HANDLE_TYPE | 221 #undef HANDLE_TYPE |
| 217 | 222 |
| 218 void* Reflection::MutableRawRepeatedString( | 223 void* Reflection::MutableRawRepeatedString( |
| 219 Message* message, const FieldDescriptor* field, bool is_string) const { | 224 Message* message, const FieldDescriptor* field, bool is_string) const { |
| 220 return MutableRawRepeatedField(message, field, | 225 return MutableRawRepeatedField(message, field, |
| 221 FieldDescriptor::CPPTYPE_STRING, FieldOptions::STRING, NULL); | 226 FieldDescriptor::CPPTYPE_STRING, FieldOptions::STRING, NULL); |
| 222 } | 227 } |
| 223 | 228 |
| 224 | 229 |
| 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( |
| 263 Message* message, |
| 264 const FieldDescriptor* field) const { |
| 265 GOOGLE_LOG(FATAL) << "Unimplemented Map Reflection API."; |
| 266 MapIterator iter(message, field); |
| 267 return iter; |
| 268 } |
| 269 |
| 270 MapIterator Reflection::MapEnd( |
| 271 Message* message, |
| 272 const FieldDescriptor* field) const { |
| 273 GOOGLE_LOG(FATAL) << "Unimplemented Map Reflection API."; |
| 274 MapIterator iter(message, field); |
| 275 return iter; |
| 276 } |
| 277 |
| 225 // ============================================================================= | 278 // ============================================================================= |
| 226 // MessageFactory | 279 // MessageFactory |
| 227 | 280 |
| 228 MessageFactory::~MessageFactory() {} | 281 MessageFactory::~MessageFactory() {} |
| 229 | 282 |
| 230 namespace { | 283 namespace { |
| 231 | 284 |
| 232 class GeneratedMessageFactory : public MessageFactory { | 285 class GeneratedMessageFactory : public MessageFactory { |
| 233 public: | 286 public: |
| 234 GeneratedMessageFactory(); | 287 GeneratedMessageFactory(); |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 347 GeneratedMessageFactory::singleton()->RegisterFile(filename, | 400 GeneratedMessageFactory::singleton()->RegisterFile(filename, |
| 348 register_messages); | 401 register_messages); |
| 349 } | 402 } |
| 350 | 403 |
| 351 void MessageFactory::InternalRegisterGeneratedMessage( | 404 void MessageFactory::InternalRegisterGeneratedMessage( |
| 352 const Descriptor* descriptor, const Message* prototype) { | 405 const Descriptor* descriptor, const Message* prototype) { |
| 353 GeneratedMessageFactory::singleton()->RegisterType(descriptor, prototype); | 406 GeneratedMessageFactory::singleton()->RegisterType(descriptor, prototype); |
| 354 } | 407 } |
| 355 | 408 |
| 356 | 409 |
| 410 MessageFactory* Reflection::GetMessageFactory() const { |
| 411 GOOGLE_LOG(FATAL) << "Not implemented."; |
| 412 return NULL; |
| 413 } |
| 414 |
| 415 void* Reflection::RepeatedFieldData( |
| 416 Message* message, const FieldDescriptor* field, |
| 417 FieldDescriptor::CppType cpp_type, |
| 418 const Descriptor* message_type) const { |
| 419 GOOGLE_LOG(FATAL) << "Not implemented."; |
| 420 return NULL; |
| 421 } |
| 422 |
| 423 namespace internal { |
| 424 RepeatedFieldAccessor::~RepeatedFieldAccessor() { |
| 425 } |
| 426 } // namespace internal |
| 427 |
| 428 const internal::RepeatedFieldAccessor* Reflection::RepeatedFieldAccessor( |
| 429 const FieldDescriptor* field) const { |
| 430 GOOGLE_CHECK(field->is_repeated()); |
| 431 switch (field->cpp_type()) { |
| 432 #define HANDLE_PRIMITIVE_TYPE(TYPE, type) \ |
| 433 case FieldDescriptor::CPPTYPE_ ## TYPE: \ |
| 434 return internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<type>
>::get(); |
| 435 HANDLE_PRIMITIVE_TYPE(INT32, int32) |
| 436 HANDLE_PRIMITIVE_TYPE(UINT32, uint32) |
| 437 HANDLE_PRIMITIVE_TYPE(INT64, int64) |
| 438 HANDLE_PRIMITIVE_TYPE(UINT64, uint64) |
| 439 HANDLE_PRIMITIVE_TYPE(FLOAT, float) |
| 440 HANDLE_PRIMITIVE_TYPE(DOUBLE, double) |
| 441 HANDLE_PRIMITIVE_TYPE(BOOL, bool) |
| 442 HANDLE_PRIMITIVE_TYPE(ENUM, int32) |
| 443 #undef HANDLE_PRIMITIVE_TYPE |
| 444 case FieldDescriptor::CPPTYPE_STRING: |
| 445 switch (field->options().ctype()) { |
| 446 default: |
| 447 case FieldOptions::STRING: |
| 448 return internal::Singleton<internal::RepeatedPtrFieldStringAccessor>::
get(); |
| 449 } |
| 450 break; |
| 451 case FieldDescriptor::CPPTYPE_MESSAGE: |
| 452 if (field->is_map()) { |
| 453 return internal::Singleton<internal::MapFieldAccessor>::get(); |
| 454 } else { |
| 455 return internal::Singleton<internal::RepeatedPtrFieldMessageAccessor>::g
et(); |
| 456 } |
| 457 } |
| 458 GOOGLE_LOG(FATAL) << "Should not reach here."; |
| 459 return NULL; |
| 460 } |
| 461 |
| 462 namespace internal { |
| 463 namespace { |
| 464 void ShutdownRepeatedFieldAccessor() { |
| 465 internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<int32> >::ShutDow
n(); |
| 466 internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<uint32> >::ShutDo
wn(); |
| 467 internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<int64> >::ShutDow
n(); |
| 468 internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<uint64> >::ShutDo
wn(); |
| 469 internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<float> >::ShutDow
n(); |
| 470 internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<double> >::ShutDo
wn(); |
| 471 internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<bool> >::ShutDown
(); |
| 472 internal::Singleton<internal::RepeatedPtrFieldStringAccessor>::ShutDown(); |
| 473 internal::Singleton<internal::RepeatedPtrFieldMessageAccessor>::ShutDown(); |
| 474 internal::Singleton<internal::MapFieldAccessor>::ShutDown(); |
| 475 } |
| 476 |
| 477 struct ShutdownRepeatedFieldRegister { |
| 478 ShutdownRepeatedFieldRegister() { |
| 479 OnShutdown(&ShutdownRepeatedFieldAccessor); |
| 480 } |
| 481 } shutdown_; |
| 482 |
| 483 } // namespace |
| 484 } // namespace internal |
| 485 |
| 486 namespace internal { |
| 487 template<> |
| 488 #if defined(_MSC_VER) && (_MSC_VER >= 1900) |
| 489 // Note: force noinline to workaround MSVC 2015 compiler bug, issue #240 |
| 490 GOOGLE_ATTRIBUTE_NOINLINE |
| 491 #endif |
| 492 Message* GenericTypeHandler<Message>::NewFromPrototype( |
| 493 const Message* prototype, google::protobuf::Arena* arena) { |
| 494 return prototype->New(arena); |
| 495 } |
| 496 template<> |
| 497 #if defined(_MSC_VER) && (_MSC_VER >= 1900) |
| 498 // Note: force noinline to workaround MSVC 2015 compiler bug, issue #240 |
| 499 GOOGLE_ATTRIBUTE_NOINLINE |
| 500 #endif |
| 501 google::protobuf::Arena* GenericTypeHandler<Message>::GetArena( |
| 502 Message* value) { |
| 503 return value->GetArena(); |
| 504 } |
| 505 template<> |
| 506 #if defined(_MSC_VER) && (_MSC_VER >= 1900) |
| 507 // Note: force noinline to workaround MSVC 2015 compiler bug, issue #240 |
| 508 GOOGLE_ATTRIBUTE_NOINLINE |
| 509 #endif |
| 510 void* GenericTypeHandler<Message>::GetMaybeArenaPointer( |
| 511 Message* value) { |
| 512 return value->GetMaybeArenaPointer(); |
| 513 } |
| 514 } // namespace internal |
| 515 |
| 357 } // namespace protobuf | 516 } // namespace protobuf |
| 358 } // namespace google | 517 } // namespace google |
| OLD | NEW |