added iOS source code
[wl-app.git] / iOS / Pods / Protobuf / objectivec / GPBUnknownField.m
1 // 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
31 #import "GPBUnknownField_PackagePrivate.h"
32
33 #import "GPBArray.h"
34 #import "GPBCodedOutputStream_PackagePrivate.h"
35
36 @implementation GPBUnknownField {
37  @protected
38   int32_t number_;
39   GPBUInt64Array *mutableVarintList_;
40   GPBUInt32Array *mutableFixed32List_;
41   GPBUInt64Array *mutableFixed64List_;
42   NSMutableArray<NSData*> *mutableLengthDelimitedList_;
43   NSMutableArray<GPBUnknownFieldSet*> *mutableGroupList_;
44 }
45
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 // 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"
75
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];
90       [copied release];
91     }
92   }
93   return result;
94 }
95
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;
101   BOOL equalVarint =
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;
113   BOOL equalLDList =
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;
122   return YES;
123 }
124
125 - (NSUInteger)hash {
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];
133   return result;
134 }
135
136 - (void)writeToOutput:(GPBCodedOutputStream *)output {
137   NSUInteger count = mutableVarintList_.count;
138   if (count > 0) {
139     [output writeUInt64Array:number_ values:mutableVarintList_ tag:0];
140   }
141   count = mutableFixed32List_.count;
142   if (count > 0) {
143     [output writeFixed32Array:number_ values:mutableFixed32List_ tag:0];
144   }
145   count = mutableFixed64List_.count;
146   if (count > 0) {
147     [output writeFixed64Array:number_ values:mutableFixed64List_ tag:0];
148   }
149   count = mutableLengthDelimitedList_.count;
150   if (count > 0) {
151     [output writeBytesArray:number_ values:mutableLengthDelimitedList_];
152   }
153   count = mutableGroupList_.count;
154   if (count > 0) {
155     [output writeUnknownGroupArray:number_ values:mutableGroupList_];
156   }
157 }
158
159 - (size_t)serializedSize {
160   __block size_t result = 0;
161   int32_t number = number_;
162   [mutableVarintList_
163       enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
164 #pragma unused(idx, stop)
165         result += GPBComputeUInt64Size(number, value);
166       }];
167
168   [mutableFixed32List_
169       enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) {
170 #pragma unused(idx, stop)
171         result += GPBComputeFixed32Size(number, value);
172       }];
173
174   [mutableFixed64List_
175       enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
176 #pragma unused(idx, stop)
177         result += GPBComputeFixed64Size(number, value);
178       }];
179
180   for (NSData *data in mutableLengthDelimitedList_) {
181     result += GPBComputeBytesSize(number, data);
182   }
183
184   for (GPBUnknownFieldSet *set in mutableGroupList_) {
185     result += GPBComputeUnknownGroupSize(number, set);
186   }
187
188   return result;
189 }
190
191 - (void)writeAsMessageSetExtensionToOutput:(GPBCodedOutputStream *)output {
192   for (NSData *data in mutableLengthDelimitedList_) {
193     [output writeRawMessageSetExtension:number_ value:data];
194   }
195 }
196
197 - (size_t)serializedSizeAsMessageSetExtension {
198   size_t result = 0;
199   for (NSData *data in mutableLengthDelimitedList_) {
200     result += GPBComputeRawMessageSetExtensionSize(number_, data);
201   }
202   return result;
203 }
204
205 - (NSString *)description {
206   NSMutableString *description =
207       [NSMutableString stringWithFormat:@"<%@ %p>: Field: %d {\n",
208        [self class], self, number_];
209   [mutableVarintList_
210       enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
211 #pragma unused(idx, stop)
212         [description appendFormat:@"\t%llu\n", value];
213       }];
214
215   [mutableFixed32List_
216       enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) {
217 #pragma unused(idx, stop)
218         [description appendFormat:@"\t%u\n", value];
219       }];
220
221   [mutableFixed64List_
222       enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) {
223 #pragma unused(idx, stop)
224         [description appendFormat:@"\t%llu\n", value];
225       }];
226
227   for (NSData *data in mutableLengthDelimitedList_) {
228     [description appendFormat:@"\t%@\n", data];
229   }
230
231   for (GPBUnknownFieldSet *set in mutableGroupList_) {
232     [description appendFormat:@"\t%@\n", set];
233   }
234   [description appendString:@"}"];
235   return description;
236 }
237
238 - (void)mergeFromField:(GPBUnknownField *)other {
239   GPBUInt64Array *otherVarintList = other.varintList;
240   if (otherVarintList.count > 0) {
241     if (mutableVarintList_ == nil) {
242       mutableVarintList_ = [otherVarintList copy];
243     } else {
244       [mutableVarintList_ addValuesFromArray:otherVarintList];
245     }
246   }
247
248   GPBUInt32Array *otherFixed32List = other.fixed32List;
249   if (otherFixed32List.count > 0) {
250     if (mutableFixed32List_ == nil) {
251       mutableFixed32List_ = [otherFixed32List copy];
252     } else {
253       [mutableFixed32List_ addValuesFromArray:otherFixed32List];
254     }
255   }
256
257   GPBUInt64Array *otherFixed64List = other.fixed64List;
258   if (otherFixed64List.count > 0) {
259     if (mutableFixed64List_ == nil) {
260       mutableFixed64List_ = [otherFixed64List copy];
261     } else {
262       [mutableFixed64List_ addValuesFromArray:otherFixed64List];
263     }
264   }
265
266   NSArray *otherLengthDelimitedList = other.lengthDelimitedList;
267   if (otherLengthDelimitedList.count > 0) {
268     if (mutableLengthDelimitedList_ == nil) {
269       mutableLengthDelimitedList_ = [otherLengthDelimitedList mutableCopy];
270     } else {
271       [mutableLengthDelimitedList_
272           addObjectsFromArray:otherLengthDelimitedList];
273     }
274   }
275
276   NSArray *otherGroupList = other.groupList;
277   if (otherGroupList.count > 0) {
278     if (mutableGroupList_ == nil) {
279       mutableGroupList_ =
280           [[NSMutableArray alloc] initWithCapacity:otherGroupList.count];
281     }
282     // Make our own mutable copies.
283     for (GPBUnknownFieldSet *group in otherGroupList) {
284       GPBUnknownFieldSet *copied = [group copy];
285       [mutableGroupList_ addObject:copied];
286       [copied release];
287     }
288   }
289 }
290
291 - (void)addVarint:(uint64_t)value {
292   if (mutableVarintList_ == nil) {
293     mutableVarintList_ = [[GPBUInt64Array alloc] initWithValues:&value count:1];
294   } else {
295     [mutableVarintList_ addValue:value];
296   }
297 }
298
299 - (void)addFixed32:(uint32_t)value {
300   if (mutableFixed32List_ == nil) {
301     mutableFixed32List_ =
302         [[GPBUInt32Array alloc] initWithValues:&value count:1];
303   } else {
304     [mutableFixed32List_ addValue:value];
305   }
306 }
307
308 - (void)addFixed64:(uint64_t)value {
309   if (mutableFixed64List_ == nil) {
310     mutableFixed64List_ =
311         [[GPBUInt64Array alloc] initWithValues:&value count:1];
312   } else {
313     [mutableFixed64List_ addValue:value];
314   }
315 }
316
317 - (void)addLengthDelimited:(NSData *)value {
318   if (mutableLengthDelimitedList_ == nil) {
319     mutableLengthDelimitedList_ =
320         [[NSMutableArray alloc] initWithObjects:&value count:1];
321   } else {
322     [mutableLengthDelimitedList_ addObject:value];
323   }
324 }
325
326 - (void)addGroup:(GPBUnknownFieldSet *)value {
327   if (mutableGroupList_ == nil) {
328     mutableGroupList_ = [[NSMutableArray alloc] initWithObjects:&value count:1];
329   } else {
330     [mutableGroupList_ addObject:value];
331   }
332 }
333
334 #pragma clang diagnostic pop
335
336 @end