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 : #include <google/protobuf/descriptor.pb.h>
38 : #include <google/protobuf/generated_message_reflection.h>
39 : #include <google/protobuf/descriptor.h>
40 : #include <google/protobuf/repeated_field.h>
41 : #include <google/protobuf/extension_set.h>
42 : #include <google/protobuf/generated_message_util.h>
43 : #include <google/protobuf/stubs/common.h>
44 :
45 : #define GOOGLE_PROTOBUF_HAS_ONEOF
46 :
47 : namespace google {
48 : namespace protobuf {
49 : namespace internal {
50 :
51 0 : int StringSpaceUsedExcludingSelf(const string& str) {
52 0 : const void* start = &str;
53 0 : const void* end = &str + 1;
54 :
55 0 : if (start <= str.data() && str.data() < end) {
56 : // The string's data is stored inside the string object itself.
57 0 : return 0;
58 : } else {
59 0 : return str.capacity();
60 : }
61 : }
62 :
63 0 : bool ParseNamedEnum(const EnumDescriptor* descriptor,
64 : const string& name,
65 : int* value) {
66 0 : const EnumValueDescriptor* d = descriptor->FindValueByName(name);
67 0 : if (d == NULL) return false;
68 0 : *value = d->number();
69 0 : return true;
70 : }
71 :
72 0 : const string& NameOfEnum(const EnumDescriptor* descriptor, int value) {
73 0 : const EnumValueDescriptor* d = descriptor->FindValueByNumber(value);
74 0 : return (d == NULL ? GetEmptyString() : d->name());
75 : }
76 :
77 : // ===================================================================
78 : // Helpers for reporting usage errors (e.g. trying to use GetInt32() on
79 : // a string field).
80 :
81 : namespace {
82 :
83 0 : void ReportReflectionUsageError(
84 : const Descriptor* descriptor, const FieldDescriptor* field,
85 : const char* method, const char* description) {
86 0 : GOOGLE_LOG(FATAL)
87 : << "Protocol Buffer reflection usage error:\n"
88 0 : " Method : google::protobuf::Reflection::" << method << "\n"
89 0 : " Message type: " << descriptor->full_name() << "\n"
90 0 : " Field : " << field->full_name() << "\n"
91 0 : " Problem : " << description;
92 0 : }
93 :
94 : const char* cpptype_names_[FieldDescriptor::MAX_CPPTYPE + 1] = {
95 : "INVALID_CPPTYPE",
96 : "CPPTYPE_INT32",
97 : "CPPTYPE_INT64",
98 : "CPPTYPE_UINT32",
99 : "CPPTYPE_UINT64",
100 : "CPPTYPE_DOUBLE",
101 : "CPPTYPE_FLOAT",
102 : "CPPTYPE_BOOL",
103 : "CPPTYPE_ENUM",
104 : "CPPTYPE_STRING",
105 : "CPPTYPE_MESSAGE"
106 : };
107 :
108 0 : static void ReportReflectionUsageTypeError(
109 : const Descriptor* descriptor, const FieldDescriptor* field,
110 : const char* method,
111 : FieldDescriptor::CppType expected_type) {
112 0 : GOOGLE_LOG(FATAL)
113 : << "Protocol Buffer reflection usage error:\n"
114 0 : " Method : google::protobuf::Reflection::" << method << "\n"
115 0 : " Message type: " << descriptor->full_name() << "\n"
116 0 : " Field : " << field->full_name() << "\n"
117 : " Problem : Field is not the right type for this message:\n"
118 0 : " Expected : " << cpptype_names_[expected_type] << "\n"
119 0 : " Field type: " << cpptype_names_[field->cpp_type()];
120 0 : }
121 :
122 0 : static void ReportReflectionUsageEnumTypeError(
123 : const Descriptor* descriptor, const FieldDescriptor* field,
124 : const char* method, const EnumValueDescriptor* value) {
125 0 : GOOGLE_LOG(FATAL)
126 : << "Protocol Buffer reflection usage error:\n"
127 0 : " Method : google::protobuf::Reflection::" << method << "\n"
128 0 : " Message type: " << descriptor->full_name() << "\n"
129 0 : " Field : " << field->full_name() << "\n"
130 : " Problem : Enum value did not match field type:\n"
131 0 : " Expected : " << field->enum_type()->full_name() << "\n"
132 0 : " Actual : " << value->full_name();
133 0 : }
134 :
135 : #define USAGE_CHECK(CONDITION, METHOD, ERROR_DESCRIPTION) \
136 : if (!(CONDITION)) \
137 : ReportReflectionUsageError(descriptor_, field, #METHOD, ERROR_DESCRIPTION)
138 : #define USAGE_CHECK_EQ(A, B, METHOD, ERROR_DESCRIPTION) \
139 : USAGE_CHECK((A) == (B), METHOD, ERROR_DESCRIPTION)
140 : #define USAGE_CHECK_NE(A, B, METHOD, ERROR_DESCRIPTION) \
141 : USAGE_CHECK((A) != (B), METHOD, ERROR_DESCRIPTION)
142 :
143 : #define USAGE_CHECK_TYPE(METHOD, CPPTYPE) \
144 : if (field->cpp_type() != FieldDescriptor::CPPTYPE_##CPPTYPE) \
145 : ReportReflectionUsageTypeError(descriptor_, field, #METHOD, \
146 : FieldDescriptor::CPPTYPE_##CPPTYPE)
147 :
148 : #define USAGE_CHECK_ENUM_VALUE(METHOD) \
149 : if (value->type() != field->enum_type()) \
150 : ReportReflectionUsageEnumTypeError(descriptor_, field, #METHOD, value)
151 :
152 : #define USAGE_CHECK_MESSAGE_TYPE(METHOD) \
153 : USAGE_CHECK_EQ(field->containing_type(), descriptor_, \
154 : METHOD, "Field does not match message type.");
155 : #define USAGE_CHECK_SINGULAR(METHOD) \
156 : USAGE_CHECK_NE(field->label(), FieldDescriptor::LABEL_REPEATED, METHOD, \
157 : "Field is repeated; the method requires a singular field.")
158 : #define USAGE_CHECK_REPEATED(METHOD) \
159 : USAGE_CHECK_EQ(field->label(), FieldDescriptor::LABEL_REPEATED, METHOD, \
160 : "Field is singular; the method requires a repeated field.")
161 :
162 : #define USAGE_CHECK_ALL(METHOD, LABEL, CPPTYPE) \
163 : USAGE_CHECK_MESSAGE_TYPE(METHOD); \
164 : USAGE_CHECK_##LABEL(METHOD); \
165 : USAGE_CHECK_TYPE(METHOD, CPPTYPE)
166 :
167 : } // namespace
168 :
169 : // ===================================================================
170 :
171 0 : GeneratedMessageReflection::GeneratedMessageReflection(
172 : const Descriptor* descriptor,
173 : const Message* default_instance,
174 : const int offsets[],
175 : int has_bits_offset,
176 : int unknown_fields_offset,
177 : int extensions_offset,
178 : const DescriptorPool* descriptor_pool,
179 : MessageFactory* factory,
180 0 : int object_size)
181 : : descriptor_ (descriptor),
182 : default_instance_ (default_instance),
183 : offsets_ (offsets),
184 : has_bits_offset_ (has_bits_offset),
185 : unknown_fields_offset_(unknown_fields_offset),
186 : extensions_offset_(extensions_offset),
187 : object_size_ (object_size),
188 0 : descriptor_pool_ ((descriptor_pool == NULL) ?
189 : DescriptorPool::generated_pool() :
190 : descriptor_pool),
191 0 : message_factory_ (factory) {
192 0 : }
193 :
194 0 : GeneratedMessageReflection::GeneratedMessageReflection(
195 : const Descriptor* descriptor,
196 : const Message* default_instance,
197 : const int offsets[],
198 : int has_bits_offset,
199 : int unknown_fields_offset,
200 : int extensions_offset,
201 : const void* default_oneof_instance,
202 : int oneof_case_offset,
203 : const DescriptorPool* descriptor_pool,
204 : MessageFactory* factory,
205 0 : int object_size)
206 : : descriptor_ (descriptor),
207 : default_instance_ (default_instance),
208 : default_oneof_instance_ (default_oneof_instance),
209 : offsets_ (offsets),
210 : has_bits_offset_ (has_bits_offset),
211 : oneof_case_offset_(oneof_case_offset),
212 : unknown_fields_offset_(unknown_fields_offset),
213 : extensions_offset_(extensions_offset),
214 : object_size_ (object_size),
215 0 : descriptor_pool_ ((descriptor_pool == NULL) ?
216 : DescriptorPool::generated_pool() :
217 : descriptor_pool),
218 0 : message_factory_ (factory) {
219 0 : }
220 :
221 0 : GeneratedMessageReflection::~GeneratedMessageReflection() {}
222 :
223 0 : const UnknownFieldSet& GeneratedMessageReflection::GetUnknownFields(
224 : const Message& message) const {
225 : const void* ptr = reinterpret_cast<const uint8*>(&message) +
226 0 : unknown_fields_offset_;
227 0 : return *reinterpret_cast<const UnknownFieldSet*>(ptr);
228 : }
229 0 : UnknownFieldSet* GeneratedMessageReflection::MutableUnknownFields(
230 : Message* message) const {
231 0 : void* ptr = reinterpret_cast<uint8*>(message) + unknown_fields_offset_;
232 0 : return reinterpret_cast<UnknownFieldSet*>(ptr);
233 : }
234 :
235 0 : int GeneratedMessageReflection::SpaceUsed(const Message& message) const {
236 : // object_size_ already includes the in-memory representation of each field
237 : // in the message, so we only need to account for additional memory used by
238 : // the fields.
239 0 : int total_size = object_size_;
240 :
241 0 : total_size += GetUnknownFields(message).SpaceUsedExcludingSelf();
242 :
243 0 : if (extensions_offset_ != -1) {
244 0 : total_size += GetExtensionSet(message).SpaceUsedExcludingSelf();
245 : }
246 :
247 0 : for (int i = 0; i < descriptor_->field_count(); i++) {
248 0 : const FieldDescriptor* field = descriptor_->field(i);
249 :
250 0 : if (field->is_repeated()) {
251 0 : switch (field->cpp_type()) {
252 : #define HANDLE_TYPE(UPPERCASE, LOWERCASE) \
253 : case FieldDescriptor::CPPTYPE_##UPPERCASE : \
254 : total_size += GetRaw<RepeatedField<LOWERCASE> >(message, field) \
255 : .SpaceUsedExcludingSelf(); \
256 : break
257 :
258 0 : HANDLE_TYPE( INT32, int32);
259 0 : HANDLE_TYPE( INT64, int64);
260 0 : HANDLE_TYPE(UINT32, uint32);
261 0 : HANDLE_TYPE(UINT64, uint64);
262 0 : HANDLE_TYPE(DOUBLE, double);
263 0 : HANDLE_TYPE( FLOAT, float);
264 0 : HANDLE_TYPE( BOOL, bool);
265 0 : HANDLE_TYPE( ENUM, int);
266 : #undef HANDLE_TYPE
267 :
268 : case FieldDescriptor::CPPTYPE_STRING:
269 0 : switch (field->options().ctype()) {
270 : default: // TODO(kenton): Support other string reps.
271 : case FieldOptions::STRING:
272 0 : total_size += GetRaw<RepeatedPtrField<string> >(message, field)
273 0 : .SpaceUsedExcludingSelf();
274 0 : break;
275 : }
276 0 : break;
277 :
278 : case FieldDescriptor::CPPTYPE_MESSAGE:
279 : // We don't know which subclass of RepeatedPtrFieldBase the type is,
280 : // so we use RepeatedPtrFieldBase directly.
281 0 : total_size +=
282 0 : GetRaw<RepeatedPtrFieldBase>(message, field)
283 0 : .SpaceUsedExcludingSelf<GenericTypeHandler<Message> >();
284 0 : break;
285 : }
286 : } else {
287 0 : if (field->containing_oneof() && !HasOneofField(message, field)) {
288 0 : continue;
289 : }
290 0 : switch (field->cpp_type()) {
291 : case FieldDescriptor::CPPTYPE_INT32 :
292 : case FieldDescriptor::CPPTYPE_INT64 :
293 : case FieldDescriptor::CPPTYPE_UINT32:
294 : case FieldDescriptor::CPPTYPE_UINT64:
295 : case FieldDescriptor::CPPTYPE_DOUBLE:
296 : case FieldDescriptor::CPPTYPE_FLOAT :
297 : case FieldDescriptor::CPPTYPE_BOOL :
298 : case FieldDescriptor::CPPTYPE_ENUM :
299 : // Field is inline, so we've already counted it.
300 0 : break;
301 :
302 : case FieldDescriptor::CPPTYPE_STRING: {
303 0 : switch (field->options().ctype()) {
304 : default: // TODO(kenton): Support other string reps.
305 : case FieldOptions::STRING: {
306 0 : const string* ptr = GetField<const string*>(message, field);
307 :
308 : // Initially, the string points to the default value stored in
309 : // the prototype. Only count the string if it has been changed
310 : // from the default value.
311 0 : const string* default_ptr = DefaultRaw<const string*>(field);
312 :
313 0 : if (ptr != default_ptr) {
314 : // string fields are represented by just a pointer, so also
315 : // include sizeof(string) as well.
316 0 : total_size += sizeof(*ptr) + StringSpaceUsedExcludingSelf(*ptr);
317 : }
318 0 : break;
319 : }
320 : }
321 0 : break;
322 : }
323 :
324 : case FieldDescriptor::CPPTYPE_MESSAGE:
325 0 : if (&message == default_instance_) {
326 : // For singular fields, the prototype just stores a pointer to the
327 : // external type's prototype, so there is no extra memory usage.
328 : } else {
329 0 : const Message* sub_message = GetRaw<const Message*>(message, field);
330 0 : if (sub_message != NULL) {
331 0 : total_size += sub_message->SpaceUsed();
332 : }
333 : }
334 0 : break;
335 : }
336 : }
337 : }
338 :
339 0 : return total_size;
340 : }
341 :
342 0 : void GeneratedMessageReflection::SwapField(
343 : Message* message1,
344 : Message* message2,
345 : const FieldDescriptor* field) const {
346 0 : if (field->is_repeated()) {
347 0 : switch (field->cpp_type()) {
348 : #define SWAP_ARRAYS(CPPTYPE, TYPE) \
349 : case FieldDescriptor::CPPTYPE_##CPPTYPE: \
350 : MutableRaw<RepeatedField<TYPE> >(message1, field)->Swap( \
351 : MutableRaw<RepeatedField<TYPE> >(message2, field)); \
352 : break;
353 :
354 0 : SWAP_ARRAYS(INT32 , int32 );
355 0 : SWAP_ARRAYS(INT64 , int64 );
356 0 : SWAP_ARRAYS(UINT32, uint32);
357 0 : SWAP_ARRAYS(UINT64, uint64);
358 0 : SWAP_ARRAYS(FLOAT , float );
359 0 : SWAP_ARRAYS(DOUBLE, double);
360 0 : SWAP_ARRAYS(BOOL , bool );
361 0 : SWAP_ARRAYS(ENUM , int );
362 : #undef SWAP_ARRAYS
363 :
364 : case FieldDescriptor::CPPTYPE_STRING:
365 : case FieldDescriptor::CPPTYPE_MESSAGE:
366 0 : MutableRaw<RepeatedPtrFieldBase>(message1, field)->Swap(
367 0 : MutableRaw<RepeatedPtrFieldBase>(message2, field));
368 0 : break;
369 :
370 : default:
371 0 : GOOGLE_LOG(FATAL) << "Unimplemented type: " << field->cpp_type();
372 : }
373 : } else {
374 0 : switch (field->cpp_type()) {
375 : #define SWAP_VALUES(CPPTYPE, TYPE) \
376 : case FieldDescriptor::CPPTYPE_##CPPTYPE: \
377 : std::swap(*MutableRaw<TYPE>(message1, field), \
378 : *MutableRaw<TYPE>(message2, field)); \
379 : break;
380 :
381 0 : SWAP_VALUES(INT32 , int32 );
382 0 : SWAP_VALUES(INT64 , int64 );
383 0 : SWAP_VALUES(UINT32, uint32);
384 0 : SWAP_VALUES(UINT64, uint64);
385 0 : SWAP_VALUES(FLOAT , float );
386 0 : SWAP_VALUES(DOUBLE, double);
387 0 : SWAP_VALUES(BOOL , bool );
388 0 : SWAP_VALUES(ENUM , int );
389 : #undef SWAP_VALUES
390 : case FieldDescriptor::CPPTYPE_MESSAGE:
391 0 : std::swap(*MutableRaw<Message*>(message1, field),
392 0 : *MutableRaw<Message*>(message2, field));
393 0 : break;
394 :
395 : case FieldDescriptor::CPPTYPE_STRING:
396 0 : switch (field->options().ctype()) {
397 : default: // TODO(kenton): Support other string reps.
398 : case FieldOptions::STRING:
399 0 : std::swap(*MutableRaw<string*>(message1, field),
400 0 : *MutableRaw<string*>(message2, field));
401 0 : break;
402 : }
403 0 : break;
404 :
405 : default:
406 0 : GOOGLE_LOG(FATAL) << "Unimplemented type: " << field->cpp_type();
407 : }
408 : }
409 0 : }
410 :
411 0 : void GeneratedMessageReflection::SwapOneofField(
412 : Message* message1,
413 : Message* message2,
414 : const OneofDescriptor* oneof_descriptor) const {
415 0 : uint32 oneof_case1 = GetOneofCase(*message1, oneof_descriptor);
416 0 : uint32 oneof_case2 = GetOneofCase(*message2, oneof_descriptor);
417 :
418 : int32 temp_int32;
419 : int64 temp_int64;
420 : uint32 temp_uint32;
421 : uint64 temp_uint64;
422 : float temp_float;
423 : double temp_double;
424 : bool temp_bool;
425 : int temp_int;
426 : Message* temp_message;
427 0 : string temp_string;
428 :
429 : // Stores message1's oneof field to a temp variable.
430 : const FieldDescriptor* field1;
431 0 : if (oneof_case1 > 0) {
432 0 : field1 = descriptor_->FindFieldByNumber(oneof_case1);
433 : //oneof_descriptor->field(oneof_case1);
434 0 : switch (field1->cpp_type()) {
435 : #define GET_TEMP_VALUE(CPPTYPE, TYPE) \
436 : case FieldDescriptor::CPPTYPE_##CPPTYPE: \
437 : temp_##TYPE = GetField<TYPE>(*message1, field1); \
438 : break;
439 :
440 0 : GET_TEMP_VALUE(INT32 , int32 );
441 0 : GET_TEMP_VALUE(INT64 , int64 );
442 0 : GET_TEMP_VALUE(UINT32, uint32);
443 0 : GET_TEMP_VALUE(UINT64, uint64);
444 0 : GET_TEMP_VALUE(FLOAT , float );
445 0 : GET_TEMP_VALUE(DOUBLE, double);
446 0 : GET_TEMP_VALUE(BOOL , bool );
447 0 : GET_TEMP_VALUE(ENUM , int );
448 : #undef GET_TEMP_VALUE
449 : case FieldDescriptor::CPPTYPE_MESSAGE:
450 0 : temp_message = ReleaseMessage(message1, field1);
451 0 : break;
452 :
453 : case FieldDescriptor::CPPTYPE_STRING:
454 0 : temp_string = GetString(*message1, field1);
455 0 : break;
456 :
457 : default:
458 0 : GOOGLE_LOG(FATAL) << "Unimplemented type: " << field1->cpp_type();
459 : }
460 : }
461 :
462 : // Sets message1's oneof field from the message2's oneof field.
463 0 : if (oneof_case2 > 0) {
464 : const FieldDescriptor* field2 =
465 0 : descriptor_->FindFieldByNumber(oneof_case2);
466 0 : switch (field2->cpp_type()) {
467 : #define SET_ONEOF_VALUE1(CPPTYPE, TYPE) \
468 : case FieldDescriptor::CPPTYPE_##CPPTYPE: \
469 : SetField<TYPE>(message1, field2, GetField<TYPE>(*message2, field2)); \
470 : break;
471 :
472 0 : SET_ONEOF_VALUE1(INT32 , int32 );
473 0 : SET_ONEOF_VALUE1(INT64 , int64 );
474 0 : SET_ONEOF_VALUE1(UINT32, uint32);
475 0 : SET_ONEOF_VALUE1(UINT64, uint64);
476 0 : SET_ONEOF_VALUE1(FLOAT , float );
477 0 : SET_ONEOF_VALUE1(DOUBLE, double);
478 0 : SET_ONEOF_VALUE1(BOOL , bool );
479 0 : SET_ONEOF_VALUE1(ENUM , int );
480 : #undef SET_ONEOF_VALUE1
481 : case FieldDescriptor::CPPTYPE_MESSAGE:
482 0 : SetAllocatedMessage(message1,
483 0 : ReleaseMessage(message2, field2),
484 0 : field2);
485 0 : break;
486 :
487 : case FieldDescriptor::CPPTYPE_STRING:
488 0 : SetString(message1, field2, GetString(*message2, field2));
489 0 : break;
490 :
491 : default:
492 0 : GOOGLE_LOG(FATAL) << "Unimplemented type: " << field2->cpp_type();
493 : }
494 : } else {
495 0 : ClearOneof(message1, oneof_descriptor);
496 : }
497 :
498 : // Sets message2's oneof field from the temp variable.
499 0 : if (oneof_case1 > 0) {
500 0 : switch (field1->cpp_type()) {
501 : #define SET_ONEOF_VALUE2(CPPTYPE, TYPE) \
502 : case FieldDescriptor::CPPTYPE_##CPPTYPE: \
503 : SetField<TYPE>(message2, field1, temp_##TYPE); \
504 : break;
505 :
506 0 : SET_ONEOF_VALUE2(INT32 , int32 );
507 0 : SET_ONEOF_VALUE2(INT64 , int64 );
508 0 : SET_ONEOF_VALUE2(UINT32, uint32);
509 0 : SET_ONEOF_VALUE2(UINT64, uint64);
510 0 : SET_ONEOF_VALUE2(FLOAT , float );
511 0 : SET_ONEOF_VALUE2(DOUBLE, double);
512 0 : SET_ONEOF_VALUE2(BOOL , bool );
513 0 : SET_ONEOF_VALUE2(ENUM , int );
514 : #undef SET_ONEOF_VALUE2
515 : case FieldDescriptor::CPPTYPE_MESSAGE:
516 0 : SetAllocatedMessage(message2, temp_message, field1);
517 0 : break;
518 :
519 : case FieldDescriptor::CPPTYPE_STRING:
520 0 : SetString(message2, field1, temp_string);
521 0 : break;
522 :
523 : default:
524 0 : GOOGLE_LOG(FATAL) << "Unimplemented type: " << field1->cpp_type();
525 : }
526 : } else {
527 0 : ClearOneof(message2, oneof_descriptor);
528 : }
529 0 : }
530 :
531 0 : void GeneratedMessageReflection::Swap(
532 : Message* message1,
533 : Message* message2) const {
534 0 : if (message1 == message2) return;
535 :
536 : // TODO(kenton): Other Reflection methods should probably check this too.
537 0 : GOOGLE_CHECK_EQ(message1->GetReflection(), this)
538 0 : << "First argument to Swap() (of type \""
539 0 : << message1->GetDescriptor()->full_name()
540 0 : << "\") is not compatible with this reflection object (which is for type \""
541 0 : << descriptor_->full_name()
542 : << "\"). Note that the exact same class is required; not just the same "
543 0 : "descriptor.";
544 0 : GOOGLE_CHECK_EQ(message2->GetReflection(), this)
545 0 : << "Second argument to Swap() (of type \""
546 0 : << message2->GetDescriptor()->full_name()
547 0 : << "\") is not compatible with this reflection object (which is for type \""
548 0 : << descriptor_->full_name()
549 : << "\"). Note that the exact same class is required; not just the same "
550 0 : "descriptor.";
551 :
552 0 : uint32* has_bits1 = MutableHasBits(message1);
553 0 : uint32* has_bits2 = MutableHasBits(message2);
554 0 : int has_bits_size = (descriptor_->field_count() + 31) / 32;
555 :
556 0 : for (int i = 0; i < has_bits_size; i++) {
557 0 : std::swap(has_bits1[i], has_bits2[i]);
558 : }
559 :
560 0 : for (int i = 0; i < descriptor_->field_count(); i++) {
561 0 : const FieldDescriptor* field = descriptor_->field(i);
562 0 : if (!field->containing_oneof()) {
563 0 : SwapField(message1, message2, field);
564 : }
565 : }
566 :
567 0 : for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
568 0 : SwapOneofField(message1, message2, descriptor_->oneof_decl(i));
569 : }
570 :
571 0 : if (extensions_offset_ != -1) {
572 0 : MutableExtensionSet(message1)->Swap(MutableExtensionSet(message2));
573 : }
574 :
575 0 : MutableUnknownFields(message1)->Swap(MutableUnknownFields(message2));
576 : }
577 :
578 0 : void GeneratedMessageReflection::SwapFields(
579 : Message* message1,
580 : Message* message2,
581 : const vector<const FieldDescriptor*>& fields) const {
582 0 : if (message1 == message2) return;
583 :
584 : // TODO(kenton): Other Reflection methods should probably check this too.
585 0 : GOOGLE_CHECK_EQ(message1->GetReflection(), this)
586 0 : << "First argument to SwapFields() (of type \""
587 0 : << message1->GetDescriptor()->full_name()
588 0 : << "\") is not compatible with this reflection object (which is for type \""
589 0 : << descriptor_->full_name()
590 : << "\"). Note that the exact same class is required; not just the same "
591 0 : "descriptor.";
592 0 : GOOGLE_CHECK_EQ(message2->GetReflection(), this)
593 0 : << "Second argument to SwapFields() (of type \""
594 0 : << message2->GetDescriptor()->full_name()
595 0 : << "\") is not compatible with this reflection object (which is for type \""
596 0 : << descriptor_->full_name()
597 : << "\"). Note that the exact same class is required; not just the same "
598 0 : "descriptor.";
599 :
600 0 : std::set<int> swapped_oneof;
601 :
602 0 : for (int i = 0; i < fields.size(); i++) {
603 0 : const FieldDescriptor* field = fields[i];
604 0 : if (field->is_extension()) {
605 0 : MutableExtensionSet(message1)->SwapExtension(
606 : MutableExtensionSet(message2),
607 0 : field->number());
608 : } else {
609 0 : if (field->containing_oneof()) {
610 0 : int oneof_index = field->containing_oneof()->index();
611 : // Only swap the oneof field once.
612 0 : if (swapped_oneof.find(oneof_index) != swapped_oneof.end()) {
613 0 : continue;
614 : }
615 0 : swapped_oneof.insert(oneof_index);
616 0 : SwapOneofField(message1, message2, field->containing_oneof());
617 : } else {
618 : // Swap has bit.
619 0 : SwapBit(message1, message2, field);
620 : // Swap field.
621 0 : SwapField(message1, message2, field);
622 : }
623 : }
624 : }
625 : }
626 :
627 : // -------------------------------------------------------------------
628 :
629 0 : bool GeneratedMessageReflection::HasField(const Message& message,
630 : const FieldDescriptor* field) const {
631 0 : USAGE_CHECK_MESSAGE_TYPE(HasField);
632 0 : USAGE_CHECK_SINGULAR(HasField);
633 :
634 0 : if (field->is_extension()) {
635 0 : return GetExtensionSet(message).Has(field->number());
636 : } else {
637 0 : if (field->containing_oneof()) {
638 0 : return HasOneofField(message, field);
639 : } else {
640 0 : return HasBit(message, field);
641 : }
642 : }
643 : }
644 :
645 0 : int GeneratedMessageReflection::FieldSize(const Message& message,
646 : const FieldDescriptor* field) const {
647 0 : USAGE_CHECK_MESSAGE_TYPE(FieldSize);
648 0 : USAGE_CHECK_REPEATED(FieldSize);
649 :
650 0 : if (field->is_extension()) {
651 0 : return GetExtensionSet(message).ExtensionSize(field->number());
652 : } else {
653 0 : switch (field->cpp_type()) {
654 : #define HANDLE_TYPE(UPPERCASE, LOWERCASE) \
655 : case FieldDescriptor::CPPTYPE_##UPPERCASE : \
656 : return GetRaw<RepeatedField<LOWERCASE> >(message, field).size()
657 :
658 0 : HANDLE_TYPE( INT32, int32);
659 0 : HANDLE_TYPE( INT64, int64);
660 0 : HANDLE_TYPE(UINT32, uint32);
661 0 : HANDLE_TYPE(UINT64, uint64);
662 0 : HANDLE_TYPE(DOUBLE, double);
663 0 : HANDLE_TYPE( FLOAT, float);
664 0 : HANDLE_TYPE( BOOL, bool);
665 0 : HANDLE_TYPE( ENUM, int);
666 : #undef HANDLE_TYPE
667 :
668 : case FieldDescriptor::CPPTYPE_STRING:
669 : case FieldDescriptor::CPPTYPE_MESSAGE:
670 0 : return GetRaw<RepeatedPtrFieldBase>(message, field).size();
671 : }
672 :
673 0 : GOOGLE_LOG(FATAL) << "Can't get here.";
674 0 : return 0;
675 : }
676 : }
677 :
678 0 : void GeneratedMessageReflection::ClearField(
679 : Message* message, const FieldDescriptor* field) const {
680 0 : USAGE_CHECK_MESSAGE_TYPE(ClearField);
681 :
682 0 : if (field->is_extension()) {
683 0 : MutableExtensionSet(message)->ClearExtension(field->number());
684 0 : } else if (!field->is_repeated()) {
685 0 : if (field->containing_oneof()) {
686 0 : ClearOneofField(message, field);
687 0 : return;
688 : }
689 :
690 0 : if (HasBit(*message, field)) {
691 0 : ClearBit(message, field);
692 :
693 : // We need to set the field back to its default value.
694 0 : switch (field->cpp_type()) {
695 : #define CLEAR_TYPE(CPPTYPE, TYPE) \
696 : case FieldDescriptor::CPPTYPE_##CPPTYPE: \
697 : *MutableRaw<TYPE>(message, field) = \
698 : field->default_value_##TYPE(); \
699 : break;
700 :
701 0 : CLEAR_TYPE(INT32 , int32 );
702 0 : CLEAR_TYPE(INT64 , int64 );
703 0 : CLEAR_TYPE(UINT32, uint32);
704 0 : CLEAR_TYPE(UINT64, uint64);
705 0 : CLEAR_TYPE(FLOAT , float );
706 0 : CLEAR_TYPE(DOUBLE, double);
707 0 : CLEAR_TYPE(BOOL , bool );
708 : #undef CLEAR_TYPE
709 :
710 : case FieldDescriptor::CPPTYPE_ENUM:
711 0 : *MutableRaw<int>(message, field) =
712 0 : field->default_value_enum()->number();
713 0 : break;
714 :
715 : case FieldDescriptor::CPPTYPE_STRING: {
716 0 : switch (field->options().ctype()) {
717 : default: // TODO(kenton): Support other string reps.
718 : case FieldOptions::STRING:
719 0 : const string* default_ptr = DefaultRaw<const string*>(field);
720 0 : string** value = MutableRaw<string*>(message, field);
721 0 : if (*value != default_ptr) {
722 0 : if (field->has_default_value()) {
723 0 : (*value)->assign(field->default_value_string());
724 : } else {
725 0 : (*value)->clear();
726 : }
727 : }
728 0 : break;
729 : }
730 0 : break;
731 : }
732 :
733 : case FieldDescriptor::CPPTYPE_MESSAGE:
734 0 : (*MutableRaw<Message*>(message, field))->Clear();
735 0 : break;
736 : }
737 : }
738 : } else {
739 0 : switch (field->cpp_type()) {
740 : #define HANDLE_TYPE(UPPERCASE, LOWERCASE) \
741 : case FieldDescriptor::CPPTYPE_##UPPERCASE : \
742 : MutableRaw<RepeatedField<LOWERCASE> >(message, field)->Clear(); \
743 : break
744 :
745 0 : HANDLE_TYPE( INT32, int32);
746 0 : HANDLE_TYPE( INT64, int64);
747 0 : HANDLE_TYPE(UINT32, uint32);
748 0 : HANDLE_TYPE(UINT64, uint64);
749 0 : HANDLE_TYPE(DOUBLE, double);
750 0 : HANDLE_TYPE( FLOAT, float);
751 0 : HANDLE_TYPE( BOOL, bool);
752 0 : HANDLE_TYPE( ENUM, int);
753 : #undef HANDLE_TYPE
754 :
755 : case FieldDescriptor::CPPTYPE_STRING: {
756 0 : switch (field->options().ctype()) {
757 : default: // TODO(kenton): Support other string reps.
758 : case FieldOptions::STRING:
759 0 : MutableRaw<RepeatedPtrField<string> >(message, field)->Clear();
760 0 : break;
761 : }
762 0 : break;
763 : }
764 :
765 : case FieldDescriptor::CPPTYPE_MESSAGE: {
766 : // We don't know which subclass of RepeatedPtrFieldBase the type is,
767 : // so we use RepeatedPtrFieldBase directly.
768 : MutableRaw<RepeatedPtrFieldBase>(message, field)
769 0 : ->Clear<GenericTypeHandler<Message> >();
770 0 : break;
771 : }
772 : }
773 : }
774 : }
775 :
776 0 : void GeneratedMessageReflection::RemoveLast(
777 : Message* message,
778 : const FieldDescriptor* field) const {
779 0 : USAGE_CHECK_MESSAGE_TYPE(RemoveLast);
780 0 : USAGE_CHECK_REPEATED(RemoveLast);
781 :
782 0 : if (field->is_extension()) {
783 0 : MutableExtensionSet(message)->RemoveLast(field->number());
784 : } else {
785 0 : switch (field->cpp_type()) {
786 : #define HANDLE_TYPE(UPPERCASE, LOWERCASE) \
787 : case FieldDescriptor::CPPTYPE_##UPPERCASE : \
788 : MutableRaw<RepeatedField<LOWERCASE> >(message, field)->RemoveLast(); \
789 : break
790 :
791 0 : HANDLE_TYPE( INT32, int32);
792 0 : HANDLE_TYPE( INT64, int64);
793 0 : HANDLE_TYPE(UINT32, uint32);
794 0 : HANDLE_TYPE(UINT64, uint64);
795 0 : HANDLE_TYPE(DOUBLE, double);
796 0 : HANDLE_TYPE( FLOAT, float);
797 0 : HANDLE_TYPE( BOOL, bool);
798 0 : HANDLE_TYPE( ENUM, int);
799 : #undef HANDLE_TYPE
800 :
801 : case FieldDescriptor::CPPTYPE_STRING:
802 0 : switch (field->options().ctype()) {
803 : default: // TODO(kenton): Support other string reps.
804 : case FieldOptions::STRING:
805 0 : MutableRaw<RepeatedPtrField<string> >(message, field)->RemoveLast();
806 0 : break;
807 : }
808 0 : break;
809 :
810 : case FieldDescriptor::CPPTYPE_MESSAGE:
811 : MutableRaw<RepeatedPtrFieldBase>(message, field)
812 0 : ->RemoveLast<GenericTypeHandler<Message> >();
813 0 : break;
814 : }
815 : }
816 0 : }
817 :
818 0 : Message* GeneratedMessageReflection::ReleaseLast(
819 : Message* message,
820 : const FieldDescriptor* field) const {
821 0 : USAGE_CHECK_ALL(ReleaseLast, REPEATED, MESSAGE);
822 :
823 0 : if (field->is_extension()) {
824 : return static_cast<Message*>(
825 0 : MutableExtensionSet(message)->ReleaseLast(field->number()));
826 : } else {
827 : return MutableRaw<RepeatedPtrFieldBase>(message, field)
828 0 : ->ReleaseLast<GenericTypeHandler<Message> >();
829 : }
830 : }
831 :
832 0 : void GeneratedMessageReflection::SwapElements(
833 : Message* message,
834 : const FieldDescriptor* field,
835 : int index1,
836 : int index2) const {
837 0 : USAGE_CHECK_MESSAGE_TYPE(Swap);
838 0 : USAGE_CHECK_REPEATED(Swap);
839 :
840 0 : if (field->is_extension()) {
841 0 : MutableExtensionSet(message)->SwapElements(field->number(), index1, index2);
842 : } else {
843 0 : switch (field->cpp_type()) {
844 : #define HANDLE_TYPE(UPPERCASE, LOWERCASE) \
845 : case FieldDescriptor::CPPTYPE_##UPPERCASE : \
846 : MutableRaw<RepeatedField<LOWERCASE> >(message, field) \
847 : ->SwapElements(index1, index2); \
848 : break
849 :
850 0 : HANDLE_TYPE( INT32, int32);
851 0 : HANDLE_TYPE( INT64, int64);
852 0 : HANDLE_TYPE(UINT32, uint32);
853 0 : HANDLE_TYPE(UINT64, uint64);
854 0 : HANDLE_TYPE(DOUBLE, double);
855 0 : HANDLE_TYPE( FLOAT, float);
856 0 : HANDLE_TYPE( BOOL, bool);
857 0 : HANDLE_TYPE( ENUM, int);
858 : #undef HANDLE_TYPE
859 :
860 : case FieldDescriptor::CPPTYPE_STRING:
861 : case FieldDescriptor::CPPTYPE_MESSAGE:
862 : MutableRaw<RepeatedPtrFieldBase>(message, field)
863 0 : ->SwapElements(index1, index2);
864 0 : break;
865 : }
866 : }
867 0 : }
868 :
869 : namespace {
870 : // Comparison functor for sorting FieldDescriptors by field number.
871 : struct FieldNumberSorter {
872 0 : bool operator()(const FieldDescriptor* left,
873 : const FieldDescriptor* right) const {
874 0 : return left->number() < right->number();
875 : }
876 : };
877 : } // namespace
878 :
879 0 : void GeneratedMessageReflection::ListFields(
880 : const Message& message,
881 : vector<const FieldDescriptor*>* output) const {
882 0 : output->clear();
883 :
884 : // Optimization: The default instance never has any fields set.
885 0 : if (&message == default_instance_) return;
886 :
887 0 : for (int i = 0; i < descriptor_->field_count(); i++) {
888 0 : const FieldDescriptor* field = descriptor_->field(i);
889 0 : if (field->is_repeated()) {
890 0 : if (FieldSize(message, field) > 0) {
891 0 : output->push_back(field);
892 : }
893 : } else {
894 0 : if (field->containing_oneof()) {
895 0 : if (HasOneofField(message, field)) {
896 0 : output->push_back(field);
897 : }
898 0 : } else if (HasBit(message, field)) {
899 0 : output->push_back(field);
900 : }
901 : }
902 : }
903 :
904 0 : if (extensions_offset_ != -1) {
905 0 : GetExtensionSet(message).AppendToList(descriptor_, descriptor_pool_,
906 0 : output);
907 : }
908 :
909 : // ListFields() must sort output by field number.
910 0 : sort(output->begin(), output->end(), FieldNumberSorter());
911 : }
912 :
913 : // -------------------------------------------------------------------
914 :
915 : #undef DEFINE_PRIMITIVE_ACCESSORS
916 : #define DEFINE_PRIMITIVE_ACCESSORS(TYPENAME, TYPE, PASSTYPE, CPPTYPE) \
917 : PASSTYPE GeneratedMessageReflection::Get##TYPENAME( \
918 : const Message& message, const FieldDescriptor* field) const { \
919 : USAGE_CHECK_ALL(Get##TYPENAME, SINGULAR, CPPTYPE); \
920 : if (field->is_extension()) { \
921 : return GetExtensionSet(message).Get##TYPENAME( \
922 : field->number(), field->default_value_##PASSTYPE()); \
923 : } else { \
924 : return GetField<TYPE>(message, field); \
925 : } \
926 : } \
927 : \
928 : void GeneratedMessageReflection::Set##TYPENAME( \
929 : Message* message, const FieldDescriptor* field, \
930 : PASSTYPE value) const { \
931 : USAGE_CHECK_ALL(Set##TYPENAME, SINGULAR, CPPTYPE); \
932 : if (field->is_extension()) { \
933 : return MutableExtensionSet(message)->Set##TYPENAME( \
934 : field->number(), field->type(), value, field); \
935 : } else { \
936 : SetField<TYPE>(message, field, value); \
937 : } \
938 : } \
939 : \
940 : PASSTYPE GeneratedMessageReflection::GetRepeated##TYPENAME( \
941 : const Message& message, \
942 : const FieldDescriptor* field, int index) const { \
943 : USAGE_CHECK_ALL(GetRepeated##TYPENAME, REPEATED, CPPTYPE); \
944 : if (field->is_extension()) { \
945 : return GetExtensionSet(message).GetRepeated##TYPENAME( \
946 : field->number(), index); \
947 : } else { \
948 : return GetRepeatedField<TYPE>(message, field, index); \
949 : } \
950 : } \
951 : \
952 : void GeneratedMessageReflection::SetRepeated##TYPENAME( \
953 : Message* message, const FieldDescriptor* field, \
954 : int index, PASSTYPE value) const { \
955 : USAGE_CHECK_ALL(SetRepeated##TYPENAME, REPEATED, CPPTYPE); \
956 : if (field->is_extension()) { \
957 : MutableExtensionSet(message)->SetRepeated##TYPENAME( \
958 : field->number(), index, value); \
959 : } else { \
960 : SetRepeatedField<TYPE>(message, field, index, value); \
961 : } \
962 : } \
963 : \
964 : void GeneratedMessageReflection::Add##TYPENAME( \
965 : Message* message, const FieldDescriptor* field, \
966 : PASSTYPE value) const { \
967 : USAGE_CHECK_ALL(Add##TYPENAME, REPEATED, CPPTYPE); \
968 : if (field->is_extension()) { \
969 : MutableExtensionSet(message)->Add##TYPENAME( \
970 : field->number(), field->type(), field->options().packed(), value, \
971 : field); \
972 : } else { \
973 : AddField<TYPE>(message, field, value); \
974 : } \
975 : }
976 :
977 0 : DEFINE_PRIMITIVE_ACCESSORS(Int32 , int32 , int32 , INT32 )
978 0 : DEFINE_PRIMITIVE_ACCESSORS(Int64 , int64 , int64 , INT64 )
979 0 : DEFINE_PRIMITIVE_ACCESSORS(UInt32, uint32, uint32, UINT32)
980 0 : DEFINE_PRIMITIVE_ACCESSORS(UInt64, uint64, uint64, UINT64)
981 0 : DEFINE_PRIMITIVE_ACCESSORS(Float , float , float , FLOAT )
982 0 : DEFINE_PRIMITIVE_ACCESSORS(Double, double, double, DOUBLE)
983 0 : DEFINE_PRIMITIVE_ACCESSORS(Bool , bool , bool , BOOL )
984 : #undef DEFINE_PRIMITIVE_ACCESSORS
985 :
986 : // -------------------------------------------------------------------
987 :
988 0 : string GeneratedMessageReflection::GetString(
989 : const Message& message, const FieldDescriptor* field) const {
990 0 : USAGE_CHECK_ALL(GetString, SINGULAR, STRING);
991 0 : if (field->is_extension()) {
992 0 : return GetExtensionSet(message).GetString(field->number(),
993 0 : field->default_value_string());
994 : } else {
995 0 : switch (field->options().ctype()) {
996 : default: // TODO(kenton): Support other string reps.
997 : case FieldOptions::STRING:
998 0 : return *GetField<const string*>(message, field);
999 : }
1000 :
1001 : GOOGLE_LOG(FATAL) << "Can't get here.";
1002 : return GetEmptyString(); // Make compiler happy.
1003 : }
1004 : }
1005 :
1006 0 : const string& GeneratedMessageReflection::GetStringReference(
1007 : const Message& message,
1008 : const FieldDescriptor* field, string* scratch) const {
1009 0 : USAGE_CHECK_ALL(GetStringReference, SINGULAR, STRING);
1010 0 : if (field->is_extension()) {
1011 0 : return GetExtensionSet(message).GetString(field->number(),
1012 0 : field->default_value_string());
1013 : } else {
1014 0 : switch (field->options().ctype()) {
1015 : default: // TODO(kenton): Support other string reps.
1016 : case FieldOptions::STRING:
1017 0 : return *GetField<const string*>(message, field);
1018 : }
1019 :
1020 : GOOGLE_LOG(FATAL) << "Can't get here.";
1021 : return GetEmptyString(); // Make compiler happy.
1022 : }
1023 : }
1024 :
1025 :
1026 0 : void GeneratedMessageReflection::SetString(
1027 : Message* message, const FieldDescriptor* field,
1028 : const string& value) const {
1029 0 : USAGE_CHECK_ALL(SetString, SINGULAR, STRING);
1030 0 : if (field->is_extension()) {
1031 0 : return MutableExtensionSet(message)->SetString(field->number(),
1032 0 : field->type(), value, field);
1033 : } else {
1034 0 : switch (field->options().ctype()) {
1035 : default: // TODO(kenton): Support other string reps.
1036 : case FieldOptions::STRING: {
1037 0 : if (field->containing_oneof() && !HasOneofField(*message, field)) {
1038 0 : ClearOneof(message, field->containing_oneof());
1039 0 : *MutableField<string*>(message, field) = new string;
1040 : }
1041 0 : string** ptr = MutableField<string*>(message, field);
1042 0 : if (*ptr == DefaultRaw<const string*>(field)) {
1043 0 : *ptr = new string(value);
1044 : } else {
1045 0 : (*ptr)->assign(value);
1046 : }
1047 0 : break;
1048 : }
1049 : }
1050 : }
1051 : }
1052 :
1053 :
1054 0 : string GeneratedMessageReflection::GetRepeatedString(
1055 : const Message& message, const FieldDescriptor* field, int index) const {
1056 0 : USAGE_CHECK_ALL(GetRepeatedString, REPEATED, STRING);
1057 0 : if (field->is_extension()) {
1058 0 : return GetExtensionSet(message).GetRepeatedString(field->number(), index);
1059 : } else {
1060 0 : switch (field->options().ctype()) {
1061 : default: // TODO(kenton): Support other string reps.
1062 : case FieldOptions::STRING:
1063 0 : return GetRepeatedPtrField<string>(message, field, index);
1064 : }
1065 :
1066 : GOOGLE_LOG(FATAL) << "Can't get here.";
1067 : return GetEmptyString(); // Make compiler happy.
1068 : }
1069 : }
1070 :
1071 0 : const string& GeneratedMessageReflection::GetRepeatedStringReference(
1072 : const Message& message, const FieldDescriptor* field,
1073 : int index, string* scratch) const {
1074 0 : USAGE_CHECK_ALL(GetRepeatedStringReference, REPEATED, STRING);
1075 0 : if (field->is_extension()) {
1076 0 : return GetExtensionSet(message).GetRepeatedString(field->number(), index);
1077 : } else {
1078 0 : switch (field->options().ctype()) {
1079 : default: // TODO(kenton): Support other string reps.
1080 : case FieldOptions::STRING:
1081 0 : return GetRepeatedPtrField<string>(message, field, index);
1082 : }
1083 :
1084 : GOOGLE_LOG(FATAL) << "Can't get here.";
1085 : return GetEmptyString(); // Make compiler happy.
1086 : }
1087 : }
1088 :
1089 :
1090 0 : void GeneratedMessageReflection::SetRepeatedString(
1091 : Message* message, const FieldDescriptor* field,
1092 : int index, const string& value) const {
1093 0 : USAGE_CHECK_ALL(SetRepeatedString, REPEATED, STRING);
1094 0 : if (field->is_extension()) {
1095 0 : MutableExtensionSet(message)->SetRepeatedString(
1096 0 : field->number(), index, value);
1097 : } else {
1098 0 : switch (field->options().ctype()) {
1099 : default: // TODO(kenton): Support other string reps.
1100 : case FieldOptions::STRING:
1101 0 : *MutableRepeatedField<string>(message, field, index) = value;
1102 0 : break;
1103 : }
1104 : }
1105 0 : }
1106 :
1107 :
1108 0 : void GeneratedMessageReflection::AddString(
1109 : Message* message, const FieldDescriptor* field,
1110 : const string& value) const {
1111 0 : USAGE_CHECK_ALL(AddString, REPEATED, STRING);
1112 0 : if (field->is_extension()) {
1113 0 : MutableExtensionSet(message)->AddString(field->number(),
1114 0 : field->type(), value, field);
1115 : } else {
1116 0 : switch (field->options().ctype()) {
1117 : default: // TODO(kenton): Support other string reps.
1118 : case FieldOptions::STRING:
1119 0 : *AddField<string>(message, field) = value;
1120 0 : break;
1121 : }
1122 : }
1123 0 : }
1124 :
1125 :
1126 : // -------------------------------------------------------------------
1127 :
1128 0 : const EnumValueDescriptor* GeneratedMessageReflection::GetEnum(
1129 : const Message& message, const FieldDescriptor* field) const {
1130 0 : USAGE_CHECK_ALL(GetEnum, SINGULAR, ENUM);
1131 :
1132 : int value;
1133 0 : if (field->is_extension()) {
1134 0 : value = GetExtensionSet(message).GetEnum(
1135 0 : field->number(), field->default_value_enum()->number());
1136 : } else {
1137 0 : value = GetField<int>(message, field);
1138 : }
1139 : const EnumValueDescriptor* result =
1140 0 : field->enum_type()->FindValueByNumber(value);
1141 0 : GOOGLE_CHECK(result != NULL) << "Value " << value << " is not valid for field "
1142 0 : << field->full_name() << " of type "
1143 0 : << field->enum_type()->full_name() << ".";
1144 0 : return result;
1145 : }
1146 :
1147 0 : void GeneratedMessageReflection::SetEnum(
1148 : Message* message, const FieldDescriptor* field,
1149 : const EnumValueDescriptor* value) const {
1150 0 : USAGE_CHECK_ALL(SetEnum, SINGULAR, ENUM);
1151 0 : USAGE_CHECK_ENUM_VALUE(SetEnum);
1152 :
1153 0 : if (field->is_extension()) {
1154 0 : MutableExtensionSet(message)->SetEnum(field->number(), field->type(),
1155 0 : value->number(), field);
1156 : } else {
1157 0 : SetField<int>(message, field, value->number());
1158 : }
1159 0 : }
1160 :
1161 0 : const EnumValueDescriptor* GeneratedMessageReflection::GetRepeatedEnum(
1162 : const Message& message, const FieldDescriptor* field, int index) const {
1163 0 : USAGE_CHECK_ALL(GetRepeatedEnum, REPEATED, ENUM);
1164 :
1165 : int value;
1166 0 : if (field->is_extension()) {
1167 0 : value = GetExtensionSet(message).GetRepeatedEnum(field->number(), index);
1168 : } else {
1169 0 : value = GetRepeatedField<int>(message, field, index);
1170 : }
1171 : const EnumValueDescriptor* result =
1172 0 : field->enum_type()->FindValueByNumber(value);
1173 0 : GOOGLE_CHECK(result != NULL) << "Value " << value << " is not valid for field "
1174 0 : << field->full_name() << " of type "
1175 0 : << field->enum_type()->full_name() << ".";
1176 0 : return result;
1177 : }
1178 :
1179 0 : void GeneratedMessageReflection::SetRepeatedEnum(
1180 : Message* message,
1181 : const FieldDescriptor* field, int index,
1182 : const EnumValueDescriptor* value) const {
1183 0 : USAGE_CHECK_ALL(SetRepeatedEnum, REPEATED, ENUM);
1184 0 : USAGE_CHECK_ENUM_VALUE(SetRepeatedEnum);
1185 :
1186 0 : if (field->is_extension()) {
1187 0 : MutableExtensionSet(message)->SetRepeatedEnum(
1188 0 : field->number(), index, value->number());
1189 : } else {
1190 0 : SetRepeatedField<int>(message, field, index, value->number());
1191 : }
1192 0 : }
1193 :
1194 0 : void GeneratedMessageReflection::AddEnum(
1195 : Message* message, const FieldDescriptor* field,
1196 : const EnumValueDescriptor* value) const {
1197 0 : USAGE_CHECK_ALL(AddEnum, REPEATED, ENUM);
1198 0 : USAGE_CHECK_ENUM_VALUE(AddEnum);
1199 :
1200 0 : if (field->is_extension()) {
1201 0 : MutableExtensionSet(message)->AddEnum(field->number(), field->type(),
1202 0 : field->options().packed(),
1203 0 : value->number(), field);
1204 : } else {
1205 0 : AddField<int>(message, field, value->number());
1206 : }
1207 0 : }
1208 :
1209 : // -------------------------------------------------------------------
1210 :
1211 0 : const Message& GeneratedMessageReflection::GetMessage(
1212 : const Message& message, const FieldDescriptor* field,
1213 : MessageFactory* factory) const {
1214 0 : USAGE_CHECK_ALL(GetMessage, SINGULAR, MESSAGE);
1215 :
1216 0 : if (factory == NULL) factory = message_factory_;
1217 :
1218 0 : if (field->is_extension()) {
1219 : return static_cast<const Message&>(
1220 0 : GetExtensionSet(message).GetMessage(
1221 0 : field->number(), field->message_type(), factory));
1222 : } else {
1223 : const Message* result;
1224 0 : result = GetRaw<const Message*>(message, field);
1225 0 : if (result == NULL) {
1226 0 : result = DefaultRaw<const Message*>(field);
1227 : }
1228 0 : return *result;
1229 : }
1230 : }
1231 :
1232 0 : Message* GeneratedMessageReflection::MutableMessage(
1233 : Message* message, const FieldDescriptor* field,
1234 : MessageFactory* factory) const {
1235 0 : if (factory == NULL) factory = message_factory_;
1236 :
1237 0 : if (field->is_extension()) {
1238 : return static_cast<Message*>(
1239 0 : MutableExtensionSet(message)->MutableMessage(field, factory));
1240 : } else {
1241 : Message* result;
1242 0 : Message** result_holder = MutableRaw<Message*>(message, field);
1243 :
1244 0 : if (field->containing_oneof()) {
1245 0 : if (!HasOneofField(*message, field)) {
1246 0 : ClearOneof(message, field->containing_oneof());
1247 0 : result_holder = MutableField<Message*>(message, field);
1248 0 : const Message* default_message = DefaultRaw<const Message*>(field);
1249 0 : *result_holder = default_message->New();
1250 : }
1251 : } else {
1252 0 : SetBit(message, field);
1253 : }
1254 :
1255 0 : if (*result_holder == NULL) {
1256 0 : const Message* default_message = DefaultRaw<const Message*>(field);
1257 0 : *result_holder = default_message->New();
1258 : }
1259 0 : result = *result_holder;
1260 0 : return result;
1261 : }
1262 : }
1263 :
1264 0 : void GeneratedMessageReflection::SetAllocatedMessage(
1265 : Message* message,
1266 : Message* sub_message,
1267 : const FieldDescriptor* field) const {
1268 0 : USAGE_CHECK_ALL(SetAllocatedMessage, SINGULAR, MESSAGE);
1269 :
1270 0 : if (field->is_extension()) {
1271 0 : MutableExtensionSet(message)->SetAllocatedMessage(
1272 0 : field->number(), field->type(), field, sub_message);
1273 : } else {
1274 0 : if (field->containing_oneof()) {
1275 0 : if (sub_message == NULL) {
1276 0 : ClearOneof(message, field->containing_oneof());
1277 0 : return;
1278 : }
1279 0 : ClearOneof(message, field->containing_oneof());
1280 0 : *MutableRaw<Message*>(message, field) = sub_message;
1281 0 : SetOneofCase(message, field);
1282 0 : return;
1283 : }
1284 :
1285 0 : if (sub_message == NULL) {
1286 0 : ClearBit(message, field);
1287 : } else {
1288 0 : SetBit(message, field);
1289 : }
1290 0 : Message** sub_message_holder = MutableRaw<Message*>(message, field);
1291 0 : delete *sub_message_holder;
1292 0 : *sub_message_holder = sub_message;
1293 : }
1294 : }
1295 :
1296 0 : Message* GeneratedMessageReflection::ReleaseMessage(
1297 : Message* message,
1298 : const FieldDescriptor* field,
1299 : MessageFactory* factory) const {
1300 0 : USAGE_CHECK_ALL(ReleaseMessage, SINGULAR, MESSAGE);
1301 :
1302 0 : if (factory == NULL) factory = message_factory_;
1303 :
1304 0 : if (field->is_extension()) {
1305 : return static_cast<Message*>(
1306 0 : MutableExtensionSet(message)->ReleaseMessage(field, factory));
1307 : } else {
1308 0 : ClearBit(message, field);
1309 0 : if (field->containing_oneof()) {
1310 0 : if (HasOneofField(*message, field)) {
1311 0 : *MutableOneofCase(message, field->containing_oneof()) = 0;
1312 : } else {
1313 0 : return NULL;
1314 : }
1315 : }
1316 0 : Message** result = MutableRaw<Message*>(message, field);
1317 0 : Message* ret = *result;
1318 0 : *result = NULL;
1319 0 : return ret;
1320 : }
1321 : }
1322 :
1323 0 : const Message& GeneratedMessageReflection::GetRepeatedMessage(
1324 : const Message& message, const FieldDescriptor* field, int index) const {
1325 0 : USAGE_CHECK_ALL(GetRepeatedMessage, REPEATED, MESSAGE);
1326 :
1327 0 : if (field->is_extension()) {
1328 : return static_cast<const Message&>(
1329 0 : GetExtensionSet(message).GetRepeatedMessage(field->number(), index));
1330 : } else {
1331 0 : return GetRaw<RepeatedPtrFieldBase>(message, field)
1332 0 : .Get<GenericTypeHandler<Message> >(index);
1333 : }
1334 : }
1335 :
1336 0 : Message* GeneratedMessageReflection::MutableRepeatedMessage(
1337 : Message* message, const FieldDescriptor* field, int index) const {
1338 0 : USAGE_CHECK_ALL(MutableRepeatedMessage, REPEATED, MESSAGE);
1339 :
1340 0 : if (field->is_extension()) {
1341 : return static_cast<Message*>(
1342 0 : MutableExtensionSet(message)->MutableRepeatedMessage(
1343 0 : field->number(), index));
1344 : } else {
1345 : return MutableRaw<RepeatedPtrFieldBase>(message, field)
1346 0 : ->Mutable<GenericTypeHandler<Message> >(index);
1347 : }
1348 : }
1349 :
1350 0 : Message* GeneratedMessageReflection::AddMessage(
1351 : Message* message, const FieldDescriptor* field,
1352 : MessageFactory* factory) const {
1353 0 : USAGE_CHECK_ALL(AddMessage, REPEATED, MESSAGE);
1354 :
1355 0 : if (factory == NULL) factory = message_factory_;
1356 :
1357 0 : if (field->is_extension()) {
1358 : return static_cast<Message*>(
1359 0 : MutableExtensionSet(message)->AddMessage(field, factory));
1360 : } else {
1361 : // We can't use AddField<Message>() because RepeatedPtrFieldBase doesn't
1362 : // know how to allocate one.
1363 : RepeatedPtrFieldBase* repeated =
1364 0 : MutableRaw<RepeatedPtrFieldBase>(message, field);
1365 0 : Message* result = repeated->AddFromCleared<GenericTypeHandler<Message> >();
1366 0 : if (result == NULL) {
1367 : // We must allocate a new object.
1368 : const Message* prototype;
1369 0 : if (repeated->size() == 0) {
1370 0 : prototype = factory->GetPrototype(field->message_type());
1371 : } else {
1372 0 : prototype = &repeated->Get<GenericTypeHandler<Message> >(0);
1373 : }
1374 0 : result = prototype->New();
1375 0 : repeated->AddAllocated<GenericTypeHandler<Message> >(result);
1376 : }
1377 0 : return result;
1378 : }
1379 : }
1380 :
1381 0 : void* GeneratedMessageReflection::MutableRawRepeatedField(
1382 : Message* message, const FieldDescriptor* field,
1383 : FieldDescriptor::CppType cpptype,
1384 : int ctype, const Descriptor* desc) const {
1385 0 : USAGE_CHECK_REPEATED("MutableRawRepeatedField");
1386 0 : if (field->cpp_type() != cpptype)
1387 0 : ReportReflectionUsageTypeError(descriptor_,
1388 0 : field, "MutableRawRepeatedField", cpptype);
1389 0 : if (ctype >= 0)
1390 0 : GOOGLE_CHECK_EQ(field->options().ctype(), ctype) << "subtype mismatch";
1391 0 : if (desc != NULL)
1392 0 : GOOGLE_CHECK_EQ(field->message_type(), desc) << "wrong submessage type";
1393 0 : if (field->is_extension())
1394 0 : return MutableExtensionSet(message)->MutableRawRepeatedField(
1395 0 : field->number(), field->type(), field->is_packed(), field);
1396 : else
1397 0 : return reinterpret_cast<uint8*>(message) + offsets_[field->index()];
1398 : }
1399 :
1400 0 : const FieldDescriptor* GeneratedMessageReflection::GetOneofFieldDescriptor(
1401 : const Message& message,
1402 : const OneofDescriptor* oneof_descriptor) const {
1403 0 : uint32 field_number = GetOneofCase(message, oneof_descriptor);
1404 0 : if (field_number == 0) {
1405 0 : return NULL;
1406 : }
1407 0 : return descriptor_->FindFieldByNumber(field_number);
1408 : }
1409 :
1410 : // -----------------------------------------------------------------------------
1411 :
1412 0 : const FieldDescriptor* GeneratedMessageReflection::FindKnownExtensionByName(
1413 : const string& name) const {
1414 0 : if (extensions_offset_ == -1) return NULL;
1415 :
1416 0 : const FieldDescriptor* result = descriptor_pool_->FindExtensionByName(name);
1417 0 : if (result != NULL && result->containing_type() == descriptor_) {
1418 0 : return result;
1419 : }
1420 :
1421 0 : if (descriptor_->options().message_set_wire_format()) {
1422 : // MessageSet extensions may be identified by type name.
1423 0 : const Descriptor* type = descriptor_pool_->FindMessageTypeByName(name);
1424 0 : if (type != NULL) {
1425 : // Look for a matching extension in the foreign type's scope.
1426 0 : for (int i = 0; i < type->extension_count(); i++) {
1427 0 : const FieldDescriptor* extension = type->extension(i);
1428 0 : if (extension->containing_type() == descriptor_ &&
1429 0 : extension->type() == FieldDescriptor::TYPE_MESSAGE &&
1430 0 : extension->is_optional() &&
1431 0 : extension->message_type() == type) {
1432 : // Found it.
1433 0 : return extension;
1434 : }
1435 : }
1436 : }
1437 : }
1438 :
1439 0 : return NULL;
1440 : }
1441 :
1442 0 : const FieldDescriptor* GeneratedMessageReflection::FindKnownExtensionByNumber(
1443 : int number) const {
1444 0 : if (extensions_offset_ == -1) return NULL;
1445 0 : return descriptor_pool_->FindExtensionByNumber(descriptor_, number);
1446 : }
1447 :
1448 : // ===================================================================
1449 : // Some private helpers.
1450 :
1451 : // These simple template accessors obtain pointers (or references) to
1452 : // the given field.
1453 : template <typename Type>
1454 0 : inline const Type& GeneratedMessageReflection::GetRaw(
1455 : const Message& message, const FieldDescriptor* field) const {
1456 0 : if (field->containing_oneof() && !HasOneofField(message, field)) {
1457 0 : return DefaultRaw<Type>(field);
1458 : }
1459 0 : int index = field->containing_oneof() ?
1460 0 : descriptor_->field_count() + field->containing_oneof()->index() :
1461 0 : field->index();
1462 0 : const void* ptr = reinterpret_cast<const uint8*>(&message) +
1463 0 : offsets_[index];
1464 0 : return *reinterpret_cast<const Type*>(ptr);
1465 : }
1466 :
1467 : template <typename Type>
1468 0 : inline Type* GeneratedMessageReflection::MutableRaw(
1469 : Message* message, const FieldDescriptor* field) const {
1470 0 : int index = field->containing_oneof() ?
1471 0 : descriptor_->field_count() + field->containing_oneof()->index() :
1472 0 : field->index();
1473 0 : void* ptr = reinterpret_cast<uint8*>(message) + offsets_[index];
1474 0 : return reinterpret_cast<Type*>(ptr);
1475 : }
1476 :
1477 : template <typename Type>
1478 0 : inline const Type& GeneratedMessageReflection::DefaultRaw(
1479 : const FieldDescriptor* field) const {
1480 0 : const void* ptr = field->containing_oneof() ?
1481 0 : reinterpret_cast<const uint8*>(default_oneof_instance_) +
1482 0 : offsets_[field->index()] :
1483 0 : reinterpret_cast<const uint8*>(default_instance_) +
1484 0 : offsets_[field->index()];
1485 0 : return *reinterpret_cast<const Type*>(ptr);
1486 : }
1487 :
1488 0 : inline const uint32* GeneratedMessageReflection::GetHasBits(
1489 : const Message& message) const {
1490 0 : const void* ptr = reinterpret_cast<const uint8*>(&message) + has_bits_offset_;
1491 0 : return reinterpret_cast<const uint32*>(ptr);
1492 : }
1493 0 : inline uint32* GeneratedMessageReflection::MutableHasBits(
1494 : Message* message) const {
1495 0 : void* ptr = reinterpret_cast<uint8*>(message) + has_bits_offset_;
1496 0 : return reinterpret_cast<uint32*>(ptr);
1497 : }
1498 :
1499 0 : inline uint32 GeneratedMessageReflection::GetOneofCase(
1500 : const Message& message,
1501 : const OneofDescriptor* oneof_descriptor) const {
1502 : const void* ptr = reinterpret_cast<const uint8*>(&message)
1503 0 : + oneof_case_offset_;
1504 0 : return reinterpret_cast<const uint32*>(ptr)[oneof_descriptor->index()];
1505 : }
1506 :
1507 0 : inline uint32* GeneratedMessageReflection::MutableOneofCase(
1508 : Message* message,
1509 : const OneofDescriptor* oneof_descriptor) const {
1510 0 : void* ptr = reinterpret_cast<uint8*>(message) + oneof_case_offset_;
1511 0 : return &(reinterpret_cast<uint32*>(ptr)[oneof_descriptor->index()]);
1512 : }
1513 :
1514 0 : inline const ExtensionSet& GeneratedMessageReflection::GetExtensionSet(
1515 : const Message& message) const {
1516 0 : GOOGLE_DCHECK_NE(extensions_offset_, -1);
1517 : const void* ptr = reinterpret_cast<const uint8*>(&message) +
1518 0 : extensions_offset_;
1519 0 : return *reinterpret_cast<const ExtensionSet*>(ptr);
1520 : }
1521 0 : inline ExtensionSet* GeneratedMessageReflection::MutableExtensionSet(
1522 : Message* message) const {
1523 0 : GOOGLE_DCHECK_NE(extensions_offset_, -1);
1524 0 : void* ptr = reinterpret_cast<uint8*>(message) + extensions_offset_;
1525 0 : return reinterpret_cast<ExtensionSet*>(ptr);
1526 : }
1527 :
1528 : // Simple accessors for manipulating has_bits_.
1529 0 : inline bool GeneratedMessageReflection::HasBit(
1530 : const Message& message, const FieldDescriptor* field) const {
1531 0 : return GetHasBits(message)[field->index() / 32] &
1532 0 : (1 << (field->index() % 32));
1533 : }
1534 :
1535 0 : inline void GeneratedMessageReflection::SetBit(
1536 : Message* message, const FieldDescriptor* field) const {
1537 0 : MutableHasBits(message)[field->index() / 32] |= (1 << (field->index() % 32));
1538 0 : }
1539 :
1540 0 : inline void GeneratedMessageReflection::ClearBit(
1541 : Message* message, const FieldDescriptor* field) const {
1542 0 : MutableHasBits(message)[field->index() / 32] &= ~(1 << (field->index() % 32));
1543 0 : }
1544 :
1545 0 : inline void GeneratedMessageReflection::SwapBit(
1546 : Message* message1, Message* message2, const FieldDescriptor* field) const {
1547 0 : bool temp_has_bit = HasBit(*message1, field);
1548 0 : if (HasBit(*message2, field)) {
1549 0 : SetBit(message1, field);
1550 : } else {
1551 0 : ClearBit(message1, field);
1552 : }
1553 0 : if (temp_has_bit) {
1554 0 : SetBit(message2, field);
1555 : } else {
1556 0 : ClearBit(message2, field);
1557 : }
1558 0 : }
1559 :
1560 0 : inline bool GeneratedMessageReflection::HasOneof(
1561 : const Message& message, const OneofDescriptor* oneof_descriptor) const {
1562 0 : return (GetOneofCase(message, oneof_descriptor) > 0);
1563 : }
1564 :
1565 0 : inline bool GeneratedMessageReflection::HasOneofField(
1566 : const Message& message, const FieldDescriptor* field) const {
1567 0 : return (GetOneofCase(message, field->containing_oneof()) == field->number());
1568 : }
1569 :
1570 0 : inline void GeneratedMessageReflection::SetOneofCase(
1571 : Message* message, const FieldDescriptor* field) const {
1572 0 : *MutableOneofCase(message, field->containing_oneof()) = field->number();
1573 0 : }
1574 :
1575 0 : inline void GeneratedMessageReflection::ClearOneofField(
1576 : Message* message, const FieldDescriptor* field) const {
1577 0 : if (HasOneofField(*message, field)) {
1578 0 : ClearOneof(message, field->containing_oneof());
1579 : }
1580 0 : }
1581 :
1582 0 : inline void GeneratedMessageReflection::ClearOneof(
1583 : Message* message, const OneofDescriptor* oneof_descriptor) const {
1584 : // TODO(jieluo): Consider to cache the unused object instead of deleting
1585 : // it. It will be much faster if an aplication switches a lot from
1586 : // a few oneof fields. Time/space tradeoff
1587 0 : uint32 oneof_case = GetOneofCase(*message, oneof_descriptor);
1588 0 : if (oneof_case > 0) {
1589 0 : const FieldDescriptor* field = descriptor_->FindFieldByNumber(oneof_case);
1590 0 : switch (field->cpp_type()) {
1591 : case FieldDescriptor::CPPTYPE_STRING: {
1592 0 : switch (field->options().ctype()) {
1593 : default: // TODO(kenton): Support other string reps.
1594 : case FieldOptions::STRING:
1595 0 : delete *MutableRaw<string*>(message, field);
1596 0 : break;
1597 : }
1598 0 : break;
1599 : }
1600 :
1601 : case FieldDescriptor::CPPTYPE_MESSAGE:
1602 0 : delete *MutableRaw<Message*>(message, field);
1603 0 : break;
1604 : default:
1605 0 : break;
1606 : }
1607 :
1608 0 : *MutableOneofCase(message, oneof_descriptor) = 0;
1609 : }
1610 0 : }
1611 :
1612 : // Template implementations of basic accessors. Inline because each
1613 : // template instance is only called from one location. These are
1614 : // used for all types except messages.
1615 : template <typename Type>
1616 0 : inline const Type& GeneratedMessageReflection::GetField(
1617 : const Message& message, const FieldDescriptor* field) const {
1618 0 : return GetRaw<Type>(message, field);
1619 : }
1620 :
1621 : template <typename Type>
1622 0 : inline void GeneratedMessageReflection::SetField(
1623 : Message* message, const FieldDescriptor* field, const Type& value) const {
1624 0 : if (field->containing_oneof() && !HasOneofField(*message, field)) {
1625 0 : ClearOneof(message, field->containing_oneof());
1626 : }
1627 0 : *MutableRaw<Type>(message, field) = value;
1628 0 : field->containing_oneof() ?
1629 0 : SetOneofCase(message, field) : SetBit(message, field);
1630 0 : }
1631 :
1632 : template <typename Type>
1633 0 : inline Type* GeneratedMessageReflection::MutableField(
1634 : Message* message, const FieldDescriptor* field) const {
1635 0 : field->containing_oneof() ?
1636 0 : SetOneofCase(message, field) : SetBit(message, field);
1637 0 : return MutableRaw<Type>(message, field);
1638 : }
1639 :
1640 : template <typename Type>
1641 0 : inline const Type& GeneratedMessageReflection::GetRepeatedField(
1642 : const Message& message, const FieldDescriptor* field, int index) const {
1643 0 : return GetRaw<RepeatedField<Type> >(message, field).Get(index);
1644 : }
1645 :
1646 : template <typename Type>
1647 0 : inline const Type& GeneratedMessageReflection::GetRepeatedPtrField(
1648 : const Message& message, const FieldDescriptor* field, int index) const {
1649 0 : return GetRaw<RepeatedPtrField<Type> >(message, field).Get(index);
1650 : }
1651 :
1652 : template <typename Type>
1653 0 : inline void GeneratedMessageReflection::SetRepeatedField(
1654 : Message* message, const FieldDescriptor* field,
1655 : int index, Type value) const {
1656 0 : MutableRaw<RepeatedField<Type> >(message, field)->Set(index, value);
1657 0 : }
1658 :
1659 : template <typename Type>
1660 0 : inline Type* GeneratedMessageReflection::MutableRepeatedField(
1661 : Message* message, const FieldDescriptor* field, int index) const {
1662 : RepeatedPtrField<Type>* repeated =
1663 0 : MutableRaw<RepeatedPtrField<Type> >(message, field);
1664 0 : return repeated->Mutable(index);
1665 : }
1666 :
1667 : template <typename Type>
1668 0 : inline void GeneratedMessageReflection::AddField(
1669 : Message* message, const FieldDescriptor* field, const Type& value) const {
1670 0 : MutableRaw<RepeatedField<Type> >(message, field)->Add(value);
1671 0 : }
1672 :
1673 : template <typename Type>
1674 0 : inline Type* GeneratedMessageReflection::AddField(
1675 : Message* message, const FieldDescriptor* field) const {
1676 : RepeatedPtrField<Type>* repeated =
1677 0 : MutableRaw<RepeatedPtrField<Type> >(message, field);
1678 0 : return repeated->Add();
1679 : }
1680 :
1681 : } // namespace internal
1682 : } // namespace protobuf
1683 : } // namespace google
|