GPBField.m 11 KB

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