aboutsummaryrefslogtreecommitdiff
path: root/objectivec/GPBUtilities.m
diff options
context:
space:
mode:
Diffstat (limited to 'objectivec/GPBUtilities.m')
-rw-r--r--objectivec/GPBUtilities.m71
1 files changed, 39 insertions, 32 deletions
diff --git a/objectivec/GPBUtilities.m b/objectivec/GPBUtilities.m
index 5ee61230..447c749a 100644
--- a/objectivec/GPBUtilities.m
+++ b/objectivec/GPBUtilities.m
@@ -145,9 +145,8 @@ void GPBSetHasIvar(GPBMessage *self, int32_t idx, uint32_t fieldNumber,
}
void GPBMaybeClearOneof(GPBMessage *self, GPBOneofDescriptor *oneof,
- uint32_t fieldNumberNotToClear) {
- int32_t hasIndex = oneof->oneofDescription_->index;
- uint32_t fieldNumberSet = GPBGetHasOneof(self, hasIndex);
+ int32_t oneofHasIndex, uint32_t fieldNumberNotToClear) {
+ uint32_t fieldNumberSet = GPBGetHasOneof(self, oneofHasIndex);
if ((fieldNumberSet == fieldNumberNotToClear) || (fieldNumberSet == 0)) {
// Do nothing/nothing set in the oneof.
return;
@@ -168,7 +167,7 @@ void GPBMaybeClearOneof(GPBMessage *self, GPBOneofDescriptor *oneof,
// Set to nothing stored in the oneof.
// (field number doesn't matter since setting to nothing).
- GPBSetHasIvar(self, hasIndex, 1, NO);
+ GPBSetHasIvar(self, oneofHasIndex, 1, NO);
}
#pragma mark - IVar accessors
@@ -200,7 +199,8 @@ void GPBMaybeClearOneof(GPBMessage *self, GPBOneofDescriptor *oneof,
//% NAME$S GPBFileSyntax syntax) {
//% GPBOneofDescriptor *oneof = field->containingOneof_;
//% if (oneof) {
-//% GPBMaybeClearOneof(self, oneof, GPBFieldNumber(field));
+//% GPBMessageFieldDescription *fieldDesc = field->description_;
+//% GPBMaybeClearOneof(self, oneof, fieldDesc->hasIndex, fieldDesc->number);
//% }
//% NSCAssert(self->messageStorage_ != NULL,
//% @"%@: All messages should have storage (from init)",
@@ -321,7 +321,8 @@ void GPBSetRetainedObjectIvarWithFieldInternal(GPBMessage *self,
// oneof.
GPBOneofDescriptor *oneof = field->containingOneof_;
if (oneof) {
- GPBMaybeClearOneof(self, oneof, GPBFieldNumber(field));
+ GPBMessageFieldDescription *fieldDesc = field->description_;
+ GPBMaybeClearOneof(self, oneof, fieldDesc->hasIndex, fieldDesc->number);
}
// Clear "has" if they are being set to nil.
BOOL setHasValue = (value != nil);
@@ -411,7 +412,8 @@ id GPBGetObjectIvarWithField(GPBMessage *self, GPBFieldDescriptor *field) {
return field.defaultValue.valueMessage;
}
- OSSpinLockLock(&self->readOnlyMutex_);
+ GPBPrepareReadOnlySemaphore(self);
+ dispatch_semaphore_wait(self->readOnlySemaphore_, DISPATCH_TIME_FOREVER);
GPBMessage *result = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
if (!result) {
// For non repeated messages, create the object, set it and return it.
@@ -420,7 +422,7 @@ id GPBGetObjectIvarWithField(GPBMessage *self, GPBFieldDescriptor *field) {
result = GPBCreateMessageWithAutocreator(field.msgClass, self, field);
GPBSetAutocreatedRetainedObjectIvarWithField(self, field, result);
}
- OSSpinLockUnlock(&self->readOnlyMutex_);
+ dispatch_semaphore_signal(self->readOnlySemaphore_);
return result;
}
@@ -476,15 +478,15 @@ void GPBSetMessageRawEnumField(GPBMessage *self, GPBFieldDescriptor *field,
GPBSetInt32IvarWithFieldInternal(self, field, value, syntax);
}
-//%PDDM-EXPAND IVAR_POD_ACCESSORS_DEFN(Bool, BOOL)
-// This block of code is generated, do not edit it directly.
-
BOOL GPBGetMessageBoolField(GPBMessage *self,
GPBFieldDescriptor *field) {
if (GPBGetHasIvarField(self, field)) {
- uint8_t *storage = (uint8_t *)self->messageStorage_;
- BOOL *typePtr = (BOOL *)&storage[field->description_->offset];
- return *typePtr;
+ // Bools are stored in the has bits to avoid needing explicit space in the
+ // storage structure.
+ // (the field number passed to the HasIvar helper doesn't really matter
+ // since the offset is never negative)
+ GPBMessageFieldDescription *fieldDesc = field->description_;
+ return GPBGetHasIvar(self, (int32_t)(fieldDesc->offset), fieldDesc->number);
} else {
return field.defaultValue.valueBool;
}
@@ -503,19 +505,18 @@ void GPBSetBoolIvarWithFieldInternal(GPBMessage *self,
GPBFieldDescriptor *field,
BOOL value,
GPBFileSyntax syntax) {
+ GPBMessageFieldDescription *fieldDesc = field->description_;
GPBOneofDescriptor *oneof = field->containingOneof_;
if (oneof) {
- GPBMaybeClearOneof(self, oneof, GPBFieldNumber(field));
+ GPBMaybeClearOneof(self, oneof, fieldDesc->hasIndex, fieldDesc->number);
}
- NSCAssert(self->messageStorage_ != NULL,
- @"%@: All messages should have storage (from init)",
- [self class]);
-#if defined(__clang_analyzer__)
- if (self->messageStorage_ == NULL) return;
-#endif
- uint8_t *storage = (uint8_t *)self->messageStorage_;
- BOOL *typePtr = (BOOL *)&storage[field->description_->offset];
- *typePtr = value;
+
+ // Bools are stored in the has bits to avoid needing explicit space in the
+ // storage structure.
+ // (the field number passed to the HasIvar helper doesn't really matter since
+ // the offset is never negative)
+ GPBSetHasIvar(self, (int32_t)(fieldDesc->offset), fieldDesc->number, value);
+
// proto2: any value counts as having been set; proto3, it
// has to be a non zero value.
BOOL hasValue =
@@ -553,7 +554,8 @@ void GPBSetInt32IvarWithFieldInternal(GPBMessage *self,
GPBFileSyntax syntax) {
GPBOneofDescriptor *oneof = field->containingOneof_;
if (oneof) {
- GPBMaybeClearOneof(self, oneof, GPBFieldNumber(field));
+ GPBMessageFieldDescription *fieldDesc = field->description_;
+ GPBMaybeClearOneof(self, oneof, fieldDesc->hasIndex, fieldDesc->number);
}
NSCAssert(self->messageStorage_ != NULL,
@"%@: All messages should have storage (from init)",
@@ -601,7 +603,8 @@ void GPBSetUInt32IvarWithFieldInternal(GPBMessage *self,
GPBFileSyntax syntax) {
GPBOneofDescriptor *oneof = field->containingOneof_;
if (oneof) {
- GPBMaybeClearOneof(self, oneof, GPBFieldNumber(field));
+ GPBMessageFieldDescription *fieldDesc = field->description_;
+ GPBMaybeClearOneof(self, oneof, fieldDesc->hasIndex, fieldDesc->number);
}
NSCAssert(self->messageStorage_ != NULL,
@"%@: All messages should have storage (from init)",
@@ -649,7 +652,8 @@ void GPBSetInt64IvarWithFieldInternal(GPBMessage *self,
GPBFileSyntax syntax) {
GPBOneofDescriptor *oneof = field->containingOneof_;
if (oneof) {
- GPBMaybeClearOneof(self, oneof, GPBFieldNumber(field));
+ GPBMessageFieldDescription *fieldDesc = field->description_;
+ GPBMaybeClearOneof(self, oneof, fieldDesc->hasIndex, fieldDesc->number);
}
NSCAssert(self->messageStorage_ != NULL,
@"%@: All messages should have storage (from init)",
@@ -697,7 +701,8 @@ void GPBSetUInt64IvarWithFieldInternal(GPBMessage *self,
GPBFileSyntax syntax) {
GPBOneofDescriptor *oneof = field->containingOneof_;
if (oneof) {
- GPBMaybeClearOneof(self, oneof, GPBFieldNumber(field));
+ GPBMessageFieldDescription *fieldDesc = field->description_;
+ GPBMaybeClearOneof(self, oneof, fieldDesc->hasIndex, fieldDesc->number);
}
NSCAssert(self->messageStorage_ != NULL,
@"%@: All messages should have storage (from init)",
@@ -745,7 +750,8 @@ void GPBSetFloatIvarWithFieldInternal(GPBMessage *self,
GPBFileSyntax syntax) {
GPBOneofDescriptor *oneof = field->containingOneof_;
if (oneof) {
- GPBMaybeClearOneof(self, oneof, GPBFieldNumber(field));
+ GPBMessageFieldDescription *fieldDesc = field->description_;
+ GPBMaybeClearOneof(self, oneof, fieldDesc->hasIndex, fieldDesc->number);
}
NSCAssert(self->messageStorage_ != NULL,
@"%@: All messages should have storage (from init)",
@@ -793,7 +799,8 @@ void GPBSetDoubleIvarWithFieldInternal(GPBMessage *self,
GPBFileSyntax syntax) {
GPBOneofDescriptor *oneof = field->containingOneof_;
if (oneof) {
- GPBMaybeClearOneof(self, oneof, GPBFieldNumber(field));
+ GPBMessageFieldDescription *fieldDesc = field->description_;
+ GPBMaybeClearOneof(self, oneof, fieldDesc->hasIndex, fieldDesc->number);
}
NSCAssert(self->messageStorage_ != NULL,
@"%@: All messages should have storage (from init)",
@@ -812,7 +819,7 @@ void GPBSetDoubleIvarWithFieldInternal(GPBMessage *self,
GPBBecomeVisibleToAutocreator(self);
}
-//%PDDM-EXPAND-END (7 expansions)
+//%PDDM-EXPAND-END (6 expansions)
// Aliases are function calls that are virtually the same.
@@ -1253,7 +1260,7 @@ static void AppendTextFormatForMessageField(GPBMessage *message,
if ([fieldName length] == 0) {
fieldName = [NSString stringWithFormat:@"%u", GPBFieldNumber(field)];
// If there is only one entry, put the objc name as a comment, other wise
- // add it before the the repeated values.
+ // add it before the repeated values.
if (count > 1) {
[toStr appendFormat:@"%@# %@\n", lineIndent, field.name];
} else {