1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
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
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.
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.
31 #import "GPBUnknownField_PackagePrivate.h"
34 #import "GPBCodedOutputStream_PackagePrivate.h"
36 @implementation GPBUnknownField {
39 GPBUInt64Array *mutableVarintList_;
40 GPBUInt32Array *mutableFixed32List_;
41 GPBUInt64Array *mutableFixed64List_;
42 NSMutableArray<NSData*> *mutableLengthDelimitedList_;
43 NSMutableArray<GPBUnknownFieldSet*> *mutableGroupList_;
46 @synthesize number = number_;
47 @synthesize varintList = mutableVarintList_;
48 @synthesize fixed32List = mutableFixed32List_;
49 @synthesize fixed64List = mutableFixed64List_;
50 @synthesize lengthDelimitedList = mutableLengthDelimitedList_;
51 @synthesize groupList = mutableGroupList_;
53 - (instancetype)initWithNumber:(int32_t)number {
54 if ((self = [super init])) {
61 [mutableVarintList_ release];
62 [mutableFixed32List_ release];
63 [mutableFixed64List_ release];
64 [mutableLengthDelimitedList_ release];
65 [mutableGroupList_ release];
70 // Direct access is use for speed, to avoid even internally declaring things
71 // read/write, etc. The warning is enabled in the project to ensure code calling
72 // protos can turn on -Wdirect-ivar-access without issues.
73 #pragma clang diagnostic push
74 #pragma clang diagnostic ignored "-Wdirect-ivar-access"
76 - (id)copyWithZone:(NSZone *)zone {
77 GPBUnknownField *result =
78 [[GPBUnknownField allocWithZone:zone] initWithNumber:number_];
79 result->mutableFixed32List_ = [mutableFixed32List_ copyWithZone:zone];
80 result->mutableFixed64List_ = [mutableFixed64List_ copyWithZone:zone];
81 result->mutableLengthDelimitedList_ =
82 [mutableLengthDelimitedList_ mutableCopyWithZone:zone];
83 result->mutableVarintList_ = [mutableVarintList_ copyWithZone:zone];
84 if (mutableGroupList_.count) {
85 result->mutableGroupList_ = [[NSMutableArray allocWithZone:zone]
86 initWithCapacity:mutableGroupList_.count];
87 for (GPBUnknownFieldSet *group in mutableGroupList_) {
88 GPBUnknownFieldSet *copied = [group copyWithZone:zone];
89 [result->mutableGroupList_ addObject:copied];
96 - (BOOL)isEqual:(id)object {
97 if (self == object) return YES;
98 if (![object isKindOfClass:[GPBUnknownField class]]) return NO;
99 GPBUnknownField *field = (GPBUnknownField *)object;
100 if (number_ != field->number_) return NO;
102 (mutableVarintList_.count == 0 && field->mutableVarintList_.count == 0) ||
103 [mutableVarintList_ isEqual:field->mutableVarintList_];
104 if (!equalVarint) return NO;
105 BOOL equalFixed32 = (mutableFixed32List_.count == 0 &&
106 field->mutableFixed32List_.count == 0) ||
107 [mutableFixed32List_ isEqual:field->mutableFixed32List_];
108 if (!equalFixed32) return NO;
109 BOOL equalFixed64 = (mutableFixed64List_.count == 0 &&
110 field->mutableFixed64List_.count == 0) ||
111 [mutableFixed64List_ isEqual:field->mutableFixed64List_];
112 if (!equalFixed64) return NO;
114 (mutableLengthDelimitedList_.count == 0 &&
115 field->mutableLengthDelimitedList_.count == 0) ||
116 [mutableLengthDelimitedList_ isEqual:field->mutableLengthDelimitedList_];
117 if (!equalLDList) return NO;
118 BOOL equalGroupList =
119 (mutableGroupList_.count == 0 && field->mutableGroupList_.count == 0) ||
120 [mutableGroupList_ isEqual:field->mutableGroupList_];
121 if (!equalGroupList) return NO;
126 // Just mix the hashes of the possible sub arrays.
127 const int prime = 31;
128 NSUInteger result = prime + [mutableVarintList_ hash];
129 result = prime * result + [mutableFixed32List_ hash];
130 result = prime * result + [mutableFixed64List_ hash];
131 result = prime * result + [mutableLengthDelimitedList_ hash];
132 result = prime * result + [mutableGroupList_ hash];
136 - (void)writeToOutput:(GPBCodedOutputStream *)output {
137 NSUInteger count = mutableVarintList_.count;
139 [output writeUInt64Array:number_ values:mutableVarintList_ tag:0];
141 count = mutableFixed32List_.count;
143 [output writeFixed32Array:number_ values:mutableFixed32List_ tag:0];
145 count = mutableFixed64List_.count;
147 [output writeFixed64Array:number_ values:mutableFixed64List_ tag:0];
149 count = mutableLengthDelimitedList_.count;
151 [output writeBytesArray:number_ values:mutableLengthDelimitedList_];
153 count = mutableGroupList_.count;
155 [output writeUnknownGroupArray:number_ values:mutableGroupList_];
159 - (size_t)serializedSize {
160 __block size_t result = 0;
161 int32_t number = number_;
163 enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
164 #pragma unused(idx, stop)
165 result += GPBComputeUInt64Size(number, value);
169 enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) {
170 #pragma unused(idx, stop)
171 result += GPBComputeFixed32Size(number, value);
175 enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
176 #pragma unused(idx, stop)
177 result += GPBComputeFixed64Size(number, value);
180 for (NSData *data in mutableLengthDelimitedList_) {
181 result += GPBComputeBytesSize(number, data);
184 for (GPBUnknownFieldSet *set in mutableGroupList_) {
185 result += GPBComputeUnknownGroupSize(number, set);
191 - (void)writeAsMessageSetExtensionToOutput:(GPBCodedOutputStream *)output {
192 for (NSData *data in mutableLengthDelimitedList_) {
193 [output writeRawMessageSetExtension:number_ value:data];
197 - (size_t)serializedSizeAsMessageSetExtension {
199 for (NSData *data in mutableLengthDelimitedList_) {
200 result += GPBComputeRawMessageSetExtensionSize(number_, data);
205 - (NSString *)description {
206 NSMutableString *description =
207 [NSMutableString stringWithFormat:@"<%@ %p>: Field: %d {\n",
208 [self class], self, number_];
210 enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
211 #pragma unused(idx, stop)
212 [description appendFormat:@"\t%llu\n", value];
216 enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) {
217 #pragma unused(idx, stop)
218 [description appendFormat:@"\t%u\n", value];
222 enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
223 #pragma unused(idx, stop)
224 [description appendFormat:@"\t%llu\n", value];
227 for (NSData *data in mutableLengthDelimitedList_) {
228 [description appendFormat:@"\t%@\n", data];
231 for (GPBUnknownFieldSet *set in mutableGroupList_) {
232 [description appendFormat:@"\t%@\n", set];
234 [description appendString:@"}"];
238 - (void)mergeFromField:(GPBUnknownField *)other {
239 GPBUInt64Array *otherVarintList = other.varintList;
240 if (otherVarintList.count > 0) {
241 if (mutableVarintList_ == nil) {
242 mutableVarintList_ = [otherVarintList copy];
244 [mutableVarintList_ addValuesFromArray:otherVarintList];
248 GPBUInt32Array *otherFixed32List = other.fixed32List;
249 if (otherFixed32List.count > 0) {
250 if (mutableFixed32List_ == nil) {
251 mutableFixed32List_ = [otherFixed32List copy];
253 [mutableFixed32List_ addValuesFromArray:otherFixed32List];
257 GPBUInt64Array *otherFixed64List = other.fixed64List;
258 if (otherFixed64List.count > 0) {
259 if (mutableFixed64List_ == nil) {
260 mutableFixed64List_ = [otherFixed64List copy];
262 [mutableFixed64List_ addValuesFromArray:otherFixed64List];
266 NSArray *otherLengthDelimitedList = other.lengthDelimitedList;
267 if (otherLengthDelimitedList.count > 0) {
268 if (mutableLengthDelimitedList_ == nil) {
269 mutableLengthDelimitedList_ = [otherLengthDelimitedList mutableCopy];
271 [mutableLengthDelimitedList_
272 addObjectsFromArray:otherLengthDelimitedList];
276 NSArray *otherGroupList = other.groupList;
277 if (otherGroupList.count > 0) {
278 if (mutableGroupList_ == nil) {
280 [[NSMutableArray alloc] initWithCapacity:otherGroupList.count];
282 // Make our own mutable copies.
283 for (GPBUnknownFieldSet *group in otherGroupList) {
284 GPBUnknownFieldSet *copied = [group copy];
285 [mutableGroupList_ addObject:copied];
291 - (void)addVarint:(uint64_t)value {
292 if (mutableVarintList_ == nil) {
293 mutableVarintList_ = [[GPBUInt64Array alloc] initWithValues:&value count:1];
295 [mutableVarintList_ addValue:value];
299 - (void)addFixed32:(uint32_t)value {
300 if (mutableFixed32List_ == nil) {
301 mutableFixed32List_ =
302 [[GPBUInt32Array alloc] initWithValues:&value count:1];
304 [mutableFixed32List_ addValue:value];
308 - (void)addFixed64:(uint64_t)value {
309 if (mutableFixed64List_ == nil) {
310 mutableFixed64List_ =
311 [[GPBUInt64Array alloc] initWithValues:&value count:1];
313 [mutableFixed64List_ addValue:value];
317 - (void)addLengthDelimited:(NSData *)value {
318 if (mutableLengthDelimitedList_ == nil) {
319 mutableLengthDelimitedList_ =
320 [[NSMutableArray alloc] initWithObjects:&value count:1];
322 [mutableLengthDelimitedList_ addObject:value];
326 - (void)addGroup:(GPBUnknownFieldSet *)value {
327 if (mutableGroupList_ == nil) {
328 mutableGroupList_ = [[NSMutableArray alloc] initWithObjects:&value count:1];
330 [mutableGroupList_ addObject:value];
334 #pragma clang diagnostic pop