aboutsummaryrefslogtreecommitdiffhomepage
path: root/objectivec/GPBDescriptor.m
diff options
context:
space:
mode:
Diffstat (limited to 'objectivec/GPBDescriptor.m')
-rw-r--r--objectivec/GPBDescriptor.m536
1 files changed, 329 insertions, 207 deletions
diff --git a/objectivec/GPBDescriptor.m b/objectivec/GPBDescriptor.m
index bae9187e..ad46ef4f 100644
--- a/objectivec/GPBDescriptor.m
+++ b/objectivec/GPBDescriptor.m
@@ -35,10 +35,17 @@
#import "GPBUtilities_PackagePrivate.h"
#import "GPBWireFormat.h"
#import "GPBMessage_PackagePrivate.h"
-#import "google/protobuf/Descriptor.pbobjc.h"
-// The address of this variable is used as a key for obj_getAssociatedObject.
+// Direct access is use for speed, to avoid even internally declaring things
+// read/write, etc. The warning is enabled in the project to ensure code calling
+// protos can turn on -Wdirect-ivar-access without issues.
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wdirect-ivar-access"
+
+// The addresses of these variables are used as keys for objc_getAssociatedObject.
static const char kTextFormatExtraValueKey = 0;
+static const char kParentClassNameValueKey = 0;
+static const char kClassNameSuffixKey = 0;
// Utility function to generate selectors on the fly.
static SEL SelFromStrings(const char *prefix, const char *middle,
@@ -92,7 +99,6 @@ static NSArray *NewFieldsArrayForHasIndex(int hasIndex,
@implementation GPBDescriptor {
Class messageClass_;
- NSArray *enums_;
GPBFileDescriptor *file_;
BOOL wireFormat_;
}
@@ -100,7 +106,6 @@ static NSArray *NewFieldsArrayForHasIndex(int hasIndex,
@synthesize messageClass = messageClass_;
@synthesize fields = fields_;
@synthesize oneofs = oneofs_;
-@synthesize enums = enums_;
@synthesize extensionRanges = extensionRanges_;
@synthesize extensionRangesCount = extensionRangesCount_;
@synthesize file = file_;
@@ -110,130 +115,58 @@ static NSArray *NewFieldsArrayForHasIndex(int hasIndex,
allocDescriptorForClass:(Class)messageClass
rootClass:(Class)rootClass
file:(GPBFileDescriptor *)file
- fields:(GPBMessageFieldDescription *)fieldDescriptions
- fieldCount:(NSUInteger)fieldCount
- oneofs:(GPBMessageOneofDescription *)oneofDescriptions
- oneofCount:(NSUInteger)oneofCount
- enums:(GPBMessageEnumDescription *)enumDescriptions
- enumCount:(NSUInteger)enumCount
- ranges:(const GPBExtensionRange *)ranges
- rangeCount:(NSUInteger)rangeCount
- storageSize:(size_t)storageSize
- wireFormat:(BOOL)wireFormat {
+ fields:(void *)fieldDescriptions
+ fieldCount:(uint32_t)fieldCount
+ storageSize:(uint32_t)storageSize
+ flags:(GPBDescriptorInitializationFlags)flags {
+ // The rootClass is no longer used, but it is passed in to ensure it
+ // was started up during initialization also.
+ (void)rootClass;
NSMutableArray *fields = nil;
- NSMutableArray *oneofs = nil;
- NSMutableArray *enums = nil;
- NSMutableArray *extensionRanges = nil;
GPBFileSyntax syntax = file.syntax;
- for (NSUInteger i = 0; i < fieldCount; ++i) {
+ BOOL fieldsIncludeDefault =
+ (flags & GPBDescriptorInitializationFlag_FieldsWithDefault) != 0;
+
+ void *desc;
+ for (uint32_t i = 0; i < fieldCount; ++i) {
if (fields == nil) {
fields = [[NSMutableArray alloc] initWithCapacity:fieldCount];
}
- GPBFieldDescriptor *fieldDescriptor = [[GPBFieldDescriptor alloc]
- initWithFieldDescription:&fieldDescriptions[i]
- rootClass:rootClass
- syntax:syntax];
+ // Need correctly typed pointer for array indexing below to work.
+ if (fieldsIncludeDefault) {
+ GPBMessageFieldDescriptionWithDefault *fieldDescWithDefault = fieldDescriptions;
+ desc = &(fieldDescWithDefault[i]);
+ } else {
+ GPBMessageFieldDescription *fieldDesc = fieldDescriptions;
+ desc = &(fieldDesc[i]);
+ }
+ GPBFieldDescriptor *fieldDescriptor =
+ [[GPBFieldDescriptor alloc] initWithFieldDescription:desc
+ includesDefault:fieldsIncludeDefault
+ syntax:syntax];
[fields addObject:fieldDescriptor];
[fieldDescriptor release];
}
- for (NSUInteger i = 0; i < oneofCount; ++i) {
- if (oneofs == nil) {
- oneofs = [[NSMutableArray alloc] initWithCapacity:oneofCount];
- }
- GPBMessageOneofDescription *oneofDescription = &oneofDescriptions[i];
- NSArray *fieldsForOneof =
- NewFieldsArrayForHasIndex(oneofDescription->index, fields);
- GPBOneofDescriptor *oneofDescriptor =
- [[GPBOneofDescriptor alloc] initWithOneofDescription:oneofDescription
- fields:fieldsForOneof];
- [oneofs addObject:oneofDescriptor];
- [oneofDescriptor release];
- [fieldsForOneof release];
- }
- for (NSUInteger i = 0; i < enumCount; ++i) {
- if (enums == nil) {
- enums = [[NSMutableArray alloc] initWithCapacity:enumCount];
- }
- GPBEnumDescriptor *enumDescriptor =
- enumDescriptions[i].enumDescriptorFunc();
- [enums addObject:enumDescriptor];
- }
+ BOOL wireFormat = (flags & GPBDescriptorInitializationFlag_WireFormat) != 0;
GPBDescriptor *descriptor = [[self alloc] initWithClass:messageClass
file:file
fields:fields
- oneofs:oneofs
- enums:enums
- extensionRanges:ranges
- extensionRangesCount:rangeCount
storageSize:storageSize
wireFormat:wireFormat];
[fields release];
- [oneofs release];
- [enums release];
- [extensionRanges release];
- return descriptor;
-}
-
-+ (instancetype)
- allocDescriptorForClass:(Class)messageClass
- rootClass:(Class)rootClass
- file:(GPBFileDescriptor *)file
- fields:(GPBMessageFieldDescription *)fieldDescriptions
- fieldCount:(NSUInteger)fieldCount
- oneofs:(GPBMessageOneofDescription *)oneofDescriptions
- oneofCount:(NSUInteger)oneofCount
- enums:(GPBMessageEnumDescription *)enumDescriptions
- enumCount:(NSUInteger)enumCount
- ranges:(const GPBExtensionRange *)ranges
- rangeCount:(NSUInteger)rangeCount
- storageSize:(size_t)storageSize
- wireFormat:(BOOL)wireFormat
- extraTextFormatInfo:(const char *)extraTextFormatInfo {
- GPBDescriptor *descriptor = [self allocDescriptorForClass:messageClass
- rootClass:rootClass
- file:file
- fields:fieldDescriptions
- fieldCount:fieldCount
- oneofs:oneofDescriptions
- oneofCount:oneofCount
- enums:enumDescriptions
- enumCount:enumCount
- ranges:ranges
- rangeCount:rangeCount
- storageSize:storageSize
- wireFormat:wireFormat];
- // Extra info is a compile time option, so skip the work if not needed.
- if (extraTextFormatInfo) {
- NSValue *extraInfoValue = [NSValue valueWithPointer:extraTextFormatInfo];
- for (GPBFieldDescriptor *fieldDescriptor in descriptor->fields_) {
- if (fieldDescriptor->description_->flags & GPBFieldTextFormatNameCustom) {
- objc_setAssociatedObject(fieldDescriptor, &kTextFormatExtraValueKey,
- extraInfoValue,
- OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- }
- }
- }
return descriptor;
}
- (instancetype)initWithClass:(Class)messageClass
file:(GPBFileDescriptor *)file
fields:(NSArray *)fields
- oneofs:(NSArray *)oneofs
- enums:(NSArray *)enums
- extensionRanges:(const GPBExtensionRange *)extensionRanges
- extensionRangesCount:(NSUInteger)extensionRangesCount
- storageSize:(size_t)storageSize
+ storageSize:(uint32_t)storageSize
wireFormat:(BOOL)wireFormat {
if ((self = [super init])) {
messageClass_ = messageClass;
file_ = file;
fields_ = [fields retain];
- oneofs_ = [oneofs retain];
- enums_ = [enums retain];
- extensionRanges_ = extensionRanges;
- extensionRangesCount_ = extensionRangesCount;
storageSize_ = storageSize;
wireFormat_ = wireFormat;
}
@@ -243,14 +176,143 @@ static NSArray *NewFieldsArrayForHasIndex(int hasIndex,
- (void)dealloc {
[fields_ release];
[oneofs_ release];
- [enums_ release];
[super dealloc];
}
+- (void)setupOneofs:(const char **)oneofNames
+ count:(uint32_t)count
+ firstHasIndex:(int32_t)firstHasIndex {
+ NSCAssert(firstHasIndex < 0, @"Should always be <0");
+ NSMutableArray *oneofs = [[NSMutableArray alloc] initWithCapacity:count];
+ for (uint32_t i = 0, hasIndex = firstHasIndex; i < count; ++i, --hasIndex) {
+ const char *name = oneofNames[i];
+ NSArray *fieldsForOneof = NewFieldsArrayForHasIndex(hasIndex, fields_);
+ NSCAssert(fieldsForOneof.count > 0,
+ @"No fields for this oneof? (%s:%d)", name, hasIndex);
+ GPBOneofDescriptor *oneofDescriptor =
+ [[GPBOneofDescriptor alloc] initWithName:name fields:fieldsForOneof];
+ [oneofs addObject:oneofDescriptor];
+ [oneofDescriptor release];
+ [fieldsForOneof release];
+ }
+ oneofs_ = oneofs;
+}
+
+- (void)setupExtraTextInfo:(const char *)extraTextFormatInfo {
+ // Extra info is a compile time option, so skip the work if not needed.
+ if (extraTextFormatInfo) {
+ NSValue *extraInfoValue = [NSValue valueWithPointer:extraTextFormatInfo];
+ for (GPBFieldDescriptor *fieldDescriptor in fields_) {
+ if (fieldDescriptor->description_->flags & GPBFieldTextFormatNameCustom) {
+ objc_setAssociatedObject(fieldDescriptor, &kTextFormatExtraValueKey,
+ extraInfoValue,
+ OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ }
+ }
+ }
+}
+
+- (void)setupExtensionRanges:(const GPBExtensionRange *)ranges count:(int32_t)count {
+ extensionRanges_ = ranges;
+ extensionRangesCount_ = count;
+}
+
+- (void)setupContainingMessageClassName:(const char *)msgClassName {
+ // Note: Only fetch the class here, can't send messages to it because
+ // that could cause cycles back to this class within +initialize if
+ // two messages have each other in fields (i.e. - they build a graph).
+ NSAssert(objc_getClass(msgClassName), @"Class %s not defined", msgClassName);
+ NSValue *parentNameValue = [NSValue valueWithPointer:msgClassName];
+ objc_setAssociatedObject(self, &kParentClassNameValueKey,
+ parentNameValue,
+ OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+}
+
+- (void)setupMessageClassNameSuffix:(NSString *)suffix {
+ if (suffix.length) {
+ objc_setAssociatedObject(self, &kClassNameSuffixKey,
+ suffix,
+ OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ }
+}
+
- (NSString *)name {
return NSStringFromClass(messageClass_);
}
+- (GPBDescriptor *)containingType {
+ NSValue *parentNameValue =
+ objc_getAssociatedObject(self, &kParentClassNameValueKey);
+ if (!parentNameValue) {
+ return nil;
+ }
+ const char *parentName = [parentNameValue pointerValue];
+ Class parentClass = objc_getClass(parentName);
+ NSAssert(parentClass, @"Class %s not defined", parentName);
+ return [parentClass descriptor];
+}
+
+- (NSString *)fullName {
+ NSString *className = NSStringFromClass(self.messageClass);
+ GPBFileDescriptor *file = self.file;
+ NSString *objcPrefix = file.objcPrefix;
+ if (objcPrefix && ![className hasPrefix:objcPrefix]) {
+ NSAssert(0,
+ @"Class didn't have correct prefix? (%@ - %@)",
+ className, objcPrefix);
+ return nil;
+ }
+ GPBDescriptor *parent = self.containingType;
+
+ NSString *name = nil;
+ if (parent) {
+ NSString *parentClassName = NSStringFromClass(parent.messageClass);
+ // The generator will add _Class to avoid reserved words, drop it.
+ NSString *suffix = objc_getAssociatedObject(parent, &kClassNameSuffixKey);
+ if (suffix) {
+ if (![parentClassName hasSuffix:suffix]) {
+ NSAssert(0,
+ @"ParentMessage class didn't have correct suffix? (%@ - %@)",
+ className, suffix);
+ return nil;
+ }
+ parentClassName =
+ [parentClassName substringToIndex:(parentClassName.length - suffix.length)];
+ }
+ NSString *parentPrefix = [parentClassName stringByAppendingString:@"_"];
+ if (![className hasPrefix:parentPrefix]) {
+ NSAssert(0,
+ @"Class didn't have the correct parent name prefix? (%@ - %@)",
+ parentPrefix, className);
+ return nil;
+ }
+ name = [className substringFromIndex:parentPrefix.length];
+ } else {
+ name = [className substringFromIndex:objcPrefix.length];
+ }
+
+ // The generator will add _Class to avoid reserved words, drop it.
+ NSString *suffix = objc_getAssociatedObject(self, &kClassNameSuffixKey);
+ if (suffix) {
+ if (![name hasSuffix:suffix]) {
+ NSAssert(0,
+ @"Message class didn't have correct suffix? (%@ - %@)",
+ name, suffix);
+ return nil;
+ }
+ name = [name substringToIndex:(name.length - suffix.length)];
+ }
+
+ NSString *prefix = (parent != nil ? parent.fullName : file.package);
+ NSString *result;
+ if (prefix.length > 0) {
+ result = [NSString stringWithFormat:@"%@.%@", prefix, name];
+ } else {
+ result = name;
+ }
+ return result;
+}
+
- (id)copyWithZone:(NSZone *)zone {
#pragma unused(zone)
return [self retain];
@@ -283,26 +345,31 @@ static NSArray *NewFieldsArrayForHasIndex(int hasIndex,
return nil;
}
-- (GPBEnumDescriptor *)enumWithName:(NSString *)name {
- for (GPBEnumDescriptor *descriptor in enums_) {
- if ([descriptor.name isEqual:name]) {
- return descriptor;
- }
- }
- return nil;
-}
-
@end
@implementation GPBFileDescriptor {
NSString *package_;
+ NSString *objcPrefix_;
GPBFileSyntax syntax_;
}
@synthesize package = package_;
+@synthesize objcPrefix = objcPrefix_;
@synthesize syntax = syntax_;
- (instancetype)initWithPackage:(NSString *)package
+ objcPrefix:(NSString *)objcPrefix
+ syntax:(GPBFileSyntax)syntax {
+ self = [super init];
+ if (self) {
+ package_ = [package copy];
+ objcPrefix_ = [objcPrefix copy];
+ syntax_ = syntax;
+ }
+ return self;
+}
+
+- (instancetype)initWithPackage:(NSString *)package
syntax:(GPBFileSyntax)syntax {
self = [super init];
if (self) {
@@ -312,25 +379,28 @@ static NSArray *NewFieldsArrayForHasIndex(int hasIndex,
return self;
}
+- (void)dealloc {
+ [package_ release];
+ [objcPrefix_ release];
+ [super dealloc];
+}
+
@end
@implementation GPBOneofDescriptor
@synthesize fields = fields_;
-- (instancetype)initWithOneofDescription:
- (GPBMessageOneofDescription *)oneofDescription
- fields:(NSArray *)fields {
+- (instancetype)initWithName:(const char *)name fields:(NSArray *)fields {
self = [super init];
if (self) {
- NSAssert(oneofDescription->index < 0, @"Should always be <0");
- oneofDescription_ = oneofDescription;
+ name_ = name;
fields_ = [fields retain];
for (GPBFieldDescriptor *fieldDesc in fields) {
fieldDesc->containingOneof_ = self;
}
- caseSel_ = SelFromStrings(NULL, oneofDescription->name, "OneOfCase", NO);
+ caseSel_ = SelFromStrings(NULL, name, "OneOfCase", NO);
}
return self;
}
@@ -341,7 +411,7 @@ static NSArray *NewFieldsArrayForHasIndex(int hasIndex,
}
- (NSString *)name {
- return @(oneofDescription_->name);
+ return (NSString * _Nonnull)@(name_);
}
- (GPBFieldDescriptor *)fieldWithNumber:(uint32_t)fieldNumber {
@@ -389,7 +459,6 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
@implementation GPBFieldDescriptor {
GPBGenericValue defaultValue_;
- GPBFieldOptions *fieldOptions_;
// Message ivars
Class msgClass_;
@@ -403,7 +472,6 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
} enumHandling_;
}
-@synthesize fieldOptions = fieldOptions_;
@synthesize msgClass = msgClass_;
@synthesize containingOneof = containingOneof_;
@@ -417,16 +485,21 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
return self;
}
-- (instancetype)initWithFieldDescription:
- (GPBMessageFieldDescription *)description
- rootClass:(Class)rootClass
+- (instancetype)initWithFieldDescription:(void *)description
+ includesDefault:(BOOL)includesDefault
syntax:(GPBFileSyntax)syntax {
if ((self = [super init])) {
- description_ = description;
- getSel_ = sel_getUid(description->name);
- setSel_ = SelFromStrings("set", description->name, NULL, YES);
+ GPBMessageFieldDescription *coreDesc;
+ if (includesDefault) {
+ coreDesc = &(((GPBMessageFieldDescriptionWithDefault *)description)->core);
+ } else {
+ coreDesc = description;
+ }
+ description_ = coreDesc;
+ getSel_ = sel_getUid(coreDesc->name);
+ setSel_ = SelFromStrings("set", coreDesc->name, NULL, YES);
- GPBDataType dataType = description->dataType;
+ GPBDataType dataType = coreDesc->dataType;
BOOL isMessage = GPBDataTypeIsMessage(dataType);
BOOL isMapOrArray = GPBFieldIsMapOrArray(self);
@@ -434,45 +507,49 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
// map<>/repeated fields get a *Count property (inplace of a has*) to
// support checking if there are any entries without triggering
// autocreation.
- hasOrCountSel_ = SelFromStrings(NULL, description->name, "_Count", NO);
+ hasOrCountSel_ = SelFromStrings(NULL, coreDesc->name, "_Count", NO);
} else {
// If there is a positive hasIndex, then:
// - All fields types for proto2 messages get has* selectors.
// - Only message fields for proto3 messages get has* selectors.
// Note: the positive check is to handle oneOfs, we can't check
// containingOneof_ because it isn't set until after initialization.
- if ((description->hasIndex >= 0) &&
- (description->hasIndex != GPBNoHasBit) &&
+ if ((coreDesc->hasIndex >= 0) &&
+ (coreDesc->hasIndex != GPBNoHasBit) &&
((syntax != GPBFileSyntaxProto3) || isMessage)) {
- hasOrCountSel_ = SelFromStrings("has", description->name, NULL, NO);
- setHasSel_ = SelFromStrings("setHas", description->name, NULL, YES);
+ hasOrCountSel_ = SelFromStrings("has", coreDesc->name, NULL, NO);
+ setHasSel_ = SelFromStrings("setHas", coreDesc->name, NULL, YES);
}
}
// Extra type specific data.
if (isMessage) {
- const char *className = description->dataTypeSpecific.className;
+ const char *className = coreDesc->dataTypeSpecific.className;
+ // Note: Only fetch the class here, can't send messages to it because
+ // that could cause cycles back to this class within +initialize if
+ // two messages have each other in fields (i.e. - they build a graph).
msgClass_ = objc_getClass(className);
NSAssert(msgClass_, @"Class %s not defined", className);
} else if (dataType == GPBDataTypeEnum) {
- if ((description_->flags & GPBFieldHasEnumDescriptor) != 0) {
+ if ((coreDesc->flags & GPBFieldHasEnumDescriptor) != 0) {
enumHandling_.enumDescriptor_ =
- description->dataTypeSpecific.enumDescFunc();
+ coreDesc->dataTypeSpecific.enumDescFunc();
} else {
enumHandling_.enumVerifier_ =
- description->dataTypeSpecific.enumVerifier;
+ coreDesc->dataTypeSpecific.enumVerifier;
}
}
- // Non map<>/repeated fields can have defaults.
- if (!isMapOrArray) {
- defaultValue_ = description->defaultValue;
+ // Non map<>/repeated fields can have defaults in proto2 syntax.
+ if (!isMapOrArray && includesDefault) {
+ defaultValue_ = ((GPBMessageFieldDescriptionWithDefault *)description)->defaultValue;
if (dataType == GPBDataTypeBytes) {
// Data stored as a length prefixed (network byte order) c-string in
// descriptor structure.
const uint8_t *bytes = (const uint8_t *)defaultValue_.valueData;
if (bytes) {
- uint32_t length = *((uint32_t *)bytes);
+ uint32_t length;
+ memcpy(&length, bytes, sizeof(length));
length = ntohl(length);
bytes += sizeof(length);
defaultValue_.valueData =
@@ -480,24 +557,6 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
}
}
}
-
- // FieldOptions stored as a length prefixed (network byte order) c-escaped
- // string in descriptor records.
- if (description->fieldOptions) {
- uint8_t *optionsBytes = (uint8_t *)description->fieldOptions;
- uint32_t optionsLength = *((uint32_t *)optionsBytes);
- optionsLength = ntohl(optionsLength);
- if (optionsLength > 0) {
- optionsBytes += sizeof(optionsLength);
- NSData *optionsData = [NSData dataWithBytesNoCopy:optionsBytes
- length:optionsLength
- freeWhenDone:NO];
- GPBExtensionRegistry *registry = [rootClass extensionRegistry];
- fieldOptions_ = [[GPBFieldOptions parseFromData:optionsData
- extensionRegistry:registry
- error:NULL] retain];
- }
- }
}
return self;
}
@@ -523,7 +582,7 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
}
- (NSString *)name {
- return @(description_->name);
+ return (NSString * _Nonnull)@(description_->name);
}
- (BOOL)isRequired {
@@ -666,7 +725,7 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
} else {
// Undo the CamelCase.
NSMutableString *result = [NSMutableString stringWithCapacity:len];
- for (NSUInteger i = 0; i < len; i++) {
+ for (uint32_t i = 0; i < len; i++) {
unichar c = [name characterAtIndex:i];
if (c >= 'A' && c <= 'Z') {
if (i > 0) {
@@ -686,10 +745,16 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
@implementation GPBEnumDescriptor {
NSString *name_;
- GPBMessageEnumValueDescription *valueDescriptions_;
- NSUInteger valueDescriptionsCount_;
+ // valueNames_ is a single c string with all of the value names appended
+ // together, each null terminated. -calcValueNameOffsets fills in
+ // nameOffsets_ with the offsets to allow quicker access to the individual
+ // names.
+ const char *valueNames_;
+ const int32_t *values_;
GPBEnumValidationFunc enumVerifier_;
const uint8_t *extraTextFormatInfo_;
+ uint32_t *nameOffsets_;
+ uint32_t valueCount_;
}
@synthesize name = name_;
@@ -697,26 +762,30 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
+ (instancetype)
allocDescriptorForName:(NSString *)name
- values:(GPBMessageEnumValueDescription *)valueDescriptions
- valueCount:(NSUInteger)valueCount
+ valueNames:(const char *)valueNames
+ values:(const int32_t *)values
+ count:(uint32_t)valueCount
enumVerifier:(GPBEnumValidationFunc)enumVerifier {
GPBEnumDescriptor *descriptor = [[self alloc] initWithName:name
- values:valueDescriptions
- valueCount:valueCount
+ valueNames:valueNames
+ values:values
+ count:valueCount
enumVerifier:enumVerifier];
return descriptor;
}
+ (instancetype)
allocDescriptorForName:(NSString *)name
- values:(GPBMessageEnumValueDescription *)valueDescriptions
- valueCount:(NSUInteger)valueCount
+ valueNames:(const char *)valueNames
+ values:(const int32_t *)values
+ count:(uint32_t)valueCount
enumVerifier:(GPBEnumValidationFunc)enumVerifier
extraTextFormatInfo:(const char *)extraTextFormatInfo {
// Call the common case.
GPBEnumDescriptor *descriptor = [self allocDescriptorForName:name
- values:valueDescriptions
- valueCount:valueCount
+ valueNames:valueNames
+ values:values
+ count:valueCount
enumVerifier:enumVerifier];
// Set the extra info.
descriptor->extraTextFormatInfo_ = (const uint8_t *)extraTextFormatInfo;
@@ -724,25 +793,46 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
}
- (instancetype)initWithName:(NSString *)name
- values:(GPBMessageEnumValueDescription *)valueDescriptions
- valueCount:(NSUInteger)valueCount
+ valueNames:(const char *)valueNames
+ values:(const int32_t *)values
+ count:(uint32_t)valueCount
enumVerifier:(GPBEnumValidationFunc)enumVerifier {
if ((self = [super init])) {
name_ = [name copy];
- valueDescriptions_ = valueDescriptions;
- valueDescriptionsCount_ = valueCount;
+ valueNames_ = valueNames;
+ values_ = values;
+ valueCount_ = valueCount;
enumVerifier_ = enumVerifier;
}
return self;
}
+- (void)dealloc {
+ [name_ release];
+ if (nameOffsets_) free(nameOffsets_);
+ [super dealloc];
+}
+
+- (void)calcValueNameOffsets {
+ @synchronized(self) {
+ if (nameOffsets_ != NULL) {
+ return;
+ }
+ uint32_t *offsets = malloc(valueCount_ * sizeof(uint32_t));
+ const char *scan = valueNames_;
+ for (uint32_t i = 0; i < valueCount_; ++i) {
+ offsets[i] = (uint32_t)(scan - valueNames_);
+ while (*scan != '\0') ++scan;
+ ++scan; // Step over the null.
+ }
+ nameOffsets_ = offsets;
+ }
+}
+
- (NSString *)enumNameForValue:(int32_t)number {
- for (NSUInteger i = 0; i < valueDescriptionsCount_; ++i) {
- GPBMessageEnumValueDescription *scan = &valueDescriptions_[i];
- if ((scan->number == number) && (scan->name != NULL)) {
- NSString *fullName =
- [NSString stringWithFormat:@"%@_%s", name_, scan->name];
- return fullName;
+ for (uint32_t i = 0; i < valueCount_; ++i) {
+ if (values_[i] == number) {
+ return [self getEnumNameForIndex:i];
}
}
return nil;
@@ -760,12 +850,14 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
const char *nameAsCStr = [name UTF8String];
nameAsCStr += prefixLen;
+ if (nameOffsets_ == NULL) [self calcValueNameOffsets];
+
// Find it.
- for (NSUInteger i = 0; i < valueDescriptionsCount_; ++i) {
- GPBMessageEnumValueDescription *scan = &valueDescriptions_[i];
- if ((scan->name != NULL) && (strcmp(nameAsCStr, scan->name) == 0)) {
+ for (uint32_t i = 0; i < valueCount_; ++i) {
+ const char *valueName = valueNames_ + nameOffsets_[i];
+ if (strcmp(nameAsCStr, valueName) == 0) {
if (outValue) {
- *outValue = scan->number;
+ *outValue = values_[i];
}
return YES;
}
@@ -773,39 +865,70 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
return NO;
}
-- (void)dealloc {
- [name_ release];
- [super dealloc];
+- (BOOL)getValue:(int32_t *)outValue forEnumTextFormatName:(NSString *)textFormatName {
+ if (nameOffsets_ == NULL) [self calcValueNameOffsets];
+
+ for (uint32_t i = 0; i < valueCount_; ++i) {
+ int32_t value = values_[i];
+ NSString *valueTextFormatName = [self textFormatNameForValue:value];
+ if ([valueTextFormatName isEqual:textFormatName]) {
+ if (outValue) {
+ *outValue = value;
+ }
+ return YES;
+ }
+ }
+ return NO;
}
- (NSString *)textFormatNameForValue:(int32_t)number {
// Find the EnumValue descriptor and its index.
- GPBMessageEnumValueDescription *valueDescriptor = NULL;
- NSUInteger valueDescriptorIndex;
- for (valueDescriptorIndex = 0; valueDescriptorIndex < valueDescriptionsCount_;
+ BOOL foundIt = NO;
+ uint32_t valueDescriptorIndex;
+ for (valueDescriptorIndex = 0; valueDescriptorIndex < valueCount_;
++valueDescriptorIndex) {
- GPBMessageEnumValueDescription *scan =
- &valueDescriptions_[valueDescriptorIndex];
- if (scan->number == number) {
- valueDescriptor = scan;
+ if (values_[valueDescriptorIndex] == number) {
+ foundIt = YES;
break;
}
}
- // If we didn't find it, or names were disable at proto compile time, nothing
- // we can do.
- if (!valueDescriptor || !valueDescriptor->name) {
+ if (!foundIt) {
return nil;
}
+ return [self getEnumTextFormatNameForIndex:valueDescriptorIndex];
+}
+- (uint32_t)enumNameCount {
+ return valueCount_;
+}
+
+- (NSString *)getEnumNameForIndex:(uint32_t)index {
+ if (nameOffsets_ == NULL) [self calcValueNameOffsets];
+
+ if (index >= valueCount_) {
+ return nil;
+ }
+ const char *valueName = valueNames_ + nameOffsets_[index];
+ NSString *fullName = [NSString stringWithFormat:@"%@_%s", name_, valueName];
+ return fullName;
+}
+
+- (NSString *)getEnumTextFormatNameForIndex:(uint32_t)index {
+ if (nameOffsets_ == NULL) [self calcValueNameOffsets];
+
+ if (index >= valueCount_) {
+ return nil;
+ }
NSString *result = nil;
// Naming adds an underscore between enum name and value name, skip that also.
- NSString *shortName = @(valueDescriptor->name);
+ const char *valueName = valueNames_ + nameOffsets_[index];
+ NSString *shortName = @(valueName);
// See if it is in the map of special format handling.
if (extraTextFormatInfo_) {
result = GPBDecodeTextFormatName(extraTextFormatInfo_,
- (int32_t)valueDescriptorIndex, shortName);
+ (int32_t)index, shortName);
}
// Logic here needs to match what objectivec_enum.cc does in the proto
// compiler.
@@ -837,7 +960,7 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
if ((self = [super init])) {
description_ = description;
-#if DEBUG
+#if defined(DEBUG) && DEBUG
const char *className = description->messageOrGroupClassName;
if (className) {
NSAssert(objc_lookUpClass(className) != Nil,
@@ -858,7 +981,8 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
const uint8_t *bytes =
(const uint8_t *)description->defaultValue.valueData;
if (bytes) {
- uint32_t length = *((uint32_t *)bytes);
+ uint32_t length;
+ memcpy(&length, bytes, sizeof(length));
// The length is stored in network byte order.
length = ntohl(length);
bytes += sizeof(length);
@@ -891,7 +1015,7 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
}
- (NSString *)singletonName {
- return @(description_->singletonName);
+ return (NSString * _Nonnull)@(description_->singletonName);
}
- (const char *)singletonNameC {
@@ -922,10 +1046,6 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
return GPBExtensionIsRepeated(description_);
}
-- (BOOL)isMap {
- return (description_->options & GPBFieldMapKeyMask) != 0;
-}
-
- (BOOL)isPackable {
return GPBExtensionIsPacked(description_);
}
@@ -995,3 +1115,5 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
}
@end
+
+#pragma clang diagnostic pop