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 26 matching lines...) Expand all Loading... |
37 | 37 |
38 #include <google/protobuf/stubs/logging.h> | 38 #include <google/protobuf/stubs/logging.h> |
39 #include <google/protobuf/stubs/common.h> | 39 #include <google/protobuf/stubs/common.h> |
40 #include <google/protobuf/descriptor.pb.h> | 40 #include <google/protobuf/descriptor.pb.h> |
41 #include <google/protobuf/descriptor.h> | 41 #include <google/protobuf/descriptor.h> |
42 #include <google/protobuf/extension_set.h> | 42 #include <google/protobuf/extension_set.h> |
43 #include <google/protobuf/generated_message_reflection.h> | 43 #include <google/protobuf/generated_message_reflection.h> |
44 #include <google/protobuf/generated_message_util.h> | 44 #include <google/protobuf/generated_message_util.h> |
45 #include <google/protobuf/map_field.h> | 45 #include <google/protobuf/map_field.h> |
46 #include <google/protobuf/repeated_field.h> | 46 #include <google/protobuf/repeated_field.h> |
| 47 // #include "google/protobuf/bridge/compatibility_mode_support.h" |
47 | 48 |
48 | 49 |
49 #define GOOGLE_PROTOBUF_HAS_ONEOF | 50 #define GOOGLE_PROTOBUF_HAS_ONEOF |
50 | 51 |
51 namespace google { | 52 namespace google { |
52 namespace protobuf { | 53 namespace protobuf { |
53 namespace internal { | 54 namespace internal { |
54 | 55 |
55 namespace { | 56 namespace { |
56 bool IsMapFieldInApi(const FieldDescriptor* field) { | 57 bool IsMapFieldInApi(const FieldDescriptor* field) { |
(...skipping 20 matching lines...) Expand all Loading... |
77 return descriptor->file()->options().cc_enable_arenas(); | 78 return descriptor->file()->options().cc_enable_arenas(); |
78 } | 79 } |
79 } // anonymous namespace | 80 } // anonymous namespace |
80 | 81 |
81 // =================================================================== | 82 // =================================================================== |
82 // Helpers for reporting usage errors (e.g. trying to use GetInt32() on | 83 // Helpers for reporting usage errors (e.g. trying to use GetInt32() on |
83 // a string field). | 84 // a string field). |
84 | 85 |
85 namespace { | 86 namespace { |
86 | 87 |
| 88 template <class To> |
| 89 To* GetPointerAtOffset(Message* message, uint32 offset) { |
| 90 return reinterpret_cast<To*>(reinterpret_cast<char*>(message) + offset); |
| 91 } |
| 92 |
| 93 template <class To> |
| 94 const To* GetConstPointerAtOffset(const Message* message, uint32 offset) { |
| 95 return reinterpret_cast<const To*>(reinterpret_cast<const char*>(message) + |
| 96 offset); |
| 97 } |
| 98 |
| 99 template <class To> |
| 100 const To& GetConstRefAtOffset(const Message& message, uint32 offset) { |
| 101 return *GetConstPointerAtOffset<To>(&message, offset); |
| 102 } |
| 103 |
87 void ReportReflectionUsageError( | 104 void ReportReflectionUsageError( |
88 const Descriptor* descriptor, const FieldDescriptor* field, | 105 const Descriptor* descriptor, const FieldDescriptor* field, |
89 const char* method, const char* description) { | 106 const char* method, const char* description) { |
90 GOOGLE_LOG(FATAL) | 107 GOOGLE_LOG(FATAL) |
91 << "Protocol Buffer reflection usage error:\n" | 108 << "Protocol Buffer reflection usage error:\n" |
92 " Method : google::protobuf::Reflection::" << method << "\n" | 109 " Method : google::protobuf::Reflection::" << method << "\n" |
93 " Message type: " << descriptor->full_name() << "\n" | 110 " Message type: " << descriptor->full_name() << "\n" |
94 " Field : " << field->full_name() << "\n" | 111 " Field : " << field->full_name() << "\n" |
95 " Problem : " << description; | 112 " Problem : " << description; |
96 } | 113 } |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 #define USAGE_CHECK_ALL(METHOD, LABEL, CPPTYPE) \ | 183 #define USAGE_CHECK_ALL(METHOD, LABEL, CPPTYPE) \ |
167 USAGE_CHECK_MESSAGE_TYPE(METHOD); \ | 184 USAGE_CHECK_MESSAGE_TYPE(METHOD); \ |
168 USAGE_CHECK_##LABEL(METHOD); \ | 185 USAGE_CHECK_##LABEL(METHOD); \ |
169 USAGE_CHECK_TYPE(METHOD, CPPTYPE) | 186 USAGE_CHECK_TYPE(METHOD, CPPTYPE) |
170 | 187 |
171 } // namespace | 188 } // namespace |
172 | 189 |
173 // =================================================================== | 190 // =================================================================== |
174 | 191 |
175 GeneratedMessageReflection::GeneratedMessageReflection( | 192 GeneratedMessageReflection::GeneratedMessageReflection( |
176 const Descriptor* descriptor, | 193 const Descriptor* descriptor, const ReflectionSchema& schema, |
177 const Message* default_instance, | 194 const DescriptorPool* pool, MessageFactory* factory) |
178 const int offsets[], | 195 : descriptor_(descriptor), |
179 int has_bits_offset, | 196 schema_(schema), |
180 int unknown_fields_offset, | 197 descriptor_pool_((pool == NULL) ? DescriptorPool::generated_pool() |
181 int extensions_offset, | 198 : pool), |
182 const DescriptorPool* descriptor_pool, | 199 message_factory_(factory), |
183 MessageFactory* factory, | 200 // TODO(haberman) remove this when upb is using our table driven. |
184 int object_size, | 201 default_instance_(schema_.default_instance_), |
185 int arena_offset, | 202 default_oneof_instance_(schema_.default_oneof_instance_), |
186 int is_default_instance_offset) | 203 offsets_(schema_.offsets_), |
187 : descriptor_ (descriptor), | 204 has_bits_indices_(schema_.has_bit_indices_), |
188 default_instance_ (default_instance), | 205 has_bits_offset_(schema_.has_bits_offset_), |
189 offsets_ (offsets), | 206 oneof_case_offset_(schema_.oneof_case_offset_), |
190 has_bits_offset_ (has_bits_offset), | 207 unknown_fields_offset_(-1), |
191 unknown_fields_offset_(unknown_fields_offset), | 208 extensions_offset_(schema_.extensions_offset_), |
192 extensions_offset_(extensions_offset), | 209 arena_offset_(schema_.metadata_offset_), |
193 arena_offset_ (arena_offset), | 210 object_size_(schema_.object_size_) { |
194 is_default_instance_offset_(is_default_instance_offset), | |
195 object_size_ (object_size), | |
196 descriptor_pool_ ((descriptor_pool == NULL) ? | |
197 DescriptorPool::generated_pool() : | |
198 descriptor_pool), | |
199 message_factory_ (factory) { | |
200 } | |
201 | |
202 GeneratedMessageReflection::GeneratedMessageReflection( | |
203 const Descriptor* descriptor, | |
204 const Message* default_instance, | |
205 const int offsets[], | |
206 int has_bits_offset, | |
207 int unknown_fields_offset, | |
208 int extensions_offset, | |
209 const void* default_oneof_instance, | |
210 int oneof_case_offset, | |
211 const DescriptorPool* descriptor_pool, | |
212 MessageFactory* factory, | |
213 int object_size, | |
214 int arena_offset, | |
215 int is_default_instance_offset) | |
216 : descriptor_ (descriptor), | |
217 default_instance_ (default_instance), | |
218 default_oneof_instance_ (default_oneof_instance), | |
219 offsets_ (offsets), | |
220 has_bits_offset_ (has_bits_offset), | |
221 oneof_case_offset_(oneof_case_offset), | |
222 unknown_fields_offset_(unknown_fields_offset), | |
223 extensions_offset_(extensions_offset), | |
224 arena_offset_ (arena_offset), | |
225 is_default_instance_offset_(is_default_instance_offset), | |
226 object_size_ (object_size), | |
227 descriptor_pool_ ((descriptor_pool == NULL) ? | |
228 DescriptorPool::generated_pool() : | |
229 descriptor_pool), | |
230 message_factory_ (factory) { | |
231 } | 211 } |
232 | 212 |
233 GeneratedMessageReflection::~GeneratedMessageReflection() {} | 213 GeneratedMessageReflection::~GeneratedMessageReflection() {} |
234 | 214 |
235 namespace { | 215 namespace { |
236 UnknownFieldSet* empty_unknown_field_set_ = NULL; | 216 UnknownFieldSet* empty_unknown_field_set_ = NULL; |
237 GOOGLE_PROTOBUF_DECLARE_ONCE(empty_unknown_field_set_once_); | 217 GOOGLE_PROTOBUF_DECLARE_ONCE(empty_unknown_field_set_once_); |
238 | 218 |
239 void DeleteEmptyUnknownFieldSet() { | 219 void DeleteEmptyUnknownFieldSet() { |
240 delete empty_unknown_field_set_; | 220 delete empty_unknown_field_set_; |
241 empty_unknown_field_set_ = NULL; | 221 empty_unknown_field_set_ = NULL; |
242 } | 222 } |
243 | 223 |
244 void InitEmptyUnknownFieldSet() { | 224 void InitEmptyUnknownFieldSet() { |
245 empty_unknown_field_set_ = new UnknownFieldSet; | 225 empty_unknown_field_set_ = new UnknownFieldSet; |
246 internal::OnShutdown(&DeleteEmptyUnknownFieldSet); | 226 internal::OnShutdown(&DeleteEmptyUnknownFieldSet); |
247 } | 227 } |
248 | 228 |
249 const UnknownFieldSet& GetEmptyUnknownFieldSet() { | 229 const UnknownFieldSet& GetEmptyUnknownFieldSet() { |
250 ::google::protobuf::GoogleOnceInit(&empty_unknown_field_set_once_, &InitEmptyU
nknownFieldSet); | 230 ::google::protobuf::GoogleOnceInit(&empty_unknown_field_set_once_, &InitEmptyU
nknownFieldSet); |
251 return *empty_unknown_field_set_; | 231 return *empty_unknown_field_set_; |
252 } | 232 } |
253 } // namespace | 233 } // namespace |
254 | 234 |
255 const UnknownFieldSet& GeneratedMessageReflection::GetUnknownFields( | 235 const UnknownFieldSet& GeneratedMessageReflection::GetUnknownFields( |
256 const Message& message) const { | 236 const Message& message) const { |
257 if (descriptor_->file()->syntax() == FileDescriptor::SYNTAX_PROTO3) { | 237 if (descriptor_->file()->syntax() == FileDescriptor::SYNTAX_PROTO3) { |
| 238 // We have to ensure that any mutations made to the return value of |
| 239 // MutableUnknownFields() are not reflected here. |
258 return GetEmptyUnknownFieldSet(); | 240 return GetEmptyUnknownFieldSet(); |
259 } | 241 } else { |
260 if (unknown_fields_offset_ == kUnknownFieldSetInMetadata) { | |
261 return GetInternalMetadataWithArena(message).unknown_fields(); | 242 return GetInternalMetadataWithArena(message).unknown_fields(); |
262 } | 243 } |
263 const void* ptr = reinterpret_cast<const uint8*>(&message) + | |
264 unknown_fields_offset_; | |
265 return *reinterpret_cast<const UnknownFieldSet*>(ptr); | |
266 } | 244 } |
267 | 245 |
268 UnknownFieldSet* GeneratedMessageReflection::MutableUnknownFields( | 246 UnknownFieldSet* GeneratedMessageReflection::MutableUnknownFields( |
269 Message* message) const { | 247 Message* message) const { |
270 if (unknown_fields_offset_ == kUnknownFieldSetInMetadata) { | 248 return MutableInternalMetadataWithArena(message)->mutable_unknown_fields(); |
271 return MutableInternalMetadataWithArena(message)-> | |
272 mutable_unknown_fields(); | |
273 } | |
274 void* ptr = reinterpret_cast<uint8*>(message) + unknown_fields_offset_; | |
275 return reinterpret_cast<UnknownFieldSet*>(ptr); | |
276 } | 249 } |
277 | 250 |
278 int GeneratedMessageReflection::SpaceUsed(const Message& message) const { | 251 int GeneratedMessageReflection::SpaceUsed(const Message& message) const { |
279 // object_size_ already includes the in-memory representation of each field | 252 // object_size_ already includes the in-memory representation of each field |
280 // in the message, so we only need to account for additional memory used by | 253 // in the message, so we only need to account for additional memory used by |
281 // the fields. | 254 // the fields. |
282 int total_size = object_size_; | 255 int total_size = schema_.GetObjectSize(); |
283 | 256 |
284 total_size += GetUnknownFields(message).SpaceUsedExcludingSelf(); | 257 total_size += GetUnknownFields(message).SpaceUsedExcludingSelf(); |
285 | 258 |
286 if (extensions_offset_ != -1) { | 259 if (schema_.HasExtensionSet()) { |
287 total_size += GetExtensionSet(message).SpaceUsedExcludingSelf(); | 260 total_size += GetExtensionSet(message).SpaceUsedExcludingSelf(); |
288 } | 261 } |
289 | 262 |
290 for (int i = 0; i < descriptor_->field_count(); i++) { | 263 const int field_count = descriptor_->field_count(); |
| 264 for (int i = 0; i < field_count; i++) { |
291 const FieldDescriptor* field = descriptor_->field(i); | 265 const FieldDescriptor* field = descriptor_->field(i); |
292 | 266 |
293 if (field->is_repeated()) { | 267 if (field->is_repeated()) { |
294 switch (field->cpp_type()) { | 268 switch (field->cpp_type()) { |
295 #define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ | 269 #define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ |
296 case FieldDescriptor::CPPTYPE_##UPPERCASE : \ | 270 case FieldDescriptor::CPPTYPE_##UPPERCASE : \ |
297 total_size += GetRaw<RepeatedField<LOWERCASE> >(message, field) \ | 271 total_size += GetRaw<RepeatedField<LOWERCASE> >(message, field) \ |
298 .SpaceUsedExcludingSelf(); \ | 272 .SpaceUsedExcludingSelf(); \ |
299 break | 273 break |
300 | 274 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 break; | 323 break; |
350 | 324 |
351 case FieldDescriptor::CPPTYPE_STRING: { | 325 case FieldDescriptor::CPPTYPE_STRING: { |
352 switch (field->options().ctype()) { | 326 switch (field->options().ctype()) { |
353 default: // TODO(kenton): Support other string reps. | 327 default: // TODO(kenton): Support other string reps. |
354 case FieldOptions::STRING: { | 328 case FieldOptions::STRING: { |
355 // Initially, the string points to the default value stored in | 329 // Initially, the string points to the default value stored in |
356 // the prototype. Only count the string if it has been changed | 330 // the prototype. Only count the string if it has been changed |
357 // from the default value. | 331 // from the default value. |
358 const string* default_ptr = | 332 const string* default_ptr = |
359 &DefaultRaw<ArenaStringPtr>(field).Get(NULL); | 333 &DefaultRaw<ArenaStringPtr>(field).Get(); |
360 const string* ptr = | 334 const string* ptr = |
361 &GetField<ArenaStringPtr>(message, field).Get(default_ptr); | 335 &GetField<ArenaStringPtr>(message, field).Get(); |
362 | 336 |
363 if (ptr != default_ptr) { | 337 if (ptr != default_ptr) { |
364 // string fields are represented by just a pointer, so also | 338 // string fields are represented by just a pointer, so also |
365 // include sizeof(string) as well. | 339 // include sizeof(string) as well. |
366 total_size += sizeof(*ptr) + StringSpaceUsedExcludingSelf(*ptr); | 340 total_size += sizeof(*ptr) + StringSpaceUsedExcludingSelf(*ptr); |
367 } | 341 } |
368 break; | 342 break; |
369 } | 343 } |
370 } | 344 } |
371 break; | 345 break; |
372 } | 346 } |
373 | 347 |
374 case FieldDescriptor::CPPTYPE_MESSAGE: | 348 case FieldDescriptor::CPPTYPE_MESSAGE: |
375 if (&message == default_instance_) { | 349 if (schema_.IsDefaultInstance(message)) { |
376 // For singular fields, the prototype just stores a pointer to the | 350 // For singular fields, the prototype just stores a pointer to the |
377 // external type's prototype, so there is no extra memory usage. | 351 // external type's prototype, so there is no extra memory usage. |
378 } else { | 352 } else { |
379 const Message* sub_message = GetRaw<const Message*>(message, field); | 353 const Message* sub_message = GetRaw<const Message*>(message, field); |
380 if (sub_message != NULL) { | 354 if (sub_message != NULL) { |
381 total_size += sub_message->SpaceUsed(); | 355 total_size += sub_message->SpaceUsed(); |
382 } | 356 } |
383 } | 357 } |
384 break; | 358 break; |
385 } | 359 } |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
487 Arena* arena1 = GetArena(message1); | 461 Arena* arena1 = GetArena(message1); |
488 Arena* arena2 = GetArena(message2); | 462 Arena* arena2 = GetArena(message2); |
489 ArenaStringPtr* string1 = | 463 ArenaStringPtr* string1 = |
490 MutableRaw<ArenaStringPtr>(message1, field); | 464 MutableRaw<ArenaStringPtr>(message1, field); |
491 ArenaStringPtr* string2 = | 465 ArenaStringPtr* string2 = |
492 MutableRaw<ArenaStringPtr>(message2, field); | 466 MutableRaw<ArenaStringPtr>(message2, field); |
493 if (arena1 == arena2) { | 467 if (arena1 == arena2) { |
494 string1->Swap(string2); | 468 string1->Swap(string2); |
495 } else { | 469 } else { |
496 const string* default_ptr = | 470 const string* default_ptr = |
497 &DefaultRaw<ArenaStringPtr>(field).Get(NULL); | 471 &DefaultRaw<ArenaStringPtr>(field).Get(); |
498 const string temp = string1->Get(default_ptr); | 472 const string temp = string1->Get(); |
499 string1->Set(default_ptr, string2->Get(default_ptr), arena1); | 473 string1->Set(default_ptr, string2->Get(), arena1); |
500 string2->Set(default_ptr, temp, arena2); | 474 string2->Set(default_ptr, temp, arena2); |
501 } | 475 } |
502 } | 476 } |
503 break; | 477 break; |
504 } | 478 } |
505 break; | 479 break; |
506 | 480 |
507 default: | 481 default: |
508 GOOGLE_LOG(FATAL) << "Unimplemented type: " << field->cpp_type(); | 482 GOOGLE_LOG(FATAL) << "Unimplemented type: " << field->cpp_type(); |
509 } | 483 } |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
650 << descriptor_->full_name() | 624 << descriptor_->full_name() |
651 << "\"). Note that the exact same class is required; not just the same " | 625 << "\"). Note that the exact same class is required; not just the same " |
652 "descriptor."; | 626 "descriptor."; |
653 | 627 |
654 // Check that both messages are in the same arena (or both on the heap). We | 628 // Check that both messages are in the same arena (or both on the heap). We |
655 // need to copy all data if not, due to ownership semantics. | 629 // need to copy all data if not, due to ownership semantics. |
656 if (GetArena(message1) != GetArena(message2)) { | 630 if (GetArena(message1) != GetArena(message2)) { |
657 // Slow copy path. | 631 // Slow copy path. |
658 // Use our arena as temp space, if available. | 632 // Use our arena as temp space, if available. |
659 Message* temp = message1->New(GetArena(message1)); | 633 Message* temp = message1->New(GetArena(message1)); |
660 temp->MergeFrom(*message1); | 634 temp->MergeFrom(*message2); |
661 message1->CopyFrom(*message2); | 635 message2->CopyFrom(*message1); |
662 message2->CopyFrom(*temp); | 636 Swap(message1, temp); |
663 if (GetArena(message1) == NULL) { | 637 if (GetArena(message1) == NULL) { |
664 delete temp; | 638 delete temp; |
665 } | 639 } |
666 return; | 640 return; |
667 } | 641 } |
668 | 642 |
669 if (has_bits_offset_ != -1) { | 643 if (schema_.HasHasbits()) { |
670 uint32* has_bits1 = MutableHasBits(message1); | 644 uint32* has_bits1 = MutableHasBits(message1); |
671 uint32* has_bits2 = MutableHasBits(message2); | 645 uint32* has_bits2 = MutableHasBits(message2); |
672 int has_bits_size = (descriptor_->field_count() + 31) / 32; | 646 int has_bits_size = (descriptor_->field_count() + 31) / 32; |
673 | 647 |
674 for (int i = 0; i < has_bits_size; i++) { | 648 for (int i = 0; i < has_bits_size; i++) { |
675 std::swap(has_bits1[i], has_bits2[i]); | 649 std::swap(has_bits1[i], has_bits2[i]); |
676 } | 650 } |
677 } | 651 } |
678 | 652 |
679 for (int i = 0; i < descriptor_->field_count(); i++) { | 653 const int field_count = descriptor_->field_count(); |
| 654 for (int i = 0; i < field_count; i++) { |
680 const FieldDescriptor* field = descriptor_->field(i); | 655 const FieldDescriptor* field = descriptor_->field(i); |
681 if (!field->containing_oneof()) { | 656 if (!field->containing_oneof()) { |
682 SwapField(message1, message2, field); | 657 SwapField(message1, message2, field); |
683 } | 658 } |
684 } | 659 } |
685 | 660 |
686 for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { | 661 const int oneof_decl_count = descriptor_->oneof_decl_count(); |
| 662 for (int i = 0; i < oneof_decl_count; i++) { |
687 SwapOneofField(message1, message2, descriptor_->oneof_decl(i)); | 663 SwapOneofField(message1, message2, descriptor_->oneof_decl(i)); |
688 } | 664 } |
689 | 665 |
690 if (extensions_offset_ != -1) { | 666 if (schema_.HasExtensionSet()) { |
691 MutableExtensionSet(message1)->Swap(MutableExtensionSet(message2)); | 667 MutableExtensionSet(message1)->Swap(MutableExtensionSet(message2)); |
692 } | 668 } |
693 | 669 |
694 MutableUnknownFields(message1)->Swap(MutableUnknownFields(message2)); | 670 MutableUnknownFields(message1)->Swap(MutableUnknownFields(message2)); |
695 } | 671 } |
696 | 672 |
697 void GeneratedMessageReflection::SwapFields( | 673 void GeneratedMessageReflection::SwapFields( |
698 Message* message1, | 674 Message* message1, |
699 Message* message2, | 675 Message* message2, |
700 const vector<const FieldDescriptor*>& fields) const { | 676 const std::vector<const FieldDescriptor*>& fields) const { |
701 if (message1 == message2) return; | 677 if (message1 == message2) return; |
702 | 678 |
703 // TODO(kenton): Other Reflection methods should probably check this too. | 679 // TODO(kenton): Other Reflection methods should probably check this too. |
704 GOOGLE_CHECK_EQ(message1->GetReflection(), this) | 680 GOOGLE_CHECK_EQ(message1->GetReflection(), this) |
705 << "First argument to SwapFields() (of type \"" | 681 << "First argument to SwapFields() (of type \"" |
706 << message1->GetDescriptor()->full_name() | 682 << message1->GetDescriptor()->full_name() |
707 << "\") is not compatible with this reflection object (which is for type \"" | 683 << "\") is not compatible with this reflection object (which is for type \"" |
708 << descriptor_->full_name() | 684 << descriptor_->full_name() |
709 << "\"). Note that the exact same class is required; not just the same " | 685 << "\"). Note that the exact same class is required; not just the same " |
710 "descriptor."; | 686 "descriptor."; |
711 GOOGLE_CHECK_EQ(message2->GetReflection(), this) | 687 GOOGLE_CHECK_EQ(message2->GetReflection(), this) |
712 << "Second argument to SwapFields() (of type \"" | 688 << "Second argument to SwapFields() (of type \"" |
713 << message2->GetDescriptor()->full_name() | 689 << message2->GetDescriptor()->full_name() |
714 << "\") is not compatible with this reflection object (which is for type \"" | 690 << "\") is not compatible with this reflection object (which is for type \"" |
715 << descriptor_->full_name() | 691 << descriptor_->full_name() |
716 << "\"). Note that the exact same class is required; not just the same " | 692 << "\"). Note that the exact same class is required; not just the same " |
717 "descriptor."; | 693 "descriptor."; |
718 | 694 |
719 std::set<int> swapped_oneof; | 695 std::set<int> swapped_oneof; |
720 | 696 |
721 for (int i = 0; i < fields.size(); i++) { | 697 const int fields_size = static_cast<int>(fields.size()); |
| 698 for (int i = 0; i < fields_size; i++) { |
722 const FieldDescriptor* field = fields[i]; | 699 const FieldDescriptor* field = fields[i]; |
723 if (field->is_extension()) { | 700 if (field->is_extension()) { |
724 MutableExtensionSet(message1)->SwapExtension( | 701 MutableExtensionSet(message1)->SwapExtension( |
725 MutableExtensionSet(message2), | 702 MutableExtensionSet(message2), |
726 field->number()); | 703 field->number()); |
727 } else { | 704 } else { |
728 if (field->containing_oneof()) { | 705 if (field->containing_oneof()) { |
729 int oneof_index = field->containing_oneof()->index(); | 706 int oneof_index = field->containing_oneof()->index(); |
730 // Only swap the oneof field once. | 707 // Only swap the oneof field once. |
731 if (swapped_oneof.find(oneof_index) != swapped_oneof.end()) { | 708 if (swapped_oneof.find(oneof_index) != swapped_oneof.end()) { |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
833 case FieldDescriptor::CPPTYPE_ENUM: | 810 case FieldDescriptor::CPPTYPE_ENUM: |
834 *MutableRaw<int>(message, field) = | 811 *MutableRaw<int>(message, field) = |
835 field->default_value_enum()->number(); | 812 field->default_value_enum()->number(); |
836 break; | 813 break; |
837 | 814 |
838 case FieldDescriptor::CPPTYPE_STRING: { | 815 case FieldDescriptor::CPPTYPE_STRING: { |
839 switch (field->options().ctype()) { | 816 switch (field->options().ctype()) { |
840 default: // TODO(kenton): Support other string reps. | 817 default: // TODO(kenton): Support other string reps. |
841 case FieldOptions::STRING: { | 818 case FieldOptions::STRING: { |
842 const string* default_ptr = | 819 const string* default_ptr = |
843 &DefaultRaw<ArenaStringPtr>(field).Get(NULL); | 820 &DefaultRaw<ArenaStringPtr>(field).Get(); |
844 MutableRaw<ArenaStringPtr>(message, field)->Destroy(default_ptr, | 821 MutableRaw<ArenaStringPtr>(message, field)->SetAllocated( |
845 GetArena(message)); | 822 default_ptr, NULL, GetArena(message)); |
846 break; | 823 break; |
847 } | 824 } |
848 } | 825 } |
849 break; | 826 break; |
850 } | 827 } |
851 | 828 |
852 case FieldDescriptor::CPPTYPE_MESSAGE: | 829 case FieldDescriptor::CPPTYPE_MESSAGE: |
853 if (has_bits_offset_ == -1) { | 830 if (!schema_.HasHasbits()) { |
854 // Proto3 does not have has-bits and we need to set a message field | 831 // Proto3 does not have has-bits and we need to set a message field |
855 // to NULL in order to indicate its un-presence. | 832 // to NULL in order to indicate its un-presence. |
856 if (GetArena(message) == NULL) { | 833 if (GetArena(message) == NULL) { |
857 delete *MutableRaw<Message*>(message, field); | 834 delete *MutableRaw<Message*>(message, field); |
858 } | 835 } |
859 *MutableRaw<Message*>(message, field) = NULL; | 836 *MutableRaw<Message*>(message, field) = NULL; |
860 } else { | 837 } else { |
861 (*MutableRaw<Message*>(message, field))->Clear(); | 838 (*MutableRaw<Message*>(message, field))->Clear(); |
862 } | 839 } |
863 break; | 840 break; |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1019 } | 996 } |
1020 | 997 |
1021 namespace { | 998 namespace { |
1022 // Comparison functor for sorting FieldDescriptors by field number. | 999 // Comparison functor for sorting FieldDescriptors by field number. |
1023 struct FieldNumberSorter { | 1000 struct FieldNumberSorter { |
1024 bool operator()(const FieldDescriptor* left, | 1001 bool operator()(const FieldDescriptor* left, |
1025 const FieldDescriptor* right) const { | 1002 const FieldDescriptor* right) const { |
1026 return left->number() < right->number(); | 1003 return left->number() < right->number(); |
1027 } | 1004 } |
1028 }; | 1005 }; |
| 1006 |
| 1007 inline bool IsIndexInHasBitSet( |
| 1008 const uint32* has_bit_set, uint32 has_bit_index) { |
| 1009 return ((has_bit_set[has_bit_index / 32] >> (has_bit_index % 32)) & |
| 1010 static_cast<uint32>(1)) != 0; |
| 1011 } |
1029 } // namespace | 1012 } // namespace |
1030 | 1013 |
1031 void GeneratedMessageReflection::ListFields( | 1014 void GeneratedMessageReflection::ListFields( |
1032 const Message& message, | 1015 const Message& message, |
1033 vector<const FieldDescriptor*>* output) const { | 1016 std::vector<const FieldDescriptor*>* output) const { |
1034 output->clear(); | 1017 output->clear(); |
1035 | 1018 |
1036 // Optimization: The default instance never has any fields set. | 1019 // Optimization: The default instance never has any fields set. |
1037 if (&message == default_instance_) return; | 1020 if (schema_.IsDefaultInstance(message)) return; |
1038 | 1021 |
1039 output->reserve(descriptor_->field_count()); | 1022 // Optimization: Avoid calling GetHasBits() and HasOneofField() many times |
1040 for (int i = 0; i < descriptor_->field_count(); i++) { | 1023 // within the field loop. We allow this violation of ReflectionSchema |
| 1024 // encapsulation because this function takes a noticable about of CPU |
| 1025 // fleetwide and properly allowing this optimization through public interfaces |
| 1026 // seems more trouble than it is worth. |
| 1027 const uint32* const has_bits = |
| 1028 schema_.HasHasbits() ? GetHasBits(message) : NULL; |
| 1029 const uint32* const has_bits_indices = schema_.has_bit_indices_; |
| 1030 const uint32* const oneof_case_array = |
| 1031 &GetConstRefAtOffset<uint32>(message, schema_.oneof_case_offset_); |
| 1032 |
| 1033 const int field_count = descriptor_->field_count(); |
| 1034 output->reserve(field_count); |
| 1035 for (int i = 0; i < field_count; i++) { |
1041 const FieldDescriptor* field = descriptor_->field(i); | 1036 const FieldDescriptor* field = descriptor_->field(i); |
1042 if (field->is_repeated()) { | 1037 if (field->is_repeated()) { |
1043 if (FieldSize(message, field) > 0) { | 1038 if (FieldSize(message, field) > 0) { |
1044 output->push_back(field); | 1039 output->push_back(field); |
1045 } | 1040 } |
1046 } else { | 1041 } else { |
1047 if (field->containing_oneof()) { | 1042 const OneofDescriptor* containing_oneof = field->containing_oneof(); |
1048 if (HasOneofField(message, field)) { | 1043 if (containing_oneof) { |
| 1044 // Equivalent to: HasOneofField(message, field) |
| 1045 if (oneof_case_array[containing_oneof->index()] == field->number()) { |
1049 output->push_back(field); | 1046 output->push_back(field); |
1050 } | 1047 } |
1051 } else if (HasBit(message, field)) { | 1048 } else if (has_bits) { |
| 1049 // Equivalent to: HasBit(message, field) |
| 1050 if (IsIndexInHasBitSet(has_bits, has_bits_indices[i])) { |
| 1051 output->push_back(field); |
| 1052 } |
| 1053 } else if (HasBit(message, field)) { // Fall back on proto3-style HasBit. |
1052 output->push_back(field); | 1054 output->push_back(field); |
1053 } | 1055 } |
1054 } | 1056 } |
1055 } | 1057 } |
1056 | 1058 |
1057 if (extensions_offset_ != -1) { | 1059 if (schema_.HasExtensionSet()) { |
1058 GetExtensionSet(message).AppendToList(descriptor_, descriptor_pool_, | 1060 GetExtensionSet(message).AppendToList(descriptor_, descriptor_pool_, |
1059 output); | 1061 output); |
1060 } | 1062 } |
1061 | 1063 |
1062 // ListFields() must sort output by field number. | 1064 // ListFields() must sort output by field number. |
1063 std::sort(output->begin(), output->end(), FieldNumberSorter()); | 1065 std::sort(output->begin(), output->end(), FieldNumberSorter()); |
1064 } | 1066 } |
1065 | 1067 |
1066 // ------------------------------------------------------------------- | 1068 // ------------------------------------------------------------------- |
1067 | 1069 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1141 string GeneratedMessageReflection::GetString( | 1143 string GeneratedMessageReflection::GetString( |
1142 const Message& message, const FieldDescriptor* field) const { | 1144 const Message& message, const FieldDescriptor* field) const { |
1143 USAGE_CHECK_ALL(GetString, SINGULAR, STRING); | 1145 USAGE_CHECK_ALL(GetString, SINGULAR, STRING); |
1144 if (field->is_extension()) { | 1146 if (field->is_extension()) { |
1145 return GetExtensionSet(message).GetString(field->number(), | 1147 return GetExtensionSet(message).GetString(field->number(), |
1146 field->default_value_string()); | 1148 field->default_value_string()); |
1147 } else { | 1149 } else { |
1148 switch (field->options().ctype()) { | 1150 switch (field->options().ctype()) { |
1149 default: // TODO(kenton): Support other string reps. | 1151 default: // TODO(kenton): Support other string reps. |
1150 case FieldOptions::STRING: { | 1152 case FieldOptions::STRING: { |
1151 const string* default_ptr = | 1153 return GetField<ArenaStringPtr>(message, field).Get(); |
1152 &DefaultRaw<ArenaStringPtr>(field).Get(NULL); | |
1153 return GetField<ArenaStringPtr>(message, field).Get(default_ptr); | |
1154 } | 1154 } |
1155 } | 1155 } |
1156 | 1156 |
1157 GOOGLE_LOG(FATAL) << "Can't get here."; | 1157 GOOGLE_LOG(FATAL) << "Can't get here."; |
1158 return GetEmptyString(); // Make compiler happy. | 1158 return GetEmptyString(); // Make compiler happy. |
1159 } | 1159 } |
1160 } | 1160 } |
1161 | 1161 |
1162 const string& GeneratedMessageReflection::GetStringReference( | 1162 const string& GeneratedMessageReflection::GetStringReference( |
1163 const Message& message, | 1163 const Message& message, |
1164 const FieldDescriptor* field, string* scratch) const { | 1164 const FieldDescriptor* field, string* scratch) const { |
1165 USAGE_CHECK_ALL(GetStringReference, SINGULAR, STRING); | 1165 USAGE_CHECK_ALL(GetStringReference, SINGULAR, STRING); |
1166 if (field->is_extension()) { | 1166 if (field->is_extension()) { |
1167 return GetExtensionSet(message).GetString(field->number(), | 1167 return GetExtensionSet(message).GetString(field->number(), |
1168 field->default_value_string()); | 1168 field->default_value_string()); |
1169 } else { | 1169 } else { |
1170 switch (field->options().ctype()) { | 1170 switch (field->options().ctype()) { |
1171 default: // TODO(kenton): Support other string reps. | 1171 default: // TODO(kenton): Support other string reps. |
1172 case FieldOptions::STRING: { | 1172 case FieldOptions::STRING: { |
1173 const string* default_ptr = | 1173 return GetField<ArenaStringPtr>(message, field).Get(); |
1174 &DefaultRaw<ArenaStringPtr>(field).Get(NULL); | |
1175 return GetField<ArenaStringPtr>(message, field).Get(default_ptr); | |
1176 } | 1174 } |
1177 } | 1175 } |
1178 | 1176 |
1179 GOOGLE_LOG(FATAL) << "Can't get here."; | 1177 GOOGLE_LOG(FATAL) << "Can't get here."; |
1180 return GetEmptyString(); // Make compiler happy. | 1178 return GetEmptyString(); // Make compiler happy. |
1181 } | 1179 } |
1182 } | 1180 } |
1183 | 1181 |
1184 | 1182 |
1185 void GeneratedMessageReflection::SetString( | 1183 void GeneratedMessageReflection::SetString( |
1186 Message* message, const FieldDescriptor* field, | 1184 Message* message, const FieldDescriptor* field, |
1187 const string& value) const { | 1185 const string& value) const { |
1188 USAGE_CHECK_ALL(SetString, SINGULAR, STRING); | 1186 USAGE_CHECK_ALL(SetString, SINGULAR, STRING); |
1189 if (field->is_extension()) { | 1187 if (field->is_extension()) { |
1190 return MutableExtensionSet(message)->SetString(field->number(), | 1188 return MutableExtensionSet(message)->SetString(field->number(), |
1191 field->type(), value, field); | 1189 field->type(), value, field); |
1192 } else { | 1190 } else { |
1193 switch (field->options().ctype()) { | 1191 switch (field->options().ctype()) { |
1194 default: // TODO(kenton): Support other string reps. | 1192 default: // TODO(kenton): Support other string reps. |
1195 case FieldOptions::STRING: { | 1193 case FieldOptions::STRING: { |
1196 const string* default_ptr = | 1194 const string* default_ptr = &DefaultRaw<ArenaStringPtr>(field).Get(); |
1197 &DefaultRaw<ArenaStringPtr>(field).Get(NULL); | |
1198 if (field->containing_oneof() && !HasOneofField(*message, field)) { | 1195 if (field->containing_oneof() && !HasOneofField(*message, field)) { |
1199 ClearOneof(message, field->containing_oneof()); | 1196 ClearOneof(message, field->containing_oneof()); |
1200 MutableField<ArenaStringPtr>(message, field)->UnsafeSetDefault( | 1197 MutableField<ArenaStringPtr>(message, field)->UnsafeSetDefault( |
1201 default_ptr); | 1198 default_ptr); |
1202 } | 1199 } |
1203 MutableField<ArenaStringPtr>(message, field)->Set(default_ptr, | 1200 MutableField<ArenaStringPtr>(message, field)->Set(default_ptr, |
1204 value, GetArena(message)); | 1201 value, GetArena(message)); |
1205 break; | 1202 break; |
1206 } | 1203 } |
1207 } | 1204 } |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1572 Message* GeneratedMessageReflection::UnsafeArenaReleaseMessage( | 1569 Message* GeneratedMessageReflection::UnsafeArenaReleaseMessage( |
1573 Message* message, | 1570 Message* message, |
1574 const FieldDescriptor* field, | 1571 const FieldDescriptor* field, |
1575 MessageFactory* factory) const { | 1572 MessageFactory* factory) const { |
1576 USAGE_CHECK_ALL(ReleaseMessage, SINGULAR, MESSAGE); | 1573 USAGE_CHECK_ALL(ReleaseMessage, SINGULAR, MESSAGE); |
1577 | 1574 |
1578 if (factory == NULL) factory = message_factory_; | 1575 if (factory == NULL) factory = message_factory_; |
1579 | 1576 |
1580 if (field->is_extension()) { | 1577 if (field->is_extension()) { |
1581 return static_cast<Message*>( | 1578 return static_cast<Message*>( |
1582 MutableExtensionSet(message)->ReleaseMessage(field, factory)); | 1579 MutableExtensionSet(message)->UnsafeArenaReleaseMessage(field, |
| 1580 factory)); |
1583 } else { | 1581 } else { |
1584 ClearBit(message, field); | 1582 ClearBit(message, field); |
1585 if (field->containing_oneof()) { | 1583 if (field->containing_oneof()) { |
1586 if (HasOneofField(*message, field)) { | 1584 if (HasOneofField(*message, field)) { |
1587 *MutableOneofCase(message, field->containing_oneof()) = 0; | 1585 *MutableOneofCase(message, field->containing_oneof()) = 0; |
1588 } else { | 1586 } else { |
1589 return NULL; | 1587 return NULL; |
1590 } | 1588 } |
1591 } | 1589 } |
1592 Message** result = MutableRaw<Message*>(message, field); | 1590 Message** result = MutableRaw<Message*>(message, field); |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1720 if (ctype >= 0) | 1718 if (ctype >= 0) |
1721 GOOGLE_CHECK_EQ(field->options().ctype(), ctype) << "subtype mismatch"; | 1719 GOOGLE_CHECK_EQ(field->options().ctype(), ctype) << "subtype mismatch"; |
1722 if (desc != NULL) | 1720 if (desc != NULL) |
1723 GOOGLE_CHECK_EQ(field->message_type(), desc) << "wrong submessage type"; | 1721 GOOGLE_CHECK_EQ(field->message_type(), desc) << "wrong submessage type"; |
1724 if (field->is_extension()) { | 1722 if (field->is_extension()) { |
1725 return MutableExtensionSet(message)->MutableRawRepeatedField( | 1723 return MutableExtensionSet(message)->MutableRawRepeatedField( |
1726 field->number(), field->type(), field->is_packed(), field); | 1724 field->number(), field->type(), field->is_packed(), field); |
1727 } else { | 1725 } else { |
1728 // Trigger transform for MapField | 1726 // Trigger transform for MapField |
1729 if (IsMapFieldInApi(field)) { | 1727 if (IsMapFieldInApi(field)) { |
1730 return reinterpret_cast<MapFieldBase*>(reinterpret_cast<uint8*>(message) + | 1728 return MutableRawNonOneof<MapFieldBase>(message, field) |
1731 offsets_[field->index()]) | |
1732 ->MutableRepeatedField(); | 1729 ->MutableRepeatedField(); |
1733 } | 1730 } |
1734 return reinterpret_cast<uint8*>(message) + offsets_[field->index()]; | 1731 return MutableRawNonOneof<void>(message, field); |
1735 } | 1732 } |
1736 } | 1733 } |
1737 | 1734 |
1738 const void* GeneratedMessageReflection::GetRawRepeatedField( | 1735 const void* GeneratedMessageReflection::GetRawRepeatedField( |
1739 const Message& message, const FieldDescriptor* field, | 1736 const Message& message, const FieldDescriptor* field, |
1740 FieldDescriptor::CppType cpptype, | 1737 FieldDescriptor::CppType cpptype, |
1741 int ctype, const Descriptor* desc) const { | 1738 int ctype, const Descriptor* desc) const { |
1742 USAGE_CHECK_REPEATED("GetRawRepeatedField"); | 1739 USAGE_CHECK_REPEATED("GetRawRepeatedField"); |
1743 if (field->cpp_type() != cpptype) | 1740 if (field->cpp_type() != cpptype) |
1744 ReportReflectionUsageTypeError(descriptor_, | 1741 ReportReflectionUsageTypeError(descriptor_, |
1745 field, "GetRawRepeatedField", cpptype); | 1742 field, "GetRawRepeatedField", cpptype); |
1746 if (ctype >= 0) | 1743 if (ctype >= 0) |
1747 GOOGLE_CHECK_EQ(field->options().ctype(), ctype) << "subtype mismatch"; | 1744 GOOGLE_CHECK_EQ(field->options().ctype(), ctype) << "subtype mismatch"; |
1748 if (desc != NULL) | 1745 if (desc != NULL) |
1749 GOOGLE_CHECK_EQ(field->message_type(), desc) << "wrong submessage type"; | 1746 GOOGLE_CHECK_EQ(field->message_type(), desc) << "wrong submessage type"; |
1750 if (field->is_extension()) { | 1747 if (field->is_extension()) { |
1751 // Should use extension_set::GetRawRepeatedField. However, the required | 1748 // Should use extension_set::GetRawRepeatedField. However, the required |
1752 // parameter "default repeated value" is not very easy to get here. | 1749 // parameter "default repeated value" is not very easy to get here. |
1753 // Map is not supported in extensions, it is acceptable to use | 1750 // Map is not supported in extensions, it is acceptable to use |
1754 // extension_set::MutableRawRepeatedField which does not change the message. | 1751 // extension_set::MutableRawRepeatedField which does not change the message. |
1755 return MutableExtensionSet(const_cast<Message*>(&message)) | 1752 return MutableExtensionSet(const_cast<Message*>(&message)) |
1756 ->MutableRawRepeatedField( | 1753 ->MutableRawRepeatedField( |
1757 field->number(), field->type(), field->is_packed(), field); | 1754 field->number(), field->type(), field->is_packed(), field); |
1758 } else { | 1755 } else { |
1759 // Trigger transform for MapField | 1756 // Trigger transform for MapField |
1760 if (IsMapFieldInApi(field)) { | 1757 if (IsMapFieldInApi(field)) { |
1761 return &(reinterpret_cast<const MapFieldBase*>( | 1758 return &(GetRawNonOneof<MapFieldBase>(message, field).GetRepeatedField()); |
1762 reinterpret_cast<const uint8*>(&message) + | |
1763 offsets_[field->index()])->GetRepeatedField()); | |
1764 } | 1759 } |
1765 return reinterpret_cast<const uint8*>(&message) + offsets_[field->index()]; | 1760 return &GetRawNonOneof<char>(message, field); |
1766 } | 1761 } |
1767 } | 1762 } |
1768 | 1763 |
1769 const FieldDescriptor* GeneratedMessageReflection::GetOneofFieldDescriptor( | 1764 const FieldDescriptor* GeneratedMessageReflection::GetOneofFieldDescriptor( |
1770 const Message& message, | 1765 const Message& message, |
1771 const OneofDescriptor* oneof_descriptor) const { | 1766 const OneofDescriptor* oneof_descriptor) const { |
1772 uint32 field_number = GetOneofCase(message, oneof_descriptor); | 1767 uint32 field_number = GetOneofCase(message, oneof_descriptor); |
1773 if (field_number == 0) { | 1768 if (field_number == 0) { |
1774 return NULL; | 1769 return NULL; |
1775 } | 1770 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1837 USAGE_CHECK(IsMapFieldInApi(field), | 1832 USAGE_CHECK(IsMapFieldInApi(field), |
1838 "MapSize", | 1833 "MapSize", |
1839 "Field is not a map field."); | 1834 "Field is not a map field."); |
1840 return GetRaw<MapFieldBase>(message, field).size(); | 1835 return GetRaw<MapFieldBase>(message, field).size(); |
1841 } | 1836 } |
1842 | 1837 |
1843 // ----------------------------------------------------------------------------- | 1838 // ----------------------------------------------------------------------------- |
1844 | 1839 |
1845 const FieldDescriptor* GeneratedMessageReflection::FindKnownExtensionByName( | 1840 const FieldDescriptor* GeneratedMessageReflection::FindKnownExtensionByName( |
1846 const string& name) const { | 1841 const string& name) const { |
1847 if (extensions_offset_ == -1) return NULL; | 1842 if (!schema_.HasExtensionSet()) return NULL; |
1848 | 1843 |
1849 const FieldDescriptor* result = descriptor_pool_->FindExtensionByName(name); | 1844 const FieldDescriptor* result = descriptor_pool_->FindExtensionByName(name); |
1850 if (result != NULL && result->containing_type() == descriptor_) { | 1845 if (result != NULL && result->containing_type() == descriptor_) { |
1851 return result; | 1846 return result; |
1852 } | 1847 } |
1853 | 1848 |
1854 if (descriptor_->options().message_set_wire_format()) { | 1849 if (descriptor_->options().message_set_wire_format()) { |
1855 // MessageSet extensions may be identified by type name. | 1850 // MessageSet extensions may be identified by type name. |
1856 const Descriptor* type = descriptor_pool_->FindMessageTypeByName(name); | 1851 const Descriptor* type = descriptor_pool_->FindMessageTypeByName(name); |
1857 if (type != NULL) { | 1852 if (type != NULL) { |
1858 // Look for a matching extension in the foreign type's scope. | 1853 // Look for a matching extension in the foreign type's scope. |
1859 for (int i = 0; i < type->extension_count(); i++) { | 1854 const int type_extension_count = type->extension_count(); |
| 1855 for (int i = 0; i < type_extension_count; i++) { |
1860 const FieldDescriptor* extension = type->extension(i); | 1856 const FieldDescriptor* extension = type->extension(i); |
1861 if (extension->containing_type() == descriptor_ && | 1857 if (extension->containing_type() == descriptor_ && |
1862 extension->type() == FieldDescriptor::TYPE_MESSAGE && | 1858 extension->type() == FieldDescriptor::TYPE_MESSAGE && |
1863 extension->is_optional() && | 1859 extension->is_optional() && |
1864 extension->message_type() == type) { | 1860 extension->message_type() == type) { |
1865 // Found it. | 1861 // Found it. |
1866 return extension; | 1862 return extension; |
1867 } | 1863 } |
1868 } | 1864 } |
1869 } | 1865 } |
1870 } | 1866 } |
1871 | 1867 |
1872 return NULL; | 1868 return NULL; |
1873 } | 1869 } |
1874 | 1870 |
1875 const FieldDescriptor* GeneratedMessageReflection::FindKnownExtensionByNumber( | 1871 const FieldDescriptor* GeneratedMessageReflection::FindKnownExtensionByNumber( |
1876 int number) const { | 1872 int number) const { |
1877 if (extensions_offset_ == -1) return NULL; | 1873 if (!schema_.HasExtensionSet()) return NULL; |
1878 return descriptor_pool_->FindExtensionByNumber(descriptor_, number); | 1874 return descriptor_pool_->FindExtensionByNumber(descriptor_, number); |
1879 } | 1875 } |
1880 | 1876 |
1881 bool GeneratedMessageReflection::SupportsUnknownEnumValues() const { | 1877 bool GeneratedMessageReflection::SupportsUnknownEnumValues() const { |
1882 return CreateUnknownEnumValues(descriptor_->file()); | 1878 return CreateUnknownEnumValues(descriptor_->file()); |
1883 } | 1879 } |
1884 | 1880 |
1885 // =================================================================== | 1881 // =================================================================== |
1886 // Some private helpers. | 1882 // Some private helpers. |
1887 | 1883 |
1888 // These simple template accessors obtain pointers (or references) to | 1884 // These simple template accessors obtain pointers (or references) to |
1889 // the given field. | 1885 // the given field. |
| 1886 |
| 1887 template <class Type> |
| 1888 const Type& GeneratedMessageReflection::GetRawNonOneof( |
| 1889 const Message& message, const FieldDescriptor* field) const { |
| 1890 return GetConstRefAtOffset<Type>(message, |
| 1891 schema_.GetFieldOffsetNonOneof(field)); |
| 1892 } |
| 1893 |
| 1894 template <class Type> |
| 1895 Type* GeneratedMessageReflection::MutableRawNonOneof( |
| 1896 Message* message, const FieldDescriptor* field) const { |
| 1897 return GetPointerAtOffset<Type>(message, |
| 1898 schema_.GetFieldOffsetNonOneof(field)); |
| 1899 } |
| 1900 |
1890 template <typename Type> | 1901 template <typename Type> |
1891 inline const Type& GeneratedMessageReflection::GetRaw( | 1902 const Type& GeneratedMessageReflection::GetRaw( |
1892 const Message& message, const FieldDescriptor* field) const { | 1903 const Message& message, const FieldDescriptor* field) const { |
1893 if (field->containing_oneof() && !HasOneofField(message, field)) { | 1904 if (field->containing_oneof() && !HasOneofField(message, field)) { |
1894 return DefaultRaw<Type>(field); | 1905 return DefaultRaw<Type>(field); |
1895 } | 1906 } |
1896 int index = field->containing_oneof() ? | 1907 return GetConstRefAtOffset<Type>(message, schema_.GetFieldOffset(field)); |
1897 descriptor_->field_count() + field->containing_oneof()->index() : | |
1898 field->index(); | |
1899 const void* ptr = reinterpret_cast<const uint8*>(&message) + | |
1900 offsets_[index]; | |
1901 return *reinterpret_cast<const Type*>(ptr); | |
1902 } | 1908 } |
1903 | 1909 |
1904 template <typename Type> | 1910 template <typename Type> |
1905 inline Type* GeneratedMessageReflection::MutableRaw( | 1911 Type* GeneratedMessageReflection::MutableRaw(Message* message, |
1906 Message* message, const FieldDescriptor* field) const { | 1912 const FieldDescriptor* field) const { |
1907 int index = field->containing_oneof() ? | 1913 return GetPointerAtOffset<Type>(message, schema_.GetFieldOffset(field)); |
1908 descriptor_->field_count() + field->containing_oneof()->index() : | 1914 } |
1909 field->index(); | 1915 |
1910 void* ptr = reinterpret_cast<uint8*>(message) + offsets_[index]; | 1916 |
1911 return reinterpret_cast<Type*>(ptr); | 1917 inline const uint32* GeneratedMessageReflection::GetHasBits( |
| 1918 const Message& message) const { |
| 1919 GOOGLE_DCHECK(schema_.HasHasbits()); |
| 1920 return &GetConstRefAtOffset<uint32>(message, schema_.HasBitsOffset()); |
| 1921 } |
| 1922 |
| 1923 inline uint32* GeneratedMessageReflection::MutableHasBits( |
| 1924 Message* message) const { |
| 1925 GOOGLE_DCHECK(schema_.HasHasbits()); |
| 1926 return GetPointerAtOffset<uint32>(message, schema_.HasBitsOffset()); |
| 1927 } |
| 1928 |
| 1929 inline uint32 GeneratedMessageReflection::GetOneofCase( |
| 1930 const Message& message, const OneofDescriptor* oneof_descriptor) const { |
| 1931 return GetConstRefAtOffset<uint32>( |
| 1932 message, schema_.GetOneofCaseOffset(oneof_descriptor)); |
| 1933 } |
| 1934 |
| 1935 inline uint32* GeneratedMessageReflection::MutableOneofCase( |
| 1936 Message* message, const OneofDescriptor* oneof_descriptor) const { |
| 1937 return GetPointerAtOffset<uint32>( |
| 1938 message, schema_.GetOneofCaseOffset(oneof_descriptor)); |
| 1939 } |
| 1940 |
| 1941 inline const ExtensionSet& GeneratedMessageReflection::GetExtensionSet( |
| 1942 const Message& message) const { |
| 1943 return GetConstRefAtOffset<ExtensionSet>(message, |
| 1944 schema_.GetExtensionSetOffset()); |
| 1945 } |
| 1946 |
| 1947 inline ExtensionSet* GeneratedMessageReflection::MutableExtensionSet( |
| 1948 Message* message) const { |
| 1949 return GetPointerAtOffset<ExtensionSet>(message, |
| 1950 schema_.GetExtensionSetOffset()); |
| 1951 } |
| 1952 |
| 1953 inline Arena* GeneratedMessageReflection::GetArena(Message* message) const { |
| 1954 return GetInternalMetadataWithArena(*message).arena(); |
| 1955 } |
| 1956 |
| 1957 inline const InternalMetadataWithArena& |
| 1958 GeneratedMessageReflection::GetInternalMetadataWithArena( |
| 1959 const Message& message) const { |
| 1960 return GetConstRefAtOffset<InternalMetadataWithArena>( |
| 1961 message, schema_.GetMetadataOffset()); |
| 1962 } |
| 1963 |
| 1964 inline InternalMetadataWithArena* |
| 1965 GeneratedMessageReflection::MutableInternalMetadataWithArena( |
| 1966 Message* message) const { |
| 1967 return GetPointerAtOffset<InternalMetadataWithArena>( |
| 1968 message, schema_.GetMetadataOffset()); |
1912 } | 1969 } |
1913 | 1970 |
1914 template <typename Type> | 1971 template <typename Type> |
1915 inline const Type& GeneratedMessageReflection::DefaultRaw( | 1972 inline const Type& GeneratedMessageReflection::DefaultRaw( |
1916 const FieldDescriptor* field) const { | 1973 const FieldDescriptor* field) const { |
1917 const void* ptr = field->containing_oneof() ? | 1974 return *reinterpret_cast<const Type*>(schema_.GetFieldDefault(field)); |
1918 reinterpret_cast<const uint8*>(default_oneof_instance_) + | |
1919 offsets_[field->index()] : | |
1920 reinterpret_cast<const uint8*>(default_instance_) + | |
1921 offsets_[field->index()]; | |
1922 return *reinterpret_cast<const Type*>(ptr); | |
1923 } | |
1924 | |
1925 inline const uint32* GeneratedMessageReflection::GetHasBits( | |
1926 const Message& message) const { | |
1927 if (has_bits_offset_ == -1) { // proto3 with no has-bits. | |
1928 return NULL; | |
1929 } | |
1930 const void* ptr = reinterpret_cast<const uint8*>(&message) + has_bits_offset_; | |
1931 return reinterpret_cast<const uint32*>(ptr); | |
1932 } | |
1933 inline uint32* GeneratedMessageReflection::MutableHasBits( | |
1934 Message* message) const { | |
1935 if (has_bits_offset_ == -1) { | |
1936 return NULL; | |
1937 } | |
1938 void* ptr = reinterpret_cast<uint8*>(message) + has_bits_offset_; | |
1939 return reinterpret_cast<uint32*>(ptr); | |
1940 } | |
1941 | |
1942 inline uint32 GeneratedMessageReflection::GetOneofCase( | |
1943 const Message& message, | |
1944 const OneofDescriptor* oneof_descriptor) const { | |
1945 const void* ptr = reinterpret_cast<const uint8*>(&message) | |
1946 + oneof_case_offset_; | |
1947 return reinterpret_cast<const uint32*>(ptr)[oneof_descriptor->index()]; | |
1948 } | |
1949 | |
1950 inline uint32* GeneratedMessageReflection::MutableOneofCase( | |
1951 Message* message, | |
1952 const OneofDescriptor* oneof_descriptor) const { | |
1953 void* ptr = reinterpret_cast<uint8*>(message) + oneof_case_offset_; | |
1954 return &(reinterpret_cast<uint32*>(ptr)[oneof_descriptor->index()]); | |
1955 } | |
1956 | |
1957 inline const ExtensionSet& GeneratedMessageReflection::GetExtensionSet( | |
1958 const Message& message) const { | |
1959 GOOGLE_DCHECK_NE(extensions_offset_, -1); | |
1960 const void* ptr = reinterpret_cast<const uint8*>(&message) + | |
1961 extensions_offset_; | |
1962 return *reinterpret_cast<const ExtensionSet*>(ptr); | |
1963 } | |
1964 inline ExtensionSet* GeneratedMessageReflection::MutableExtensionSet( | |
1965 Message* message) const { | |
1966 GOOGLE_DCHECK_NE(extensions_offset_, -1); | |
1967 void* ptr = reinterpret_cast<uint8*>(message) + extensions_offset_; | |
1968 return reinterpret_cast<ExtensionSet*>(ptr); | |
1969 } | |
1970 | |
1971 inline Arena* GeneratedMessageReflection::GetArena(Message* message) const { | |
1972 if (arena_offset_ == kNoArenaPointer) { | |
1973 return NULL; | |
1974 } | |
1975 | |
1976 if (unknown_fields_offset_ == kUnknownFieldSetInMetadata) { | |
1977 // zero-overhead arena pointer overloading UnknownFields | |
1978 return GetInternalMetadataWithArena(*message).arena(); | |
1979 } | |
1980 | |
1981 // Baseline case: message class has a dedicated arena pointer. | |
1982 void* ptr = reinterpret_cast<uint8*>(message) + arena_offset_; | |
1983 return *reinterpret_cast<Arena**>(ptr); | |
1984 } | |
1985 | |
1986 inline const InternalMetadataWithArena& | |
1987 GeneratedMessageReflection::GetInternalMetadataWithArena( | |
1988 const Message& message) const { | |
1989 const void* ptr = reinterpret_cast<const uint8*>(&message) + arena_offset_; | |
1990 return *reinterpret_cast<const InternalMetadataWithArena*>(ptr); | |
1991 } | |
1992 | |
1993 inline InternalMetadataWithArena* | |
1994 GeneratedMessageReflection::MutableInternalMetadataWithArena( | |
1995 Message* message) const { | |
1996 void* ptr = reinterpret_cast<uint8*>(message) + arena_offset_; | |
1997 return reinterpret_cast<InternalMetadataWithArena*>(ptr); | |
1998 } | |
1999 | |
2000 inline bool | |
2001 GeneratedMessageReflection::GetIsDefaultInstance( | |
2002 const Message& message) const { | |
2003 if (is_default_instance_offset_ == kHasNoDefaultInstanceField) { | |
2004 return false; | |
2005 } | |
2006 const void* ptr = reinterpret_cast<const uint8*>(&message) + | |
2007 is_default_instance_offset_; | |
2008 return *reinterpret_cast<const bool*>(ptr); | |
2009 } | 1975 } |
2010 | 1976 |
2011 // Simple accessors for manipulating has_bits_. | 1977 // Simple accessors for manipulating has_bits_. |
2012 inline bool GeneratedMessageReflection::HasBit( | 1978 inline bool GeneratedMessageReflection::HasBit( |
2013 const Message& message, const FieldDescriptor* field) const { | 1979 const Message& message, const FieldDescriptor* field) const { |
2014 if (has_bits_offset_ == -1) { | 1980 if (schema_.HasHasbits()) { |
2015 // proto3: no has-bits. All fields present except messages, which are | 1981 return IsIndexInHasBitSet(GetHasBits(message), schema_.HasBitIndex(field)); |
2016 // present only if their message-field pointer is non-NULL. | 1982 } |
2017 if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { | |
2018 return !GetIsDefaultInstance(message) && | |
2019 GetRaw<const Message*>(message, field) != NULL; | |
2020 } else { | |
2021 // Non-message field (and non-oneof, since that was handled in HasField() | |
2022 // before calling us), and singular (again, checked in HasField). So, this | |
2023 // field must be a scalar. | |
2024 | 1983 |
2025 // Scalar primitive (numeric or string/bytes) fields are present if | 1984 // proto3: no has-bits. All fields present except messages, which are |
2026 // their value is non-zero (numeric) or non-empty (string/bytes). N.B.: | 1985 // present only if their message-field pointer is non-NULL. |
2027 // we must use this definition here, rather than the "scalar fields | 1986 if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
2028 // always present" in the proto3 docs, because MergeFrom() semantics | 1987 return !schema_.IsDefaultInstance(message) && |
2029 // require presence as "present on wire", and reflection-based merge | 1988 GetRaw<const Message*>(message, field) != NULL; |
2030 // (which uses HasField()) needs to be consistent with this. | 1989 } else { |
2031 switch (field->cpp_type()) { | 1990 // Non-message field (and non-oneof, since that was handled in HasField() |
2032 case FieldDescriptor::CPPTYPE_STRING: | 1991 // before calling us), and singular (again, checked in HasField). So, this |
2033 switch (field->options().ctype()) { | 1992 // field must be a scalar. |
2034 default: { | 1993 |
2035 const string* default_ptr = | 1994 // Scalar primitive (numeric or string/bytes) fields are present if |
2036 &DefaultRaw<ArenaStringPtr>(field).Get(NULL); | 1995 // their value is non-zero (numeric) or non-empty (string/bytes). N.B.: |
2037 return GetField<ArenaStringPtr>(message, field).Get( | 1996 // we must use this definition here, rather than the "scalar fields |
2038 default_ptr).size() > 0; | 1997 // always present" in the proto3 docs, because MergeFrom() semantics |
2039 } | 1998 // require presence as "present on wire", and reflection-based merge |
| 1999 // (which uses HasField()) needs to be consistent with this. |
| 2000 switch (field->cpp_type()) { |
| 2001 case FieldDescriptor::CPPTYPE_STRING: |
| 2002 switch (field->options().ctype()) { |
| 2003 default: { |
| 2004 return GetField<ArenaStringPtr>(message, field).Get().size() > 0; |
2040 } | 2005 } |
2041 return false; | 2006 } |
2042 case FieldDescriptor::CPPTYPE_BOOL: | 2007 return false; |
2043 return GetRaw<bool>(message, field) != false; | 2008 case FieldDescriptor::CPPTYPE_BOOL: |
2044 case FieldDescriptor::CPPTYPE_INT32: | 2009 return GetRaw<bool>(message, field) != false; |
2045 return GetRaw<int32>(message, field) != 0; | 2010 case FieldDescriptor::CPPTYPE_INT32: |
2046 case FieldDescriptor::CPPTYPE_INT64: | 2011 return GetRaw<int32>(message, field) != 0; |
2047 return GetRaw<int64>(message, field) != 0; | 2012 case FieldDescriptor::CPPTYPE_INT64: |
2048 case FieldDescriptor::CPPTYPE_UINT32: | 2013 return GetRaw<int64>(message, field) != 0; |
2049 return GetRaw<uint32>(message, field) != 0; | 2014 case FieldDescriptor::CPPTYPE_UINT32: |
2050 case FieldDescriptor::CPPTYPE_UINT64: | 2015 return GetRaw<uint32>(message, field) != 0; |
2051 return GetRaw<uint64>(message, field) != 0; | 2016 case FieldDescriptor::CPPTYPE_UINT64: |
2052 case FieldDescriptor::CPPTYPE_FLOAT: | 2017 return GetRaw<uint64>(message, field) != 0; |
2053 return GetRaw<float>(message, field) != 0.0; | 2018 case FieldDescriptor::CPPTYPE_FLOAT: |
2054 case FieldDescriptor::CPPTYPE_DOUBLE: | 2019 return GetRaw<float>(message, field) != 0.0; |
2055 return GetRaw<double>(message, field) != 0.0; | 2020 case FieldDescriptor::CPPTYPE_DOUBLE: |
2056 case FieldDescriptor::CPPTYPE_ENUM: | 2021 return GetRaw<double>(message, field) != 0.0; |
2057 return GetRaw<int>(message, field) != 0; | 2022 case FieldDescriptor::CPPTYPE_ENUM: |
2058 case FieldDescriptor::CPPTYPE_MESSAGE: | 2023 return GetRaw<int>(message, field) != 0; |
2059 // handled above; avoid warning | 2024 case FieldDescriptor::CPPTYPE_MESSAGE: |
2060 GOOGLE_LOG(FATAL) << "Reached impossible case in HasBit()."; | 2025 // handled above; avoid warning |
2061 break; | 2026 break; |
2062 } | |
2063 } | 2027 } |
| 2028 GOOGLE_LOG(FATAL) << "Reached impossible case in HasBit()."; |
| 2029 return false; |
2064 } | 2030 } |
2065 return GetHasBits(message)[field->index() / 32] & | |
2066 (1 << (field->index() % 32)); | |
2067 } | 2031 } |
2068 | 2032 |
2069 inline void GeneratedMessageReflection::SetBit( | 2033 inline void GeneratedMessageReflection::SetBit( |
2070 Message* message, const FieldDescriptor* field) const { | 2034 Message* message, const FieldDescriptor* field) const { |
2071 if (has_bits_offset_ == -1) { | 2035 if (!schema_.HasHasbits()) { |
2072 return; | 2036 return; |
2073 } | 2037 } |
2074 MutableHasBits(message)[field->index() / 32] |= (1 << (field->index() % 32)); | 2038 const uint32 index = schema_.HasBitIndex(field); |
| 2039 MutableHasBits(message)[index / 32] |= |
| 2040 (static_cast<uint32>(1) << (index % 32)); |
2075 } | 2041 } |
2076 | 2042 |
2077 inline void GeneratedMessageReflection::ClearBit( | 2043 inline void GeneratedMessageReflection::ClearBit( |
2078 Message* message, const FieldDescriptor* field) const { | 2044 Message* message, const FieldDescriptor* field) const { |
2079 if (has_bits_offset_ == -1) { | 2045 if (!schema_.HasHasbits()) { |
2080 return; | 2046 return; |
2081 } | 2047 } |
2082 MutableHasBits(message)[field->index() / 32] &= ~(1 << (field->index() % 32)); | 2048 const uint32 index = schema_.HasBitIndex(field); |
| 2049 MutableHasBits(message)[index / 32] &= |
| 2050 ~(static_cast<uint32>(1) << (index % 32)); |
2083 } | 2051 } |
2084 | 2052 |
2085 inline void GeneratedMessageReflection::SwapBit( | 2053 inline void GeneratedMessageReflection::SwapBit( |
2086 Message* message1, Message* message2, const FieldDescriptor* field) const { | 2054 Message* message1, Message* message2, const FieldDescriptor* field) const { |
2087 if (has_bits_offset_ == -1) { | 2055 if (!schema_.HasHasbits()) { |
2088 return; | 2056 return; |
2089 } | 2057 } |
2090 bool temp_has_bit = HasBit(*message1, field); | 2058 bool temp_has_bit = HasBit(*message1, field); |
2091 if (HasBit(*message2, field)) { | 2059 if (HasBit(*message2, field)) { |
2092 SetBit(message1, field); | 2060 SetBit(message1, field); |
2093 } else { | 2061 } else { |
2094 ClearBit(message1, field); | 2062 ClearBit(message1, field); |
2095 } | 2063 } |
2096 if (temp_has_bit) { | 2064 if (temp_has_bit) { |
2097 SetBit(message2, field); | 2065 SetBit(message2, field); |
(...skipping 20 matching lines...) Expand all Loading... |
2118 inline void GeneratedMessageReflection::ClearOneofField( | 2086 inline void GeneratedMessageReflection::ClearOneofField( |
2119 Message* message, const FieldDescriptor* field) const { | 2087 Message* message, const FieldDescriptor* field) const { |
2120 if (HasOneofField(*message, field)) { | 2088 if (HasOneofField(*message, field)) { |
2121 ClearOneof(message, field->containing_oneof()); | 2089 ClearOneof(message, field->containing_oneof()); |
2122 } | 2090 } |
2123 } | 2091 } |
2124 | 2092 |
2125 inline void GeneratedMessageReflection::ClearOneof( | 2093 inline void GeneratedMessageReflection::ClearOneof( |
2126 Message* message, const OneofDescriptor* oneof_descriptor) const { | 2094 Message* message, const OneofDescriptor* oneof_descriptor) const { |
2127 // TODO(jieluo): Consider to cache the unused object instead of deleting | 2095 // TODO(jieluo): Consider to cache the unused object instead of deleting |
2128 // it. It will be much faster if an aplication switches a lot from | 2096 // it. It will be much faster if an application switches a lot from |
2129 // a few oneof fields. Time/space tradeoff | 2097 // a few oneof fields. Time/space tradeoff |
2130 uint32 oneof_case = GetOneofCase(*message, oneof_descriptor); | 2098 uint32 oneof_case = GetOneofCase(*message, oneof_descriptor); |
2131 if (oneof_case > 0) { | 2099 if (oneof_case > 0) { |
2132 const FieldDescriptor* field = descriptor_->FindFieldByNumber(oneof_case); | 2100 const FieldDescriptor* field = descriptor_->FindFieldByNumber(oneof_case); |
2133 if (GetArena(message) == NULL) { | 2101 if (GetArena(message) == NULL) { |
2134 switch (field->cpp_type()) { | 2102 switch (field->cpp_type()) { |
2135 case FieldDescriptor::CPPTYPE_STRING: { | 2103 case FieldDescriptor::CPPTYPE_STRING: { |
2136 switch (field->options().ctype()) { | 2104 switch (field->options().ctype()) { |
2137 default: // TODO(kenton): Support other string reps. | 2105 default: // TODO(kenton): Support other string reps. |
2138 case FieldOptions::STRING: { | 2106 case FieldOptions::STRING: { |
2139 const string* default_ptr = | 2107 const string* default_ptr = |
2140 &DefaultRaw<ArenaStringPtr>(field).Get(NULL); | 2108 &DefaultRaw<ArenaStringPtr>(field).Get(); |
2141 MutableField<ArenaStringPtr>(message, field)-> | 2109 MutableField<ArenaStringPtr>(message, field)-> |
2142 Destroy(default_ptr, GetArena(message)); | 2110 Destroy(default_ptr, GetArena(message)); |
2143 break; | 2111 break; |
2144 } | 2112 } |
2145 } | 2113 } |
2146 break; | 2114 break; |
2147 } | 2115 } |
2148 | 2116 |
2149 case FieldDescriptor::CPPTYPE_MESSAGE: | 2117 case FieldDescriptor::CPPTYPE_MESSAGE: |
2150 delete *MutableRaw<Message*>(message, field); | 2118 delete *MutableRaw<Message*>(message, field); |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2242 << "The type parameter T in RepeatedFieldRef<T> API doesn't match " | 2210 << "The type parameter T in RepeatedFieldRef<T> API doesn't match " |
2243 << "the actual field type (for enums T should be the generated enum " | 2211 << "the actual field type (for enums T should be the generated enum " |
2244 << "type or int32)."; | 2212 << "type or int32)."; |
2245 if (message_type != NULL) { | 2213 if (message_type != NULL) { |
2246 GOOGLE_CHECK_EQ(message_type, field->message_type()); | 2214 GOOGLE_CHECK_EQ(message_type, field->message_type()); |
2247 } | 2215 } |
2248 if (field->is_extension()) { | 2216 if (field->is_extension()) { |
2249 return MutableExtensionSet(message)->MutableRawRepeatedField( | 2217 return MutableExtensionSet(message)->MutableRawRepeatedField( |
2250 field->number(), field->type(), field->is_packed(), field); | 2218 field->number(), field->type(), field->is_packed(), field); |
2251 } else { | 2219 } else { |
2252 return reinterpret_cast<uint8*>(message) + offsets_[field->index()]; | 2220 return MutableRawNonOneof<char>(message, field); |
2253 } | 2221 } |
2254 } | 2222 } |
2255 | 2223 |
2256 MapFieldBase* GeneratedMessageReflection::MapData( | 2224 MapFieldBase* GeneratedMessageReflection::MapData( |
2257 Message* message, const FieldDescriptor* field) const { | 2225 Message* message, const FieldDescriptor* field) const { |
2258 USAGE_CHECK(IsMapFieldInApi(field), | 2226 USAGE_CHECK(IsMapFieldInApi(field), |
2259 "GetMapData", | 2227 "GetMapData", |
2260 "Field is not a map field."); | 2228 "Field is not a map field."); |
2261 return MutableRaw<MapFieldBase>(message, field); | 2229 return MutableRaw<MapFieldBase>(message, field); |
2262 } | 2230 } |
2263 | 2231 |
2264 GeneratedMessageReflection* | 2232 namespace { |
2265 GeneratedMessageReflection::NewGeneratedMessageReflection( | 2233 |
2266 const Descriptor* descriptor, | 2234 // Helper function to transform migration schema into reflection schema. |
2267 const Message* default_instance, | 2235 ReflectionSchema MigrationToReflectionSchema( |
2268 const int offsets[], | 2236 const DefaultInstanceData* default_instance_data, const uint32* offsets, |
2269 int has_bits_offset, | 2237 MigrationSchema migration_schema) { |
2270 int unknown_fields_offset, | 2238 ReflectionSchema result; |
2271 int extensions_offset, | 2239 result.default_instance_ = default_instance_data->default_instance; |
2272 const void* default_oneof_instance, | 2240 // First 5 offsets are offsets to the special fields. The following offsets |
2273 int oneof_case_offset, | 2241 // are the proto fields. |
2274 int object_size, | 2242 result.offsets_ = offsets + migration_schema.offsets_index + 4; |
2275 int arena_offset, | 2243 result.has_bit_indices_ = offsets + migration_schema.has_bit_indices_index; |
2276 int is_default_instance_offset) { | 2244 result.has_bits_offset_ = offsets[migration_schema.offsets_index + 0]; |
2277 return new GeneratedMessageReflection(descriptor, | 2245 result.metadata_offset_ = offsets[migration_schema.offsets_index + 1]; |
2278 default_instance, | 2246 result.extensions_offset_ = offsets[migration_schema.offsets_index + 2]; |
2279 offsets, | 2247 result.default_oneof_instance_ = default_instance_data->default_oneof_instance
; |
2280 has_bits_offset, | 2248 result.oneof_case_offset_ = offsets[migration_schema.offsets_index + 3]; |
2281 unknown_fields_offset, | 2249 result.object_size_ = migration_schema.object_size; |
2282 extensions_offset, | 2250 return result; |
2283 default_oneof_instance, | |
2284 oneof_case_offset, | |
2285 DescriptorPool::generated_pool(), | |
2286 MessageFactory::generated_factory(), | |
2287 object_size, | |
2288 arena_offset, | |
2289 is_default_instance_offset); | |
2290 } | 2251 } |
2291 | 2252 |
2292 GeneratedMessageReflection* | 2253 ReflectionSchema MigrationToReflectionSchema( |
2293 GeneratedMessageReflection::NewGeneratedMessageReflection( | 2254 const DefaultInstanceData* default_instance_data, const uint32* offsets, |
2294 const Descriptor* descriptor, | 2255 ReflectionSchema schema) { |
2295 const Message* default_instance, | 2256 return schema; |
2296 const int offsets[], | 2257 } |
2297 int has_bits_offset, | 2258 |
2298 int unknown_fields_offset, | 2259 template<typename Schema> |
2299 int extensions_offset, | 2260 class AssignDescriptorsHelper { |
2300 int object_size, | 2261 public: |
2301 int arena_offset, | 2262 AssignDescriptorsHelper(MessageFactory* factory, |
2302 int is_default_instance_offset) { | 2263 Metadata* file_level_metadata, |
2303 return new GeneratedMessageReflection(descriptor, | 2264 const EnumDescriptor** file_level_enum_descriptors, |
2304 default_instance, | 2265 const Schema* schemas, |
2305 offsets, | 2266 const DefaultInstanceData* default_instance_data, |
2306 has_bits_offset, | 2267 const uint32* offsets) |
2307 unknown_fields_offset, | 2268 : factory_(factory), |
2308 extensions_offset, | 2269 file_level_metadata_(file_level_metadata), |
2309 DescriptorPool::generated_pool(), | 2270 file_level_enum_descriptors_(file_level_enum_descriptors), |
2310 MessageFactory::generated_factory(), | 2271 schemas_(schemas), |
2311 object_size, | 2272 default_instance_data_(default_instance_data), |
2312 arena_offset, | 2273 offsets_(offsets) {} |
2313 is_default_instance_offset); | 2274 |
| 2275 void AssignMessageDescriptor(const Descriptor* descriptor) { |
| 2276 for (int i = 0; i < descriptor->nested_type_count(); i++) { |
| 2277 AssignMessageDescriptor(descriptor->nested_type(i)); |
| 2278 } |
| 2279 |
| 2280 file_level_metadata_->descriptor = descriptor; |
| 2281 |
| 2282 if (!descriptor->options().map_entry()) { |
| 2283 // Only set reflection for non map types. |
| 2284 file_level_metadata_->reflection = new GeneratedMessageReflection( |
| 2285 descriptor, MigrationToReflectionSchema(default_instance_data_++, |
| 2286 offsets_, *schemas_), |
| 2287 ::google::protobuf::DescriptorPool::generated_pool(), factory_); |
| 2288 for (int i = 0; i < descriptor->enum_type_count(); i++) { |
| 2289 AssignEnumDescriptor(descriptor->enum_type(i)); |
| 2290 } |
| 2291 schemas_++; |
| 2292 } |
| 2293 file_level_metadata_++; |
| 2294 } |
| 2295 |
| 2296 void AssignEnumDescriptor(const EnumDescriptor* descriptor) { |
| 2297 *file_level_enum_descriptors_ = descriptor; |
| 2298 file_level_enum_descriptors_++; |
| 2299 } |
| 2300 |
| 2301 private: |
| 2302 MessageFactory* factory_; |
| 2303 Metadata* file_level_metadata_; |
| 2304 const EnumDescriptor** file_level_enum_descriptors_; |
| 2305 const Schema* schemas_; |
| 2306 const DefaultInstanceData* default_instance_data_; |
| 2307 const uint32* offsets_; |
| 2308 }; |
| 2309 |
| 2310 } // namespace |
| 2311 |
| 2312 void AssignDescriptors( |
| 2313 const string& filename, const MigrationSchema* schemas, |
| 2314 const DefaultInstanceData* default_instance_data, const uint32* offsets, |
| 2315 MessageFactory* factory, |
| 2316 // update the following descriptor arrays. |
| 2317 Metadata* file_level_metadata, |
| 2318 const EnumDescriptor** file_level_enum_descriptors, |
| 2319 const ServiceDescriptor** file_level_service_descriptors) { |
| 2320 const ::google::protobuf::FileDescriptor* file = |
| 2321 ::google::protobuf::DescriptorPool::generated_pool()->FindFileByName(filen
ame); |
| 2322 GOOGLE_CHECK(file != NULL); |
| 2323 |
| 2324 if (!factory) factory = MessageFactory::generated_factory(); |
| 2325 |
| 2326 AssignDescriptorsHelper<MigrationSchema> helper(factory, file_level_metadata, |
| 2327 file_level_enum_descriptors, schemas, |
| 2328 default_instance_data, offsets); |
| 2329 |
| 2330 for (int i = 0; i < file->message_type_count(); i++) { |
| 2331 helper.AssignMessageDescriptor(file->message_type(i)); |
| 2332 } |
| 2333 |
| 2334 for (int i = 0; i < file->enum_type_count(); i++) { |
| 2335 helper.AssignEnumDescriptor(file->enum_type(i)); |
| 2336 } |
| 2337 if (file->options().cc_generic_services()) { |
| 2338 for (int i = 0; i < file->service_count(); i++) { |
| 2339 file_level_service_descriptors[i] = file->service(i); |
| 2340 } |
| 2341 } |
| 2342 } |
| 2343 |
| 2344 void AssignDescriptors( |
| 2345 const string& filename, const ReflectionSchema* schemas, |
| 2346 MessageFactory* factory, |
| 2347 // update the following descriptor arrays. |
| 2348 Metadata* file_level_metadata, |
| 2349 const EnumDescriptor** file_level_enum_descriptors, |
| 2350 const ServiceDescriptor** file_level_service_descriptors) { |
| 2351 const ::google::protobuf::FileDescriptor* file = |
| 2352 ::google::protobuf::DescriptorPool::generated_pool()->FindFileByName(filen
ame); |
| 2353 GOOGLE_CHECK(file != NULL); |
| 2354 |
| 2355 if (!factory) factory = MessageFactory::generated_factory(); |
| 2356 |
| 2357 AssignDescriptorsHelper<ReflectionSchema> helper(factory, file_level_metadata, |
| 2358 file_level_enum_descriptors, schemas, |
| 2359 NULL, NULL); |
| 2360 |
| 2361 for (int i = 0; i < file->message_type_count(); i++) { |
| 2362 helper.AssignMessageDescriptor(file->message_type(i)); |
| 2363 } |
| 2364 |
| 2365 for (int i = 0; i < file->enum_type_count(); i++) { |
| 2366 helper.AssignEnumDescriptor(file->enum_type(i)); |
| 2367 } |
| 2368 if (file->options().cc_generic_services()) { |
| 2369 for (int i = 0; i < file->service_count(); i++) { |
| 2370 file_level_service_descriptors[i] = file->service(i); |
| 2371 } |
| 2372 } |
| 2373 } |
| 2374 |
| 2375 void RegisterAllTypesInternal(const Metadata* file_level_metadata, int size) { |
| 2376 for (int i = 0; i < size; i++) { |
| 2377 const GeneratedMessageReflection* reflection = |
| 2378 static_cast<const GeneratedMessageReflection*>( |
| 2379 file_level_metadata[i].reflection); |
| 2380 if (reflection) { |
| 2381 // It's not a map type |
| 2382 ::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage( |
| 2383 file_level_metadata[i].descriptor, |
| 2384 reflection->schema_.default_instance_); |
| 2385 } |
| 2386 } |
| 2387 } |
| 2388 |
| 2389 void RegisterAllTypes(const Metadata* file_level_metadata, int size) { |
| 2390 RegisterAllTypesInternal(file_level_metadata, size); |
2314 } | 2391 } |
2315 | 2392 |
2316 } // namespace internal | 2393 } // namespace internal |
2317 } // namespace protobuf | 2394 } // namespace protobuf |
2318 } // namespace google | 2395 } // namespace google |
OLD | NEW |