forked from protocolbuffers/protobuf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GPBUnknownField.m
295 lines (258 loc) · 9.73 KB
/
GPBUnknownField.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#import "GPBUnknownField_PackagePrivate.h"
#import "GPBArray.h"
#import "GPBCodedOutputStream_PackagePrivate.h"
#import "GPBUnknownFieldSet.h"
@implementation GPBUnknownField {
@protected
int32_t number_;
GPBUInt64Array *mutableVarintList_;
GPBUInt32Array *mutableFixed32List_;
GPBUInt64Array *mutableFixed64List_;
NSMutableArray<NSData *> *mutableLengthDelimitedList_;
NSMutableArray<GPBUnknownFieldSet *> *mutableGroupList_;
}
@synthesize number = number_;
@synthesize varintList = mutableVarintList_;
@synthesize fixed32List = mutableFixed32List_;
@synthesize fixed64List = mutableFixed64List_;
@synthesize lengthDelimitedList = mutableLengthDelimitedList_;
@synthesize groupList = mutableGroupList_;
- (instancetype)initWithNumber:(int32_t)number {
if ((self = [super init])) {
number_ = number;
}
return self;
}
- (void)dealloc {
[mutableVarintList_ release];
[mutableFixed32List_ release];
[mutableFixed64List_ release];
[mutableLengthDelimitedList_ release];
[mutableGroupList_ release];
[super dealloc];
}
// 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.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdirect-ivar-access"
- (id)copyWithZone:(NSZone *)zone {
GPBUnknownField *result = [[GPBUnknownField allocWithZone:zone] initWithNumber:number_];
result->mutableFixed32List_ = [mutableFixed32List_ copyWithZone:zone];
result->mutableFixed64List_ = [mutableFixed64List_ copyWithZone:zone];
result->mutableLengthDelimitedList_ = [mutableLengthDelimitedList_ mutableCopyWithZone:zone];
result->mutableVarintList_ = [mutableVarintList_ copyWithZone:zone];
if (mutableGroupList_.count) {
result->mutableGroupList_ =
[[NSMutableArray allocWithZone:zone] initWithCapacity:mutableGroupList_.count];
for (GPBUnknownFieldSet *group in mutableGroupList_) {
GPBUnknownFieldSet *copied = [group copyWithZone:zone];
[result->mutableGroupList_ addObject:copied];
[copied release];
}
}
return result;
}
- (BOOL)isEqual:(id)object {
if (self == object) return YES;
if (![object isKindOfClass:[GPBUnknownField class]]) return NO;
GPBUnknownField *field = (GPBUnknownField *)object;
if (number_ != field->number_) return NO;
BOOL equalVarint = (mutableVarintList_.count == 0 && field->mutableVarintList_.count == 0) ||
[mutableVarintList_ isEqual:field->mutableVarintList_];
if (!equalVarint) return NO;
BOOL equalFixed32 = (mutableFixed32List_.count == 0 && field->mutableFixed32List_.count == 0) ||
[mutableFixed32List_ isEqual:field->mutableFixed32List_];
if (!equalFixed32) return NO;
BOOL equalFixed64 = (mutableFixed64List_.count == 0 && field->mutableFixed64List_.count == 0) ||
[mutableFixed64List_ isEqual:field->mutableFixed64List_];
if (!equalFixed64) return NO;
BOOL equalLDList =
(mutableLengthDelimitedList_.count == 0 && field->mutableLengthDelimitedList_.count == 0) ||
[mutableLengthDelimitedList_ isEqual:field->mutableLengthDelimitedList_];
if (!equalLDList) return NO;
BOOL equalGroupList = (mutableGroupList_.count == 0 && field->mutableGroupList_.count == 0) ||
[mutableGroupList_ isEqual:field->mutableGroupList_];
if (!equalGroupList) return NO;
return YES;
}
- (NSUInteger)hash {
// Just mix the hashes of the possible sub arrays.
const int prime = 31;
NSUInteger result = prime + [mutableVarintList_ hash];
result = prime * result + [mutableFixed32List_ hash];
result = prime * result + [mutableFixed64List_ hash];
result = prime * result + [mutableLengthDelimitedList_ hash];
result = prime * result + [mutableGroupList_ hash];
return result;
}
- (void)writeToOutput:(GPBCodedOutputStream *)output {
NSUInteger count = mutableVarintList_.count;
if (count > 0) {
[output writeUInt64Array:number_ values:mutableVarintList_ tag:0];
}
count = mutableFixed32List_.count;
if (count > 0) {
[output writeFixed32Array:number_ values:mutableFixed32List_ tag:0];
}
count = mutableFixed64List_.count;
if (count > 0) {
[output writeFixed64Array:number_ values:mutableFixed64List_ tag:0];
}
count = mutableLengthDelimitedList_.count;
if (count > 0) {
[output writeBytesArray:number_ values:mutableLengthDelimitedList_];
}
count = mutableGroupList_.count;
if (count > 0) {
[output writeUnknownGroupArray:number_ values:mutableGroupList_];
}
}
- (size_t)serializedSize {
__block size_t result = 0;
int32_t number = number_;
[mutableVarintList_
enumerateValuesWithBlock:^(uint64_t value, __unused NSUInteger idx, __unused BOOL *stop) {
result += GPBComputeUInt64Size(number, value);
}];
[mutableFixed32List_
enumerateValuesWithBlock:^(uint32_t value, __unused NSUInteger idx, __unused BOOL *stop) {
result += GPBComputeFixed32Size(number, value);
}];
[mutableFixed64List_
enumerateValuesWithBlock:^(uint64_t value, __unused NSUInteger idx, __unused BOOL *stop) {
result += GPBComputeFixed64Size(number, value);
}];
for (NSData *data in mutableLengthDelimitedList_) {
result += GPBComputeBytesSize(number, data);
}
for (GPBUnknownFieldSet *set in mutableGroupList_) {
result += GPBComputeUnknownGroupSize(number, set);
}
return result;
}
- (void)writeAsMessageSetExtensionToOutput:(GPBCodedOutputStream *)output {
for (NSData *data in mutableLengthDelimitedList_) {
[output writeRawMessageSetExtension:number_ value:data];
}
}
- (size_t)serializedSizeAsMessageSetExtension {
size_t result = 0;
for (NSData *data in mutableLengthDelimitedList_) {
result += GPBComputeRawMessageSetExtensionSize(number_, data);
}
return result;
}
- (NSString *)description {
NSMutableString *description =
[NSMutableString stringWithFormat:@"<%@ %p>: Field: %d {\n", [self class], self, number_];
[mutableVarintList_
enumerateValuesWithBlock:^(uint64_t value, __unused NSUInteger idx, __unused BOOL *stop) {
[description appendFormat:@"\t%llu\n", value];
}];
[mutableFixed32List_
enumerateValuesWithBlock:^(uint32_t value, __unused NSUInteger idx, __unused BOOL *stop) {
[description appendFormat:@"\t%u\n", value];
}];
[mutableFixed64List_
enumerateValuesWithBlock:^(uint64_t value, __unused NSUInteger idx, __unused BOOL *stop) {
[description appendFormat:@"\t%llu\n", value];
}];
for (NSData *data in mutableLengthDelimitedList_) {
[description appendFormat:@"\t%@\n", data];
}
for (GPBUnknownFieldSet *set in mutableGroupList_) {
[description appendFormat:@"\t%@\n", set];
}
[description appendString:@"}"];
return description;
}
- (void)mergeFromField:(GPBUnknownField *)other {
GPBUInt64Array *otherVarintList = other.varintList;
if (otherVarintList.count > 0) {
if (mutableVarintList_ == nil) {
mutableVarintList_ = [otherVarintList copy];
} else {
[mutableVarintList_ addValuesFromArray:otherVarintList];
}
}
GPBUInt32Array *otherFixed32List = other.fixed32List;
if (otherFixed32List.count > 0) {
if (mutableFixed32List_ == nil) {
mutableFixed32List_ = [otherFixed32List copy];
} else {
[mutableFixed32List_ addValuesFromArray:otherFixed32List];
}
}
GPBUInt64Array *otherFixed64List = other.fixed64List;
if (otherFixed64List.count > 0) {
if (mutableFixed64List_ == nil) {
mutableFixed64List_ = [otherFixed64List copy];
} else {
[mutableFixed64List_ addValuesFromArray:otherFixed64List];
}
}
NSArray *otherLengthDelimitedList = other.lengthDelimitedList;
if (otherLengthDelimitedList.count > 0) {
if (mutableLengthDelimitedList_ == nil) {
mutableLengthDelimitedList_ = [otherLengthDelimitedList mutableCopy];
} else {
[mutableLengthDelimitedList_ addObjectsFromArray:otherLengthDelimitedList];
}
}
NSArray *otherGroupList = other.groupList;
if (otherGroupList.count > 0) {
if (mutableGroupList_ == nil) {
mutableGroupList_ = [[NSMutableArray alloc] initWithCapacity:otherGroupList.count];
}
// Make our own mutable copies.
for (GPBUnknownFieldSet *group in otherGroupList) {
GPBUnknownFieldSet *copied = [group copy];
[mutableGroupList_ addObject:copied];
[copied release];
}
}
}
- (void)addVarint:(uint64_t)value {
if (mutableVarintList_ == nil) {
mutableVarintList_ = [[GPBUInt64Array alloc] initWithValues:&value count:1];
} else {
[mutableVarintList_ addValue:value];
}
}
- (void)addFixed32:(uint32_t)value {
if (mutableFixed32List_ == nil) {
mutableFixed32List_ = [[GPBUInt32Array alloc] initWithValues:&value count:1];
} else {
[mutableFixed32List_ addValue:value];
}
}
- (void)addFixed64:(uint64_t)value {
if (mutableFixed64List_ == nil) {
mutableFixed64List_ = [[GPBUInt64Array alloc] initWithValues:&value count:1];
} else {
[mutableFixed64List_ addValue:value];
}
}
- (void)addLengthDelimited:(NSData *)value {
if (mutableLengthDelimitedList_ == nil) {
mutableLengthDelimitedList_ = [[NSMutableArray alloc] initWithObjects:&value count:1];
} else {
[mutableLengthDelimitedList_ addObject:value];
}
}
- (void)addGroup:(GPBUnknownFieldSet *)value {
if (mutableGroupList_ == nil) {
mutableGroupList_ = [[NSMutableArray alloc] initWithObjects:&value count:1];
} else {
[mutableGroupList_ addObject:value];
}
}
#pragma clang diagnostic pop
@end