1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
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
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.
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.
31 #import "GPBMessage_PackagePrivate.h"
33 #import <objc/runtime.h>
34 #import <objc/message.h>
37 #import "GPBArray_PackagePrivate.h"
38 #import "GPBCodedInputStream_PackagePrivate.h"
39 #import "GPBCodedOutputStream_PackagePrivate.h"
40 #import "GPBDescriptor_PackagePrivate.h"
41 #import "GPBDictionary_PackagePrivate.h"
42 #import "GPBExtensionInternals.h"
43 #import "GPBExtensionRegistry.h"
44 #import "GPBRootObject_PackagePrivate.h"
45 #import "GPBUnknownFieldSet_PackagePrivate.h"
46 #import "GPBUtilities_PackagePrivate.h"
48 // Direct access is use for speed, to avoid even internally declaring things
49 // read/write, etc. The warning is enabled in the project to ensure code calling
50 // protos can turn on -Wdirect-ivar-access without issues.
51 #pragma clang diagnostic push
52 #pragma clang diagnostic ignored "-Wdirect-ivar-access"
54 NSString *const GPBMessageErrorDomain =
55 GPBNSStringifySymbol(GPBMessageErrorDomain);
57 NSString *const GPBErrorReasonKey = @"Reason";
59 static NSString *const kGPBDataCoderKey = @"GPBData";
64 // This is the base class for *all* messages generated, so any selector defined,
65 // *public* or *private* could end up colliding with a proto message field. So
66 // avoid using selectors that could match a property, use C functions to hide
70 @interface GPBMessage () {
72 GPBUnknownFieldSet *unknownFields_;
73 NSMutableDictionary *extensionMap_;
74 NSMutableDictionary *autocreatedExtensionMap_;
76 // If the object was autocreated, we remember the creator so that if we get
77 // mutated, we can inform the creator to make our field visible.
78 GPBMessage *autocreator_;
79 GPBFieldDescriptor *autocreatorField_;
80 GPBExtensionDescriptor *autocreatorExtension_;
82 // A lock to provide mutual exclusion from internal data that can be modified
83 // by *read* operations such as getters (autocreation of message fields and
84 // message extensions, not setting of values). Used to guarantee thread safety
85 // for concurrent reads on the message.
86 // NOTE: OSSpinLock may seem like a good fit here but Apple engineers have
87 // pointed out that they are vulnerable to live locking on iOS in cases of
88 // priority inversion:
89 // http://mjtsai.com/blog/2015/12/16/osspinlock-is-unsafe/
90 // https://lists.swift.org/pipermail/swift-dev/Week-of-Mon-20151214/000372.html
91 // Use of readOnlySemaphore_ must be prefaced by a call to
92 // GPBPrepareReadOnlySemaphore to ensure it has been created. This allows
93 // readOnlySemaphore_ to be only created when actually needed.
94 _Atomic(dispatch_semaphore_t) readOnlySemaphore_;
98 static id CreateArrayForField(GPBFieldDescriptor *field,
99 GPBMessage *autocreator)
100 __attribute__((ns_returns_retained));
101 static id GetOrCreateArrayIvarWithField(GPBMessage *self,
102 GPBFieldDescriptor *field,
103 GPBFileSyntax syntax);
104 static id GetArrayIvarWithField(GPBMessage *self, GPBFieldDescriptor *field);
105 static id CreateMapForField(GPBFieldDescriptor *field,
106 GPBMessage *autocreator)
107 __attribute__((ns_returns_retained));
108 static id GetOrCreateMapIvarWithField(GPBMessage *self,
109 GPBFieldDescriptor *field,
110 GPBFileSyntax syntax);
111 static id GetMapIvarWithField(GPBMessage *self, GPBFieldDescriptor *field);
112 static NSMutableDictionary *CloneExtensionMap(NSDictionary *extensionMap,
114 __attribute__((ns_returns_retained));
117 static NSError *MessageError(NSInteger code, NSDictionary *userInfo) {
118 return [NSError errorWithDomain:GPBMessageErrorDomain
124 static NSError *ErrorFromException(NSException *exception) {
125 NSError *error = nil;
127 if ([exception.name isEqual:GPBCodedInputStreamException]) {
128 NSDictionary *exceptionInfo = exception.userInfo;
129 error = exceptionInfo[GPBCodedInputStreamUnderlyingErrorKey];
133 NSString *reason = exception.reason;
134 NSDictionary *userInfo = nil;
135 if ([reason length]) {
136 userInfo = @{ GPBErrorReasonKey : reason };
139 error = [NSError errorWithDomain:GPBMessageErrorDomain
140 code:GPBMessageErrorCodeOther
146 static void CheckExtension(GPBMessage *self,
147 GPBExtensionDescriptor *extension) {
148 if (![self isKindOfClass:extension.containingMessageClass]) {
150 raise:NSInvalidArgumentException
151 format:@"Extension %@ used on wrong class (%@ instead of %@)",
152 extension.singletonName,
153 [self class], extension.containingMessageClass];
157 static NSMutableDictionary *CloneExtensionMap(NSDictionary *extensionMap,
159 if (extensionMap.count == 0) {
162 NSMutableDictionary *result = [[NSMutableDictionary allocWithZone:zone]
163 initWithCapacity:extensionMap.count];
165 for (GPBExtensionDescriptor *extension in extensionMap) {
166 id value = [extensionMap objectForKey:extension];
167 BOOL isMessageExtension = GPBExtensionIsMessage(extension);
169 if (extension.repeated) {
170 if (isMessageExtension) {
171 NSMutableArray *list =
172 [[NSMutableArray alloc] initWithCapacity:[value count]];
173 for (GPBMessage *listValue in value) {
174 GPBMessage *copiedValue = [listValue copyWithZone:zone];
175 [list addObject:copiedValue];
176 [copiedValue release];
178 [result setObject:list forKey:extension];
181 NSMutableArray *copiedValue = [value mutableCopyWithZone:zone];
182 [result setObject:copiedValue forKey:extension];
183 [copiedValue release];
186 if (isMessageExtension) {
187 GPBMessage *copiedValue = [value copyWithZone:zone];
188 [result setObject:copiedValue forKey:extension];
189 [copiedValue release];
191 [result setObject:value forKey:extension];
199 static id CreateArrayForField(GPBFieldDescriptor *field,
200 GPBMessage *autocreator) {
202 GPBDataType fieldDataType = GPBGetFieldDataType(field);
203 switch (fieldDataType) {
204 case GPBDataTypeBool:
205 result = [[GPBBoolArray alloc] init];
207 case GPBDataTypeFixed32:
208 case GPBDataTypeUInt32:
209 result = [[GPBUInt32Array alloc] init];
211 case GPBDataTypeInt32:
212 case GPBDataTypeSFixed32:
213 case GPBDataTypeSInt32:
214 result = [[GPBInt32Array alloc] init];
216 case GPBDataTypeFixed64:
217 case GPBDataTypeUInt64:
218 result = [[GPBUInt64Array alloc] init];
220 case GPBDataTypeInt64:
221 case GPBDataTypeSFixed64:
222 case GPBDataTypeSInt64:
223 result = [[GPBInt64Array alloc] init];
225 case GPBDataTypeFloat:
226 result = [[GPBFloatArray alloc] init];
228 case GPBDataTypeDouble:
229 result = [[GPBDoubleArray alloc] init];
232 case GPBDataTypeEnum:
233 result = [[GPBEnumArray alloc]
234 initWithValidationFunction:field.enumDescriptor.enumVerifier];
237 case GPBDataTypeBytes:
238 case GPBDataTypeGroup:
239 case GPBDataTypeMessage:
240 case GPBDataTypeString:
242 result = [[GPBAutocreatedArray alloc] init];
244 result = [[NSMutableArray alloc] init];
250 if (GPBDataTypeIsObject(fieldDataType)) {
251 GPBAutocreatedArray *autoArray = result;
252 autoArray->_autocreator = autocreator;
254 GPBInt32Array *gpbArray = result;
255 gpbArray->_autocreator = autocreator;
262 static id CreateMapForField(GPBFieldDescriptor *field,
263 GPBMessage *autocreator) {
265 GPBDataType keyDataType = field.mapKeyDataType;
266 GPBDataType valueDataType = GPBGetFieldDataType(field);
267 switch (keyDataType) {
268 case GPBDataTypeBool:
269 switch (valueDataType) {
270 case GPBDataTypeBool:
271 result = [[GPBBoolBoolDictionary alloc] init];
273 case GPBDataTypeFixed32:
274 case GPBDataTypeUInt32:
275 result = [[GPBBoolUInt32Dictionary alloc] init];
277 case GPBDataTypeInt32:
278 case GPBDataTypeSFixed32:
279 case GPBDataTypeSInt32:
280 result = [[GPBBoolInt32Dictionary alloc] init];
282 case GPBDataTypeFixed64:
283 case GPBDataTypeUInt64:
284 result = [[GPBBoolUInt64Dictionary alloc] init];
286 case GPBDataTypeInt64:
287 case GPBDataTypeSFixed64:
288 case GPBDataTypeSInt64:
289 result = [[GPBBoolInt64Dictionary alloc] init];
291 case GPBDataTypeFloat:
292 result = [[GPBBoolFloatDictionary alloc] init];
294 case GPBDataTypeDouble:
295 result = [[GPBBoolDoubleDictionary alloc] init];
297 case GPBDataTypeEnum:
298 result = [[GPBBoolEnumDictionary alloc]
299 initWithValidationFunction:field.enumDescriptor.enumVerifier];
301 case GPBDataTypeBytes:
302 case GPBDataTypeMessage:
303 case GPBDataTypeString:
304 result = [[GPBBoolObjectDictionary alloc] init];
306 case GPBDataTypeGroup:
307 NSCAssert(NO, @"shouldn't happen");
311 case GPBDataTypeFixed32:
312 case GPBDataTypeUInt32:
313 switch (valueDataType) {
314 case GPBDataTypeBool:
315 result = [[GPBUInt32BoolDictionary alloc] init];
317 case GPBDataTypeFixed32:
318 case GPBDataTypeUInt32:
319 result = [[GPBUInt32UInt32Dictionary alloc] init];
321 case GPBDataTypeInt32:
322 case GPBDataTypeSFixed32:
323 case GPBDataTypeSInt32:
324 result = [[GPBUInt32Int32Dictionary alloc] init];
326 case GPBDataTypeFixed64:
327 case GPBDataTypeUInt64:
328 result = [[GPBUInt32UInt64Dictionary alloc] init];
330 case GPBDataTypeInt64:
331 case GPBDataTypeSFixed64:
332 case GPBDataTypeSInt64:
333 result = [[GPBUInt32Int64Dictionary alloc] init];
335 case GPBDataTypeFloat:
336 result = [[GPBUInt32FloatDictionary alloc] init];
338 case GPBDataTypeDouble:
339 result = [[GPBUInt32DoubleDictionary alloc] init];
341 case GPBDataTypeEnum:
342 result = [[GPBUInt32EnumDictionary alloc]
343 initWithValidationFunction:field.enumDescriptor.enumVerifier];
345 case GPBDataTypeBytes:
346 case GPBDataTypeMessage:
347 case GPBDataTypeString:
348 result = [[GPBUInt32ObjectDictionary alloc] init];
350 case GPBDataTypeGroup:
351 NSCAssert(NO, @"shouldn't happen");
355 case GPBDataTypeInt32:
356 case GPBDataTypeSFixed32:
357 case GPBDataTypeSInt32:
358 switch (valueDataType) {
359 case GPBDataTypeBool:
360 result = [[GPBInt32BoolDictionary alloc] init];
362 case GPBDataTypeFixed32:
363 case GPBDataTypeUInt32:
364 result = [[GPBInt32UInt32Dictionary alloc] init];
366 case GPBDataTypeInt32:
367 case GPBDataTypeSFixed32:
368 case GPBDataTypeSInt32:
369 result = [[GPBInt32Int32Dictionary alloc] init];
371 case GPBDataTypeFixed64:
372 case GPBDataTypeUInt64:
373 result = [[GPBInt32UInt64Dictionary alloc] init];
375 case GPBDataTypeInt64:
376 case GPBDataTypeSFixed64:
377 case GPBDataTypeSInt64:
378 result = [[GPBInt32Int64Dictionary alloc] init];
380 case GPBDataTypeFloat:
381 result = [[GPBInt32FloatDictionary alloc] init];
383 case GPBDataTypeDouble:
384 result = [[GPBInt32DoubleDictionary alloc] init];
386 case GPBDataTypeEnum:
387 result = [[GPBInt32EnumDictionary alloc]
388 initWithValidationFunction:field.enumDescriptor.enumVerifier];
390 case GPBDataTypeBytes:
391 case GPBDataTypeMessage:
392 case GPBDataTypeString:
393 result = [[GPBInt32ObjectDictionary alloc] init];
395 case GPBDataTypeGroup:
396 NSCAssert(NO, @"shouldn't happen");
400 case GPBDataTypeFixed64:
401 case GPBDataTypeUInt64:
402 switch (valueDataType) {
403 case GPBDataTypeBool:
404 result = [[GPBUInt64BoolDictionary alloc] init];
406 case GPBDataTypeFixed32:
407 case GPBDataTypeUInt32:
408 result = [[GPBUInt64UInt32Dictionary alloc] init];
410 case GPBDataTypeInt32:
411 case GPBDataTypeSFixed32:
412 case GPBDataTypeSInt32:
413 result = [[GPBUInt64Int32Dictionary alloc] init];
415 case GPBDataTypeFixed64:
416 case GPBDataTypeUInt64:
417 result = [[GPBUInt64UInt64Dictionary alloc] init];
419 case GPBDataTypeInt64:
420 case GPBDataTypeSFixed64:
421 case GPBDataTypeSInt64:
422 result = [[GPBUInt64Int64Dictionary alloc] init];
424 case GPBDataTypeFloat:
425 result = [[GPBUInt64FloatDictionary alloc] init];
427 case GPBDataTypeDouble:
428 result = [[GPBUInt64DoubleDictionary alloc] init];
430 case GPBDataTypeEnum:
431 result = [[GPBUInt64EnumDictionary alloc]
432 initWithValidationFunction:field.enumDescriptor.enumVerifier];
434 case GPBDataTypeBytes:
435 case GPBDataTypeMessage:
436 case GPBDataTypeString:
437 result = [[GPBUInt64ObjectDictionary alloc] init];
439 case GPBDataTypeGroup:
440 NSCAssert(NO, @"shouldn't happen");
444 case GPBDataTypeInt64:
445 case GPBDataTypeSFixed64:
446 case GPBDataTypeSInt64:
447 switch (valueDataType) {
448 case GPBDataTypeBool:
449 result = [[GPBInt64BoolDictionary alloc] init];
451 case GPBDataTypeFixed32:
452 case GPBDataTypeUInt32:
453 result = [[GPBInt64UInt32Dictionary alloc] init];
455 case GPBDataTypeInt32:
456 case GPBDataTypeSFixed32:
457 case GPBDataTypeSInt32:
458 result = [[GPBInt64Int32Dictionary alloc] init];
460 case GPBDataTypeFixed64:
461 case GPBDataTypeUInt64:
462 result = [[GPBInt64UInt64Dictionary alloc] init];
464 case GPBDataTypeInt64:
465 case GPBDataTypeSFixed64:
466 case GPBDataTypeSInt64:
467 result = [[GPBInt64Int64Dictionary alloc] init];
469 case GPBDataTypeFloat:
470 result = [[GPBInt64FloatDictionary alloc] init];
472 case GPBDataTypeDouble:
473 result = [[GPBInt64DoubleDictionary alloc] init];
475 case GPBDataTypeEnum:
476 result = [[GPBInt64EnumDictionary alloc]
477 initWithValidationFunction:field.enumDescriptor.enumVerifier];
479 case GPBDataTypeBytes:
480 case GPBDataTypeMessage:
481 case GPBDataTypeString:
482 result = [[GPBInt64ObjectDictionary alloc] init];
484 case GPBDataTypeGroup:
485 NSCAssert(NO, @"shouldn't happen");
489 case GPBDataTypeString:
490 switch (valueDataType) {
491 case GPBDataTypeBool:
492 result = [[GPBStringBoolDictionary alloc] init];
494 case GPBDataTypeFixed32:
495 case GPBDataTypeUInt32:
496 result = [[GPBStringUInt32Dictionary alloc] init];
498 case GPBDataTypeInt32:
499 case GPBDataTypeSFixed32:
500 case GPBDataTypeSInt32:
501 result = [[GPBStringInt32Dictionary alloc] init];
503 case GPBDataTypeFixed64:
504 case GPBDataTypeUInt64:
505 result = [[GPBStringUInt64Dictionary alloc] init];
507 case GPBDataTypeInt64:
508 case GPBDataTypeSFixed64:
509 case GPBDataTypeSInt64:
510 result = [[GPBStringInt64Dictionary alloc] init];
512 case GPBDataTypeFloat:
513 result = [[GPBStringFloatDictionary alloc] init];
515 case GPBDataTypeDouble:
516 result = [[GPBStringDoubleDictionary alloc] init];
518 case GPBDataTypeEnum:
519 result = [[GPBStringEnumDictionary alloc]
520 initWithValidationFunction:field.enumDescriptor.enumVerifier];
522 case GPBDataTypeBytes:
523 case GPBDataTypeMessage:
524 case GPBDataTypeString:
526 result = [[GPBAutocreatedDictionary alloc] init];
528 result = [[NSMutableDictionary alloc] init];
531 case GPBDataTypeGroup:
532 NSCAssert(NO, @"shouldn't happen");
537 case GPBDataTypeFloat:
538 case GPBDataTypeDouble:
539 case GPBDataTypeEnum:
540 case GPBDataTypeBytes:
541 case GPBDataTypeGroup:
542 case GPBDataTypeMessage:
543 NSCAssert(NO, @"shouldn't happen");
548 if ((keyDataType == GPBDataTypeString) &&
549 GPBDataTypeIsObject(valueDataType)) {
550 GPBAutocreatedDictionary *autoDict = result;
551 autoDict->_autocreator = autocreator;
553 GPBInt32Int32Dictionary *gpbDict = result;
554 gpbDict->_autocreator = autocreator;
561 #if !defined(__clang_analyzer__)
562 // These functions are blocked from the analyzer because the analyzer sees the
563 // GPBSetRetainedObjectIvarWithFieldInternal() call as consuming the array/map,
564 // so use of the array/map after the call returns is flagged as a use after
566 // But GPBSetRetainedObjectIvarWithFieldInternal() is "consuming" the retain
567 // count be holding onto the object (it is transfering it), the object is
568 // still valid after returning from the call. The other way to avoid this
569 // would be to add a -retain/-autorelease, but that would force every
570 // repeated/map field parsed into the autorelease pool which is both a memory
571 // and performance hit.
573 static id GetOrCreateArrayIvarWithField(GPBMessage *self,
574 GPBFieldDescriptor *field,
575 GPBFileSyntax syntax) {
576 id array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
578 // No lock needed, this is called from places expecting to mutate
579 // so no threading protection is needed.
580 array = CreateArrayForField(field, nil);
581 GPBSetRetainedObjectIvarWithFieldInternal(self, field, array, syntax);
586 // This is like GPBGetObjectIvarWithField(), but for arrays, it should
587 // only be used to wire the method into the class.
588 static id GetArrayIvarWithField(GPBMessage *self, GPBFieldDescriptor *field) {
589 id array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
591 // Check again after getting the lock.
592 GPBPrepareReadOnlySemaphore(self);
593 dispatch_semaphore_wait(self->readOnlySemaphore_, DISPATCH_TIME_FOREVER);
594 array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
596 array = CreateArrayForField(field, self);
597 GPBSetAutocreatedRetainedObjectIvarWithField(self, field, array);
599 dispatch_semaphore_signal(self->readOnlySemaphore_);
604 static id GetOrCreateMapIvarWithField(GPBMessage *self,
605 GPBFieldDescriptor *field,
606 GPBFileSyntax syntax) {
607 id dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
609 // No lock needed, this is called from places expecting to mutate
610 // so no threading protection is needed.
611 dict = CreateMapForField(field, nil);
612 GPBSetRetainedObjectIvarWithFieldInternal(self, field, dict, syntax);
617 // This is like GPBGetObjectIvarWithField(), but for maps, it should
618 // only be used to wire the method into the class.
619 static id GetMapIvarWithField(GPBMessage *self, GPBFieldDescriptor *field) {
620 id dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
622 // Check again after getting the lock.
623 GPBPrepareReadOnlySemaphore(self);
624 dispatch_semaphore_wait(self->readOnlySemaphore_, DISPATCH_TIME_FOREVER);
625 dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
627 dict = CreateMapForField(field, self);
628 GPBSetAutocreatedRetainedObjectIvarWithField(self, field, dict);
630 dispatch_semaphore_signal(self->readOnlySemaphore_);
635 #endif // !defined(__clang_analyzer__)
637 GPBMessage *GPBCreateMessageWithAutocreator(Class msgClass,
638 GPBMessage *autocreator,
639 GPBFieldDescriptor *field) {
640 GPBMessage *message = [[msgClass alloc] init];
641 message->autocreator_ = autocreator;
642 message->autocreatorField_ = [field retain];
646 static GPBMessage *CreateMessageWithAutocreatorForExtension(
647 Class msgClass, GPBMessage *autocreator, GPBExtensionDescriptor *extension)
648 __attribute__((ns_returns_retained));
650 static GPBMessage *CreateMessageWithAutocreatorForExtension(
651 Class msgClass, GPBMessage *autocreator,
652 GPBExtensionDescriptor *extension) {
653 GPBMessage *message = [[msgClass alloc] init];
654 message->autocreator_ = autocreator;
655 message->autocreatorExtension_ = [extension retain];
659 BOOL GPBWasMessageAutocreatedBy(GPBMessage *message, GPBMessage *parent) {
660 return (message->autocreator_ == parent);
663 void GPBBecomeVisibleToAutocreator(GPBMessage *self) {
664 // Message objects that are implicitly created by accessing a message field
665 // are initially not visible via the hasX selector. This method makes them
667 if (self->autocreator_) {
668 // This will recursively make all parent messages visible until it reaches a
669 // super-creator that's visible.
670 if (self->autocreatorField_) {
671 GPBFileSyntax syntax = [self->autocreator_ descriptor].file.syntax;
672 GPBSetObjectIvarWithFieldInternal(self->autocreator_,
673 self->autocreatorField_, self, syntax);
675 [self->autocreator_ setExtension:self->autocreatorExtension_ value:self];
680 void GPBAutocreatedArrayModified(GPBMessage *self, id array) {
681 // When one of our autocreated arrays adds elements, make it visible.
682 GPBDescriptor *descriptor = [[self class] descriptor];
683 for (GPBFieldDescriptor *field in descriptor->fields_) {
684 if (field.fieldType == GPBFieldTypeRepeated) {
685 id curArray = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
686 if (curArray == array) {
687 if (GPBFieldDataTypeIsObject(field)) {
688 GPBAutocreatedArray *autoArray = array;
689 autoArray->_autocreator = nil;
691 GPBInt32Array *gpbArray = array;
692 gpbArray->_autocreator = nil;
694 GPBBecomeVisibleToAutocreator(self);
699 NSCAssert(NO, @"Unknown autocreated %@ for %@.", [array class], self);
702 void GPBAutocreatedDictionaryModified(GPBMessage *self, id dictionary) {
703 // When one of our autocreated dicts adds elements, make it visible.
704 GPBDescriptor *descriptor = [[self class] descriptor];
705 for (GPBFieldDescriptor *field in descriptor->fields_) {
706 if (field.fieldType == GPBFieldTypeMap) {
707 id curDict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
708 if (curDict == dictionary) {
709 if ((field.mapKeyDataType == GPBDataTypeString) &&
710 GPBFieldDataTypeIsObject(field)) {
711 GPBAutocreatedDictionary *autoDict = dictionary;
712 autoDict->_autocreator = nil;
714 GPBInt32Int32Dictionary *gpbDict = dictionary;
715 gpbDict->_autocreator = nil;
717 GPBBecomeVisibleToAutocreator(self);
722 NSCAssert(NO, @"Unknown autocreated %@ for %@.", [dictionary class], self);
725 void GPBClearMessageAutocreator(GPBMessage *self) {
726 if ((self == nil) || !self->autocreator_) {
730 #if defined(DEBUG) && DEBUG && !defined(NS_BLOCK_ASSERTIONS)
731 // Either the autocreator must have its "has" flag set to YES, or it must be
732 // NO and not equal to ourselves.
733 BOOL autocreatorHas =
734 (self->autocreatorField_
735 ? GPBGetHasIvarField(self->autocreator_, self->autocreatorField_)
736 : [self->autocreator_ hasExtension:self->autocreatorExtension_]);
737 GPBMessage *autocreatorFieldValue =
738 (self->autocreatorField_
739 ? GPBGetObjectIvarWithFieldNoAutocreate(self->autocreator_,
740 self->autocreatorField_)
741 : [self->autocreator_->autocreatedExtensionMap_
742 objectForKey:self->autocreatorExtension_]);
743 NSCAssert(autocreatorHas || autocreatorFieldValue != self,
744 @"Cannot clear autocreator because it still refers to self, self: %@.",
747 #endif // DEBUG && !defined(NS_BLOCK_ASSERTIONS)
749 self->autocreator_ = nil;
750 [self->autocreatorField_ release];
751 self->autocreatorField_ = nil;
752 [self->autocreatorExtension_ release];
753 self->autocreatorExtension_ = nil;
756 // Call this before using the readOnlySemaphore_. This ensures it is created only once.
757 void GPBPrepareReadOnlySemaphore(GPBMessage *self) {
758 #pragma clang diagnostic push
759 #pragma clang diagnostic ignored "-Wdirect-ivar-access"
761 // Create the semaphore on demand (rather than init) as developers might not cause them
762 // to be needed, and the heap usage can add up. The atomic swap is used to avoid needing
763 // another lock around creating it.
764 if (self->readOnlySemaphore_ == nil) {
765 dispatch_semaphore_t worker = dispatch_semaphore_create(1);
766 dispatch_semaphore_t expected = nil;
767 if (!atomic_compare_exchange_strong(&self->readOnlySemaphore_, &expected, worker)) {
768 dispatch_release(worker);
770 #if defined(__clang_analyzer__)
771 // The Xcode 9.2 (and 9.3 beta) static analyzer thinks worker is leaked
772 // (doesn't seem to know about atomic_compare_exchange_strong); so just
773 // for the analyzer, let it think worker is also released in this case.
774 else { dispatch_release(worker); }
778 #pragma clang diagnostic pop
781 static GPBUnknownFieldSet *GetOrMakeUnknownFields(GPBMessage *self) {
782 if (!self->unknownFields_) {
783 self->unknownFields_ = [[GPBUnknownFieldSet alloc] init];
784 GPBBecomeVisibleToAutocreator(self);
786 return self->unknownFields_;
789 @implementation GPBMessage
792 Class pbMessageClass = [GPBMessage class];
793 if ([self class] == pbMessageClass) {
794 // This is here to start up the "base" class descriptor.
796 // Message shares extension method resolving with GPBRootObject so insure
797 // it is started up at the same time.
798 (void)[GPBRootObject class];
799 } else if ([self superclass] == pbMessageClass) {
800 // This is here to start up all the "message" subclasses. Just needs to be
801 // done for the messages, not any of the subclasses.
802 // This must be done in initialize to enforce thread safety of start up of
803 // the protocol buffer library.
804 // Note: The generated code for -descriptor calls
805 // +[GPBDescriptor allocDescriptorForClass:...], passing the GPBRootObject
806 // subclass for the file. That call chain is what ensures that *Root class
807 // is started up to support extension resolution off the message class
808 // (+resolveClassMethod: below) in a thread safe manner.
813 + (instancetype)allocWithZone:(NSZone *)zone {
814 // Override alloc to allocate our classes with the additional storage
815 // required for the instance variables.
816 GPBDescriptor *descriptor = [self descriptor];
817 return NSAllocateObject(self, descriptor->storageSize_, zone);
820 + (instancetype)alloc {
821 return [self allocWithZone:nil];
824 + (GPBDescriptor *)descriptor {
825 // This is thread safe because it is called from +initialize.
826 static GPBDescriptor *descriptor = NULL;
827 static GPBFileDescriptor *fileDescriptor = NULL;
829 // Use a dummy file that marks it as proto2 syntax so when used generically
830 // it supports unknowns/etc.
832 [[GPBFileDescriptor alloc] initWithPackage:@"internal"
833 syntax:GPBFileSyntaxProto2];
835 descriptor = [GPBDescriptor allocDescriptorForClass:[GPBMessage class]
846 + (instancetype)message {
847 return [[[self alloc] init] autorelease];
850 - (instancetype)init {
851 if ((self = [super init])) {
852 messageStorage_ = (GPBMessage_StoragePtr)(
853 ((uint8_t *)self) + class_getInstanceSize([self class]));
859 - (instancetype)initWithData:(NSData *)data error:(NSError **)errorPtr {
860 return [self initWithData:data extensionRegistry:nil error:errorPtr];
863 - (instancetype)initWithData:(NSData *)data
864 extensionRegistry:(GPBExtensionRegistry *)extensionRegistry
865 error:(NSError **)errorPtr {
866 if ((self = [self init])) {
868 [self mergeFromData:data extensionRegistry:extensionRegistry];
873 @catch (NSException *exception) {
877 *errorPtr = ErrorFromException(exception);
881 if (self && !self.initialized) {
885 *errorPtr = MessageError(GPBMessageErrorCodeMissingRequiredField, nil);
893 - (instancetype)initWithCodedInputStream:(GPBCodedInputStream *)input
895 (GPBExtensionRegistry *)extensionRegistry
896 error:(NSError **)errorPtr {
897 if ((self = [self init])) {
899 [self mergeFromCodedInputStream:input extensionRegistry:extensionRegistry];
904 @catch (NSException *exception) {
908 *errorPtr = ErrorFromException(exception);
912 if (self && !self.initialized) {
916 *errorPtr = MessageError(GPBMessageErrorCodeMissingRequiredField, nil);
925 [self internalClear:NO];
926 NSCAssert(!autocreator_, @"Autocreator was not cleared before dealloc.");
927 if (readOnlySemaphore_) {
928 dispatch_release(readOnlySemaphore_);
933 - (void)copyFieldsInto:(GPBMessage *)message
935 descriptor:(GPBDescriptor *)descriptor {
936 // Copy all the storage...
937 memcpy(message->messageStorage_, messageStorage_, descriptor->storageSize_);
939 GPBFileSyntax syntax = descriptor.file.syntax;
941 // Loop over the fields doing fixup...
942 for (GPBFieldDescriptor *field in descriptor->fields_) {
943 if (GPBFieldIsMapOrArray(field)) {
944 id value = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
946 // We need to copy the array/map, but the catch is for message fields,
947 // we also need to ensure all the messages as those need copying also.
949 if (GPBFieldDataTypeIsMessage(field)) {
950 if (field.fieldType == GPBFieldTypeRepeated) {
951 NSArray *existingArray = (NSArray *)value;
952 NSMutableArray *newArray =
953 [[NSMutableArray alloc] initWithCapacity:existingArray.count];
955 for (GPBMessage *msg in existingArray) {
956 GPBMessage *copiedMsg = [msg copyWithZone:zone];
957 [newArray addObject:copiedMsg];
961 if (field.mapKeyDataType == GPBDataTypeString) {
962 // Map is an NSDictionary.
963 NSDictionary *existingDict = value;
964 NSMutableDictionary *newDict = [[NSMutableDictionary alloc]
965 initWithCapacity:existingDict.count];
967 [existingDict enumerateKeysAndObjectsUsingBlock:^(NSString *key,
971 GPBMessage *copiedMsg = [msg copyWithZone:zone];
972 [newDict setObject:copiedMsg forKey:key];
976 // Is one of the GPB*ObjectDictionary classes. Type doesn't
977 // matter, just need one to invoke the selector.
978 GPBInt32ObjectDictionary *existingDict = value;
979 newValue = [existingDict deepCopyWithZone:zone];
983 // Not messages (but is a map/array)...
984 if (field.fieldType == GPBFieldTypeRepeated) {
985 if (GPBFieldDataTypeIsObject(field)) {
987 newValue = [value mutableCopyWithZone:zone];
990 newValue = [value copyWithZone:zone];
993 if ((field.mapKeyDataType == GPBDataTypeString) &&
994 GPBFieldDataTypeIsObject(field)) {
996 newValue = [value mutableCopyWithZone:zone];
998 // Is one of the GPB*Dictionary classes. Type doesn't matter,
999 // just need one to invoke the selector.
1000 GPBInt32Int32Dictionary *existingDict = value;
1001 newValue = [existingDict copyWithZone:zone];
1005 // We retain here because the memcpy picked up the pointer value and
1006 // the next call to SetRetainedObject... will release the current value.
1008 GPBSetRetainedObjectIvarWithFieldInternal(message, field, newValue,
1011 } else if (GPBFieldDataTypeIsMessage(field)) {
1012 // For object types, if we have a value, copy it. If we don't,
1013 // zero it to remove the pointer to something that was autocreated
1014 // (and the ptr just got memcpyed).
1015 if (GPBGetHasIvarField(self, field)) {
1016 GPBMessage *value = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1017 GPBMessage *newValue = [value copyWithZone:zone];
1018 // We retain here because the memcpy picked up the pointer value and
1019 // the next call to SetRetainedObject... will release the current value.
1021 GPBSetRetainedObjectIvarWithFieldInternal(message, field, newValue,
1024 uint8_t *storage = (uint8_t *)message->messageStorage_;
1025 id *typePtr = (id *)&storage[field->description_->offset];
1028 } else if (GPBFieldDataTypeIsObject(field) &&
1029 GPBGetHasIvarField(self, field)) {
1030 // A set string/data value (message picked off above), copy it.
1031 id value = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1032 id newValue = [value copyWithZone:zone];
1033 // We retain here because the memcpy picked up the pointer value and
1034 // the next call to SetRetainedObject... will release the current value.
1036 GPBSetRetainedObjectIvarWithFieldInternal(message, field, newValue,
1039 // memcpy took care of the rest of the primitive fields if they were set.
1041 } // for (field in descriptor->fields_)
1044 - (id)copyWithZone:(NSZone *)zone {
1045 GPBDescriptor *descriptor = [self descriptor];
1046 GPBMessage *result = [[descriptor.messageClass allocWithZone:zone] init];
1048 [self copyFieldsInto:result zone:zone descriptor:descriptor];
1049 // Make immutable copies of the extra bits.
1050 result->unknownFields_ = [unknownFields_ copyWithZone:zone];
1051 result->extensionMap_ = CloneExtensionMap(extensionMap_, zone);
1056 [self internalClear:YES];
1059 - (void)internalClear:(BOOL)zeroStorage {
1060 GPBDescriptor *descriptor = [self descriptor];
1061 for (GPBFieldDescriptor *field in descriptor->fields_) {
1062 if (GPBFieldIsMapOrArray(field)) {
1063 id arrayOrMap = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1065 if (field.fieldType == GPBFieldTypeRepeated) {
1066 if (GPBFieldDataTypeIsObject(field)) {
1067 if ([arrayOrMap isKindOfClass:[GPBAutocreatedArray class]]) {
1068 GPBAutocreatedArray *autoArray = arrayOrMap;
1069 if (autoArray->_autocreator == self) {
1070 autoArray->_autocreator = nil;
1074 // Type doesn't matter, it is a GPB*Array.
1075 GPBInt32Array *gpbArray = arrayOrMap;
1076 if (gpbArray->_autocreator == self) {
1077 gpbArray->_autocreator = nil;
1081 if ((field.mapKeyDataType == GPBDataTypeString) &&
1082 GPBFieldDataTypeIsObject(field)) {
1083 if ([arrayOrMap isKindOfClass:[GPBAutocreatedDictionary class]]) {
1084 GPBAutocreatedDictionary *autoDict = arrayOrMap;
1085 if (autoDict->_autocreator == self) {
1086 autoDict->_autocreator = nil;
1090 // Type doesn't matter, it is a GPB*Dictionary.
1091 GPBInt32Int32Dictionary *gpbDict = arrayOrMap;
1092 if (gpbDict->_autocreator == self) {
1093 gpbDict->_autocreator = nil;
1097 [arrayOrMap release];
1099 } else if (GPBFieldDataTypeIsMessage(field)) {
1100 GPBClearAutocreatedMessageIvarWithField(self, field);
1101 GPBMessage *value = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1103 } else if (GPBFieldDataTypeIsObject(field) &&
1104 GPBGetHasIvarField(self, field)) {
1105 id value = GPBGetObjectIvarWithField(self, field);
1110 // GPBClearMessageAutocreator() expects that its caller has already been
1111 // removed from autocreatedExtensionMap_ so we set to nil first.
1112 NSArray *autocreatedValues = [autocreatedExtensionMap_ allValues];
1113 [autocreatedExtensionMap_ release];
1114 autocreatedExtensionMap_ = nil;
1116 // Since we're clearing all of our extensions, make sure that we clear the
1117 // autocreator on any that we've created so they no longer refer to us.
1118 for (GPBMessage *value in autocreatedValues) {
1119 NSCAssert(GPBWasMessageAutocreatedBy(value, self),
1120 @"Autocreated extension does not refer back to self.");
1121 GPBClearMessageAutocreator(value);
1124 [extensionMap_ release];
1125 extensionMap_ = nil;
1126 [unknownFields_ release];
1127 unknownFields_ = nil;
1129 // Note that clearing does not affect autocreator_. If we are being cleared
1130 // because of a dealloc, then autocreator_ should be nil anyway. If we are
1131 // being cleared because someone explicitly clears us, we don't want to
1132 // sever our relationship with our autocreator.
1135 memset(messageStorage_, 0, descriptor->storageSize_);
1139 - (BOOL)isInitialized {
1140 GPBDescriptor *descriptor = [self descriptor];
1141 for (GPBFieldDescriptor *field in descriptor->fields_) {
1142 if (field.isRequired) {
1143 if (!GPBGetHasIvarField(self, field)) {
1147 if (GPBFieldDataTypeIsMessage(field)) {
1148 GPBFieldType fieldType = field.fieldType;
1149 if (fieldType == GPBFieldTypeSingle) {
1150 if (field.isRequired) {
1151 GPBMessage *message = GPBGetMessageMessageField(self, field);
1152 if (!message.initialized) {
1156 NSAssert(field.isOptional,
1157 @"%@: Single message field %@ not required or optional?",
1158 [self class], field.name);
1159 if (GPBGetHasIvarField(self, field)) {
1160 GPBMessage *message = GPBGetMessageMessageField(self, field);
1161 if (!message.initialized) {
1166 } else if (fieldType == GPBFieldTypeRepeated) {
1167 NSArray *array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1168 for (GPBMessage *message in array) {
1169 if (!message.initialized) {
1173 } else { // fieldType == GPBFieldTypeMap
1174 if (field.mapKeyDataType == GPBDataTypeString) {
1176 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1177 if (map && !GPBDictionaryIsInitializedInternalHelper(map, field)) {
1181 // Real type is GPB*ObjectDictionary, exact type doesn't matter.
1182 GPBInt32ObjectDictionary *map =
1183 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1184 if (map && ![map isInitialized]) {
1192 __block BOOL result = YES;
1194 enumerateKeysAndObjectsUsingBlock:^(GPBExtensionDescriptor *extension,
1197 if (GPBExtensionIsMessage(extension)) {
1198 if (extension.isRepeated) {
1199 for (GPBMessage *msg in obj) {
1200 if (!msg.initialized) {
1207 GPBMessage *asMsg = obj;
1208 if (!asMsg.initialized) {
1218 - (GPBDescriptor *)descriptor {
1219 return [[self class] descriptor];
1224 if (!self.initialized) {
1228 NSMutableData *data = [NSMutableData dataWithLength:[self serializedSize]];
1229 GPBCodedOutputStream *stream =
1230 [[GPBCodedOutputStream alloc] initWithData:data];
1232 [self writeToCodedOutputStream:stream];
1234 @catch (NSException *exception) {
1235 // This really shouldn't happen. The only way writeToCodedOutputStream:
1236 // could throw is if something in the library has a bug and the
1237 // serializedSize was wrong.
1239 NSLog(@"%@: Internal exception while building message data: %@",
1240 [self class], exception);
1248 - (NSData *)delimitedData {
1249 size_t serializedSize = [self serializedSize];
1250 size_t varintSize = GPBComputeRawVarint32SizeForInteger(serializedSize);
1251 NSMutableData *data =
1252 [NSMutableData dataWithLength:(serializedSize + varintSize)];
1253 GPBCodedOutputStream *stream =
1254 [[GPBCodedOutputStream alloc] initWithData:data];
1256 [self writeDelimitedToCodedOutputStream:stream];
1258 @catch (NSException *exception) {
1259 // This really shouldn't happen. The only way writeToCodedOutputStream:
1260 // could throw is if something in the library has a bug and the
1261 // serializedSize was wrong.
1263 NSLog(@"%@: Internal exception while building message delimitedData: %@",
1264 [self class], exception);
1266 // If it happens, truncate.
1273 - (void)writeToOutputStream:(NSOutputStream *)output {
1274 GPBCodedOutputStream *stream =
1275 [[GPBCodedOutputStream alloc] initWithOutputStream:output];
1276 [self writeToCodedOutputStream:stream];
1280 - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)output {
1281 GPBDescriptor *descriptor = [self descriptor];
1282 NSArray *fieldsArray = descriptor->fields_;
1283 NSUInteger fieldCount = fieldsArray.count;
1284 const GPBExtensionRange *extensionRanges = descriptor.extensionRanges;
1285 NSUInteger extensionRangesCount = descriptor.extensionRangesCount;
1286 for (NSUInteger i = 0, j = 0; i < fieldCount || j < extensionRangesCount;) {
1287 if (i == fieldCount) {
1288 [self writeExtensionsToCodedOutputStream:output
1289 range:extensionRanges[j++]];
1290 } else if (j == extensionRangesCount ||
1291 GPBFieldNumber(fieldsArray[i]) < extensionRanges[j].start) {
1292 [self writeField:fieldsArray[i++] toCodedOutputStream:output];
1294 [self writeExtensionsToCodedOutputStream:output
1295 range:extensionRanges[j++]];
1298 if (descriptor.isWireFormat) {
1299 [unknownFields_ writeAsMessageSetTo:output];
1301 [unknownFields_ writeToCodedOutputStream:output];
1305 - (void)writeDelimitedToOutputStream:(NSOutputStream *)output {
1306 GPBCodedOutputStream *codedOutput =
1307 [[GPBCodedOutputStream alloc] initWithOutputStream:output];
1308 [self writeDelimitedToCodedOutputStream:codedOutput];
1309 [codedOutput release];
1312 - (void)writeDelimitedToCodedOutputStream:(GPBCodedOutputStream *)output {
1313 [output writeRawVarintSizeTAs32:[self serializedSize]];
1314 [self writeToCodedOutputStream:output];
1317 - (void)writeField:(GPBFieldDescriptor *)field
1318 toCodedOutputStream:(GPBCodedOutputStream *)output {
1319 GPBFieldType fieldType = field.fieldType;
1320 if (fieldType == GPBFieldTypeSingle) {
1321 BOOL has = GPBGetHasIvarField(self, field);
1326 uint32_t fieldNumber = GPBFieldNumber(field);
1328 //%PDDM-DEFINE FIELD_CASE(TYPE, REAL_TYPE)
1329 //%FIELD_CASE_FULL(TYPE, REAL_TYPE, REAL_TYPE)
1330 //%PDDM-DEFINE FIELD_CASE_FULL(TYPE, REAL_TYPE, ARRAY_TYPE)
1331 //% case GPBDataType##TYPE:
1332 //% if (fieldType == GPBFieldTypeRepeated) {
1333 //% uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
1334 //% GPB##ARRAY_TYPE##Array *array =
1335 //% GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1336 //% [output write##TYPE##Array:fieldNumber values:array tag:tag];
1337 //% } else if (fieldType == GPBFieldTypeSingle) {
1338 //% [output write##TYPE:fieldNumber
1339 //% TYPE$S value:GPBGetMessage##REAL_TYPE##Field(self, field)];
1340 //% } else { // fieldType == GPBFieldTypeMap
1341 //% // Exact type here doesn't matter.
1342 //% GPBInt32##ARRAY_TYPE##Dictionary *dict =
1343 //% GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1344 //% [dict writeToCodedOutputStream:output asField:field];
1348 //%PDDM-DEFINE FIELD_CASE2(TYPE)
1349 //% case GPBDataType##TYPE:
1350 //% if (fieldType == GPBFieldTypeRepeated) {
1351 //% NSArray *array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1352 //% [output write##TYPE##Array:fieldNumber values:array];
1353 //% } else if (fieldType == GPBFieldTypeSingle) {
1354 //% // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has check
1356 //% [output write##TYPE:fieldNumber
1357 //% TYPE$S value:GPBGetObjectIvarWithFieldNoAutocreate(self, field)];
1358 //% } else { // fieldType == GPBFieldTypeMap
1359 //% // Exact type here doesn't matter.
1360 //% id dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1361 //% GPBDataType mapKeyDataType = field.mapKeyDataType;
1362 //% if (mapKeyDataType == GPBDataTypeString) {
1363 //% GPBDictionaryWriteToStreamInternalHelper(output, dict, field);
1365 //% [dict writeToCodedOutputStream:output asField:field];
1371 switch (GPBGetFieldDataType(field)) {
1373 //%PDDM-EXPAND FIELD_CASE(Bool, Bool)
1374 // This block of code is generated, do not edit it directly.
1376 case GPBDataTypeBool:
1377 if (fieldType == GPBFieldTypeRepeated) {
1378 uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
1379 GPBBoolArray *array =
1380 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1381 [output writeBoolArray:fieldNumber values:array tag:tag];
1382 } else if (fieldType == GPBFieldTypeSingle) {
1383 [output writeBool:fieldNumber
1384 value:GPBGetMessageBoolField(self, field)];
1385 } else { // fieldType == GPBFieldTypeMap
1386 // Exact type here doesn't matter.
1387 GPBInt32BoolDictionary *dict =
1388 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1389 [dict writeToCodedOutputStream:output asField:field];
1393 //%PDDM-EXPAND FIELD_CASE(Fixed32, UInt32)
1394 // This block of code is generated, do not edit it directly.
1396 case GPBDataTypeFixed32:
1397 if (fieldType == GPBFieldTypeRepeated) {
1398 uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
1399 GPBUInt32Array *array =
1400 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1401 [output writeFixed32Array:fieldNumber values:array tag:tag];
1402 } else if (fieldType == GPBFieldTypeSingle) {
1403 [output writeFixed32:fieldNumber
1404 value:GPBGetMessageUInt32Field(self, field)];
1405 } else { // fieldType == GPBFieldTypeMap
1406 // Exact type here doesn't matter.
1407 GPBInt32UInt32Dictionary *dict =
1408 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1409 [dict writeToCodedOutputStream:output asField:field];
1413 //%PDDM-EXPAND FIELD_CASE(SFixed32, Int32)
1414 // This block of code is generated, do not edit it directly.
1416 case GPBDataTypeSFixed32:
1417 if (fieldType == GPBFieldTypeRepeated) {
1418 uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
1419 GPBInt32Array *array =
1420 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1421 [output writeSFixed32Array:fieldNumber values:array tag:tag];
1422 } else if (fieldType == GPBFieldTypeSingle) {
1423 [output writeSFixed32:fieldNumber
1424 value:GPBGetMessageInt32Field(self, field)];
1425 } else { // fieldType == GPBFieldTypeMap
1426 // Exact type here doesn't matter.
1427 GPBInt32Int32Dictionary *dict =
1428 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1429 [dict writeToCodedOutputStream:output asField:field];
1433 //%PDDM-EXPAND FIELD_CASE(Float, Float)
1434 // This block of code is generated, do not edit it directly.
1436 case GPBDataTypeFloat:
1437 if (fieldType == GPBFieldTypeRepeated) {
1438 uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
1439 GPBFloatArray *array =
1440 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1441 [output writeFloatArray:fieldNumber values:array tag:tag];
1442 } else if (fieldType == GPBFieldTypeSingle) {
1443 [output writeFloat:fieldNumber
1444 value:GPBGetMessageFloatField(self, field)];
1445 } else { // fieldType == GPBFieldTypeMap
1446 // Exact type here doesn't matter.
1447 GPBInt32FloatDictionary *dict =
1448 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1449 [dict writeToCodedOutputStream:output asField:field];
1453 //%PDDM-EXPAND FIELD_CASE(Fixed64, UInt64)
1454 // This block of code is generated, do not edit it directly.
1456 case GPBDataTypeFixed64:
1457 if (fieldType == GPBFieldTypeRepeated) {
1458 uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
1459 GPBUInt64Array *array =
1460 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1461 [output writeFixed64Array:fieldNumber values:array tag:tag];
1462 } else if (fieldType == GPBFieldTypeSingle) {
1463 [output writeFixed64:fieldNumber
1464 value:GPBGetMessageUInt64Field(self, field)];
1465 } else { // fieldType == GPBFieldTypeMap
1466 // Exact type here doesn't matter.
1467 GPBInt32UInt64Dictionary *dict =
1468 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1469 [dict writeToCodedOutputStream:output asField:field];
1473 //%PDDM-EXPAND FIELD_CASE(SFixed64, Int64)
1474 // This block of code is generated, do not edit it directly.
1476 case GPBDataTypeSFixed64:
1477 if (fieldType == GPBFieldTypeRepeated) {
1478 uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
1479 GPBInt64Array *array =
1480 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1481 [output writeSFixed64Array:fieldNumber values:array tag:tag];
1482 } else if (fieldType == GPBFieldTypeSingle) {
1483 [output writeSFixed64:fieldNumber
1484 value:GPBGetMessageInt64Field(self, field)];
1485 } else { // fieldType == GPBFieldTypeMap
1486 // Exact type here doesn't matter.
1487 GPBInt32Int64Dictionary *dict =
1488 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1489 [dict writeToCodedOutputStream:output asField:field];
1493 //%PDDM-EXPAND FIELD_CASE(Double, Double)
1494 // This block of code is generated, do not edit it directly.
1496 case GPBDataTypeDouble:
1497 if (fieldType == GPBFieldTypeRepeated) {
1498 uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
1499 GPBDoubleArray *array =
1500 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1501 [output writeDoubleArray:fieldNumber values:array tag:tag];
1502 } else if (fieldType == GPBFieldTypeSingle) {
1503 [output writeDouble:fieldNumber
1504 value:GPBGetMessageDoubleField(self, field)];
1505 } else { // fieldType == GPBFieldTypeMap
1506 // Exact type here doesn't matter.
1507 GPBInt32DoubleDictionary *dict =
1508 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1509 [dict writeToCodedOutputStream:output asField:field];
1513 //%PDDM-EXPAND FIELD_CASE(Int32, Int32)
1514 // This block of code is generated, do not edit it directly.
1516 case GPBDataTypeInt32:
1517 if (fieldType == GPBFieldTypeRepeated) {
1518 uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
1519 GPBInt32Array *array =
1520 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1521 [output writeInt32Array:fieldNumber values:array tag:tag];
1522 } else if (fieldType == GPBFieldTypeSingle) {
1523 [output writeInt32:fieldNumber
1524 value:GPBGetMessageInt32Field(self, field)];
1525 } else { // fieldType == GPBFieldTypeMap
1526 // Exact type here doesn't matter.
1527 GPBInt32Int32Dictionary *dict =
1528 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1529 [dict writeToCodedOutputStream:output asField:field];
1533 //%PDDM-EXPAND FIELD_CASE(Int64, Int64)
1534 // This block of code is generated, do not edit it directly.
1536 case GPBDataTypeInt64:
1537 if (fieldType == GPBFieldTypeRepeated) {
1538 uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
1539 GPBInt64Array *array =
1540 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1541 [output writeInt64Array:fieldNumber values:array tag:tag];
1542 } else if (fieldType == GPBFieldTypeSingle) {
1543 [output writeInt64:fieldNumber
1544 value:GPBGetMessageInt64Field(self, field)];
1545 } else { // fieldType == GPBFieldTypeMap
1546 // Exact type here doesn't matter.
1547 GPBInt32Int64Dictionary *dict =
1548 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1549 [dict writeToCodedOutputStream:output asField:field];
1553 //%PDDM-EXPAND FIELD_CASE(SInt32, Int32)
1554 // This block of code is generated, do not edit it directly.
1556 case GPBDataTypeSInt32:
1557 if (fieldType == GPBFieldTypeRepeated) {
1558 uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
1559 GPBInt32Array *array =
1560 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1561 [output writeSInt32Array:fieldNumber values:array tag:tag];
1562 } else if (fieldType == GPBFieldTypeSingle) {
1563 [output writeSInt32:fieldNumber
1564 value:GPBGetMessageInt32Field(self, field)];
1565 } else { // fieldType == GPBFieldTypeMap
1566 // Exact type here doesn't matter.
1567 GPBInt32Int32Dictionary *dict =
1568 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1569 [dict writeToCodedOutputStream:output asField:field];
1573 //%PDDM-EXPAND FIELD_CASE(SInt64, Int64)
1574 // This block of code is generated, do not edit it directly.
1576 case GPBDataTypeSInt64:
1577 if (fieldType == GPBFieldTypeRepeated) {
1578 uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
1579 GPBInt64Array *array =
1580 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1581 [output writeSInt64Array:fieldNumber values:array tag:tag];
1582 } else if (fieldType == GPBFieldTypeSingle) {
1583 [output writeSInt64:fieldNumber
1584 value:GPBGetMessageInt64Field(self, field)];
1585 } else { // fieldType == GPBFieldTypeMap
1586 // Exact type here doesn't matter.
1587 GPBInt32Int64Dictionary *dict =
1588 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1589 [dict writeToCodedOutputStream:output asField:field];
1593 //%PDDM-EXPAND FIELD_CASE(UInt32, UInt32)
1594 // This block of code is generated, do not edit it directly.
1596 case GPBDataTypeUInt32:
1597 if (fieldType == GPBFieldTypeRepeated) {
1598 uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
1599 GPBUInt32Array *array =
1600 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1601 [output writeUInt32Array:fieldNumber values:array tag:tag];
1602 } else if (fieldType == GPBFieldTypeSingle) {
1603 [output writeUInt32:fieldNumber
1604 value:GPBGetMessageUInt32Field(self, field)];
1605 } else { // fieldType == GPBFieldTypeMap
1606 // Exact type here doesn't matter.
1607 GPBInt32UInt32Dictionary *dict =
1608 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1609 [dict writeToCodedOutputStream:output asField:field];
1613 //%PDDM-EXPAND FIELD_CASE(UInt64, UInt64)
1614 // This block of code is generated, do not edit it directly.
1616 case GPBDataTypeUInt64:
1617 if (fieldType == GPBFieldTypeRepeated) {
1618 uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
1619 GPBUInt64Array *array =
1620 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1621 [output writeUInt64Array:fieldNumber values:array tag:tag];
1622 } else if (fieldType == GPBFieldTypeSingle) {
1623 [output writeUInt64:fieldNumber
1624 value:GPBGetMessageUInt64Field(self, field)];
1625 } else { // fieldType == GPBFieldTypeMap
1626 // Exact type here doesn't matter.
1627 GPBInt32UInt64Dictionary *dict =
1628 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1629 [dict writeToCodedOutputStream:output asField:field];
1633 //%PDDM-EXPAND FIELD_CASE_FULL(Enum, Int32, Enum)
1634 // This block of code is generated, do not edit it directly.
1636 case GPBDataTypeEnum:
1637 if (fieldType == GPBFieldTypeRepeated) {
1638 uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
1639 GPBEnumArray *array =
1640 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1641 [output writeEnumArray:fieldNumber values:array tag:tag];
1642 } else if (fieldType == GPBFieldTypeSingle) {
1643 [output writeEnum:fieldNumber
1644 value:GPBGetMessageInt32Field(self, field)];
1645 } else { // fieldType == GPBFieldTypeMap
1646 // Exact type here doesn't matter.
1647 GPBInt32EnumDictionary *dict =
1648 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1649 [dict writeToCodedOutputStream:output asField:field];
1653 //%PDDM-EXPAND FIELD_CASE2(Bytes)
1654 // This block of code is generated, do not edit it directly.
1656 case GPBDataTypeBytes:
1657 if (fieldType == GPBFieldTypeRepeated) {
1658 NSArray *array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1659 [output writeBytesArray:fieldNumber values:array];
1660 } else if (fieldType == GPBFieldTypeSingle) {
1661 // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has check
1663 [output writeBytes:fieldNumber
1664 value:GPBGetObjectIvarWithFieldNoAutocreate(self, field)];
1665 } else { // fieldType == GPBFieldTypeMap
1666 // Exact type here doesn't matter.
1667 id dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1668 GPBDataType mapKeyDataType = field.mapKeyDataType;
1669 if (mapKeyDataType == GPBDataTypeString) {
1670 GPBDictionaryWriteToStreamInternalHelper(output, dict, field);
1672 [dict writeToCodedOutputStream:output asField:field];
1677 //%PDDM-EXPAND FIELD_CASE2(String)
1678 // This block of code is generated, do not edit it directly.
1680 case GPBDataTypeString:
1681 if (fieldType == GPBFieldTypeRepeated) {
1682 NSArray *array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1683 [output writeStringArray:fieldNumber values:array];
1684 } else if (fieldType == GPBFieldTypeSingle) {
1685 // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has check
1687 [output writeString:fieldNumber
1688 value:GPBGetObjectIvarWithFieldNoAutocreate(self, field)];
1689 } else { // fieldType == GPBFieldTypeMap
1690 // Exact type here doesn't matter.
1691 id dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1692 GPBDataType mapKeyDataType = field.mapKeyDataType;
1693 if (mapKeyDataType == GPBDataTypeString) {
1694 GPBDictionaryWriteToStreamInternalHelper(output, dict, field);
1696 [dict writeToCodedOutputStream:output asField:field];
1701 //%PDDM-EXPAND FIELD_CASE2(Message)
1702 // This block of code is generated, do not edit it directly.
1704 case GPBDataTypeMessage:
1705 if (fieldType == GPBFieldTypeRepeated) {
1706 NSArray *array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1707 [output writeMessageArray:fieldNumber values:array];
1708 } else if (fieldType == GPBFieldTypeSingle) {
1709 // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has check
1711 [output writeMessage:fieldNumber
1712 value:GPBGetObjectIvarWithFieldNoAutocreate(self, field)];
1713 } else { // fieldType == GPBFieldTypeMap
1714 // Exact type here doesn't matter.
1715 id dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1716 GPBDataType mapKeyDataType = field.mapKeyDataType;
1717 if (mapKeyDataType == GPBDataTypeString) {
1718 GPBDictionaryWriteToStreamInternalHelper(output, dict, field);
1720 [dict writeToCodedOutputStream:output asField:field];
1725 //%PDDM-EXPAND FIELD_CASE2(Group)
1726 // This block of code is generated, do not edit it directly.
1728 case GPBDataTypeGroup:
1729 if (fieldType == GPBFieldTypeRepeated) {
1730 NSArray *array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1731 [output writeGroupArray:fieldNumber values:array];
1732 } else if (fieldType == GPBFieldTypeSingle) {
1733 // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has check
1735 [output writeGroup:fieldNumber
1736 value:GPBGetObjectIvarWithFieldNoAutocreate(self, field)];
1737 } else { // fieldType == GPBFieldTypeMap
1738 // Exact type here doesn't matter.
1739 id dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
1740 GPBDataType mapKeyDataType = field.mapKeyDataType;
1741 if (mapKeyDataType == GPBDataTypeString) {
1742 GPBDictionaryWriteToStreamInternalHelper(output, dict, field);
1744 [dict writeToCodedOutputStream:output asField:field];
1749 //%PDDM-EXPAND-END (18 expansions)
1753 #pragma mark - Extensions
1755 - (id)getExtension:(GPBExtensionDescriptor *)extension {
1756 CheckExtension(self, extension);
1757 id value = [extensionMap_ objectForKey:extension];
1762 // No default for repeated.
1763 if (extension.isRepeated) {
1766 // Non messages get their default.
1767 if (!GPBExtensionIsMessage(extension)) {
1768 return extension.defaultValue;
1771 // Check for an autocreated value.
1772 GPBPrepareReadOnlySemaphore(self);
1773 dispatch_semaphore_wait(readOnlySemaphore_, DISPATCH_TIME_FOREVER);
1774 value = [autocreatedExtensionMap_ objectForKey:extension];
1776 // Auto create the message extensions to match normal fields.
1777 value = CreateMessageWithAutocreatorForExtension(extension.msgClass, self,
1780 if (autocreatedExtensionMap_ == nil) {
1781 autocreatedExtensionMap_ = [[NSMutableDictionary alloc] init];
1784 // We can't simply call setExtension here because that would clear the new
1785 // value's autocreator.
1786 [autocreatedExtensionMap_ setObject:value forKey:extension];
1790 dispatch_semaphore_signal(readOnlySemaphore_);
1794 - (id)getExistingExtension:(GPBExtensionDescriptor *)extension {
1795 // This is an internal method so we don't need to call CheckExtension().
1796 return [extensionMap_ objectForKey:extension];
1799 - (BOOL)hasExtension:(GPBExtensionDescriptor *)extension {
1800 #if defined(DEBUG) && DEBUG
1801 CheckExtension(self, extension);
1803 return nil != [extensionMap_ objectForKey:extension];
1806 - (NSArray *)extensionsCurrentlySet {
1807 return [extensionMap_ allKeys];
1810 - (void)writeExtensionsToCodedOutputStream:(GPBCodedOutputStream *)output
1811 range:(GPBExtensionRange)range {
1812 NSArray *sortedExtensions = [[extensionMap_ allKeys]
1813 sortedArrayUsingSelector:@selector(compareByFieldNumber:)];
1814 uint32_t start = range.start;
1815 uint32_t end = range.end;
1816 for (GPBExtensionDescriptor *extension in sortedExtensions) {
1817 uint32_t fieldNumber = extension.fieldNumber;
1818 if (fieldNumber >= start && fieldNumber < end) {
1819 id value = [extensionMap_ objectForKey:extension];
1820 GPBWriteExtensionValueToOutputStream(extension, value, output);
1825 - (void)setExtension:(GPBExtensionDescriptor *)extension value:(id)value {
1827 [self clearExtension:extension];
1831 CheckExtension(self, extension);
1833 if (extension.repeated) {
1834 [NSException raise:NSInvalidArgumentException
1835 format:@"Must call addExtension() for repeated types."];
1838 if (extensionMap_ == nil) {
1839 extensionMap_ = [[NSMutableDictionary alloc] init];
1842 // This pointless cast is for CLANG_WARN_NULLABLE_TO_NONNULL_CONVERSION.
1843 // Without it, the compiler complains we're passing an id nullable when
1844 // setObject:forKey: requires a id nonnull for the value. The check for
1845 // !value at the start of the method ensures it isn't nil, but the check
1846 // isn't smart enough to realize that.
1847 [extensionMap_ setObject:(id)value forKey:extension];
1849 GPBExtensionDescriptor *descriptor = extension;
1851 if (GPBExtensionIsMessage(descriptor) && !descriptor.isRepeated) {
1852 GPBMessage *autocreatedValue =
1853 [[autocreatedExtensionMap_ objectForKey:extension] retain];
1854 // Must remove from the map before calling GPBClearMessageAutocreator() so
1855 // that GPBClearMessageAutocreator() knows its safe to clear.
1856 [autocreatedExtensionMap_ removeObjectForKey:extension];
1857 GPBClearMessageAutocreator(autocreatedValue);
1858 [autocreatedValue release];
1861 GPBBecomeVisibleToAutocreator(self);
1864 - (void)addExtension:(GPBExtensionDescriptor *)extension value:(id)value {
1865 CheckExtension(self, extension);
1867 if (!extension.repeated) {
1868 [NSException raise:NSInvalidArgumentException
1869 format:@"Must call setExtension() for singular types."];
1872 if (extensionMap_ == nil) {
1873 extensionMap_ = [[NSMutableDictionary alloc] init];
1875 NSMutableArray *list = [extensionMap_ objectForKey:extension];
1877 list = [NSMutableArray array];
1878 [extensionMap_ setObject:list forKey:extension];
1881 [list addObject:value];
1882 GPBBecomeVisibleToAutocreator(self);
1885 - (void)setExtension:(GPBExtensionDescriptor *)extension
1886 index:(NSUInteger)idx
1888 CheckExtension(self, extension);
1890 if (!extension.repeated) {
1891 [NSException raise:NSInvalidArgumentException
1892 format:@"Must call setExtension() for singular types."];
1895 if (extensionMap_ == nil) {
1896 extensionMap_ = [[NSMutableDictionary alloc] init];
1899 NSMutableArray *list = [extensionMap_ objectForKey:extension];
1901 [list replaceObjectAtIndex:idx withObject:value];
1902 GPBBecomeVisibleToAutocreator(self);
1905 - (void)clearExtension:(GPBExtensionDescriptor *)extension {
1906 CheckExtension(self, extension);
1908 // Only become visible if there was actually a value to clear.
1909 if ([extensionMap_ objectForKey:extension]) {
1910 [extensionMap_ removeObjectForKey:extension];
1911 GPBBecomeVisibleToAutocreator(self);
1915 #pragma mark - mergeFrom
1917 - (void)mergeFromData:(NSData *)data
1918 extensionRegistry:(GPBExtensionRegistry *)extensionRegistry {
1919 GPBCodedInputStream *input = [[GPBCodedInputStream alloc] initWithData:data];
1920 [self mergeFromCodedInputStream:input extensionRegistry:extensionRegistry];
1921 [input checkLastTagWas:0];
1925 #pragma mark - mergeDelimitedFrom
1927 - (void)mergeDelimitedFromCodedInputStream:(GPBCodedInputStream *)input
1928 extensionRegistry:(GPBExtensionRegistry *)extensionRegistry {
1929 GPBCodedInputStreamState *state = &input->state_;
1930 if (GPBCodedInputStreamIsAtEnd(state)) {
1933 NSData *data = GPBCodedInputStreamReadRetainedBytesNoCopy(state);
1937 [self mergeFromData:data extensionRegistry:extensionRegistry];
1941 #pragma mark - Parse From Data Support
1943 + (instancetype)parseFromData:(NSData *)data error:(NSError **)errorPtr {
1944 return [self parseFromData:data extensionRegistry:nil error:errorPtr];
1947 + (instancetype)parseFromData:(NSData *)data
1948 extensionRegistry:(GPBExtensionRegistry *)extensionRegistry
1949 error:(NSError **)errorPtr {
1950 return [[[self alloc] initWithData:data
1951 extensionRegistry:extensionRegistry
1952 error:errorPtr] autorelease];
1955 + (instancetype)parseFromCodedInputStream:(GPBCodedInputStream *)input
1956 extensionRegistry:(GPBExtensionRegistry *)extensionRegistry
1957 error:(NSError **)errorPtr {
1959 [[[self alloc] initWithCodedInputStream:input
1960 extensionRegistry:extensionRegistry
1961 error:errorPtr] autorelease];
1964 #pragma mark - Parse Delimited From Data Support
1966 + (instancetype)parseDelimitedFromCodedInputStream:(GPBCodedInputStream *)input
1968 (GPBExtensionRegistry *)extensionRegistry
1969 error:(NSError **)errorPtr {
1970 GPBMessage *message = [[[self alloc] init] autorelease];
1972 [message mergeDelimitedFromCodedInputStream:input
1973 extensionRegistry:extensionRegistry];
1978 @catch (NSException *exception) {
1981 *errorPtr = ErrorFromException(exception);
1985 if (message && !message.initialized) {
1988 *errorPtr = MessageError(GPBMessageErrorCodeMissingRequiredField, nil);
1995 #pragma mark - Unknown Field Support
1997 - (GPBUnknownFieldSet *)unknownFields {
1998 return unknownFields_;
2001 - (void)setUnknownFields:(GPBUnknownFieldSet *)unknownFields {
2002 if (unknownFields != unknownFields_) {
2003 [unknownFields_ release];
2004 unknownFields_ = [unknownFields copy];
2005 GPBBecomeVisibleToAutocreator(self);
2009 - (void)parseMessageSet:(GPBCodedInputStream *)input
2010 extensionRegistry:(GPBExtensionRegistry *)extensionRegistry {
2011 uint32_t typeId = 0;
2012 NSData *rawBytes = nil;
2013 GPBExtensionDescriptor *extension = nil;
2014 GPBCodedInputStreamState *state = &input->state_;
2016 uint32_t tag = GPBCodedInputStreamReadTag(state);
2021 if (tag == GPBWireFormatMessageSetTypeIdTag) {
2022 typeId = GPBCodedInputStreamReadUInt32(state);
2024 extension = [extensionRegistry extensionForDescriptor:[self descriptor]
2025 fieldNumber:typeId];
2027 } else if (tag == GPBWireFormatMessageSetMessageTag) {
2029 [GPBCodedInputStreamReadRetainedBytesNoCopy(state) autorelease];
2031 if (![input skipField:tag]) {
2037 [input checkLastTagWas:GPBWireFormatMessageSetItemEndTag];
2039 if (rawBytes != nil && typeId != 0) {
2040 if (extension != nil) {
2041 GPBCodedInputStream *newInput =
2042 [[GPBCodedInputStream alloc] initWithData:rawBytes];
2043 GPBExtensionMergeFromInputStream(extension,
2050 GPBUnknownFieldSet *unknownFields = GetOrMakeUnknownFields(self);
2051 // rawBytes was created via a NoCopy, so it can be reusing a
2052 // subrange of another NSData that might go out of scope as things
2053 // unwind, so a copy is needed to ensure what is saved in the
2054 // unknown fields stays valid.
2055 NSData *cloned = [NSData dataWithData:rawBytes];
2056 [unknownFields mergeMessageSetMessage:typeId data:cloned];
2061 - (BOOL)parseUnknownField:(GPBCodedInputStream *)input
2062 extensionRegistry:(GPBExtensionRegistry *)extensionRegistry
2064 GPBWireFormat wireType = GPBWireFormatGetTagWireType(tag);
2065 int32_t fieldNumber = GPBWireFormatGetTagFieldNumber(tag);
2067 GPBDescriptor *descriptor = [self descriptor];
2068 GPBExtensionDescriptor *extension =
2069 [extensionRegistry extensionForDescriptor:descriptor
2070 fieldNumber:fieldNumber];
2071 if (extension == nil) {
2072 if (descriptor.wireFormat && GPBWireFormatMessageSetItemTag == tag) {
2073 [self parseMessageSet:input extensionRegistry:extensionRegistry];
2077 if (extension.wireType == wireType) {
2078 GPBExtensionMergeFromInputStream(extension,
2085 // Primitive, repeated types can be packed on unpacked on the wire, and are
2086 // parsed either way.
2087 if ([extension isRepeated] &&
2088 !GPBDataTypeIsObject(extension->description_->dataType) &&
2089 (extension.alternateWireType == wireType)) {
2090 GPBExtensionMergeFromInputStream(extension,
2091 !extension.packable,
2098 if ([GPBUnknownFieldSet isFieldTag:tag]) {
2099 GPBUnknownFieldSet *unknownFields = GetOrMakeUnknownFields(self);
2100 return [unknownFields mergeFieldFrom:tag input:input];
2106 - (void)addUnknownMapEntry:(int32_t)fieldNum value:(NSData *)data {
2107 GPBUnknownFieldSet *unknownFields = GetOrMakeUnknownFields(self);
2108 [unknownFields addUnknownMapEntry:fieldNum value:data];
2111 #pragma mark - MergeFromCodedInputStream Support
2113 static void MergeSingleFieldFromCodedInputStream(
2114 GPBMessage *self, GPBFieldDescriptor *field, GPBFileSyntax syntax,
2115 GPBCodedInputStream *input, GPBExtensionRegistry *extensionRegistry) {
2116 GPBDataType fieldDataType = GPBGetFieldDataType(field);
2117 switch (fieldDataType) {
2118 #define CASE_SINGLE_POD(NAME, TYPE, FUNC_TYPE) \
2119 case GPBDataType##NAME: { \
2120 TYPE val = GPBCodedInputStreamRead##NAME(&input->state_); \
2121 GPBSet##FUNC_TYPE##IvarWithFieldInternal(self, field, val, syntax); \
2124 #define CASE_SINGLE_OBJECT(NAME) \
2125 case GPBDataType##NAME: { \
2126 id val = GPBCodedInputStreamReadRetained##NAME(&input->state_); \
2127 GPBSetRetainedObjectIvarWithFieldInternal(self, field, val, syntax); \
2130 CASE_SINGLE_POD(Bool, BOOL, Bool)
2131 CASE_SINGLE_POD(Fixed32, uint32_t, UInt32)
2132 CASE_SINGLE_POD(SFixed32, int32_t, Int32)
2133 CASE_SINGLE_POD(Float, float, Float)
2134 CASE_SINGLE_POD(Fixed64, uint64_t, UInt64)
2135 CASE_SINGLE_POD(SFixed64, int64_t, Int64)
2136 CASE_SINGLE_POD(Double, double, Double)
2137 CASE_SINGLE_POD(Int32, int32_t, Int32)
2138 CASE_SINGLE_POD(Int64, int64_t, Int64)
2139 CASE_SINGLE_POD(SInt32, int32_t, Int32)
2140 CASE_SINGLE_POD(SInt64, int64_t, Int64)
2141 CASE_SINGLE_POD(UInt32, uint32_t, UInt32)
2142 CASE_SINGLE_POD(UInt64, uint64_t, UInt64)
2143 CASE_SINGLE_OBJECT(Bytes)
2144 CASE_SINGLE_OBJECT(String)
2145 #undef CASE_SINGLE_POD
2146 #undef CASE_SINGLE_OBJECT
2148 case GPBDataTypeMessage: {
2149 if (GPBGetHasIvarField(self, field)) {
2150 // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has
2152 GPBMessage *message =
2153 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
2154 [input readMessage:message extensionRegistry:extensionRegistry];
2156 GPBMessage *message = [[field.msgClass alloc] init];
2157 [input readMessage:message extensionRegistry:extensionRegistry];
2158 GPBSetRetainedObjectIvarWithFieldInternal(self, field, message, syntax);
2163 case GPBDataTypeGroup: {
2164 if (GPBGetHasIvarField(self, field)) {
2165 // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has
2167 GPBMessage *message =
2168 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
2169 [input readGroup:GPBFieldNumber(field)
2171 extensionRegistry:extensionRegistry];
2173 GPBMessage *message = [[field.msgClass alloc] init];
2174 [input readGroup:GPBFieldNumber(field)
2176 extensionRegistry:extensionRegistry];
2177 GPBSetRetainedObjectIvarWithFieldInternal(self, field, message, syntax);
2182 case GPBDataTypeEnum: {
2183 int32_t val = GPBCodedInputStreamReadEnum(&input->state_);
2184 if (GPBHasPreservingUnknownEnumSemantics(syntax) ||
2185 [field isValidEnumValue:val]) {
2186 GPBSetInt32IvarWithFieldInternal(self, field, val, syntax);
2188 GPBUnknownFieldSet *unknownFields = GetOrMakeUnknownFields(self);
2189 [unknownFields mergeVarintField:GPBFieldNumber(field) value:val];
2195 static void MergeRepeatedPackedFieldFromCodedInputStream(
2196 GPBMessage *self, GPBFieldDescriptor *field, GPBFileSyntax syntax,
2197 GPBCodedInputStream *input) {
2198 GPBDataType fieldDataType = GPBGetFieldDataType(field);
2199 GPBCodedInputStreamState *state = &input->state_;
2200 id genericArray = GetOrCreateArrayIvarWithField(self, field, syntax);
2201 int32_t length = GPBCodedInputStreamReadInt32(state);
2202 size_t limit = GPBCodedInputStreamPushLimit(state, length);
2203 while (GPBCodedInputStreamBytesUntilLimit(state) > 0) {
2204 switch (fieldDataType) {
2205 #define CASE_REPEATED_PACKED_POD(NAME, TYPE, ARRAY_TYPE) \
2206 case GPBDataType##NAME: { \
2207 TYPE val = GPBCodedInputStreamRead##NAME(state); \
2208 [(GPB##ARRAY_TYPE##Array *)genericArray addValue:val]; \
2211 CASE_REPEATED_PACKED_POD(Bool, BOOL, Bool)
2212 CASE_REPEATED_PACKED_POD(Fixed32, uint32_t, UInt32)
2213 CASE_REPEATED_PACKED_POD(SFixed32, int32_t, Int32)
2214 CASE_REPEATED_PACKED_POD(Float, float, Float)
2215 CASE_REPEATED_PACKED_POD(Fixed64, uint64_t, UInt64)
2216 CASE_REPEATED_PACKED_POD(SFixed64, int64_t, Int64)
2217 CASE_REPEATED_PACKED_POD(Double, double, Double)
2218 CASE_REPEATED_PACKED_POD(Int32, int32_t, Int32)
2219 CASE_REPEATED_PACKED_POD(Int64, int64_t, Int64)
2220 CASE_REPEATED_PACKED_POD(SInt32, int32_t, Int32)
2221 CASE_REPEATED_PACKED_POD(SInt64, int64_t, Int64)
2222 CASE_REPEATED_PACKED_POD(UInt32, uint32_t, UInt32)
2223 CASE_REPEATED_PACKED_POD(UInt64, uint64_t, UInt64)
2224 #undef CASE_REPEATED_PACKED_POD
2226 case GPBDataTypeBytes:
2227 case GPBDataTypeString:
2228 case GPBDataTypeMessage:
2229 case GPBDataTypeGroup:
2230 NSCAssert(NO, @"Non primitive types can't be packed");
2233 case GPBDataTypeEnum: {
2234 int32_t val = GPBCodedInputStreamReadEnum(state);
2235 if (GPBHasPreservingUnknownEnumSemantics(syntax) ||
2236 [field isValidEnumValue:val]) {
2237 [(GPBEnumArray*)genericArray addRawValue:val];
2239 GPBUnknownFieldSet *unknownFields = GetOrMakeUnknownFields(self);
2240 [unknownFields mergeVarintField:GPBFieldNumber(field) value:val];
2245 } // while(BytesUntilLimit() > 0)
2246 GPBCodedInputStreamPopLimit(state, limit);
2249 static void MergeRepeatedNotPackedFieldFromCodedInputStream(
2250 GPBMessage *self, GPBFieldDescriptor *field, GPBFileSyntax syntax,
2251 GPBCodedInputStream *input, GPBExtensionRegistry *extensionRegistry) {
2252 GPBCodedInputStreamState *state = &input->state_;
2253 id genericArray = GetOrCreateArrayIvarWithField(self, field, syntax);
2254 switch (GPBGetFieldDataType(field)) {
2255 #define CASE_REPEATED_NOT_PACKED_POD(NAME, TYPE, ARRAY_TYPE) \
2256 case GPBDataType##NAME: { \
2257 TYPE val = GPBCodedInputStreamRead##NAME(state); \
2258 [(GPB##ARRAY_TYPE##Array *)genericArray addValue:val]; \
2261 #define CASE_REPEATED_NOT_PACKED_OBJECT(NAME) \
2262 case GPBDataType##NAME: { \
2263 id val = GPBCodedInputStreamReadRetained##NAME(state); \
2264 [(NSMutableArray*)genericArray addObject:val]; \
2268 CASE_REPEATED_NOT_PACKED_POD(Bool, BOOL, Bool)
2269 CASE_REPEATED_NOT_PACKED_POD(Fixed32, uint32_t, UInt32)
2270 CASE_REPEATED_NOT_PACKED_POD(SFixed32, int32_t, Int32)
2271 CASE_REPEATED_NOT_PACKED_POD(Float, float, Float)
2272 CASE_REPEATED_NOT_PACKED_POD(Fixed64, uint64_t, UInt64)
2273 CASE_REPEATED_NOT_PACKED_POD(SFixed64, int64_t, Int64)
2274 CASE_REPEATED_NOT_PACKED_POD(Double, double, Double)
2275 CASE_REPEATED_NOT_PACKED_POD(Int32, int32_t, Int32)
2276 CASE_REPEATED_NOT_PACKED_POD(Int64, int64_t, Int64)
2277 CASE_REPEATED_NOT_PACKED_POD(SInt32, int32_t, Int32)
2278 CASE_REPEATED_NOT_PACKED_POD(SInt64, int64_t, Int64)
2279 CASE_REPEATED_NOT_PACKED_POD(UInt32, uint32_t, UInt32)
2280 CASE_REPEATED_NOT_PACKED_POD(UInt64, uint64_t, UInt64)
2281 CASE_REPEATED_NOT_PACKED_OBJECT(Bytes)
2282 CASE_REPEATED_NOT_PACKED_OBJECT(String)
2283 #undef CASE_REPEATED_NOT_PACKED_POD
2284 #undef CASE_NOT_PACKED_OBJECT
2285 case GPBDataTypeMessage: {
2286 GPBMessage *message = [[field.msgClass alloc] init];
2287 [input readMessage:message extensionRegistry:extensionRegistry];
2288 [(NSMutableArray*)genericArray addObject:message];
2292 case GPBDataTypeGroup: {
2293 GPBMessage *message = [[field.msgClass alloc] init];
2294 [input readGroup:GPBFieldNumber(field)
2296 extensionRegistry:extensionRegistry];
2297 [(NSMutableArray*)genericArray addObject:message];
2301 case GPBDataTypeEnum: {
2302 int32_t val = GPBCodedInputStreamReadEnum(state);
2303 if (GPBHasPreservingUnknownEnumSemantics(syntax) ||
2304 [field isValidEnumValue:val]) {
2305 [(GPBEnumArray*)genericArray addRawValue:val];
2307 GPBUnknownFieldSet *unknownFields = GetOrMakeUnknownFields(self);
2308 [unknownFields mergeVarintField:GPBFieldNumber(field) value:val];
2315 - (void)mergeFromCodedInputStream:(GPBCodedInputStream *)input
2316 extensionRegistry:(GPBExtensionRegistry *)extensionRegistry {
2317 GPBDescriptor *descriptor = [self descriptor];
2318 GPBFileSyntax syntax = descriptor.file.syntax;
2319 GPBCodedInputStreamState *state = &input->state_;
2321 NSUInteger startingIndex = 0;
2322 NSArray *fields = descriptor->fields_;
2323 NSUInteger numFields = fields.count;
2326 tag = GPBCodedInputStreamReadTag(state);
2328 break; // Reached end.
2330 for (NSUInteger i = 0; i < numFields; ++i) {
2331 if (startingIndex >= numFields) startingIndex = 0;
2332 GPBFieldDescriptor *fieldDescriptor = fields[startingIndex];
2333 if (GPBFieldTag(fieldDescriptor) == tag) {
2334 GPBFieldType fieldType = fieldDescriptor.fieldType;
2335 if (fieldType == GPBFieldTypeSingle) {
2336 MergeSingleFieldFromCodedInputStream(self, fieldDescriptor, syntax,
2337 input, extensionRegistry);
2338 // Well formed protos will only have a single field once, advance
2339 // the starting index to the next field.
2341 } else if (fieldType == GPBFieldTypeRepeated) {
2342 if (fieldDescriptor.isPackable) {
2343 MergeRepeatedPackedFieldFromCodedInputStream(
2344 self, fieldDescriptor, syntax, input);
2345 // Well formed protos will only have a repeated field that is
2346 // packed once, advance the starting index to the next field.
2349 MergeRepeatedNotPackedFieldFromCodedInputStream(
2350 self, fieldDescriptor, syntax, input, extensionRegistry);
2352 } else { // fieldType == GPBFieldTypeMap
2353 // GPB*Dictionary or NSDictionary, exact type doesn't matter at this
2355 id map = GetOrCreateMapIvarWithField(self, fieldDescriptor, syntax);
2356 [input readMapEntry:map
2357 extensionRegistry:extensionRegistry
2358 field:fieldDescriptor
2359 parentMessage:self];
2366 } // for(i < numFields)
2368 if (!merged && (tag != 0)) {
2369 // Primitive, repeated types can be packed on unpacked on the wire, and
2370 // are parsed either way. The above loop covered tag in the preferred
2371 // for, so this need to check the alternate form.
2372 for (NSUInteger i = 0; i < numFields; ++i) {
2373 if (startingIndex >= numFields) startingIndex = 0;
2374 GPBFieldDescriptor *fieldDescriptor = fields[startingIndex];
2375 if ((fieldDescriptor.fieldType == GPBFieldTypeRepeated) &&
2376 !GPBFieldDataTypeIsObject(fieldDescriptor) &&
2377 (GPBFieldAlternateTag(fieldDescriptor) == tag)) {
2378 BOOL alternateIsPacked = !fieldDescriptor.isPackable;
2379 if (alternateIsPacked) {
2380 MergeRepeatedPackedFieldFromCodedInputStream(
2381 self, fieldDescriptor, syntax, input);
2382 // Well formed protos will only have a repeated field that is
2383 // packed once, advance the starting index to the next field.
2386 MergeRepeatedNotPackedFieldFromCodedInputStream(
2387 self, fieldDescriptor, syntax, input, extensionRegistry);
2399 // zero signals EOF / limit reached
2402 if (![self parseUnknownField:input
2403 extensionRegistry:extensionRegistry
2405 // it's an endgroup tag
2414 #pragma mark - MergeFrom Support
2416 - (void)mergeFrom:(GPBMessage *)other {
2417 Class selfClass = [self class];
2418 Class otherClass = [other class];
2419 if (!([selfClass isSubclassOfClass:otherClass] ||
2420 [otherClass isSubclassOfClass:selfClass])) {
2421 [NSException raise:NSInvalidArgumentException
2422 format:@"Classes must match %@ != %@", selfClass, otherClass];
2425 // We assume something will be done and become visible.
2426 GPBBecomeVisibleToAutocreator(self);
2428 GPBDescriptor *descriptor = [[self class] descriptor];
2429 GPBFileSyntax syntax = descriptor.file.syntax;
2431 for (GPBFieldDescriptor *field in descriptor->fields_) {
2432 GPBFieldType fieldType = field.fieldType;
2433 if (fieldType == GPBFieldTypeSingle) {
2434 int32_t hasIndex = GPBFieldHasIndex(field);
2435 uint32_t fieldNumber = GPBFieldNumber(field);
2436 if (!GPBGetHasIvar(other, hasIndex, fieldNumber)) {
2437 // Other doesn't have the field set, on to the next.
2440 GPBDataType fieldDataType = GPBGetFieldDataType(field);
2441 switch (fieldDataType) {
2442 case GPBDataTypeBool:
2443 GPBSetBoolIvarWithFieldInternal(
2444 self, field, GPBGetMessageBoolField(other, field), syntax);
2446 case GPBDataTypeSFixed32:
2447 case GPBDataTypeEnum:
2448 case GPBDataTypeInt32:
2449 case GPBDataTypeSInt32:
2450 GPBSetInt32IvarWithFieldInternal(
2451 self, field, GPBGetMessageInt32Field(other, field), syntax);
2453 case GPBDataTypeFixed32:
2454 case GPBDataTypeUInt32:
2455 GPBSetUInt32IvarWithFieldInternal(
2456 self, field, GPBGetMessageUInt32Field(other, field), syntax);
2458 case GPBDataTypeSFixed64:
2459 case GPBDataTypeInt64:
2460 case GPBDataTypeSInt64:
2461 GPBSetInt64IvarWithFieldInternal(
2462 self, field, GPBGetMessageInt64Field(other, field), syntax);
2464 case GPBDataTypeFixed64:
2465 case GPBDataTypeUInt64:
2466 GPBSetUInt64IvarWithFieldInternal(
2467 self, field, GPBGetMessageUInt64Field(other, field), syntax);
2469 case GPBDataTypeFloat:
2470 GPBSetFloatIvarWithFieldInternal(
2471 self, field, GPBGetMessageFloatField(other, field), syntax);
2473 case GPBDataTypeDouble:
2474 GPBSetDoubleIvarWithFieldInternal(
2475 self, field, GPBGetMessageDoubleField(other, field), syntax);
2477 case GPBDataTypeBytes:
2478 case GPBDataTypeString: {
2479 id otherVal = GPBGetObjectIvarWithFieldNoAutocreate(other, field);
2480 GPBSetObjectIvarWithFieldInternal(self, field, otherVal, syntax);
2483 case GPBDataTypeMessage:
2484 case GPBDataTypeGroup: {
2485 id otherVal = GPBGetObjectIvarWithFieldNoAutocreate(other, field);
2486 if (GPBGetHasIvar(self, hasIndex, fieldNumber)) {
2487 GPBMessage *message =
2488 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
2489 [message mergeFrom:otherVal];
2491 GPBMessage *message = [otherVal copy];
2492 GPBSetRetainedObjectIvarWithFieldInternal(self, field, message,
2498 } else if (fieldType == GPBFieldTypeRepeated) {
2499 // In the case of a list, they need to be appended, and there is no
2500 // _hasIvar to worry about setting.
2502 GPBGetObjectIvarWithFieldNoAutocreate(other, field);
2504 GPBDataType fieldDataType = field->description_->dataType;
2505 if (GPBDataTypeIsObject(fieldDataType)) {
2506 NSMutableArray *resultArray =
2507 GetOrCreateArrayIvarWithField(self, field, syntax);
2508 [resultArray addObjectsFromArray:otherArray];
2509 } else if (fieldDataType == GPBDataTypeEnum) {
2510 GPBEnumArray *resultArray =
2511 GetOrCreateArrayIvarWithField(self, field, syntax);
2512 [resultArray addRawValuesFromArray:otherArray];
2514 // The array type doesn't matter, that all implment
2515 // -addValuesFromArray:.
2516 GPBInt32Array *resultArray =
2517 GetOrCreateArrayIvarWithField(self, field, syntax);
2518 [resultArray addValuesFromArray:otherArray];
2521 } else { // fieldType = GPBFieldTypeMap
2522 // In the case of a map, they need to be merged, and there is no
2523 // _hasIvar to worry about setting.
2524 id otherDict = GPBGetObjectIvarWithFieldNoAutocreate(other, field);
2526 GPBDataType keyDataType = field.mapKeyDataType;
2527 GPBDataType valueDataType = field->description_->dataType;
2528 if (GPBDataTypeIsObject(keyDataType) &&
2529 GPBDataTypeIsObject(valueDataType)) {
2530 NSMutableDictionary *resultDict =
2531 GetOrCreateMapIvarWithField(self, field, syntax);
2532 [resultDict addEntriesFromDictionary:otherDict];
2533 } else if (valueDataType == GPBDataTypeEnum) {
2534 // The exact type doesn't matter, just need to know it is a
2535 // GPB*EnumDictionary.
2536 GPBInt32EnumDictionary *resultDict =
2537 GetOrCreateMapIvarWithField(self, field, syntax);
2538 [resultDict addRawEntriesFromDictionary:otherDict];
2540 // The exact type doesn't matter, they all implement
2541 // -addEntriesFromDictionary:.
2542 GPBInt32Int32Dictionary *resultDict =
2543 GetOrCreateMapIvarWithField(self, field, syntax);
2544 [resultDict addEntriesFromDictionary:otherDict];
2547 } // if (fieldType)..else if...else
2551 if (!unknownFields_) {
2552 [self setUnknownFields:other.unknownFields];
2554 [unknownFields_ mergeUnknownFields:other.unknownFields];
2559 if (other->extensionMap_.count == 0) {
2563 if (extensionMap_ == nil) {
2565 CloneExtensionMap(other->extensionMap_, NSZoneFromPointer(self));
2567 for (GPBExtensionDescriptor *extension in other->extensionMap_) {
2568 id otherValue = [other->extensionMap_ objectForKey:extension];
2569 id value = [extensionMap_ objectForKey:extension];
2570 BOOL isMessageExtension = GPBExtensionIsMessage(extension);
2572 if (extension.repeated) {
2573 NSMutableArray *list = value;
2575 list = [[NSMutableArray alloc] init];
2576 [extensionMap_ setObject:list forKey:extension];
2579 if (isMessageExtension) {
2580 for (GPBMessage *otherListValue in otherValue) {
2581 GPBMessage *copiedValue = [otherListValue copy];
2582 [list addObject:copiedValue];
2583 [copiedValue release];
2586 [list addObjectsFromArray:otherValue];
2589 if (isMessageExtension) {
2591 [(GPBMessage *)value mergeFrom:(GPBMessage *)otherValue];
2593 GPBMessage *copiedValue = [otherValue copy];
2594 [extensionMap_ setObject:copiedValue forKey:extension];
2595 [copiedValue release];
2598 [extensionMap_ setObject:otherValue forKey:extension];
2602 if (isMessageExtension && !extension.isRepeated) {
2603 GPBMessage *autocreatedValue =
2604 [[autocreatedExtensionMap_ objectForKey:extension] retain];
2605 // Must remove from the map before calling GPBClearMessageAutocreator()
2606 // so that GPBClearMessageAutocreator() knows its safe to clear.
2607 [autocreatedExtensionMap_ removeObjectForKey:extension];
2608 GPBClearMessageAutocreator(autocreatedValue);
2609 [autocreatedValue release];
2615 #pragma mark - isEqual: & hash Support
2617 - (BOOL)isEqual:(id)other {
2618 if (other == self) {
2621 if (![other isKindOfClass:[GPBMessage class]]) {
2624 GPBMessage *otherMsg = other;
2625 GPBDescriptor *descriptor = [[self class] descriptor];
2626 if ([[otherMsg class] descriptor] != descriptor) {
2629 uint8_t *selfStorage = (uint8_t *)messageStorage_;
2630 uint8_t *otherStorage = (uint8_t *)otherMsg->messageStorage_;
2632 for (GPBFieldDescriptor *field in descriptor->fields_) {
2633 if (GPBFieldIsMapOrArray(field)) {
2634 // In the case of a list or map, there is no _hasIvar to worry about.
2635 // NOTE: These are NSArray/GPB*Array or NSDictionary/GPB*Dictionary, but
2636 // the type doesn't really matter as the objects all support -count and
2638 NSArray *resultMapOrArray =
2639 GPBGetObjectIvarWithFieldNoAutocreate(self, field);
2640 NSArray *otherMapOrArray =
2641 GPBGetObjectIvarWithFieldNoAutocreate(other, field);
2642 // nil and empty are equal
2643 if (resultMapOrArray.count != 0 || otherMapOrArray.count != 0) {
2644 if (![resultMapOrArray isEqual:otherMapOrArray]) {
2648 } else { // Single field
2649 int32_t hasIndex = GPBFieldHasIndex(field);
2650 uint32_t fieldNum = GPBFieldNumber(field);
2651 BOOL selfHas = GPBGetHasIvar(self, hasIndex, fieldNum);
2652 BOOL otherHas = GPBGetHasIvar(other, hasIndex, fieldNum);
2653 if (selfHas != otherHas) {
2654 return NO; // Differing has values, not equal.
2657 // Same has values, was no, nothing else to check for this field.
2660 // Now compare the values.
2661 GPBDataType fieldDataType = GPBGetFieldDataType(field);
2662 size_t fieldOffset = field->description_->offset;
2663 switch (fieldDataType) {
2664 case GPBDataTypeBool: {
2665 // Bools are stored in has_bits to avoid needing explicit space in
2666 // the storage structure.
2667 // (the field number passed to the HasIvar helper doesn't really
2668 // matter since the offset is never negative)
2669 BOOL selfValue = GPBGetHasIvar(self, (int32_t)(fieldOffset), 0);
2670 BOOL otherValue = GPBGetHasIvar(other, (int32_t)(fieldOffset), 0);
2671 if (selfValue != otherValue) {
2676 case GPBDataTypeSFixed32:
2677 case GPBDataTypeInt32:
2678 case GPBDataTypeSInt32:
2679 case GPBDataTypeEnum:
2680 case GPBDataTypeFixed32:
2681 case GPBDataTypeUInt32:
2682 case GPBDataTypeFloat: {
2683 GPBInternalCompileAssert(sizeof(float) == sizeof(uint32_t), float_not_32_bits);
2684 // These are all 32bit, signed/unsigned doesn't matter for equality.
2685 uint32_t *selfValPtr = (uint32_t *)&selfStorage[fieldOffset];
2686 uint32_t *otherValPtr = (uint32_t *)&otherStorage[fieldOffset];
2687 if (*selfValPtr != *otherValPtr) {
2692 case GPBDataTypeSFixed64:
2693 case GPBDataTypeInt64:
2694 case GPBDataTypeSInt64:
2695 case GPBDataTypeFixed64:
2696 case GPBDataTypeUInt64:
2697 case GPBDataTypeDouble: {
2698 GPBInternalCompileAssert(sizeof(double) == sizeof(uint64_t), double_not_64_bits);
2699 // These are all 64bit, signed/unsigned doesn't matter for equality.
2700 uint64_t *selfValPtr = (uint64_t *)&selfStorage[fieldOffset];
2701 uint64_t *otherValPtr = (uint64_t *)&otherStorage[fieldOffset];
2702 if (*selfValPtr != *otherValPtr) {
2707 case GPBDataTypeBytes:
2708 case GPBDataTypeString:
2709 case GPBDataTypeMessage:
2710 case GPBDataTypeGroup: {
2711 // Type doesn't matter here, they all implement -isEqual:.
2712 id *selfValPtr = (id *)&selfStorage[fieldOffset];
2713 id *otherValPtr = (id *)&otherStorage[fieldOffset];
2714 if (![*selfValPtr isEqual:*otherValPtr]) {
2720 } // if(mapOrArray)...else
2723 // nil and empty are equal
2724 if (extensionMap_.count != 0 || otherMsg->extensionMap_.count != 0) {
2725 if (![extensionMap_ isEqual:otherMsg->extensionMap_]) {
2730 // nil and empty are equal
2731 GPBUnknownFieldSet *otherUnknowns = otherMsg->unknownFields_;
2732 if ([unknownFields_ countOfFields] != 0 ||
2733 [otherUnknowns countOfFields] != 0) {
2734 if (![unknownFields_ isEqual:otherUnknowns]) {
2742 // It is very difficult to implement a generic hash for ProtoBuf messages that
2743 // will perform well. If you need hashing on your ProtoBufs (eg you are using
2744 // them as dictionary keys) you will probably want to implement a ProtoBuf
2745 // message specific hash as a category on your protobuf class. Do not make it a
2746 // category on GPBMessage as you will conflict with this hash, and will possibly
2747 // override hash for all generated protobufs. A good implementation of hash will
2748 // be really fast, so we would recommend only hashing protobufs that have an
2749 // identifier field of some kind that you can easily hash. If you implement
2750 // hash, we would strongly recommend overriding isEqual: in your category as
2751 // well, as the default implementation of isEqual: is extremely slow, and may
2752 // drastically affect performance in large sets.
2753 - (NSUInteger)hash {
2754 GPBDescriptor *descriptor = [[self class] descriptor];
2755 const NSUInteger prime = 19;
2756 uint8_t *storage = (uint8_t *)messageStorage_;
2758 // Start with the descriptor and then mix it with some instance info.
2759 // Hopefully that will give a spread based on classes and what fields are set.
2760 NSUInteger result = (NSUInteger)descriptor;
2762 for (GPBFieldDescriptor *field in descriptor->fields_) {
2763 if (GPBFieldIsMapOrArray(field)) {
2764 // Exact type doesn't matter, just check if there are any elements.
2765 NSArray *mapOrArray = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
2766 NSUInteger count = mapOrArray.count;
2768 // NSArray/NSDictionary use count, use the field number and the count.
2769 result = prime * result + GPBFieldNumber(field);
2770 result = prime * result + count;
2772 } else if (GPBGetHasIvarField(self, field)) {
2773 // Just using the field number seemed simple/fast, but then a small
2774 // message class where all the same fields are always set (to different
2775 // things would end up all with the same hash, so pull in some data).
2776 GPBDataType fieldDataType = GPBGetFieldDataType(field);
2777 size_t fieldOffset = field->description_->offset;
2778 switch (fieldDataType) {
2779 case GPBDataTypeBool: {
2780 // Bools are stored in has_bits to avoid needing explicit space in
2781 // the storage structure.
2782 // (the field number passed to the HasIvar helper doesn't really
2783 // matter since the offset is never negative)
2784 BOOL value = GPBGetHasIvar(self, (int32_t)(fieldOffset), 0);
2785 result = prime * result + value;
2788 case GPBDataTypeSFixed32:
2789 case GPBDataTypeInt32:
2790 case GPBDataTypeSInt32:
2791 case GPBDataTypeEnum:
2792 case GPBDataTypeFixed32:
2793 case GPBDataTypeUInt32:
2794 case GPBDataTypeFloat: {
2795 GPBInternalCompileAssert(sizeof(float) == sizeof(uint32_t), float_not_32_bits);
2796 // These are all 32bit, just mix it in.
2797 uint32_t *valPtr = (uint32_t *)&storage[fieldOffset];
2798 result = prime * result + *valPtr;
2801 case GPBDataTypeSFixed64:
2802 case GPBDataTypeInt64:
2803 case GPBDataTypeSInt64:
2804 case GPBDataTypeFixed64:
2805 case GPBDataTypeUInt64:
2806 case GPBDataTypeDouble: {
2807 GPBInternalCompileAssert(sizeof(double) == sizeof(uint64_t), double_not_64_bits);
2808 // These are all 64bit, just mix what fits into an NSUInteger in.
2809 uint64_t *valPtr = (uint64_t *)&storage[fieldOffset];
2810 result = prime * result + (NSUInteger)(*valPtr);
2813 case GPBDataTypeBytes:
2814 case GPBDataTypeString: {
2815 // Type doesn't matter here, they both implement -hash:.
2816 id *valPtr = (id *)&storage[fieldOffset];
2817 result = prime * result + [*valPtr hash];
2821 case GPBDataTypeMessage:
2822 case GPBDataTypeGroup: {
2823 GPBMessage **valPtr = (GPBMessage **)&storage[fieldOffset];
2824 // Could call -hash on the sub message, but that could recurse pretty
2825 // deep; follow the lead of NSArray/NSDictionary and don't really
2826 // recurse for hash, instead use the field number and the descriptor
2827 // of the sub message. Yes, this could suck for a bunch of messages
2828 // where they all only differ in the sub messages, but if you are
2829 // using a message with sub messages for something that needs -hash,
2830 // odds are you are also copying them as keys, and that deep copy
2832 result = prime * result + GPBFieldNumber(field);
2833 result = prime * result + (NSUInteger)[[*valPtr class] descriptor];
2840 // Unknowns and extensions are not included.
2845 #pragma mark - Description Support
2847 - (NSString *)description {
2848 NSString *textFormat = GPBTextFormatForMessage(self, @" ");
2849 NSString *description = [NSString
2850 stringWithFormat:@"<%@ %p>: {\n%@}", [self class], self, textFormat];
2854 #if defined(DEBUG) && DEBUG
2856 // Xcode 5.1 added support for custom quick look info.
2857 // https://developer.apple.com/library/ios/documentation/IDEs/Conceptual/CustomClassDisplay_in_QuickLook/CH01-quick_look_for_custom_objects/CH01-quick_look_for_custom_objects.html#//apple_ref/doc/uid/TP40014001-CH2-SW1
2858 - (id)debugQuickLookObject {
2859 return GPBTextFormatForMessage(self, nil);
2864 #pragma mark - SerializedSize
2866 - (size_t)serializedSize {
2867 GPBDescriptor *descriptor = [[self class] descriptor];
2870 // Has check is done explicitly, so GPBGetObjectIvarWithFieldNoAutocreate()
2871 // avoids doing the has check again.
2874 for (GPBFieldDescriptor *fieldDescriptor in descriptor->fields_) {
2875 GPBFieldType fieldType = fieldDescriptor.fieldType;
2876 GPBDataType fieldDataType = GPBGetFieldDataType(fieldDescriptor);
2879 if (fieldType == GPBFieldTypeSingle) {
2880 BOOL selfHas = GPBGetHasIvarField(self, fieldDescriptor);
2882 continue; // Nothing to do.
2885 uint32_t fieldNumber = GPBFieldNumber(fieldDescriptor);
2887 switch (fieldDataType) {
2888 #define CASE_SINGLE_POD(NAME, TYPE, FUNC_TYPE) \
2889 case GPBDataType##NAME: { \
2890 TYPE fieldVal = GPBGetMessage##FUNC_TYPE##Field(self, fieldDescriptor); \
2891 result += GPBCompute##NAME##Size(fieldNumber, fieldVal); \
2894 #define CASE_SINGLE_OBJECT(NAME) \
2895 case GPBDataType##NAME: { \
2896 id fieldVal = GPBGetObjectIvarWithFieldNoAutocreate(self, fieldDescriptor); \
2897 result += GPBCompute##NAME##Size(fieldNumber, fieldVal); \
2900 CASE_SINGLE_POD(Bool, BOOL, Bool)
2901 CASE_SINGLE_POD(Fixed32, uint32_t, UInt32)
2902 CASE_SINGLE_POD(SFixed32, int32_t, Int32)
2903 CASE_SINGLE_POD(Float, float, Float)
2904 CASE_SINGLE_POD(Fixed64, uint64_t, UInt64)
2905 CASE_SINGLE_POD(SFixed64, int64_t, Int64)
2906 CASE_SINGLE_POD(Double, double, Double)
2907 CASE_SINGLE_POD(Int32, int32_t, Int32)
2908 CASE_SINGLE_POD(Int64, int64_t, Int64)
2909 CASE_SINGLE_POD(SInt32, int32_t, Int32)
2910 CASE_SINGLE_POD(SInt64, int64_t, Int64)
2911 CASE_SINGLE_POD(UInt32, uint32_t, UInt32)
2912 CASE_SINGLE_POD(UInt64, uint64_t, UInt64)
2913 CASE_SINGLE_OBJECT(Bytes)
2914 CASE_SINGLE_OBJECT(String)
2915 CASE_SINGLE_OBJECT(Message)
2916 CASE_SINGLE_OBJECT(Group)
2917 CASE_SINGLE_POD(Enum, int32_t, Int32)
2918 #undef CASE_SINGLE_POD
2919 #undef CASE_SINGLE_OBJECT
2923 } else if (fieldType == GPBFieldTypeRepeated) {
2925 GPBGetObjectIvarWithFieldNoAutocreate(self, fieldDescriptor);
2926 NSUInteger count = [genericArray count];
2928 continue; // Nothing to add.
2930 __block size_t dataSize = 0;
2932 switch (fieldDataType) {
2933 #define CASE_REPEATED_POD(NAME, TYPE, ARRAY_TYPE) \
2934 CASE_REPEATED_POD_EXTRA(NAME, TYPE, ARRAY_TYPE, )
2935 #define CASE_REPEATED_POD_EXTRA(NAME, TYPE, ARRAY_TYPE, ARRAY_ACCESSOR_NAME) \
2936 case GPBDataType##NAME: { \
2937 GPB##ARRAY_TYPE##Array *array = genericArray; \
2938 [array enumerate##ARRAY_ACCESSOR_NAME##ValuesWithBlock:^(TYPE value, NSUInteger idx, BOOL *stop) { \
2939 _Pragma("unused(idx, stop)"); \
2940 dataSize += GPBCompute##NAME##SizeNoTag(value); \
2944 #define CASE_REPEATED_OBJECT(NAME) \
2945 case GPBDataType##NAME: { \
2946 for (id value in genericArray) { \
2947 dataSize += GPBCompute##NAME##SizeNoTag(value); \
2951 CASE_REPEATED_POD(Bool, BOOL, Bool)
2952 CASE_REPEATED_POD(Fixed32, uint32_t, UInt32)
2953 CASE_REPEATED_POD(SFixed32, int32_t, Int32)
2954 CASE_REPEATED_POD(Float, float, Float)
2955 CASE_REPEATED_POD(Fixed64, uint64_t, UInt64)
2956 CASE_REPEATED_POD(SFixed64, int64_t, Int64)
2957 CASE_REPEATED_POD(Double, double, Double)
2958 CASE_REPEATED_POD(Int32, int32_t, Int32)
2959 CASE_REPEATED_POD(Int64, int64_t, Int64)
2960 CASE_REPEATED_POD(SInt32, int32_t, Int32)
2961 CASE_REPEATED_POD(SInt64, int64_t, Int64)
2962 CASE_REPEATED_POD(UInt32, uint32_t, UInt32)
2963 CASE_REPEATED_POD(UInt64, uint64_t, UInt64)
2964 CASE_REPEATED_OBJECT(Bytes)
2965 CASE_REPEATED_OBJECT(String)
2966 CASE_REPEATED_OBJECT(Message)
2967 CASE_REPEATED_OBJECT(Group)
2968 CASE_REPEATED_POD_EXTRA(Enum, int32_t, Enum, Raw)
2969 #undef CASE_REPEATED_POD
2970 #undef CASE_REPEATED_POD_EXTRA
2971 #undef CASE_REPEATED_OBJECT
2974 size_t tagSize = GPBComputeTagSize(GPBFieldNumber(fieldDescriptor));
2975 if (fieldDataType == GPBDataTypeGroup) {
2976 // Groups have both a start and an end tag.
2979 if (fieldDescriptor.isPackable) {
2981 result += GPBComputeSizeTSizeAsInt32NoTag(dataSize);
2983 result += count * tagSize;
2987 } else { // fieldType == GPBFieldTypeMap
2988 if (GPBDataTypeIsObject(fieldDataType) &&
2989 (fieldDescriptor.mapKeyDataType == GPBDataTypeString)) {
2990 // If key type was string, then the map is an NSDictionary.
2992 GPBGetObjectIvarWithFieldNoAutocreate(self, fieldDescriptor);
2994 result += GPBDictionaryComputeSizeInternalHelper(map, fieldDescriptor);
2997 // Type will be GPB*GroupDictionary, exact type doesn't matter.
2998 GPBInt32Int32Dictionary *map =
2999 GPBGetObjectIvarWithFieldNoAutocreate(self, fieldDescriptor);
3000 result += [map computeSerializedSizeAsField:fieldDescriptor];
3005 // Add any unknown fields.
3006 if (descriptor.wireFormat) {
3007 result += [unknownFields_ serializedSizeAsMessageSet];
3009 result += [unknownFields_ serializedSize];
3012 // Add any extensions.
3013 for (GPBExtensionDescriptor *extension in extensionMap_) {
3014 id value = [extensionMap_ objectForKey:extension];
3015 result += GPBComputeExtensionSerializedSizeIncludingTag(extension, value);
3021 #pragma mark - Resolve Methods Support
3023 typedef struct ResolveIvarAccessorMethodResult {
3025 SEL encodingSelector;
3026 } ResolveIvarAccessorMethodResult;
3028 // |field| can be __unsafe_unretained because they are created at startup
3029 // and are essentially global. No need to pay for retain/release when
3030 // they are captured in blocks.
3031 static void ResolveIvarGet(__unsafe_unretained GPBFieldDescriptor *field,
3032 ResolveIvarAccessorMethodResult *result) {
3033 GPBDataType fieldDataType = GPBGetFieldDataType(field);
3034 switch (fieldDataType) {
3035 #define CASE_GET(NAME, TYPE, TRUE_NAME) \
3036 case GPBDataType##NAME: { \
3037 result->impToAdd = imp_implementationWithBlock(^(id obj) { \
3038 return GPBGetMessage##TRUE_NAME##Field(obj, field); \
3040 result->encodingSelector = @selector(get##NAME); \
3043 #define CASE_GET_OBJECT(NAME, TYPE, TRUE_NAME) \
3044 case GPBDataType##NAME: { \
3045 result->impToAdd = imp_implementationWithBlock(^(id obj) { \
3046 return GPBGetObjectIvarWithField(obj, field); \
3048 result->encodingSelector = @selector(get##NAME); \
3051 CASE_GET(Bool, BOOL, Bool)
3052 CASE_GET(Fixed32, uint32_t, UInt32)
3053 CASE_GET(SFixed32, int32_t, Int32)
3054 CASE_GET(Float, float, Float)
3055 CASE_GET(Fixed64, uint64_t, UInt64)
3056 CASE_GET(SFixed64, int64_t, Int64)
3057 CASE_GET(Double, double, Double)
3058 CASE_GET(Int32, int32_t, Int32)
3059 CASE_GET(Int64, int64_t, Int64)
3060 CASE_GET(SInt32, int32_t, Int32)
3061 CASE_GET(SInt64, int64_t, Int64)
3062 CASE_GET(UInt32, uint32_t, UInt32)
3063 CASE_GET(UInt64, uint64_t, UInt64)
3064 CASE_GET_OBJECT(Bytes, id, Object)
3065 CASE_GET_OBJECT(String, id, Object)
3066 CASE_GET_OBJECT(Message, id, Object)
3067 CASE_GET_OBJECT(Group, id, Object)
3068 CASE_GET(Enum, int32_t, Enum)
3073 // See comment about __unsafe_unretained on ResolveIvarGet.
3074 static void ResolveIvarSet(__unsafe_unretained GPBFieldDescriptor *field,
3075 GPBFileSyntax syntax,
3076 ResolveIvarAccessorMethodResult *result) {
3077 GPBDataType fieldDataType = GPBGetFieldDataType(field);
3078 switch (fieldDataType) {
3079 #define CASE_SET(NAME, TYPE, TRUE_NAME) \
3080 case GPBDataType##NAME: { \
3081 result->impToAdd = imp_implementationWithBlock(^(id obj, TYPE value) { \
3082 return GPBSet##TRUE_NAME##IvarWithFieldInternal(obj, field, value, syntax); \
3084 result->encodingSelector = @selector(set##NAME:); \
3087 CASE_SET(Bool, BOOL, Bool)
3088 CASE_SET(Fixed32, uint32_t, UInt32)
3089 CASE_SET(SFixed32, int32_t, Int32)
3090 CASE_SET(Float, float, Float)
3091 CASE_SET(Fixed64, uint64_t, UInt64)
3092 CASE_SET(SFixed64, int64_t, Int64)
3093 CASE_SET(Double, double, Double)
3094 CASE_SET(Int32, int32_t, Int32)
3095 CASE_SET(Int64, int64_t, Int64)
3096 CASE_SET(SInt32, int32_t, Int32)
3097 CASE_SET(SInt64, int64_t, Int64)
3098 CASE_SET(UInt32, uint32_t, UInt32)
3099 CASE_SET(UInt64, uint64_t, UInt64)
3100 CASE_SET(Bytes, id, Object)
3101 CASE_SET(String, id, Object)
3102 CASE_SET(Message, id, Object)
3103 CASE_SET(Group, id, Object)
3104 CASE_SET(Enum, int32_t, Enum)
3109 + (BOOL)resolveInstanceMethod:(SEL)sel {
3110 const GPBDescriptor *descriptor = [self descriptor];
3112 return [super resolveInstanceMethod:sel];
3115 // NOTE: hasOrCountSel_/setHasSel_ will be NULL if the field for the given
3116 // message should not have has support (done in GPBDescriptor.m), so there is
3117 // no need for checks here to see if has*/setHas* are allowed.
3118 ResolveIvarAccessorMethodResult result = {NULL, NULL};
3120 // See comment about __unsafe_unretained on ResolveIvarGet.
3121 for (__unsafe_unretained GPBFieldDescriptor *field in descriptor->fields_) {
3122 BOOL isMapOrArray = GPBFieldIsMapOrArray(field);
3123 if (!isMapOrArray) {
3125 if (sel == field->getSel_) {
3126 ResolveIvarGet(field, &result);
3128 } else if (sel == field->setSel_) {
3129 ResolveIvarSet(field, descriptor.file.syntax, &result);
3131 } else if (sel == field->hasOrCountSel_) {
3132 int32_t index = GPBFieldHasIndex(field);
3133 uint32_t fieldNum = GPBFieldNumber(field);
3134 result.impToAdd = imp_implementationWithBlock(^(id obj) {
3135 return GPBGetHasIvar(obj, index, fieldNum);
3137 result.encodingSelector = @selector(getBool);
3139 } else if (sel == field->setHasSel_) {
3140 result.impToAdd = imp_implementationWithBlock(^(id obj, BOOL value) {
3142 [NSException raise:NSInvalidArgumentException
3143 format:@"%@: %@ can only be set to NO (to clear field).",
3145 NSStringFromSelector(field->setHasSel_)];
3147 GPBClearMessageField(obj, field);
3149 result.encodingSelector = @selector(setBool:);
3152 GPBOneofDescriptor *oneof = field->containingOneof_;
3153 if (oneof && (sel == oneof->caseSel_)) {
3154 int32_t index = GPBFieldHasIndex(field);
3155 result.impToAdd = imp_implementationWithBlock(^(id obj) {
3156 return GPBGetHasOneof(obj, index);
3158 result.encodingSelector = @selector(getEnum);
3163 // map<>/repeated fields.
3164 if (sel == field->getSel_) {
3165 if (field.fieldType == GPBFieldTypeRepeated) {
3166 result.impToAdd = imp_implementationWithBlock(^(id obj) {
3167 return GetArrayIvarWithField(obj, field);
3170 result.impToAdd = imp_implementationWithBlock(^(id obj) {
3171 return GetMapIvarWithField(obj, field);
3174 result.encodingSelector = @selector(getArray);
3176 } else if (sel == field->setSel_) {
3177 // Local for syntax so the block can directly capture it and not the
3179 const GPBFileSyntax syntax = descriptor.file.syntax;
3180 result.impToAdd = imp_implementationWithBlock(^(id obj, id value) {
3181 return GPBSetObjectIvarWithFieldInternal(obj, field, value, syntax);
3183 result.encodingSelector = @selector(setArray:);
3185 } else if (sel == field->hasOrCountSel_) {
3186 result.impToAdd = imp_implementationWithBlock(^(id obj) {
3187 // Type doesn't matter, all *Array and *Dictionary types support
3189 NSArray *arrayOrMap =
3190 GPBGetObjectIvarWithFieldNoAutocreate(obj, field);
3191 return [arrayOrMap count];
3193 result.encodingSelector = @selector(getArrayCount);
3198 if (result.impToAdd) {
3199 const char *encoding =
3200 GPBMessageEncodingForSelector(result.encodingSelector, YES);
3201 Class msgClass = descriptor.messageClass;
3202 BOOL methodAdded = class_addMethod(msgClass, sel, result.impToAdd, encoding);
3203 // class_addMethod() is documented as also failing if the method was already
3204 // added; so we check if the method is already there and return success so
3205 // the method dispatch will still happen. Why would it already be added?
3206 // Two threads could cause the same method to be bound at the same time,
3207 // but only one will actually bind it; the other still needs to return true
3208 // so things will dispatch.
3210 methodAdded = GPBClassHasSel(msgClass, sel);
3214 return [super resolveInstanceMethod:sel];
3217 + (BOOL)resolveClassMethod:(SEL)sel {
3218 // Extensions scoped to a Message and looked up via class methods.
3219 if (GPBResolveExtensionClassMethod([self descriptor].messageClass, sel)) {
3222 return [super resolveClassMethod:sel];
3225 #pragma mark - NSCoding Support
3227 + (BOOL)supportsSecureCoding {
3231 - (instancetype)initWithCoder:(NSCoder *)aDecoder {
3235 [aDecoder decodeObjectOfClass:[NSData class] forKey:kGPBDataCoderKey];
3237 [self mergeFromData:data extensionRegistry:nil];
3243 - (void)encodeWithCoder:(NSCoder *)aCoder {
3244 NSData *data = [self data];
3246 [aCoder encodeObject:data forKey:kGPBDataCoderKey];
3250 #pragma mark - KVC Support
3252 + (BOOL)accessInstanceVariablesDirectly {
3253 // Make sure KVC doesn't use instance variables.
3259 #pragma mark - Messages from GPBUtilities.h but defined here for access to helpers.
3261 // Only exists for public api, no core code should use this.
3262 id GPBGetMessageRepeatedField(GPBMessage *self, GPBFieldDescriptor *field) {
3263 #if defined(DEBUG) && DEBUG
3264 if (field.fieldType != GPBFieldTypeRepeated) {
3265 [NSException raise:NSInvalidArgumentException
3266 format:@"%@.%@ is not a repeated field.",
3267 [self class], field.name];
3270 GPBDescriptor *descriptor = [[self class] descriptor];
3271 GPBFileSyntax syntax = descriptor.file.syntax;
3272 return GetOrCreateArrayIvarWithField(self, field, syntax);
3275 // Only exists for public api, no core code should use this.
3276 id GPBGetMessageMapField(GPBMessage *self, GPBFieldDescriptor *field) {
3277 #if defined(DEBUG) && DEBUG
3278 if (field.fieldType != GPBFieldTypeMap) {
3279 [NSException raise:NSInvalidArgumentException
3280 format:@"%@.%@ is not a map<> field.",
3281 [self class], field.name];
3284 GPBDescriptor *descriptor = [[self class] descriptor];
3285 GPBFileSyntax syntax = descriptor.file.syntax;
3286 return GetOrCreateMapIvarWithField(self, field, syntax);
3289 id GPBGetObjectIvarWithField(GPBMessage *self, GPBFieldDescriptor *field) {
3290 NSCAssert(!GPBFieldIsMapOrArray(field), @"Shouldn't get here");
3291 if (GPBGetHasIvarField(self, field)) {
3292 uint8_t *storage = (uint8_t *)self->messageStorage_;
3293 id *typePtr = (id *)&storage[field->description_->offset];
3298 // Non messages (string/data), get their default.
3299 if (!GPBFieldDataTypeIsMessage(field)) {
3300 return field.defaultValue.valueMessage;
3303 GPBPrepareReadOnlySemaphore(self);
3304 dispatch_semaphore_wait(self->readOnlySemaphore_, DISPATCH_TIME_FOREVER);
3305 GPBMessage *result = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
3307 // For non repeated messages, create the object, set it and return it.
3308 // This object will not initially be visible via GPBGetHasIvar, so
3309 // we save its creator so it can become visible if it's mutated later.
3310 result = GPBCreateMessageWithAutocreator(field.msgClass, self, field);
3311 GPBSetAutocreatedRetainedObjectIvarWithField(self, field, result);
3313 dispatch_semaphore_signal(self->readOnlySemaphore_);
3317 #pragma clang diagnostic pop