Skip to content

Commit

Permalink
[ObjC] Minor cleanups for the descriptor startup.
Browse files Browse the repository at this point in the history
- Streamline the FieldDescriptor loop a little.
- Move some of the init methods from PackagePrivate into the impl file as
  nothing outside calls them and it better encapsulates the signature.

PiperOrigin-RevId: 488671041
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Nov 15, 2022
1 parent 09f4901 commit 3a74266
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 35 deletions.
50 changes: 36 additions & 14 deletions objectivec/GPBDescriptor.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,33 @@
#import "GPBUtilities_PackagePrivate.h"
#import "GPBWireFormat.h"

@interface GPBDescriptor ()
- (instancetype)initWithClass:(Class)messageClass
file:(GPBFileDescriptor *)file
fields:(NSArray *)fields
storageSize:(uint32_t)storage
wireFormat:(BOOL)wireFormat;
@end

@interface GPBFieldDescriptor ()
// Single initializer
// description has to be long lived, it is held as a raw pointer.
- (instancetype)initWithFieldDescription:(void *)description
file:(GPBFileDescriptor *)file
includesDefault:(BOOL)includesDefault
usesClassRefs:(BOOL)usesClassRefs
proto3OptionalKnown:(BOOL)proto3OptionalKnown;

@end

@interface GPBEnumDescriptor ()
- (instancetype)initWithName:(NSString *)name
valueNames:(const char *)valueNames
values:(const int32_t *)values
count:(uint32_t)valueCount
enumVerifier:(GPBEnumValidationFunc)enumVerifier;
@end

// 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.
Expand Down Expand Up @@ -118,31 +145,26 @@ + (instancetype)allocDescriptorForClass:(Class)messageClass
// 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;
GPBFileSyntax syntax = file.syntax;
NSMutableArray *fields =
(fieldCount ? [[NSMutableArray alloc] initWithCapacity:fieldCount] : nil);
BOOL fieldsIncludeDefault = (flags & GPBDescriptorInitializationFlag_FieldsWithDefault) != 0;
BOOL usesClassRefs = (flags & GPBDescriptorInitializationFlag_UsesClassRefs) != 0;
BOOL proto3OptionalKnown = (flags & GPBDescriptorInitializationFlag_Proto3OptionalKnown) != 0;

void *desc;
for (uint32_t i = 0; i < fieldCount; ++i) {
if (fields == nil) {
fields = [[NSMutableArray alloc] initWithCapacity:fieldCount];
}
// Need correctly typed pointer for array indexing below to work.
if (fieldsIncludeDefault) {
GPBMessageFieldDescriptionWithDefault *fieldDescWithDefault = fieldDescriptions;
desc = &(fieldDescWithDefault[i]);
desc = &(((GPBMessageFieldDescriptionWithDefault *)fieldDescriptions)[i]);
} else {
GPBMessageFieldDescription *fieldDesc = fieldDescriptions;
desc = &(fieldDesc[i]);
desc = &(((GPBMessageFieldDescription *)fieldDescriptions)[i]);
}
GPBFieldDescriptor *fieldDescriptor =
[[GPBFieldDescriptor alloc] initWithFieldDescription:desc
file:file
includesDefault:fieldsIncludeDefault
usesClassRefs:usesClassRefs
proto3OptionalKnown:proto3OptionalKnown
syntax:syntax];
proto3OptionalKnown:proto3OptionalKnown];
[fields addObject:fieldDescriptor];
[fieldDescriptor release];
}
Expand Down Expand Up @@ -450,10 +472,10 @@ @implementation GPBFieldDescriptor {
@synthesize containingOneof = containingOneof_;

- (instancetype)initWithFieldDescription:(void *)description
file:(GPBFileDescriptor *)file
includesDefault:(BOOL)includesDefault
usesClassRefs:(BOOL)usesClassRefs
proto3OptionalKnown:(BOOL)proto3OptionalKnown
syntax:(GPBFileSyntax)syntax {
proto3OptionalKnown:(BOOL)proto3OptionalKnown {
if ((self = [super init])) {
GPBMessageFieldDescription *coreDesc;
if (includesDefault) {
Expand All @@ -477,7 +499,7 @@ - (instancetype)initWithFieldDescription:(void *)description
// - not repeated/map
// - not in a oneof (negative has index)
// - not a message (the flag doesn't make sense for messages)
BOOL clearOnZero = ((syntax == GPBFileSyntaxProto3) && !isMapOrArray &&
BOOL clearOnZero = ((file.syntax == GPBFileSyntaxProto3) && !isMapOrArray &&
(coreDesc->hasIndex >= 0) && !isMessage);
if (clearOnZero) {
coreDesc->flags |= GPBFieldClearHasIvarOnZero;
Expand Down
21 changes: 0 additions & 21 deletions objectivec/GPBDescriptor_PackagePrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,6 @@ typedef NS_OPTIONS(uint32_t, GPBDescriptorInitializationFlags) {
storageSize:(uint32_t)storageSize
flags:(GPBDescriptorInitializationFlags)flags;

- (instancetype)initWithClass:(Class)messageClass
file:(GPBFileDescriptor *)file
fields:(NSArray *)fields
storageSize:(uint32_t)storage
wireFormat:(BOOL)wireFormat;

// Called right after init to provide extra information to avoid init having
// an explosion of args. These pointers are recorded, so they are expected
// to live for the lifetime of the app.
Expand Down Expand Up @@ -240,15 +234,6 @@ typedef NS_OPTIONS(uint32_t, GPBDescriptorInitializationFlags) {
SEL hasOrCountSel_; // *Count for map<>/repeated fields, has* otherwise.
SEL setHasSel_;
}

// Single initializer
// description has to be long lived, it is held as a raw pointer.
- (instancetype)initWithFieldDescription:(void *)description
includesDefault:(BOOL)includesDefault
usesClassRefs:(BOOL)usesClassRefs
proto3OptionalKnown:(BOOL)proto3OptionalKnown
syntax:(GPBFileSyntax)syntax;

@end

@interface GPBEnumDescriptor ()
Expand All @@ -265,12 +250,6 @@ typedef NS_OPTIONS(uint32_t, GPBDescriptorInitializationFlags) {
count:(uint32_t)valueCount
enumVerifier:(GPBEnumValidationFunc)enumVerifier
extraTextFormatInfo:(const char *)extraTextFormatInfo;

- (instancetype)initWithName:(NSString *)name
valueNames:(const char *)valueNames
values:(const int32_t *)values
count:(uint32_t)valueCount
enumVerifier:(GPBEnumValidationFunc)enumVerifier;
@end

@interface GPBExtensionDescriptor () {
Expand Down

0 comments on commit 3a74266

Please sign in to comment.