1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
30
31 // Author: kenton@google.com (Kenton Varda)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
34
35 #include <algorithm>
36 #include <set>
37
38 #include <google/protobuf/stubs/logging.h>
39 #include <google/protobuf/stubs/common.h>
40 #include <google/protobuf/descriptor.pb.h>
41 #include <google/protobuf/descriptor.h>
42 #include <google/protobuf/extension_set.h>
43 #include <google/protobuf/generated_message_reflection.h>
44 #include <google/protobuf/generated_message_util.h>
45 #include <google/protobuf/map_field.h>
46 #include <google/protobuf/repeated_field.h>
47
48
49 #define GOOGLE_PROTOBUF_HAS_ONEOF
50
51 namespace google {
52 namespace protobuf {
53 namespace internal {
54
55 namespace {
IsMapFieldInApi(const FieldDescriptor * field)56 bool IsMapFieldInApi(const FieldDescriptor* field) {
57 return field->is_map();
58 }
59 } // anonymous namespace
60
ParseNamedEnum(const EnumDescriptor * descriptor,const string & name,int * value)61 bool ParseNamedEnum(const EnumDescriptor* descriptor,
62 const string& name,
63 int* value) {
64 const EnumValueDescriptor* d = descriptor->FindValueByName(name);
65 if (d == NULL) return false;
66 *value = d->number();
67 return true;
68 }
69
NameOfEnum(const EnumDescriptor * descriptor,int value)70 const string& NameOfEnum(const EnumDescriptor* descriptor, int value) {
71 const EnumValueDescriptor* d = descriptor->FindValueByNumber(value);
72 return (d == NULL ? GetEmptyString() : d->name());
73 }
74
75 namespace {
SupportsArenas(const Descriptor * descriptor)76 inline bool SupportsArenas(const Descriptor* descriptor) {
77 return descriptor->file()->options().cc_enable_arenas();
78 }
79 } // anonymous namespace
80
81 // ===================================================================
82 // Helpers for reporting usage errors (e.g. trying to use GetInt32() on
83 // a string field).
84
85 namespace {
86
ReportReflectionUsageError(const Descriptor * descriptor,const FieldDescriptor * field,const char * method,const char * description)87 void ReportReflectionUsageError(
88 const Descriptor* descriptor, const FieldDescriptor* field,
89 const char* method, const char* description) {
90 GOOGLE_LOG(FATAL)
91 << "Protocol Buffer reflection usage error:\n"
92 " Method : google::protobuf::Reflection::" << method << "\n"
93 " Message type: " << descriptor->full_name() << "\n"
94 " Field : " << field->full_name() << "\n"
95 " Problem : " << description;
96 }
97
98 const char* cpptype_names_[FieldDescriptor::MAX_CPPTYPE + 1] = {
99 "INVALID_CPPTYPE",
100 "CPPTYPE_INT32",
101 "CPPTYPE_INT64",
102 "CPPTYPE_UINT32",
103 "CPPTYPE_UINT64",
104 "CPPTYPE_DOUBLE",
105 "CPPTYPE_FLOAT",
106 "CPPTYPE_BOOL",
107 "CPPTYPE_ENUM",
108 "CPPTYPE_STRING",
109 "CPPTYPE_MESSAGE"
110 };
111
ReportReflectionUsageTypeError(const Descriptor * descriptor,const FieldDescriptor * field,const char * method,FieldDescriptor::CppType expected_type)112 static void ReportReflectionUsageTypeError(
113 const Descriptor* descriptor, const FieldDescriptor* field,
114 const char* method,
115 FieldDescriptor::CppType expected_type) {
116 GOOGLE_LOG(FATAL)
117 << "Protocol Buffer reflection usage error:\n"
118 " Method : google::protobuf::Reflection::" << method << "\n"
119 " Message type: " << descriptor->full_name() << "\n"
120 " Field : " << field->full_name() << "\n"
121 " Problem : Field is not the right type for this message:\n"
122 " Expected : " << cpptype_names_[expected_type] << "\n"
123 " Field type: " << cpptype_names_[field->cpp_type()];
124 }
125
ReportReflectionUsageEnumTypeError(const Descriptor * descriptor,const FieldDescriptor * field,const char * method,const EnumValueDescriptor * value)126 static void ReportReflectionUsageEnumTypeError(
127 const Descriptor* descriptor, const FieldDescriptor* field,
128 const char* method, const EnumValueDescriptor* value) {
129 GOOGLE_LOG(FATAL)
130 << "Protocol Buffer reflection usage error:\n"
131 " Method : google::protobuf::Reflection::" << method << "\n"
132 " Message type: " << descriptor->full_name() << "\n"
133 " Field : " << field->full_name() << "\n"
134 " Problem : Enum value did not match field type:\n"
135 " Expected : " << field->enum_type()->full_name() << "\n"
136 " Actual : " << value->full_name();
137 }
138
139 #define USAGE_CHECK(CONDITION, METHOD, ERROR_DESCRIPTION) \
140 if (!(CONDITION)) \
141 ReportReflectionUsageError(descriptor_, field, #METHOD, ERROR_DESCRIPTION)
142 #define USAGE_CHECK_EQ(A, B, METHOD, ERROR_DESCRIPTION) \
143 USAGE_CHECK((A) == (B), METHOD, ERROR_DESCRIPTION)
144 #define USAGE_CHECK_NE(A, B, METHOD, ERROR_DESCRIPTION) \
145 USAGE_CHECK((A) != (B), METHOD, ERROR_DESCRIPTION)
146
147 #define USAGE_CHECK_TYPE(METHOD, CPPTYPE) \
148 if (field->cpp_type() != FieldDescriptor::CPPTYPE_##CPPTYPE) \
149 ReportReflectionUsageTypeError(descriptor_, field, #METHOD, \
150 FieldDescriptor::CPPTYPE_##CPPTYPE)
151
152 #define USAGE_CHECK_ENUM_VALUE(METHOD) \
153 if (value->type() != field->enum_type()) \
154 ReportReflectionUsageEnumTypeError(descriptor_, field, #METHOD, value)
155
156 #define USAGE_CHECK_MESSAGE_TYPE(METHOD) \
157 USAGE_CHECK_EQ(field->containing_type(), descriptor_, \
158 METHOD, "Field does not match message type.");
159 #define USAGE_CHECK_SINGULAR(METHOD) \
160 USAGE_CHECK_NE(field->label(), FieldDescriptor::LABEL_REPEATED, METHOD, \
161 "Field is repeated; the method requires a singular field.")
162 #define USAGE_CHECK_REPEATED(METHOD) \
163 USAGE_CHECK_EQ(field->label(), FieldDescriptor::LABEL_REPEATED, METHOD, \
164 "Field is singular; the method requires a repeated field.")
165
166 #define USAGE_CHECK_ALL(METHOD, LABEL, CPPTYPE) \
167 USAGE_CHECK_MESSAGE_TYPE(METHOD); \
168 USAGE_CHECK_##LABEL(METHOD); \
169 USAGE_CHECK_TYPE(METHOD, CPPTYPE)
170
171 } // namespace
172
173 // ===================================================================
174
GeneratedMessageReflection(const Descriptor * descriptor,const Message * default_instance,const int offsets[],int has_bits_offset,int unknown_fields_offset,int extensions_offset,const DescriptorPool * descriptor_pool,MessageFactory * factory,int object_size,int arena_offset,int is_default_instance_offset)175 GeneratedMessageReflection::GeneratedMessageReflection(
176 const Descriptor* descriptor,
177 const Message* default_instance,
178 const int offsets[],
179 int has_bits_offset,
180 int unknown_fields_offset,
181 int extensions_offset,
182 const DescriptorPool* descriptor_pool,
183 MessageFactory* factory,
184 int object_size,
185 int arena_offset,
186 int is_default_instance_offset)
187 : descriptor_ (descriptor),
188 default_instance_ (default_instance),
189 offsets_ (offsets),
190 has_bits_offset_ (has_bits_offset),
191 unknown_fields_offset_(unknown_fields_offset),
192 extensions_offset_(extensions_offset),
193 arena_offset_ (arena_offset),
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
GeneratedMessageReflection(const Descriptor * descriptor,const Message * default_instance,const int offsets[],int has_bits_offset,int unknown_fields_offset,int extensions_offset,const void * default_oneof_instance,int oneof_case_offset,const DescriptorPool * descriptor_pool,MessageFactory * factory,int object_size,int arena_offset,int is_default_instance_offset)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 }
232
~GeneratedMessageReflection()233 GeneratedMessageReflection::~GeneratedMessageReflection() {}
234
235 namespace {
236 UnknownFieldSet* empty_unknown_field_set_ = NULL;
237 GOOGLE_PROTOBUF_DECLARE_ONCE(empty_unknown_field_set_once_);
238
DeleteEmptyUnknownFieldSet()239 void DeleteEmptyUnknownFieldSet() {
240 delete empty_unknown_field_set_;
241 empty_unknown_field_set_ = NULL;
242 }
243
InitEmptyUnknownFieldSet()244 void InitEmptyUnknownFieldSet() {
245 empty_unknown_field_set_ = new UnknownFieldSet;
246 internal::OnShutdown(&DeleteEmptyUnknownFieldSet);
247 }
248
GetEmptyUnknownFieldSet()249 const UnknownFieldSet& GetEmptyUnknownFieldSet() {
250 ::google::protobuf::GoogleOnceInit(&empty_unknown_field_set_once_, &InitEmptyUnknownFieldSet);
251 return *empty_unknown_field_set_;
252 }
253 } // namespace
254
GetUnknownFields(const Message & message) const255 const UnknownFieldSet& GeneratedMessageReflection::GetUnknownFields(
256 const Message& message) const {
257 if (descriptor_->file()->syntax() == FileDescriptor::SYNTAX_PROTO3) {
258 return GetEmptyUnknownFieldSet();
259 }
260 if (unknown_fields_offset_ == kUnknownFieldSetInMetadata) {
261 return GetInternalMetadataWithArena(message).unknown_fields();
262 }
263 const void* ptr = reinterpret_cast<const uint8*>(&message) +
264 unknown_fields_offset_;
265 return *reinterpret_cast<const UnknownFieldSet*>(ptr);
266 }
267
MutableUnknownFields(Message * message) const268 UnknownFieldSet* GeneratedMessageReflection::MutableUnknownFields(
269 Message* message) const {
270 if (unknown_fields_offset_ == kUnknownFieldSetInMetadata) {
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 }
277
SpaceUsed(const Message & message) const278 int GeneratedMessageReflection::SpaceUsed(const Message& message) const {
279 // 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
281 // the fields.
282 int total_size = object_size_;
283
284 total_size += GetUnknownFields(message).SpaceUsedExcludingSelf();
285
286 if (extensions_offset_ != -1) {
287 total_size += GetExtensionSet(message).SpaceUsedExcludingSelf();
288 }
289
290 for (int i = 0; i < descriptor_->field_count(); i++) {
291 const FieldDescriptor* field = descriptor_->field(i);
292
293 if (field->is_repeated()) {
294 switch (field->cpp_type()) {
295 #define HANDLE_TYPE(UPPERCASE, LOWERCASE) \
296 case FieldDescriptor::CPPTYPE_##UPPERCASE : \
297 total_size += GetRaw<RepeatedField<LOWERCASE> >(message, field) \
298 .SpaceUsedExcludingSelf(); \
299 break
300
301 HANDLE_TYPE( INT32, int32);
302 HANDLE_TYPE( INT64, int64);
303 HANDLE_TYPE(UINT32, uint32);
304 HANDLE_TYPE(UINT64, uint64);
305 HANDLE_TYPE(DOUBLE, double);
306 HANDLE_TYPE( FLOAT, float);
307 HANDLE_TYPE( BOOL, bool);
308 HANDLE_TYPE( ENUM, int);
309 #undef HANDLE_TYPE
310
311 case FieldDescriptor::CPPTYPE_STRING:
312 switch (field->options().ctype()) {
313 default: // TODO(kenton): Support other string reps.
314 case FieldOptions::STRING:
315 total_size += GetRaw<RepeatedPtrField<string> >(message, field)
316 .SpaceUsedExcludingSelf();
317 break;
318 }
319 break;
320
321 case FieldDescriptor::CPPTYPE_MESSAGE:
322 if (IsMapFieldInApi(field)) {
323 total_size +=
324 GetRaw<MapFieldBase>(message, field).SpaceUsedExcludingSelf();
325 } else {
326 // We don't know which subclass of RepeatedPtrFieldBase the type is,
327 // so we use RepeatedPtrFieldBase directly.
328 total_size +=
329 GetRaw<RepeatedPtrFieldBase>(message, field)
330 .SpaceUsedExcludingSelf<GenericTypeHandler<Message> >();
331 }
332
333 break;
334 }
335 } else {
336 if (field->containing_oneof() && !HasOneofField(message, field)) {
337 continue;
338 }
339 switch (field->cpp_type()) {
340 case FieldDescriptor::CPPTYPE_INT32 :
341 case FieldDescriptor::CPPTYPE_INT64 :
342 case FieldDescriptor::CPPTYPE_UINT32:
343 case FieldDescriptor::CPPTYPE_UINT64:
344 case FieldDescriptor::CPPTYPE_DOUBLE:
345 case FieldDescriptor::CPPTYPE_FLOAT :
346 case FieldDescriptor::CPPTYPE_BOOL :
347 case FieldDescriptor::CPPTYPE_ENUM :
348 // Field is inline, so we've already counted it.
349 break;
350
351 case FieldDescriptor::CPPTYPE_STRING: {
352 switch (field->options().ctype()) {
353 default: // TODO(kenton): Support other string reps.
354 case FieldOptions::STRING: {
355 // Initially, the string points to the default value stored in
356 // the prototype. Only count the string if it has been changed
357 // from the default value.
358 const string* default_ptr =
359 &DefaultRaw<ArenaStringPtr>(field).Get(NULL);
360 const string* ptr =
361 &GetField<ArenaStringPtr>(message, field).Get(default_ptr);
362
363 if (ptr != default_ptr) {
364 // string fields are represented by just a pointer, so also
365 // include sizeof(string) as well.
366 total_size += sizeof(*ptr) + StringSpaceUsedExcludingSelf(*ptr);
367 }
368 break;
369 }
370 }
371 break;
372 }
373
374 case FieldDescriptor::CPPTYPE_MESSAGE:
375 if (&message == default_instance_) {
376 // For singular fields, the prototype just stores a pointer to the
377 // external type's prototype, so there is no extra memory usage.
378 } else {
379 const Message* sub_message = GetRaw<const Message*>(message, field);
380 if (sub_message != NULL) {
381 total_size += sub_message->SpaceUsed();
382 }
383 }
384 break;
385 }
386 }
387 }
388
389 return total_size;
390 }
391
SwapField(Message * message1,Message * message2,const FieldDescriptor * field) const392 void GeneratedMessageReflection::SwapField(
393 Message* message1,
394 Message* message2,
395 const FieldDescriptor* field) const {
396 if (field->is_repeated()) {
397 switch (field->cpp_type()) {
398 #define SWAP_ARRAYS(CPPTYPE, TYPE) \
399 case FieldDescriptor::CPPTYPE_##CPPTYPE: \
400 MutableRaw<RepeatedField<TYPE> >(message1, field)->Swap( \
401 MutableRaw<RepeatedField<TYPE> >(message2, field)); \
402 break;
403
404 SWAP_ARRAYS(INT32 , int32 );
405 SWAP_ARRAYS(INT64 , int64 );
406 SWAP_ARRAYS(UINT32, uint32);
407 SWAP_ARRAYS(UINT64, uint64);
408 SWAP_ARRAYS(FLOAT , float );
409 SWAP_ARRAYS(DOUBLE, double);
410 SWAP_ARRAYS(BOOL , bool );
411 SWAP_ARRAYS(ENUM , int );
412 #undef SWAP_ARRAYS
413
414 case FieldDescriptor::CPPTYPE_STRING:
415 switch (field->options().ctype()) {
416 default: // TODO(kenton): Support other string reps.
417 case FieldOptions::STRING:
418 MutableRaw<RepeatedPtrFieldBase>(message1, field)->
419 Swap<GenericTypeHandler<string> >(
420 MutableRaw<RepeatedPtrFieldBase>(message2, field));
421 break;
422 }
423 break;
424 case FieldDescriptor::CPPTYPE_MESSAGE:
425 if (IsMapFieldInApi(field)) {
426 MutableRaw<MapFieldBase>(message1, field)->
427 MutableRepeatedField()->
428 Swap<GenericTypeHandler<google::protobuf::Message> >(
429 MutableRaw<MapFieldBase>(message2, field)->
430 MutableRepeatedField());
431 } else {
432 MutableRaw<RepeatedPtrFieldBase>(message1, field)->
433 Swap<GenericTypeHandler<google::protobuf::Message> >(
434 MutableRaw<RepeatedPtrFieldBase>(message2, field));
435 }
436 break;
437
438 default:
439 GOOGLE_LOG(FATAL) << "Unimplemented type: " << field->cpp_type();
440 }
441 } else {
442 switch (field->cpp_type()) {
443 #define SWAP_VALUES(CPPTYPE, TYPE) \
444 case FieldDescriptor::CPPTYPE_##CPPTYPE: \
445 std::swap(*MutableRaw<TYPE>(message1, field), \
446 *MutableRaw<TYPE>(message2, field)); \
447 break;
448
449 SWAP_VALUES(INT32 , int32 );
450 SWAP_VALUES(INT64 , int64 );
451 SWAP_VALUES(UINT32, uint32);
452 SWAP_VALUES(UINT64, uint64);
453 SWAP_VALUES(FLOAT , float );
454 SWAP_VALUES(DOUBLE, double);
455 SWAP_VALUES(BOOL , bool );
456 SWAP_VALUES(ENUM , int );
457 #undef SWAP_VALUES
458 case FieldDescriptor::CPPTYPE_MESSAGE:
459 if (GetArena(message1) == GetArena(message2)) {
460 std::swap(*MutableRaw<Message*>(message1, field),
461 *MutableRaw<Message*>(message2, field));
462 } else {
463 Message** sub_msg1 = MutableRaw<Message*>(message1, field);
464 Message** sub_msg2 = MutableRaw<Message*>(message2, field);
465 if (*sub_msg1 == NULL && *sub_msg2 == NULL) break;
466 if (*sub_msg1 && *sub_msg2) {
467 (*sub_msg1)->GetReflection()->Swap(*sub_msg1, *sub_msg2);
468 break;
469 }
470 if (*sub_msg1 == NULL) {
471 *sub_msg1 = (*sub_msg2)->New(message1->GetArena());
472 (*sub_msg1)->CopyFrom(**sub_msg2);
473 ClearField(message2, field);
474 } else {
475 *sub_msg2 = (*sub_msg1)->New(message2->GetArena());
476 (*sub_msg2)->CopyFrom(**sub_msg1);
477 ClearField(message1, field);
478 }
479 }
480 break;
481
482 case FieldDescriptor::CPPTYPE_STRING:
483 switch (field->options().ctype()) {
484 default: // TODO(kenton): Support other string reps.
485 case FieldOptions::STRING:
486 {
487 Arena* arena1 = GetArena(message1);
488 Arena* arena2 = GetArena(message2);
489 ArenaStringPtr* string1 =
490 MutableRaw<ArenaStringPtr>(message1, field);
491 ArenaStringPtr* string2 =
492 MutableRaw<ArenaStringPtr>(message2, field);
493 if (arena1 == arena2) {
494 string1->Swap(string2);
495 } else {
496 const string* default_ptr =
497 &DefaultRaw<ArenaStringPtr>(field).Get(NULL);
498 const string temp = string1->Get(default_ptr);
499 string1->Set(default_ptr, string2->Get(default_ptr), arena1);
500 string2->Set(default_ptr, temp, arena2);
501 }
502 }
503 break;
504 }
505 break;
506
507 default:
508 GOOGLE_LOG(FATAL) << "Unimplemented type: " << field->cpp_type();
509 }
510 }
511 }
512
SwapOneofField(Message * message1,Message * message2,const OneofDescriptor * oneof_descriptor) const513 void GeneratedMessageReflection::SwapOneofField(
514 Message* message1,
515 Message* message2,
516 const OneofDescriptor* oneof_descriptor) const {
517 uint32 oneof_case1 = GetOneofCase(*message1, oneof_descriptor);
518 uint32 oneof_case2 = GetOneofCase(*message2, oneof_descriptor);
519
520 int32 temp_int32;
521 int64 temp_int64;
522 uint32 temp_uint32;
523 uint64 temp_uint64;
524 float temp_float;
525 double temp_double;
526 bool temp_bool;
527 int temp_int;
528 Message* temp_message = NULL;
529 string temp_string;
530
531 // Stores message1's oneof field to a temp variable.
532 const FieldDescriptor* field1 = NULL;
533 if (oneof_case1 > 0) {
534 field1 = descriptor_->FindFieldByNumber(oneof_case1);
535 //oneof_descriptor->field(oneof_case1);
536 switch (field1->cpp_type()) {
537 #define GET_TEMP_VALUE(CPPTYPE, TYPE) \
538 case FieldDescriptor::CPPTYPE_##CPPTYPE: \
539 temp_##TYPE = GetField<TYPE>(*message1, field1); \
540 break;
541
542 GET_TEMP_VALUE(INT32 , int32 );
543 GET_TEMP_VALUE(INT64 , int64 );
544 GET_TEMP_VALUE(UINT32, uint32);
545 GET_TEMP_VALUE(UINT64, uint64);
546 GET_TEMP_VALUE(FLOAT , float );
547 GET_TEMP_VALUE(DOUBLE, double);
548 GET_TEMP_VALUE(BOOL , bool );
549 GET_TEMP_VALUE(ENUM , int );
550 #undef GET_TEMP_VALUE
551 case FieldDescriptor::CPPTYPE_MESSAGE:
552 temp_message = ReleaseMessage(message1, field1);
553 break;
554
555 case FieldDescriptor::CPPTYPE_STRING:
556 temp_string = GetString(*message1, field1);
557 break;
558
559 default:
560 GOOGLE_LOG(FATAL) << "Unimplemented type: " << field1->cpp_type();
561 }
562 }
563
564 // Sets message1's oneof field from the message2's oneof field.
565 if (oneof_case2 > 0) {
566 const FieldDescriptor* field2 =
567 descriptor_->FindFieldByNumber(oneof_case2);
568 switch (field2->cpp_type()) {
569 #define SET_ONEOF_VALUE1(CPPTYPE, TYPE) \
570 case FieldDescriptor::CPPTYPE_##CPPTYPE: \
571 SetField<TYPE>(message1, field2, GetField<TYPE>(*message2, field2)); \
572 break;
573
574 SET_ONEOF_VALUE1(INT32 , int32 );
575 SET_ONEOF_VALUE1(INT64 , int64 );
576 SET_ONEOF_VALUE1(UINT32, uint32);
577 SET_ONEOF_VALUE1(UINT64, uint64);
578 SET_ONEOF_VALUE1(FLOAT , float );
579 SET_ONEOF_VALUE1(DOUBLE, double);
580 SET_ONEOF_VALUE1(BOOL , bool );
581 SET_ONEOF_VALUE1(ENUM , int );
582 #undef SET_ONEOF_VALUE1
583 case FieldDescriptor::CPPTYPE_MESSAGE:
584 SetAllocatedMessage(message1,
585 ReleaseMessage(message2, field2),
586 field2);
587 break;
588
589 case FieldDescriptor::CPPTYPE_STRING:
590 SetString(message1, field2, GetString(*message2, field2));
591 break;
592
593 default:
594 GOOGLE_LOG(FATAL) << "Unimplemented type: " << field2->cpp_type();
595 }
596 } else {
597 ClearOneof(message1, oneof_descriptor);
598 }
599
600 // Sets message2's oneof field from the temp variable.
601 if (oneof_case1 > 0) {
602 switch (field1->cpp_type()) {
603 #define SET_ONEOF_VALUE2(CPPTYPE, TYPE) \
604 case FieldDescriptor::CPPTYPE_##CPPTYPE: \
605 SetField<TYPE>(message2, field1, temp_##TYPE); \
606 break;
607
608 SET_ONEOF_VALUE2(INT32 , int32 );
609 SET_ONEOF_VALUE2(INT64 , int64 );
610 SET_ONEOF_VALUE2(UINT32, uint32);
611 SET_ONEOF_VALUE2(UINT64, uint64);
612 SET_ONEOF_VALUE2(FLOAT , float );
613 SET_ONEOF_VALUE2(DOUBLE, double);
614 SET_ONEOF_VALUE2(BOOL , bool );
615 SET_ONEOF_VALUE2(ENUM , int );
616 #undef SET_ONEOF_VALUE2
617 case FieldDescriptor::CPPTYPE_MESSAGE:
618 SetAllocatedMessage(message2, temp_message, field1);
619 break;
620
621 case FieldDescriptor::CPPTYPE_STRING:
622 SetString(message2, field1, temp_string);
623 break;
624
625 default:
626 GOOGLE_LOG(FATAL) << "Unimplemented type: " << field1->cpp_type();
627 }
628 } else {
629 ClearOneof(message2, oneof_descriptor);
630 }
631 }
632
Swap(Message * message1,Message * message2) const633 void GeneratedMessageReflection::Swap(
634 Message* message1,
635 Message* message2) const {
636 if (message1 == message2) return;
637
638 // TODO(kenton): Other Reflection methods should probably check this too.
639 GOOGLE_CHECK_EQ(message1->GetReflection(), this)
640 << "First argument to Swap() (of type \""
641 << message1->GetDescriptor()->full_name()
642 << "\") is not compatible with this reflection object (which is for type \""
643 << descriptor_->full_name()
644 << "\"). Note that the exact same class is required; not just the same "
645 "descriptor.";
646 GOOGLE_CHECK_EQ(message2->GetReflection(), this)
647 << "Second argument to Swap() (of type \""
648 << message2->GetDescriptor()->full_name()
649 << "\") is not compatible with this reflection object (which is for type \""
650 << descriptor_->full_name()
651 << "\"). Note that the exact same class is required; not just the same "
652 "descriptor.";
653
654 // 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.
656 if (GetArena(message1) != GetArena(message2)) {
657 // Slow copy path.
658 // Use our arena as temp space, if available.
659 Message* temp = message1->New(GetArena(message1));
660 temp->MergeFrom(*message1);
661 message1->CopyFrom(*message2);
662 message2->CopyFrom(*temp);
663 if (GetArena(message1) == NULL) {
664 delete temp;
665 }
666 return;
667 }
668
669 if (has_bits_offset_ != -1) {
670 uint32* has_bits1 = MutableHasBits(message1);
671 uint32* has_bits2 = MutableHasBits(message2);
672 int has_bits_size = (descriptor_->field_count() + 31) / 32;
673
674 for (int i = 0; i < has_bits_size; i++) {
675 std::swap(has_bits1[i], has_bits2[i]);
676 }
677 }
678
679 for (int i = 0; i < descriptor_->field_count(); i++) {
680 const FieldDescriptor* field = descriptor_->field(i);
681 if (!field->containing_oneof()) {
682 SwapField(message1, message2, field);
683 }
684 }
685
686 for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
687 SwapOneofField(message1, message2, descriptor_->oneof_decl(i));
688 }
689
690 if (extensions_offset_ != -1) {
691 MutableExtensionSet(message1)->Swap(MutableExtensionSet(message2));
692 }
693
694 MutableUnknownFields(message1)->Swap(MutableUnknownFields(message2));
695 }
696
SwapFields(Message * message1,Message * message2,const vector<const FieldDescriptor * > & fields) const697 void GeneratedMessageReflection::SwapFields(
698 Message* message1,
699 Message* message2,
700 const vector<const FieldDescriptor*>& fields) const {
701 if (message1 == message2) return;
702
703 // TODO(kenton): Other Reflection methods should probably check this too.
704 GOOGLE_CHECK_EQ(message1->GetReflection(), this)
705 << "First argument to SwapFields() (of type \""
706 << message1->GetDescriptor()->full_name()
707 << "\") is not compatible with this reflection object (which is for type \""
708 << descriptor_->full_name()
709 << "\"). Note that the exact same class is required; not just the same "
710 "descriptor.";
711 GOOGLE_CHECK_EQ(message2->GetReflection(), this)
712 << "Second argument to SwapFields() (of type \""
713 << message2->GetDescriptor()->full_name()
714 << "\") is not compatible with this reflection object (which is for type \""
715 << descriptor_->full_name()
716 << "\"). Note that the exact same class is required; not just the same "
717 "descriptor.";
718
719 std::set<int> swapped_oneof;
720
721 for (int i = 0; i < fields.size(); i++) {
722 const FieldDescriptor* field = fields[i];
723 if (field->is_extension()) {
724 MutableExtensionSet(message1)->SwapExtension(
725 MutableExtensionSet(message2),
726 field->number());
727 } else {
728 if (field->containing_oneof()) {
729 int oneof_index = field->containing_oneof()->index();
730 // Only swap the oneof field once.
731 if (swapped_oneof.find(oneof_index) != swapped_oneof.end()) {
732 continue;
733 }
734 swapped_oneof.insert(oneof_index);
735 SwapOneofField(message1, message2, field->containing_oneof());
736 } else {
737 // Swap has bit.
738 SwapBit(message1, message2, field);
739 // Swap field.
740 SwapField(message1, message2, field);
741 }
742 }
743 }
744 }
745
746 // -------------------------------------------------------------------
747
HasField(const Message & message,const FieldDescriptor * field) const748 bool GeneratedMessageReflection::HasField(const Message& message,
749 const FieldDescriptor* field) const {
750 USAGE_CHECK_MESSAGE_TYPE(HasField);
751 USAGE_CHECK_SINGULAR(HasField);
752
753 if (field->is_extension()) {
754 return GetExtensionSet(message).Has(field->number());
755 } else {
756 if (field->containing_oneof()) {
757 return HasOneofField(message, field);
758 } else {
759 return HasBit(message, field);
760 }
761 }
762 }
763
FieldSize(const Message & message,const FieldDescriptor * field) const764 int GeneratedMessageReflection::FieldSize(const Message& message,
765 const FieldDescriptor* field) const {
766 USAGE_CHECK_MESSAGE_TYPE(FieldSize);
767 USAGE_CHECK_REPEATED(FieldSize);
768
769 if (field->is_extension()) {
770 return GetExtensionSet(message).ExtensionSize(field->number());
771 } else {
772 switch (field->cpp_type()) {
773 #define HANDLE_TYPE(UPPERCASE, LOWERCASE) \
774 case FieldDescriptor::CPPTYPE_##UPPERCASE : \
775 return GetRaw<RepeatedField<LOWERCASE> >(message, field).size()
776
777 HANDLE_TYPE( INT32, int32);
778 HANDLE_TYPE( INT64, int64);
779 HANDLE_TYPE(UINT32, uint32);
780 HANDLE_TYPE(UINT64, uint64);
781 HANDLE_TYPE(DOUBLE, double);
782 HANDLE_TYPE( FLOAT, float);
783 HANDLE_TYPE( BOOL, bool);
784 HANDLE_TYPE( ENUM, int);
785 #undef HANDLE_TYPE
786
787 case FieldDescriptor::CPPTYPE_STRING:
788 case FieldDescriptor::CPPTYPE_MESSAGE:
789 if (IsMapFieldInApi(field)) {
790 return GetRaw<MapFieldBase>(message, field).GetRepeatedField().size();
791 } else {
792 return GetRaw<RepeatedPtrFieldBase>(message, field).size();
793 }
794 }
795
796 GOOGLE_LOG(FATAL) << "Can't get here.";
797 return 0;
798 }
799 }
800
ClearField(Message * message,const FieldDescriptor * field) const801 void GeneratedMessageReflection::ClearField(
802 Message* message, const FieldDescriptor* field) const {
803 USAGE_CHECK_MESSAGE_TYPE(ClearField);
804
805 if (field->is_extension()) {
806 MutableExtensionSet(message)->ClearExtension(field->number());
807 } else if (!field->is_repeated()) {
808 if (field->containing_oneof()) {
809 ClearOneofField(message, field);
810 return;
811 }
812
813 if (HasBit(*message, field)) {
814 ClearBit(message, field);
815
816 // We need to set the field back to its default value.
817 switch (field->cpp_type()) {
818 #define CLEAR_TYPE(CPPTYPE, TYPE) \
819 case FieldDescriptor::CPPTYPE_##CPPTYPE: \
820 *MutableRaw<TYPE>(message, field) = \
821 field->default_value_##TYPE(); \
822 break;
823
824 CLEAR_TYPE(INT32 , int32 );
825 CLEAR_TYPE(INT64 , int64 );
826 CLEAR_TYPE(UINT32, uint32);
827 CLEAR_TYPE(UINT64, uint64);
828 CLEAR_TYPE(FLOAT , float );
829 CLEAR_TYPE(DOUBLE, double);
830 CLEAR_TYPE(BOOL , bool );
831 #undef CLEAR_TYPE
832
833 case FieldDescriptor::CPPTYPE_ENUM:
834 *MutableRaw<int>(message, field) =
835 field->default_value_enum()->number();
836 break;
837
838 case FieldDescriptor::CPPTYPE_STRING: {
839 switch (field->options().ctype()) {
840 default: // TODO(kenton): Support other string reps.
841 case FieldOptions::STRING: {
842 const string* default_ptr =
843 &DefaultRaw<ArenaStringPtr>(field).Get(NULL);
844 MutableRaw<ArenaStringPtr>(message, field)->Destroy(default_ptr,
845 GetArena(message));
846 break;
847 }
848 }
849 break;
850 }
851
852 case FieldDescriptor::CPPTYPE_MESSAGE:
853 if (has_bits_offset_ == -1) {
854 // Proto3 does not have has-bits and we need to set a message field
855 // to NULL in order to indicate its un-presence.
856 if (GetArena(message) == NULL) {
857 delete *MutableRaw<Message*>(message, field);
858 }
859 *MutableRaw<Message*>(message, field) = NULL;
860 } else {
861 (*MutableRaw<Message*>(message, field))->Clear();
862 }
863 break;
864 }
865 }
866 } else {
867 switch (field->cpp_type()) {
868 #define HANDLE_TYPE(UPPERCASE, LOWERCASE) \
869 case FieldDescriptor::CPPTYPE_##UPPERCASE : \
870 MutableRaw<RepeatedField<LOWERCASE> >(message, field)->Clear(); \
871 break
872
873 HANDLE_TYPE( INT32, int32);
874 HANDLE_TYPE( INT64, int64);
875 HANDLE_TYPE(UINT32, uint32);
876 HANDLE_TYPE(UINT64, uint64);
877 HANDLE_TYPE(DOUBLE, double);
878 HANDLE_TYPE( FLOAT, float);
879 HANDLE_TYPE( BOOL, bool);
880 HANDLE_TYPE( ENUM, int);
881 #undef HANDLE_TYPE
882
883 case FieldDescriptor::CPPTYPE_STRING: {
884 switch (field->options().ctype()) {
885 default: // TODO(kenton): Support other string reps.
886 case FieldOptions::STRING:
887 MutableRaw<RepeatedPtrField<string> >(message, field)->Clear();
888 break;
889 }
890 break;
891 }
892
893 case FieldDescriptor::CPPTYPE_MESSAGE: {
894 if (IsMapFieldInApi(field)) {
895 MutableRaw<MapFieldBase>(message, field)
896 ->MutableRepeatedField()
897 ->Clear<GenericTypeHandler<Message> >();
898 } else {
899 // We don't know which subclass of RepeatedPtrFieldBase the type is,
900 // so we use RepeatedPtrFieldBase directly.
901 MutableRaw<RepeatedPtrFieldBase>(message, field)
902 ->Clear<GenericTypeHandler<Message> >();
903 }
904 break;
905 }
906 }
907 }
908 }
909
RemoveLast(Message * message,const FieldDescriptor * field) const910 void GeneratedMessageReflection::RemoveLast(
911 Message* message,
912 const FieldDescriptor* field) const {
913 USAGE_CHECK_MESSAGE_TYPE(RemoveLast);
914 USAGE_CHECK_REPEATED(RemoveLast);
915
916 if (field->is_extension()) {
917 MutableExtensionSet(message)->RemoveLast(field->number());
918 } else {
919 switch (field->cpp_type()) {
920 #define HANDLE_TYPE(UPPERCASE, LOWERCASE) \
921 case FieldDescriptor::CPPTYPE_##UPPERCASE : \
922 MutableRaw<RepeatedField<LOWERCASE> >(message, field)->RemoveLast(); \
923 break
924
925 HANDLE_TYPE( INT32, int32);
926 HANDLE_TYPE( INT64, int64);
927 HANDLE_TYPE(UINT32, uint32);
928 HANDLE_TYPE(UINT64, uint64);
929 HANDLE_TYPE(DOUBLE, double);
930 HANDLE_TYPE( FLOAT, float);
931 HANDLE_TYPE( BOOL, bool);
932 HANDLE_TYPE( ENUM, int);
933 #undef HANDLE_TYPE
934
935 case FieldDescriptor::CPPTYPE_STRING:
936 switch (field->options().ctype()) {
937 default: // TODO(kenton): Support other string reps.
938 case FieldOptions::STRING:
939 MutableRaw<RepeatedPtrField<string> >(message, field)->RemoveLast();
940 break;
941 }
942 break;
943
944 case FieldDescriptor::CPPTYPE_MESSAGE:
945 if (IsMapFieldInApi(field)) {
946 MutableRaw<MapFieldBase>(message, field)
947 ->MutableRepeatedField()
948 ->RemoveLast<GenericTypeHandler<Message> >();
949 } else {
950 MutableRaw<RepeatedPtrFieldBase>(message, field)
951 ->RemoveLast<GenericTypeHandler<Message> >();
952 }
953 break;
954 }
955 }
956 }
957
ReleaseLast(Message * message,const FieldDescriptor * field) const958 Message* GeneratedMessageReflection::ReleaseLast(
959 Message* message,
960 const FieldDescriptor* field) const {
961 USAGE_CHECK_ALL(ReleaseLast, REPEATED, MESSAGE);
962
963 if (field->is_extension()) {
964 return static_cast<Message*>(
965 MutableExtensionSet(message)->ReleaseLast(field->number()));
966 } else {
967 if (IsMapFieldInApi(field)) {
968 return MutableRaw<MapFieldBase>(message, field)
969 ->MutableRepeatedField()
970 ->ReleaseLast<GenericTypeHandler<Message> >();
971 } else {
972 return MutableRaw<RepeatedPtrFieldBase>(message, field)
973 ->ReleaseLast<GenericTypeHandler<Message> >();
974 }
975 }
976 }
977
SwapElements(Message * message,const FieldDescriptor * field,int index1,int index2) const978 void GeneratedMessageReflection::SwapElements(
979 Message* message,
980 const FieldDescriptor* field,
981 int index1,
982 int index2) const {
983 USAGE_CHECK_MESSAGE_TYPE(Swap);
984 USAGE_CHECK_REPEATED(Swap);
985
986 if (field->is_extension()) {
987 MutableExtensionSet(message)->SwapElements(field->number(), index1, index2);
988 } else {
989 switch (field->cpp_type()) {
990 #define HANDLE_TYPE(UPPERCASE, LOWERCASE) \
991 case FieldDescriptor::CPPTYPE_##UPPERCASE : \
992 MutableRaw<RepeatedField<LOWERCASE> >(message, field) \
993 ->SwapElements(index1, index2); \
994 break
995
996 HANDLE_TYPE( INT32, int32);
997 HANDLE_TYPE( INT64, int64);
998 HANDLE_TYPE(UINT32, uint32);
999 HANDLE_TYPE(UINT64, uint64);
1000 HANDLE_TYPE(DOUBLE, double);
1001 HANDLE_TYPE( FLOAT, float);
1002 HANDLE_TYPE( BOOL, bool);
1003 HANDLE_TYPE( ENUM, int);
1004 #undef HANDLE_TYPE
1005
1006 case FieldDescriptor::CPPTYPE_STRING:
1007 case FieldDescriptor::CPPTYPE_MESSAGE:
1008 if (IsMapFieldInApi(field)) {
1009 MutableRaw<MapFieldBase>(message, field)
1010 ->MutableRepeatedField()
1011 ->SwapElements(index1, index2);
1012 } else {
1013 MutableRaw<RepeatedPtrFieldBase>(message, field)
1014 ->SwapElements(index1, index2);
1015 }
1016 break;
1017 }
1018 }
1019 }
1020
1021 namespace {
1022 // Comparison functor for sorting FieldDescriptors by field number.
1023 struct FieldNumberSorter {
operator ()google::protobuf::internal::__anonf81e794e0511::FieldNumberSorter1024 bool operator()(const FieldDescriptor* left,
1025 const FieldDescriptor* right) const {
1026 return left->number() < right->number();
1027 }
1028 };
1029 } // namespace
1030
ListFields(const Message & message,vector<const FieldDescriptor * > * output) const1031 void GeneratedMessageReflection::ListFields(
1032 const Message& message,
1033 vector<const FieldDescriptor*>* output) const {
1034 output->clear();
1035
1036 // Optimization: The default instance never has any fields set.
1037 if (&message == default_instance_) return;
1038
1039 output->reserve(descriptor_->field_count());
1040 for (int i = 0; i < descriptor_->field_count(); i++) {
1041 const FieldDescriptor* field = descriptor_->field(i);
1042 if (field->is_repeated()) {
1043 if (FieldSize(message, field) > 0) {
1044 output->push_back(field);
1045 }
1046 } else {
1047 if (field->containing_oneof()) {
1048 if (HasOneofField(message, field)) {
1049 output->push_back(field);
1050 }
1051 } else if (HasBit(message, field)) {
1052 output->push_back(field);
1053 }
1054 }
1055 }
1056
1057 if (extensions_offset_ != -1) {
1058 GetExtensionSet(message).AppendToList(descriptor_, descriptor_pool_,
1059 output);
1060 }
1061
1062 // ListFields() must sort output by field number.
1063 std::sort(output->begin(), output->end(), FieldNumberSorter());
1064 }
1065
1066 // -------------------------------------------------------------------
1067
1068 #undef DEFINE_PRIMITIVE_ACCESSORS
1069 #define DEFINE_PRIMITIVE_ACCESSORS(TYPENAME, TYPE, PASSTYPE, CPPTYPE) \
1070 PASSTYPE GeneratedMessageReflection::Get##TYPENAME( \
1071 const Message& message, const FieldDescriptor* field) const { \
1072 USAGE_CHECK_ALL(Get##TYPENAME, SINGULAR, CPPTYPE); \
1073 if (field->is_extension()) { \
1074 return GetExtensionSet(message).Get##TYPENAME( \
1075 field->number(), field->default_value_##PASSTYPE()); \
1076 } else { \
1077 return GetField<TYPE>(message, field); \
1078 } \
1079 } \
1080 \
1081 void GeneratedMessageReflection::Set##TYPENAME( \
1082 Message* message, const FieldDescriptor* field, \
1083 PASSTYPE value) const { \
1084 USAGE_CHECK_ALL(Set##TYPENAME, SINGULAR, CPPTYPE); \
1085 if (field->is_extension()) { \
1086 return MutableExtensionSet(message)->Set##TYPENAME( \
1087 field->number(), field->type(), value, field); \
1088 } else { \
1089 SetField<TYPE>(message, field, value); \
1090 } \
1091 } \
1092 \
1093 PASSTYPE GeneratedMessageReflection::GetRepeated##TYPENAME( \
1094 const Message& message, \
1095 const FieldDescriptor* field, int index) const { \
1096 USAGE_CHECK_ALL(GetRepeated##TYPENAME, REPEATED, CPPTYPE); \
1097 if (field->is_extension()) { \
1098 return GetExtensionSet(message).GetRepeated##TYPENAME( \
1099 field->number(), index); \
1100 } else { \
1101 return GetRepeatedField<TYPE>(message, field, index); \
1102 } \
1103 } \
1104 \
1105 void GeneratedMessageReflection::SetRepeated##TYPENAME( \
1106 Message* message, const FieldDescriptor* field, \
1107 int index, PASSTYPE value) const { \
1108 USAGE_CHECK_ALL(SetRepeated##TYPENAME, REPEATED, CPPTYPE); \
1109 if (field->is_extension()) { \
1110 MutableExtensionSet(message)->SetRepeated##TYPENAME( \
1111 field->number(), index, value); \
1112 } else { \
1113 SetRepeatedField<TYPE>(message, field, index, value); \
1114 } \
1115 } \
1116 \
1117 void GeneratedMessageReflection::Add##TYPENAME( \
1118 Message* message, const FieldDescriptor* field, \
1119 PASSTYPE value) const { \
1120 USAGE_CHECK_ALL(Add##TYPENAME, REPEATED, CPPTYPE); \
1121 if (field->is_extension()) { \
1122 MutableExtensionSet(message)->Add##TYPENAME( \
1123 field->number(), field->type(), field->options().packed(), value, \
1124 field); \
1125 } else { \
1126 AddField<TYPE>(message, field, value); \
1127 } \
1128 }
1129
DEFINE_PRIMITIVE_ACCESSORS(Int32,int32,int32,INT32)1130 DEFINE_PRIMITIVE_ACCESSORS(Int32 , int32 , int32 , INT32 )
1131 DEFINE_PRIMITIVE_ACCESSORS(Int64 , int64 , int64 , INT64 )
1132 DEFINE_PRIMITIVE_ACCESSORS(UInt32, uint32, uint32, UINT32)
1133 DEFINE_PRIMITIVE_ACCESSORS(UInt64, uint64, uint64, UINT64)
1134 DEFINE_PRIMITIVE_ACCESSORS(Float , float , float , FLOAT )
1135 DEFINE_PRIMITIVE_ACCESSORS(Double, double, double, DOUBLE)
1136 DEFINE_PRIMITIVE_ACCESSORS(Bool , bool , bool , BOOL )
1137 #undef DEFINE_PRIMITIVE_ACCESSORS
1138
1139 // -------------------------------------------------------------------
1140
1141 string GeneratedMessageReflection::GetString(
1142 const Message& message, const FieldDescriptor* field) const {
1143 USAGE_CHECK_ALL(GetString, SINGULAR, STRING);
1144 if (field->is_extension()) {
1145 return GetExtensionSet(message).GetString(field->number(),
1146 field->default_value_string());
1147 } else {
1148 switch (field->options().ctype()) {
1149 default: // TODO(kenton): Support other string reps.
1150 case FieldOptions::STRING: {
1151 const string* default_ptr =
1152 &DefaultRaw<ArenaStringPtr>(field).Get(NULL);
1153 return GetField<ArenaStringPtr>(message, field).Get(default_ptr);
1154 }
1155 }
1156
1157 GOOGLE_LOG(FATAL) << "Can't get here.";
1158 return GetEmptyString(); // Make compiler happy.
1159 }
1160 }
1161
GetStringReference(const Message & message,const FieldDescriptor * field,string * scratch) const1162 const string& GeneratedMessageReflection::GetStringReference(
1163 const Message& message,
1164 const FieldDescriptor* field, string* scratch) const {
1165 USAGE_CHECK_ALL(GetStringReference, SINGULAR, STRING);
1166 if (field->is_extension()) {
1167 return GetExtensionSet(message).GetString(field->number(),
1168 field->default_value_string());
1169 } else {
1170 switch (field->options().ctype()) {
1171 default: // TODO(kenton): Support other string reps.
1172 case FieldOptions::STRING: {
1173 const string* default_ptr =
1174 &DefaultRaw<ArenaStringPtr>(field).Get(NULL);
1175 return GetField<ArenaStringPtr>(message, field).Get(default_ptr);
1176 }
1177 }
1178
1179 GOOGLE_LOG(FATAL) << "Can't get here.";
1180 return GetEmptyString(); // Make compiler happy.
1181 }
1182 }
1183
1184
SetString(Message * message,const FieldDescriptor * field,const string & value) const1185 void GeneratedMessageReflection::SetString(
1186 Message* message, const FieldDescriptor* field,
1187 const string& value) const {
1188 USAGE_CHECK_ALL(SetString, SINGULAR, STRING);
1189 if (field->is_extension()) {
1190 return MutableExtensionSet(message)->SetString(field->number(),
1191 field->type(), value, field);
1192 } else {
1193 switch (field->options().ctype()) {
1194 default: // TODO(kenton): Support other string reps.
1195 case FieldOptions::STRING: {
1196 const string* default_ptr =
1197 &DefaultRaw<ArenaStringPtr>(field).Get(NULL);
1198 if (field->containing_oneof() && !HasOneofField(*message, field)) {
1199 ClearOneof(message, field->containing_oneof());
1200 MutableField<ArenaStringPtr>(message, field)->UnsafeSetDefault(
1201 default_ptr);
1202 }
1203 MutableField<ArenaStringPtr>(message, field)->Set(default_ptr,
1204 value, GetArena(message));
1205 break;
1206 }
1207 }
1208 }
1209 }
1210
1211
GetRepeatedString(const Message & message,const FieldDescriptor * field,int index) const1212 string GeneratedMessageReflection::GetRepeatedString(
1213 const Message& message, const FieldDescriptor* field, int index) const {
1214 USAGE_CHECK_ALL(GetRepeatedString, REPEATED, STRING);
1215 if (field->is_extension()) {
1216 return GetExtensionSet(message).GetRepeatedString(field->number(), index);
1217 } else {
1218 switch (field->options().ctype()) {
1219 default: // TODO(kenton): Support other string reps.
1220 case FieldOptions::STRING:
1221 return GetRepeatedPtrField<string>(message, field, index);
1222 }
1223
1224 GOOGLE_LOG(FATAL) << "Can't get here.";
1225 return GetEmptyString(); // Make compiler happy.
1226 }
1227 }
1228
GetRepeatedStringReference(const Message & message,const FieldDescriptor * field,int index,string * scratch) const1229 const string& GeneratedMessageReflection::GetRepeatedStringReference(
1230 const Message& message, const FieldDescriptor* field,
1231 int index, string* scratch) const {
1232 USAGE_CHECK_ALL(GetRepeatedStringReference, REPEATED, STRING);
1233 if (field->is_extension()) {
1234 return GetExtensionSet(message).GetRepeatedString(field->number(), index);
1235 } else {
1236 switch (field->options().ctype()) {
1237 default: // TODO(kenton): Support other string reps.
1238 case FieldOptions::STRING:
1239 return GetRepeatedPtrField<string>(message, field, index);
1240 }
1241
1242 GOOGLE_LOG(FATAL) << "Can't get here.";
1243 return GetEmptyString(); // Make compiler happy.
1244 }
1245 }
1246
1247
SetRepeatedString(Message * message,const FieldDescriptor * field,int index,const string & value) const1248 void GeneratedMessageReflection::SetRepeatedString(
1249 Message* message, const FieldDescriptor* field,
1250 int index, const string& value) const {
1251 USAGE_CHECK_ALL(SetRepeatedString, REPEATED, STRING);
1252 if (field->is_extension()) {
1253 MutableExtensionSet(message)->SetRepeatedString(
1254 field->number(), index, value);
1255 } else {
1256 switch (field->options().ctype()) {
1257 default: // TODO(kenton): Support other string reps.
1258 case FieldOptions::STRING:
1259 *MutableRepeatedField<string>(message, field, index) = value;
1260 break;
1261 }
1262 }
1263 }
1264
1265
AddString(Message * message,const FieldDescriptor * field,const string & value) const1266 void GeneratedMessageReflection::AddString(
1267 Message* message, const FieldDescriptor* field,
1268 const string& value) const {
1269 USAGE_CHECK_ALL(AddString, REPEATED, STRING);
1270 if (field->is_extension()) {
1271 MutableExtensionSet(message)->AddString(field->number(),
1272 field->type(), value, field);
1273 } else {
1274 switch (field->options().ctype()) {
1275 default: // TODO(kenton): Support other string reps.
1276 case FieldOptions::STRING:
1277 *AddField<string>(message, field) = value;
1278 break;
1279 }
1280 }
1281 }
1282
1283
1284 // -------------------------------------------------------------------
1285
CreateUnknownEnumValues(const FileDescriptor * file)1286 inline bool CreateUnknownEnumValues(const FileDescriptor* file) {
1287 return file->syntax() == FileDescriptor::SYNTAX_PROTO3;
1288 }
1289
GetEnum(const Message & message,const FieldDescriptor * field) const1290 const EnumValueDescriptor* GeneratedMessageReflection::GetEnum(
1291 const Message& message, const FieldDescriptor* field) const {
1292 // Usage checked by GetEnumValue.
1293 int value = GetEnumValue(message, field);
1294 return field->enum_type()->FindValueByNumberCreatingIfUnknown(value);
1295 }
1296
GetEnumValue(const Message & message,const FieldDescriptor * field) const1297 int GeneratedMessageReflection::GetEnumValue(
1298 const Message& message, const FieldDescriptor* field) const {
1299 USAGE_CHECK_ALL(GetEnumValue, SINGULAR, ENUM);
1300
1301 int32 value;
1302 if (field->is_extension()) {
1303 value = GetExtensionSet(message).GetEnum(
1304 field->number(), field->default_value_enum()->number());
1305 } else {
1306 value = GetField<int>(message, field);
1307 }
1308 return value;
1309 }
1310
SetEnum(Message * message,const FieldDescriptor * field,const EnumValueDescriptor * value) const1311 void GeneratedMessageReflection::SetEnum(
1312 Message* message, const FieldDescriptor* field,
1313 const EnumValueDescriptor* value) const {
1314 // Usage checked by SetEnumValue.
1315 USAGE_CHECK_ENUM_VALUE(SetEnum);
1316 SetEnumValueInternal(message, field, value->number());
1317 }
1318
SetEnumValue(Message * message,const FieldDescriptor * field,int value) const1319 void GeneratedMessageReflection::SetEnumValue(
1320 Message* message, const FieldDescriptor* field,
1321 int value) const {
1322 USAGE_CHECK_ALL(SetEnumValue, SINGULAR, ENUM);
1323 if (!CreateUnknownEnumValues(descriptor_->file())) {
1324 // Check that the value is valid if we don't support direct storage of
1325 // unknown enum values.
1326 const EnumValueDescriptor* value_desc =
1327 field->enum_type()->FindValueByNumber(value);
1328 if (value_desc == NULL) {
1329 GOOGLE_LOG(DFATAL) << "SetEnumValue accepts only valid integer values: value "
1330 << value << " unexpected for field " << field->full_name();
1331 // In production builds, DFATAL will not terminate the program, so we have
1332 // to do something reasonable: just set the default value.
1333 value = field->default_value_enum()->number();
1334 }
1335 }
1336 SetEnumValueInternal(message, field, value);
1337 }
1338
SetEnumValueInternal(Message * message,const FieldDescriptor * field,int value) const1339 void GeneratedMessageReflection::SetEnumValueInternal(
1340 Message* message, const FieldDescriptor* field,
1341 int value) const {
1342 if (field->is_extension()) {
1343 MutableExtensionSet(message)->SetEnum(field->number(), field->type(),
1344 value, field);
1345 } else {
1346 SetField<int>(message, field, value);
1347 }
1348 }
1349
GetRepeatedEnum(const Message & message,const FieldDescriptor * field,int index) const1350 const EnumValueDescriptor* GeneratedMessageReflection::GetRepeatedEnum(
1351 const Message& message, const FieldDescriptor* field, int index) const {
1352 // Usage checked by GetRepeatedEnumValue.
1353 int value = GetRepeatedEnumValue(message, field, index);
1354 return field->enum_type()->FindValueByNumberCreatingIfUnknown(value);
1355 }
1356
GetRepeatedEnumValue(const Message & message,const FieldDescriptor * field,int index) const1357 int GeneratedMessageReflection::GetRepeatedEnumValue(
1358 const Message& message, const FieldDescriptor* field, int index) const {
1359 USAGE_CHECK_ALL(GetRepeatedEnumValue, REPEATED, ENUM);
1360
1361 int value;
1362 if (field->is_extension()) {
1363 value = GetExtensionSet(message).GetRepeatedEnum(field->number(), index);
1364 } else {
1365 value = GetRepeatedField<int>(message, field, index);
1366 }
1367 return value;
1368 }
1369
SetRepeatedEnum(Message * message,const FieldDescriptor * field,int index,const EnumValueDescriptor * value) const1370 void GeneratedMessageReflection::SetRepeatedEnum(
1371 Message* message,
1372 const FieldDescriptor* field, int index,
1373 const EnumValueDescriptor* value) const {
1374 // Usage checked by SetRepeatedEnumValue.
1375 USAGE_CHECK_ENUM_VALUE(SetRepeatedEnum);
1376 SetRepeatedEnumValueInternal(message, field, index, value->number());
1377 }
1378
SetRepeatedEnumValue(Message * message,const FieldDescriptor * field,int index,int value) const1379 void GeneratedMessageReflection::SetRepeatedEnumValue(
1380 Message* message,
1381 const FieldDescriptor* field, int index,
1382 int value) const {
1383 USAGE_CHECK_ALL(SetRepeatedEnum, REPEATED, ENUM);
1384 if (!CreateUnknownEnumValues(descriptor_->file())) {
1385 // Check that the value is valid if we don't support direct storage of
1386 // unknown enum values.
1387 const EnumValueDescriptor* value_desc =
1388 field->enum_type()->FindValueByNumber(value);
1389 if (value_desc == NULL) {
1390 GOOGLE_LOG(DFATAL) << "SetRepeatedEnumValue accepts only valid integer values: "
1391 << "value " << value << " unexpected for field "
1392 << field->full_name();
1393 // In production builds, DFATAL will not terminate the program, so we have
1394 // to do something reasonable: just set the default value.
1395 value = field->default_value_enum()->number();
1396 }
1397 }
1398 SetRepeatedEnumValueInternal(message, field, index, value);
1399 }
1400
SetRepeatedEnumValueInternal(Message * message,const FieldDescriptor * field,int index,int value) const1401 void GeneratedMessageReflection::SetRepeatedEnumValueInternal(
1402 Message* message,
1403 const FieldDescriptor* field, int index,
1404 int value) const {
1405 if (field->is_extension()) {
1406 MutableExtensionSet(message)->SetRepeatedEnum(
1407 field->number(), index, value);
1408 } else {
1409 SetRepeatedField<int>(message, field, index, value);
1410 }
1411 }
1412
AddEnum(Message * message,const FieldDescriptor * field,const EnumValueDescriptor * value) const1413 void GeneratedMessageReflection::AddEnum(
1414 Message* message, const FieldDescriptor* field,
1415 const EnumValueDescriptor* value) const {
1416 // Usage checked by AddEnumValue.
1417 USAGE_CHECK_ENUM_VALUE(AddEnum);
1418 AddEnumValueInternal(message, field, value->number());
1419 }
1420
AddEnumValue(Message * message,const FieldDescriptor * field,int value) const1421 void GeneratedMessageReflection::AddEnumValue(
1422 Message* message, const FieldDescriptor* field,
1423 int value) const {
1424 USAGE_CHECK_ALL(AddEnum, REPEATED, ENUM);
1425 if (!CreateUnknownEnumValues(descriptor_->file())) {
1426 // Check that the value is valid if we don't support direct storage of
1427 // unknown enum values.
1428 const EnumValueDescriptor* value_desc =
1429 field->enum_type()->FindValueByNumber(value);
1430 if (value_desc == NULL) {
1431 GOOGLE_LOG(DFATAL) << "AddEnumValue accepts only valid integer values: value "
1432 << value << " unexpected for field " << field->full_name();
1433 // In production builds, DFATAL will not terminate the program, so we have
1434 // to do something reasonable: just set the default value.
1435 value = field->default_value_enum()->number();
1436 }
1437 }
1438 AddEnumValueInternal(message, field, value);
1439 }
1440
AddEnumValueInternal(Message * message,const FieldDescriptor * field,int value) const1441 void GeneratedMessageReflection::AddEnumValueInternal(
1442 Message* message, const FieldDescriptor* field,
1443 int value) const {
1444 if (field->is_extension()) {
1445 MutableExtensionSet(message)->AddEnum(field->number(), field->type(),
1446 field->options().packed(),
1447 value, field);
1448 } else {
1449 AddField<int>(message, field, value);
1450 }
1451 }
1452
1453 // -------------------------------------------------------------------
1454
GetMessage(const Message & message,const FieldDescriptor * field,MessageFactory * factory) const1455 const Message& GeneratedMessageReflection::GetMessage(
1456 const Message& message, const FieldDescriptor* field,
1457 MessageFactory* factory) const {
1458 USAGE_CHECK_ALL(GetMessage, SINGULAR, MESSAGE);
1459
1460 if (factory == NULL) factory = message_factory_;
1461
1462 if (field->is_extension()) {
1463 return static_cast<const Message&>(
1464 GetExtensionSet(message).GetMessage(
1465 field->number(), field->message_type(), factory));
1466 } else {
1467 const Message* result;
1468 result = GetRaw<const Message*>(message, field);
1469 if (result == NULL) {
1470 result = DefaultRaw<const Message*>(field);
1471 }
1472 return *result;
1473 }
1474 }
1475
MutableMessage(Message * message,const FieldDescriptor * field,MessageFactory * factory) const1476 Message* GeneratedMessageReflection::MutableMessage(
1477 Message* message, const FieldDescriptor* field,
1478 MessageFactory* factory) const {
1479 USAGE_CHECK_ALL(MutableMessage, SINGULAR, MESSAGE);
1480
1481 if (factory == NULL) factory = message_factory_;
1482
1483 if (field->is_extension()) {
1484 return static_cast<Message*>(
1485 MutableExtensionSet(message)->MutableMessage(field, factory));
1486 } else {
1487 Message* result;
1488 Message** result_holder = MutableRaw<Message*>(message, field);
1489
1490 if (field->containing_oneof()) {
1491 if (!HasOneofField(*message, field)) {
1492 ClearOneof(message, field->containing_oneof());
1493 result_holder = MutableField<Message*>(message, field);
1494 const Message* default_message = DefaultRaw<const Message*>(field);
1495 *result_holder = default_message->New(message->GetArena());
1496 }
1497 } else {
1498 SetBit(message, field);
1499 }
1500
1501 if (*result_holder == NULL) {
1502 const Message* default_message = DefaultRaw<const Message*>(field);
1503 *result_holder = default_message->New(message->GetArena());
1504 }
1505 result = *result_holder;
1506 return result;
1507 }
1508 }
1509
UnsafeArenaSetAllocatedMessage(Message * message,Message * sub_message,const FieldDescriptor * field) const1510 void GeneratedMessageReflection::UnsafeArenaSetAllocatedMessage(
1511 Message* message,
1512 Message* sub_message,
1513 const FieldDescriptor* field) const {
1514 USAGE_CHECK_ALL(SetAllocatedMessage, SINGULAR, MESSAGE);
1515
1516 if (field->is_extension()) {
1517 MutableExtensionSet(message)->SetAllocatedMessage(
1518 field->number(), field->type(), field, sub_message);
1519 } else {
1520 if (field->containing_oneof()) {
1521 if (sub_message == NULL) {
1522 ClearOneof(message, field->containing_oneof());
1523 return;
1524 }
1525 ClearOneof(message, field->containing_oneof());
1526 *MutableRaw<Message*>(message, field) = sub_message;
1527 SetOneofCase(message, field);
1528 return;
1529 }
1530
1531 if (sub_message == NULL) {
1532 ClearBit(message, field);
1533 } else {
1534 SetBit(message, field);
1535 }
1536 Message** sub_message_holder = MutableRaw<Message*>(message, field);
1537 if (GetArena(message) == NULL) {
1538 delete *sub_message_holder;
1539 }
1540 *sub_message_holder = sub_message;
1541 }
1542 }
1543
SetAllocatedMessage(Message * message,Message * sub_message,const FieldDescriptor * field) const1544 void GeneratedMessageReflection::SetAllocatedMessage(
1545 Message* message,
1546 Message* sub_message,
1547 const FieldDescriptor* field) const {
1548 // If message and sub-message are in different memory ownership domains
1549 // (different arenas, or one is on heap and one is not), then we may need to
1550 // do a copy.
1551 if (sub_message != NULL &&
1552 sub_message->GetArena() != message->GetArena()) {
1553 if (sub_message->GetArena() == NULL && message->GetArena() != NULL) {
1554 // Case 1: parent is on an arena and child is heap-allocated. We can add
1555 // the child to the arena's Own() list to free on arena destruction, then
1556 // set our pointer.
1557 message->GetArena()->Own(sub_message);
1558 UnsafeArenaSetAllocatedMessage(message, sub_message, field);
1559 } else {
1560 // Case 2: all other cases. We need to make a copy. MutableMessage() will
1561 // either get the existing message object, or instantiate a new one as
1562 // appropriate w.r.t. our arena.
1563 Message* sub_message_copy = MutableMessage(message, field);
1564 sub_message_copy->CopyFrom(*sub_message);
1565 }
1566 } else {
1567 // Same memory ownership domains.
1568 UnsafeArenaSetAllocatedMessage(message, sub_message, field);
1569 }
1570 }
1571
UnsafeArenaReleaseMessage(Message * message,const FieldDescriptor * field,MessageFactory * factory) const1572 Message* GeneratedMessageReflection::UnsafeArenaReleaseMessage(
1573 Message* message,
1574 const FieldDescriptor* field,
1575 MessageFactory* factory) const {
1576 USAGE_CHECK_ALL(ReleaseMessage, SINGULAR, MESSAGE);
1577
1578 if (factory == NULL) factory = message_factory_;
1579
1580 if (field->is_extension()) {
1581 return static_cast<Message*>(
1582 MutableExtensionSet(message)->UnsafeArenaReleaseMessage(field,
1583 factory));
1584 } else {
1585 ClearBit(message, field);
1586 if (field->containing_oneof()) {
1587 if (HasOneofField(*message, field)) {
1588 *MutableOneofCase(message, field->containing_oneof()) = 0;
1589 } else {
1590 return NULL;
1591 }
1592 }
1593 Message** result = MutableRaw<Message*>(message, field);
1594 Message* ret = *result;
1595 *result = NULL;
1596 return ret;
1597 }
1598 }
1599
ReleaseMessage(Message * message,const FieldDescriptor * field,MessageFactory * factory) const1600 Message* GeneratedMessageReflection::ReleaseMessage(
1601 Message* message,
1602 const FieldDescriptor* field,
1603 MessageFactory* factory) const {
1604 Message* released = UnsafeArenaReleaseMessage(message, field, factory);
1605 if (GetArena(message) != NULL && released != NULL) {
1606 Message* copy_from_arena = released->New();
1607 copy_from_arena->CopyFrom(*released);
1608 released = copy_from_arena;
1609 }
1610 return released;
1611 }
1612
GetRepeatedMessage(const Message & message,const FieldDescriptor * field,int index) const1613 const Message& GeneratedMessageReflection::GetRepeatedMessage(
1614 const Message& message, const FieldDescriptor* field, int index) const {
1615 USAGE_CHECK_ALL(GetRepeatedMessage, REPEATED, MESSAGE);
1616
1617 if (field->is_extension()) {
1618 return static_cast<const Message&>(
1619 GetExtensionSet(message).GetRepeatedMessage(field->number(), index));
1620 } else {
1621 if (IsMapFieldInApi(field)) {
1622 return GetRaw<MapFieldBase>(message, field)
1623 .GetRepeatedField()
1624 .Get<GenericTypeHandler<Message> >(index);
1625 } else {
1626 return GetRaw<RepeatedPtrFieldBase>(message, field)
1627 .Get<GenericTypeHandler<Message> >(index);
1628 }
1629 }
1630 }
1631
MutableRepeatedMessage(Message * message,const FieldDescriptor * field,int index) const1632 Message* GeneratedMessageReflection::MutableRepeatedMessage(
1633 Message* message, const FieldDescriptor* field, int index) const {
1634 USAGE_CHECK_ALL(MutableRepeatedMessage, REPEATED, MESSAGE);
1635
1636 if (field->is_extension()) {
1637 return static_cast<Message*>(
1638 MutableExtensionSet(message)->MutableRepeatedMessage(
1639 field->number(), index));
1640 } else {
1641 if (IsMapFieldInApi(field)) {
1642 return MutableRaw<MapFieldBase>(message, field)
1643 ->MutableRepeatedField()
1644 ->Mutable<GenericTypeHandler<Message> >(index);
1645 } else {
1646 return MutableRaw<RepeatedPtrFieldBase>(message, field)
1647 ->Mutable<GenericTypeHandler<Message> >(index);
1648 }
1649 }
1650 }
1651
AddMessage(Message * message,const FieldDescriptor * field,MessageFactory * factory) const1652 Message* GeneratedMessageReflection::AddMessage(
1653 Message* message, const FieldDescriptor* field,
1654 MessageFactory* factory) const {
1655 USAGE_CHECK_ALL(AddMessage, REPEATED, MESSAGE);
1656
1657 if (factory == NULL) factory = message_factory_;
1658
1659 if (field->is_extension()) {
1660 return static_cast<Message*>(
1661 MutableExtensionSet(message)->AddMessage(field, factory));
1662 } else {
1663 Message* result = NULL;
1664
1665 // We can't use AddField<Message>() because RepeatedPtrFieldBase doesn't
1666 // know how to allocate one.
1667 RepeatedPtrFieldBase* repeated = NULL;
1668 if (IsMapFieldInApi(field)) {
1669 repeated =
1670 MutableRaw<MapFieldBase>(message, field)->MutableRepeatedField();
1671 } else {
1672 repeated = MutableRaw<RepeatedPtrFieldBase>(message, field);
1673 }
1674 result = repeated->AddFromCleared<GenericTypeHandler<Message> >();
1675 if (result == NULL) {
1676 // We must allocate a new object.
1677 const Message* prototype;
1678 if (repeated->size() == 0) {
1679 prototype = factory->GetPrototype(field->message_type());
1680 } else {
1681 prototype = &repeated->Get<GenericTypeHandler<Message> >(0);
1682 }
1683 result = prototype->New(message->GetArena());
1684 // We can guarantee here that repeated and result are either both heap
1685 // allocated or arena owned. So it is safe to call the unsafe version
1686 // of AddAllocated.
1687 repeated->UnsafeArenaAddAllocated<GenericTypeHandler<Message> >(result);
1688 }
1689
1690 return result;
1691 }
1692 }
1693
AddAllocatedMessage(Message * message,const FieldDescriptor * field,Message * new_entry) const1694 void GeneratedMessageReflection::AddAllocatedMessage(
1695 Message* message, const FieldDescriptor* field,
1696 Message* new_entry) const {
1697 USAGE_CHECK_ALL(AddAllocatedMessage, REPEATED, MESSAGE);
1698
1699 if (field->is_extension()) {
1700 MutableExtensionSet(message)->AddAllocatedMessage(field, new_entry);
1701 } else {
1702 RepeatedPtrFieldBase* repeated = NULL;
1703 if (IsMapFieldInApi(field)) {
1704 repeated =
1705 MutableRaw<MapFieldBase>(message, field)->MutableRepeatedField();
1706 } else {
1707 repeated = MutableRaw<RepeatedPtrFieldBase>(message, field);
1708 }
1709 repeated->AddAllocated<GenericTypeHandler<Message> >(new_entry);
1710 }
1711 }
1712
MutableRawRepeatedField(Message * message,const FieldDescriptor * field,FieldDescriptor::CppType cpptype,int ctype,const Descriptor * desc) const1713 void* GeneratedMessageReflection::MutableRawRepeatedField(
1714 Message* message, const FieldDescriptor* field,
1715 FieldDescriptor::CppType cpptype,
1716 int ctype, const Descriptor* desc) const {
1717 USAGE_CHECK_REPEATED("MutableRawRepeatedField");
1718 if (field->cpp_type() != cpptype)
1719 ReportReflectionUsageTypeError(descriptor_,
1720 field, "MutableRawRepeatedField", cpptype);
1721 if (ctype >= 0)
1722 GOOGLE_CHECK_EQ(field->options().ctype(), ctype) << "subtype mismatch";
1723 if (desc != NULL)
1724 GOOGLE_CHECK_EQ(field->message_type(), desc) << "wrong submessage type";
1725 if (field->is_extension()) {
1726 return MutableExtensionSet(message)->MutableRawRepeatedField(
1727 field->number(), field->type(), field->is_packed(), field);
1728 } else {
1729 // Trigger transform for MapField
1730 if (IsMapFieldInApi(field)) {
1731 return reinterpret_cast<MapFieldBase*>(reinterpret_cast<uint8*>(message) +
1732 offsets_[field->index()])
1733 ->MutableRepeatedField();
1734 }
1735 return reinterpret_cast<uint8*>(message) + offsets_[field->index()];
1736 }
1737 }
1738
GetRawRepeatedField(const Message & message,const FieldDescriptor * field,FieldDescriptor::CppType cpptype,int ctype,const Descriptor * desc) const1739 const void* GeneratedMessageReflection::GetRawRepeatedField(
1740 const Message& message, const FieldDescriptor* field,
1741 FieldDescriptor::CppType cpptype,
1742 int ctype, const Descriptor* desc) const {
1743 USAGE_CHECK_REPEATED("GetRawRepeatedField");
1744 if (field->cpp_type() != cpptype)
1745 ReportReflectionUsageTypeError(descriptor_,
1746 field, "GetRawRepeatedField", cpptype);
1747 if (ctype >= 0)
1748 GOOGLE_CHECK_EQ(field->options().ctype(), ctype) << "subtype mismatch";
1749 if (desc != NULL)
1750 GOOGLE_CHECK_EQ(field->message_type(), desc) << "wrong submessage type";
1751 if (field->is_extension()) {
1752 // Should use extension_set::GetRawRepeatedField. However, the required
1753 // parameter "default repeated value" is not very easy to get here.
1754 // Map is not supported in extensions, it is acceptable to use
1755 // extension_set::MutableRawRepeatedField which does not change the message.
1756 return MutableExtensionSet(const_cast<Message*>(&message))
1757 ->MutableRawRepeatedField(
1758 field->number(), field->type(), field->is_packed(), field);
1759 } else {
1760 // Trigger transform for MapField
1761 if (IsMapFieldInApi(field)) {
1762 return &(reinterpret_cast<const MapFieldBase*>(
1763 reinterpret_cast<const uint8*>(&message) +
1764 offsets_[field->index()])->GetRepeatedField());
1765 }
1766 return reinterpret_cast<const uint8*>(&message) + offsets_[field->index()];
1767 }
1768 }
1769
GetOneofFieldDescriptor(const Message & message,const OneofDescriptor * oneof_descriptor) const1770 const FieldDescriptor* GeneratedMessageReflection::GetOneofFieldDescriptor(
1771 const Message& message,
1772 const OneofDescriptor* oneof_descriptor) const {
1773 uint32 field_number = GetOneofCase(message, oneof_descriptor);
1774 if (field_number == 0) {
1775 return NULL;
1776 }
1777 return descriptor_->FindFieldByNumber(field_number);
1778 }
1779
ContainsMapKey(const Message & message,const FieldDescriptor * field,const MapKey & key) const1780 bool GeneratedMessageReflection::ContainsMapKey(
1781 const Message& message,
1782 const FieldDescriptor* field,
1783 const MapKey& key) const {
1784 USAGE_CHECK(IsMapFieldInApi(field),
1785 "LookupMapValue",
1786 "Field is not a map field.");
1787 return GetRaw<MapFieldBase>(message, field).ContainsMapKey(key);
1788 }
1789
InsertOrLookupMapValue(Message * message,const FieldDescriptor * field,const MapKey & key,MapValueRef * val) const1790 bool GeneratedMessageReflection::InsertOrLookupMapValue(
1791 Message* message,
1792 const FieldDescriptor* field,
1793 const MapKey& key,
1794 MapValueRef* val) const {
1795 USAGE_CHECK(IsMapFieldInApi(field),
1796 "InsertOrLookupMapValue",
1797 "Field is not a map field.");
1798 val->SetType(field->message_type()->FindFieldByName("value")->cpp_type());
1799 return MutableRaw<MapFieldBase>(message, field)->InsertOrLookupMapValue(
1800 key, val);
1801 }
1802
DeleteMapValue(Message * message,const FieldDescriptor * field,const MapKey & key) const1803 bool GeneratedMessageReflection::DeleteMapValue(
1804 Message* message,
1805 const FieldDescriptor* field,
1806 const MapKey& key) const {
1807 USAGE_CHECK(IsMapFieldInApi(field),
1808 "DeleteMapValue",
1809 "Field is not a map field.");
1810 return MutableRaw<MapFieldBase>(message, field)->DeleteMapValue(key);
1811 }
1812
MapBegin(Message * message,const FieldDescriptor * field) const1813 MapIterator GeneratedMessageReflection::MapBegin(
1814 Message* message,
1815 const FieldDescriptor* field) const {
1816 USAGE_CHECK(IsMapFieldInApi(field),
1817 "MapBegin",
1818 "Field is not a map field.");
1819 MapIterator iter(message, field);
1820 GetRaw<MapFieldBase>(*message, field).MapBegin(&iter);
1821 return iter;
1822 }
1823
MapEnd(Message * message,const FieldDescriptor * field) const1824 MapIterator GeneratedMessageReflection::MapEnd(
1825 Message* message,
1826 const FieldDescriptor* field) const {
1827 USAGE_CHECK(IsMapFieldInApi(field),
1828 "MapEnd",
1829 "Field is not a map field.");
1830 MapIterator iter(message, field);
1831 GetRaw<MapFieldBase>(*message, field).MapEnd(&iter);
1832 return iter;
1833 }
1834
MapSize(const Message & message,const FieldDescriptor * field) const1835 int GeneratedMessageReflection::MapSize(
1836 const Message& message,
1837 const FieldDescriptor* field) const {
1838 USAGE_CHECK(IsMapFieldInApi(field),
1839 "MapSize",
1840 "Field is not a map field.");
1841 return GetRaw<MapFieldBase>(message, field).size();
1842 }
1843
1844 // -----------------------------------------------------------------------------
1845
FindKnownExtensionByName(const string & name) const1846 const FieldDescriptor* GeneratedMessageReflection::FindKnownExtensionByName(
1847 const string& name) const {
1848 if (extensions_offset_ == -1) return NULL;
1849
1850 const FieldDescriptor* result = descriptor_pool_->FindExtensionByName(name);
1851 if (result != NULL && result->containing_type() == descriptor_) {
1852 return result;
1853 }
1854
1855 if (descriptor_->options().message_set_wire_format()) {
1856 // MessageSet extensions may be identified by type name.
1857 const Descriptor* type = descriptor_pool_->FindMessageTypeByName(name);
1858 if (type != NULL) {
1859 // Look for a matching extension in the foreign type's scope.
1860 for (int i = 0; i < type->extension_count(); i++) {
1861 const FieldDescriptor* extension = type->extension(i);
1862 if (extension->containing_type() == descriptor_ &&
1863 extension->type() == FieldDescriptor::TYPE_MESSAGE &&
1864 extension->is_optional() &&
1865 extension->message_type() == type) {
1866 // Found it.
1867 return extension;
1868 }
1869 }
1870 }
1871 }
1872
1873 return NULL;
1874 }
1875
FindKnownExtensionByNumber(int number) const1876 const FieldDescriptor* GeneratedMessageReflection::FindKnownExtensionByNumber(
1877 int number) const {
1878 if (extensions_offset_ == -1) return NULL;
1879 return descriptor_pool_->FindExtensionByNumber(descriptor_, number);
1880 }
1881
SupportsUnknownEnumValues() const1882 bool GeneratedMessageReflection::SupportsUnknownEnumValues() const {
1883 return CreateUnknownEnumValues(descriptor_->file());
1884 }
1885
1886 // ===================================================================
1887 // Some private helpers.
1888
1889 // These simple template accessors obtain pointers (or references) to
1890 // the given field.
1891 template <typename Type>
GetRaw(const Message & message,const FieldDescriptor * field) const1892 inline const Type& GeneratedMessageReflection::GetRaw(
1893 const Message& message, const FieldDescriptor* field) const {
1894 if (field->containing_oneof() && !HasOneofField(message, field)) {
1895 return DefaultRaw<Type>(field);
1896 }
1897 int index = field->containing_oneof() ?
1898 descriptor_->field_count() + field->containing_oneof()->index() :
1899 field->index();
1900 const void* ptr = reinterpret_cast<const uint8*>(&message) +
1901 offsets_[index];
1902 return *reinterpret_cast<const Type*>(ptr);
1903 }
1904
1905 template <typename Type>
MutableRaw(Message * message,const FieldDescriptor * field) const1906 inline Type* GeneratedMessageReflection::MutableRaw(
1907 Message* message, const FieldDescriptor* field) const {
1908 int index = field->containing_oneof() ?
1909 descriptor_->field_count() + field->containing_oneof()->index() :
1910 field->index();
1911 void* ptr = reinterpret_cast<uint8*>(message) + offsets_[index];
1912 return reinterpret_cast<Type*>(ptr);
1913 }
1914
1915 template <typename Type>
DefaultRaw(const FieldDescriptor * field) const1916 inline const Type& GeneratedMessageReflection::DefaultRaw(
1917 const FieldDescriptor* field) const {
1918 const void* ptr = field->containing_oneof() ?
1919 reinterpret_cast<const uint8*>(default_oneof_instance_) +
1920 offsets_[field->index()] :
1921 reinterpret_cast<const uint8*>(default_instance_) +
1922 offsets_[field->index()];
1923 return *reinterpret_cast<const Type*>(ptr);
1924 }
1925
GetHasBits(const Message & message) const1926 inline const uint32* GeneratedMessageReflection::GetHasBits(
1927 const Message& message) const {
1928 if (has_bits_offset_ == -1) { // proto3 with no has-bits.
1929 return NULL;
1930 }
1931 const void* ptr = reinterpret_cast<const uint8*>(&message) + has_bits_offset_;
1932 return reinterpret_cast<const uint32*>(ptr);
1933 }
MutableHasBits(Message * message) const1934 inline uint32* GeneratedMessageReflection::MutableHasBits(
1935 Message* message) const {
1936 if (has_bits_offset_ == -1) {
1937 return NULL;
1938 }
1939 void* ptr = reinterpret_cast<uint8*>(message) + has_bits_offset_;
1940 return reinterpret_cast<uint32*>(ptr);
1941 }
1942
GetOneofCase(const Message & message,const OneofDescriptor * oneof_descriptor) const1943 inline uint32 GeneratedMessageReflection::GetOneofCase(
1944 const Message& message,
1945 const OneofDescriptor* oneof_descriptor) const {
1946 const void* ptr = reinterpret_cast<const uint8*>(&message)
1947 + oneof_case_offset_;
1948 return reinterpret_cast<const uint32*>(ptr)[oneof_descriptor->index()];
1949 }
1950
MutableOneofCase(Message * message,const OneofDescriptor * oneof_descriptor) const1951 inline uint32* GeneratedMessageReflection::MutableOneofCase(
1952 Message* message,
1953 const OneofDescriptor* oneof_descriptor) const {
1954 void* ptr = reinterpret_cast<uint8*>(message) + oneof_case_offset_;
1955 return &(reinterpret_cast<uint32*>(ptr)[oneof_descriptor->index()]);
1956 }
1957
GetExtensionSet(const Message & message) const1958 inline const ExtensionSet& GeneratedMessageReflection::GetExtensionSet(
1959 const Message& message) const {
1960 GOOGLE_DCHECK_NE(extensions_offset_, -1);
1961 const void* ptr = reinterpret_cast<const uint8*>(&message) +
1962 extensions_offset_;
1963 return *reinterpret_cast<const ExtensionSet*>(ptr);
1964 }
MutableExtensionSet(Message * message) const1965 inline ExtensionSet* GeneratedMessageReflection::MutableExtensionSet(
1966 Message* message) const {
1967 GOOGLE_DCHECK_NE(extensions_offset_, -1);
1968 void* ptr = reinterpret_cast<uint8*>(message) + extensions_offset_;
1969 return reinterpret_cast<ExtensionSet*>(ptr);
1970 }
1971
GetArena(Message * message) const1972 inline Arena* GeneratedMessageReflection::GetArena(Message* message) const {
1973 if (arena_offset_ == kNoArenaPointer) {
1974 return NULL;
1975 }
1976
1977 if (unknown_fields_offset_ == kUnknownFieldSetInMetadata) {
1978 // zero-overhead arena pointer overloading UnknownFields
1979 return GetInternalMetadataWithArena(*message).arena();
1980 }
1981
1982 // Baseline case: message class has a dedicated arena pointer.
1983 void* ptr = reinterpret_cast<uint8*>(message) + arena_offset_;
1984 return *reinterpret_cast<Arena**>(ptr);
1985 }
1986
1987 inline const InternalMetadataWithArena&
GetInternalMetadataWithArena(const Message & message) const1988 GeneratedMessageReflection::GetInternalMetadataWithArena(
1989 const Message& message) const {
1990 const void* ptr = reinterpret_cast<const uint8*>(&message) + arena_offset_;
1991 return *reinterpret_cast<const InternalMetadataWithArena*>(ptr);
1992 }
1993
1994 inline InternalMetadataWithArena*
MutableInternalMetadataWithArena(Message * message) const1995 GeneratedMessageReflection::MutableInternalMetadataWithArena(
1996 Message* message) const {
1997 void* ptr = reinterpret_cast<uint8*>(message) + arena_offset_;
1998 return reinterpret_cast<InternalMetadataWithArena*>(ptr);
1999 }
2000
2001 inline bool
GetIsDefaultInstance(const Message & message) const2002 GeneratedMessageReflection::GetIsDefaultInstance(
2003 const Message& message) const {
2004 if (is_default_instance_offset_ == kHasNoDefaultInstanceField) {
2005 return false;
2006 }
2007 const void* ptr = reinterpret_cast<const uint8*>(&message) +
2008 is_default_instance_offset_;
2009 return *reinterpret_cast<const bool*>(ptr);
2010 }
2011
2012 // Simple accessors for manipulating has_bits_.
HasBit(const Message & message,const FieldDescriptor * field) const2013 inline bool GeneratedMessageReflection::HasBit(
2014 const Message& message, const FieldDescriptor* field) const {
2015 if (has_bits_offset_ == -1) {
2016 // proto3: no has-bits. All fields present except messages, which are
2017 // present only if their message-field pointer is non-NULL.
2018 if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
2019 return !GetIsDefaultInstance(message) &&
2020 GetRaw<const Message*>(message, field) != NULL;
2021 } else {
2022 // Non-message field (and non-oneof, since that was handled in HasField()
2023 // before calling us), and singular (again, checked in HasField). So, this
2024 // field must be a scalar.
2025
2026 // Scalar primitive (numeric or string/bytes) fields are present if
2027 // their value is non-zero (numeric) or non-empty (string/bytes). N.B.:
2028 // we must use this definition here, rather than the "scalar fields
2029 // always present" in the proto3 docs, because MergeFrom() semantics
2030 // require presence as "present on wire", and reflection-based merge
2031 // (which uses HasField()) needs to be consistent with this.
2032 switch (field->cpp_type()) {
2033 case FieldDescriptor::CPPTYPE_STRING:
2034 switch (field->options().ctype()) {
2035 default: {
2036 const string* default_ptr =
2037 &DefaultRaw<ArenaStringPtr>(field).Get(NULL);
2038 return GetField<ArenaStringPtr>(message, field).Get(
2039 default_ptr).size() > 0;
2040 }
2041 }
2042 return false;
2043 case FieldDescriptor::CPPTYPE_BOOL:
2044 return GetRaw<bool>(message, field) != false;
2045 case FieldDescriptor::CPPTYPE_INT32:
2046 return GetRaw<int32>(message, field) != 0;
2047 case FieldDescriptor::CPPTYPE_INT64:
2048 return GetRaw<int64>(message, field) != 0;
2049 case FieldDescriptor::CPPTYPE_UINT32:
2050 return GetRaw<uint32>(message, field) != 0;
2051 case FieldDescriptor::CPPTYPE_UINT64:
2052 return GetRaw<uint64>(message, field) != 0;
2053 case FieldDescriptor::CPPTYPE_FLOAT:
2054 return GetRaw<float>(message, field) != 0.0;
2055 case FieldDescriptor::CPPTYPE_DOUBLE:
2056 return GetRaw<double>(message, field) != 0.0;
2057 case FieldDescriptor::CPPTYPE_ENUM:
2058 return GetRaw<int>(message, field) != 0;
2059 case FieldDescriptor::CPPTYPE_MESSAGE:
2060 // handled above; avoid warning
2061 GOOGLE_LOG(FATAL) << "Reached impossible case in HasBit().";
2062 break;
2063 }
2064 }
2065 }
2066 return GetHasBits(message)[field->index() / 32] &
2067 (1 << (field->index() % 32));
2068 }
2069
SetBit(Message * message,const FieldDescriptor * field) const2070 inline void GeneratedMessageReflection::SetBit(
2071 Message* message, const FieldDescriptor* field) const {
2072 if (has_bits_offset_ == -1) {
2073 return;
2074 }
2075 MutableHasBits(message)[field->index() / 32] |= (1 << (field->index() % 32));
2076 }
2077
ClearBit(Message * message,const FieldDescriptor * field) const2078 inline void GeneratedMessageReflection::ClearBit(
2079 Message* message, const FieldDescriptor* field) const {
2080 if (has_bits_offset_ == -1) {
2081 return;
2082 }
2083 MutableHasBits(message)[field->index() / 32] &= ~(1 << (field->index() % 32));
2084 }
2085
SwapBit(Message * message1,Message * message2,const FieldDescriptor * field) const2086 inline void GeneratedMessageReflection::SwapBit(
2087 Message* message1, Message* message2, const FieldDescriptor* field) const {
2088 if (has_bits_offset_ == -1) {
2089 return;
2090 }
2091 bool temp_has_bit = HasBit(*message1, field);
2092 if (HasBit(*message2, field)) {
2093 SetBit(message1, field);
2094 } else {
2095 ClearBit(message1, field);
2096 }
2097 if (temp_has_bit) {
2098 SetBit(message2, field);
2099 } else {
2100 ClearBit(message2, field);
2101 }
2102 }
2103
HasOneof(const Message & message,const OneofDescriptor * oneof_descriptor) const2104 inline bool GeneratedMessageReflection::HasOneof(
2105 const Message& message, const OneofDescriptor* oneof_descriptor) const {
2106 return (GetOneofCase(message, oneof_descriptor) > 0);
2107 }
2108
HasOneofField(const Message & message,const FieldDescriptor * field) const2109 inline bool GeneratedMessageReflection::HasOneofField(
2110 const Message& message, const FieldDescriptor* field) const {
2111 return (GetOneofCase(message, field->containing_oneof()) == field->number());
2112 }
2113
SetOneofCase(Message * message,const FieldDescriptor * field) const2114 inline void GeneratedMessageReflection::SetOneofCase(
2115 Message* message, const FieldDescriptor* field) const {
2116 *MutableOneofCase(message, field->containing_oneof()) = field->number();
2117 }
2118
ClearOneofField(Message * message,const FieldDescriptor * field) const2119 inline void GeneratedMessageReflection::ClearOneofField(
2120 Message* message, const FieldDescriptor* field) const {
2121 if (HasOneofField(*message, field)) {
2122 ClearOneof(message, field->containing_oneof());
2123 }
2124 }
2125
ClearOneof(Message * message,const OneofDescriptor * oneof_descriptor) const2126 inline void GeneratedMessageReflection::ClearOneof(
2127 Message* message, const OneofDescriptor* oneof_descriptor) const {
2128 // TODO(jieluo): Consider to cache the unused object instead of deleting
2129 // it. It will be much faster if an application switches a lot from
2130 // a few oneof fields. Time/space tradeoff
2131 uint32 oneof_case = GetOneofCase(*message, oneof_descriptor);
2132 if (oneof_case > 0) {
2133 const FieldDescriptor* field = descriptor_->FindFieldByNumber(oneof_case);
2134 if (GetArena(message) == NULL) {
2135 switch (field->cpp_type()) {
2136 case FieldDescriptor::CPPTYPE_STRING: {
2137 switch (field->options().ctype()) {
2138 default: // TODO(kenton): Support other string reps.
2139 case FieldOptions::STRING: {
2140 const string* default_ptr =
2141 &DefaultRaw<ArenaStringPtr>(field).Get(NULL);
2142 MutableField<ArenaStringPtr>(message, field)->
2143 Destroy(default_ptr, GetArena(message));
2144 break;
2145 }
2146 }
2147 break;
2148 }
2149
2150 case FieldDescriptor::CPPTYPE_MESSAGE:
2151 delete *MutableRaw<Message*>(message, field);
2152 break;
2153 default:
2154 break;
2155 }
2156 }
2157
2158 *MutableOneofCase(message, oneof_descriptor) = 0;
2159 }
2160 }
2161
2162 // Template implementations of basic accessors. Inline because each
2163 // template instance is only called from one location. These are
2164 // used for all types except messages.
2165 template <typename Type>
GetField(const Message & message,const FieldDescriptor * field) const2166 inline const Type& GeneratedMessageReflection::GetField(
2167 const Message& message, const FieldDescriptor* field) const {
2168 return GetRaw<Type>(message, field);
2169 }
2170
2171 template <typename Type>
SetField(Message * message,const FieldDescriptor * field,const Type & value) const2172 inline void GeneratedMessageReflection::SetField(
2173 Message* message, const FieldDescriptor* field, const Type& value) const {
2174 if (field->containing_oneof() && !HasOneofField(*message, field)) {
2175 ClearOneof(message, field->containing_oneof());
2176 }
2177 *MutableRaw<Type>(message, field) = value;
2178 field->containing_oneof() ?
2179 SetOneofCase(message, field) : SetBit(message, field);
2180 }
2181
2182 template <typename Type>
MutableField(Message * message,const FieldDescriptor * field) const2183 inline Type* GeneratedMessageReflection::MutableField(
2184 Message* message, const FieldDescriptor* field) const {
2185 field->containing_oneof() ?
2186 SetOneofCase(message, field) : SetBit(message, field);
2187 return MutableRaw<Type>(message, field);
2188 }
2189
2190 template <typename Type>
GetRepeatedField(const Message & message,const FieldDescriptor * field,int index) const2191 inline const Type& GeneratedMessageReflection::GetRepeatedField(
2192 const Message& message, const FieldDescriptor* field, int index) const {
2193 return GetRaw<RepeatedField<Type> >(message, field).Get(index);
2194 }
2195
2196 template <typename Type>
GetRepeatedPtrField(const Message & message,const FieldDescriptor * field,int index) const2197 inline const Type& GeneratedMessageReflection::GetRepeatedPtrField(
2198 const Message& message, const FieldDescriptor* field, int index) const {
2199 return GetRaw<RepeatedPtrField<Type> >(message, field).Get(index);
2200 }
2201
2202 template <typename Type>
SetRepeatedField(Message * message,const FieldDescriptor * field,int index,Type value) const2203 inline void GeneratedMessageReflection::SetRepeatedField(
2204 Message* message, const FieldDescriptor* field,
2205 int index, Type value) const {
2206 MutableRaw<RepeatedField<Type> >(message, field)->Set(index, value);
2207 }
2208
2209 template <typename Type>
MutableRepeatedField(Message * message,const FieldDescriptor * field,int index) const2210 inline Type* GeneratedMessageReflection::MutableRepeatedField(
2211 Message* message, const FieldDescriptor* field, int index) const {
2212 RepeatedPtrField<Type>* repeated =
2213 MutableRaw<RepeatedPtrField<Type> >(message, field);
2214 return repeated->Mutable(index);
2215 }
2216
2217 template <typename Type>
AddField(Message * message,const FieldDescriptor * field,const Type & value) const2218 inline void GeneratedMessageReflection::AddField(
2219 Message* message, const FieldDescriptor* field, const Type& value) const {
2220 MutableRaw<RepeatedField<Type> >(message, field)->Add(value);
2221 }
2222
2223 template <typename Type>
AddField(Message * message,const FieldDescriptor * field) const2224 inline Type* GeneratedMessageReflection::AddField(
2225 Message* message, const FieldDescriptor* field) const {
2226 RepeatedPtrField<Type>* repeated =
2227 MutableRaw<RepeatedPtrField<Type> >(message, field);
2228 return repeated->Add();
2229 }
2230
GetMessageFactory() const2231 MessageFactory* GeneratedMessageReflection::GetMessageFactory() const {
2232 return message_factory_;
2233 }
2234
RepeatedFieldData(Message * message,const FieldDescriptor * field,FieldDescriptor::CppType cpp_type,const Descriptor * message_type) const2235 void* GeneratedMessageReflection::RepeatedFieldData(
2236 Message* message, const FieldDescriptor* field,
2237 FieldDescriptor::CppType cpp_type,
2238 const Descriptor* message_type) const {
2239 GOOGLE_CHECK(field->is_repeated());
2240 GOOGLE_CHECK(field->cpp_type() == cpp_type ||
2241 (field->cpp_type() == FieldDescriptor::CPPTYPE_ENUM &&
2242 cpp_type == FieldDescriptor::CPPTYPE_INT32))
2243 << "The type parameter T in RepeatedFieldRef<T> API doesn't match "
2244 << "the actual field type (for enums T should be the generated enum "
2245 << "type or int32).";
2246 if (message_type != NULL) {
2247 GOOGLE_CHECK_EQ(message_type, field->message_type());
2248 }
2249 if (field->is_extension()) {
2250 return MutableExtensionSet(message)->MutableRawRepeatedField(
2251 field->number(), field->type(), field->is_packed(), field);
2252 } else {
2253 return reinterpret_cast<uint8*>(message) + offsets_[field->index()];
2254 }
2255 }
2256
MapData(Message * message,const FieldDescriptor * field) const2257 MapFieldBase* GeneratedMessageReflection::MapData(
2258 Message* message, const FieldDescriptor* field) const {
2259 USAGE_CHECK(IsMapFieldInApi(field),
2260 "GetMapData",
2261 "Field is not a map field.");
2262 return MutableRaw<MapFieldBase>(message, field);
2263 }
2264
2265 GeneratedMessageReflection*
NewGeneratedMessageReflection(const Descriptor * descriptor,const Message * default_instance,const int offsets[],int has_bits_offset,int unknown_fields_offset,int extensions_offset,const void * default_oneof_instance,int oneof_case_offset,int object_size,int arena_offset,int is_default_instance_offset)2266 GeneratedMessageReflection::NewGeneratedMessageReflection(
2267 const Descriptor* descriptor,
2268 const Message* default_instance,
2269 const int offsets[],
2270 int has_bits_offset,
2271 int unknown_fields_offset,
2272 int extensions_offset,
2273 const void* default_oneof_instance,
2274 int oneof_case_offset,
2275 int object_size,
2276 int arena_offset,
2277 int is_default_instance_offset) {
2278 return new GeneratedMessageReflection(descriptor,
2279 default_instance,
2280 offsets,
2281 has_bits_offset,
2282 unknown_fields_offset,
2283 extensions_offset,
2284 default_oneof_instance,
2285 oneof_case_offset,
2286 DescriptorPool::generated_pool(),
2287 MessageFactory::generated_factory(),
2288 object_size,
2289 arena_offset,
2290 is_default_instance_offset);
2291 }
2292
2293 GeneratedMessageReflection*
NewGeneratedMessageReflection(const Descriptor * descriptor,const Message * default_instance,const int offsets[],int has_bits_offset,int unknown_fields_offset,int extensions_offset,int object_size,int arena_offset,int is_default_instance_offset)2294 GeneratedMessageReflection::NewGeneratedMessageReflection(
2295 const Descriptor* descriptor,
2296 const Message* default_instance,
2297 const int offsets[],
2298 int has_bits_offset,
2299 int unknown_fields_offset,
2300 int extensions_offset,
2301 int object_size,
2302 int arena_offset,
2303 int is_default_instance_offset) {
2304 return new GeneratedMessageReflection(descriptor,
2305 default_instance,
2306 offsets,
2307 has_bits_offset,
2308 unknown_fields_offset,
2309 extensions_offset,
2310 DescriptorPool::generated_pool(),
2311 MessageFactory::generated_factory(),
2312 object_size,
2313 arena_offset,
2314 is_default_instance_offset);
2315 }
2316
2317 } // namespace internal
2318 } // namespace protobuf
2319 } // namespace google
2320