You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3256 lines
122 KiB

6 years ago
  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 "GPBMessage_PackagePrivate.h"
  31. #import <objc/runtime.h>
  32. #import <objc/message.h>
  33. #import "GPBArray_PackagePrivate.h"
  34. #import "GPBCodedInputStream_PackagePrivate.h"
  35. #import "GPBCodedOutputStream_PackagePrivate.h"
  36. #import "GPBDescriptor_PackagePrivate.h"
  37. #import "GPBDictionary_PackagePrivate.h"
  38. #import "GPBExtensionInternals.h"
  39. #import "GPBExtensionRegistry.h"
  40. #import "GPBRootObject_PackagePrivate.h"
  41. #import "GPBUnknownFieldSet_PackagePrivate.h"
  42. #import "GPBUtilities_PackagePrivate.h"
  43. // Direct access is use for speed, to avoid even internally declaring things
  44. // read/write, etc. The warning is enabled in the project to ensure code calling
  45. // protos can turn on -Wdirect-ivar-access without issues.
  46. #pragma clang diagnostic push
  47. #pragma clang diagnostic ignored "-Wdirect-ivar-access"
  48. NSString *const GPBMessageErrorDomain =
  49. GPBNSStringifySymbol(GPBMessageErrorDomain);
  50. NSString *const GPBErrorReasonKey = @"Reason";
  51. static NSString *const kGPBDataCoderKey = @"GPBData";
  52. //
  53. // PLEASE REMEMBER:
  54. //
  55. // This is the base class for *all* messages generated, so any selector defined,
  56. // *public* or *private* could end up colliding with a proto message field. So
  57. // avoid using selectors that could match a property, use C functions to hide
  58. // them, etc.
  59. //
  60. @interface GPBMessage () {
  61. @package
  62. GPBUnknownFieldSet *unknownFields_;
  63. NSMutableDictionary *extensionMap_;
  64. NSMutableDictionary *autocreatedExtensionMap_;
  65. // If the object was autocreated, we remember the creator so that if we get
  66. // mutated, we can inform the creator to make our field visible.
  67. GPBMessage *autocreator_;
  68. GPBFieldDescriptor *autocreatorField_;
  69. GPBExtensionDescriptor *autocreatorExtension_;
  70. }
  71. @end
  72. static id CreateArrayForField(GPBFieldDescriptor *field,
  73. GPBMessage *autocreator)
  74. __attribute__((ns_returns_retained));
  75. static id GetOrCreateArrayIvarWithField(GPBMessage *self,
  76. GPBFieldDescriptor *field,
  77. GPBFileSyntax syntax);
  78. static id GetArrayIvarWithField(GPBMessage *self, GPBFieldDescriptor *field);
  79. static id CreateMapForField(GPBFieldDescriptor *field,
  80. GPBMessage *autocreator)
  81. __attribute__((ns_returns_retained));
  82. static id GetOrCreateMapIvarWithField(GPBMessage *self,
  83. GPBFieldDescriptor *field,
  84. GPBFileSyntax syntax);
  85. static id GetMapIvarWithField(GPBMessage *self, GPBFieldDescriptor *field);
  86. static NSMutableDictionary *CloneExtensionMap(NSDictionary *extensionMap,
  87. NSZone *zone)
  88. __attribute__((ns_returns_retained));
  89. #ifdef DEBUG
  90. static NSError *MessageError(NSInteger code, NSDictionary *userInfo) {
  91. return [NSError errorWithDomain:GPBMessageErrorDomain
  92. code:code
  93. userInfo:userInfo];
  94. }
  95. #endif
  96. static NSError *ErrorFromException(NSException *exception) {
  97. NSError *error = nil;
  98. if ([exception.name isEqual:GPBCodedInputStreamException]) {
  99. NSDictionary *exceptionInfo = exception.userInfo;
  100. error = exceptionInfo[GPBCodedInputStreamUnderlyingErrorKey];
  101. }
  102. if (!error) {
  103. NSString *reason = exception.reason;
  104. NSDictionary *userInfo = nil;
  105. if ([reason length]) {
  106. userInfo = @{ GPBErrorReasonKey : reason };
  107. }
  108. error = [NSError errorWithDomain:GPBMessageErrorDomain
  109. code:GPBMessageErrorCodeOther
  110. userInfo:userInfo];
  111. }
  112. return error;
  113. }
  114. static void CheckExtension(GPBMessage *self,
  115. GPBExtensionDescriptor *extension) {
  116. if (![self isKindOfClass:extension.containingMessageClass]) {
  117. [NSException
  118. raise:NSInvalidArgumentException
  119. format:@"Extension %@ used on wrong class (%@ instead of %@)",
  120. extension.singletonName,
  121. [self class], extension.containingMessageClass];
  122. }
  123. }
  124. static NSMutableDictionary *CloneExtensionMap(NSDictionary *extensionMap,
  125. NSZone *zone) {
  126. if (extensionMap.count == 0) {
  127. return nil;
  128. }
  129. NSMutableDictionary *result = [[NSMutableDictionary allocWithZone:zone]
  130. initWithCapacity:extensionMap.count];
  131. for (GPBExtensionDescriptor *extension in extensionMap) {
  132. id value = [extensionMap objectForKey:extension];
  133. BOOL isMessageExtension = GPBExtensionIsMessage(extension);
  134. if (extension.repeated) {
  135. if (isMessageExtension) {
  136. NSMutableArray *list =
  137. [[NSMutableArray alloc] initWithCapacity:[value count]];
  138. for (GPBMessage *listValue in value) {
  139. GPBMessage *copiedValue = [listValue copyWithZone:zone];
  140. [list addObject:copiedValue];
  141. [copiedValue release];
  142. }
  143. [result setObject:list forKey:extension];
  144. [list release];
  145. } else {
  146. NSMutableArray *copiedValue = [value mutableCopyWithZone:zone];
  147. [result setObject:copiedValue forKey:extension];
  148. [copiedValue release];
  149. }
  150. } else {
  151. if (isMessageExtension) {
  152. GPBMessage *copiedValue = [value copyWithZone:zone];
  153. [result setObject:copiedValue forKey:extension];
  154. [copiedValue release];
  155. } else {
  156. [result setObject:value forKey:extension];
  157. }
  158. }
  159. }
  160. return result;
  161. }
  162. static id CreateArrayForField(GPBFieldDescriptor *field,
  163. GPBMessage *autocreator) {
  164. id result;
  165. GPBDataType fieldDataType = GPBGetFieldDataType(field);
  166. switch (fieldDataType) {
  167. case GPBDataTypeBool:
  168. result = [[GPBBoolArray alloc] init];
  169. break;
  170. case GPBDataTypeFixed32:
  171. case GPBDataTypeUInt32:
  172. result = [[GPBUInt32Array alloc] init];
  173. break;
  174. case GPBDataTypeInt32:
  175. case GPBDataTypeSFixed32:
  176. case GPBDataTypeSInt32:
  177. result = [[GPBInt32Array alloc] init];
  178. break;
  179. case GPBDataTypeFixed64:
  180. case GPBDataTypeUInt64:
  181. result = [[GPBUInt64Array alloc] init];
  182. break;
  183. case GPBDataTypeInt64:
  184. case GPBDataTypeSFixed64:
  185. case GPBDataTypeSInt64:
  186. result = [[GPBInt64Array alloc] init];
  187. break;
  188. case GPBDataTypeFloat:
  189. result = [[GPBFloatArray alloc] init];
  190. break;
  191. case GPBDataTypeDouble:
  192. result = [[GPBDoubleArray alloc] init];
  193. break;
  194. case GPBDataTypeEnum:
  195. result = [[GPBEnumArray alloc]
  196. initWithValidationFunction:field.enumDescriptor.enumVerifier];
  197. break;
  198. case GPBDataTypeBytes:
  199. case GPBDataTypeGroup:
  200. case GPBDataTypeMessage:
  201. case GPBDataTypeString:
  202. if (autocreator) {
  203. result = [[GPBAutocreatedArray alloc] init];
  204. } else {
  205. result = [[NSMutableArray alloc] init];
  206. }
  207. break;
  208. }
  209. if (autocreator) {
  210. if (GPBDataTypeIsObject(fieldDataType)) {
  211. GPBAutocreatedArray *autoArray = result;
  212. autoArray->_autocreator = autocreator;
  213. } else {
  214. GPBInt32Array *gpbArray = result;
  215. gpbArray->_autocreator = autocreator;
  216. }
  217. }
  218. return result;
  219. }
  220. static id CreateMapForField(GPBFieldDescriptor *field,
  221. GPBMessage *autocreator) {
  222. id result;
  223. GPBDataType keyDataType = field.mapKeyDataType;
  224. GPBDataType valueDataType = GPBGetFieldDataType(field);
  225. switch (keyDataType) {
  226. case GPBDataTypeBool:
  227. switch (valueDataType) {
  228. case GPBDataTypeBool:
  229. result = [[GPBBoolBoolDictionary alloc] init];
  230. break;
  231. case GPBDataTypeFixed32:
  232. case GPBDataTypeUInt32:
  233. result = [[GPBBoolUInt32Dictionary alloc] init];
  234. break;
  235. case GPBDataTypeInt32:
  236. case GPBDataTypeSFixed32:
  237. case GPBDataTypeSInt32:
  238. result = [[GPBBoolInt32Dictionary alloc] init];
  239. break;
  240. case GPBDataTypeFixed64:
  241. case GPBDataTypeUInt64:
  242. result = [[GPBBoolUInt64Dictionary alloc] init];
  243. break;
  244. case GPBDataTypeInt64:
  245. case GPBDataTypeSFixed64:
  246. case GPBDataTypeSInt64:
  247. result = [[GPBBoolInt64Dictionary alloc] init];
  248. break;
  249. case GPBDataTypeFloat:
  250. result = [[GPBBoolFloatDictionary alloc] init];
  251. break;
  252. case GPBDataTypeDouble:
  253. result = [[GPBBoolDoubleDictionary alloc] init];
  254. break;
  255. case GPBDataTypeEnum:
  256. result = [[GPBBoolEnumDictionary alloc]
  257. initWithValidationFunction:field.enumDescriptor.enumVerifier];
  258. break;
  259. case GPBDataTypeBytes:
  260. case GPBDataTypeMessage:
  261. case GPBDataTypeString:
  262. result = [[GPBBoolObjectDictionary alloc] init];
  263. break;
  264. case GPBDataTypeGroup:
  265. NSCAssert(NO, @"shouldn't happen");
  266. return nil;
  267. }
  268. break;
  269. case GPBDataTypeFixed32:
  270. case GPBDataTypeUInt32:
  271. switch (valueDataType) {
  272. case GPBDataTypeBool:
  273. result = [[GPBUInt32BoolDictionary alloc] init];
  274. break;
  275. case GPBDataTypeFixed32:
  276. case GPBDataTypeUInt32:
  277. result = [[GPBUInt32UInt32Dictionary alloc] init];
  278. break;
  279. case GPBDataTypeInt32:
  280. case GPBDataTypeSFixed32:
  281. case GPBDataTypeSInt32:
  282. result = [[GPBUInt32Int32Dictionary alloc] init];
  283. break;
  284. case GPBDataTypeFixed64:
  285. case GPBDataTypeUInt64:
  286. result = [[GPBUInt32UInt64Dictionary alloc] init];
  287. break;
  288. case GPBDataTypeInt64:
  289. case GPBDataTypeSFixed64:
  290. case GPBDataTypeSInt64:
  291. result = [[GPBUInt32Int64Dictionary alloc] init];
  292. break;
  293. case GPBDataTypeFloat:
  294. result = [[GPBUInt32FloatDictionary alloc] init];
  295. break;
  296. case GPBDataTypeDouble:
  297. result = [[GPBUInt32DoubleDictionary alloc] init];
  298. break;
  299. case GPBDataTypeEnum:
  300. result = [[GPBUInt32EnumDictionary alloc]
  301. initWithValidationFunction:field.enumDescriptor.enumVerifier];
  302. break;
  303. case GPBDataTypeBytes:
  304. case GPBDataTypeMessage:
  305. case GPBDataTypeString:
  306. result = [[GPBUInt32ObjectDictionary alloc] init];
  307. break;
  308. case GPBDataTypeGroup:
  309. NSCAssert(NO, @"shouldn't happen");
  310. return nil;
  311. }
  312. break;
  313. case GPBDataTypeInt32:
  314. case GPBDataTypeSFixed32:
  315. case GPBDataTypeSInt32:
  316. switch (valueDataType) {
  317. case GPBDataTypeBool:
  318. result = [[GPBInt32BoolDictionary alloc] init];
  319. break;
  320. case GPBDataTypeFixed32:
  321. case GPBDataTypeUInt32:
  322. result = [[GPBInt32UInt32Dictionary alloc] init];
  323. break;
  324. case GPBDataTypeInt32:
  325. case GPBDataTypeSFixed32:
  326. case GPBDataTypeSInt32:
  327. result = [[GPBInt32Int32Dictionary alloc] init];
  328. break;
  329. case GPBDataTypeFixed64:
  330. case GPBDataTypeUInt64:
  331. result = [[GPBInt32UInt64Dictionary alloc] init];
  332. break;
  333. case GPBDataTypeInt64:
  334. case GPBDataTypeSFixed64:
  335. case GPBDataTypeSInt64:
  336. result = [[GPBInt32Int64Dictionary alloc] init];
  337. break;
  338. case GPBDataTypeFloat:
  339. result = [[GPBInt32FloatDictionary alloc] init];
  340. break;
  341. case GPBDataTypeDouble:
  342. result = [[GPBInt32DoubleDictionary alloc] init];
  343. break;
  344. case GPBDataTypeEnum:
  345. result = [[GPBInt32EnumDictionary alloc]
  346. initWithValidationFunction:field.enumDescriptor.enumVerifier];
  347. break;
  348. case GPBDataTypeBytes:
  349. case GPBDataTypeMessage:
  350. case GPBDataTypeString:
  351. result = [[GPBInt32ObjectDictionary alloc] init];
  352. break;
  353. case GPBDataTypeGroup:
  354. NSCAssert(NO, @"shouldn't happen");
  355. return nil;
  356. }
  357. break;
  358. case GPBDataTypeFixed64:
  359. case GPBDataTypeUInt64:
  360. switch (valueDataType) {
  361. case GPBDataTypeBool:
  362. result = [[GPBUInt64BoolDictionary alloc] init];
  363. break;
  364. case GPBDataTypeFixed32:
  365. case GPBDataTypeUInt32:
  366. result = [[GPBUInt64UInt32Dictionary alloc] init];
  367. break;
  368. case GPBDataTypeInt32:
  369. case GPBDataTypeSFixed32:
  370. case GPBDataTypeSInt32:
  371. result = [[GPBUInt64Int32Dictionary alloc] init];
  372. break;
  373. case GPBDataTypeFixed64:
  374. case GPBDataTypeUInt64:
  375. result = [[GPBUInt64UInt64Dictionary alloc] init];
  376. break;
  377. case GPBDataTypeInt64:
  378. case GPBDataTypeSFixed64:
  379. case GPBDataTypeSInt64:
  380. result = [[GPBUInt64Int64Dictionary alloc] init];
  381. break;
  382. case GPBDataTypeFloat:
  383. result = [[GPBUInt64FloatDictionary alloc] init];
  384. break;
  385. case GPBDataTypeDouble:
  386. result = [[GPBUInt64DoubleDictionary alloc] init];
  387. break;
  388. case GPBDataTypeEnum:
  389. result = [[GPBUInt64EnumDictionary alloc]
  390. initWithValidationFunction:field.enumDescriptor.enumVerifier];
  391. break;
  392. case GPBDataTypeBytes:
  393. case GPBDataTypeMessage:
  394. case GPBDataTypeString:
  395. result = [[GPBUInt64ObjectDictionary alloc] init];
  396. break;
  397. case GPBDataTypeGroup:
  398. NSCAssert(NO, @"shouldn't happen");
  399. return nil;
  400. }
  401. break;
  402. case GPBDataTypeInt64:
  403. case GPBDataTypeSFixed64:
  404. case GPBDataTypeSInt64:
  405. switch (valueDataType) {
  406. case GPBDataTypeBool:
  407. result = [[GPBInt64BoolDictionary alloc] init];
  408. break;
  409. case GPBDataTypeFixed32:
  410. case GPBDataTypeUInt32:
  411. result = [[GPBInt64UInt32Dictionary alloc] init];
  412. break;
  413. case GPBDataTypeInt32:
  414. case GPBDataTypeSFixed32:
  415. case GPBDataTypeSInt32:
  416. result = [[GPBInt64Int32Dictionary alloc] init];
  417. break;
  418. case GPBDataTypeFixed64:
  419. case GPBDataTypeUInt64:
  420. result = [[GPBInt64UInt64Dictionary alloc] init];
  421. break;
  422. case GPBDataTypeInt64:
  423. case GPBDataTypeSFixed64:
  424. case GPBDataTypeSInt64:
  425. result = [[GPBInt64Int64Dictionary alloc] init];
  426. break;
  427. case GPBDataTypeFloat:
  428. result = [[GPBInt64FloatDictionary alloc] init];
  429. break;
  430. case GPBDataTypeDouble:
  431. result = [[GPBInt64DoubleDictionary alloc] init];
  432. break;
  433. case GPBDataTypeEnum:
  434. result = [[GPBInt64EnumDictionary alloc]
  435. initWithValidationFunction:field.enumDescriptor.enumVerifier];
  436. break;
  437. case GPBDataTypeBytes:
  438. case GPBDataTypeMessage:
  439. case GPBDataTypeString:
  440. result = [[GPBInt64ObjectDictionary alloc] init];
  441. break;
  442. case GPBDataTypeGroup:
  443. NSCAssert(NO, @"shouldn't happen");
  444. return nil;
  445. }
  446. break;
  447. case GPBDataTypeString:
  448. switch (valueDataType) {
  449. case GPBDataTypeBool:
  450. result = [[GPBStringBoolDictionary alloc] init];
  451. break;
  452. case GPBDataTypeFixed32:
  453. case GPBDataTypeUInt32:
  454. result = [[GPBStringUInt32Dictionary alloc] init];
  455. break;
  456. case GPBDataTypeInt32:
  457. case GPBDataTypeSFixed32:
  458. case GPBDataTypeSInt32:
  459. result = [[GPBStringInt32Dictionary alloc] init];
  460. break;
  461. case GPBDataTypeFixed64:
  462. case GPBDataTypeUInt64:
  463. result = [[GPBStringUInt64Dictionary alloc] init];
  464. break;
  465. case GPBDataTypeInt64:
  466. case GPBDataTypeSFixed64:
  467. case GPBDataTypeSInt64:
  468. result = [[GPBStringInt64Dictionary alloc] init];
  469. break;
  470. case GPBDataTypeFloat:
  471. result = [[GPBStringFloatDictionary alloc] init];
  472. break;
  473. case GPBDataTypeDouble:
  474. result = [[GPBStringDoubleDictionary alloc] init];
  475. break;
  476. case GPBDataTypeEnum:
  477. result = [[GPBStringEnumDictionary alloc]
  478. initWithValidationFunction:field.enumDescriptor.enumVerifier];
  479. break;
  480. case GPBDataTypeBytes:
  481. case GPBDataTypeMessage:
  482. case GPBDataTypeString:
  483. if (autocreator) {
  484. result = [[GPBAutocreatedDictionary alloc] init];
  485. } else {
  486. result = [[NSMutableDictionary alloc] init];
  487. }
  488. break;
  489. case GPBDataTypeGroup:
  490. NSCAssert(NO, @"shouldn't happen");
  491. return nil;
  492. }
  493. break;
  494. case GPBDataTypeFloat:
  495. case GPBDataTypeDouble:
  496. case GPBDataTypeEnum:
  497. case GPBDataTypeBytes:
  498. case GPBDataTypeGroup:
  499. case GPBDataTypeMessage:
  500. NSCAssert(NO, @"shouldn't happen");
  501. return nil;
  502. }
  503. if (autocreator) {
  504. if ((keyDataType == GPBDataTypeString) &&
  505. GPBDataTypeIsObject(valueDataType)) {
  506. GPBAutocreatedDictionary *autoDict = result;
  507. autoDict->_autocreator = autocreator;
  508. } else {
  509. GPBInt32Int32Dictionary *gpbDict = result;
  510. gpbDict->_autocreator = autocreator;
  511. }
  512. }
  513. return result;
  514. }
  515. #if !defined(__clang_analyzer__)
  516. // These functions are blocked from the analyzer because the analyzer sees the
  517. // GPBSetRetainedObjectIvarWithFieldInternal() call as consuming the array/map,
  518. // so use of the array/map after the call returns is flagged as a use after
  519. // free.
  520. // But GPBSetRetainedObjectIvarWithFieldInternal() is "consuming" the retain
  521. // count be holding onto the object (it is transfering it), the object is
  522. // still valid after returning from the call. The other way to avoid this
  523. // would be to add a -retain/-autorelease, but that would force every
  524. // repeated/map field parsed into the autorelease pool which is both a memory
  525. // and performance hit.
  526. static id GetOrCreateArrayIvarWithField(GPBMessage *self,
  527. GPBFieldDescriptor *field,
  528. GPBFileSyntax syntax) {
  529. id array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  530. if (!array) {
  531. // No lock needed, this is called from places expecting to mutate
  532. // so no threading protection is needed.
  533. array = CreateArrayForField(field, nil);
  534. GPBSetRetainedObjectIvarWithFieldInternal(self, field, array, syntax);
  535. }
  536. return array;
  537. }
  538. // This is like GPBGetObjectIvarWithField(), but for arrays, it should
  539. // only be used to wire the method into the class.
  540. static id GetArrayIvarWithField(GPBMessage *self, GPBFieldDescriptor *field) {
  541. id array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  542. if (!array) {
  543. // Check again after getting the lock.
  544. GPBPrepareReadOnlySemaphore(self);
  545. dispatch_semaphore_wait(self->readOnlySemaphore_, DISPATCH_TIME_FOREVER);
  546. array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  547. if (!array) {
  548. array = CreateArrayForField(field, self);
  549. GPBSetAutocreatedRetainedObjectIvarWithField(self, field, array);
  550. }
  551. dispatch_semaphore_signal(self->readOnlySemaphore_);
  552. }
  553. return array;
  554. }
  555. static id GetOrCreateMapIvarWithField(GPBMessage *self,
  556. GPBFieldDescriptor *field,
  557. GPBFileSyntax syntax) {
  558. id dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  559. if (!dict) {
  560. // No lock needed, this is called from places expecting to mutate
  561. // so no threading protection is needed.
  562. dict = CreateMapForField(field, nil);
  563. GPBSetRetainedObjectIvarWithFieldInternal(self, field, dict, syntax);
  564. }
  565. return dict;
  566. }
  567. // This is like GPBGetObjectIvarWithField(), but for maps, it should
  568. // only be used to wire the method into the class.
  569. static id GetMapIvarWithField(GPBMessage *self, GPBFieldDescriptor *field) {
  570. id dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  571. if (!dict) {
  572. // Check again after getting the lock.
  573. GPBPrepareReadOnlySemaphore(self);
  574. dispatch_semaphore_wait(self->readOnlySemaphore_, DISPATCH_TIME_FOREVER);
  575. dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  576. if (!dict) {
  577. dict = CreateMapForField(field, self);
  578. GPBSetAutocreatedRetainedObjectIvarWithField(self, field, dict);
  579. }
  580. dispatch_semaphore_signal(self->readOnlySemaphore_);
  581. }
  582. return dict;
  583. }
  584. #endif // !defined(__clang_analyzer__)
  585. GPBMessage *GPBCreateMessageWithAutocreator(Class msgClass,
  586. GPBMessage *autocreator,
  587. GPBFieldDescriptor *field) {
  588. GPBMessage *message = [[msgClass alloc] init];
  589. message->autocreator_ = autocreator;
  590. message->autocreatorField_ = [field retain];
  591. return message;
  592. }
  593. static GPBMessage *CreateMessageWithAutocreatorForExtension(
  594. Class msgClass, GPBMessage *autocreator, GPBExtensionDescriptor *extension)
  595. __attribute__((ns_returns_retained));
  596. static GPBMessage *CreateMessageWithAutocreatorForExtension(
  597. Class msgClass, GPBMessage *autocreator,
  598. GPBExtensionDescriptor *extension) {
  599. GPBMessage *message = [[msgClass alloc] init];
  600. message->autocreator_ = autocreator;
  601. message->autocreatorExtension_ = [extension retain];
  602. return message;
  603. }
  604. BOOL GPBWasMessageAutocreatedBy(GPBMessage *message, GPBMessage *parent) {
  605. return (message->autocreator_ == parent);
  606. }
  607. void GPBBecomeVisibleToAutocreator(GPBMessage *self) {
  608. // Message objects that are implicitly created by accessing a message field
  609. // are initially not visible via the hasX selector. This method makes them
  610. // visible.
  611. if (self->autocreator_) {
  612. // This will recursively make all parent messages visible until it reaches a
  613. // super-creator that's visible.
  614. if (self->autocreatorField_) {
  615. GPBFileSyntax syntax = [self->autocreator_ descriptor].file.syntax;
  616. GPBSetObjectIvarWithFieldInternal(self->autocreator_,
  617. self->autocreatorField_, self, syntax);
  618. } else {
  619. [self->autocreator_ setExtension:self->autocreatorExtension_ value:self];
  620. }
  621. }
  622. }
  623. void GPBAutocreatedArrayModified(GPBMessage *self, id array) {
  624. // When one of our autocreated arrays adds elements, make it visible.
  625. GPBDescriptor *descriptor = [[self class] descriptor];
  626. for (GPBFieldDescriptor *field in descriptor->fields_) {
  627. if (field.fieldType == GPBFieldTypeRepeated) {
  628. id curArray = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  629. if (curArray == array) {
  630. if (GPBFieldDataTypeIsObject(field)) {
  631. GPBAutocreatedArray *autoArray = array;
  632. autoArray->_autocreator = nil;
  633. } else {
  634. GPBInt32Array *gpbArray = array;
  635. gpbArray->_autocreator = nil;
  636. }
  637. GPBBecomeVisibleToAutocreator(self);
  638. return;
  639. }
  640. }
  641. }
  642. NSCAssert(NO, @"Unknown autocreated %@ for %@.", [array class], self);
  643. }
  644. void GPBAutocreatedDictionaryModified(GPBMessage *self, id dictionary) {
  645. // When one of our autocreated dicts adds elements, make it visible.
  646. GPBDescriptor *descriptor = [[self class] descriptor];
  647. for (GPBFieldDescriptor *field in descriptor->fields_) {
  648. if (field.fieldType == GPBFieldTypeMap) {
  649. id curDict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  650. if (curDict == dictionary) {
  651. if ((field.mapKeyDataType == GPBDataTypeString) &&
  652. GPBFieldDataTypeIsObject(field)) {
  653. GPBAutocreatedDictionary *autoDict = dictionary;
  654. autoDict->_autocreator = nil;
  655. } else {
  656. GPBInt32Int32Dictionary *gpbDict = dictionary;
  657. gpbDict->_autocreator = nil;
  658. }
  659. GPBBecomeVisibleToAutocreator(self);
  660. return;
  661. }
  662. }
  663. }
  664. NSCAssert(NO, @"Unknown autocreated %@ for %@.", [dictionary class], self);
  665. }
  666. void GPBClearMessageAutocreator(GPBMessage *self) {
  667. if ((self == nil) || !self->autocreator_) {
  668. return;
  669. }
  670. #if defined(DEBUG) && DEBUG && !defined(NS_BLOCK_ASSERTIONS)
  671. // Either the autocreator must have its "has" flag set to YES, or it must be
  672. // NO and not equal to ourselves.
  673. BOOL autocreatorHas =
  674. (self->autocreatorField_
  675. ? GPBGetHasIvarField(self->autocreator_, self->autocreatorField_)
  676. : [self->autocreator_ hasExtension:self->autocreatorExtension_]);
  677. GPBMessage *autocreatorFieldValue =
  678. (self->autocreatorField_
  679. ? GPBGetObjectIvarWithFieldNoAutocreate(self->autocreator_,
  680. self->autocreatorField_)
  681. : [self->autocreator_->autocreatedExtensionMap_
  682. objectForKey:self->autocreatorExtension_]);
  683. NSCAssert(autocreatorHas || autocreatorFieldValue != self,
  684. @"Cannot clear autocreator because it still refers to self, self: %@.",
  685. self);
  686. #endif // DEBUG && !defined(NS_BLOCK_ASSERTIONS)
  687. self->autocreator_ = nil;
  688. [self->autocreatorField_ release];
  689. self->autocreatorField_ = nil;
  690. [self->autocreatorExtension_ release];
  691. self->autocreatorExtension_ = nil;
  692. }
  693. // Call this before using the readOnlySemaphore_. This ensures it is created only once.
  694. void GPBPrepareReadOnlySemaphore(GPBMessage *self) {
  695. #pragma clang diagnostic push
  696. #pragma clang diagnostic ignored "-Wdirect-ivar-access"
  697. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  698. // Create the semaphore on demand (rather than init) as developers might not cause them
  699. // to be needed, and the heap usage can add up. The atomic swap is used to avoid needing
  700. // another lock around creating it.
  701. if (self->readOnlySemaphore_ == nil) {
  702. dispatch_semaphore_t worker = dispatch_semaphore_create(1);
  703. if (!OSAtomicCompareAndSwapPtrBarrier(NULL, worker, (void * volatile *)&(self->readOnlySemaphore_))) {
  704. dispatch_release(worker);
  705. }
  706. }
  707. #pragma clang diagnostic pop
  708. }
  709. static GPBUnknownFieldSet *GetOrMakeUnknownFields(GPBMessage *self) {
  710. if (!self->unknownFields_) {
  711. self->unknownFields_ = [[GPBUnknownFieldSet alloc] init];
  712. GPBBecomeVisibleToAutocreator(self);
  713. }
  714. return self->unknownFields_;
  715. }
  716. @implementation GPBMessage
  717. + (void)initialize {
  718. Class pbMessageClass = [GPBMessage class];
  719. if ([self class] == pbMessageClass) {
  720. // This is here to start up the "base" class descriptor.
  721. [self descriptor];
  722. // Message shares extension method resolving with GPBRootObject so insure
  723. // it is started up at the same time.
  724. (void)[GPBRootObject class];
  725. } else if ([self superclass] == pbMessageClass) {
  726. // This is here to start up all the "message" subclasses. Just needs to be
  727. // done for the messages, not any of the subclasses.
  728. // This must be done in initialize to enforce thread safety of start up of
  729. // the protocol buffer library.
  730. // Note: The generated code for -descriptor calls
  731. // +[GPBDescriptor allocDescriptorForClass:...], passing the GPBRootObject
  732. // subclass for the file. That call chain is what ensures that *Root class
  733. // is started up to support extension resolution off the message class
  734. // (+resolveClassMethod: below) in a thread safe manner.
  735. [self descriptor];
  736. }
  737. }
  738. + (instancetype)allocWithZone:(NSZone *)zone {
  739. // Override alloc to allocate our classes with the additional storage
  740. // required for the instance variables.
  741. GPBDescriptor *descriptor = [self descriptor];
  742. return NSAllocateObject(self, descriptor->storageSize_, zone);
  743. }
  744. + (instancetype)alloc {
  745. return [self allocWithZone:nil];
  746. }
  747. + (GPBDescriptor *)descriptor {
  748. // This is thread safe because it is called from +initialize.
  749. static GPBDescriptor *descriptor = NULL;
  750. static GPBFileDescriptor *fileDescriptor = NULL;
  751. if (!descriptor) {
  752. // Use a dummy file that marks it as proto2 syntax so when used generically
  753. // it supports unknowns/etc.
  754. fileDescriptor =
  755. [[GPBFileDescriptor alloc] initWithPackage:@"internal"
  756. syntax:GPBFileSyntaxProto2];
  757. descriptor = [GPBDescriptor allocDescriptorForClass:[GPBMessage class]
  758. rootClass:Nil
  759. file:fileDescriptor
  760. fields:NULL
  761. fieldCount:0
  762. storageSize:0
  763. flags:0];
  764. }
  765. return descriptor;
  766. }
  767. + (instancetype)message {
  768. return [[[self alloc] init] autorelease];
  769. }
  770. - (instancetype)init {
  771. if ((self = [super init])) {
  772. messageStorage_ = (GPBMessage_StoragePtr)(
  773. ((uint8_t *)self) + class_getInstanceSize([self class]));
  774. }
  775. return self;
  776. }
  777. - (instancetype)initWithData:(NSData *)data error:(NSError **)errorPtr {
  778. return [self initWithData:data extensionRegistry:nil error:errorPtr];
  779. }
  780. - (instancetype)initWithData:(NSData *)data
  781. extensionRegistry:(GPBExtensionRegistry *)extensionRegistry
  782. error:(NSError **)errorPtr {
  783. if ((self = [self init])) {
  784. @try {
  785. [self mergeFromData:data extensionRegistry:extensionRegistry];
  786. if (errorPtr) {
  787. *errorPtr = nil;
  788. }
  789. }
  790. @catch (NSException *exception) {
  791. [self release];
  792. self = nil;
  793. if (errorPtr) {
  794. *errorPtr = ErrorFromException(exception);
  795. }
  796. }
  797. #ifdef DEBUG
  798. if (self && !self.initialized) {
  799. [self release];
  800. self = nil;
  801. if (errorPtr) {
  802. *errorPtr = MessageError(GPBMessageErrorCodeMissingRequiredField, nil);
  803. }
  804. }
  805. #endif
  806. }
  807. return self;
  808. }
  809. - (instancetype)initWithCodedInputStream:(GPBCodedInputStream *)input
  810. extensionRegistry:
  811. (GPBExtensionRegistry *)extensionRegistry
  812. error:(NSError **)errorPtr {
  813. if ((self = [self init])) {
  814. @try {
  815. [self mergeFromCodedInputStream:input extensionRegistry:extensionRegistry];
  816. if (errorPtr) {
  817. *errorPtr = nil;
  818. }
  819. }
  820. @catch (NSException *exception) {
  821. [self release];
  822. self = nil;
  823. if (errorPtr) {
  824. *errorPtr = ErrorFromException(exception);
  825. }
  826. }
  827. #ifdef DEBUG
  828. if (self && !self.initialized) {
  829. [self release];
  830. self = nil;
  831. if (errorPtr) {
  832. *errorPtr = MessageError(GPBMessageErrorCodeMissingRequiredField, nil);
  833. }
  834. }
  835. #endif
  836. }
  837. return self;
  838. }
  839. - (void)dealloc {
  840. [self internalClear:NO];
  841. NSCAssert(!autocreator_, @"Autocreator was not cleared before dealloc.");
  842. if (readOnlySemaphore_) {
  843. dispatch_release(readOnlySemaphore_);
  844. }
  845. [super dealloc];
  846. }
  847. - (void)copyFieldsInto:(GPBMessage *)message
  848. zone:(NSZone *)zone
  849. descriptor:(GPBDescriptor *)descriptor {
  850. // Copy all the storage...
  851. memcpy(message->messageStorage_, messageStorage_, descriptor->storageSize_);
  852. GPBFileSyntax syntax = descriptor.file.syntax;
  853. // Loop over the fields doing fixup...
  854. for (GPBFieldDescriptor *field in descriptor->fields_) {
  855. if (GPBFieldIsMapOrArray(field)) {
  856. id value = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  857. if (value) {
  858. // We need to copy the array/map, but the catch is for message fields,
  859. // we also need to ensure all the messages as those need copying also.
  860. id newValue;
  861. if (GPBFieldDataTypeIsMessage(field)) {
  862. if (field.fieldType == GPBFieldTypeRepeated) {
  863. NSArray *existingArray = (NSArray *)value;
  864. NSMutableArray *newArray =
  865. [[NSMutableArray alloc] initWithCapacity:existingArray.count];
  866. newValue = newArray;
  867. for (GPBMessage *msg in existingArray) {
  868. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  869. [newArray addObject:copiedMsg];
  870. [copiedMsg release];
  871. }
  872. } else {
  873. if (field.mapKeyDataType == GPBDataTypeString) {
  874. // Map is an NSDictionary.
  875. NSDictionary *existingDict = value;
  876. NSMutableDictionary *newDict = [[NSMutableDictionary alloc]
  877. initWithCapacity:existingDict.count];
  878. newValue = newDict;
  879. [existingDict enumerateKeysAndObjectsUsingBlock:^(NSString *key,
  880. GPBMessage *msg,
  881. BOOL *stop) {
  882. #pragma unused(stop)
  883. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  884. [newDict setObject:copiedMsg forKey:key];
  885. [copiedMsg release];
  886. }];
  887. } else {
  888. // Is one of the GPB*ObjectDictionary classes. Type doesn't
  889. // matter, just need one to invoke the selector.
  890. GPBInt32ObjectDictionary *existingDict = value;
  891. newValue = [existingDict deepCopyWithZone:zone];
  892. }
  893. }
  894. } else {
  895. // Not messages (but is a map/array)...
  896. if (field.fieldType == GPBFieldTypeRepeated) {
  897. if (GPBFieldDataTypeIsObject(field)) {
  898. // NSArray
  899. newValue = [value mutableCopyWithZone:zone];
  900. } else {
  901. // GPB*Array
  902. newValue = [value copyWithZone:zone];
  903. }
  904. } else {
  905. if (field.mapKeyDataType == GPBDataTypeString) {
  906. // NSDictionary
  907. newValue = [value mutableCopyWithZone:zone];
  908. } else {
  909. // Is one of the GPB*Dictionary classes. Type doesn't matter,
  910. // just need one to invoke the selector.
  911. GPBInt32Int32Dictionary *existingDict = value;
  912. newValue = [existingDict copyWithZone:zone];
  913. }
  914. }
  915. }
  916. // We retain here because the memcpy picked up the pointer value and
  917. // the next call to SetRetainedObject... will release the current value.
  918. [value retain];
  919. GPBSetRetainedObjectIvarWithFieldInternal(message, field, newValue,
  920. syntax);
  921. }
  922. } else if (GPBFieldDataTypeIsMessage(field)) {
  923. // For object types, if we have a value, copy it. If we don't,
  924. // zero it to remove the pointer to something that was autocreated
  925. // (and the ptr just got memcpyed).
  926. if (GPBGetHasIvarField(self, field)) {
  927. GPBMessage *value = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  928. GPBMessage *newValue = [value copyWithZone:zone];
  929. // We retain here because the memcpy picked up the pointer value and
  930. // the next call to SetRetainedObject... will release the current value.
  931. [value retain];
  932. GPBSetRetainedObjectIvarWithFieldInternal(message, field, newValue,
  933. syntax);
  934. } else {
  935. uint8_t *storage = (uint8_t *)message->messageStorage_;
  936. id *typePtr = (id *)&storage[field->description_->offset];
  937. *typePtr = NULL;
  938. }
  939. } else if (GPBFieldDataTypeIsObject(field) &&
  940. GPBGetHasIvarField(self, field)) {
  941. // A set string/data value (message picked off above), copy it.
  942. id value = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  943. id newValue = [value copyWithZone:zone];
  944. // We retain here because the memcpy picked up the pointer value and
  945. // the next call to SetRetainedObject... will release the current value.
  946. [value retain];
  947. GPBSetRetainedObjectIvarWithFieldInternal(message, field, newValue,
  948. syntax);
  949. } else {
  950. // memcpy took care of the rest of the primitive fields if they were set.
  951. }
  952. } // for (field in descriptor->fields_)
  953. }
  954. - (id)copyWithZone:(NSZone *)zone {
  955. GPBDescriptor *descriptor = [self descriptor];
  956. GPBMessage *result = [[descriptor.messageClass allocWithZone:zone] init];
  957. [self copyFieldsInto:result zone:zone descriptor:descriptor];
  958. // Make immutable copies of the extra bits.
  959. result->unknownFields_ = [unknownFields_ copyWithZone:zone];
  960. result->extensionMap_ = CloneExtensionMap(extensionMap_, zone);
  961. return result;
  962. }
  963. - (void)clear {
  964. [self internalClear:YES];
  965. }
  966. - (void)internalClear:(BOOL)zeroStorage {
  967. GPBDescriptor *descriptor = [self descriptor];
  968. for (GPBFieldDescriptor *field in descriptor->fields_) {
  969. if (GPBFieldIsMapOrArray(field)) {
  970. id arrayOrMap = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  971. if (arrayOrMap) {
  972. if (field.fieldType == GPBFieldTypeRepeated) {
  973. if (GPBFieldDataTypeIsObject(field)) {
  974. if ([arrayOrMap isKindOfClass:[GPBAutocreatedArray class]]) {
  975. GPBAutocreatedArray *autoArray = arrayOrMap;
  976. if (autoArray->_autocreator == self) {
  977. autoArray->_autocreator = nil;
  978. }
  979. }
  980. } else {
  981. // Type doesn't matter, it is a GPB*Array.
  982. GPBInt32Array *gpbArray = arrayOrMap;
  983. if (gpbArray->_autocreator == self) {
  984. gpbArray->_autocreator = nil;
  985. }
  986. }
  987. } else {
  988. if ((field.mapKeyDataType == GPBDataTypeString) &&
  989. GPBFieldDataTypeIsObject(field)) {
  990. if ([arrayOrMap isKindOfClass:[GPBAutocreatedDictionary class]]) {
  991. GPBAutocreatedDictionary *autoDict = arrayOrMap;
  992. if (autoDict->_autocreator == self) {
  993. autoDict->_autocreator = nil;
  994. }
  995. }
  996. } else {
  997. // Type doesn't matter, it is a GPB*Dictionary.
  998. GPBInt32Int32Dictionary *gpbDict = arrayOrMap;
  999. if (gpbDict->_autocreator == self) {
  1000. gpbDict->_autocreator = nil;
  1001. }
  1002. }
  1003. }
  1004. [arrayOrMap release];
  1005. }
  1006. } else if (GPBFieldDataTypeIsMessage(field)) {
  1007. GPBClearAutocreatedMessageIvarWithField(self, field);
  1008. GPBMessage *value = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1009. [value release];
  1010. } else if (GPBFieldDataTypeIsObject(field) &&
  1011. GPBGetHasIvarField(self, field)) {
  1012. id value = GPBGetObjectIvarWithField(self, field);
  1013. [value release];
  1014. }
  1015. }
  1016. // GPBClearMessageAutocreator() expects that its caller has already been
  1017. // removed from autocreatedExtensionMap_ so we set to nil first.
  1018. NSArray *autocreatedValues = [autocreatedExtensionMap_ allValues];
  1019. [autocreatedExtensionMap_ release];
  1020. autocreatedExtensionMap_ = nil;
  1021. // Since we're clearing all of our extensions, make sure that we clear the
  1022. // autocreator on any that we've created so they no longer refer to us.
  1023. for (GPBMessage *value in autocreatedValues) {
  1024. NSCAssert(GPBWasMessageAutocreatedBy(value, self),
  1025. @"Autocreated extension does not refer back to self.");
  1026. GPBClearMessageAutocreator(value);
  1027. }
  1028. [extensionMap_ release];
  1029. extensionMap_ = nil;
  1030. [unknownFields_ release];
  1031. unknownFields_ = nil;
  1032. // Note that clearing does not affect autocreator_. If we are being cleared
  1033. // because of a dealloc, then autocreator_ should be nil anyway. If we are
  1034. // being cleared because someone explicitly clears us, we don't want to
  1035. // sever our relationship with our autocreator.
  1036. if (zeroStorage) {
  1037. memset(messageStorage_, 0, descriptor->storageSize_);
  1038. }
  1039. }
  1040. - (BOOL)isInitialized {
  1041. GPBDescriptor *descriptor = [self descriptor];
  1042. for (GPBFieldDescriptor *field in descriptor->fields_) {
  1043. if (field.isRequired) {
  1044. if (!GPBGetHasIvarField(self, field)) {
  1045. return NO;
  1046. }
  1047. }
  1048. if (GPBFieldDataTypeIsMessage(field)) {
  1049. GPBFieldType fieldType = field.fieldType;
  1050. if (fieldType == GPBFieldTypeSingle) {
  1051. if (field.isRequired) {
  1052. GPBMessage *message = GPBGetMessageMessageField(self, field);
  1053. if (!message.initialized) {
  1054. return NO;
  1055. }
  1056. } else {
  1057. NSAssert(field.isOptional,
  1058. @"%@: Single message field %@ not required or optional?",
  1059. [self class], field.name);
  1060. if (GPBGetHasIvarField(self, field)) {
  1061. GPBMessage *message = GPBGetMessageMessageField(self, field);
  1062. if (!message.initialized) {
  1063. return NO;
  1064. }
  1065. }
  1066. }
  1067. } else if (fieldType == GPBFieldTypeRepeated) {
  1068. NSArray *array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1069. for (GPBMessage *message in array) {
  1070. if (!message.initialized) {
  1071. return NO;
  1072. }
  1073. }
  1074. } else { // fieldType == GPBFieldTypeMap
  1075. if (field.mapKeyDataType == GPBDataTypeString) {
  1076. NSDictionary *map =
  1077. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1078. if (map && !GPBDictionaryIsInitializedInternalHelper(map, field)) {
  1079. return NO;
  1080. }
  1081. } else {
  1082. // Real type is GPB*ObjectDictionary, exact type doesn't matter.
  1083. GPBInt32ObjectDictionary *map =
  1084. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1085. if (map && ![map isInitialized]) {
  1086. return NO;
  1087. }
  1088. }
  1089. }
  1090. }
  1091. }
  1092. __block BOOL result = YES;
  1093. [extensionMap_
  1094. enumerateKeysAndObjectsUsingBlock:^(GPBExtensionDescriptor *extension,
  1095. id obj,
  1096. BOOL *stop) {
  1097. if (GPBExtensionIsMessage(extension)) {
  1098. if (extension.isRepeated) {
  1099. for (GPBMessage *msg in obj) {
  1100. if (!msg.initialized) {
  1101. result = NO;
  1102. *stop = YES;
  1103. break;
  1104. }
  1105. }
  1106. } else {
  1107. GPBMessage *asMsg = obj;
  1108. if (!asMsg.initialized) {
  1109. result = NO;
  1110. *stop = YES;
  1111. }
  1112. }
  1113. }
  1114. }];
  1115. return result;
  1116. }
  1117. - (GPBDescriptor *)descriptor {
  1118. return [[self class] descriptor];
  1119. }
  1120. - (NSData *)data {
  1121. #ifdef DEBUG
  1122. if (!self.initialized) {
  1123. return nil;
  1124. }
  1125. #endif
  1126. NSMutableData *data = [NSMutableData dataWithLength:[self serializedSize]];
  1127. GPBCodedOutputStream *stream =
  1128. [[GPBCodedOutputStream alloc] initWithData:data];
  1129. @try {
  1130. [self writeToCodedOutputStream:stream];
  1131. }
  1132. @catch (NSException *exception) {
  1133. // This really shouldn't happen. The only way writeToCodedOutputStream:
  1134. // could throw is if something in the library has a bug and the
  1135. // serializedSize was wrong.
  1136. #ifdef DEBUG
  1137. NSLog(@"%@: Internal exception while building message data: %@",
  1138. [self class], exception);
  1139. #endif
  1140. data = nil;
  1141. }
  1142. [stream release];
  1143. return data;
  1144. }
  1145. - (NSData *)delimitedData {
  1146. size_t serializedSize = [self serializedSize];
  1147. size_t varintSize = GPBComputeRawVarint32SizeForInteger(serializedSize);
  1148. NSMutableData *data =
  1149. [NSMutableData dataWithLength:(serializedSize + varintSize)];
  1150. GPBCodedOutputStream *stream =
  1151. [[GPBCodedOutputStream alloc] initWithData:data];
  1152. @try {
  1153. [self writeDelimitedToCodedOutputStream:stream];
  1154. }
  1155. @catch (NSException *exception) {
  1156. // This really shouldn't happen. The only way writeToCodedOutputStream:
  1157. // could throw is if something in the library has a bug and the
  1158. // serializedSize was wrong.
  1159. #ifdef DEBUG
  1160. NSLog(@"%@: Internal exception while building message delimitedData: %@",
  1161. [self class], exception);
  1162. #endif
  1163. // If it happens, truncate.
  1164. data.length = 0;
  1165. }
  1166. [stream release];
  1167. return data;
  1168. }
  1169. - (void)writeToOutputStream:(NSOutputStream *)output {
  1170. GPBCodedOutputStream *stream =
  1171. [[GPBCodedOutputStream alloc] initWithOutputStream:output];
  1172. [self writeToCodedOutputStream:stream];
  1173. [stream release];
  1174. }
  1175. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)output {
  1176. GPBDescriptor *descriptor = [self descriptor];
  1177. NSArray *fieldsArray = descriptor->fields_;
  1178. NSUInteger fieldCount = fieldsArray.count;
  1179. const GPBExtensionRange *extensionRanges = descriptor.extensionRanges;
  1180. NSUInteger extensionRangesCount = descriptor.extensionRangesCount;
  1181. for (NSUInteger i = 0, j = 0; i < fieldCount || j < extensionRangesCount;) {
  1182. if (i == fieldCount) {
  1183. [self writeExtensionsToCodedOutputStream:output
  1184. range:extensionRanges[j++]];
  1185. } else if (j == extensionRangesCount ||
  1186. GPBFieldNumber(fieldsArray[i]) < extensionRanges[j].start) {
  1187. [self writeField:fieldsArray[i++] toCodedOutputStream:output];
  1188. } else {
  1189. [self writeExtensionsToCodedOutputStream:output
  1190. range:extensionRanges[j++]];
  1191. }
  1192. }
  1193. if (descriptor.isWireFormat) {
  1194. [unknownFields_ writeAsMessageSetTo:output];
  1195. } else {
  1196. [unknownFields_ writeToCodedOutputStream:output];
  1197. }
  1198. }
  1199. - (void)writeDelimitedToOutputStream:(NSOutputStream *)output {
  1200. GPBCodedOutputStream *codedOutput =
  1201. [[GPBCodedOutputStream alloc] initWithOutputStream:output];
  1202. [self writeDelimitedToCodedOutputStream:codedOutput];
  1203. [codedOutput release];
  1204. }
  1205. - (void)writeDelimitedToCodedOutputStream:(GPBCodedOutputStream *)output {
  1206. [output writeRawVarintSizeTAs32:[self serializedSize]];
  1207. [self writeToCodedOutputStream:output];
  1208. }
  1209. - (void)writeField:(GPBFieldDescriptor *)field
  1210. toCodedOutputStream:(GPBCodedOutputStream *)output {
  1211. GPBFieldType fieldType = field.fieldType;
  1212. if (fieldType == GPBFieldTypeSingle) {
  1213. BOOL has = GPBGetHasIvarField(self, field);
  1214. if (!has) {
  1215. return;
  1216. }
  1217. }
  1218. uint32_t fieldNumber = GPBFieldNumber(field);
  1219. //%PDDM-DEFINE FIELD_CASE(TYPE, REAL_TYPE)
  1220. //%FIELD_CASE_FULL(TYPE, REAL_TYPE, REAL_TYPE)
  1221. //%PDDM-DEFINE FIELD_CASE_FULL(TYPE, REAL_TYPE, ARRAY_TYPE)
  1222. //% case GPBDataType##TYPE:
  1223. //% if (fieldType == GPBFieldTypeRepeated) {
  1224. //% uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
  1225. //% GPB##ARRAY_TYPE##Array *array =
  1226. //% GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1227. //% [output write##TYPE##Array:fieldNumber values:array tag:tag];
  1228. //% } else if (fieldType == GPBFieldTypeSingle) {
  1229. //% [output write##TYPE:fieldNumber
  1230. //% TYPE$S value:GPBGetMessage##REAL_TYPE##Field(self, field)];
  1231. //% } else { // fieldType == GPBFieldTypeMap
  1232. //% // Exact type here doesn't matter.
  1233. //% GPBInt32##ARRAY_TYPE##Dictionary *dict =
  1234. //% GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1235. //% [dict writeToCodedOutputStream:output asField:field];
  1236. //% }
  1237. //% break;
  1238. //%
  1239. //%PDDM-DEFINE FIELD_CASE2(TYPE)
  1240. //% case GPBDataType##TYPE:
  1241. //% if (fieldType == GPBFieldTypeRepeated) {
  1242. //% NSArray *array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1243. //% [output write##TYPE##Array:fieldNumber values:array];
  1244. //% } else if (fieldType == GPBFieldTypeSingle) {
  1245. //% // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has check
  1246. //% // again.
  1247. //% [output write##TYPE:fieldNumber
  1248. //% TYPE$S value:GPBGetObjectIvarWithFieldNoAutocreate(self, field)];
  1249. //% } else { // fieldType == GPBFieldTypeMap
  1250. //% // Exact type here doesn't matter.
  1251. //% id dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1252. //% GPBDataType mapKeyDataType = field.mapKeyDataType;
  1253. //% if (mapKeyDataType == GPBDataTypeString) {
  1254. //% GPBDictionaryWriteToStreamInternalHelper(output, dict, field);
  1255. //% } else {
  1256. //% [dict writeToCodedOutputStream:output asField:field];
  1257. //% }
  1258. //% }
  1259. //% break;
  1260. //%
  1261. switch (GPBGetFieldDataType(field)) {
  1262. //%PDDM-EXPAND FIELD_CASE(Bool, Bool)
  1263. // This block of code is generated, do not edit it directly.
  1264. case GPBDataTypeBool:
  1265. if (fieldType == GPBFieldTypeRepeated) {
  1266. uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
  1267. GPBBoolArray *array =
  1268. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1269. [output writeBoolArray:fieldNumber values:array tag:tag];
  1270. } else if (fieldType == GPBFieldTypeSingle) {
  1271. [output writeBool:fieldNumber
  1272. value:GPBGetMessageBoolField(self, field)];
  1273. } else { // fieldType == GPBFieldTypeMap
  1274. // Exact type here doesn't matter.
  1275. GPBInt32BoolDictionary *dict =
  1276. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1277. [dict writeToCodedOutputStream:output asField:field];
  1278. }
  1279. break;
  1280. //%PDDM-EXPAND FIELD_CASE(Fixed32, UInt32)
  1281. // This block of code is generated, do not edit it directly.
  1282. case GPBDataTypeFixed32:
  1283. if (fieldType == GPBFieldTypeRepeated) {
  1284. uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
  1285. GPBUInt32Array *array =
  1286. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1287. [output writeFixed32Array:fieldNumber values:array tag:tag];
  1288. } else if (fieldType == GPBFieldTypeSingle) {
  1289. [output writeFixed32:fieldNumber
  1290. value:GPBGetMessageUInt32Field(self, field)];
  1291. } else { // fieldType == GPBFieldTypeMap
  1292. // Exact type here doesn't matter.
  1293. GPBInt32UInt32Dictionary *dict =
  1294. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1295. [dict writeToCodedOutputStream:output asField:field];
  1296. }
  1297. break;
  1298. //%PDDM-EXPAND FIELD_CASE(SFixed32, Int32)
  1299. // This block of code is generated, do not edit it directly.
  1300. case GPBDataTypeSFixed32:
  1301. if (fieldType == GPBFieldTypeRepeated) {
  1302. uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
  1303. GPBInt32Array *array =
  1304. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1305. [output writeSFixed32Array:fieldNumber values:array tag:tag];
  1306. } else if (fieldType == GPBFieldTypeSingle) {
  1307. [output writeSFixed32:fieldNumber
  1308. value:GPBGetMessageInt32Field(self, field)];
  1309. } else { // fieldType == GPBFieldTypeMap
  1310. // Exact type here doesn't matter.
  1311. GPBInt32Int32Dictionary *dict =
  1312. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1313. [dict writeToCodedOutputStream:output asField:field];
  1314. }
  1315. break;
  1316. //%PDDM-EXPAND FIELD_CASE(Float, Float)
  1317. // This block of code is generated, do not edit it directly.
  1318. case GPBDataTypeFloat:
  1319. if (fieldType == GPBFieldTypeRepeated) {
  1320. uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
  1321. GPBFloatArray *array =
  1322. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1323. [output writeFloatArray:fieldNumber values:array tag:tag];
  1324. } else if (fieldType == GPBFieldTypeSingle) {
  1325. [output writeFloat:fieldNumber
  1326. value:GPBGetMessageFloatField(self, field)];
  1327. } else { // fieldType == GPBFieldTypeMap
  1328. // Exact type here doesn't matter.
  1329. GPBInt32FloatDictionary *dict =
  1330. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1331. [dict writeToCodedOutputStream:output asField:field];
  1332. }
  1333. break;
  1334. //%PDDM-EXPAND FIELD_CASE(Fixed64, UInt64)
  1335. // This block of code is generated, do not edit it directly.
  1336. case GPBDataTypeFixed64:
  1337. if (fieldType == GPBFieldTypeRepeated) {
  1338. uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
  1339. GPBUInt64Array *array =
  1340. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1341. [output writeFixed64Array:fieldNumber values:array tag:tag];
  1342. } else if (fieldType == GPBFieldTypeSingle) {
  1343. [output writeFixed64:fieldNumber
  1344. value:GPBGetMessageUInt64Field(self, field)];
  1345. } else { // fieldType == GPBFieldTypeMap
  1346. // Exact type here doesn't matter.
  1347. GPBInt32UInt64Dictionary *dict =
  1348. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1349. [dict writeToCodedOutputStream:output asField:field];
  1350. }
  1351. break;
  1352. //%PDDM-EXPAND FIELD_CASE(SFixed64, Int64)
  1353. // This block of code is generated, do not edit it directly.
  1354. case GPBDataTypeSFixed64:
  1355. if (fieldType == GPBFieldTypeRepeated) {
  1356. uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
  1357. GPBInt64Array *array =
  1358. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1359. [output writeSFixed64Array:fieldNumber values:array tag:tag];
  1360. } else if (fieldType == GPBFieldTypeSingle) {
  1361. [output writeSFixed64:fieldNumber
  1362. value:GPBGetMessageInt64Field(self, field)];
  1363. } else { // fieldType == GPBFieldTypeMap
  1364. // Exact type here doesn't matter.
  1365. GPBInt32Int64Dictionary *dict =
  1366. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1367. [dict writeToCodedOutputStream:output asField:field];
  1368. }
  1369. break;
  1370. //%PDDM-EXPAND FIELD_CASE(Double, Double)
  1371. // This block of code is generated, do not edit it directly.
  1372. case GPBDataTypeDouble:
  1373. if (fieldType == GPBFieldTypeRepeated) {
  1374. uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
  1375. GPBDoubleArray *array =
  1376. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1377. [output writeDoubleArray:fieldNumber values:array tag:tag];
  1378. } else if (fieldType == GPBFieldTypeSingle) {
  1379. [output writeDouble:fieldNumber
  1380. value:GPBGetMessageDoubleField(self, field)];
  1381. } else { // fieldType == GPBFieldTypeMap
  1382. // Exact type here doesn't matter.
  1383. GPBInt32DoubleDictionary *dict =
  1384. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1385. [dict writeToCodedOutputStream:output asField:field];
  1386. }
  1387. break;
  1388. //%PDDM-EXPAND FIELD_CASE(Int32, Int32)
  1389. // This block of code is generated, do not edit it directly.
  1390. case GPBDataTypeInt32:
  1391. if (fieldType == GPBFieldTypeRepeated) {
  1392. uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
  1393. GPBInt32Array *array =
  1394. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1395. [output writeInt32Array:fieldNumber values:array tag:tag];
  1396. } else if (fieldType == GPBFieldTypeSingle) {
  1397. [output writeInt32:fieldNumber
  1398. value:GPBGetMessageInt32Field(self, field)];
  1399. } else { // fieldType == GPBFieldTypeMap
  1400. // Exact type here doesn't matter.
  1401. GPBInt32Int32Dictionary *dict =
  1402. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1403. [dict writeToCodedOutputStream:output asField:field];
  1404. }
  1405. break;
  1406. //%PDDM-EXPAND FIELD_CASE(Int64, Int64)
  1407. // This block of code is generated, do not edit it directly.
  1408. case GPBDataTypeInt64:
  1409. if (fieldType == GPBFieldTypeRepeated) {
  1410. uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
  1411. GPBInt64Array *array =
  1412. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1413. [output writeInt64Array:fieldNumber values:array tag:tag];
  1414. } else if (fieldType == GPBFieldTypeSingle) {
  1415. [output writeInt64:fieldNumber
  1416. value:GPBGetMessageInt64Field(self, field)];
  1417. } else { // fieldType == GPBFieldTypeMap
  1418. // Exact type here doesn't matter.
  1419. GPBInt32Int64Dictionary *dict =
  1420. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1421. [dict writeToCodedOutputStream:output asField:field];
  1422. }
  1423. break;
  1424. //%PDDM-EXPAND FIELD_CASE(SInt32, Int32)
  1425. // This block of code is generated, do not edit it directly.
  1426. case GPBDataTypeSInt32:
  1427. if (fieldType == GPBFieldTypeRepeated) {
  1428. uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
  1429. GPBInt32Array *array =
  1430. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1431. [output writeSInt32Array:fieldNumber values:array tag:tag];
  1432. } else if (fieldType == GPBFieldTypeSingle) {
  1433. [output writeSInt32:fieldNumber
  1434. value:GPBGetMessageInt32Field(self, field)];
  1435. } else { // fieldType == GPBFieldTypeMap
  1436. // Exact type here doesn't matter.
  1437. GPBInt32Int32Dictionary *dict =
  1438. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1439. [dict writeToCodedOutputStream:output asField:field];
  1440. }
  1441. break;
  1442. //%PDDM-EXPAND FIELD_CASE(SInt64, Int64)
  1443. // This block of code is generated, do not edit it directly.
  1444. case GPBDataTypeSInt64:
  1445. if (fieldType == GPBFieldTypeRepeated) {
  1446. uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
  1447. GPBInt64Array *array =
  1448. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1449. [output writeSInt64Array:fieldNumber values:array tag:tag];
  1450. } else if (fieldType == GPBFieldTypeSingle) {
  1451. [output writeSInt64:fieldNumber
  1452. value:GPBGetMessageInt64Field(self, field)];
  1453. } else { // fieldType == GPBFieldTypeMap
  1454. // Exact type here doesn't matter.
  1455. GPBInt32Int64Dictionary *dict =
  1456. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1457. [dict writeToCodedOutputStream:output asField:field];
  1458. }
  1459. break;
  1460. //%PDDM-EXPAND FIELD_CASE(UInt32, UInt32)
  1461. // This block of code is generated, do not edit it directly.
  1462. case GPBDataTypeUInt32:
  1463. if (fieldType == GPBFieldTypeRepeated) {
  1464. uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
  1465. GPBUInt32Array *array =
  1466. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1467. [output writeUInt32Array:fieldNumber values:array tag:tag];
  1468. } else if (fieldType == GPBFieldTypeSingle) {
  1469. [output writeUInt32:fieldNumber
  1470. value:GPBGetMessageUInt32Field(self, field)];
  1471. } else { // fieldType == GPBFieldTypeMap
  1472. // Exact type here doesn't matter.
  1473. GPBInt32UInt32Dictionary *dict =
  1474. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1475. [dict writeToCodedOutputStream:output asField:field];
  1476. }
  1477. break;
  1478. //%PDDM-EXPAND FIELD_CASE(UInt64, UInt64)
  1479. // This block of code is generated, do not edit it directly.
  1480. case GPBDataTypeUInt64:
  1481. if (fieldType == GPBFieldTypeRepeated) {
  1482. uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
  1483. GPBUInt64Array *array =
  1484. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1485. [output writeUInt64Array:fieldNumber values:array tag:tag];
  1486. } else if (fieldType == GPBFieldTypeSingle) {
  1487. [output writeUInt64:fieldNumber
  1488. value:GPBGetMessageUInt64Field(self, field)];
  1489. } else { // fieldType == GPBFieldTypeMap
  1490. // Exact type here doesn't matter.
  1491. GPBInt32UInt64Dictionary *dict =
  1492. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1493. [dict writeToCodedOutputStream:output asField:field];
  1494. }
  1495. break;
  1496. //%PDDM-EXPAND FIELD_CASE_FULL(Enum, Int32, Enum)
  1497. // This block of code is generated, do not edit it directly.
  1498. case GPBDataTypeEnum:
  1499. if (fieldType == GPBFieldTypeRepeated) {
  1500. uint32_t tag = field.isPackable ? GPBFieldTag(field) : 0;
  1501. GPBEnumArray *array =
  1502. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1503. [output writeEnumArray:fieldNumber values:array tag:tag];
  1504. } else if (fieldType == GPBFieldTypeSingle) {
  1505. [output writeEnum:fieldNumber
  1506. value:GPBGetMessageInt32Field(self, field)];
  1507. } else { // fieldType == GPBFieldTypeMap
  1508. // Exact type here doesn't matter.
  1509. GPBInt32EnumDictionary *dict =
  1510. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1511. [dict writeToCodedOutputStream:output asField:field];
  1512. }
  1513. break;
  1514. //%PDDM-EXPAND FIELD_CASE2(Bytes)
  1515. // This block of code is generated, do not edit it directly.
  1516. case GPBDataTypeBytes:
  1517. if (fieldType == GPBFieldTypeRepeated) {
  1518. NSArray *array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1519. [output writeBytesArray:fieldNumber values:array];
  1520. } else if (fieldType == GPBFieldTypeSingle) {
  1521. // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has check
  1522. // again.
  1523. [output writeBytes:fieldNumber
  1524. value:GPBGetObjectIvarWithFieldNoAutocreate(self, field)];
  1525. } else { // fieldType == GPBFieldTypeMap
  1526. // Exact type here doesn't matter.
  1527. id dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1528. GPBDataType mapKeyDataType = field.mapKeyDataType;
  1529. if (mapKeyDataType == GPBDataTypeString) {
  1530. GPBDictionaryWriteToStreamInternalHelper(output, dict, field);
  1531. } else {
  1532. [dict writeToCodedOutputStream:output asField:field];
  1533. }
  1534. }
  1535. break;
  1536. //%PDDM-EXPAND FIELD_CASE2(String)
  1537. // This block of code is generated, do not edit it directly.
  1538. case GPBDataTypeString:
  1539. if (fieldType == GPBFieldTypeRepeated) {
  1540. NSArray *array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1541. [output writeStringArray:fieldNumber values:array];
  1542. } else if (fieldType == GPBFieldTypeSingle) {
  1543. // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has check
  1544. // again.
  1545. [output writeString:fieldNumber
  1546. value:GPBGetObjectIvarWithFieldNoAutocreate(self, field)];
  1547. } else { // fieldType == GPBFieldTypeMap
  1548. // Exact type here doesn't matter.
  1549. id dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1550. GPBDataType mapKeyDataType = field.mapKeyDataType;
  1551. if (mapKeyDataType == GPBDataTypeString) {
  1552. GPBDictionaryWriteToStreamInternalHelper(output, dict, field);
  1553. } else {
  1554. [dict writeToCodedOutputStream:output asField:field];
  1555. }
  1556. }
  1557. break;
  1558. //%PDDM-EXPAND FIELD_CASE2(Message)
  1559. // This block of code is generated, do not edit it directly.
  1560. case GPBDataTypeMessage:
  1561. if (fieldType == GPBFieldTypeRepeated) {
  1562. NSArray *array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1563. [output writeMessageArray:fieldNumber values:array];
  1564. } else if (fieldType == GPBFieldTypeSingle) {
  1565. // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has check
  1566. // again.
  1567. [output writeMessage:fieldNumber
  1568. value:GPBGetObjectIvarWithFieldNoAutocreate(self, field)];
  1569. } else { // fieldType == GPBFieldTypeMap
  1570. // Exact type here doesn't matter.
  1571. id dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1572. GPBDataType mapKeyDataType = field.mapKeyDataType;
  1573. if (mapKeyDataType == GPBDataTypeString) {
  1574. GPBDictionaryWriteToStreamInternalHelper(output, dict, field);
  1575. } else {
  1576. [dict writeToCodedOutputStream:output asField:field];
  1577. }
  1578. }
  1579. break;
  1580. //%PDDM-EXPAND FIELD_CASE2(Group)
  1581. // This block of code is generated, do not edit it directly.
  1582. case GPBDataTypeGroup:
  1583. if (fieldType == GPBFieldTypeRepeated) {
  1584. NSArray *array = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1585. [output writeGroupArray:fieldNumber values:array];
  1586. } else if (fieldType == GPBFieldTypeSingle) {
  1587. // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has check
  1588. // again.
  1589. [output writeGroup:fieldNumber
  1590. value:GPBGetObjectIvarWithFieldNoAutocreate(self, field)];
  1591. } else { // fieldType == GPBFieldTypeMap
  1592. // Exact type here doesn't matter.
  1593. id dict = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1594. GPBDataType mapKeyDataType = field.mapKeyDataType;
  1595. if (mapKeyDataType == GPBDataTypeString) {
  1596. GPBDictionaryWriteToStreamInternalHelper(output, dict, field);
  1597. } else {
  1598. [dict writeToCodedOutputStream:output asField:field];
  1599. }
  1600. }
  1601. break;
  1602. //%PDDM-EXPAND-END (18 expansions)
  1603. }
  1604. }
  1605. #pragma mark - Extensions
  1606. - (id)getExtension:(GPBExtensionDescriptor *)extension {
  1607. CheckExtension(self, extension);
  1608. id value = [extensionMap_ objectForKey:extension];
  1609. if (value != nil) {
  1610. return value;
  1611. }
  1612. // No default for repeated.
  1613. if (extension.isRepeated) {
  1614. return nil;
  1615. }
  1616. // Non messages get their default.
  1617. if (!GPBExtensionIsMessage(extension)) {
  1618. return extension.defaultValue;
  1619. }
  1620. // Check for an autocreated value.
  1621. GPBPrepareReadOnlySemaphore(self);
  1622. dispatch_semaphore_wait(readOnlySemaphore_, DISPATCH_TIME_FOREVER);
  1623. value = [autocreatedExtensionMap_ objectForKey:extension];
  1624. if (!value) {
  1625. // Auto create the message extensions to match normal fields.
  1626. value = CreateMessageWithAutocreatorForExtension(extension.msgClass, self,
  1627. extension);
  1628. if (autocreatedExtensionMap_ == nil) {
  1629. autocreatedExtensionMap_ = [[NSMutableDictionary alloc] init];
  1630. }
  1631. // We can't simply call setExtension here because that would clear the new
  1632. // value's autocreator.
  1633. [autocreatedExtensionMap_ setObject:value forKey:extension];
  1634. [value release];
  1635. }
  1636. dispatch_semaphore_signal(readOnlySemaphore_);
  1637. return value;
  1638. }
  1639. - (id)getExistingExtension:(GPBExtensionDescriptor *)extension {
  1640. // This is an internal method so we don't need to call CheckExtension().
  1641. return [extensionMap_ objectForKey:extension];
  1642. }
  1643. - (BOOL)hasExtension:(GPBExtensionDescriptor *)extension {
  1644. #if defined(DEBUG) && DEBUG
  1645. CheckExtension(self, extension);
  1646. #endif // DEBUG
  1647. return nil != [extensionMap_ objectForKey:extension];
  1648. }
  1649. - (NSArray *)extensionsCurrentlySet {
  1650. return [extensionMap_ allKeys];
  1651. }
  1652. - (void)writeExtensionsToCodedOutputStream:(GPBCodedOutputStream *)output
  1653. range:(GPBExtensionRange)range {
  1654. NSArray *sortedExtensions = [[extensionMap_ allKeys]
  1655. sortedArrayUsingSelector:@selector(compareByFieldNumber:)];
  1656. uint32_t start = range.start;
  1657. uint32_t end = range.end;
  1658. for (GPBExtensionDescriptor *extension in sortedExtensions) {
  1659. uint32_t fieldNumber = extension.fieldNumber;
  1660. if (fieldNumber >= start && fieldNumber < end) {
  1661. id value = [extensionMap_ objectForKey:extension];
  1662. GPBWriteExtensionValueToOutputStream(extension, value, output);
  1663. }
  1664. }
  1665. }
  1666. - (void)setExtension:(GPBExtensionDescriptor *)extension value:(id)value {
  1667. if (!value) {
  1668. [self clearExtension:extension];
  1669. return;
  1670. }
  1671. CheckExtension(self, extension);
  1672. if (extension.repeated) {
  1673. [NSException raise:NSInvalidArgumentException
  1674. format:@"Must call addExtension() for repeated types."];
  1675. }
  1676. if (extensionMap_ == nil) {
  1677. extensionMap_ = [[NSMutableDictionary alloc] init];
  1678. }
  1679. // This pointless cast is for CLANG_WARN_NULLABLE_TO_NONNULL_CONVERSION.
  1680. // Without it, the compiler complains we're passing an id nullable when
  1681. // setObject:forKey: requires a id nonnull for the value. The check for
  1682. // !value at the start of the method ensures it isn't nil, but the check
  1683. // isn't smart enough to realize that.
  1684. [extensionMap_ setObject:(id)value forKey:extension];
  1685. GPBExtensionDescriptor *descriptor = extension;
  1686. if (GPBExtensionIsMessage(descriptor) && !descriptor.isRepeated) {
  1687. GPBMessage *autocreatedValue =
  1688. [[autocreatedExtensionMap_ objectForKey:extension] retain];
  1689. // Must remove from the map before calling GPBClearMessageAutocreator() so
  1690. // that GPBClearMessageAutocreator() knows its safe to clear.
  1691. [autocreatedExtensionMap_ removeObjectForKey:extension];
  1692. GPBClearMessageAutocreator(autocreatedValue);
  1693. [autocreatedValue release];
  1694. }
  1695. GPBBecomeVisibleToAutocreator(self);
  1696. }
  1697. - (void)addExtension:(GPBExtensionDescriptor *)extension value:(id)value {
  1698. CheckExtension(self, extension);
  1699. if (!extension.repeated) {
  1700. [NSException raise:NSInvalidArgumentException
  1701. format:@"Must call setExtension() for singular types."];
  1702. }
  1703. if (extensionMap_ == nil) {
  1704. extensionMap_ = [[NSMutableDictionary alloc] init];
  1705. }
  1706. NSMutableArray *list = [extensionMap_ objectForKey:extension];
  1707. if (list == nil) {
  1708. list = [NSMutableArray array];
  1709. [extensionMap_ setObject:list forKey:extension];
  1710. }
  1711. [list addObject:value];
  1712. GPBBecomeVisibleToAutocreator(self);
  1713. }
  1714. - (void)setExtension:(GPBExtensionDescriptor *)extension
  1715. index:(NSUInteger)idx
  1716. value:(id)value {
  1717. CheckExtension(self, extension);
  1718. if (!extension.repeated) {
  1719. [NSException raise:NSInvalidArgumentException
  1720. format:@"Must call setExtension() for singular types."];
  1721. }
  1722. if (extensionMap_ == nil) {
  1723. extensionMap_ = [[NSMutableDictionary alloc] init];
  1724. }
  1725. NSMutableArray *list = [extensionMap_ objectForKey:extension];
  1726. [list replaceObjectAtIndex:idx withObject:value];
  1727. GPBBecomeVisibleToAutocreator(self);
  1728. }
  1729. - (void)clearExtension:(GPBExtensionDescriptor *)extension {
  1730. CheckExtension(self, extension);
  1731. // Only become visible if there was actually a value to clear.
  1732. if ([extensionMap_ objectForKey:extension]) {
  1733. [extensionMap_ removeObjectForKey:extension];
  1734. GPBBecomeVisibleToAutocreator(self);
  1735. }
  1736. }
  1737. #pragma mark - mergeFrom
  1738. - (void)mergeFromData:(NSData *)data
  1739. extensionRegistry:(GPBExtensionRegistry *)extensionRegistry {
  1740. GPBCodedInputStream *input = [[GPBCodedInputStream alloc] initWithData:data];
  1741. [self mergeFromCodedInputStream:input extensionRegistry:extensionRegistry];
  1742. [input checkLastTagWas:0];
  1743. [input release];
  1744. }
  1745. #pragma mark - mergeDelimitedFrom
  1746. - (void)mergeDelimitedFromCodedInputStream:(GPBCodedInputStream *)input
  1747. extensionRegistry:(GPBExtensionRegistry *)extensionRegistry {
  1748. GPBCodedInputStreamState *state = &input->state_;
  1749. if (GPBCodedInputStreamIsAtEnd(state)) {
  1750. return;
  1751. }
  1752. NSData *data = GPBCodedInputStreamReadRetainedBytesNoCopy(state);
  1753. if (data == nil) {
  1754. return;
  1755. }
  1756. [self mergeFromData:data extensionRegistry:extensionRegistry];
  1757. [data release];
  1758. }
  1759. #pragma mark - Parse From Data Support
  1760. + (instancetype)parseFromData:(NSData *)data error:(NSError **)errorPtr {
  1761. return [self parseFromData:data extensionRegistry:nil error:errorPtr];
  1762. }
  1763. + (instancetype)parseFromData:(NSData *)data
  1764. extensionRegistry:(GPBExtensionRegistry *)extensionRegistry
  1765. error:(NSError **)errorPtr {
  1766. return [[[self alloc] initWithData:data
  1767. extensionRegistry:extensionRegistry
  1768. error:errorPtr] autorelease];
  1769. }
  1770. + (instancetype)parseFromCodedInputStream:(GPBCodedInputStream *)input
  1771. extensionRegistry:(GPBExtensionRegistry *)extensionRegistry
  1772. error:(NSError **)errorPtr {
  1773. return
  1774. [[[self alloc] initWithCodedInputStream:input
  1775. extensionRegistry:extensionRegistry
  1776. error:errorPtr] autorelease];
  1777. }
  1778. #pragma mark - Parse Delimited From Data Support
  1779. + (instancetype)parseDelimitedFromCodedInputStream:(GPBCodedInputStream *)input
  1780. extensionRegistry:
  1781. (GPBExtensionRegistry *)extensionRegistry
  1782. error:(NSError **)errorPtr {
  1783. GPBMessage *message = [[[self alloc] init] autorelease];
  1784. @try {
  1785. [message mergeDelimitedFromCodedInputStream:input
  1786. extensionRegistry:extensionRegistry];
  1787. if (errorPtr) {
  1788. *errorPtr = nil;
  1789. }
  1790. }
  1791. @catch (NSException *exception) {
  1792. message = nil;
  1793. if (errorPtr) {
  1794. *errorPtr = ErrorFromException(exception);
  1795. }
  1796. }
  1797. #ifdef DEBUG
  1798. if (message && !message.initialized) {
  1799. message = nil;
  1800. if (errorPtr) {
  1801. *errorPtr = MessageError(GPBMessageErrorCodeMissingRequiredField, nil);
  1802. }
  1803. }
  1804. #endif
  1805. return message;
  1806. }
  1807. #pragma mark - Unknown Field Support
  1808. - (GPBUnknownFieldSet *)unknownFields {
  1809. return unknownFields_;
  1810. }
  1811. - (void)setUnknownFields:(GPBUnknownFieldSet *)unknownFields {
  1812. if (unknownFields != unknownFields_) {
  1813. [unknownFields_ release];
  1814. unknownFields_ = [unknownFields copy];
  1815. GPBBecomeVisibleToAutocreator(self);
  1816. }
  1817. }
  1818. - (void)parseMessageSet:(GPBCodedInputStream *)input
  1819. extensionRegistry:(GPBExtensionRegistry *)extensionRegistry {
  1820. uint32_t typeId = 0;
  1821. NSData *rawBytes = nil;
  1822. GPBExtensionDescriptor *extension = nil;
  1823. GPBCodedInputStreamState *state = &input->state_;
  1824. while (true) {
  1825. uint32_t tag = GPBCodedInputStreamReadTag(state);
  1826. if (tag == 0) {
  1827. break;
  1828. }
  1829. if (tag == GPBWireFormatMessageSetTypeIdTag) {
  1830. typeId = GPBCodedInputStreamReadUInt32(state);
  1831. if (typeId != 0) {
  1832. extension = [extensionRegistry extensionForDescriptor:[self descriptor]
  1833. fieldNumber:typeId];
  1834. }
  1835. } else if (tag == GPBWireFormatMessageSetMessageTag) {
  1836. rawBytes =
  1837. [GPBCodedInputStreamReadRetainedBytesNoCopy(state) autorelease];
  1838. } else {
  1839. if (![input skipField:tag]) {
  1840. break;
  1841. }
  1842. }
  1843. }
  1844. [input checkLastTagWas:GPBWireFormatMessageSetItemEndTag];
  1845. if (rawBytes != nil && typeId != 0) {
  1846. if (extension != nil) {
  1847. GPBCodedInputStream *newInput =
  1848. [[GPBCodedInputStream alloc] initWithData:rawBytes];
  1849. GPBExtensionMergeFromInputStream(extension,
  1850. extension.packable,
  1851. newInput,
  1852. extensionRegistry,
  1853. self);
  1854. [newInput release];
  1855. } else {
  1856. GPBUnknownFieldSet *unknownFields = GetOrMakeUnknownFields(self);
  1857. [unknownFields mergeMessageSetMessage:typeId data:rawBytes];
  1858. }
  1859. }
  1860. }
  1861. - (BOOL)parseUnknownField:(GPBCodedInputStream *)input
  1862. extensionRegistry:(GPBExtensionRegistry *)extensionRegistry
  1863. tag:(uint32_t)tag {
  1864. GPBWireFormat wireType = GPBWireFormatGetTagWireType(tag);
  1865. int32_t fieldNumber = GPBWireFormatGetTagFieldNumber(tag);
  1866. GPBDescriptor *descriptor = [self descriptor];
  1867. GPBExtensionDescriptor *extension =
  1868. [extensionRegistry extensionForDescriptor:descriptor
  1869. fieldNumber:fieldNumber];
  1870. if (extension == nil) {
  1871. if (descriptor.wireFormat && GPBWireFormatMessageSetItemTag == tag) {
  1872. [self parseMessageSet:input extensionRegistry:extensionRegistry];
  1873. return YES;
  1874. }
  1875. } else {
  1876. if (extension.wireType == wireType) {
  1877. GPBExtensionMergeFromInputStream(extension,
  1878. extension.packable,
  1879. input,
  1880. extensionRegistry,
  1881. self);
  1882. return YES;
  1883. }
  1884. // Primitive, repeated types can be packed on unpacked on the wire, and are
  1885. // parsed either way.
  1886. if ([extension isRepeated] &&
  1887. !GPBDataTypeIsObject(extension->description_->dataType) &&
  1888. (extension.alternateWireType == wireType)) {
  1889. GPBExtensionMergeFromInputStream(extension,
  1890. !extension.packable,
  1891. input,
  1892. extensionRegistry,
  1893. self);
  1894. return YES;
  1895. }
  1896. }
  1897. if ([GPBUnknownFieldSet isFieldTag:tag]) {
  1898. GPBUnknownFieldSet *unknownFields = GetOrMakeUnknownFields(self);
  1899. return [unknownFields mergeFieldFrom:tag input:input];
  1900. } else {
  1901. return NO;
  1902. }
  1903. }
  1904. - (void)addUnknownMapEntry:(int32_t)fieldNum value:(NSData *)data {
  1905. GPBUnknownFieldSet *unknownFields = GetOrMakeUnknownFields(self);
  1906. [unknownFields addUnknownMapEntry:fieldNum value:data];
  1907. }
  1908. #pragma mark - MergeFromCodedInputStream Support
  1909. static void MergeSingleFieldFromCodedInputStream(
  1910. GPBMessage *self, GPBFieldDescriptor *field, GPBFileSyntax syntax,
  1911. GPBCodedInputStream *input, GPBExtensionRegistry *extensionRegistry) {
  1912. GPBDataType fieldDataType = GPBGetFieldDataType(field);
  1913. switch (fieldDataType) {
  1914. #define CASE_SINGLE_POD(NAME, TYPE, FUNC_TYPE) \
  1915. case GPBDataType##NAME: { \
  1916. TYPE val = GPBCodedInputStreamRead##NAME(&input->state_); \
  1917. GPBSet##FUNC_TYPE##IvarWithFieldInternal(self, field, val, syntax); \
  1918. break; \
  1919. }
  1920. #define CASE_SINGLE_OBJECT(NAME) \
  1921. case GPBDataType##NAME: { \
  1922. id val = GPBCodedInputStreamReadRetained##NAME(&input->state_); \
  1923. GPBSetRetainedObjectIvarWithFieldInternal(self, field, val, syntax); \
  1924. break; \
  1925. }
  1926. CASE_SINGLE_POD(Bool, BOOL, Bool)
  1927. CASE_SINGLE_POD(Fixed32, uint32_t, UInt32)
  1928. CASE_SINGLE_POD(SFixed32, int32_t, Int32)
  1929. CASE_SINGLE_POD(Float, float, Float)
  1930. CASE_SINGLE_POD(Fixed64, uint64_t, UInt64)
  1931. CASE_SINGLE_POD(SFixed64, int64_t, Int64)
  1932. CASE_SINGLE_POD(Double, double, Double)
  1933. CASE_SINGLE_POD(Int32, int32_t, Int32)
  1934. CASE_SINGLE_POD(Int64, int64_t, Int64)
  1935. CASE_SINGLE_POD(SInt32, int32_t, Int32)
  1936. CASE_SINGLE_POD(SInt64, int64_t, Int64)
  1937. CASE_SINGLE_POD(UInt32, uint32_t, UInt32)
  1938. CASE_SINGLE_POD(UInt64, uint64_t, UInt64)
  1939. CASE_SINGLE_OBJECT(Bytes)
  1940. CASE_SINGLE_OBJECT(String)
  1941. #undef CASE_SINGLE_POD
  1942. #undef CASE_SINGLE_OBJECT
  1943. case GPBDataTypeMessage: {
  1944. if (GPBGetHasIvarField(self, field)) {
  1945. // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has
  1946. // check again.
  1947. GPBMessage *message =
  1948. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1949. [input readMessage:message extensionRegistry:extensionRegistry];
  1950. } else {
  1951. GPBMessage *message = [[field.msgClass alloc] init];
  1952. [input readMessage:message extensionRegistry:extensionRegistry];
  1953. GPBSetRetainedObjectIvarWithFieldInternal(self, field, message, syntax);
  1954. }
  1955. break;
  1956. }
  1957. case GPBDataTypeGroup: {
  1958. if (GPBGetHasIvarField(self, field)) {
  1959. // GPBGetObjectIvarWithFieldNoAutocreate() avoids doing the has
  1960. // check again.
  1961. GPBMessage *message =
  1962. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  1963. [input readGroup:GPBFieldNumber(field)
  1964. message:message
  1965. extensionRegistry:extensionRegistry];
  1966. } else {
  1967. GPBMessage *message = [[field.msgClass alloc] init];
  1968. [input readGroup:GPBFieldNumber(field)
  1969. message:message
  1970. extensionRegistry:extensionRegistry];
  1971. GPBSetRetainedObjectIvarWithFieldInternal(self, field, message, syntax);
  1972. }
  1973. break;
  1974. }
  1975. case GPBDataTypeEnum: {
  1976. int32_t val = GPBCodedInputStreamReadEnum(&input->state_);
  1977. if (GPBHasPreservingUnknownEnumSemantics(syntax) ||
  1978. [field isValidEnumValue:val]) {
  1979. GPBSetInt32IvarWithFieldInternal(self, field, val, syntax);
  1980. } else {
  1981. GPBUnknownFieldSet *unknownFields = GetOrMakeUnknownFields(self);
  1982. [unknownFields mergeVarintField:GPBFieldNumber(field) value:val];
  1983. }
  1984. }
  1985. } // switch
  1986. }
  1987. static void MergeRepeatedPackedFieldFromCodedInputStream(
  1988. GPBMessage *self, GPBFieldDescriptor *field, GPBFileSyntax syntax,
  1989. GPBCodedInputStream *input) {
  1990. GPBDataType fieldDataType = GPBGetFieldDataType(field);
  1991. GPBCodedInputStreamState *state = &input->state_;
  1992. id genericArray = GetOrCreateArrayIvarWithField(self, field, syntax);
  1993. int32_t length = GPBCodedInputStreamReadInt32(state);
  1994. size_t limit = GPBCodedInputStreamPushLimit(state, length);
  1995. while (GPBCodedInputStreamBytesUntilLimit(state) > 0) {
  1996. switch (fieldDataType) {
  1997. #define CASE_REPEATED_PACKED_POD(NAME, TYPE, ARRAY_TYPE) \
  1998. case GPBDataType##NAME: { \
  1999. TYPE val = GPBCodedInputStreamRead##NAME(state); \
  2000. [(GPB##ARRAY_TYPE##Array *)genericArray addValue:val]; \
  2001. break; \
  2002. }
  2003. CASE_REPEATED_PACKED_POD(Bool, BOOL, Bool)
  2004. CASE_REPEATED_PACKED_POD(Fixed32, uint32_t, UInt32)
  2005. CASE_REPEATED_PACKED_POD(SFixed32, int32_t, Int32)
  2006. CASE_REPEATED_PACKED_POD(Float, float, Float)
  2007. CASE_REPEATED_PACKED_POD(Fixed64, uint64_t, UInt64)
  2008. CASE_REPEATED_PACKED_POD(SFixed64, int64_t, Int64)
  2009. CASE_REPEATED_PACKED_POD(Double, double, Double)
  2010. CASE_REPEATED_PACKED_POD(Int32, int32_t, Int32)
  2011. CASE_REPEATED_PACKED_POD(Int64, int64_t, Int64)
  2012. CASE_REPEATED_PACKED_POD(SInt32, int32_t, Int32)
  2013. CASE_REPEATED_PACKED_POD(SInt64, int64_t, Int64)
  2014. CASE_REPEATED_PACKED_POD(UInt32, uint32_t, UInt32)
  2015. CASE_REPEATED_PACKED_POD(UInt64, uint64_t, UInt64)
  2016. #undef CASE_REPEATED_PACKED_POD
  2017. case GPBDataTypeBytes:
  2018. case GPBDataTypeString:
  2019. case GPBDataTypeMessage:
  2020. case GPBDataTypeGroup:
  2021. NSCAssert(NO, @"Non primitive types can't be packed");
  2022. break;
  2023. case GPBDataTypeEnum: {
  2024. int32_t val = GPBCodedInputStreamReadEnum(state);
  2025. if (GPBHasPreservingUnknownEnumSemantics(syntax) ||
  2026. [field isValidEnumValue:val]) {
  2027. [(GPBEnumArray*)genericArray addRawValue:val];
  2028. } else {
  2029. GPBUnknownFieldSet *unknownFields = GetOrMakeUnknownFields(self);
  2030. [unknownFields mergeVarintField:GPBFieldNumber(field) value:val];
  2031. }
  2032. break;
  2033. }
  2034. } // switch
  2035. } // while(BytesUntilLimit() > 0)
  2036. GPBCodedInputStreamPopLimit(state, limit);
  2037. }
  2038. static void MergeRepeatedNotPackedFieldFromCodedInputStream(
  2039. GPBMessage *self, GPBFieldDescriptor *field, GPBFileSyntax syntax,
  2040. GPBCodedInputStream *input, GPBExtensionRegistry *extensionRegistry) {
  2041. GPBCodedInputStreamState *state = &input->state_;
  2042. id genericArray = GetOrCreateArrayIvarWithField(self, field, syntax);
  2043. switch (GPBGetFieldDataType(field)) {
  2044. #define CASE_REPEATED_NOT_PACKED_POD(NAME, TYPE, ARRAY_TYPE) \
  2045. case GPBDataType##NAME: { \
  2046. TYPE val = GPBCodedInputStreamRead##NAME(state); \
  2047. [(GPB##ARRAY_TYPE##Array *)genericArray addValue:val]; \
  2048. break; \
  2049. }
  2050. #define CASE_REPEATED_NOT_PACKED_OBJECT(NAME) \
  2051. case GPBDataType##NAME: { \
  2052. id val = GPBCodedInputStreamReadRetained##NAME(state); \
  2053. [(NSMutableArray*)genericArray addObject:val]; \
  2054. [val release]; \
  2055. break; \
  2056. }
  2057. CASE_REPEATED_NOT_PACKED_POD(Bool, BOOL, Bool)
  2058. CASE_REPEATED_NOT_PACKED_POD(Fixed32, uint32_t, UInt32)
  2059. CASE_REPEATED_NOT_PACKED_POD(SFixed32, int32_t, Int32)
  2060. CASE_REPEATED_NOT_PACKED_POD(Float, float, Float)
  2061. CASE_REPEATED_NOT_PACKED_POD(Fixed64, uint64_t, UInt64)
  2062. CASE_REPEATED_NOT_PACKED_POD(SFixed64, int64_t, Int64)
  2063. CASE_REPEATED_NOT_PACKED_POD(Double, double, Double)
  2064. CASE_REPEATED_NOT_PACKED_POD(Int32, int32_t, Int32)
  2065. CASE_REPEATED_NOT_PACKED_POD(Int64, int64_t, Int64)
  2066. CASE_REPEATED_NOT_PACKED_POD(SInt32, int32_t, Int32)
  2067. CASE_REPEATED_NOT_PACKED_POD(SInt64, int64_t, Int64)
  2068. CASE_REPEATED_NOT_PACKED_POD(UInt32, uint32_t, UInt32)
  2069. CASE_REPEATED_NOT_PACKED_POD(UInt64, uint64_t, UInt64)
  2070. CASE_REPEATED_NOT_PACKED_OBJECT(Bytes)
  2071. CASE_REPEATED_NOT_PACKED_OBJECT(String)
  2072. #undef CASE_REPEATED_NOT_PACKED_POD
  2073. #undef CASE_NOT_PACKED_OBJECT
  2074. case GPBDataTypeMessage: {
  2075. GPBMessage *message = [[field.msgClass alloc] init];
  2076. [input readMessage:message extensionRegistry:extensionRegistry];
  2077. [(NSMutableArray*)genericArray addObject:message];
  2078. [message release];
  2079. break;
  2080. }
  2081. case GPBDataTypeGroup: {
  2082. GPBMessage *message = [[field.msgClass alloc] init];
  2083. [input readGroup:GPBFieldNumber(field)
  2084. message:message
  2085. extensionRegistry:extensionRegistry];
  2086. [(NSMutableArray*)genericArray addObject:message];
  2087. [message release];
  2088. break;
  2089. }
  2090. case GPBDataTypeEnum: {
  2091. int32_t val = GPBCodedInputStreamReadEnum(state);
  2092. if (GPBHasPreservingUnknownEnumSemantics(syntax) ||
  2093. [field isValidEnumValue:val]) {
  2094. [(GPBEnumArray*)genericArray addRawValue:val];
  2095. } else {
  2096. GPBUnknownFieldSet *unknownFields = GetOrMakeUnknownFields(self);
  2097. [unknownFields mergeVarintField:GPBFieldNumber(field) value:val];
  2098. }
  2099. break;
  2100. }
  2101. } // switch
  2102. }
  2103. - (void)mergeFromCodedInputStream:(GPBCodedInputStream *)input
  2104. extensionRegistry:(GPBExtensionRegistry *)extensionRegistry {
  2105. GPBDescriptor *descriptor = [self descriptor];
  2106. GPBFileSyntax syntax = descriptor.file.syntax;
  2107. GPBCodedInputStreamState *state = &input->state_;
  2108. uint32_t tag = 0;
  2109. NSUInteger startingIndex = 0;
  2110. NSArray *fields = descriptor->fields_;
  2111. NSUInteger numFields = fields.count;
  2112. while (YES) {
  2113. BOOL merged = NO;
  2114. tag = GPBCodedInputStreamReadTag(state);
  2115. if (tag == 0) {
  2116. break; // Reached end.
  2117. }
  2118. for (NSUInteger i = 0; i < numFields; ++i) {
  2119. if (startingIndex >= numFields) startingIndex = 0;
  2120. GPBFieldDescriptor *fieldDescriptor = fields[startingIndex];
  2121. if (GPBFieldTag(fieldDescriptor) == tag) {
  2122. GPBFieldType fieldType = fieldDescriptor.fieldType;
  2123. if (fieldType == GPBFieldTypeSingle) {
  2124. MergeSingleFieldFromCodedInputStream(self, fieldDescriptor, syntax,
  2125. input, extensionRegistry);
  2126. // Well formed protos will only have a single field once, advance
  2127. // the starting index to the next field.
  2128. startingIndex += 1;
  2129. } else if (fieldType == GPBFieldTypeRepeated) {
  2130. if (fieldDescriptor.isPackable) {
  2131. MergeRepeatedPackedFieldFromCodedInputStream(
  2132. self, fieldDescriptor, syntax, input);
  2133. // Well formed protos will only have a repeated field that is
  2134. // packed once, advance the starting index to the next field.
  2135. startingIndex += 1;
  2136. } else {
  2137. MergeRepeatedNotPackedFieldFromCodedInputStream(
  2138. self, fieldDescriptor, syntax, input, extensionRegistry);
  2139. }
  2140. } else { // fieldType == GPBFieldTypeMap
  2141. // GPB*Dictionary or NSDictionary, exact type doesn't matter at this
  2142. // point.
  2143. id map = GetOrCreateMapIvarWithField(self, fieldDescriptor, syntax);
  2144. [input readMapEntry:map
  2145. extensionRegistry:extensionRegistry
  2146. field:fieldDescriptor
  2147. parentMessage:self];
  2148. }
  2149. merged = YES;
  2150. break;
  2151. } else {
  2152. startingIndex += 1;
  2153. }
  2154. } // for(i < numFields)
  2155. if (!merged && (tag != 0)) {
  2156. // Primitive, repeated types can be packed on unpacked on the wire, and
  2157. // are parsed either way. The above loop covered tag in the preferred
  2158. // for, so this need to check the alternate form.
  2159. for (NSUInteger i = 0; i < numFields; ++i) {
  2160. if (startingIndex >= numFields) startingIndex = 0;
  2161. GPBFieldDescriptor *fieldDescriptor = fields[startingIndex];
  2162. if ((fieldDescriptor.fieldType == GPBFieldTypeRepeated) &&
  2163. !GPBFieldDataTypeIsObject(fieldDescriptor) &&
  2164. (GPBFieldAlternateTag(fieldDescriptor) == tag)) {
  2165. BOOL alternateIsPacked = !fieldDescriptor.isPackable;
  2166. if (alternateIsPacked) {
  2167. MergeRepeatedPackedFieldFromCodedInputStream(
  2168. self, fieldDescriptor, syntax, input);
  2169. // Well formed protos will only have a repeated field that is
  2170. // packed once, advance the starting index to the next field.
  2171. startingIndex += 1;
  2172. } else {
  2173. MergeRepeatedNotPackedFieldFromCodedInputStream(
  2174. self, fieldDescriptor, syntax, input, extensionRegistry);
  2175. }
  2176. merged = YES;
  2177. break;
  2178. } else {
  2179. startingIndex += 1;
  2180. }
  2181. }
  2182. }
  2183. if (!merged) {
  2184. if (tag == 0) {
  2185. // zero signals EOF / limit reached
  2186. return;
  2187. } else {
  2188. if (![self parseUnknownField:input
  2189. extensionRegistry:extensionRegistry
  2190. tag:tag]) {
  2191. // it's an endgroup tag
  2192. return;
  2193. }
  2194. }
  2195. } // if(!merged)
  2196. } // while(YES)
  2197. }
  2198. #pragma mark - MergeFrom Support
  2199. - (void)mergeFrom:(GPBMessage *)other {
  2200. Class selfClass = [self class];
  2201. Class otherClass = [other class];
  2202. if (!([selfClass isSubclassOfClass:otherClass] ||
  2203. [otherClass isSubclassOfClass:selfClass])) {
  2204. [NSException raise:NSInvalidArgumentException
  2205. format:@"Classes must match %@ != %@", selfClass, otherClass];
  2206. }
  2207. // We assume something will be done and become visible.
  2208. GPBBecomeVisibleToAutocreator(self);
  2209. GPBDescriptor *descriptor = [[self class] descriptor];
  2210. GPBFileSyntax syntax = descriptor.file.syntax;
  2211. for (GPBFieldDescriptor *field in descriptor->fields_) {
  2212. GPBFieldType fieldType = field.fieldType;
  2213. if (fieldType == GPBFieldTypeSingle) {
  2214. int32_t hasIndex = GPBFieldHasIndex(field);
  2215. uint32_t fieldNumber = GPBFieldNumber(field);
  2216. if (!GPBGetHasIvar(other, hasIndex, fieldNumber)) {
  2217. // Other doesn't have the field set, on to the next.
  2218. continue;
  2219. }
  2220. GPBDataType fieldDataType = GPBGetFieldDataType(field);
  2221. switch (fieldDataType) {
  2222. case GPBDataTypeBool:
  2223. GPBSetBoolIvarWithFieldInternal(
  2224. self, field, GPBGetMessageBoolField(other, field), syntax);
  2225. break;
  2226. case GPBDataTypeSFixed32:
  2227. case GPBDataTypeEnum:
  2228. case GPBDataTypeInt32:
  2229. case GPBDataTypeSInt32:
  2230. GPBSetInt32IvarWithFieldInternal(
  2231. self, field, GPBGetMessageInt32Field(other, field), syntax);
  2232. break;
  2233. case GPBDataTypeFixed32:
  2234. case GPBDataTypeUInt32:
  2235. GPBSetUInt32IvarWithFieldInternal(
  2236. self, field, GPBGetMessageUInt32Field(other, field), syntax);
  2237. break;
  2238. case GPBDataTypeSFixed64:
  2239. case GPBDataTypeInt64:
  2240. case GPBDataTypeSInt64:
  2241. GPBSetInt64IvarWithFieldInternal(
  2242. self, field, GPBGetMessageInt64Field(other, field), syntax);
  2243. break;
  2244. case GPBDataTypeFixed64:
  2245. case GPBDataTypeUInt64:
  2246. GPBSetUInt64IvarWithFieldInternal(
  2247. self, field, GPBGetMessageUInt64Field(other, field), syntax);
  2248. break;
  2249. case GPBDataTypeFloat:
  2250. GPBSetFloatIvarWithFieldInternal(
  2251. self, field, GPBGetMessageFloatField(other, field), syntax);
  2252. break;
  2253. case GPBDataTypeDouble:
  2254. GPBSetDoubleIvarWithFieldInternal(
  2255. self, field, GPBGetMessageDoubleField(other, field), syntax);
  2256. break;
  2257. case GPBDataTypeBytes:
  2258. case GPBDataTypeString: {
  2259. id otherVal = GPBGetObjectIvarWithFieldNoAutocreate(other, field);
  2260. GPBSetObjectIvarWithFieldInternal(self, field, otherVal, syntax);
  2261. break;
  2262. }
  2263. case GPBDataTypeMessage:
  2264. case GPBDataTypeGroup: {
  2265. id otherVal = GPBGetObjectIvarWithFieldNoAutocreate(other, field);
  2266. if (GPBGetHasIvar(self, hasIndex, fieldNumber)) {
  2267. GPBMessage *message =
  2268. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  2269. [message mergeFrom:otherVal];
  2270. } else {
  2271. GPBMessage *message = [otherVal copy];
  2272. GPBSetRetainedObjectIvarWithFieldInternal(self, field, message,
  2273. syntax);
  2274. }
  2275. break;
  2276. }
  2277. } // switch()
  2278. } else if (fieldType == GPBFieldTypeRepeated) {
  2279. // In the case of a list, they need to be appended, and there is no
  2280. // _hasIvar to worry about setting.
  2281. id otherArray =
  2282. GPBGetObjectIvarWithFieldNoAutocreate(other, field);
  2283. if (otherArray) {
  2284. GPBDataType fieldDataType = field->description_->dataType;
  2285. if (GPBDataTypeIsObject(fieldDataType)) {
  2286. NSMutableArray *resultArray =
  2287. GetOrCreateArrayIvarWithField(self, field, syntax);
  2288. [resultArray addObjectsFromArray:otherArray];
  2289. } else if (fieldDataType == GPBDataTypeEnum) {
  2290. GPBEnumArray *resultArray =
  2291. GetOrCreateArrayIvarWithField(self, field, syntax);
  2292. [resultArray addRawValuesFromArray:otherArray];
  2293. } else {
  2294. // The array type doesn't matter, that all implment
  2295. // -addValuesFromArray:.
  2296. GPBInt32Array *resultArray =
  2297. GetOrCreateArrayIvarWithField(self, field, syntax);
  2298. [resultArray addValuesFromArray:otherArray];
  2299. }
  2300. }
  2301. } else { // fieldType = GPBFieldTypeMap
  2302. // In the case of a map, they need to be merged, and there is no
  2303. // _hasIvar to worry about setting.
  2304. id otherDict = GPBGetObjectIvarWithFieldNoAutocreate(other, field);
  2305. if (otherDict) {
  2306. GPBDataType keyDataType = field.mapKeyDataType;
  2307. GPBDataType valueDataType = field->description_->dataType;
  2308. if (GPBDataTypeIsObject(keyDataType) &&
  2309. GPBDataTypeIsObject(valueDataType)) {
  2310. NSMutableDictionary *resultDict =
  2311. GetOrCreateMapIvarWithField(self, field, syntax);
  2312. [resultDict addEntriesFromDictionary:otherDict];
  2313. } else if (valueDataType == GPBDataTypeEnum) {
  2314. // The exact type doesn't matter, just need to know it is a
  2315. // GPB*EnumDictionary.
  2316. GPBInt32EnumDictionary *resultDict =
  2317. GetOrCreateMapIvarWithField(self, field, syntax);
  2318. [resultDict addRawEntriesFromDictionary:otherDict];
  2319. } else {
  2320. // The exact type doesn't matter, they all implement
  2321. // -addEntriesFromDictionary:.
  2322. GPBInt32Int32Dictionary *resultDict =
  2323. GetOrCreateMapIvarWithField(self, field, syntax);
  2324. [resultDict addEntriesFromDictionary:otherDict];
  2325. }
  2326. }
  2327. } // if (fieldType)..else if...else
  2328. } // for(fields)
  2329. // Unknown fields.
  2330. if (!unknownFields_) {
  2331. [self setUnknownFields:other.unknownFields];
  2332. } else {
  2333. [unknownFields_ mergeUnknownFields:other.unknownFields];
  2334. }
  2335. // Extensions
  2336. if (other->extensionMap_.count == 0) {
  2337. return;
  2338. }
  2339. if (extensionMap_ == nil) {
  2340. extensionMap_ =
  2341. CloneExtensionMap(other->extensionMap_, NSZoneFromPointer(self));
  2342. } else {
  2343. for (GPBExtensionDescriptor *extension in other->extensionMap_) {
  2344. id otherValue = [other->extensionMap_ objectForKey:extension];
  2345. id value = [extensionMap_ objectForKey:extension];
  2346. BOOL isMessageExtension = GPBExtensionIsMessage(extension);
  2347. if (extension.repeated) {
  2348. NSMutableArray *list = value;
  2349. if (list == nil) {
  2350. list = [[NSMutableArray alloc] init];
  2351. [extensionMap_ setObject:list forKey:extension];
  2352. [list release];
  2353. }
  2354. if (isMessageExtension) {
  2355. for (GPBMessage *otherListValue in otherValue) {
  2356. GPBMessage *copiedValue = [otherListValue copy];
  2357. [list addObject:copiedValue];
  2358. [copiedValue release];
  2359. }
  2360. } else {
  2361. [list addObjectsFromArray:otherValue];
  2362. }
  2363. } else {
  2364. if (isMessageExtension) {
  2365. if (value) {
  2366. [(GPBMessage *)value mergeFrom:(GPBMessage *)otherValue];
  2367. } else {
  2368. GPBMessage *copiedValue = [otherValue copy];
  2369. [extensionMap_ setObject:copiedValue forKey:extension];
  2370. [copiedValue release];
  2371. }
  2372. } else {
  2373. [extensionMap_ setObject:otherValue forKey:extension];
  2374. }
  2375. }
  2376. if (isMessageExtension && !extension.isRepeated) {
  2377. GPBMessage *autocreatedValue =
  2378. [[autocreatedExtensionMap_ objectForKey:extension] retain];
  2379. // Must remove from the map before calling GPBClearMessageAutocreator()
  2380. // so that GPBClearMessageAutocreator() knows its safe to clear.
  2381. [autocreatedExtensionMap_ removeObjectForKey:extension];
  2382. GPBClearMessageAutocreator(autocreatedValue);
  2383. [autocreatedValue release];
  2384. }
  2385. }
  2386. }
  2387. }
  2388. #pragma mark - isEqual: & hash Support
  2389. - (BOOL)isEqual:(id)other {
  2390. if (other == self) {
  2391. return YES;
  2392. }
  2393. if (![other isKindOfClass:[self class]] &&
  2394. ![self isKindOfClass:[other class]]) {
  2395. return NO;
  2396. }
  2397. GPBMessage *otherMsg = other;
  2398. GPBDescriptor *descriptor = [[self class] descriptor];
  2399. uint8_t *selfStorage = (uint8_t *)messageStorage_;
  2400. uint8_t *otherStorage = (uint8_t *)otherMsg->messageStorage_;
  2401. for (GPBFieldDescriptor *field in descriptor->fields_) {
  2402. if (GPBFieldIsMapOrArray(field)) {
  2403. // In the case of a list or map, there is no _hasIvar to worry about.
  2404. // NOTE: These are NSArray/GPB*Array or NSDictionary/GPB*Dictionary, but
  2405. // the type doesn't really matter as the objects all support -count and
  2406. // -isEqual:.
  2407. NSArray *resultMapOrArray =
  2408. GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  2409. NSArray *otherMapOrArray =
  2410. GPBGetObjectIvarWithFieldNoAutocreate(other, field);
  2411. // nil and empty are equal
  2412. if (resultMapOrArray.count != 0 || otherMapOrArray.count != 0) {
  2413. if (![resultMapOrArray isEqual:otherMapOrArray]) {
  2414. return NO;
  2415. }
  2416. }
  2417. } else { // Single field
  2418. int32_t hasIndex = GPBFieldHasIndex(field);
  2419. uint32_t fieldNum = GPBFieldNumber(field);
  2420. BOOL selfHas = GPBGetHasIvar(self, hasIndex, fieldNum);
  2421. BOOL otherHas = GPBGetHasIvar(other, hasIndex, fieldNum);
  2422. if (selfHas != otherHas) {
  2423. return NO; // Differing has values, not equal.
  2424. }
  2425. if (!selfHas) {
  2426. // Same has values, was no, nothing else to check for this field.
  2427. continue;
  2428. }
  2429. // Now compare the values.
  2430. GPBDataType fieldDataType = GPBGetFieldDataType(field);
  2431. size_t fieldOffset = field->description_->offset;
  2432. switch (fieldDataType) {
  2433. case GPBDataTypeBool: {
  2434. // Bools are stored in has_bits to avoid needing explicit space in
  2435. // the storage structure.
  2436. // (the field number passed to the HasIvar helper doesn't really
  2437. // matter since the offset is never negative)
  2438. BOOL selfValue = GPBGetHasIvar(self, (int32_t)(fieldOffset), 0);
  2439. BOOL otherValue = GPBGetHasIvar(other, (int32_t)(fieldOffset), 0);
  2440. if (selfValue != otherValue) {
  2441. return NO;
  2442. }
  2443. break;
  2444. }
  2445. case GPBDataTypeSFixed32:
  2446. case GPBDataTypeInt32:
  2447. case GPBDataTypeSInt32:
  2448. case GPBDataTypeEnum:
  2449. case GPBDataTypeFixed32:
  2450. case GPBDataTypeUInt32:
  2451. case GPBDataTypeFloat: {
  2452. GPBInternalCompileAssert(sizeof(float) == sizeof(uint32_t), float_not_32_bits);
  2453. // These are all 32bit, signed/unsigned doesn't matter for equality.
  2454. uint32_t *selfValPtr = (uint32_t *)&selfStorage[fieldOffset];
  2455. uint32_t *otherValPtr = (uint32_t *)&otherStorage[fieldOffset];
  2456. if (*selfValPtr != *otherValPtr) {
  2457. return NO;
  2458. }
  2459. break;
  2460. }
  2461. case GPBDataTypeSFixed64:
  2462. case GPBDataTypeInt64:
  2463. case GPBDataTypeSInt64:
  2464. case GPBDataTypeFixed64:
  2465. case GPBDataTypeUInt64:
  2466. case GPBDataTypeDouble: {
  2467. GPBInternalCompileAssert(sizeof(double) == sizeof(uint64_t), double_not_64_bits);
  2468. // These are all 64bit, signed/unsigned doesn't matter for equality.
  2469. uint64_t *selfValPtr = (uint64_t *)&selfStorage[fieldOffset];
  2470. uint64_t *otherValPtr = (uint64_t *)&otherStorage[fieldOffset];
  2471. if (*selfValPtr != *otherValPtr) {
  2472. return NO;
  2473. }
  2474. break;
  2475. }
  2476. case GPBDataTypeBytes:
  2477. case GPBDataTypeString:
  2478. case GPBDataTypeMessage:
  2479. case GPBDataTypeGroup: {
  2480. // Type doesn't matter here, they all implement -isEqual:.
  2481. id *selfValPtr = (id *)&selfStorage[fieldOffset];
  2482. id *otherValPtr = (id *)&otherStorage[fieldOffset];
  2483. if (![*selfValPtr isEqual:*otherValPtr]) {
  2484. return NO;
  2485. }
  2486. break;
  2487. }
  2488. } // switch()
  2489. } // if(mapOrArray)...else
  2490. } // for(fields)
  2491. // nil and empty are equal
  2492. if (extensionMap_.count != 0 || otherMsg->extensionMap_.count != 0) {
  2493. if (![extensionMap_ isEqual:otherMsg->extensionMap_]) {
  2494. return NO;
  2495. }
  2496. }
  2497. // nil and empty are equal
  2498. GPBUnknownFieldSet *otherUnknowns = otherMsg->unknownFields_;
  2499. if ([unknownFields_ countOfFields] != 0 ||
  2500. [otherUnknowns countOfFields] != 0) {
  2501. if (![unknownFields_ isEqual:otherUnknowns]) {
  2502. return NO;
  2503. }
  2504. }
  2505. return YES;
  2506. }
  2507. // It is very difficult to implement a generic hash for ProtoBuf messages that
  2508. // will perform well. If you need hashing on your ProtoBufs (eg you are using
  2509. // them as dictionary keys) you will probably want to implement a ProtoBuf
  2510. // message specific hash as a category on your protobuf class. Do not make it a
  2511. // category on GPBMessage as you will conflict with this hash, and will possibly
  2512. // override hash for all generated protobufs. A good implementation of hash will
  2513. // be really fast, so we would recommend only hashing protobufs that have an
  2514. // identifier field of some kind that you can easily hash. If you implement
  2515. // hash, we would strongly recommend overriding isEqual: in your category as
  2516. // well, as the default implementation of isEqual: is extremely slow, and may
  2517. // drastically affect performance in large sets.
  2518. - (NSUInteger)hash {
  2519. GPBDescriptor *descriptor = [[self class] descriptor];
  2520. const NSUInteger prime = 19;
  2521. uint8_t *storage = (uint8_t *)messageStorage_;
  2522. // Start with the descriptor and then mix it with some instance info.
  2523. // Hopefully that will give a spread based on classes and what fields are set.
  2524. NSUInteger result = (NSUInteger)descriptor;
  2525. for (GPBFieldDescriptor *field in descriptor->fields_) {
  2526. if (GPBFieldIsMapOrArray(field)) {
  2527. // Exact type doesn't matter, just check if there are any elements.
  2528. NSArray *mapOrArray = GPBGetObjectIvarWithFieldNoAutocreate(self, field);
  2529. NSUInteger count = mapOrArray.count;
  2530. if (count) {
  2531. // NSArray/NSDictionary use count, use the field number and the count.
  2532. result = prime * result + GPBFieldNumber(field);
  2533. result = prime * result + count;
  2534. }
  2535. } else if (GPBGetHasIvarField(self, field)) {
  2536. // Just using the field number seemed simple/fast, but then a small
  2537. // message class where all the same fields are always set (to different
  2538. // things would end up all with the same hash, so pull in some data).
  2539. GPBDataType fieldDataType = GPBGetFieldDataType(field);
  2540. size_t fieldOffset = field->description_->offset;
  2541. switch (fieldDataType) {
  2542. case GPBDataTypeBool: {
  2543. // Bools are stored in has_bits to avoid needing explicit space in
  2544. // the storage structure.
  2545. // (the field number passed to the HasIvar helper doesn't really
  2546. // matter since the offset is never negative)
  2547. BOOL value = GPBGetHasIvar(self, (int32_t)(fieldOffset), 0);
  2548. result = prime * result + value;
  2549. break;
  2550. }
  2551. case GPBDataTypeSFixed32:
  2552. case GPBDataTypeInt32:
  2553. case GPBDataTypeSInt32:
  2554. case GPBDataTypeEnum:
  2555. case GPBDataTypeFixed32:
  2556. case GPBDataTypeUInt32:
  2557. case GPBDataTypeFloat: {
  2558. GPBInternalCompileAssert(sizeof(float) == sizeof(uint32_t), float_not_32_bits);
  2559. // These are all 32bit, just mix it in.
  2560. uint32_t *valPtr = (uint32_t *)&storage[fieldOffset];
  2561. result = prime * result + *valPtr;
  2562. break;
  2563. }
  2564. case GPBDataTypeSFixed64:
  2565. case GPBDataTypeInt64:
  2566. case GPBDataTypeSInt64:
  2567. case GPBDataTypeFixed64:
  2568. case GPBDataTypeUInt64:
  2569. case GPBDataTypeDouble: {
  2570. GPBInternalCompileAssert(sizeof(double) == sizeof(uint64_t), double_not_64_bits);
  2571. // These are all 64bit, just mix what fits into an NSUInteger in.
  2572. uint64_t *valPtr = (uint64_t *)&storage[fieldOffset];
  2573. result = prime * result + (NSUInteger)(*valPtr);
  2574. break;
  2575. }
  2576. case GPBDataTypeBytes:
  2577. case GPBDataTypeString: {
  2578. // Type doesn't matter here, they both implement -hash:.
  2579. id *valPtr = (id *)&storage[fieldOffset];
  2580. result = prime * result + [*valPtr hash];
  2581. break;
  2582. }
  2583. case GPBDataTypeMessage:
  2584. case GPBDataTypeGroup: {
  2585. GPBMessage **valPtr = (GPBMessage **)&storage[fieldOffset];
  2586. // Could call -hash on the sub message, but that could recurse pretty
  2587. // deep; follow the lead of NSArray/NSDictionary and don't really
  2588. // recurse for hash, instead use the field number and the descriptor
  2589. // of the sub message. Yes, this could suck for a bunch of messages
  2590. // where they all only differ in the sub messages, but if you are
  2591. // using a message with sub messages for something that needs -hash,
  2592. // odds are you are also copying them as keys, and that deep copy
  2593. // will also suck.
  2594. result = prime * result + GPBFieldNumber(field);
  2595. result = prime * result + (NSUInteger)[[*valPtr class] descriptor];
  2596. break;
  2597. }
  2598. } // switch()
  2599. }
  2600. }
  2601. // Unknowns and extensions are not included.
  2602. return result;
  2603. }
  2604. #pragma mark - Description Support
  2605. - (NSString *)description {
  2606. NSString *textFormat = GPBTextFormatForMessage(self, @" ");
  2607. NSString *description = [NSString
  2608. stringWithFormat:@"<%@ %p>: {\n%@}", [self class], self, textFormat];
  2609. return description;
  2610. }
  2611. #if defined(DEBUG) && DEBUG
  2612. // Xcode 5.1 added support for custom quick look info.
  2613. // https://developer.apple.com/library/ios/documentation/IDEs/Conceptual/CustomClassDisplay_in_QuickLook/CH01-quick_look_for_custom_objects/CH01-quick_look_for_custom_objects.html#//apple_ref/doc/uid/TP40014001-CH2-SW1
  2614. - (id)debugQuickLookObject {
  2615. return GPBTextFormatForMessage(self, nil);
  2616. }
  2617. #endif // DEBUG
  2618. #pragma mark - SerializedSize
  2619. - (size_t)serializedSize {
  2620. GPBDescriptor *descriptor = [[self class] descriptor];
  2621. size_t result = 0;
  2622. // Has check is done explicitly, so GPBGetObjectIvarWithFieldNoAutocreate()
  2623. // avoids doing the has check again.
  2624. // Fields.
  2625. for (GPBFieldDescriptor *fieldDescriptor in descriptor->fields_) {
  2626. GPBFieldType fieldType = fieldDescriptor.fieldType;
  2627. GPBDataType fieldDataType = GPBGetFieldDataType(fieldDescriptor);
  2628. // Single Fields
  2629. if (fieldType == GPBFieldTypeSingle) {
  2630. BOOL selfHas = GPBGetHasIvarField(self, fieldDescriptor);
  2631. if (!selfHas) {
  2632. continue; // Nothing to do.
  2633. }
  2634. uint32_t fieldNumber = GPBFieldNumber(fieldDescriptor);
  2635. switch (fieldDataType) {
  2636. #define CASE_SINGLE_POD(NAME, TYPE, FUNC_TYPE) \
  2637. case GPBDataType##NAME: { \
  2638. TYPE fieldVal = GPBGetMessage##FUNC_TYPE##Field(self, fieldDescriptor); \
  2639. result += GPBCompute##NAME##Size(fieldNumber, fieldVal); \
  2640. break; \
  2641. }
  2642. #define CASE_SINGLE_OBJECT(NAME) \
  2643. case GPBDataType##NAME: { \
  2644. id fieldVal = GPBGetObjectIvarWithFieldNoAutocreate(self, fieldDescriptor); \
  2645. result += GPBCompute##NAME##Size(fieldNumber, fieldVal); \
  2646. break; \
  2647. }
  2648. CASE_SINGLE_POD(Bool, BOOL, Bool)
  2649. CASE_SINGLE_POD(Fixed32, uint32_t, UInt32)
  2650. CASE_SINGLE_POD(SFixed32, int32_t, Int32)
  2651. CASE_SINGLE_POD(Float, float, Float)
  2652. CASE_SINGLE_POD(Fixed64, uint64_t, UInt64)
  2653. CASE_SINGLE_POD(SFixed64, int64_t, Int64)
  2654. CASE_SINGLE_POD(Double, double, Double)
  2655. CASE_SINGLE_POD(Int32, int32_t, Int32)
  2656. CASE_SINGLE_POD(Int64, int64_t, Int64)
  2657. CASE_SINGLE_POD(SInt32, int32_t, Int32)
  2658. CASE_SINGLE_POD(SInt64, int64_t, Int64)
  2659. CASE_SINGLE_POD(UInt32, uint32_t, UInt32)
  2660. CASE_SINGLE_POD(UInt64, uint64_t, UInt64)
  2661. CASE_SINGLE_OBJECT(Bytes)
  2662. CASE_SINGLE_OBJECT(String)
  2663. CASE_SINGLE_OBJECT(Message)
  2664. CASE_SINGLE_OBJECT(Group)
  2665. CASE_SINGLE_POD(Enum, int32_t, Int32)
  2666. #undef CASE_SINGLE_POD
  2667. #undef CASE_SINGLE_OBJECT
  2668. }
  2669. // Repeated Fields
  2670. } else if (fieldType == GPBFieldTypeRepeated) {
  2671. id genericArray =
  2672. GPBGetObjectIvarWithFieldNoAutocreate(self, fieldDescriptor);
  2673. NSUInteger count = [genericArray count];
  2674. if (count == 0) {
  2675. continue; // Nothing to add.
  2676. }
  2677. __block size_t dataSize = 0;
  2678. switch (fieldDataType) {
  2679. #define CASE_REPEATED_POD(NAME, TYPE, ARRAY_TYPE) \
  2680. CASE_REPEATED_POD_EXTRA(NAME, TYPE, ARRAY_TYPE, )
  2681. #define CASE_REPEATED_POD_EXTRA(NAME, TYPE, ARRAY_TYPE, ARRAY_ACCESSOR_NAME) \
  2682. case GPBDataType##NAME: { \
  2683. GPB##ARRAY_TYPE##Array *array = genericArray; \
  2684. [array enumerate##ARRAY_ACCESSOR_NAME##ValuesWithBlock:^(TYPE value, NSUInteger idx, BOOL *stop) { \
  2685. _Pragma("unused(idx, stop)"); \
  2686. dataSize += GPBCompute##NAME##SizeNoTag(value); \
  2687. }]; \
  2688. break; \
  2689. }
  2690. #define CASE_REPEATED_OBJECT(NAME) \
  2691. case GPBDataType##NAME: { \
  2692. for (id value in genericArray) { \
  2693. dataSize += GPBCompute##NAME##SizeNoTag(value); \
  2694. } \
  2695. break; \
  2696. }
  2697. CASE_REPEATED_POD(Bool, BOOL, Bool)
  2698. CASE_REPEATED_POD(Fixed32, uint32_t, UInt32)
  2699. CASE_REPEATED_POD(SFixed32, int32_t, Int32)
  2700. CASE_REPEATED_POD(Float, float, Float)
  2701. CASE_REPEATED_POD(Fixed64, uint64_t, UInt64)
  2702. CASE_REPEATED_POD(SFixed64, int64_t, Int64)
  2703. CASE_REPEATED_POD(Double, double, Double)
  2704. CASE_REPEATED_POD(Int32, int32_t, Int32)
  2705. CASE_REPEATED_POD(Int64, int64_t, Int64)
  2706. CASE_REPEATED_POD(SInt32, int32_t, Int32)
  2707. CASE_REPEATED_POD(SInt64, int64_t, Int64)
  2708. CASE_REPEATED_POD(UInt32, uint32_t, UInt32)
  2709. CASE_REPEATED_POD(UInt64, uint64_t, UInt64)
  2710. CASE_REPEATED_OBJECT(Bytes)
  2711. CASE_REPEATED_OBJECT(String)
  2712. CASE_REPEATED_OBJECT(Message)
  2713. CASE_REPEATED_OBJECT(Group)
  2714. CASE_REPEATED_POD_EXTRA(Enum, int32_t, Enum, Raw)
  2715. #undef CASE_REPEATED_POD
  2716. #undef CASE_REPEATED_POD_EXTRA
  2717. #undef CASE_REPEATED_OBJECT
  2718. } // switch
  2719. result += dataSize;
  2720. size_t tagSize = GPBComputeTagSize(GPBFieldNumber(fieldDescriptor));
  2721. if (fieldDataType == GPBDataTypeGroup) {
  2722. // Groups have both a start and an end tag.
  2723. tagSize *= 2;
  2724. }
  2725. if (fieldDescriptor.isPackable) {
  2726. result += tagSize;
  2727. result += GPBComputeSizeTSizeAsInt32NoTag(dataSize);
  2728. } else {
  2729. result += count * tagSize;
  2730. }
  2731. // Map<> Fields
  2732. } else { // fieldType == GPBFieldTypeMap
  2733. if (GPBDataTypeIsObject(fieldDataType) &&
  2734. (fieldDescriptor.mapKeyDataType == GPBDataTypeString)) {
  2735. // If key type was string, then the map is an NSDictionary.
  2736. NSDictionary *map =
  2737. GPBGetObjectIvarWithFieldNoAutocreate(self, fieldDescriptor);
  2738. if (map) {
  2739. result += GPBDictionaryComputeSizeInternalHelper(map, fieldDescriptor);
  2740. }
  2741. } else {
  2742. // Type will be GPB*GroupDictionary, exact type doesn't matter.
  2743. GPBInt32Int32Dictionary *map =
  2744. GPBGetObjectIvarWithFieldNoAutocreate(self, fieldDescriptor);
  2745. result += [map computeSerializedSizeAsField:fieldDescriptor];
  2746. }
  2747. }
  2748. } // for(fields)
  2749. // Add any unknown fields.
  2750. if (descriptor.wireFormat) {
  2751. result += [unknownFields_ serializedSizeAsMessageSet];
  2752. } else {
  2753. result += [unknownFields_ serializedSize];
  2754. }
  2755. // Add any extensions.
  2756. for (GPBExtensionDescriptor *extension in extensionMap_) {
  2757. id value = [extensionMap_ objectForKey:extension];
  2758. result += GPBComputeExtensionSerializedSizeIncludingTag(extension, value);
  2759. }
  2760. return result;
  2761. }
  2762. #pragma mark - Resolve Methods Support
  2763. typedef struct ResolveIvarAccessorMethodResult {
  2764. IMP impToAdd;
  2765. SEL encodingSelector;
  2766. } ResolveIvarAccessorMethodResult;
  2767. static void ResolveIvarGet(GPBFieldDescriptor *field,
  2768. ResolveIvarAccessorMethodResult *result) {
  2769. GPBDataType fieldDataType = GPBGetFieldDataType(field);
  2770. switch (fieldDataType) {
  2771. #define CASE_GET(NAME, TYPE, TRUE_NAME) \
  2772. case GPBDataType##NAME: { \
  2773. result->impToAdd = imp_implementationWithBlock(^(id obj) { \
  2774. return GPBGetMessage##TRUE_NAME##Field(obj, field); \
  2775. }); \
  2776. result->encodingSelector = @selector(get##NAME); \
  2777. break; \
  2778. }
  2779. #define CASE_GET_OBJECT(NAME, TYPE, TRUE_NAME) \
  2780. case GPBDataType##NAME: { \
  2781. result->impToAdd = imp_implementationWithBlock(^(id obj) { \
  2782. return GPBGetObjectIvarWithField(obj, field); \
  2783. }); \
  2784. result->encodingSelector = @selector(get##NAME); \
  2785. break; \
  2786. }
  2787. CASE_GET(Bool, BOOL, Bool)
  2788. CASE_GET(Fixed32, uint32_t, UInt32)
  2789. CASE_GET(SFixed32, int32_t, Int32)
  2790. CASE_GET(Float, float, Float)
  2791. CASE_GET(Fixed64, uint64_t, UInt64)
  2792. CASE_GET(SFixed64, int64_t, Int64)
  2793. CASE_GET(Double, double, Double)
  2794. CASE_GET(Int32, int32_t, Int32)
  2795. CASE_GET(Int64, int64_t, Int64)
  2796. CASE_GET(SInt32, int32_t, Int32)
  2797. CASE_GET(SInt64, int64_t, Int64)
  2798. CASE_GET(UInt32, uint32_t, UInt32)
  2799. CASE_GET(UInt64, uint64_t, UInt64)
  2800. CASE_GET_OBJECT(Bytes, id, Object)
  2801. CASE_GET_OBJECT(String, id, Object)
  2802. CASE_GET_OBJECT(Message, id, Object)
  2803. CASE_GET_OBJECT(Group, id, Object)
  2804. CASE_GET(Enum, int32_t, Enum)
  2805. #undef CASE_GET
  2806. }
  2807. }
  2808. static void ResolveIvarSet(GPBFieldDescriptor *field,
  2809. GPBFileSyntax syntax,
  2810. ResolveIvarAccessorMethodResult *result) {
  2811. GPBDataType fieldDataType = GPBGetFieldDataType(field);
  2812. switch (fieldDataType) {
  2813. #define CASE_SET(NAME, TYPE, TRUE_NAME) \
  2814. case GPBDataType##NAME: { \
  2815. result->impToAdd = imp_implementationWithBlock(^(id obj, TYPE value) { \
  2816. return GPBSet##TRUE_NAME##IvarWithFieldInternal(obj, field, value, syntax); \
  2817. }); \
  2818. result->encodingSelector = @selector(set##NAME:); \
  2819. break; \
  2820. }
  2821. CASE_SET(Bool, BOOL, Bool)
  2822. CASE_SET(Fixed32, uint32_t, UInt32)
  2823. CASE_SET(SFixed32, int32_t, Int32)
  2824. CASE_SET(Float, float, Float)
  2825. CASE_SET(Fixed64, uint64_t, UInt64)
  2826. CASE_SET(SFixed64, int64_t, Int64)
  2827. CASE_SET(Double, double, Double)
  2828. CASE_SET(Int32, int32_t, Int32)
  2829. CASE_SET(Int64, int64_t, Int64)
  2830. CASE_SET(SInt32, int32_t, Int32)
  2831. CASE_SET(SInt64, int64_t, Int64)
  2832. CASE_SET(UInt32, uint32_t, UInt32)
  2833. CASE_SET(UInt64, uint64_t, UInt64)
  2834. CASE_SET(Bytes, id, Object)
  2835. CASE_SET(String, id, Object)
  2836. CASE_SET(Message, id, Object)
  2837. CASE_SET(Group, id, Object)
  2838. CASE_SET(Enum, int32_t, Enum)
  2839. #undef CASE_SET
  2840. }
  2841. }
  2842. + (BOOL)resolveInstanceMethod:(SEL)sel {
  2843. const GPBDescriptor *descriptor = [self descriptor];
  2844. if (!descriptor) {
  2845. return [super resolveInstanceMethod:sel];
  2846. }
  2847. // NOTE: hasOrCountSel_/setHasSel_ will be NULL if the field for the given
  2848. // message should not have has support (done in GPBDescriptor.m), so there is
  2849. // no need for checks here to see if has*/setHas* are allowed.
  2850. ResolveIvarAccessorMethodResult result = {NULL, NULL};
  2851. for (GPBFieldDescriptor *field in descriptor->fields_) {
  2852. BOOL isMapOrArray = GPBFieldIsMapOrArray(field);
  2853. if (!isMapOrArray) {
  2854. // Single fields.
  2855. if (sel == field->getSel_) {
  2856. ResolveIvarGet(field, &result);
  2857. break;
  2858. } else if (sel == field->setSel_) {
  2859. ResolveIvarSet(field, descriptor.file.syntax, &result);
  2860. break;
  2861. } else if (sel == field->hasOrCountSel_) {
  2862. int32_t index = GPBFieldHasIndex(field);
  2863. uint32_t fieldNum = GPBFieldNumber(field);
  2864. result.impToAdd = imp_implementationWithBlock(^(id obj) {
  2865. return GPBGetHasIvar(obj, index, fieldNum);
  2866. });
  2867. result.encodingSelector = @selector(getBool);
  2868. break;
  2869. } else if (sel == field->setHasSel_) {
  2870. result.impToAdd = imp_implementationWithBlock(^(id obj, BOOL value) {
  2871. if (value) {
  2872. [NSException raise:NSInvalidArgumentException
  2873. format:@"%@: %@ can only be set to NO (to clear field).",
  2874. [obj class],
  2875. NSStringFromSelector(field->setHasSel_)];
  2876. }
  2877. GPBClearMessageField(obj, field);
  2878. });
  2879. result.encodingSelector = @selector(setBool:);
  2880. break;
  2881. } else {
  2882. GPBOneofDescriptor *oneof = field->containingOneof_;
  2883. if (oneof && (sel == oneof->caseSel_)) {
  2884. int32_t index = GPBFieldHasIndex(field);
  2885. result.impToAdd = imp_implementationWithBlock(^(id obj) {
  2886. return GPBGetHasOneof(obj, index);
  2887. });
  2888. result.encodingSelector = @selector(getEnum);
  2889. break;
  2890. }
  2891. }
  2892. } else {
  2893. // map<>/repeated fields.
  2894. if (sel == field->getSel_) {
  2895. if (field.fieldType == GPBFieldTypeRepeated) {
  2896. result.impToAdd = imp_implementationWithBlock(^(id obj) {
  2897. return GetArrayIvarWithField(obj, field);
  2898. });
  2899. } else {
  2900. result.impToAdd = imp_implementationWithBlock(^(id obj) {
  2901. return GetMapIvarWithField(obj, field);
  2902. });
  2903. }
  2904. result.encodingSelector = @selector(getArray);
  2905. break;
  2906. } else if (sel == field->setSel_) {
  2907. // Local for syntax so the block can directly capture it and not the
  2908. // full lookup.
  2909. const GPBFileSyntax syntax = descriptor.file.syntax;
  2910. result.impToAdd = imp_implementationWithBlock(^(id obj, id value) {
  2911. return GPBSetObjectIvarWithFieldInternal(obj, field, value, syntax);
  2912. });
  2913. result.encodingSelector = @selector(setArray:);
  2914. break;
  2915. } else if (sel == field->hasOrCountSel_) {
  2916. result.impToAdd = imp_implementationWithBlock(^(id obj) {
  2917. // Type doesn't matter, all *Array and *Dictionary types support
  2918. // -count.
  2919. NSArray *arrayOrMap =
  2920. GPBGetObjectIvarWithFieldNoAutocreate(obj, field);
  2921. return [arrayOrMap count];
  2922. });
  2923. result.encodingSelector = @selector(getArrayCount);
  2924. break;
  2925. }
  2926. }
  2927. }
  2928. if (result.impToAdd) {
  2929. const char *encoding =
  2930. GPBMessageEncodingForSelector(result.encodingSelector, YES);
  2931. Class msgClass = descriptor.messageClass;
  2932. BOOL methodAdded = class_addMethod(msgClass, sel, result.impToAdd, encoding);
  2933. // class_addMethod() is documented as also failing if the method was already
  2934. // added; so we check if the method is already there and return success so
  2935. // the method dispatch will still happen. Why would it already be added?
  2936. // Two threads could cause the same method to be bound at the same time,
  2937. // but only one will actually bind it; the other still needs to return true
  2938. // so things will dispatch.
  2939. if (!methodAdded) {
  2940. methodAdded = GPBClassHasSel(msgClass, sel);
  2941. }
  2942. return methodAdded;
  2943. }
  2944. return [super resolveInstanceMethod:sel];
  2945. }
  2946. + (BOOL)resolveClassMethod:(SEL)sel {
  2947. // Extensions scoped to a Message and looked up via class methods.
  2948. if (GPBResolveExtensionClassMethod([self descriptor].messageClass, sel)) {
  2949. return YES;
  2950. }
  2951. return [super resolveClassMethod:sel];
  2952. }
  2953. #pragma mark - NSCoding Support
  2954. + (BOOL)supportsSecureCoding {
  2955. return YES;
  2956. }
  2957. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  2958. self = [self init];
  2959. if (self) {
  2960. NSData *data =
  2961. [aDecoder decodeObjectOfClass:[NSData class] forKey:kGPBDataCoderKey];
  2962. if (data.length) {
  2963. [self mergeFromData:data extensionRegistry:nil];
  2964. }
  2965. }
  2966. return self;
  2967. }
  2968. - (void)encodeWithCoder:(NSCoder *)aCoder {
  2969. NSData *data = [self data];
  2970. if (data.length) {
  2971. [aCoder encodeObject:data forKey:kGPBDataCoderKey];
  2972. }
  2973. }
  2974. #pragma mark - KVC Support
  2975. + (BOOL)accessInstanceVariablesDirectly {
  2976. // Make sure KVC doesn't use instance variables.
  2977. return NO;
  2978. }
  2979. @end
  2980. #pragma mark - Messages from GPBUtilities.h but defined here for access to helpers.
  2981. // Only exists for public api, no core code should use this.
  2982. id GPBGetMessageRepeatedField(GPBMessage *self, GPBFieldDescriptor *field) {
  2983. #if defined(DEBUG) && DEBUG
  2984. if (field.fieldType != GPBFieldTypeRepeated) {
  2985. [NSException raise:NSInvalidArgumentException
  2986. format:@"%@.%@ is not a repeated field.",
  2987. [self class], field.name];
  2988. }
  2989. #endif
  2990. GPBDescriptor *descriptor = [[self class] descriptor];
  2991. GPBFileSyntax syntax = descriptor.file.syntax;
  2992. return GetOrCreateArrayIvarWithField(self, field, syntax);
  2993. }
  2994. // Only exists for public api, no core code should use this.
  2995. id GPBGetMessageMapField(GPBMessage *self, GPBFieldDescriptor *field) {
  2996. #if defined(DEBUG) && DEBUG
  2997. if (field.fieldType != GPBFieldTypeMap) {
  2998. [NSException raise:NSInvalidArgumentException
  2999. format:@"%@.%@ is not a map<> field.",
  3000. [self class], field.name];
  3001. }
  3002. #endif
  3003. GPBDescriptor *descriptor = [[self class] descriptor];
  3004. GPBFileSyntax syntax = descriptor.file.syntax;
  3005. return GetOrCreateMapIvarWithField(self, field, syntax);
  3006. }
  3007. #pragma clang diagnostic pop