forked from protocolbuffers/protobuf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GPBUtilities.m
2280 lines (2081 loc) · 86.9 KB
/
GPBUtilities.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#import "GPBUtilities_PackagePrivate.h"
#import <objc/runtime.h>
#import "GPBArray_PackagePrivate.h"
#import "GPBDescriptor_PackagePrivate.h"
#import "GPBDictionary_PackagePrivate.h"
#import "GPBMessage_PackagePrivate.h"
#import "GPBUnknownField.h"
#import "GPBUnknownFieldSet.h"
// Direct access is use for speed, to avoid even internally declaring things
// read/write, etc. The warning is enabled in the project to ensure code calling
// protos can turn on -Wdirect-ivar-access without issues.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdirect-ivar-access"
static void AppendTextFormatForMessage(GPBMessage *message,
NSMutableString *toStr,
NSString *lineIndent);
// Are two datatypes the same basic type representation (ex Int32 and SInt32).
// Marked unused because currently only called from asserts/debug.
static BOOL DataTypesEquivalent(GPBDataType type1,
GPBDataType type2) __attribute__ ((unused));
// Basic type representation for a type (ex: for SInt32 it is Int32).
// Marked unused because currently only called from asserts/debug.
static GPBDataType BaseDataType(GPBDataType type) __attribute__ ((unused));
// String name for a data type.
// Marked unused because currently only called from asserts/debug.
static NSString *TypeToString(GPBDataType dataType) __attribute__ ((unused));
// Helper for clearing oneofs.
static void GPBMaybeClearOneofPrivate(GPBMessage *self,
GPBOneofDescriptor *oneof,
int32_t oneofHasIndex,
uint32_t fieldNumberNotToClear);
NSData *GPBEmptyNSData(void) {
static dispatch_once_t onceToken;
static NSData *defaultNSData = nil;
dispatch_once(&onceToken, ^{
defaultNSData = [[NSData alloc] init];
});
return defaultNSData;
}
void GPBMessageDropUnknownFieldsRecursively(GPBMessage *initialMessage) {
if (!initialMessage) {
return;
}
// Use an array as a list to process to avoid recursion.
NSMutableArray *todo = [NSMutableArray arrayWithObject:initialMessage];
while (todo.count) {
GPBMessage *msg = todo.lastObject;
[todo removeLastObject];
// Clear unknowns.
msg.unknownFields = nil;
// Handle the message fields.
GPBDescriptor *descriptor = [[msg class] descriptor];
for (GPBFieldDescriptor *field in descriptor->fields_) {
if (!GPBFieldDataTypeIsMessage(field)) {
continue;
}
switch (field.fieldType) {
case GPBFieldTypeSingle:
if (GPBGetHasIvarField(msg, field)) {
GPBMessage *fieldMessage = GPBGetObjectIvarWithFieldNoAutocreate(msg, field);
[todo addObject:fieldMessage];
}
break;
case GPBFieldTypeRepeated: {
NSArray *fieldMessages = GPBGetObjectIvarWithFieldNoAutocreate(msg, field);
if (fieldMessages.count) {
[todo addObjectsFromArray:fieldMessages];
}
break;
}
case GPBFieldTypeMap: {
id rawFieldMap = GPBGetObjectIvarWithFieldNoAutocreate(msg, field);
switch (field.mapKeyDataType) {
case GPBDataTypeBool:
[(GPBBoolObjectDictionary*)rawFieldMap enumerateKeysAndObjectsUsingBlock:^(
BOOL key, id _Nonnull object, BOOL * _Nonnull stop) {
#pragma unused(key, stop)
[todo addObject:object];
}];
break;
case GPBDataTypeFixed32:
case GPBDataTypeUInt32:
[(GPBUInt32ObjectDictionary*)rawFieldMap enumerateKeysAndObjectsUsingBlock:^(
uint32_t key, id _Nonnull object, BOOL * _Nonnull stop) {
#pragma unused(key, stop)
[todo addObject:object];
}];
break;
case GPBDataTypeInt32:
case GPBDataTypeSFixed32:
case GPBDataTypeSInt32:
[(GPBInt32ObjectDictionary*)rawFieldMap enumerateKeysAndObjectsUsingBlock:^(
int32_t key, id _Nonnull object, BOOL * _Nonnull stop) {
#pragma unused(key, stop)
[todo addObject:object];
}];
break;
case GPBDataTypeFixed64:
case GPBDataTypeUInt64:
[(GPBUInt64ObjectDictionary*)rawFieldMap enumerateKeysAndObjectsUsingBlock:^(
uint64_t key, id _Nonnull object, BOOL * _Nonnull stop) {
#pragma unused(key, stop)
[todo addObject:object];
}];
break;
case GPBDataTypeInt64:
case GPBDataTypeSFixed64:
case GPBDataTypeSInt64:
[(GPBInt64ObjectDictionary*)rawFieldMap enumerateKeysAndObjectsUsingBlock:^(
int64_t key, id _Nonnull object, BOOL * _Nonnull stop) {
#pragma unused(key, stop)
[todo addObject:object];
}];
break;
case GPBDataTypeString:
[(NSDictionary*)rawFieldMap enumerateKeysAndObjectsUsingBlock:^(
NSString * _Nonnull key, GPBMessage * _Nonnull obj, BOOL * _Nonnull stop) {
#pragma unused(key, stop)
[todo addObject:obj];
}];
break;
case GPBDataTypeFloat:
case GPBDataTypeDouble:
case GPBDataTypeEnum:
case GPBDataTypeBytes:
case GPBDataTypeGroup:
case GPBDataTypeMessage:
NSCAssert(NO, @"Aren't valid key types.");
}
break;
} // switch(field.mapKeyDataType)
} // switch(field.fieldType)
} // for(fields)
// Handle any extensions holding messages.
for (GPBExtensionDescriptor *extension in [msg extensionsCurrentlySet]) {
if (!GPBDataTypeIsMessage(extension.dataType)) {
continue;
}
if (extension.isRepeated) {
NSArray *extMessages = [msg getExtension:extension];
[todo addObjectsFromArray:extMessages];
} else {
GPBMessage *extMessage = [msg getExtension:extension];
[todo addObject:extMessage];
}
} // for(extensionsCurrentlySet)
} // while(todo.count)
}
// -- About Version Checks --
// There's actually 3 places these checks all come into play:
// 1. When the generated source is compile into .o files, the header check
// happens. This is checking the protoc used matches the library being used
// when making the .o.
// 2. Every place a generated proto header is included in a developer's code,
// the header check comes into play again. But this time it is checking that
// the current library headers being used still support/match the ones for
// the generated code.
// 3. At runtime the final check here (GPBCheckRuntimeVersionsInternal), is
// called from the generated code passing in values captured when the
// generated code's .o was made. This checks that at runtime the generated
// code and runtime library match.
void GPBCheckRuntimeVersionSupport(int32_t objcRuntimeVersion) {
// NOTE: This is passing the value captured in the compiled code to check
// against the values captured when the runtime support was compiled. This
// ensures the library code isn't in a different framework/library that
// was generated with a non matching version.
if (GOOGLE_PROTOBUF_OBJC_VERSION < objcRuntimeVersion) {
// Library is too old for headers.
[NSException raise:NSInternalInconsistencyException
format:@"Linked to ProtocolBuffer runtime version %d,"
@" but code compiled needing at least %d!",
GOOGLE_PROTOBUF_OBJC_VERSION, objcRuntimeVersion];
}
if (objcRuntimeVersion < GOOGLE_PROTOBUF_OBJC_MIN_SUPPORTED_VERSION) {
// Headers are too old for library.
[NSException raise:NSInternalInconsistencyException
format:@"Proto generation source compiled against runtime"
@" version %d, but this version of the runtime only"
@" supports back to %d!",
objcRuntimeVersion,
GOOGLE_PROTOBUF_OBJC_MIN_SUPPORTED_VERSION];
}
}
// This api is no longer used for version checks. 30001 is the last version
// using this old versioning model. When that support is removed, this function
// can be removed (along with the declaration in GPBUtilities_PackagePrivate.h).
void GPBCheckRuntimeVersionInternal(int32_t version) {
GPBInternalCompileAssert(GOOGLE_PROTOBUF_OBJC_MIN_SUPPORTED_VERSION == 30001,
time_to_remove_this_old_version_shim);
if (version != GOOGLE_PROTOBUF_OBJC_MIN_SUPPORTED_VERSION) {
[NSException raise:NSInternalInconsistencyException
format:@"Linked to ProtocolBuffer runtime version %d,"
@" but code compiled with version %d!",
GOOGLE_PROTOBUF_OBJC_GEN_VERSION, version];
}
}
BOOL GPBMessageHasFieldNumberSet(GPBMessage *self, uint32_t fieldNumber) {
GPBDescriptor *descriptor = [self descriptor];
GPBFieldDescriptor *field = [descriptor fieldWithNumber:fieldNumber];
return GPBMessageHasFieldSet(self, field);
}
BOOL GPBMessageHasFieldSet(GPBMessage *self, GPBFieldDescriptor *field) {
if (self == nil || field == nil) return NO;
// Repeated/Map don't use the bit, they check the count.
if (GPBFieldIsMapOrArray(field)) {
// Array/map type doesn't matter, since GPB*Array/NSArray and
// GPB*Dictionary/NSDictionary all support -count;
NSArray *arrayOrMap = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
return (arrayOrMap.count > 0);
} else {
return GPBGetHasIvarField(self, field);
}
}
void GPBClearMessageField(GPBMessage *self, GPBFieldDescriptor *field) {
// If not set, nothing to do.
if (!GPBGetHasIvarField(self, field)) {
return;
}
GPBMessageFieldDescription *fieldDesc = field->description_;
if (GPBFieldStoresObject(field)) {
// Object types are handled slightly differently, they need to be released.
uint8_t *storage = (uint8_t *)self->messageStorage_;
id *typePtr = (id *)&storage[fieldDesc->offset];
[*typePtr release];
*typePtr = nil;
} else {
// POD types just need to clear the has bit as the Get* method will
// fetch the default when needed.
}
GPBSetHasIvar(self, fieldDesc->hasIndex, fieldDesc->number, NO);
}
void GPBClearOneof(GPBMessage *self, GPBOneofDescriptor *oneof) {
#if defined(DEBUG) && DEBUG
NSCAssert([[self descriptor] oneofWithName:oneof.name] == oneof,
@"OneofDescriptor %@ doesn't appear to be for %@ messages.",
oneof.name, [self class]);
#endif
GPBFieldDescriptor *firstField = oneof->fields_[0];
GPBMaybeClearOneofPrivate(self, oneof, firstField->description_->hasIndex, 0);
}
BOOL GPBGetHasIvar(GPBMessage *self, int32_t idx, uint32_t fieldNumber) {
NSCAssert(self->messageStorage_ != NULL,
@"%@: All messages should have storage (from init)",
[self class]);
if (idx < 0) {
NSCAssert(fieldNumber != 0, @"Invalid field number.");
BOOL hasIvar = (self->messageStorage_->_has_storage_[-idx] == fieldNumber);
return hasIvar;
} else {
NSCAssert(idx != GPBNoHasBit, @"Invalid has bit.");
uint32_t byteIndex = idx / 32;
uint32_t bitMask = (1U << (idx % 32));
BOOL hasIvar =
(self->messageStorage_->_has_storage_[byteIndex] & bitMask) ? YES : NO;
return hasIvar;
}
}
uint32_t GPBGetHasOneof(GPBMessage *self, int32_t idx) {
NSCAssert(idx < 0, @"%@: invalid index (%d) for oneof.",
[self class], idx);
uint32_t result = self->messageStorage_->_has_storage_[-idx];
return result;
}
void GPBSetHasIvar(GPBMessage *self, int32_t idx, uint32_t fieldNumber,
BOOL value) {
if (idx < 0) {
NSCAssert(fieldNumber != 0, @"Invalid field number.");
uint32_t *has_storage = self->messageStorage_->_has_storage_;
has_storage[-idx] = (value ? fieldNumber : 0);
} else {
NSCAssert(idx != GPBNoHasBit, @"Invalid has bit.");
uint32_t *has_storage = self->messageStorage_->_has_storage_;
uint32_t byte = idx / 32;
uint32_t bitMask = (1U << (idx % 32));
if (value) {
has_storage[byte] |= bitMask;
} else {
has_storage[byte] &= ~bitMask;
}
}
}
static void GPBMaybeClearOneofPrivate(GPBMessage *self,
GPBOneofDescriptor *oneof,
int32_t oneofHasIndex,
uint32_t fieldNumberNotToClear) {
uint32_t fieldNumberSet = GPBGetHasOneof(self, oneofHasIndex);
if ((fieldNumberSet == fieldNumberNotToClear) || (fieldNumberSet == 0)) {
// Do nothing/nothing set in the oneof.
return;
}
// Like GPBClearMessageField(), free the memory if an objecttype is set,
// pod types don't need to do anything.
GPBFieldDescriptor *fieldSet = [oneof fieldWithNumber:fieldNumberSet];
NSCAssert(fieldSet,
@"%@: oneof set to something (%u) not in the oneof?",
[self class], fieldNumberSet);
if (fieldSet && GPBFieldStoresObject(fieldSet)) {
uint8_t *storage = (uint8_t *)self->messageStorage_;
id *typePtr = (id *)&storage[fieldSet->description_->offset];
[*typePtr release];
*typePtr = nil;
}
// Set to nothing stored in the oneof.
// (field number doesn't matter since setting to nothing).
GPBSetHasIvar(self, oneofHasIndex, 1, NO);
}
#pragma mark - IVar accessors
//%PDDM-DEFINE IVAR_POD_ACCESSORS_DEFN(NAME, TYPE)
//%TYPE GPBGetMessage##NAME##Field(GPBMessage *self,
//% TYPE$S NAME$S GPBFieldDescriptor *field) {
//%#if defined(DEBUG) && DEBUG
//% NSCAssert([[self descriptor] fieldWithNumber:field.number] == field,
//% @"FieldDescriptor %@ doesn't appear to be for %@ messages.",
//% field.name, [self class]);
//% NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
//% GPBDataType##NAME),
//% @"Attempting to get value of TYPE from field %@ "
//% @"of %@ which is of type %@.",
//% [self class], field.name,
//% TypeToString(GPBGetFieldDataType(field)));
//%#endif
//% if (GPBGetHasIvarField(self, field)) {
//% uint8_t *storage = (uint8_t *)self->messageStorage_;
//% TYPE *typePtr = (TYPE *)&storage[field->description_->offset];
//% return *typePtr;
//% } else {
//% return field.defaultValue.value##NAME;
//% }
//%}
//%
//%// Only exists for public api, no core code should use this.
//%void GPBSetMessage##NAME##Field(GPBMessage *self,
//% NAME$S GPBFieldDescriptor *field,
//% NAME$S TYPE value) {
//% if (self == nil || field == nil) return;
//%#if defined(DEBUG) && DEBUG
//% NSCAssert([[self descriptor] fieldWithNumber:field.number] == field,
//% @"FieldDescriptor %@ doesn't appear to be for %@ messages.",
//% field.name, [self class]);
//% NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
//% GPBDataType##NAME),
//% @"Attempting to set field %@ of %@ which is of type %@ with "
//% @"value of type TYPE.",
//% [self class], field.name,
//% TypeToString(GPBGetFieldDataType(field)));
//%#endif
//% GPBSet##NAME##IvarWithFieldPrivate(self, field, value);
//%}
//%
//%void GPBSet##NAME##IvarWithFieldPrivate(GPBMessage *self,
//% NAME$S GPBFieldDescriptor *field,
//% NAME$S TYPE value) {
//% GPBOneofDescriptor *oneof = field->containingOneof_;
//% GPBMessageFieldDescription *fieldDesc = field->description_;
//% if (oneof) {
//% GPBMaybeClearOneofPrivate(self, oneof, fieldDesc->hasIndex, fieldDesc->number);
//% }
//%#if defined(DEBUG) && DEBUG
//% NSCAssert(self->messageStorage_ != NULL,
//% @"%@: All messages should have storage (from init)",
//% [self class]);
//%#endif
//%#if defined(__clang_analyzer__)
//% if (self->messageStorage_ == NULL) return;
//%#endif
//% uint8_t *storage = (uint8_t *)self->messageStorage_;
//% TYPE *typePtr = (TYPE *)&storage[fieldDesc->offset];
//% *typePtr = value;
//% // If the value is zero, then we only count the field as "set" if the field
//% // shouldn't auto clear on zero.
//% BOOL hasValue = ((value != (TYPE)0)
//% || ((fieldDesc->flags & GPBFieldClearHasIvarOnZero) == 0));
//% GPBSetHasIvar(self, fieldDesc->hasIndex, fieldDesc->number, hasValue);
//% GPBBecomeVisibleToAutocreator(self);
//%}
//%
//%PDDM-DEFINE IVAR_ALIAS_DEFN_OBJECT(NAME, TYPE)
//%// Only exists for public api, no core code should use this.
//%TYPE *GPBGetMessage##NAME##Field(GPBMessage *self,
//% TYPE$S NAME$S GPBFieldDescriptor *field) {
//%#if defined(DEBUG) && DEBUG
//% NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
//% GPBDataType##NAME),
//% @"Attempting to get value of TYPE from field %@ "
//% @"of %@ which is of type %@.",
//% [self class], field.name,
//% TypeToString(GPBGetFieldDataType(field)));
//%#endif
//% return (TYPE *)GPBGetObjectIvarWithField(self, field);
//%}
//%
//%// Only exists for public api, no core code should use this.
//%void GPBSetMessage##NAME##Field(GPBMessage *self,
//% NAME$S GPBFieldDescriptor *field,
//% NAME$S TYPE *value) {
//%#if defined(DEBUG) && DEBUG
//% NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
//% GPBDataType##NAME),
//% @"Attempting to set field %@ of %@ which is of type %@ with "
//% @"value of type TYPE.",
//% [self class], field.name,
//% TypeToString(GPBGetFieldDataType(field)));
//%#endif
//% GPBSetObjectIvarWithField(self, field, (id)value);
//%}
//%
//%PDDM-DEFINE IVAR_ALIAS_DEFN_COPY_OBJECT(NAME, TYPE)
//%// Only exists for public api, no core code should use this.
//%TYPE *GPBGetMessage##NAME##Field(GPBMessage *self,
//% TYPE$S NAME$S GPBFieldDescriptor *field) {
//%#if defined(DEBUG) && DEBUG
//% NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
//% GPBDataType##NAME),
//% @"Attempting to get value of TYPE from field %@ "
//% @"of %@ which is of type %@.",
//% [self class], field.name,
//% TypeToString(GPBGetFieldDataType(field)));
//%#endif
//% return (TYPE *)GPBGetObjectIvarWithField(self, field);
//%}
//%
//%// Only exists for public api, no core code should use this.
//%void GPBSetMessage##NAME##Field(GPBMessage *self,
//% NAME$S GPBFieldDescriptor *field,
//% NAME$S TYPE *value) {
//%#if defined(DEBUG) && DEBUG
//% NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
//% GPBDataType##NAME),
//% @"Attempting to set field %@ of %@ which is of type %@ with "
//% @"value of type TYPE.",
//% [self class], field.name,
//% TypeToString(GPBGetFieldDataType(field)));
//%#endif
//% GPBSetCopyObjectIvarWithField(self, field, (id)value);
//%}
//%
// Object types are handled slightly differently, they need to be released
// and retained.
void GPBClearAutocreatedMessageIvarWithField(GPBMessage *self,
GPBFieldDescriptor *field) {
if (GPBGetHasIvarField(self, field)) {
return;
}
uint8_t *storage = (uint8_t *)self->messageStorage_;
id *typePtr = (id *)&storage[field->description_->offset];
GPBMessage *oldValue = *typePtr;
*typePtr = NULL;
GPBClearMessageAutocreator(oldValue);
[oldValue release];
}
// This exists only for bridging some aliased types, nothing else should use it.
static void GPBSetObjectIvarWithField(GPBMessage *self,
GPBFieldDescriptor *field, id value) {
if (self == nil || field == nil) return;
GPBSetRetainedObjectIvarWithFieldPrivate(self, field, [value retain]);
}
static void GPBSetCopyObjectIvarWithField(GPBMessage *self,
GPBFieldDescriptor *field, id value);
// GPBSetCopyObjectIvarWithField is blocked from the analyzer because it flags
// a leak for the -copy even though GPBSetRetainedObjectIvarWithFieldPrivate
// is marked as consuming the value. Note: For some reason this doesn't happen
// with the -retain in GPBSetObjectIvarWithField.
#if !defined(__clang_analyzer__)
// This exists only for bridging some aliased types, nothing else should use it.
static void GPBSetCopyObjectIvarWithField(GPBMessage *self,
GPBFieldDescriptor *field, id value) {
if (self == nil || field == nil) return;
GPBSetRetainedObjectIvarWithFieldPrivate(self, field, [value copy]);
}
#endif // !defined(__clang_analyzer__)
void GPBSetObjectIvarWithFieldPrivate(GPBMessage *self,
GPBFieldDescriptor *field, id value) {
GPBSetRetainedObjectIvarWithFieldPrivate(self, field, [value retain]);
}
void GPBSetRetainedObjectIvarWithFieldPrivate(GPBMessage *self,
GPBFieldDescriptor *field,
id value) {
NSCAssert(self->messageStorage_ != NULL,
@"%@: All messages should have storage (from init)",
[self class]);
#if defined(__clang_analyzer__)
if (self->messageStorage_ == NULL) return;
#endif
GPBDataType fieldType = GPBGetFieldDataType(field);
BOOL isMapOrArray = GPBFieldIsMapOrArray(field);
BOOL fieldIsMessage = GPBDataTypeIsMessage(fieldType);
#if defined(DEBUG) && DEBUG
if (value == nil && !isMapOrArray && !fieldIsMessage &&
field.hasDefaultValue) {
// Setting a message to nil is an obvious way to "clear" the value
// as there is no way to set a non-empty default value for messages.
//
// For Strings and Bytes that have default values set it is not clear what
// should be done when their value is set to nil. Is the intention just to
// clear the set value and reset to default, or is the intention to set the
// value to the empty string/data? Arguments can be made for both cases.
// 'nil' has been abused as a replacement for an empty string/data in ObjC.
// We decided to be consistent with all "object" types and clear the has
// field, and fall back on the default value. The warning below will only
// appear in debug, but the could should be changed so the intention is
// clear.
NSString *hasSel = NSStringFromSelector(field->hasOrCountSel_);
NSString *propName = field.name;
NSString *className = self.descriptor.name;
NSLog(@"warning: '%@.%@ = nil;' is not clearly defined for fields with "
@"default values. Please use '%@.%@ = %@' if you want to set it to "
@"empty, or call '%@.%@ = NO' to reset it to it's default value of "
@"'%@'. Defaulting to resetting default value.",
className, propName, className, propName,
(fieldType == GPBDataTypeString) ? @"@\"\"" : @"GPBEmptyNSData()",
className, hasSel, field.defaultValue.valueString);
// Note: valueString, depending on the type, it could easily be
// valueData/valueMessage.
}
#endif // DEBUG
GPBMessageFieldDescription *fieldDesc = field->description_;
if (!isMapOrArray) {
// Non repeated/map can be in an oneof, clear any existing value from the
// oneof.
GPBOneofDescriptor *oneof = field->containingOneof_;
if (oneof) {
GPBMaybeClearOneofPrivate(self, oneof, fieldDesc->hasIndex, fieldDesc->number);
}
// Clear "has" if they are being set to nil.
BOOL setHasValue = (value != nil);
// If the field should clear on a "zero" value, then check if the string/data
// was zero length, and clear instead.
if (((fieldDesc->flags & GPBFieldClearHasIvarOnZero) != 0) &&
([value length] == 0)) {
setHasValue = NO;
// The value passed in was retained, it must be released since we
// aren't saving anything in the field.
[value release];
value = nil;
}
GPBSetHasIvar(self, fieldDesc->hasIndex, fieldDesc->number, setHasValue);
}
uint8_t *storage = (uint8_t *)self->messageStorage_;
id *typePtr = (id *)&storage[fieldDesc->offset];
id oldValue = *typePtr;
*typePtr = value;
if (oldValue) {
if (isMapOrArray) {
if (field.fieldType == GPBFieldTypeRepeated) {
// If the old array was autocreated by us, then clear it.
if (GPBDataTypeIsObject(fieldType)) {
if ([oldValue isKindOfClass:[GPBAutocreatedArray class]]) {
GPBAutocreatedArray *autoArray = oldValue;
if (autoArray->_autocreator == self) {
autoArray->_autocreator = nil;
}
}
} else {
// Type doesn't matter, it is a GPB*Array.
GPBInt32Array *gpbArray = oldValue;
if (gpbArray->_autocreator == self) {
gpbArray->_autocreator = nil;
}
}
} else { // GPBFieldTypeMap
// If the old map was autocreated by us, then clear it.
if ((field.mapKeyDataType == GPBDataTypeString) &&
GPBDataTypeIsObject(fieldType)) {
if ([oldValue isKindOfClass:[GPBAutocreatedDictionary class]]) {
GPBAutocreatedDictionary *autoDict = oldValue;
if (autoDict->_autocreator == self) {
autoDict->_autocreator = nil;
}
}
} else {
// Type doesn't matter, it is a GPB*Dictionary.
GPBInt32Int32Dictionary *gpbDict = oldValue;
if (gpbDict->_autocreator == self) {
gpbDict->_autocreator = nil;
}
}
}
} else if (fieldIsMessage) {
// If the old message value was autocreated by us, then clear it.
GPBMessage *oldMessageValue = oldValue;
if (GPBWasMessageAutocreatedBy(oldMessageValue, self)) {
GPBClearMessageAutocreator(oldMessageValue);
}
}
[oldValue release];
}
GPBBecomeVisibleToAutocreator(self);
}
id GPBGetObjectIvarWithFieldNoAutocreate(GPBMessage *self,
GPBFieldDescriptor *field) {
if (self->messageStorage_ == nil) {
return nil;
}
uint8_t *storage = (uint8_t *)self->messageStorage_;
id *typePtr = (id *)&storage[field->description_->offset];
return *typePtr;
}
// Only exists for public api, no core code should use this.
int32_t GPBGetMessageEnumField(GPBMessage *self, GPBFieldDescriptor *field) {
#if defined(DEBUG) && DEBUG
NSCAssert([[self descriptor] fieldWithNumber:field.number] == field,
@"FieldDescriptor %@ doesn't appear to be for %@ messages.",
field.name, [self class]);
NSCAssert(GPBGetFieldDataType(field) == GPBDataTypeEnum,
@"Attempting to get value of type Enum from field %@ "
@"of %@ which is of type %@.",
[self class], field.name,
TypeToString(GPBGetFieldDataType(field)));
#endif
int32_t result = GPBGetMessageInt32Field(self, field);
// If this is presevering unknown enums, make sure the value is valid before
// returning it.
GPBFileSyntax syntax = [self descriptor].file.syntax;
if (GPBHasPreservingUnknownEnumSemantics(syntax) &&
![field isValidEnumValue:result]) {
result = kGPBUnrecognizedEnumeratorValue;
}
return result;
}
// Only exists for public api, no core code should use this.
void GPBSetMessageEnumField(GPBMessage *self, GPBFieldDescriptor *field,
int32_t value) {
#if defined(DEBUG) && DEBUG
NSCAssert([[self descriptor] fieldWithNumber:field.number] == field,
@"FieldDescriptor %@ doesn't appear to be for %@ messages.",
field.name, [self class]);
NSCAssert(GPBGetFieldDataType(field) == GPBDataTypeEnum,
@"Attempting to set field %@ of %@ which is of type %@ with "
@"value of type Enum.",
[self class], field.name,
TypeToString(GPBGetFieldDataType(field)));
#endif
GPBSetEnumIvarWithFieldPrivate(self, field, value);
}
void GPBSetEnumIvarWithFieldPrivate(GPBMessage *self,
GPBFieldDescriptor *field, int32_t value) {
// Don't allow in unknown values. Proto3 can use the Raw method.
if (![field isValidEnumValue:value]) {
[NSException raise:NSInvalidArgumentException
format:@"%@.%@: Attempt to set an unknown enum value (%d)",
[self class], field.name, value];
}
GPBSetInt32IvarWithFieldPrivate(self, field, value);
}
// Only exists for public api, no core code should use this.
int32_t GPBGetMessageRawEnumField(GPBMessage *self,
GPBFieldDescriptor *field) {
int32_t result = GPBGetMessageInt32Field(self, field);
return result;
}
// Only exists for public api, no core code should use this.
void GPBSetMessageRawEnumField(GPBMessage *self, GPBFieldDescriptor *field,
int32_t value) {
GPBSetInt32IvarWithFieldPrivate(self, field, value);
}
BOOL GPBGetMessageBoolField(GPBMessage *self,
GPBFieldDescriptor *field) {
#if defined(DEBUG) && DEBUG
NSCAssert([[self descriptor] fieldWithNumber:field.number] == field,
@"FieldDescriptor %@ doesn't appear to be for %@ messages.",
field.name, [self class]);
NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field), GPBDataTypeBool),
@"Attempting to get value of type bool from field %@ "
@"of %@ which is of type %@.",
[self class], field.name,
TypeToString(GPBGetFieldDataType(field)));
#endif
if (GPBGetHasIvarField(self, field)) {
// Bools are stored in the has bits to avoid needing explicit space in the
// storage structure.
// (the field number passed to the HasIvar helper doesn't really matter
// since the offset is never negative)
GPBMessageFieldDescription *fieldDesc = field->description_;
return GPBGetHasIvar(self, (int32_t)(fieldDesc->offset), fieldDesc->number);
} else {
return field.defaultValue.valueBool;
}
}
// Only exists for public api, no core code should use this.
void GPBSetMessageBoolField(GPBMessage *self,
GPBFieldDescriptor *field,
BOOL value) {
if (self == nil || field == nil) return;
#if defined(DEBUG) && DEBUG
NSCAssert([[self descriptor] fieldWithNumber:field.number] == field,
@"FieldDescriptor %@ doesn't appear to be for %@ messages.",
field.name, [self class]);
NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field), GPBDataTypeBool),
@"Attempting to set field %@ of %@ which is of type %@ with "
@"value of type bool.",
[self class], field.name,
TypeToString(GPBGetFieldDataType(field)));
#endif
GPBSetBoolIvarWithFieldPrivate(self, field, value);
}
void GPBSetBoolIvarWithFieldPrivate(GPBMessage *self,
GPBFieldDescriptor *field,
BOOL value) {
GPBMessageFieldDescription *fieldDesc = field->description_;
GPBOneofDescriptor *oneof = field->containingOneof_;
if (oneof) {
GPBMaybeClearOneofPrivate(self, oneof, fieldDesc->hasIndex, fieldDesc->number);
}
// Bools are stored in the has bits to avoid needing explicit space in the
// storage structure.
// (the field number passed to the HasIvar helper doesn't really matter since
// the offset is never negative)
GPBSetHasIvar(self, (int32_t)(fieldDesc->offset), fieldDesc->number, value);
// If the value is zero, then we only count the field as "set" if the field
// shouldn't auto clear on zero.
BOOL hasValue = ((value != (BOOL)0)
|| ((fieldDesc->flags & GPBFieldClearHasIvarOnZero) == 0));
GPBSetHasIvar(self, fieldDesc->hasIndex, fieldDesc->number, hasValue);
GPBBecomeVisibleToAutocreator(self);
}
//%PDDM-EXPAND IVAR_POD_ACCESSORS_DEFN(Int32, int32_t)
// This block of code is generated, do not edit it directly.
// clang-format off
int32_t GPBGetMessageInt32Field(GPBMessage *self,
GPBFieldDescriptor *field) {
#if defined(DEBUG) && DEBUG
NSCAssert([[self descriptor] fieldWithNumber:field.number] == field,
@"FieldDescriptor %@ doesn't appear to be for %@ messages.",
field.name, [self class]);
NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
GPBDataTypeInt32),
@"Attempting to get value of int32_t from field %@ "
@"of %@ which is of type %@.",
[self class], field.name,
TypeToString(GPBGetFieldDataType(field)));
#endif
if (GPBGetHasIvarField(self, field)) {
uint8_t *storage = (uint8_t *)self->messageStorage_;
int32_t *typePtr = (int32_t *)&storage[field->description_->offset];
return *typePtr;
} else {
return field.defaultValue.valueInt32;
}
}
// Only exists for public api, no core code should use this.
void GPBSetMessageInt32Field(GPBMessage *self,
GPBFieldDescriptor *field,
int32_t value) {
if (self == nil || field == nil) return;
#if defined(DEBUG) && DEBUG
NSCAssert([[self descriptor] fieldWithNumber:field.number] == field,
@"FieldDescriptor %@ doesn't appear to be for %@ messages.",
field.name, [self class]);
NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
GPBDataTypeInt32),
@"Attempting to set field %@ of %@ which is of type %@ with "
@"value of type int32_t.",
[self class], field.name,
TypeToString(GPBGetFieldDataType(field)));
#endif
GPBSetInt32IvarWithFieldPrivate(self, field, value);
}
void GPBSetInt32IvarWithFieldPrivate(GPBMessage *self,
GPBFieldDescriptor *field,
int32_t value) {
GPBOneofDescriptor *oneof = field->containingOneof_;
GPBMessageFieldDescription *fieldDesc = field->description_;
if (oneof) {
GPBMaybeClearOneofPrivate(self, oneof, fieldDesc->hasIndex, fieldDesc->number);
}
#if defined(DEBUG) && DEBUG
NSCAssert(self->messageStorage_ != NULL,
@"%@: All messages should have storage (from init)",
[self class]);
#endif
#if defined(__clang_analyzer__)
if (self->messageStorage_ == NULL) return;
#endif
uint8_t *storage = (uint8_t *)self->messageStorage_;
int32_t *typePtr = (int32_t *)&storage[fieldDesc->offset];
*typePtr = value;
// If the value is zero, then we only count the field as "set" if the field
// shouldn't auto clear on zero.
BOOL hasValue = ((value != (int32_t)0)
|| ((fieldDesc->flags & GPBFieldClearHasIvarOnZero) == 0));
GPBSetHasIvar(self, fieldDesc->hasIndex, fieldDesc->number, hasValue);
GPBBecomeVisibleToAutocreator(self);
}
// clang-format on
//%PDDM-EXPAND IVAR_POD_ACCESSORS_DEFN(UInt32, uint32_t)
// This block of code is generated, do not edit it directly.
// clang-format off
uint32_t GPBGetMessageUInt32Field(GPBMessage *self,
GPBFieldDescriptor *field) {
#if defined(DEBUG) && DEBUG
NSCAssert([[self descriptor] fieldWithNumber:field.number] == field,
@"FieldDescriptor %@ doesn't appear to be for %@ messages.",
field.name, [self class]);
NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
GPBDataTypeUInt32),
@"Attempting to get value of uint32_t from field %@ "
@"of %@ which is of type %@.",
[self class], field.name,
TypeToString(GPBGetFieldDataType(field)));
#endif
if (GPBGetHasIvarField(self, field)) {
uint8_t *storage = (uint8_t *)self->messageStorage_;
uint32_t *typePtr = (uint32_t *)&storage[field->description_->offset];
return *typePtr;
} else {
return field.defaultValue.valueUInt32;
}
}
// Only exists for public api, no core code should use this.
void GPBSetMessageUInt32Field(GPBMessage *self,
GPBFieldDescriptor *field,
uint32_t value) {
if (self == nil || field == nil) return;
#if defined(DEBUG) && DEBUG
NSCAssert([[self descriptor] fieldWithNumber:field.number] == field,
@"FieldDescriptor %@ doesn't appear to be for %@ messages.",
field.name, [self class]);
NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
GPBDataTypeUInt32),
@"Attempting to set field %@ of %@ which is of type %@ with "
@"value of type uint32_t.",
[self class], field.name,
TypeToString(GPBGetFieldDataType(field)));
#endif
GPBSetUInt32IvarWithFieldPrivate(self, field, value);
}
void GPBSetUInt32IvarWithFieldPrivate(GPBMessage *self,
GPBFieldDescriptor *field,
uint32_t value) {
GPBOneofDescriptor *oneof = field->containingOneof_;
GPBMessageFieldDescription *fieldDesc = field->description_;
if (oneof) {
GPBMaybeClearOneofPrivate(self, oneof, fieldDesc->hasIndex, fieldDesc->number);
}
#if defined(DEBUG) && DEBUG
NSCAssert(self->messageStorage_ != NULL,
@"%@: All messages should have storage (from init)",
[self class]);
#endif
#if defined(__clang_analyzer__)
if (self->messageStorage_ == NULL) return;
#endif
uint8_t *storage = (uint8_t *)self->messageStorage_;
uint32_t *typePtr = (uint32_t *)&storage[fieldDesc->offset];
*typePtr = value;
// If the value is zero, then we only count the field as "set" if the field
// shouldn't auto clear on zero.
BOOL hasValue = ((value != (uint32_t)0)
|| ((fieldDesc->flags & GPBFieldClearHasIvarOnZero) == 0));
GPBSetHasIvar(self, fieldDesc->hasIndex, fieldDesc->number, hasValue);
GPBBecomeVisibleToAutocreator(self);
}
// clang-format on
//%PDDM-EXPAND IVAR_POD_ACCESSORS_DEFN(Int64, int64_t)
// This block of code is generated, do not edit it directly.
// clang-format off
int64_t GPBGetMessageInt64Field(GPBMessage *self,
GPBFieldDescriptor *field) {
#if defined(DEBUG) && DEBUG
NSCAssert([[self descriptor] fieldWithNumber:field.number] == field,
@"FieldDescriptor %@ doesn't appear to be for %@ messages.",
field.name, [self class]);
NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
GPBDataTypeInt64),
@"Attempting to get value of int64_t from field %@ "
@"of %@ which is of type %@.",
[self class], field.name,
TypeToString(GPBGetFieldDataType(field)));
#endif
if (GPBGetHasIvarField(self, field)) {
uint8_t *storage = (uint8_t *)self->messageStorage_;
int64_t *typePtr = (int64_t *)&storage[field->description_->offset];
return *typePtr;
} else {
return field.defaultValue.valueInt64;
}
}
// Only exists for public api, no core code should use this.
void GPBSetMessageInt64Field(GPBMessage *self,
GPBFieldDescriptor *field,
int64_t value) {
if (self == nil || field == nil) return;
#if defined(DEBUG) && DEBUG
NSCAssert([[self descriptor] fieldWithNumber:field.number] == field,
@"FieldDescriptor %@ doesn't appear to be for %@ messages.",
field.name, [self class]);
NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
GPBDataTypeInt64),
@"Attempting to set field %@ of %@ which is of type %@ with "
@"value of type int64_t.",
[self class], field.name,
TypeToString(GPBGetFieldDataType(field)));
#endif
GPBSetInt64IvarWithFieldPrivate(self, field, value);
}
void GPBSetInt64IvarWithFieldPrivate(GPBMessage *self,
GPBFieldDescriptor *field,
int64_t value) {