GPBUnknownField.m 11 KB

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