OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 17 matching lines...) Expand all Loading... |
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | 30 |
31 #import "GPBDescriptor_PackagePrivate.h" | 31 #import "GPBDescriptor_PackagePrivate.h" |
32 | 32 |
33 #import <objc/runtime.h> | 33 #import <objc/runtime.h> |
34 | 34 |
35 #import "GPBUtilities_PackagePrivate.h" | 35 #import "GPBUtilities_PackagePrivate.h" |
36 #import "GPBWireFormat.h" | 36 #import "GPBWireFormat.h" |
37 #import "GPBMessage_PackagePrivate.h" | 37 #import "GPBMessage_PackagePrivate.h" |
38 #import "google/protobuf/Descriptor.pbobjc.h" | |
39 | 38 |
40 // The address of this variable is used as a key for obj_getAssociatedObject. | 39 // The address of this variable is used as a key for obj_getAssociatedObject. |
41 static const char kTextFormatExtraValueKey = 0; | 40 static const char kTextFormatExtraValueKey = 0; |
42 | 41 |
43 // Utility function to generate selectors on the fly. | 42 // Utility function to generate selectors on the fly. |
44 static SEL SelFromStrings(const char *prefix, const char *middle, | 43 static SEL SelFromStrings(const char *prefix, const char *middle, |
45 const char *suffix, BOOL takesArg) { | 44 const char *suffix, BOOL takesArg) { |
46 if (prefix == NULL && suffix == NULL && !takesArg) { | 45 if (prefix == NULL && suffix == NULL && !takesArg) { |
47 return sel_getUid(middle); | 46 return sel_getUid(middle); |
48 } | 47 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 for (GPBFieldDescriptor *fieldDesc in allMessageFields) { | 84 for (GPBFieldDescriptor *fieldDesc in allMessageFields) { |
86 if (fieldDesc->description_->hasIndex == hasIndex) { | 85 if (fieldDesc->description_->hasIndex == hasIndex) { |
87 [result addObject:fieldDesc]; | 86 [result addObject:fieldDesc]; |
88 } | 87 } |
89 } | 88 } |
90 return result; | 89 return result; |
91 } | 90 } |
92 | 91 |
93 @implementation GPBDescriptor { | 92 @implementation GPBDescriptor { |
94 Class messageClass_; | 93 Class messageClass_; |
95 NSArray *enums_; | |
96 GPBFileDescriptor *file_; | 94 GPBFileDescriptor *file_; |
97 BOOL wireFormat_; | 95 BOOL wireFormat_; |
98 } | 96 } |
99 | 97 |
100 @synthesize messageClass = messageClass_; | 98 @synthesize messageClass = messageClass_; |
101 @synthesize fields = fields_; | 99 @synthesize fields = fields_; |
102 @synthesize oneofs = oneofs_; | 100 @synthesize oneofs = oneofs_; |
103 @synthesize enums = enums_; | |
104 @synthesize extensionRanges = extensionRanges_; | 101 @synthesize extensionRanges = extensionRanges_; |
105 @synthesize extensionRangesCount = extensionRangesCount_; | 102 @synthesize extensionRangesCount = extensionRangesCount_; |
106 @synthesize file = file_; | 103 @synthesize file = file_; |
107 @synthesize wireFormat = wireFormat_; | 104 @synthesize wireFormat = wireFormat_; |
108 | 105 |
109 + (instancetype) | 106 + (instancetype) |
110 allocDescriptorForClass:(Class)messageClass | 107 allocDescriptorForClass:(Class)messageClass |
111 rootClass:(Class)rootClass | 108 rootClass:(Class)rootClass |
112 file:(GPBFileDescriptor *)file | 109 file:(GPBFileDescriptor *)file |
113 fields:(GPBMessageFieldDescription *)fieldDescriptions | 110 fields:(void *)fieldDescriptions |
114 fieldCount:(NSUInteger)fieldCount | 111 fieldCount:(uint32_t)fieldCount |
115 oneofs:(GPBMessageOneofDescription *)oneofDescriptions | 112 storageSize:(uint32_t)storageSize |
116 oneofCount:(NSUInteger)oneofCount | 113 flags:(GPBDescriptorInitializationFlags)flags { |
117 enums:(GPBMessageEnumDescription *)enumDescriptions | 114 // The rootClass is no longer used, but it is passed in to ensure it |
118 enumCount:(NSUInteger)enumCount | 115 // was started up during initialization also. |
119 ranges:(const GPBExtensionRange *)ranges | 116 (void)rootClass; |
120 rangeCount:(NSUInteger)rangeCount | |
121 storageSize:(size_t)storageSize | |
122 wireFormat:(BOOL)wireFormat { | |
123 NSMutableArray *fields = nil; | 117 NSMutableArray *fields = nil; |
124 NSMutableArray *oneofs = nil; | |
125 NSMutableArray *enums = nil; | |
126 NSMutableArray *extensionRanges = nil; | |
127 GPBFileSyntax syntax = file.syntax; | 118 GPBFileSyntax syntax = file.syntax; |
128 for (NSUInteger i = 0; i < fieldCount; ++i) { | 119 BOOL fieldsIncludeDefault = |
| 120 (flags & GPBDescriptorInitializationFlag_FieldsWithDefault) != 0; |
| 121 |
| 122 void *desc; |
| 123 for (uint32_t i = 0; i < fieldCount; ++i) { |
129 if (fields == nil) { | 124 if (fields == nil) { |
130 fields = [[NSMutableArray alloc] initWithCapacity:fieldCount]; | 125 fields = [[NSMutableArray alloc] initWithCapacity:fieldCount]; |
131 } | 126 } |
132 GPBFieldDescriptor *fieldDescriptor = [[GPBFieldDescriptor alloc] | 127 // Need correctly typed pointer for array indexing below to work. |
133 initWithFieldDescription:&fieldDescriptions[i] | 128 if (fieldsIncludeDefault) { |
134 rootClass:rootClass | 129 GPBMessageFieldDescriptionWithDefault *fieldDescWithDefault = fieldDescrip
tions; |
135 syntax:syntax]; | 130 desc = &(fieldDescWithDefault[i]); |
| 131 } else { |
| 132 GPBMessageFieldDescription *fieldDesc = fieldDescriptions; |
| 133 desc = &(fieldDesc[i]); |
| 134 } |
| 135 GPBFieldDescriptor *fieldDescriptor = |
| 136 [[GPBFieldDescriptor alloc] initWithFieldDescription:desc |
| 137 includesDefault:fieldsIncludeDefaul
t |
| 138 syntax:syntax]; |
136 [fields addObject:fieldDescriptor]; | 139 [fields addObject:fieldDescriptor]; |
137 [fieldDescriptor release]; | 140 [fieldDescriptor release]; |
138 } | 141 } |
139 for (NSUInteger i = 0; i < oneofCount; ++i) { | |
140 if (oneofs == nil) { | |
141 oneofs = [[NSMutableArray alloc] initWithCapacity:oneofCount]; | |
142 } | |
143 GPBMessageOneofDescription *oneofDescription = &oneofDescriptions[i]; | |
144 NSArray *fieldsForOneof = | |
145 NewFieldsArrayForHasIndex(oneofDescription->index, fields); | |
146 GPBOneofDescriptor *oneofDescriptor = | |
147 [[GPBOneofDescriptor alloc] initWithOneofDescription:oneofDescription | |
148 fields:fieldsForOneof]; | |
149 [oneofs addObject:oneofDescriptor]; | |
150 [oneofDescriptor release]; | |
151 [fieldsForOneof release]; | |
152 } | |
153 for (NSUInteger i = 0; i < enumCount; ++i) { | |
154 if (enums == nil) { | |
155 enums = [[NSMutableArray alloc] initWithCapacity:enumCount]; | |
156 } | |
157 GPBEnumDescriptor *enumDescriptor = | |
158 enumDescriptions[i].enumDescriptorFunc(); | |
159 [enums addObject:enumDescriptor]; | |
160 } | |
161 | 142 |
| 143 BOOL wireFormat = (flags & GPBDescriptorInitializationFlag_WireFormat) != 0; |
162 GPBDescriptor *descriptor = [[self alloc] initWithClass:messageClass | 144 GPBDescriptor *descriptor = [[self alloc] initWithClass:messageClass |
163 file:file | 145 file:file |
164 fields:fields | 146 fields:fields |
165 oneofs:oneofs | |
166 enums:enums | |
167 extensionRanges:ranges | |
168 extensionRangesCount:rangeCount | |
169 storageSize:storageSize | 147 storageSize:storageSize |
170 wireFormat:wireFormat]; | 148 wireFormat:wireFormat]; |
171 [fields release]; | 149 [fields release]; |
172 [oneofs release]; | |
173 [enums release]; | |
174 [extensionRanges release]; | |
175 return descriptor; | |
176 } | |
177 | |
178 + (instancetype) | |
179 allocDescriptorForClass:(Class)messageClass | |
180 rootClass:(Class)rootClass | |
181 file:(GPBFileDescriptor *)file | |
182 fields:(GPBMessageFieldDescription *)fieldDescriptions | |
183 fieldCount:(NSUInteger)fieldCount | |
184 oneofs:(GPBMessageOneofDescription *)oneofDescriptions | |
185 oneofCount:(NSUInteger)oneofCount | |
186 enums:(GPBMessageEnumDescription *)enumDescriptions | |
187 enumCount:(NSUInteger)enumCount | |
188 ranges:(const GPBExtensionRange *)ranges | |
189 rangeCount:(NSUInteger)rangeCount | |
190 storageSize:(size_t)storageSize | |
191 wireFormat:(BOOL)wireFormat | |
192 extraTextFormatInfo:(const char *)extraTextFormatInfo { | |
193 GPBDescriptor *descriptor = [self allocDescriptorForClass:messageClass | |
194 rootClass:rootClass | |
195 file:file | |
196 fields:fieldDescriptions | |
197 fieldCount:fieldCount | |
198 oneofs:oneofDescriptions | |
199 oneofCount:oneofCount | |
200 enums:enumDescriptions | |
201 enumCount:enumCount | |
202 ranges:ranges | |
203 rangeCount:rangeCount | |
204 storageSize:storageSize | |
205 wireFormat:wireFormat]; | |
206 // Extra info is a compile time option, so skip the work if not needed. | |
207 if (extraTextFormatInfo) { | |
208 NSValue *extraInfoValue = [NSValue valueWithPointer:extraTextFormatInfo]; | |
209 for (GPBFieldDescriptor *fieldDescriptor in descriptor->fields_) { | |
210 if (fieldDescriptor->description_->flags & GPBFieldTextFormatNameCustom) { | |
211 objc_setAssociatedObject(fieldDescriptor, &kTextFormatExtraValueKey, | |
212 extraInfoValue, | |
213 OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
214 } | |
215 } | |
216 } | |
217 return descriptor; | 150 return descriptor; |
218 } | 151 } |
219 | 152 |
220 - (instancetype)initWithClass:(Class)messageClass | 153 - (instancetype)initWithClass:(Class)messageClass |
221 file:(GPBFileDescriptor *)file | 154 file:(GPBFileDescriptor *)file |
222 fields:(NSArray *)fields | 155 fields:(NSArray *)fields |
223 oneofs:(NSArray *)oneofs | 156 storageSize:(uint32_t)storageSize |
224 enums:(NSArray *)enums | |
225 extensionRanges:(const GPBExtensionRange *)extensionRanges | |
226 extensionRangesCount:(NSUInteger)extensionRangesCount | |
227 storageSize:(size_t)storageSize | |
228 wireFormat:(BOOL)wireFormat { | 157 wireFormat:(BOOL)wireFormat { |
229 if ((self = [super init])) { | 158 if ((self = [super init])) { |
230 messageClass_ = messageClass; | 159 messageClass_ = messageClass; |
231 file_ = file; | 160 file_ = file; |
232 fields_ = [fields retain]; | 161 fields_ = [fields retain]; |
233 oneofs_ = [oneofs retain]; | |
234 enums_ = [enums retain]; | |
235 extensionRanges_ = extensionRanges; | |
236 extensionRangesCount_ = extensionRangesCount; | |
237 storageSize_ = storageSize; | 162 storageSize_ = storageSize; |
238 wireFormat_ = wireFormat; | 163 wireFormat_ = wireFormat; |
239 } | 164 } |
240 return self; | 165 return self; |
241 } | 166 } |
242 | 167 |
243 - (void)dealloc { | 168 - (void)dealloc { |
244 [fields_ release]; | 169 [fields_ release]; |
245 [oneofs_ release]; | 170 [oneofs_ release]; |
246 [enums_ release]; | |
247 [super dealloc]; | 171 [super dealloc]; |
248 } | 172 } |
249 | 173 |
| 174 - (void)setupOneofs:(const char **)oneofNames |
| 175 count:(uint32_t)count |
| 176 firstHasIndex:(int32_t)firstHasIndex { |
| 177 NSCAssert(firstHasIndex < 0, @"Should always be <0"); |
| 178 NSMutableArray *oneofs = [[NSMutableArray alloc] initWithCapacity:count]; |
| 179 for (uint32_t i = 0, hasIndex = firstHasIndex; i < count; ++i, --hasIndex) { |
| 180 const char *name = oneofNames[i]; |
| 181 NSArray *fieldsForOneof = NewFieldsArrayForHasIndex(hasIndex, fields_); |
| 182 NSCAssert(fieldsForOneof.count > 0, |
| 183 @"No fields for this oneof? (%s:%d)", name, hasIndex); |
| 184 GPBOneofDescriptor *oneofDescriptor = |
| 185 [[GPBOneofDescriptor alloc] initWithName:name fields:fieldsForOneof]; |
| 186 [oneofs addObject:oneofDescriptor]; |
| 187 [oneofDescriptor release]; |
| 188 [fieldsForOneof release]; |
| 189 } |
| 190 oneofs_ = oneofs; |
| 191 } |
| 192 |
| 193 - (void)setupExtraTextInfo:(const char *)extraTextFormatInfo { |
| 194 // Extra info is a compile time option, so skip the work if not needed. |
| 195 if (extraTextFormatInfo) { |
| 196 NSValue *extraInfoValue = [NSValue valueWithPointer:extraTextFormatInfo]; |
| 197 for (GPBFieldDescriptor *fieldDescriptor in fields_) { |
| 198 if (fieldDescriptor->description_->flags & GPBFieldTextFormatNameCustom) { |
| 199 objc_setAssociatedObject(fieldDescriptor, &kTextFormatExtraValueKey, |
| 200 extraInfoValue, |
| 201 OBJC_ASSOCIATION_RETAIN_NONATOMIC); |
| 202 } |
| 203 } |
| 204 } |
| 205 } |
| 206 |
| 207 - (void)setupExtensionRanges:(const GPBExtensionRange *)ranges count:(int32_t)co
unt { |
| 208 extensionRanges_ = ranges; |
| 209 extensionRangesCount_ = count; |
| 210 } |
| 211 |
250 - (NSString *)name { | 212 - (NSString *)name { |
251 return NSStringFromClass(messageClass_); | 213 return NSStringFromClass(messageClass_); |
252 } | 214 } |
253 | 215 |
254 - (id)copyWithZone:(NSZone *)zone { | 216 - (id)copyWithZone:(NSZone *)zone { |
255 #pragma unused(zone) | 217 #pragma unused(zone) |
256 return [self retain]; | 218 return [self retain]; |
257 } | 219 } |
258 | 220 |
259 - (GPBFieldDescriptor *)fieldWithNumber:(uint32_t)fieldNumber { | 221 - (GPBFieldDescriptor *)fieldWithNumber:(uint32_t)fieldNumber { |
(...skipping 16 matching lines...) Expand all Loading... |
276 | 238 |
277 - (GPBOneofDescriptor *)oneofWithName:(NSString *)name { | 239 - (GPBOneofDescriptor *)oneofWithName:(NSString *)name { |
278 for (GPBOneofDescriptor *descriptor in oneofs_) { | 240 for (GPBOneofDescriptor *descriptor in oneofs_) { |
279 if ([descriptor.name isEqual:name]) { | 241 if ([descriptor.name isEqual:name]) { |
280 return descriptor; | 242 return descriptor; |
281 } | 243 } |
282 } | 244 } |
283 return nil; | 245 return nil; |
284 } | 246 } |
285 | 247 |
286 - (GPBEnumDescriptor *)enumWithName:(NSString *)name { | |
287 for (GPBEnumDescriptor *descriptor in enums_) { | |
288 if ([descriptor.name isEqual:name]) { | |
289 return descriptor; | |
290 } | |
291 } | |
292 return nil; | |
293 } | |
294 | |
295 @end | 248 @end |
296 | 249 |
297 @implementation GPBFileDescriptor { | 250 @implementation GPBFileDescriptor { |
298 NSString *package_; | 251 NSString *package_; |
299 GPBFileSyntax syntax_; | 252 GPBFileSyntax syntax_; |
300 } | 253 } |
301 | 254 |
302 @synthesize package = package_; | 255 @synthesize package = package_; |
303 @synthesize syntax = syntax_; | 256 @synthesize syntax = syntax_; |
304 | 257 |
305 - (instancetype)initWithPackage:(NSString *)package | 258 - (instancetype)initWithPackage:(NSString *)package |
306 syntax:(GPBFileSyntax)syntax { | 259 syntax:(GPBFileSyntax)syntax { |
307 self = [super init]; | 260 self = [super init]; |
308 if (self) { | 261 if (self) { |
309 package_ = [package copy]; | 262 package_ = [package copy]; |
310 syntax_ = syntax; | 263 syntax_ = syntax; |
311 } | 264 } |
312 return self; | 265 return self; |
313 } | 266 } |
314 | 267 |
315 @end | 268 @end |
316 | 269 |
317 @implementation GPBOneofDescriptor | 270 @implementation GPBOneofDescriptor |
318 | 271 |
319 @synthesize fields = fields_; | 272 @synthesize fields = fields_; |
320 | 273 |
321 - (instancetype)initWithOneofDescription: | 274 - (instancetype)initWithName:(const char *)name fields:(NSArray *)fields { |
322 (GPBMessageOneofDescription *)oneofDescription | |
323 fields:(NSArray *)fields { | |
324 self = [super init]; | 275 self = [super init]; |
325 if (self) { | 276 if (self) { |
326 NSAssert(oneofDescription->index < 0, @"Should always be <0"); | 277 name_ = name; |
327 oneofDescription_ = oneofDescription; | |
328 fields_ = [fields retain]; | 278 fields_ = [fields retain]; |
329 for (GPBFieldDescriptor *fieldDesc in fields) { | 279 for (GPBFieldDescriptor *fieldDesc in fields) { |
330 fieldDesc->containingOneof_ = self; | 280 fieldDesc->containingOneof_ = self; |
331 } | 281 } |
332 | 282 |
333 caseSel_ = SelFromStrings(NULL, oneofDescription->name, "OneOfCase", NO); | 283 caseSel_ = SelFromStrings(NULL, name, "OneOfCase", NO); |
334 } | 284 } |
335 return self; | 285 return self; |
336 } | 286 } |
337 | 287 |
338 - (void)dealloc { | 288 - (void)dealloc { |
339 [fields_ release]; | 289 [fields_ release]; |
340 [super dealloc]; | 290 [super dealloc]; |
341 } | 291 } |
342 | 292 |
343 - (NSString *)name { | 293 - (NSString *)name { |
344 return @(oneofDescription_->name); | 294 return @(name_); |
345 } | 295 } |
346 | 296 |
347 - (GPBFieldDescriptor *)fieldWithNumber:(uint32_t)fieldNumber { | 297 - (GPBFieldDescriptor *)fieldWithNumber:(uint32_t)fieldNumber { |
348 for (GPBFieldDescriptor *descriptor in fields_) { | 298 for (GPBFieldDescriptor *descriptor in fields_) { |
349 if (GPBFieldNumber(descriptor) == fieldNumber) { | 299 if (GPBFieldNumber(descriptor) == fieldNumber) { |
350 return descriptor; | 300 return descriptor; |
351 } | 301 } |
352 } | 302 } |
353 return nil; | 303 return nil; |
354 } | 304 } |
(...skipping 27 matching lines...) Expand all Loading... |
382 NSCAssert((description->flags & GPBFieldRepeated) != 0, | 332 NSCAssert((description->flags & GPBFieldRepeated) != 0, |
383 @"Only valid on repeated fields"); | 333 @"Only valid on repeated fields"); |
384 GPBWireFormat format = | 334 GPBWireFormat format = |
385 GPBWireFormatForType(description->dataType, | 335 GPBWireFormatForType(description->dataType, |
386 ((description->flags & GPBFieldPacked) == 0)); | 336 ((description->flags & GPBFieldPacked) == 0)); |
387 return GPBWireFormatMakeTag(description->number, format); | 337 return GPBWireFormatMakeTag(description->number, format); |
388 } | 338 } |
389 | 339 |
390 @implementation GPBFieldDescriptor { | 340 @implementation GPBFieldDescriptor { |
391 GPBGenericValue defaultValue_; | 341 GPBGenericValue defaultValue_; |
392 GPBFieldOptions *fieldOptions_; | |
393 | 342 |
394 // Message ivars | 343 // Message ivars |
395 Class msgClass_; | 344 Class msgClass_; |
396 | 345 |
397 // Enum ivars. | 346 // Enum ivars. |
398 // If protos are generated with GenerateEnumDescriptors on then it will | 347 // If protos are generated with GenerateEnumDescriptors on then it will |
399 // be a enumDescriptor, otherwise it will be a enumVerifier. | 348 // be a enumDescriptor, otherwise it will be a enumVerifier. |
400 union { | 349 union { |
401 GPBEnumDescriptor *enumDescriptor_; | 350 GPBEnumDescriptor *enumDescriptor_; |
402 GPBEnumValidationFunc enumVerifier_; | 351 GPBEnumValidationFunc enumVerifier_; |
403 } enumHandling_; | 352 } enumHandling_; |
404 } | 353 } |
405 | 354 |
406 @synthesize fieldOptions = fieldOptions_; | |
407 @synthesize msgClass = msgClass_; | 355 @synthesize msgClass = msgClass_; |
408 @synthesize containingOneof = containingOneof_; | 356 @synthesize containingOneof = containingOneof_; |
409 | 357 |
410 - (instancetype)init { | 358 - (instancetype)init { |
411 // Throw an exception if people attempt to not use the designated initializer. | 359 // Throw an exception if people attempt to not use the designated initializer. |
412 self = [super init]; | 360 self = [super init]; |
413 if (self != nil) { | 361 if (self != nil) { |
414 [self doesNotRecognizeSelector:_cmd]; | 362 [self doesNotRecognizeSelector:_cmd]; |
415 self = nil; | 363 self = nil; |
416 } | 364 } |
417 return self; | 365 return self; |
418 } | 366 } |
419 | 367 |
420 - (instancetype)initWithFieldDescription: | 368 - (instancetype)initWithFieldDescription:(void *)description |
421 (GPBMessageFieldDescription *)description | 369 includesDefault:(BOOL)includesDefault |
422 rootClass:(Class)rootClass | |
423 syntax:(GPBFileSyntax)syntax { | 370 syntax:(GPBFileSyntax)syntax { |
424 if ((self = [super init])) { | 371 if ((self = [super init])) { |
425 description_ = description; | 372 GPBMessageFieldDescription *coreDesc; |
426 getSel_ = sel_getUid(description->name); | 373 if (includesDefault) { |
427 setSel_ = SelFromStrings("set", description->name, NULL, YES); | 374 coreDesc = &(((GPBMessageFieldDescriptionWithDefault *)description)->core)
; |
| 375 } else { |
| 376 coreDesc = description; |
| 377 } |
| 378 description_ = coreDesc; |
| 379 getSel_ = sel_getUid(coreDesc->name); |
| 380 setSel_ = SelFromStrings("set", coreDesc->name, NULL, YES); |
428 | 381 |
429 GPBDataType dataType = description->dataType; | 382 GPBDataType dataType = coreDesc->dataType; |
430 BOOL isMessage = GPBDataTypeIsMessage(dataType); | 383 BOOL isMessage = GPBDataTypeIsMessage(dataType); |
431 BOOL isMapOrArray = GPBFieldIsMapOrArray(self); | 384 BOOL isMapOrArray = GPBFieldIsMapOrArray(self); |
432 | 385 |
433 if (isMapOrArray) { | 386 if (isMapOrArray) { |
434 // map<>/repeated fields get a *Count property (inplace of a has*) to | 387 // map<>/repeated fields get a *Count property (inplace of a has*) to |
435 // support checking if there are any entries without triggering | 388 // support checking if there are any entries without triggering |
436 // autocreation. | 389 // autocreation. |
437 hasOrCountSel_ = SelFromStrings(NULL, description->name, "_Count", NO); | 390 hasOrCountSel_ = SelFromStrings(NULL, coreDesc->name, "_Count", NO); |
438 } else { | 391 } else { |
439 // If there is a positive hasIndex, then: | 392 // If there is a positive hasIndex, then: |
440 // - All fields types for proto2 messages get has* selectors. | 393 // - All fields types for proto2 messages get has* selectors. |
441 // - Only message fields for proto3 messages get has* selectors. | 394 // - Only message fields for proto3 messages get has* selectors. |
442 // Note: the positive check is to handle oneOfs, we can't check | 395 // Note: the positive check is to handle oneOfs, we can't check |
443 // containingOneof_ because it isn't set until after initialization. | 396 // containingOneof_ because it isn't set until after initialization. |
444 if ((description->hasIndex >= 0) && | 397 if ((coreDesc->hasIndex >= 0) && |
445 (description->hasIndex != GPBNoHasBit) && | 398 (coreDesc->hasIndex != GPBNoHasBit) && |
446 ((syntax != GPBFileSyntaxProto3) || isMessage)) { | 399 ((syntax != GPBFileSyntaxProto3) || isMessage)) { |
447 hasOrCountSel_ = SelFromStrings("has", description->name, NULL, NO); | 400 hasOrCountSel_ = SelFromStrings("has", coreDesc->name, NULL, NO); |
448 setHasSel_ = SelFromStrings("setHas", description->name, NULL, YES); | 401 setHasSel_ = SelFromStrings("setHas", coreDesc->name, NULL, YES); |
449 } | 402 } |
450 } | 403 } |
451 | 404 |
452 // Extra type specific data. | 405 // Extra type specific data. |
453 if (isMessage) { | 406 if (isMessage) { |
454 const char *className = description->dataTypeSpecific.className; | 407 const char *className = coreDesc->dataTypeSpecific.className; |
455 msgClass_ = objc_getClass(className); | 408 msgClass_ = objc_getClass(className); |
456 NSAssert(msgClass_, @"Class %s not defined", className); | 409 NSAssert(msgClass_, @"Class %s not defined", className); |
457 } else if (dataType == GPBDataTypeEnum) { | 410 } else if (dataType == GPBDataTypeEnum) { |
458 if ((description_->flags & GPBFieldHasEnumDescriptor) != 0) { | 411 if ((coreDesc->flags & GPBFieldHasEnumDescriptor) != 0) { |
459 enumHandling_.enumDescriptor_ = | 412 enumHandling_.enumDescriptor_ = |
460 description->dataTypeSpecific.enumDescFunc(); | 413 coreDesc->dataTypeSpecific.enumDescFunc(); |
461 } else { | 414 } else { |
462 enumHandling_.enumVerifier_ = | 415 enumHandling_.enumVerifier_ = |
463 description->dataTypeSpecific.enumVerifier; | 416 coreDesc->dataTypeSpecific.enumVerifier; |
464 } | 417 } |
465 } | 418 } |
466 | 419 |
467 // Non map<>/repeated fields can have defaults. | 420 // Non map<>/repeated fields can have defaults in proto2 syntax. |
468 if (!isMapOrArray) { | 421 if (!isMapOrArray && includesDefault) { |
469 defaultValue_ = description->defaultValue; | 422 defaultValue_ = ((GPBMessageFieldDescriptionWithDefault *)description)->de
faultValue; |
470 if (dataType == GPBDataTypeBytes) { | 423 if (dataType == GPBDataTypeBytes) { |
471 // Data stored as a length prefixed (network byte order) c-string in | 424 // Data stored as a length prefixed (network byte order) c-string in |
472 // descriptor structure. | 425 // descriptor structure. |
473 const uint8_t *bytes = (const uint8_t *)defaultValue_.valueData; | 426 const uint8_t *bytes = (const uint8_t *)defaultValue_.valueData; |
474 if (bytes) { | 427 if (bytes) { |
475 uint32_t length = *((uint32_t *)bytes); | 428 uint32_t length = *((uint32_t *)bytes); |
476 length = ntohl(length); | 429 length = ntohl(length); |
477 bytes += sizeof(length); | 430 bytes += sizeof(length); |
478 defaultValue_.valueData = | 431 defaultValue_.valueData = |
479 [[NSData alloc] initWithBytes:bytes length:length]; | 432 [[NSData alloc] initWithBytes:bytes length:length]; |
480 } | 433 } |
481 } | 434 } |
482 } | 435 } |
483 | |
484 // FieldOptions stored as a length prefixed (network byte order) c-escaped | |
485 // string in descriptor records. | |
486 if (description->fieldOptions) { | |
487 uint8_t *optionsBytes = (uint8_t *)description->fieldOptions; | |
488 uint32_t optionsLength = *((uint32_t *)optionsBytes); | |
489 optionsLength = ntohl(optionsLength); | |
490 if (optionsLength > 0) { | |
491 optionsBytes += sizeof(optionsLength); | |
492 NSData *optionsData = [NSData dataWithBytesNoCopy:optionsBytes | |
493 length:optionsLength | |
494 freeWhenDone:NO]; | |
495 GPBExtensionRegistry *registry = [rootClass extensionRegistry]; | |
496 fieldOptions_ = [[GPBFieldOptions parseFromData:optionsData | |
497 extensionRegistry:registry | |
498 error:NULL] retain]; | |
499 } | |
500 } | |
501 } | 436 } |
502 return self; | 437 return self; |
503 } | 438 } |
504 | 439 |
505 - (void)dealloc { | 440 - (void)dealloc { |
506 if (description_->dataType == GPBDataTypeBytes && | 441 if (description_->dataType == GPBDataTypeBytes && |
507 !(description_->flags & GPBFieldRepeated)) { | 442 !(description_->flags & GPBFieldRepeated)) { |
508 [defaultValue_.valueData release]; | 443 [defaultValue_.valueData release]; |
509 } | 444 } |
510 [super dealloc]; | 445 [super dealloc]; |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
659 NSString *result = | 594 NSString *result = |
660 [name stringByReplacingCharactersInRange:NSMakeRange(0, 1) | 595 [name stringByReplacingCharactersInRange:NSMakeRange(0, 1) |
661 withString:firstCharString]; | 596 withString:firstCharString]; |
662 return result; | 597 return result; |
663 } | 598 } |
664 return name; | 599 return name; |
665 | 600 |
666 } else { | 601 } else { |
667 // Undo the CamelCase. | 602 // Undo the CamelCase. |
668 NSMutableString *result = [NSMutableString stringWithCapacity:len]; | 603 NSMutableString *result = [NSMutableString stringWithCapacity:len]; |
669 for (NSUInteger i = 0; i < len; i++) { | 604 for (uint32_t i = 0; i < len; i++) { |
670 unichar c = [name characterAtIndex:i]; | 605 unichar c = [name characterAtIndex:i]; |
671 if (c >= 'A' && c <= 'Z') { | 606 if (c >= 'A' && c <= 'Z') { |
672 if (i > 0) { | 607 if (i > 0) { |
673 [result appendFormat:@"_%C", (unichar)(c - 'A' + 'a')]; | 608 [result appendFormat:@"_%C", (unichar)(c - 'A' + 'a')]; |
674 } else { | 609 } else { |
675 [result appendFormat:@"%C", c]; | 610 [result appendFormat:@"%C", c]; |
676 } | 611 } |
677 } else { | 612 } else { |
678 [result appendFormat:@"%C", c]; | 613 [result appendFormat:@"%C", c]; |
679 } | 614 } |
680 } | 615 } |
681 return result; | 616 return result; |
682 } | 617 } |
683 } | 618 } |
684 | 619 |
685 @end | 620 @end |
686 | 621 |
687 @implementation GPBEnumDescriptor { | 622 @implementation GPBEnumDescriptor { |
688 NSString *name_; | 623 NSString *name_; |
689 GPBMessageEnumValueDescription *valueDescriptions_; | 624 // valueNames_ is a single c string with all of the value names appended |
690 NSUInteger valueDescriptionsCount_; | 625 // together, each null terminated. -calcValueNameOffsets fills in |
| 626 // nameOffsets_ with the offsets to allow quicker access to the individual |
| 627 // names. |
| 628 const char *valueNames_; |
| 629 const int32_t *values_; |
691 GPBEnumValidationFunc enumVerifier_; | 630 GPBEnumValidationFunc enumVerifier_; |
692 const uint8_t *extraTextFormatInfo_; | 631 const uint8_t *extraTextFormatInfo_; |
| 632 uint32_t *nameOffsets_; |
| 633 uint32_t valueCount_; |
693 } | 634 } |
694 | 635 |
695 @synthesize name = name_; | 636 @synthesize name = name_; |
696 @synthesize enumVerifier = enumVerifier_; | 637 @synthesize enumVerifier = enumVerifier_; |
697 | 638 |
698 + (instancetype) | 639 + (instancetype) |
699 allocDescriptorForName:(NSString *)name | 640 allocDescriptorForName:(NSString *)name |
700 values:(GPBMessageEnumValueDescription *)valueDescriptions | 641 valueNames:(const char *)valueNames |
701 valueCount:(NSUInteger)valueCount | 642 values:(const int32_t *)values |
| 643 count:(uint32_t)valueCount |
702 enumVerifier:(GPBEnumValidationFunc)enumVerifier { | 644 enumVerifier:(GPBEnumValidationFunc)enumVerifier { |
703 GPBEnumDescriptor *descriptor = [[self alloc] initWithName:name | 645 GPBEnumDescriptor *descriptor = [[self alloc] initWithName:name |
704 values:valueDescriptions | 646 valueNames:valueNames |
705 valueCount:valueCount | 647 values:values |
| 648 count:valueCount |
706 enumVerifier:enumVerifier]; | 649 enumVerifier:enumVerifier]; |
707 return descriptor; | 650 return descriptor; |
708 } | 651 } |
709 | 652 |
710 + (instancetype) | 653 + (instancetype) |
711 allocDescriptorForName:(NSString *)name | 654 allocDescriptorForName:(NSString *)name |
712 values:(GPBMessageEnumValueDescription *)valueDescriptions | 655 valueNames:(const char *)valueNames |
713 valueCount:(NSUInteger)valueCount | 656 values:(const int32_t *)values |
| 657 count:(uint32_t)valueCount |
714 enumVerifier:(GPBEnumValidationFunc)enumVerifier | 658 enumVerifier:(GPBEnumValidationFunc)enumVerifier |
715 extraTextFormatInfo:(const char *)extraTextFormatInfo { | 659 extraTextFormatInfo:(const char *)extraTextFormatInfo { |
716 // Call the common case. | 660 // Call the common case. |
717 GPBEnumDescriptor *descriptor = [self allocDescriptorForName:name | 661 GPBEnumDescriptor *descriptor = [self allocDescriptorForName:name |
718 values:valueDescriptions | 662 valueNames:valueNames |
719 valueCount:valueCount | 663 values:values |
| 664 count:valueCount |
720 enumVerifier:enumVerifier]; | 665 enumVerifier:enumVerifier]; |
721 // Set the extra info. | 666 // Set the extra info. |
722 descriptor->extraTextFormatInfo_ = (const uint8_t *)extraTextFormatInfo; | 667 descriptor->extraTextFormatInfo_ = (const uint8_t *)extraTextFormatInfo; |
723 return descriptor; | 668 return descriptor; |
724 } | 669 } |
725 | 670 |
726 - (instancetype)initWithName:(NSString *)name | 671 - (instancetype)initWithName:(NSString *)name |
727 values:(GPBMessageEnumValueDescription *)valueDescriptions | 672 valueNames:(const char *)valueNames |
728 valueCount:(NSUInteger)valueCount | 673 values:(const int32_t *)values |
| 674 count:(uint32_t)valueCount |
729 enumVerifier:(GPBEnumValidationFunc)enumVerifier { | 675 enumVerifier:(GPBEnumValidationFunc)enumVerifier { |
730 if ((self = [super init])) { | 676 if ((self = [super init])) { |
731 name_ = [name copy]; | 677 name_ = [name copy]; |
732 valueDescriptions_ = valueDescriptions; | 678 valueNames_ = valueNames; |
733 valueDescriptionsCount_ = valueCount; | 679 values_ = values; |
| 680 valueCount_ = valueCount; |
734 enumVerifier_ = enumVerifier; | 681 enumVerifier_ = enumVerifier; |
735 } | 682 } |
736 return self; | 683 return self; |
737 } | 684 } |
738 | 685 |
| 686 - (void)dealloc { |
| 687 [name_ release]; |
| 688 if (nameOffsets_) free(nameOffsets_); |
| 689 [super dealloc]; |
| 690 } |
| 691 |
| 692 - (void)calcValueNameOffsets { |
| 693 @synchronized(self) { |
| 694 if (nameOffsets_ != NULL) { |
| 695 return; |
| 696 } |
| 697 uint32_t *offsets = malloc(valueCount_ * sizeof(uint32_t)); |
| 698 const char *scan = valueNames_; |
| 699 for (uint32_t i = 0; i < valueCount_; ++i) { |
| 700 offsets[i] = (uint32_t)(scan - valueNames_); |
| 701 while (*scan != '\0') ++scan; |
| 702 ++scan; // Step over the null. |
| 703 } |
| 704 nameOffsets_ = offsets; |
| 705 } |
| 706 } |
| 707 |
739 - (NSString *)enumNameForValue:(int32_t)number { | 708 - (NSString *)enumNameForValue:(int32_t)number { |
740 for (NSUInteger i = 0; i < valueDescriptionsCount_; ++i) { | 709 if (nameOffsets_ == NULL) [self calcValueNameOffsets]; |
741 GPBMessageEnumValueDescription *scan = &valueDescriptions_[i]; | 710 |
742 if ((scan->number == number) && (scan->name != NULL)) { | 711 for (uint32_t i = 0; i < valueCount_; ++i) { |
743 NSString *fullName = | 712 if (values_[i] == number) { |
744 [NSString stringWithFormat:@"%@_%s", name_, scan->name]; | 713 const char *valueName = valueNames_ + nameOffsets_[i]; |
| 714 NSString *fullName = [NSString stringWithFormat:@"%@_%s", name_, valueName
]; |
745 return fullName; | 715 return fullName; |
746 } | 716 } |
747 } | 717 } |
748 return nil; | 718 return nil; |
749 } | 719 } |
750 | 720 |
751 - (BOOL)getValue:(int32_t *)outValue forEnumName:(NSString *)name { | 721 - (BOOL)getValue:(int32_t *)outValue forEnumName:(NSString *)name { |
752 // Must have the prefix. | 722 // Must have the prefix. |
753 NSUInteger prefixLen = name_.length + 1; | 723 NSUInteger prefixLen = name_.length + 1; |
754 if ((name.length <= prefixLen) || ![name hasPrefix:name_] || | 724 if ((name.length <= prefixLen) || ![name hasPrefix:name_] || |
755 ([name characterAtIndex:prefixLen - 1] != '_')) { | 725 ([name characterAtIndex:prefixLen - 1] != '_')) { |
756 return NO; | 726 return NO; |
757 } | 727 } |
758 | 728 |
759 // Skip over the prefix. | 729 // Skip over the prefix. |
760 const char *nameAsCStr = [name UTF8String]; | 730 const char *nameAsCStr = [name UTF8String]; |
761 nameAsCStr += prefixLen; | 731 nameAsCStr += prefixLen; |
762 | 732 |
| 733 if (nameOffsets_ == NULL) [self calcValueNameOffsets]; |
| 734 |
763 // Find it. | 735 // Find it. |
764 for (NSUInteger i = 0; i < valueDescriptionsCount_; ++i) { | 736 for (uint32_t i = 0; i < valueCount_; ++i) { |
765 GPBMessageEnumValueDescription *scan = &valueDescriptions_[i]; | 737 const char *valueName = valueNames_ + nameOffsets_[i]; |
766 if ((scan->name != NULL) && (strcmp(nameAsCStr, scan->name) == 0)) { | 738 if (strcmp(nameAsCStr, valueName) == 0) { |
767 if (outValue) { | 739 if (outValue) { |
768 *outValue = scan->number; | 740 *outValue = values_[i]; |
769 } | 741 } |
770 return YES; | 742 return YES; |
771 } | 743 } |
772 } | 744 } |
773 return NO; | 745 return NO; |
774 } | 746 } |
775 | 747 |
776 - (void)dealloc { | 748 - (NSString *)textFormatNameForValue:(int32_t)number { |
777 [name_ release]; | 749 if (nameOffsets_ == NULL) [self calcValueNameOffsets]; |
778 [super dealloc]; | |
779 } | |
780 | 750 |
781 - (NSString *)textFormatNameForValue:(int32_t)number { | |
782 // Find the EnumValue descriptor and its index. | 751 // Find the EnumValue descriptor and its index. |
783 GPBMessageEnumValueDescription *valueDescriptor = NULL; | 752 BOOL foundIt = NO; |
784 NSUInteger valueDescriptorIndex; | 753 uint32_t valueDescriptorIndex; |
785 for (valueDescriptorIndex = 0; valueDescriptorIndex < valueDescriptionsCount_; | 754 for (valueDescriptorIndex = 0; valueDescriptorIndex < valueCount_; |
786 ++valueDescriptorIndex) { | 755 ++valueDescriptorIndex) { |
787 GPBMessageEnumValueDescription *scan = | 756 if (values_[valueDescriptorIndex] == number) { |
788 &valueDescriptions_[valueDescriptorIndex]; | 757 foundIt = YES; |
789 if (scan->number == number) { | |
790 valueDescriptor = scan; | |
791 break; | 758 break; |
792 } | 759 } |
793 } | 760 } |
794 | 761 |
795 // If we didn't find it, or names were disable at proto compile time, nothing | 762 if (!foundIt) { |
796 // we can do. | |
797 if (!valueDescriptor || !valueDescriptor->name) { | |
798 return nil; | 763 return nil; |
799 } | 764 } |
800 | 765 |
801 NSString *result = nil; | 766 NSString *result = nil; |
802 // Naming adds an underscore between enum name and value name, skip that also. | 767 // Naming adds an underscore between enum name and value name, skip that also. |
803 NSString *shortName = @(valueDescriptor->name); | 768 const char *valueName = valueNames_ + nameOffsets_[valueDescriptorIndex]; |
| 769 NSString *shortName = @(valueName); |
804 | 770 |
805 // See if it is in the map of special format handling. | 771 // See if it is in the map of special format handling. |
806 if (extraTextFormatInfo_) { | 772 if (extraTextFormatInfo_) { |
807 result = GPBDecodeTextFormatName(extraTextFormatInfo_, | 773 result = GPBDecodeTextFormatName(extraTextFormatInfo_, |
808 (int32_t)valueDescriptorIndex, shortName); | 774 (int32_t)valueDescriptorIndex, shortName); |
809 } | 775 } |
810 // Logic here needs to match what objectivec_enum.cc does in the proto | 776 // Logic here needs to match what objectivec_enum.cc does in the proto |
811 // compiler. | 777 // compiler. |
812 if (result == nil) { | 778 if (result == nil) { |
813 NSUInteger len = [shortName length]; | 779 NSUInteger len = [shortName length]; |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
988 if (selfNumber < otherNumber) { | 954 if (selfNumber < otherNumber) { |
989 return NSOrderedAscending; | 955 return NSOrderedAscending; |
990 } else if (selfNumber == otherNumber) { | 956 } else if (selfNumber == otherNumber) { |
991 return NSOrderedSame; | 957 return NSOrderedSame; |
992 } else { | 958 } else { |
993 return NSOrderedDescending; | 959 return NSOrderedDescending; |
994 } | 960 } |
995 } | 961 } |
996 | 962 |
997 @end | 963 @end |
OLD | NEW |