OLD | NEW |
| (Empty) |
1 // Protocol Buffers - Google's data interchange format | |
2 // Copyright 2008 Google Inc. All rights reserved. | |
3 // https://developers.google.com/protocol-buffers/ | |
4 // | |
5 // Redistribution and use in source and binary forms, with or without | |
6 // modification, are permitted provided that the following conditions are | |
7 // met: | |
8 // | |
9 // * Redistributions of source code must retain the above copyright | |
10 // notice, this list of conditions and the following disclaimer. | |
11 // * Redistributions in binary form must reproduce the above | |
12 // copyright notice, this list of conditions and the following disclaimer | |
13 // in the documentation and/or other materials provided with the | |
14 // distribution. | |
15 // * Neither the name of Google Inc. nor the names of its | |
16 // contributors may be used to endorse or promote products derived from | |
17 // this software without specific prior written permission. | |
18 // | |
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
30 | |
31 #import "GPBUtilities_PackagePrivate.h" | |
32 | |
33 #import <objc/runtime.h> | |
34 | |
35 #import "GPBArray_PackagePrivate.h" | |
36 #import "GPBDescriptor_PackagePrivate.h" | |
37 #import "GPBDictionary_PackagePrivate.h" | |
38 #import "GPBMessage_PackagePrivate.h" | |
39 #import "GPBUnknownField.h" | |
40 #import "GPBUnknownFieldSet.h" | |
41 | |
42 static void AppendTextFormatForMessage(GPBMessage *message, | |
43 NSMutableString *toStr, | |
44 NSString *lineIndent); | |
45 | |
46 NSData *GPBEmptyNSData(void) { | |
47 static dispatch_once_t onceToken; | |
48 static NSData *defaultNSData = nil; | |
49 dispatch_once(&onceToken, ^{ | |
50 defaultNSData = [[NSData alloc] init]; | |
51 }); | |
52 return defaultNSData; | |
53 } | |
54 | |
55 void GPBCheckRuntimeVersionInternal(int32_t version) { | |
56 if (version != GOOGLE_PROTOBUF_OBJC_GEN_VERSION) { | |
57 [NSException raise:NSInternalInconsistencyException | |
58 format:@"Linked to ProtocolBuffer runtime version %d," | |
59 @" but code compiled with version %d!", | |
60 GOOGLE_PROTOBUF_OBJC_GEN_VERSION, version]; | |
61 } | |
62 } | |
63 | |
64 BOOL GPBMessageHasFieldNumberSet(GPBMessage *self, uint32_t fieldNumber) { | |
65 GPBDescriptor *descriptor = [self descriptor]; | |
66 GPBFieldDescriptor *field = [descriptor fieldWithNumber:fieldNumber]; | |
67 return GPBMessageHasFieldSet(self, field); | |
68 } | |
69 | |
70 BOOL GPBMessageHasFieldSet(GPBMessage *self, GPBFieldDescriptor *field) { | |
71 if (self == nil || field == nil) return NO; | |
72 | |
73 // Repeated/Map don't use the bit, they check the count. | |
74 if (GPBFieldIsMapOrArray(field)) { | |
75 // Array/map type doesn't matter, since GPB*Array/NSArray and | |
76 // GPB*Dictionary/NSDictionary all support -count; | |
77 NSArray *arrayOrMap = GPBGetObjectIvarWithFieldNoAutocreate(self, field); | |
78 return (arrayOrMap.count > 0); | |
79 } else { | |
80 return GPBGetHasIvarField(self, field); | |
81 } | |
82 } | |
83 | |
84 void GPBClearMessageField(GPBMessage *self, GPBFieldDescriptor *field) { | |
85 // If not set, nothing to do. | |
86 if (!GPBGetHasIvarField(self, field)) { | |
87 return; | |
88 } | |
89 | |
90 if (GPBFieldStoresObject(field)) { | |
91 // Object types are handled slightly differently, they need to be released. | |
92 uint8_t *storage = (uint8_t *)self->messageStorage_; | |
93 id *typePtr = (id *)&storage[field->description_->offset]; | |
94 [*typePtr release]; | |
95 *typePtr = nil; | |
96 } else { | |
97 // POD types just need to clear the has bit as the Get* method will | |
98 // fetch the default when needed. | |
99 } | |
100 GPBSetHasIvarField(self, field, NO); | |
101 } | |
102 | |
103 BOOL GPBGetHasIvar(GPBMessage *self, int32_t idx, uint32_t fieldNumber) { | |
104 NSCAssert(self->messageStorage_ != NULL, | |
105 @"%@: All messages should have storage (from init)", | |
106 [self class]); | |
107 if (idx < 0) { | |
108 NSCAssert(fieldNumber != 0, @"Invalid field number."); | |
109 BOOL hasIvar = (self->messageStorage_->_has_storage_[-idx] == fieldNumber); | |
110 return hasIvar; | |
111 } else { | |
112 NSCAssert(idx != GPBNoHasBit, @"Invalid has bit."); | |
113 uint32_t byteIndex = idx / 32; | |
114 uint32_t bitMask = (1 << (idx % 32)); | |
115 BOOL hasIvar = | |
116 (self->messageStorage_->_has_storage_[byteIndex] & bitMask) ? YES : NO; | |
117 return hasIvar; | |
118 } | |
119 } | |
120 | |
121 uint32_t GPBGetHasOneof(GPBMessage *self, int32_t idx) { | |
122 NSCAssert(idx < 0, @"%@: invalid index (%d) for oneof.", | |
123 [self class], idx); | |
124 uint32_t result = self->messageStorage_->_has_storage_[-idx]; | |
125 return result; | |
126 } | |
127 | |
128 void GPBSetHasIvar(GPBMessage *self, int32_t idx, uint32_t fieldNumber, | |
129 BOOL value) { | |
130 if (idx < 0) { | |
131 NSCAssert(fieldNumber != 0, @"Invalid field number."); | |
132 uint32_t *has_storage = self->messageStorage_->_has_storage_; | |
133 has_storage[-idx] = (value ? fieldNumber : 0); | |
134 } else { | |
135 NSCAssert(idx != GPBNoHasBit, @"Invalid has bit."); | |
136 uint32_t *has_storage = self->messageStorage_->_has_storage_; | |
137 uint32_t byte = idx / 32; | |
138 uint32_t bitMask = (1 << (idx % 32)); | |
139 if (value) { | |
140 has_storage[byte] |= bitMask; | |
141 } else { | |
142 has_storage[byte] &= ~bitMask; | |
143 } | |
144 } | |
145 } | |
146 | |
147 void GPBMaybeClearOneof(GPBMessage *self, GPBOneofDescriptor *oneof, | |
148 uint32_t fieldNumberNotToClear) { | |
149 int32_t hasIndex = oneof->oneofDescription_->index; | |
150 uint32_t fieldNumberSet = GPBGetHasOneof(self, hasIndex); | |
151 if ((fieldNumberSet == fieldNumberNotToClear) || (fieldNumberSet == 0)) { | |
152 // Do nothing/nothing set in the oneof. | |
153 return; | |
154 } | |
155 | |
156 // Like GPBClearMessageField(), free the memory if an objecttype is set, | |
157 // pod types don't need to do anything. | |
158 GPBFieldDescriptor *fieldSet = [oneof fieldWithNumber:fieldNumberSet]; | |
159 NSCAssert(fieldSet, | |
160 @"%@: oneof set to something (%u) not in the oneof?", | |
161 [self class], fieldNumberSet); | |
162 if (fieldSet && GPBFieldStoresObject(fieldSet)) { | |
163 uint8_t *storage = (uint8_t *)self->messageStorage_; | |
164 id *typePtr = (id *)&storage[fieldSet->description_->offset]; | |
165 [*typePtr release]; | |
166 *typePtr = nil; | |
167 } | |
168 | |
169 // Set to nothing stored in the oneof. | |
170 // (field number doesn't matter since setting to nothing). | |
171 GPBSetHasIvar(self, hasIndex, 1, NO); | |
172 } | |
173 | |
174 #pragma mark - IVar accessors | |
175 | |
176 //%PDDM-DEFINE IVAR_POD_ACCESSORS_DEFN(NAME, TYPE) | |
177 //%TYPE GPBGetMessage##NAME##Field(GPBMessage *self, | |
178 //% TYPE$S NAME$S GPBFieldDescriptor *field) { | |
179 //% if (GPBGetHasIvarField(self, field)) { | |
180 //% uint8_t *storage = (uint8_t *)self->messageStorage_; | |
181 //% TYPE *typePtr = (TYPE *)&storage[field->description_->offset]; | |
182 //% return *typePtr; | |
183 //% } else { | |
184 //% return field.defaultValue.value##NAME; | |
185 //% } | |
186 //%} | |
187 //% | |
188 //%// Only exists for public api, no core code should use this. | |
189 //%void GPBSetMessage##NAME##Field(GPBMessage *self, | |
190 //% NAME$S GPBFieldDescriptor *field, | |
191 //% NAME$S TYPE value) { | |
192 //% if (self == nil || field == nil) return; | |
193 //% GPBFileSyntax syntax = [self descriptor].file.syntax; | |
194 //% GPBSet##NAME##IvarWithFieldInternal(self, field, value, syntax); | |
195 //%} | |
196 //% | |
197 //%void GPBSet##NAME##IvarWithFieldInternal(GPBMessage *self, | |
198 //% NAME$S GPBFieldDescriptor *field, | |
199 //% NAME$S TYPE value, | |
200 //% NAME$S GPBFileSyntax syntax) { | |
201 //% GPBOneofDescriptor *oneof = field->containingOneof_; | |
202 //% if (oneof) { | |
203 //% GPBMaybeClearOneof(self, oneof, GPBFieldNumber(field)); | |
204 //% } | |
205 //% NSCAssert(self->messageStorage_ != NULL, | |
206 //% @"%@: All messages should have storage (from init)", | |
207 //% [self class]); | |
208 //%#if defined(__clang_analyzer__) | |
209 //% if (self->messageStorage_ == NULL) return; | |
210 //%#endif | |
211 //% uint8_t *storage = (uint8_t *)self->messageStorage_; | |
212 //% TYPE *typePtr = (TYPE *)&storage[field->description_->offset]; | |
213 //% *typePtr = value; | |
214 //% // proto2: any value counts as having been set; proto3, it | |
215 //% // has to be a non zero value. | |
216 //% BOOL hasValue = | |
217 //% (syntax == GPBFileSyntaxProto2) || (value != (TYPE)0); | |
218 //% GPBSetHasIvarField(self, field, hasValue); | |
219 //% GPBBecomeVisibleToAutocreator(self); | |
220 //%} | |
221 //% | |
222 //%PDDM-DEFINE IVAR_ALIAS_DEFN_OBJECT(NAME, TYPE) | |
223 //%// Only exists for public api, no core code should use this. | |
224 //%TYPE *GPBGetMessage##NAME##Field(GPBMessage *self, | |
225 //% TYPE$S NAME$S GPBFieldDescriptor *field) { | |
226 //% return (TYPE *)GPBGetObjectIvarWithField(self, field); | |
227 //%} | |
228 //% | |
229 //%// Only exists for public api, no core code should use this. | |
230 //%void GPBSetMessage##NAME##Field(GPBMessage *self, | |
231 //% NAME$S GPBFieldDescriptor *field, | |
232 //% NAME$S TYPE *value) { | |
233 //% GPBSetObjectIvarWithField(self, field, (id)value); | |
234 //%} | |
235 //% | |
236 | |
237 // Object types are handled slightly differently, they need to be released | |
238 // and retained. | |
239 | |
240 void GPBSetAutocreatedRetainedObjectIvarWithField( | |
241 GPBMessage *self, GPBFieldDescriptor *field, | |
242 id __attribute__((ns_consumed)) value) { | |
243 uint8_t *storage = (uint8_t *)self->messageStorage_; | |
244 id *typePtr = (id *)&storage[field->description_->offset]; | |
245 NSCAssert(*typePtr == NULL, @"Can't set autocreated object more than once."); | |
246 *typePtr = value; | |
247 } | |
248 | |
249 void GPBClearAutocreatedMessageIvarWithField(GPBMessage *self, | |
250 GPBFieldDescriptor *field) { | |
251 if (GPBGetHasIvarField(self, field)) { | |
252 return; | |
253 } | |
254 uint8_t *storage = (uint8_t *)self->messageStorage_; | |
255 id *typePtr = (id *)&storage[field->description_->offset]; | |
256 GPBMessage *oldValue = *typePtr; | |
257 *typePtr = NULL; | |
258 GPBClearMessageAutocreator(oldValue); | |
259 [oldValue release]; | |
260 } | |
261 | |
262 // This exists only for briging some aliased types, nothing else should use it. | |
263 static void GPBSetObjectIvarWithField(GPBMessage *self, | |
264 GPBFieldDescriptor *field, id value) { | |
265 if (self == nil || field == nil) return; | |
266 GPBFileSyntax syntax = [self descriptor].file.syntax; | |
267 GPBSetRetainedObjectIvarWithFieldInternal(self, field, [value retain], | |
268 syntax); | |
269 } | |
270 | |
271 void GPBSetObjectIvarWithFieldInternal(GPBMessage *self, | |
272 GPBFieldDescriptor *field, id value, | |
273 GPBFileSyntax syntax) { | |
274 GPBSetRetainedObjectIvarWithFieldInternal(self, field, [value retain], | |
275 syntax); | |
276 } | |
277 | |
278 void GPBSetRetainedObjectIvarWithFieldInternal(GPBMessage *self, | |
279 GPBFieldDescriptor *field, | |
280 id value, GPBFileSyntax syntax) { | |
281 NSCAssert(self->messageStorage_ != NULL, | |
282 @"%@: All messages should have storage (from init)", | |
283 [self class]); | |
284 #if defined(__clang_analyzer__) | |
285 if (self->messageStorage_ == NULL) return; | |
286 #endif | |
287 GPBDataType fieldType = GPBGetFieldDataType(field); | |
288 BOOL isMapOrArray = GPBFieldIsMapOrArray(field); | |
289 BOOL fieldIsMessage = GPBDataTypeIsMessage(fieldType); | |
290 #ifdef DEBUG | |
291 if (value == nil && !isMapOrArray && !fieldIsMessage && | |
292 field.hasDefaultValue) { | |
293 // Setting a message to nil is an obvious way to "clear" the value | |
294 // as there is no way to set a non-empty default value for messages. | |
295 // | |
296 // For Strings and Bytes that have default values set it is not clear what | |
297 // should be done when their value is set to nil. Is the intention just to | |
298 // clear the set value and reset to default, or is the intention to set the | |
299 // value to the empty string/data? Arguments can be made for both cases. | |
300 // 'nil' has been abused as a replacement for an empty string/data in ObjC. | |
301 // We decided to be consistent with all "object" types and clear the has | |
302 // field, and fall back on the default value. The warning below will only | |
303 // appear in debug, but the could should be changed so the intention is | |
304 // clear. | |
305 NSString *hasSel = NSStringFromSelector(field->hasOrCountSel_); | |
306 NSString *propName = field.name; | |
307 NSString *className = self.descriptor.name; | |
308 NSLog(@"warning: '%@.%@ = nil;' is not clearly defined for fields with " | |
309 @"default values. Please use '%@.%@ = %@' if you want to set it to " | |
310 @"empty, or call '%@.%@ = NO' to reset it to it's default value of " | |
311 @"'%@'. Defaulting to resetting default value.", | |
312 className, propName, className, propName, | |
313 (fieldType == GPBDataTypeString) ? @"@\"\"" : @"GPBEmptyNSData()", | |
314 className, hasSel, field.defaultValue.valueString); | |
315 // Note: valueString, depending on the type, it could easily be | |
316 // valueData/valueMessage. | |
317 } | |
318 #endif // DEBUG | |
319 if (!isMapOrArray) { | |
320 // Non repeated/map can be in an oneof, clear any existing value from the | |
321 // oneof. | |
322 GPBOneofDescriptor *oneof = field->containingOneof_; | |
323 if (oneof) { | |
324 GPBMaybeClearOneof(self, oneof, GPBFieldNumber(field)); | |
325 } | |
326 // Clear "has" if they are being set to nil. | |
327 BOOL setHasValue = (value != nil); | |
328 // Under proto3, Bytes & String fields get cleared by resetting them to | |
329 // their default (empty) values, so if they are set to something of length | |
330 // zero, they are being cleared. | |
331 if ((syntax == GPBFileSyntaxProto3) && !fieldIsMessage && | |
332 ([value length] == 0)) { | |
333 setHasValue = NO; | |
334 value = nil; | |
335 } | |
336 GPBSetHasIvarField(self, field, setHasValue); | |
337 } | |
338 uint8_t *storage = (uint8_t *)self->messageStorage_; | |
339 id *typePtr = (id *)&storage[field->description_->offset]; | |
340 | |
341 id oldValue = *typePtr; | |
342 | |
343 *typePtr = value; | |
344 | |
345 if (oldValue) { | |
346 if (isMapOrArray) { | |
347 if (field.fieldType == GPBFieldTypeRepeated) { | |
348 // If the old array was autocreated by us, then clear it. | |
349 if (GPBDataTypeIsObject(fieldType)) { | |
350 GPBAutocreatedArray *autoArray = oldValue; | |
351 if (autoArray->_autocreator == self) { | |
352 autoArray->_autocreator = nil; | |
353 } | |
354 } else { | |
355 // Type doesn't matter, it is a GPB*Array. | |
356 GPBInt32Array *gpbArray = oldValue; | |
357 if (gpbArray->_autocreator == self) { | |
358 gpbArray->_autocreator = nil; | |
359 } | |
360 } | |
361 } else { // GPBFieldTypeMap | |
362 // If the old map was autocreated by us, then clear it. | |
363 if ((field.mapKeyDataType == GPBDataTypeString) && | |
364 GPBDataTypeIsObject(fieldType)) { | |
365 GPBAutocreatedDictionary *autoDict = oldValue; | |
366 if (autoDict->_autocreator == self) { | |
367 autoDict->_autocreator = nil; | |
368 } | |
369 } else { | |
370 // Type doesn't matter, it is a GPB*Dictionary. | |
371 GPBInt32Int32Dictionary *gpbDict = oldValue; | |
372 if (gpbDict->_autocreator == self) { | |
373 gpbDict->_autocreator = nil; | |
374 } | |
375 } | |
376 } | |
377 } else if (fieldIsMessage) { | |
378 // If the old message value was autocreated by us, then clear it. | |
379 GPBMessage *oldMessageValue = oldValue; | |
380 if (GPBWasMessageAutocreatedBy(oldMessageValue, self)) { | |
381 GPBClearMessageAutocreator(oldMessageValue); | |
382 } | |
383 } | |
384 [oldValue release]; | |
385 } | |
386 | |
387 GPBBecomeVisibleToAutocreator(self); | |
388 } | |
389 | |
390 id GPBGetObjectIvarWithFieldNoAutocreate(GPBMessage *self, | |
391 GPBFieldDescriptor *field) { | |
392 if (self->messageStorage_ == nil) { | |
393 return nil; | |
394 } | |
395 uint8_t *storage = (uint8_t *)self->messageStorage_; | |
396 id *typePtr = (id *)&storage[field->description_->offset]; | |
397 return *typePtr; | |
398 } | |
399 | |
400 id GPBGetObjectIvarWithField(GPBMessage *self, GPBFieldDescriptor *field) { | |
401 NSCAssert(!GPBFieldIsMapOrArray(field), @"Shouldn't get here"); | |
402 if (GPBGetHasIvarField(self, field)) { | |
403 uint8_t *storage = (uint8_t *)self->messageStorage_; | |
404 id *typePtr = (id *)&storage[field->description_->offset]; | |
405 return *typePtr; | |
406 } | |
407 // Not set... | |
408 | |
409 // Non messages (string/data), get their default. | |
410 if (!GPBFieldDataTypeIsMessage(field)) { | |
411 return field.defaultValue.valueMessage; | |
412 } | |
413 | |
414 OSSpinLockLock(&self->readOnlyMutex_); | |
415 GPBMessage *result = GPBGetObjectIvarWithFieldNoAutocreate(self, field); | |
416 if (!result) { | |
417 // For non repeated messages, create the object, set it and return it. | |
418 // This object will not initially be visible via GPBGetHasIvar, so | |
419 // we save its creator so it can become visible if it's mutated later. | |
420 result = GPBCreateMessageWithAutocreator(field.msgClass, self, field); | |
421 GPBSetAutocreatedRetainedObjectIvarWithField(self, field, result); | |
422 } | |
423 OSSpinLockUnlock(&self->readOnlyMutex_); | |
424 return result; | |
425 } | |
426 | |
427 // Only exists for public api, no core code should use this. | |
428 int32_t GPBGetMessageEnumField(GPBMessage *self, GPBFieldDescriptor *field) { | |
429 GPBFileSyntax syntax = [self descriptor].file.syntax; | |
430 return GPBGetEnumIvarWithFieldInternal(self, field, syntax); | |
431 } | |
432 | |
433 int32_t GPBGetEnumIvarWithFieldInternal(GPBMessage *self, | |
434 GPBFieldDescriptor *field, | |
435 GPBFileSyntax syntax) { | |
436 int32_t result = GPBGetMessageInt32Field(self, field); | |
437 // If this is presevering unknown enums, make sure the value is valid before | |
438 // returning it. | |
439 if (GPBHasPreservingUnknownEnumSemantics(syntax) && | |
440 ![field isValidEnumValue:result]) { | |
441 result = kGPBUnrecognizedEnumeratorValue; | |
442 } | |
443 return result; | |
444 } | |
445 | |
446 // Only exists for public api, no core code should use this. | |
447 void GPBSetMessageEnumField(GPBMessage *self, GPBFieldDescriptor *field, | |
448 int32_t value) { | |
449 GPBFileSyntax syntax = [self descriptor].file.syntax; | |
450 GPBSetInt32IvarWithFieldInternal(self, field, value, syntax); | |
451 } | |
452 | |
453 void GPBSetEnumIvarWithFieldInternal(GPBMessage *self, | |
454 GPBFieldDescriptor *field, int32_t value, | |
455 GPBFileSyntax syntax) { | |
456 // Don't allow in unknown values. Proto3 can use the Raw method. | |
457 if (![field isValidEnumValue:value]) { | |
458 [NSException raise:NSInvalidArgumentException | |
459 format:@"%@.%@: Attempt to set an unknown enum value (%d)", | |
460 [self class], field.name, value]; | |
461 } | |
462 GPBSetInt32IvarWithFieldInternal(self, field, value, syntax); | |
463 } | |
464 | |
465 // Only exists for public api, no core code should use this. | |
466 int32_t GPBGetMessageRawEnumField(GPBMessage *self, | |
467 GPBFieldDescriptor *field) { | |
468 int32_t result = GPBGetMessageInt32Field(self, field); | |
469 return result; | |
470 } | |
471 | |
472 // Only exists for public api, no core code should use this. | |
473 void GPBSetMessageRawEnumField(GPBMessage *self, GPBFieldDescriptor *field, | |
474 int32_t value) { | |
475 GPBFileSyntax syntax = [self descriptor].file.syntax; | |
476 GPBSetInt32IvarWithFieldInternal(self, field, value, syntax); | |
477 } | |
478 | |
479 //%PDDM-EXPAND IVAR_POD_ACCESSORS_DEFN(Bool, BOOL) | |
480 // This block of code is generated, do not edit it directly. | |
481 | |
482 BOOL GPBGetMessageBoolField(GPBMessage *self, | |
483 GPBFieldDescriptor *field) { | |
484 if (GPBGetHasIvarField(self, field)) { | |
485 uint8_t *storage = (uint8_t *)self->messageStorage_; | |
486 BOOL *typePtr = (BOOL *)&storage[field->description_->offset]; | |
487 return *typePtr; | |
488 } else { | |
489 return field.defaultValue.valueBool; | |
490 } | |
491 } | |
492 | |
493 // Only exists for public api, no core code should use this. | |
494 void GPBSetMessageBoolField(GPBMessage *self, | |
495 GPBFieldDescriptor *field, | |
496 BOOL value) { | |
497 if (self == nil || field == nil) return; | |
498 GPBFileSyntax syntax = [self descriptor].file.syntax; | |
499 GPBSetBoolIvarWithFieldInternal(self, field, value, syntax); | |
500 } | |
501 | |
502 void GPBSetBoolIvarWithFieldInternal(GPBMessage *self, | |
503 GPBFieldDescriptor *field, | |
504 BOOL value, | |
505 GPBFileSyntax syntax) { | |
506 GPBOneofDescriptor *oneof = field->containingOneof_; | |
507 if (oneof) { | |
508 GPBMaybeClearOneof(self, oneof, GPBFieldNumber(field)); | |
509 } | |
510 NSCAssert(self->messageStorage_ != NULL, | |
511 @"%@: All messages should have storage (from init)", | |
512 [self class]); | |
513 #if defined(__clang_analyzer__) | |
514 if (self->messageStorage_ == NULL) return; | |
515 #endif | |
516 uint8_t *storage = (uint8_t *)self->messageStorage_; | |
517 BOOL *typePtr = (BOOL *)&storage[field->description_->offset]; | |
518 *typePtr = value; | |
519 // proto2: any value counts as having been set; proto3, it | |
520 // has to be a non zero value. | |
521 BOOL hasValue = | |
522 (syntax == GPBFileSyntaxProto2) || (value != (BOOL)0); | |
523 GPBSetHasIvarField(self, field, hasValue); | |
524 GPBBecomeVisibleToAutocreator(self); | |
525 } | |
526 | |
527 //%PDDM-EXPAND IVAR_POD_ACCESSORS_DEFN(Int32, int32_t) | |
528 // This block of code is generated, do not edit it directly. | |
529 | |
530 int32_t GPBGetMessageInt32Field(GPBMessage *self, | |
531 GPBFieldDescriptor *field) { | |
532 if (GPBGetHasIvarField(self, field)) { | |
533 uint8_t *storage = (uint8_t *)self->messageStorage_; | |
534 int32_t *typePtr = (int32_t *)&storage[field->description_->offset]; | |
535 return *typePtr; | |
536 } else { | |
537 return field.defaultValue.valueInt32; | |
538 } | |
539 } | |
540 | |
541 // Only exists for public api, no core code should use this. | |
542 void GPBSetMessageInt32Field(GPBMessage *self, | |
543 GPBFieldDescriptor *field, | |
544 int32_t value) { | |
545 if (self == nil || field == nil) return; | |
546 GPBFileSyntax syntax = [self descriptor].file.syntax; | |
547 GPBSetInt32IvarWithFieldInternal(self, field, value, syntax); | |
548 } | |
549 | |
550 void GPBSetInt32IvarWithFieldInternal(GPBMessage *self, | |
551 GPBFieldDescriptor *field, | |
552 int32_t value, | |
553 GPBFileSyntax syntax) { | |
554 GPBOneofDescriptor *oneof = field->containingOneof_; | |
555 if (oneof) { | |
556 GPBMaybeClearOneof(self, oneof, GPBFieldNumber(field)); | |
557 } | |
558 NSCAssert(self->messageStorage_ != NULL, | |
559 @"%@: All messages should have storage (from init)", | |
560 [self class]); | |
561 #if defined(__clang_analyzer__) | |
562 if (self->messageStorage_ == NULL) return; | |
563 #endif | |
564 uint8_t *storage = (uint8_t *)self->messageStorage_; | |
565 int32_t *typePtr = (int32_t *)&storage[field->description_->offset]; | |
566 *typePtr = value; | |
567 // proto2: any value counts as having been set; proto3, it | |
568 // has to be a non zero value. | |
569 BOOL hasValue = | |
570 (syntax == GPBFileSyntaxProto2) || (value != (int32_t)0); | |
571 GPBSetHasIvarField(self, field, hasValue); | |
572 GPBBecomeVisibleToAutocreator(self); | |
573 } | |
574 | |
575 //%PDDM-EXPAND IVAR_POD_ACCESSORS_DEFN(UInt32, uint32_t) | |
576 // This block of code is generated, do not edit it directly. | |
577 | |
578 uint32_t GPBGetMessageUInt32Field(GPBMessage *self, | |
579 GPBFieldDescriptor *field) { | |
580 if (GPBGetHasIvarField(self, field)) { | |
581 uint8_t *storage = (uint8_t *)self->messageStorage_; | |
582 uint32_t *typePtr = (uint32_t *)&storage[field->description_->offset]; | |
583 return *typePtr; | |
584 } else { | |
585 return field.defaultValue.valueUInt32; | |
586 } | |
587 } | |
588 | |
589 // Only exists for public api, no core code should use this. | |
590 void GPBSetMessageUInt32Field(GPBMessage *self, | |
591 GPBFieldDescriptor *field, | |
592 uint32_t value) { | |
593 if (self == nil || field == nil) return; | |
594 GPBFileSyntax syntax = [self descriptor].file.syntax; | |
595 GPBSetUInt32IvarWithFieldInternal(self, field, value, syntax); | |
596 } | |
597 | |
598 void GPBSetUInt32IvarWithFieldInternal(GPBMessage *self, | |
599 GPBFieldDescriptor *field, | |
600 uint32_t value, | |
601 GPBFileSyntax syntax) { | |
602 GPBOneofDescriptor *oneof = field->containingOneof_; | |
603 if (oneof) { | |
604 GPBMaybeClearOneof(self, oneof, GPBFieldNumber(field)); | |
605 } | |
606 NSCAssert(self->messageStorage_ != NULL, | |
607 @"%@: All messages should have storage (from init)", | |
608 [self class]); | |
609 #if defined(__clang_analyzer__) | |
610 if (self->messageStorage_ == NULL) return; | |
611 #endif | |
612 uint8_t *storage = (uint8_t *)self->messageStorage_; | |
613 uint32_t *typePtr = (uint32_t *)&storage[field->description_->offset]; | |
614 *typePtr = value; | |
615 // proto2: any value counts as having been set; proto3, it | |
616 // has to be a non zero value. | |
617 BOOL hasValue = | |
618 (syntax == GPBFileSyntaxProto2) || (value != (uint32_t)0); | |
619 GPBSetHasIvarField(self, field, hasValue); | |
620 GPBBecomeVisibleToAutocreator(self); | |
621 } | |
622 | |
623 //%PDDM-EXPAND IVAR_POD_ACCESSORS_DEFN(Int64, int64_t) | |
624 // This block of code is generated, do not edit it directly. | |
625 | |
626 int64_t GPBGetMessageInt64Field(GPBMessage *self, | |
627 GPBFieldDescriptor *field) { | |
628 if (GPBGetHasIvarField(self, field)) { | |
629 uint8_t *storage = (uint8_t *)self->messageStorage_; | |
630 int64_t *typePtr = (int64_t *)&storage[field->description_->offset]; | |
631 return *typePtr; | |
632 } else { | |
633 return field.defaultValue.valueInt64; | |
634 } | |
635 } | |
636 | |
637 // Only exists for public api, no core code should use this. | |
638 void GPBSetMessageInt64Field(GPBMessage *self, | |
639 GPBFieldDescriptor *field, | |
640 int64_t value) { | |
641 if (self == nil || field == nil) return; | |
642 GPBFileSyntax syntax = [self descriptor].file.syntax; | |
643 GPBSetInt64IvarWithFieldInternal(self, field, value, syntax); | |
644 } | |
645 | |
646 void GPBSetInt64IvarWithFieldInternal(GPBMessage *self, | |
647 GPBFieldDescriptor *field, | |
648 int64_t value, | |
649 GPBFileSyntax syntax) { | |
650 GPBOneofDescriptor *oneof = field->containingOneof_; | |
651 if (oneof) { | |
652 GPBMaybeClearOneof(self, oneof, GPBFieldNumber(field)); | |
653 } | |
654 NSCAssert(self->messageStorage_ != NULL, | |
655 @"%@: All messages should have storage (from init)", | |
656 [self class]); | |
657 #if defined(__clang_analyzer__) | |
658 if (self->messageStorage_ == NULL) return; | |
659 #endif | |
660 uint8_t *storage = (uint8_t *)self->messageStorage_; | |
661 int64_t *typePtr = (int64_t *)&storage[field->description_->offset]; | |
662 *typePtr = value; | |
663 // proto2: any value counts as having been set; proto3, it | |
664 // has to be a non zero value. | |
665 BOOL hasValue = | |
666 (syntax == GPBFileSyntaxProto2) || (value != (int64_t)0); | |
667 GPBSetHasIvarField(self, field, hasValue); | |
668 GPBBecomeVisibleToAutocreator(self); | |
669 } | |
670 | |
671 //%PDDM-EXPAND IVAR_POD_ACCESSORS_DEFN(UInt64, uint64_t) | |
672 // This block of code is generated, do not edit it directly. | |
673 | |
674 uint64_t GPBGetMessageUInt64Field(GPBMessage *self, | |
675 GPBFieldDescriptor *field) { | |
676 if (GPBGetHasIvarField(self, field)) { | |
677 uint8_t *storage = (uint8_t *)self->messageStorage_; | |
678 uint64_t *typePtr = (uint64_t *)&storage[field->description_->offset]; | |
679 return *typePtr; | |
680 } else { | |
681 return field.defaultValue.valueUInt64; | |
682 } | |
683 } | |
684 | |
685 // Only exists for public api, no core code should use this. | |
686 void GPBSetMessageUInt64Field(GPBMessage *self, | |
687 GPBFieldDescriptor *field, | |
688 uint64_t value) { | |
689 if (self == nil || field == nil) return; | |
690 GPBFileSyntax syntax = [self descriptor].file.syntax; | |
691 GPBSetUInt64IvarWithFieldInternal(self, field, value, syntax); | |
692 } | |
693 | |
694 void GPBSetUInt64IvarWithFieldInternal(GPBMessage *self, | |
695 GPBFieldDescriptor *field, | |
696 uint64_t value, | |
697 GPBFileSyntax syntax) { | |
698 GPBOneofDescriptor *oneof = field->containingOneof_; | |
699 if (oneof) { | |
700 GPBMaybeClearOneof(self, oneof, GPBFieldNumber(field)); | |
701 } | |
702 NSCAssert(self->messageStorage_ != NULL, | |
703 @"%@: All messages should have storage (from init)", | |
704 [self class]); | |
705 #if defined(__clang_analyzer__) | |
706 if (self->messageStorage_ == NULL) return; | |
707 #endif | |
708 uint8_t *storage = (uint8_t *)self->messageStorage_; | |
709 uint64_t *typePtr = (uint64_t *)&storage[field->description_->offset]; | |
710 *typePtr = value; | |
711 // proto2: any value counts as having been set; proto3, it | |
712 // has to be a non zero value. | |
713 BOOL hasValue = | |
714 (syntax == GPBFileSyntaxProto2) || (value != (uint64_t)0); | |
715 GPBSetHasIvarField(self, field, hasValue); | |
716 GPBBecomeVisibleToAutocreator(self); | |
717 } | |
718 | |
719 //%PDDM-EXPAND IVAR_POD_ACCESSORS_DEFN(Float, float) | |
720 // This block of code is generated, do not edit it directly. | |
721 | |
722 float GPBGetMessageFloatField(GPBMessage *self, | |
723 GPBFieldDescriptor *field) { | |
724 if (GPBGetHasIvarField(self, field)) { | |
725 uint8_t *storage = (uint8_t *)self->messageStorage_; | |
726 float *typePtr = (float *)&storage[field->description_->offset]; | |
727 return *typePtr; | |
728 } else { | |
729 return field.defaultValue.valueFloat; | |
730 } | |
731 } | |
732 | |
733 // Only exists for public api, no core code should use this. | |
734 void GPBSetMessageFloatField(GPBMessage *self, | |
735 GPBFieldDescriptor *field, | |
736 float value) { | |
737 if (self == nil || field == nil) return; | |
738 GPBFileSyntax syntax = [self descriptor].file.syntax; | |
739 GPBSetFloatIvarWithFieldInternal(self, field, value, syntax); | |
740 } | |
741 | |
742 void GPBSetFloatIvarWithFieldInternal(GPBMessage *self, | |
743 GPBFieldDescriptor *field, | |
744 float value, | |
745 GPBFileSyntax syntax) { | |
746 GPBOneofDescriptor *oneof = field->containingOneof_; | |
747 if (oneof) { | |
748 GPBMaybeClearOneof(self, oneof, GPBFieldNumber(field)); | |
749 } | |
750 NSCAssert(self->messageStorage_ != NULL, | |
751 @"%@: All messages should have storage (from init)", | |
752 [self class]); | |
753 #if defined(__clang_analyzer__) | |
754 if (self->messageStorage_ == NULL) return; | |
755 #endif | |
756 uint8_t *storage = (uint8_t *)self->messageStorage_; | |
757 float *typePtr = (float *)&storage[field->description_->offset]; | |
758 *typePtr = value; | |
759 // proto2: any value counts as having been set; proto3, it | |
760 // has to be a non zero value. | |
761 BOOL hasValue = | |
762 (syntax == GPBFileSyntaxProto2) || (value != (float)0); | |
763 GPBSetHasIvarField(self, field, hasValue); | |
764 GPBBecomeVisibleToAutocreator(self); | |
765 } | |
766 | |
767 //%PDDM-EXPAND IVAR_POD_ACCESSORS_DEFN(Double, double) | |
768 // This block of code is generated, do not edit it directly. | |
769 | |
770 double GPBGetMessageDoubleField(GPBMessage *self, | |
771 GPBFieldDescriptor *field) { | |
772 if (GPBGetHasIvarField(self, field)) { | |
773 uint8_t *storage = (uint8_t *)self->messageStorage_; | |
774 double *typePtr = (double *)&storage[field->description_->offset]; | |
775 return *typePtr; | |
776 } else { | |
777 return field.defaultValue.valueDouble; | |
778 } | |
779 } | |
780 | |
781 // Only exists for public api, no core code should use this. | |
782 void GPBSetMessageDoubleField(GPBMessage *self, | |
783 GPBFieldDescriptor *field, | |
784 double value) { | |
785 if (self == nil || field == nil) return; | |
786 GPBFileSyntax syntax = [self descriptor].file.syntax; | |
787 GPBSetDoubleIvarWithFieldInternal(self, field, value, syntax); | |
788 } | |
789 | |
790 void GPBSetDoubleIvarWithFieldInternal(GPBMessage *self, | |
791 GPBFieldDescriptor *field, | |
792 double value, | |
793 GPBFileSyntax syntax) { | |
794 GPBOneofDescriptor *oneof = field->containingOneof_; | |
795 if (oneof) { | |
796 GPBMaybeClearOneof(self, oneof, GPBFieldNumber(field)); | |
797 } | |
798 NSCAssert(self->messageStorage_ != NULL, | |
799 @"%@: All messages should have storage (from init)", | |
800 [self class]); | |
801 #if defined(__clang_analyzer__) | |
802 if (self->messageStorage_ == NULL) return; | |
803 #endif | |
804 uint8_t *storage = (uint8_t *)self->messageStorage_; | |
805 double *typePtr = (double *)&storage[field->description_->offset]; | |
806 *typePtr = value; | |
807 // proto2: any value counts as having been set; proto3, it | |
808 // has to be a non zero value. | |
809 BOOL hasValue = | |
810 (syntax == GPBFileSyntaxProto2) || (value != (double)0); | |
811 GPBSetHasIvarField(self, field, hasValue); | |
812 GPBBecomeVisibleToAutocreator(self); | |
813 } | |
814 | |
815 //%PDDM-EXPAND-END (7 expansions) | |
816 | |
817 // Aliases are function calls that are virtually the same. | |
818 | |
819 //%PDDM-EXPAND IVAR_ALIAS_DEFN_OBJECT(String, NSString) | |
820 // This block of code is generated, do not edit it directly. | |
821 | |
822 // Only exists for public api, no core code should use this. | |
823 NSString *GPBGetMessageStringField(GPBMessage *self, | |
824 GPBFieldDescriptor *field) { | |
825 return (NSString *)GPBGetObjectIvarWithField(self, field); | |
826 } | |
827 | |
828 // Only exists for public api, no core code should use this. | |
829 void GPBSetMessageStringField(GPBMessage *self, | |
830 GPBFieldDescriptor *field, | |
831 NSString *value) { | |
832 GPBSetObjectIvarWithField(self, field, (id)value); | |
833 } | |
834 | |
835 //%PDDM-EXPAND IVAR_ALIAS_DEFN_OBJECT(Bytes, NSData) | |
836 // This block of code is generated, do not edit it directly. | |
837 | |
838 // Only exists for public api, no core code should use this. | |
839 NSData *GPBGetMessageBytesField(GPBMessage *self, | |
840 GPBFieldDescriptor *field) { | |
841 return (NSData *)GPBGetObjectIvarWithField(self, field); | |
842 } | |
843 | |
844 // Only exists for public api, no core code should use this. | |
845 void GPBSetMessageBytesField(GPBMessage *self, | |
846 GPBFieldDescriptor *field, | |
847 NSData *value) { | |
848 GPBSetObjectIvarWithField(self, field, (id)value); | |
849 } | |
850 | |
851 //%PDDM-EXPAND IVAR_ALIAS_DEFN_OBJECT(Message, GPBMessage) | |
852 // This block of code is generated, do not edit it directly. | |
853 | |
854 // Only exists for public api, no core code should use this. | |
855 GPBMessage *GPBGetMessageMessageField(GPBMessage *self, | |
856 GPBFieldDescriptor *field) { | |
857 return (GPBMessage *)GPBGetObjectIvarWithField(self, field); | |
858 } | |
859 | |
860 // Only exists for public api, no core code should use this. | |
861 void GPBSetMessageMessageField(GPBMessage *self, | |
862 GPBFieldDescriptor *field, | |
863 GPBMessage *value) { | |
864 GPBSetObjectIvarWithField(self, field, (id)value); | |
865 } | |
866 | |
867 //%PDDM-EXPAND IVAR_ALIAS_DEFN_OBJECT(Group, GPBMessage) | |
868 // This block of code is generated, do not edit it directly. | |
869 | |
870 // Only exists for public api, no core code should use this. | |
871 GPBMessage *GPBGetMessageGroupField(GPBMessage *self, | |
872 GPBFieldDescriptor *field) { | |
873 return (GPBMessage *)GPBGetObjectIvarWithField(self, field); | |
874 } | |
875 | |
876 // Only exists for public api, no core code should use this. | |
877 void GPBSetMessageGroupField(GPBMessage *self, | |
878 GPBFieldDescriptor *field, | |
879 GPBMessage *value) { | |
880 GPBSetObjectIvarWithField(self, field, (id)value); | |
881 } | |
882 | |
883 //%PDDM-EXPAND-END (4 expansions) | |
884 | |
885 // Only exists for public api, no core code should use this. | |
886 id GPBGetMessageRepeatedField(GPBMessage *self, GPBFieldDescriptor *field) { | |
887 #if DEBUG | |
888 if (field.fieldType != GPBFieldTypeRepeated) { | |
889 [NSException raise:NSInvalidArgumentException | |
890 format:@"%@.%@ is not a repeated field.", | |
891 [self class], field.name]; | |
892 } | |
893 #endif | |
894 return GPBGetObjectIvarWithField(self, field); | |
895 } | |
896 | |
897 // Only exists for public api, no core code should use this. | |
898 void GPBSetMessageRepeatedField(GPBMessage *self, GPBFieldDescriptor *field, id
array) { | |
899 #if DEBUG | |
900 if (field.fieldType != GPBFieldTypeRepeated) { | |
901 [NSException raise:NSInvalidArgumentException | |
902 format:@"%@.%@ is not a repeated field.", | |
903 [self class], field.name]; | |
904 } | |
905 Class expectedClass = Nil; | |
906 switch (GPBGetFieldDataType(field)) { | |
907 case GPBDataTypeBool: | |
908 expectedClass = [GPBBoolArray class]; | |
909 break; | |
910 case GPBDataTypeSFixed32: | |
911 case GPBDataTypeInt32: | |
912 case GPBDataTypeSInt32: | |
913 expectedClass = [GPBInt32Array class]; | |
914 break; | |
915 case GPBDataTypeFixed32: | |
916 case GPBDataTypeUInt32: | |
917 expectedClass = [GPBUInt32Array class]; | |
918 break; | |
919 case GPBDataTypeSFixed64: | |
920 case GPBDataTypeInt64: | |
921 case GPBDataTypeSInt64: | |
922 expectedClass = [GPBInt64Array class]; | |
923 break; | |
924 case GPBDataTypeFixed64: | |
925 case GPBDataTypeUInt64: | |
926 expectedClass = [GPBUInt64Array class]; | |
927 break; | |
928 case GPBDataTypeFloat: | |
929 expectedClass = [GPBFloatArray class]; | |
930 break; | |
931 case GPBDataTypeDouble: | |
932 expectedClass = [GPBDoubleArray class]; | |
933 break; | |
934 case GPBDataTypeBytes: | |
935 case GPBDataTypeString: | |
936 case GPBDataTypeMessage: | |
937 case GPBDataTypeGroup: | |
938 expectedClass = [NSMutableDictionary class]; | |
939 break; | |
940 case GPBDataTypeEnum: | |
941 expectedClass = [GPBBoolArray class]; | |
942 break; | |
943 } | |
944 if (array && ![array isKindOfClass:expectedClass]) { | |
945 [NSException raise:NSInvalidArgumentException | |
946 format:@"%@.%@: Expected %@ object, got %@.", | |
947 [self class], field.name, expectedClass, [array class]]; | |
948 } | |
949 #endif | |
950 GPBSetObjectIvarWithField(self, field, array); | |
951 } | |
952 | |
953 #if DEBUG | |
954 static NSString *TypeToStr(GPBDataType dataType) { | |
955 switch (dataType) { | |
956 case GPBDataTypeBool: | |
957 return @"Bool"; | |
958 case GPBDataTypeSFixed32: | |
959 case GPBDataTypeInt32: | |
960 case GPBDataTypeSInt32: | |
961 return @"Int32"; | |
962 case GPBDataTypeFixed32: | |
963 case GPBDataTypeUInt32: | |
964 return @"UInt32"; | |
965 case GPBDataTypeSFixed64: | |
966 case GPBDataTypeInt64: | |
967 case GPBDataTypeSInt64: | |
968 return @"Int64"; | |
969 case GPBDataTypeFixed64: | |
970 case GPBDataTypeUInt64: | |
971 return @"UInt64"; | |
972 case GPBDataTypeFloat: | |
973 return @"Float"; | |
974 case GPBDataTypeDouble: | |
975 return @"Double"; | |
976 case GPBDataTypeBytes: | |
977 case GPBDataTypeString: | |
978 case GPBDataTypeMessage: | |
979 case GPBDataTypeGroup: | |
980 return @"Object"; | |
981 case GPBDataTypeEnum: | |
982 return @"Bool"; | |
983 } | |
984 } | |
985 #endif | |
986 | |
987 // Only exists for public api, no core code should use this. | |
988 id GPBGetMessageMapField(GPBMessage *self, GPBFieldDescriptor *field) { | |
989 #if DEBUG | |
990 if (field.fieldType != GPBFieldTypeMap) { | |
991 [NSException raise:NSInvalidArgumentException | |
992 format:@"%@.%@ is not a map<> field.", | |
993 [self class], field.name]; | |
994 } | |
995 #endif | |
996 return GPBGetObjectIvarWithField(self, field); | |
997 } | |
998 | |
999 // Only exists for public api, no core code should use this. | |
1000 void GPBSetMessageMapField(GPBMessage *self, GPBFieldDescriptor *field, | |
1001 id dictionary) { | |
1002 #if DEBUG | |
1003 if (field.fieldType != GPBFieldTypeMap) { | |
1004 [NSException raise:NSInvalidArgumentException | |
1005 format:@"%@.%@ is not a map<> field.", | |
1006 [self class], field.name]; | |
1007 } | |
1008 if (dictionary) { | |
1009 GPBDataType keyDataType = field.mapKeyDataType; | |
1010 GPBDataType valueDataType = GPBGetFieldDataType(field); | |
1011 NSString *keyStr = TypeToStr(keyDataType); | |
1012 NSString *valueStr = TypeToStr(valueDataType); | |
1013 if (keyDataType == GPBDataTypeString) { | |
1014 keyStr = @"String"; | |
1015 } | |
1016 Class expectedClass = Nil; | |
1017 if ((keyDataType == GPBDataTypeString) && | |
1018 GPBDataTypeIsObject(valueDataType)) { | |
1019 expectedClass = [NSMutableDictionary class]; | |
1020 } else { | |
1021 NSString *className = | |
1022 [NSString stringWithFormat:@"GPB%@%@Dictionary", keyStr, valueStr]; | |
1023 expectedClass = NSClassFromString(className); | |
1024 NSCAssert(expectedClass, @"Missing a class (%@)?", expectedClass); | |
1025 } | |
1026 if (![dictionary isKindOfClass:expectedClass]) { | |
1027 [NSException raise:NSInvalidArgumentException | |
1028 format:@"%@.%@: Expected %@ object, got %@.", | |
1029 [self class], field.name, expectedClass, | |
1030 [dictionary class]]; | |
1031 } | |
1032 } | |
1033 #endif | |
1034 GPBSetObjectIvarWithField(self, field, dictionary); | |
1035 } | |
1036 | |
1037 #pragma mark - Misc Dynamic Runtime Utils | |
1038 | |
1039 const char *GPBMessageEncodingForSelector(SEL selector, BOOL instanceSel) { | |
1040 Protocol *protocol = | |
1041 objc_getProtocol(GPBStringifySymbol(GPBMessageSignatureProtocol)); | |
1042 struct objc_method_description description = | |
1043 protocol_getMethodDescription(protocol, selector, NO, instanceSel); | |
1044 return description.types; | |
1045 } | |
1046 | |
1047 #pragma mark - Text Format Support | |
1048 | |
1049 static void AppendStringEscaped(NSString *toPrint, NSMutableString *destStr) { | |
1050 [destStr appendString:@"\""]; | |
1051 NSUInteger len = [toPrint length]; | |
1052 for (NSUInteger i = 0; i < len; ++i) { | |
1053 unichar aChar = [toPrint characterAtIndex:i]; | |
1054 switch (aChar) { | |
1055 case '\n': [destStr appendString:@"\\n"]; break; | |
1056 case '\r': [destStr appendString:@"\\r"]; break; | |
1057 case '\t': [destStr appendString:@"\\t"]; break; | |
1058 case '\"': [destStr appendString:@"\\\""]; break; | |
1059 case '\'': [destStr appendString:@"\\\'"]; break; | |
1060 case '\\': [destStr appendString:@"\\\\"]; break; | |
1061 default: | |
1062 [destStr appendFormat:@"%C", aChar]; | |
1063 break; | |
1064 } | |
1065 } | |
1066 [destStr appendString:@"\""]; | |
1067 } | |
1068 | |
1069 static void AppendBufferAsString(NSData *buffer, NSMutableString *destStr) { | |
1070 const char *src = (const char *)[buffer bytes]; | |
1071 size_t srcLen = [buffer length]; | |
1072 [destStr appendString:@"\""]; | |
1073 for (const char *srcEnd = src + srcLen; src < srcEnd; src++) { | |
1074 switch (*src) { | |
1075 case '\n': [destStr appendString:@"\\n"]; break; | |
1076 case '\r': [destStr appendString:@"\\r"]; break; | |
1077 case '\t': [destStr appendString:@"\\t"]; break; | |
1078 case '\"': [destStr appendString:@"\\\""]; break; | |
1079 case '\'': [destStr appendString:@"\\\'"]; break; | |
1080 case '\\': [destStr appendString:@"\\\\"]; break; | |
1081 default: | |
1082 if (isprint(*src)) { | |
1083 [destStr appendFormat:@"%c", *src]; | |
1084 } else { | |
1085 // NOTE: doing hex means you have to worry about the letter after | |
1086 // the hex being another hex char and forcing that to be escaped, so | |
1087 // use octal to keep it simple. | |
1088 [destStr appendFormat:@"\\%03o", (uint8_t)(*src)]; | |
1089 } | |
1090 break; | |
1091 } | |
1092 } | |
1093 [destStr appendString:@"\""]; | |
1094 } | |
1095 | |
1096 static void AppendTextFormatForMapMessageField( | |
1097 id map, GPBFieldDescriptor *field, NSMutableString *toStr, | |
1098 NSString *lineIndent, NSString *fieldName, NSString *lineEnding) { | |
1099 GPBDataType keyDataType = field.mapKeyDataType; | |
1100 GPBDataType valueDataType = GPBGetFieldDataType(field); | |
1101 BOOL isMessageValue = GPBDataTypeIsMessage(valueDataType); | |
1102 | |
1103 NSString *msgStartFirst = | |
1104 [NSString stringWithFormat:@"%@%@ {%@\n", lineIndent, fieldName, lineEndin
g]; | |
1105 NSString *msgStart = | |
1106 [NSString stringWithFormat:@"%@%@ {\n", lineIndent, fieldName]; | |
1107 NSString *msgEnd = [NSString stringWithFormat:@"%@}\n", lineIndent]; | |
1108 | |
1109 NSString *keyLine = [NSString stringWithFormat:@"%@ key: ", lineIndent]; | |
1110 NSString *valueLine = [NSString stringWithFormat:@"%@ value%s ", lineIndent, | |
1111 (isMessageValue ? "" : ":")]; | |
1112 | |
1113 __block BOOL isFirst = YES; | |
1114 | |
1115 if ((keyDataType == GPBDataTypeString) && | |
1116 GPBDataTypeIsObject(valueDataType)) { | |
1117 // map is an NSDictionary. | |
1118 NSDictionary *dict = map; | |
1119 [dict enumerateKeysAndObjectsUsingBlock:^(NSString *key, id value, BOOL *sto
p) { | |
1120 #pragma unused(stop) | |
1121 [toStr appendString:(isFirst ? msgStartFirst : msgStart)]; | |
1122 isFirst = NO; | |
1123 | |
1124 [toStr appendString:keyLine]; | |
1125 AppendStringEscaped(key, toStr); | |
1126 [toStr appendString:@"\n"]; | |
1127 | |
1128 [toStr appendString:valueLine]; | |
1129 switch (valueDataType) { | |
1130 case GPBDataTypeString: | |
1131 AppendStringEscaped(value, toStr); | |
1132 break; | |
1133 | |
1134 case GPBDataTypeBytes: | |
1135 AppendBufferAsString(value, toStr); | |
1136 break; | |
1137 | |
1138 case GPBDataTypeMessage: | |
1139 [toStr appendString:@"{\n"]; | |
1140 NSString *subIndent = [lineIndent stringByAppendingString:@" "]; | |
1141 AppendTextFormatForMessage(value, toStr, subIndent); | |
1142 [toStr appendFormat:@"%@ }", lineIndent]; | |
1143 break; | |
1144 | |
1145 default: | |
1146 NSCAssert(NO, @"Can't happen"); | |
1147 break; | |
1148 } | |
1149 [toStr appendString:@"\n"]; | |
1150 | |
1151 [toStr appendString:msgEnd]; | |
1152 }]; | |
1153 } else { | |
1154 // map is one of the GPB*Dictionary classes, type doesn't matter. | |
1155 GPBInt32Int32Dictionary *dict = map; | |
1156 [dict enumerateForTextFormat:^(id keyObj, id valueObj) { | |
1157 [toStr appendString:(isFirst ? msgStartFirst : msgStart)]; | |
1158 isFirst = NO; | |
1159 | |
1160 // Key always is a NSString. | |
1161 if (keyDataType == GPBDataTypeString) { | |
1162 [toStr appendString:keyLine]; | |
1163 AppendStringEscaped(keyObj, toStr); | |
1164 [toStr appendString:@"\n"]; | |
1165 } else { | |
1166 [toStr appendFormat:@"%@%@\n", keyLine, keyObj]; | |
1167 } | |
1168 | |
1169 [toStr appendString:valueLine]; | |
1170 switch (valueDataType) { | |
1171 case GPBDataTypeString: | |
1172 AppendStringEscaped(valueObj, toStr); | |
1173 break; | |
1174 | |
1175 case GPBDataTypeBytes: | |
1176 AppendBufferAsString(valueObj, toStr); | |
1177 break; | |
1178 | |
1179 case GPBDataTypeMessage: | |
1180 [toStr appendString:@"{\n"]; | |
1181 NSString *subIndent = [lineIndent stringByAppendingString:@" "]; | |
1182 AppendTextFormatForMessage(valueObj, toStr, subIndent); | |
1183 [toStr appendFormat:@"%@ }", lineIndent]; | |
1184 break; | |
1185 | |
1186 case GPBDataTypeEnum: { | |
1187 int32_t enumValue = [valueObj intValue]; | |
1188 NSString *valueStr = nil; | |
1189 GPBEnumDescriptor *descriptor = field.enumDescriptor; | |
1190 if (descriptor) { | |
1191 valueStr = [descriptor textFormatNameForValue:enumValue]; | |
1192 } | |
1193 if (valueStr) { | |
1194 [toStr appendString:valueStr]; | |
1195 } else { | |
1196 [toStr appendFormat:@"%d", enumValue]; | |
1197 } | |
1198 break; | |
1199 } | |
1200 | |
1201 default: | |
1202 NSCAssert(valueDataType != GPBDataTypeGroup, @"Can't happen"); | |
1203 // Everything else is a NSString. | |
1204 [toStr appendString:valueObj]; | |
1205 break; | |
1206 } | |
1207 [toStr appendString:@"\n"]; | |
1208 | |
1209 [toStr appendString:msgEnd]; | |
1210 }]; | |
1211 } | |
1212 } | |
1213 | |
1214 static void AppendTextFormatForMessageField(GPBMessage *message, | |
1215 GPBFieldDescriptor *field, | |
1216 NSMutableString *toStr, | |
1217 NSString *lineIndent) { | |
1218 id arrayOrMap; | |
1219 NSUInteger count; | |
1220 GPBFieldType fieldType = field.fieldType; | |
1221 switch (fieldType) { | |
1222 case GPBFieldTypeSingle: | |
1223 arrayOrMap = nil; | |
1224 count = (GPBGetHasIvarField(message, field) ? 1 : 0); | |
1225 break; | |
1226 | |
1227 case GPBFieldTypeRepeated: | |
1228 // Will be NSArray or GPB*Array, type doesn't matter, they both | |
1229 // implement count. | |
1230 arrayOrMap = GPBGetObjectIvarWithFieldNoAutocreate(message, field); | |
1231 count = [(NSArray *)arrayOrMap count]; | |
1232 break; | |
1233 | |
1234 case GPBFieldTypeMap: { | |
1235 // Will be GPB*Dictionary or NSMutableDictionary, type doesn't matter, | |
1236 // they both implement count. | |
1237 arrayOrMap = GPBGetObjectIvarWithFieldNoAutocreate(message, field); | |
1238 count = [(NSDictionary *)arrayOrMap count]; | |
1239 break; | |
1240 } | |
1241 } | |
1242 | |
1243 if (count == 0) { | |
1244 // Nothing to print, out of here. | |
1245 return; | |
1246 } | |
1247 | |
1248 NSString *lineEnding = @""; | |
1249 | |
1250 // If the name can't be reversed or support for extra info was turned off, | |
1251 // this can return nil. | |
1252 NSString *fieldName = [field textFormatName]; | |
1253 if ([fieldName length] == 0) { | |
1254 fieldName = [NSString stringWithFormat:@"%u", GPBFieldNumber(field)]; | |
1255 // If there is only one entry, put the objc name as a comment, other wise | |
1256 // add it before the the repeated values. | |
1257 if (count > 1) { | |
1258 [toStr appendFormat:@"%@# %@\n", lineIndent, field.name]; | |
1259 } else { | |
1260 lineEnding = [NSString stringWithFormat:@" # %@", field.name]; | |
1261 } | |
1262 } | |
1263 | |
1264 if (fieldType == GPBFieldTypeMap) { | |
1265 AppendTextFormatForMapMessageField(arrayOrMap, field, toStr, lineIndent, | |
1266 fieldName, lineEnding); | |
1267 return; | |
1268 } | |
1269 | |
1270 id array = arrayOrMap; | |
1271 const BOOL isRepeated = (array != nil); | |
1272 | |
1273 GPBDataType fieldDataType = GPBGetFieldDataType(field); | |
1274 BOOL isMessageField = GPBDataTypeIsMessage(fieldDataType); | |
1275 for (NSUInteger j = 0; j < count; ++j) { | |
1276 // Start the line. | |
1277 [toStr appendFormat:@"%@%@%s ", lineIndent, fieldName, | |
1278 (isMessageField ? "" : ":")]; | |
1279 | |
1280 // The value. | |
1281 switch (fieldDataType) { | |
1282 #define FIELD_CASE(GPBDATATYPE, CTYPE, REAL_TYPE, ...) \ | |
1283 case GPBDataType##GPBDATATYPE: { \ | |
1284 CTYPE v = (isRepeated ? [(GPB##REAL_TYPE##Array *)array valueAtIndex:j] \ | |
1285 : GPBGetMessage##REAL_TYPE##Field(message, field)); \ | |
1286 [toStr appendFormat:__VA_ARGS__, v]; \ | |
1287 break; \ | |
1288 } | |
1289 | |
1290 FIELD_CASE(Int32, int32_t, Int32, @"%d") | |
1291 FIELD_CASE(SInt32, int32_t, Int32, @"%d") | |
1292 FIELD_CASE(SFixed32, int32_t, Int32, @"%d") | |
1293 FIELD_CASE(UInt32, uint32_t, UInt32, @"%u") | |
1294 FIELD_CASE(Fixed32, uint32_t, UInt32, @"%u") | |
1295 FIELD_CASE(Int64, int64_t, Int64, @"%lld") | |
1296 FIELD_CASE(SInt64, int64_t, Int64, @"%lld") | |
1297 FIELD_CASE(SFixed64, int64_t, Int64, @"%lld") | |
1298 FIELD_CASE(UInt64, uint64_t, UInt64, @"%llu") | |
1299 FIELD_CASE(Fixed64, uint64_t, UInt64, @"%llu") | |
1300 FIELD_CASE(Float, float, Float, @"%.*g", FLT_DIG) | |
1301 FIELD_CASE(Double, double, Double, @"%.*lg", DBL_DIG) | |
1302 | |
1303 #undef FIELD_CASE | |
1304 | |
1305 case GPBDataTypeEnum: { | |
1306 int32_t v = (isRepeated ? [(GPBEnumArray *)array rawValueAtIndex:j] | |
1307 : GPBGetMessageInt32Field(message, field)); | |
1308 NSString *valueStr = nil; | |
1309 GPBEnumDescriptor *descriptor = field.enumDescriptor; | |
1310 if (descriptor) { | |
1311 valueStr = [descriptor textFormatNameForValue:v]; | |
1312 } | |
1313 if (valueStr) { | |
1314 [toStr appendString:valueStr]; | |
1315 } else { | |
1316 [toStr appendFormat:@"%d", v]; | |
1317 } | |
1318 break; | |
1319 } | |
1320 | |
1321 case GPBDataTypeBool: { | |
1322 BOOL v = (isRepeated ? [(GPBBoolArray *)array valueAtIndex:j] | |
1323 : GPBGetMessageBoolField(message, field)); | |
1324 [toStr appendString:(v ? @"true" : @"false")]; | |
1325 break; | |
1326 } | |
1327 | |
1328 case GPBDataTypeString: { | |
1329 NSString *v = (isRepeated ? [(NSArray *)array objectAtIndex:j] | |
1330 : GPBGetMessageStringField(message, field)); | |
1331 AppendStringEscaped(v, toStr); | |
1332 break; | |
1333 } | |
1334 | |
1335 case GPBDataTypeBytes: { | |
1336 NSData *v = (isRepeated ? [(NSArray *)array objectAtIndex:j] | |
1337 : GPBGetMessageBytesField(message, field)); | |
1338 AppendBufferAsString(v, toStr); | |
1339 break; | |
1340 } | |
1341 | |
1342 case GPBDataTypeGroup: | |
1343 case GPBDataTypeMessage: { | |
1344 GPBMessage *v = | |
1345 (isRepeated ? [(NSArray *)array objectAtIndex:j] | |
1346 : GPBGetObjectIvarWithField(message, field)); | |
1347 [toStr appendFormat:@"{%@\n", lineEnding]; | |
1348 NSString *subIndent = [lineIndent stringByAppendingString:@" "]; | |
1349 AppendTextFormatForMessage(v, toStr, subIndent); | |
1350 [toStr appendFormat:@"%@}", lineIndent]; | |
1351 lineEnding = @""; | |
1352 break; | |
1353 } | |
1354 | |
1355 } // switch(fieldDataType) | |
1356 | |
1357 // End the line. | |
1358 [toStr appendFormat:@"%@\n", lineEnding]; | |
1359 | |
1360 } // for(count) | |
1361 } | |
1362 | |
1363 static void AppendTextFormatForMessageExtensionRange(GPBMessage *message, | |
1364 NSArray *activeExtensions, | |
1365 GPBExtensionRange range, | |
1366 NSMutableString *toStr, | |
1367 NSString *lineIndent) { | |
1368 uint32_t start = range.start; | |
1369 uint32_t end = range.end; | |
1370 for (GPBExtensionDescriptor *extension in activeExtensions) { | |
1371 uint32_t fieldNumber = extension.fieldNumber; | |
1372 if (fieldNumber < start) { | |
1373 // Not there yet. | |
1374 continue; | |
1375 } | |
1376 if (fieldNumber > end) { | |
1377 // Done. | |
1378 break; | |
1379 } | |
1380 | |
1381 id rawExtValue = [message getExtension:extension]; | |
1382 BOOL isRepeated = extension.isRepeated; | |
1383 | |
1384 NSUInteger numValues = 1; | |
1385 NSString *lineEnding = @""; | |
1386 if (isRepeated) { | |
1387 numValues = [(NSArray *)rawExtValue count]; | |
1388 } | |
1389 | |
1390 NSString *singletonName = extension.singletonName; | |
1391 if (numValues == 1) { | |
1392 lineEnding = [NSString stringWithFormat:@" # [%@]", singletonName]; | |
1393 } else { | |
1394 [toStr appendFormat:@"%@# [%@]\n", lineIndent, singletonName]; | |
1395 } | |
1396 | |
1397 GPBDataType extDataType = extension.dataType; | |
1398 for (NSUInteger j = 0; j < numValues; ++j) { | |
1399 id curValue = (isRepeated ? [rawExtValue objectAtIndex:j] : rawExtValue); | |
1400 | |
1401 // Start the line. | |
1402 [toStr appendFormat:@"%@%u%s ", lineIndent, fieldNumber, | |
1403 (GPBDataTypeIsMessage(extDataType) ? "" : ":")]; | |
1404 | |
1405 // The value. | |
1406 switch (extDataType) { | |
1407 #define FIELD_CASE(GPBDATATYPE, CTYPE, NUMSELECTOR, ...) \ | |
1408 case GPBDataType##GPBDATATYPE: { \ | |
1409 CTYPE v = [(NSNumber *)curValue NUMSELECTOR]; \ | |
1410 [toStr appendFormat:__VA_ARGS__, v]; \ | |
1411 break; \ | |
1412 } | |
1413 | |
1414 FIELD_CASE(Int32, int32_t, intValue, @"%d") | |
1415 FIELD_CASE(SInt32, int32_t, intValue, @"%d") | |
1416 FIELD_CASE(SFixed32, int32_t, unsignedIntValue, @"%d") | |
1417 FIELD_CASE(UInt32, uint32_t, unsignedIntValue, @"%u") | |
1418 FIELD_CASE(Fixed32, uint32_t, unsignedIntValue, @"%u") | |
1419 FIELD_CASE(Int64, int64_t, longLongValue, @"%lld") | |
1420 FIELD_CASE(SInt64, int64_t, longLongValue, @"%lld") | |
1421 FIELD_CASE(SFixed64, int64_t, longLongValue, @"%lld") | |
1422 FIELD_CASE(UInt64, uint64_t, unsignedLongLongValue, @"%llu") | |
1423 FIELD_CASE(Fixed64, uint64_t, unsignedLongLongValue, @"%llu") | |
1424 FIELD_CASE(Float, float, floatValue, @"%.*g", FLT_DIG) | |
1425 FIELD_CASE(Double, double, doubleValue, @"%.*lg", DBL_DIG) | |
1426 // TODO: Add a comment with the enum name from enum descriptors | |
1427 // (might not be real value, so leave it as a comment, ObjC compiler | |
1428 // name mangles differently). Doesn't look like we actually generate | |
1429 // an enum descriptor reference like we do for normal fields, so this | |
1430 // will take a compiler change. | |
1431 FIELD_CASE(Enum, int32_t, intValue, @"%d") | |
1432 | |
1433 #undef FIELD_CASE | |
1434 | |
1435 case GPBDataTypeBool: | |
1436 [toStr appendString:([(NSNumber *)curValue boolValue] ? @"true" | |
1437 : @"false")]; | |
1438 break; | |
1439 | |
1440 case GPBDataTypeString: | |
1441 AppendStringEscaped(curValue, toStr); | |
1442 break; | |
1443 | |
1444 case GPBDataTypeBytes: | |
1445 AppendBufferAsString((NSData *)curValue, toStr); | |
1446 break; | |
1447 | |
1448 case GPBDataTypeGroup: | |
1449 case GPBDataTypeMessage: { | |
1450 [toStr appendFormat:@"{%@\n", lineEnding]; | |
1451 NSString *subIndent = [lineIndent stringByAppendingString:@" "]; | |
1452 AppendTextFormatForMessage(curValue, toStr, subIndent); | |
1453 [toStr appendFormat:@"%@}", lineIndent]; | |
1454 lineEnding = @""; | |
1455 break; | |
1456 } | |
1457 | |
1458 } // switch(extDataType) | |
1459 | |
1460 } // for(numValues) | |
1461 | |
1462 // End the line. | |
1463 [toStr appendFormat:@"%@\n", lineEnding]; | |
1464 | |
1465 } // for..in(activeExtensions) | |
1466 } | |
1467 | |
1468 static void AppendTextFormatForMessage(GPBMessage *message, | |
1469 NSMutableString *toStr, | |
1470 NSString *lineIndent) { | |
1471 GPBDescriptor *descriptor = [message descriptor]; | |
1472 NSArray *fieldsArray = descriptor->fields_; | |
1473 NSUInteger fieldCount = fieldsArray.count; | |
1474 const GPBExtensionRange *extensionRanges = descriptor.extensionRanges; | |
1475 NSUInteger extensionRangesCount = descriptor.extensionRangesCount; | |
1476 NSArray *activeExtensions = [message sortedExtensionsInUse]; | |
1477 for (NSUInteger i = 0, j = 0; i < fieldCount || j < extensionRangesCount;) { | |
1478 if (i == fieldCount) { | |
1479 AppendTextFormatForMessageExtensionRange( | |
1480 message, activeExtensions, extensionRanges[j++], toStr, lineIndent); | |
1481 } else if (j == extensionRangesCount || | |
1482 GPBFieldNumber(fieldsArray[i]) < extensionRanges[j].start) { | |
1483 AppendTextFormatForMessageField(message, fieldsArray[i++], toStr, | |
1484 lineIndent); | |
1485 } else { | |
1486 AppendTextFormatForMessageExtensionRange( | |
1487 message, activeExtensions, extensionRanges[j++], toStr, lineIndent); | |
1488 } | |
1489 } | |
1490 | |
1491 NSString *unknownFieldsStr = | |
1492 GPBTextFormatForUnknownFieldSet(message.unknownFields, lineIndent); | |
1493 if ([unknownFieldsStr length] > 0) { | |
1494 [toStr appendFormat:@"%@# --- Unknown fields ---\n", lineIndent]; | |
1495 [toStr appendString:unknownFieldsStr]; | |
1496 } | |
1497 } | |
1498 | |
1499 NSString *GPBTextFormatForMessage(GPBMessage *message, NSString *lineIndent) { | |
1500 if (message == nil) return @""; | |
1501 if (lineIndent == nil) lineIndent = @""; | |
1502 | |
1503 NSMutableString *buildString = [NSMutableString string]; | |
1504 AppendTextFormatForMessage(message, buildString, lineIndent); | |
1505 return buildString; | |
1506 } | |
1507 | |
1508 NSString *GPBTextFormatForUnknownFieldSet(GPBUnknownFieldSet *unknownSet, | |
1509 NSString *lineIndent) { | |
1510 if (unknownSet == nil) return @""; | |
1511 if (lineIndent == nil) lineIndent = @""; | |
1512 | |
1513 NSMutableString *result = [NSMutableString string]; | |
1514 for (GPBUnknownField *field in [unknownSet sortedFields]) { | |
1515 int32_t fieldNumber = [field number]; | |
1516 | |
1517 #define PRINT_LOOP(PROPNAME, CTYPE, FORMAT) \ | |
1518 [field.PROPNAME \ | |
1519 enumerateValuesWithBlock:^(CTYPE value, NSUInteger idx, BOOL * stop) { \ | |
1520 _Pragma("unused(idx, stop)"); \ | |
1521 [result \ | |
1522 appendFormat:@"%@%d: " #FORMAT "\n", lineIndent, fieldNumber, value]; \ | |
1523 }]; | |
1524 | |
1525 PRINT_LOOP(varintList, uint64_t, %llu); | |
1526 PRINT_LOOP(fixed32List, uint32_t, 0x%X); | |
1527 PRINT_LOOP(fixed64List, uint64_t, 0x%llX); | |
1528 | |
1529 #undef PRINT_LOOP | |
1530 | |
1531 // NOTE: C++ version of TextFormat tries to parse this as a message | |
1532 // and print that if it succeeds. | |
1533 for (NSData *data in field.lengthDelimitedList) { | |
1534 [result appendFormat:@"%@%d: ", lineIndent, fieldNumber]; | |
1535 AppendBufferAsString(data, result); | |
1536 [result appendString:@"\n"]; | |
1537 } | |
1538 | |
1539 for (GPBUnknownFieldSet *subUnknownSet in field.groupList) { | |
1540 [result appendFormat:@"%@%d: {\n", lineIndent, fieldNumber]; | |
1541 NSString *subIndent = [lineIndent stringByAppendingString:@" "]; | |
1542 NSString *subUnknwonSetStr = | |
1543 GPBTextFormatForUnknownFieldSet(subUnknownSet, subIndent); | |
1544 [result appendString:subUnknwonSetStr]; | |
1545 [result appendFormat:@"%@}\n", lineIndent]; | |
1546 } | |
1547 } | |
1548 return result; | |
1549 } | |
1550 | |
1551 // Helpers to decode a varint. Not using GPBCodedInputStream version because | |
1552 // that needs a state object, and we don't want to create an input stream out | |
1553 // of the data. | |
1554 GPB_INLINE int8_t ReadRawByteFromData(const uint8_t **data) { | |
1555 int8_t result = *((int8_t *)(*data)); | |
1556 ++(*data); | |
1557 return result; | |
1558 } | |
1559 | |
1560 static int32_t ReadRawVarint32FromData(const uint8_t **data) { | |
1561 int8_t tmp = ReadRawByteFromData(data); | |
1562 if (tmp >= 0) { | |
1563 return tmp; | |
1564 } | |
1565 int32_t result = tmp & 0x7f; | |
1566 if ((tmp = ReadRawByteFromData(data)) >= 0) { | |
1567 result |= tmp << 7; | |
1568 } else { | |
1569 result |= (tmp & 0x7f) << 7; | |
1570 if ((tmp = ReadRawByteFromData(data)) >= 0) { | |
1571 result |= tmp << 14; | |
1572 } else { | |
1573 result |= (tmp & 0x7f) << 14; | |
1574 if ((tmp = ReadRawByteFromData(data)) >= 0) { | |
1575 result |= tmp << 21; | |
1576 } else { | |
1577 result |= (tmp & 0x7f) << 21; | |
1578 result |= (tmp = ReadRawByteFromData(data)) << 28; | |
1579 if (tmp < 0) { | |
1580 // Discard upper 32 bits. | |
1581 for (int i = 0; i < 5; i++) { | |
1582 if (ReadRawByteFromData(data) >= 0) { | |
1583 return result; | |
1584 } | |
1585 } | |
1586 [NSException raise:NSParseErrorException | |
1587 format:@"Unable to read varint32"]; | |
1588 } | |
1589 } | |
1590 } | |
1591 } | |
1592 return result; | |
1593 } | |
1594 | |
1595 NSString *GPBDecodeTextFormatName(const uint8_t *decodeData, int32_t key, | |
1596 NSString *inputStr) { | |
1597 // decodData form: | |
1598 // varint32: num entries | |
1599 // for each entry: | |
1600 // varint32: key | |
1601 // bytes*: decode data | |
1602 // | |
1603 // decode data one of two forms: | |
1604 // 1: a \0 followed by the string followed by an \0 | |
1605 // 2: bytecodes to transform an input into the right thing, ending with \0 | |
1606 // | |
1607 // the bytes codes are of the form: | |
1608 // 0xabbccccc | |
1609 // 0x0 (all zeros), end. | |
1610 // a - if set, add an underscore | |
1611 // bb - 00 ccccc bytes as is | |
1612 // bb - 10 ccccc upper first, as is on rest, ccccc byte total | |
1613 // bb - 01 ccccc lower first, as is on rest, ccccc byte total | |
1614 // bb - 11 ccccc all upper, ccccc byte total | |
1615 | |
1616 if (!decodeData || !inputStr) { | |
1617 return nil; | |
1618 } | |
1619 | |
1620 // Find key | |
1621 const uint8_t *scan = decodeData; | |
1622 int32_t numEntries = ReadRawVarint32FromData(&scan); | |
1623 BOOL foundKey = NO; | |
1624 while (!foundKey && (numEntries > 0)) { | |
1625 --numEntries; | |
1626 int32_t dataKey = ReadRawVarint32FromData(&scan); | |
1627 if (dataKey == key) { | |
1628 foundKey = YES; | |
1629 } else { | |
1630 // If it is a inlined string, it will start with \0; if it is bytecode it | |
1631 // will start with a code. So advance one (skipping the inline string | |
1632 // marker), and then loop until reaching the end marker (\0). | |
1633 ++scan; | |
1634 while (*scan != 0) ++scan; | |
1635 // Now move past the end marker. | |
1636 ++scan; | |
1637 } | |
1638 } | |
1639 | |
1640 if (!foundKey) { | |
1641 return nil; | |
1642 } | |
1643 | |
1644 // Decode | |
1645 | |
1646 if (*scan == 0) { | |
1647 // Inline string. Move over the marker, and NSString can take it as | |
1648 // UTF8. | |
1649 ++scan; | |
1650 NSString *result = [NSString stringWithUTF8String:(const char *)scan]; | |
1651 return result; | |
1652 } | |
1653 | |
1654 NSMutableString *result = | |
1655 [NSMutableString stringWithCapacity:[inputStr length]]; | |
1656 | |
1657 const uint8_t kAddUnderscore = 0b10000000; | |
1658 const uint8_t kOpMask = 0b01100000; | |
1659 // const uint8_t kOpAsIs = 0b00000000; | |
1660 const uint8_t kOpFirstUpper = 0b01000000; | |
1661 const uint8_t kOpFirstLower = 0b00100000; | |
1662 const uint8_t kOpAllUpper = 0b01100000; | |
1663 const uint8_t kSegmentLenMask = 0b00011111; | |
1664 | |
1665 NSInteger i = 0; | |
1666 for (; *scan != 0; ++scan) { | |
1667 if (*scan & kAddUnderscore) { | |
1668 [result appendString:@"_"]; | |
1669 } | |
1670 int segmentLen = *scan & kSegmentLenMask; | |
1671 uint8_t decodeOp = *scan & kOpMask; | |
1672 | |
1673 // Do op specific handling of the first character. | |
1674 if (decodeOp == kOpFirstUpper) { | |
1675 unichar c = [inputStr characterAtIndex:i]; | |
1676 [result appendFormat:@"%c", toupper((char)c)]; | |
1677 ++i; | |
1678 --segmentLen; | |
1679 } else if (decodeOp == kOpFirstLower) { | |
1680 unichar c = [inputStr characterAtIndex:i]; | |
1681 [result appendFormat:@"%c", tolower((char)c)]; | |
1682 ++i; | |
1683 --segmentLen; | |
1684 } | |
1685 // else op == kOpAsIs || op == kOpAllUpper | |
1686 | |
1687 // Now pull over the rest of the length for this segment. | |
1688 for (int x = 0; x < segmentLen; ++x) { | |
1689 unichar c = [inputStr characterAtIndex:(i + x)]; | |
1690 if (decodeOp == kOpAllUpper) { | |
1691 [result appendFormat:@"%c", toupper((char)c)]; | |
1692 } else { | |
1693 [result appendFormat:@"%C", c]; | |
1694 } | |
1695 } | |
1696 i += segmentLen; | |
1697 } | |
1698 | |
1699 return result; | |
1700 } | |
1701 | |
1702 #pragma mark - GPBMessageSignatureProtocol | |
1703 | |
1704 // A series of selectors that are used solely to get @encoding values | |
1705 // for them by the dynamic protobuf runtime code. An object using the protocol | |
1706 // needs to be declared for the protocol to be valid at runtime. | |
1707 @interface GPBMessageSignatureProtocol : NSObject<GPBMessageSignatureProtocol> | |
1708 @end | |
1709 @implementation GPBMessageSignatureProtocol | |
1710 @end | |
OLD | NEW |