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