GPBUnknownField.m 11 KB

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