blob: 0e29bde50f66b5861d93a180fa36a5b5e1b85c37 [file] [log] [blame]
Thomas Van Lenten30650d82015-05-01 08:57:16 -04001// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc. All rights reserved.
3// https://developers.google.com/protocol-buffers/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
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
14// distribution.
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.
18//
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.
30
Thomas Van Lentend846b0b2015-06-08 16:24:57 -040031#import "GPBUnknownField_PackagePrivate.h"
Thomas Van Lenten30650d82015-05-01 08:57:16 -040032
33#import "GPBArray.h"
Thomas Van Lenten36650a02016-03-07 12:07:03 -050034#import "GPBCodedOutputStream_PackagePrivate.h"
Thomas Van Lenten30650d82015-05-01 08:57:16 -040035
Thomas Van Lentend846b0b2015-06-08 16:24:57 -040036@implementation GPBUnknownField {
Thomas Van Lenten30650d82015-05-01 08:57:16 -040037 @protected
38 int32_t number_;
39 GPBUInt64Array *mutableVarintList_;
40 GPBUInt32Array *mutableFixed32List_;
41 GPBUInt64Array *mutableFixed64List_;
Thomas Van Lenten2480acb2015-11-30 14:38:04 -050042 NSMutableArray<NSData*> *mutableLengthDelimitedList_;
43 NSMutableArray<GPBUnknownFieldSet*> *mutableGroupList_;
Thomas Van Lenten30650d82015-05-01 08:57:16 -040044}
Thomas Van Lenten30650d82015-05-01 08:57:16 -040045
46@synthesize number = number_;
47@synthesize varintList = mutableVarintList_;
48@synthesize fixed32List = mutableFixed32List_;
49@synthesize fixed64List = mutableFixed64List_;
50@synthesize lengthDelimitedList = mutableLengthDelimitedList_;
51@synthesize groupList = mutableGroupList_;
52
53- (instancetype)initWithNumber:(int32_t)number {
54 if ((self = [super init])) {
55 number_ = number;
56 }
57 return self;
58}
59
60- (void)dealloc {
61 [mutableVarintList_ release];
62 [mutableFixed32List_ release];
63 [mutableFixed64List_ release];
64 [mutableLengthDelimitedList_ release];
65 [mutableGroupList_ release];
66
67 [super dealloc];
68}
69
70- (id)copyWithZone:(NSZone *)zone {
Thomas Van Lentend846b0b2015-06-08 16:24:57 -040071 GPBUnknownField *result =
72 [[GPBUnknownField allocWithZone:zone] initWithNumber:number_];
Thomas Van Lenten30650d82015-05-01 08:57:16 -040073 result->mutableFixed32List_ = [mutableFixed32List_ copyWithZone:zone];
74 result->mutableFixed64List_ = [mutableFixed64List_ copyWithZone:zone];
75 result->mutableLengthDelimitedList_ =
76 [mutableLengthDelimitedList_ copyWithZone:zone];
77 result->mutableVarintList_ = [mutableVarintList_ copyWithZone:zone];
78 if (mutableGroupList_.count) {
79 result->mutableGroupList_ = [[NSMutableArray allocWithZone:zone]
80 initWithCapacity:mutableGroupList_.count];
81 for (GPBUnknownFieldSet *group in mutableGroupList_) {
82 GPBUnknownFieldSet *copied = [group copyWithZone:zone];
83 [result->mutableGroupList_ addObject:copied];
84 [copied release];
85 }
86 }
87 return result;
88}
89
90- (BOOL)isEqual:(id)object {
91 if (self == object) return YES;
Thomas Van Lentend846b0b2015-06-08 16:24:57 -040092 if (![object isKindOfClass:[GPBUnknownField class]]) return NO;
93 GPBUnknownField *field = (GPBUnknownField *)object;
Thomas Van Lenten30650d82015-05-01 08:57:16 -040094 BOOL equalVarint =
95 (mutableVarintList_.count == 0 && field->mutableVarintList_.count == 0) ||
96 [mutableVarintList_ isEqual:field->mutableVarintList_];
97 if (!equalVarint) return NO;
98 BOOL equalFixed32 = (mutableFixed32List_.count == 0 &&
99 field->mutableFixed32List_.count == 0) ||
100 [mutableFixed32List_ isEqual:field->mutableFixed32List_];
101 if (!equalFixed32) return NO;
102 BOOL equalFixed64 = (mutableFixed64List_.count == 0 &&
103 field->mutableFixed64List_.count == 0) ||
104 [mutableFixed64List_ isEqual:field->mutableFixed64List_];
105 if (!equalFixed64) return NO;
106 BOOL equalLDList =
107 (mutableLengthDelimitedList_.count == 0 &&
108 field->mutableLengthDelimitedList_.count == 0) ||
109 [mutableLengthDelimitedList_ isEqual:field->mutableLengthDelimitedList_];
110 if (!equalLDList) return NO;
111 BOOL equalGroupList =
112 (mutableGroupList_.count == 0 && field->mutableGroupList_.count == 0) ||
113 [mutableGroupList_ isEqual:field->mutableGroupList_];
114 if (!equalGroupList) return NO;
115 return YES;
116}
117
118- (NSUInteger)hash {
119 // Just mix the hashes of the possible sub arrays.
120 const int prime = 31;
121 NSUInteger result = prime + [mutableVarintList_ hash];
122 result = prime * result + [mutableFixed32List_ hash];
123 result = prime * result + [mutableFixed64List_ hash];
124 result = prime * result + [mutableLengthDelimitedList_ hash];
125 result = prime * result + [mutableGroupList_ hash];
126 return result;
127}
128
129- (void)writeToOutput:(GPBCodedOutputStream *)output {
130 NSUInteger count = mutableVarintList_.count;
131 if (count > 0) {
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400132 [output writeUInt64Array:number_ values:mutableVarintList_ tag:0];
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400133 }
134 count = mutableFixed32List_.count;
135 if (count > 0) {
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400136 [output writeFixed32Array:number_ values:mutableFixed32List_ tag:0];
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400137 }
138 count = mutableFixed64List_.count;
139 if (count > 0) {
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400140 [output writeFixed64Array:number_ values:mutableFixed64List_ tag:0];
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400141 }
142 count = mutableLengthDelimitedList_.count;
143 if (count > 0) {
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400144 [output writeBytesArray:number_ values:mutableLengthDelimitedList_];
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400145 }
146 count = mutableGroupList_.count;
147 if (count > 0) {
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400148 [output writeUnknownGroupArray:number_ values:mutableGroupList_];
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400149 }
150}
151
152- (size_t)serializedSize {
153 __block size_t result = 0;
154 int32_t number = number_;
155 [mutableVarintList_
156 enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
157#pragma unused(idx, stop)
158 result += GPBComputeUInt64Size(number, value);
159 }];
160
161 [mutableFixed32List_
162 enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) {
163#pragma unused(idx, stop)
164 result += GPBComputeFixed32Size(number, value);
165 }];
166
167 [mutableFixed64List_
168 enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
169#pragma unused(idx, stop)
170 result += GPBComputeFixed64Size(number, value);
171 }];
172
173 for (NSData *data in mutableLengthDelimitedList_) {
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400174 result += GPBComputeBytesSize(number, data);
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400175 }
176
177 for (GPBUnknownFieldSet *set in mutableGroupList_) {
178 result += GPBComputeUnknownGroupSize(number, set);
179 }
180
181 return result;
182}
183
184- (void)writeAsMessageSetExtensionToOutput:(GPBCodedOutputStream *)output {
185 for (NSData *data in mutableLengthDelimitedList_) {
186 [output writeRawMessageSetExtension:number_ value:data];
187 }
188}
189
190- (size_t)serializedSizeAsMessageSetExtension {
191 size_t result = 0;
192 for (NSData *data in mutableLengthDelimitedList_) {
193 result += GPBComputeRawMessageSetExtensionSize(number_, data);
194 }
195 return result;
196}
197
198- (NSString *)description {
199 NSMutableString *description = [NSMutableString
200 stringWithFormat:@"<%@ %p>: Field: %d {\n", [self class], self, number_];
201 [mutableVarintList_
202 enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
203#pragma unused(idx, stop)
204 [description appendFormat:@"\t%llu\n", value];
205 }];
206
207 [mutableFixed32List_
208 enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) {
209#pragma unused(idx, stop)
210 [description appendFormat:@"\t%u\n", value];
211 }];
212
213 [mutableFixed64List_
214 enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
215#pragma unused(idx, stop)
216 [description appendFormat:@"\t%llu\n", value];
217 }];
218
219 for (NSData *data in mutableLengthDelimitedList_) {
220 [description appendFormat:@"\t%@\n", data];
221 }
222
223 for (GPBUnknownFieldSet *set in mutableGroupList_) {
224 [description appendFormat:@"\t%@\n", set];
225 }
226 [description appendString:@"}"];
227 return description;
228}
229
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400230- (void)mergeFromField:(GPBUnknownField *)other {
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400231 GPBUInt64Array *otherVarintList = other.varintList;
232 if (otherVarintList.count > 0) {
233 if (mutableVarintList_ == nil) {
234 mutableVarintList_ = [otherVarintList copy];
235 } else {
236 [mutableVarintList_ addValuesFromArray:otherVarintList];
237 }
238 }
239
240 GPBUInt32Array *otherFixed32List = other.fixed32List;
241 if (otherFixed32List.count > 0) {
242 if (mutableFixed32List_ == nil) {
243 mutableFixed32List_ = [otherFixed32List copy];
244 } else {
245 [mutableFixed32List_ addValuesFromArray:otherFixed32List];
246 }
247 }
248
249 GPBUInt64Array *otherFixed64List = other.fixed64List;
250 if (otherFixed64List.count > 0) {
251 if (mutableFixed64List_ == nil) {
252 mutableFixed64List_ = [otherFixed64List copy];
253 } else {
254 [mutableFixed64List_ addValuesFromArray:otherFixed64List];
255 }
256 }
257
258 NSArray *otherLengthDelimitedList = other.lengthDelimitedList;
259 if (otherLengthDelimitedList.count > 0) {
260 if (mutableLengthDelimitedList_ == nil) {
261 mutableLengthDelimitedList_ = [otherLengthDelimitedList mutableCopy];
262 } else {
263 [mutableLengthDelimitedList_
264 addObjectsFromArray:otherLengthDelimitedList];
265 }
266 }
267
268 NSArray *otherGroupList = other.groupList;
269 if (otherGroupList.count > 0) {
270 if (mutableGroupList_ == nil) {
271 mutableGroupList_ =
272 [[NSMutableArray alloc] initWithCapacity:otherGroupList.count];
273 }
274 // Make our own mutable copies.
275 for (GPBUnknownFieldSet *group in otherGroupList) {
276 GPBUnknownFieldSet *copied = [group copy];
277 [mutableGroupList_ addObject:copied];
278 [copied release];
279 }
280 }
281}
282
283- (void)addVarint:(uint64_t)value {
284 if (mutableVarintList_ == nil) {
285 mutableVarintList_ = [[GPBUInt64Array alloc] initWithValues:&value count:1];
286 } else {
287 [mutableVarintList_ addValue:value];
288 }
289}
290
291- (void)addFixed32:(uint32_t)value {
292 if (mutableFixed32List_ == nil) {
293 mutableFixed32List_ =
294 [[GPBUInt32Array alloc] initWithValues:&value count:1];
295 } else {
296 [mutableFixed32List_ addValue:value];
297 }
298}
299
300- (void)addFixed64:(uint64_t)value {
301 if (mutableFixed64List_ == nil) {
302 mutableFixed64List_ =
303 [[GPBUInt64Array alloc] initWithValues:&value count:1];
304 } else {
305 [mutableFixed64List_ addValue:value];
306 }
307}
308
309- (void)addLengthDelimited:(NSData *)value {
310 if (mutableLengthDelimitedList_ == nil) {
311 mutableLengthDelimitedList_ =
312 [[NSMutableArray alloc] initWithObjects:&value count:1];
313 } else {
314 [mutableLengthDelimitedList_ addObject:value];
315 }
316}
317
318- (void)addGroup:(GPBUnknownFieldSet *)value {
319 if (mutableGroupList_ == nil) {
320 mutableGroupList_ = [[NSMutableArray alloc] initWithObjects:&value count:1];
321 } else {
322 [mutableGroupList_ addObject:value];
323 }
324}
325
326@end