GPBUnknownField.m 9.7 KB

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