GPBMessage.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. //
  4. // Use of this source code is governed by a BSD-style
  5. // license that can be found in the LICENSE file or at
  6. // https://developers.google.com/open-source/licenses/bsd
  7. #import <Foundation/Foundation.h>
  8. #import "GPBBootstrap.h"
  9. #import "GPBExtensionRegistry.h"
  10. @class GPBDescriptor;
  11. @class GPBCodedInputStream;
  12. @class GPBCodedOutputStream;
  13. @class GPBExtensionDescriptor;
  14. @class GPBFieldDescriptor;
  15. @class GPBUnknownFieldSet;
  16. NS_ASSUME_NONNULL_BEGIN
  17. CF_EXTERN_C_BEGIN
  18. /** NSError domain used for errors. */
  19. extern NSString *const GPBMessageErrorDomain;
  20. /** Error codes for NSErrors originated in GPBMessage. */
  21. typedef NS_ENUM(NSInteger, GPBMessageErrorCode) {
  22. /** Uncategorized error. */
  23. GPBMessageErrorCodeOther = -100,
  24. /** Message couldn't be serialized because it is missing required fields. */
  25. GPBMessageErrorCodeMissingRequiredField = -101,
  26. };
  27. /**
  28. * Key under which the GPBMessage error's reason is stored inside the userInfo
  29. * dictionary.
  30. **/
  31. extern NSString *const GPBErrorReasonKey;
  32. /**
  33. * An exception name raised during serialization when the message would be
  34. * larger than the 2GB limit.
  35. **/
  36. extern NSString *const GPBMessageExceptionMessageTooLarge;
  37. CF_EXTERN_C_END
  38. /**
  39. * Base class that each generated message subclasses from.
  40. *
  41. * @note @c NSCopying support is a "deep copy", in that all sub objects are
  42. * copied. Just like you wouldn't want a UIView/NSView trying to
  43. * exist in two places, you don't want a sub message to be a property
  44. * property of two other messages.
  45. *
  46. * @note While the class support NSSecureCoding, if the message has any
  47. * extensions, they will end up reloaded in @c unknownFields as there is
  48. * no way for the @c NSCoding plumbing to pass through a
  49. * @c GPBExtensionRegistry. To support extensions, instead of passing the
  50. * calls off to the Message, simple store the result of @c data, and then
  51. * when loading, fetch the data and use
  52. * @c +parseFromData:extensionRegistry:error: to provide an extension
  53. * registry.
  54. **/
  55. @interface GPBMessage : NSObject <NSSecureCoding, NSCopying>
  56. // If you add an instance method/property to this class that may conflict with
  57. // fields declared in protos, you need to update objective_helpers.cc. The main
  58. // cases are methods that take no arguments, or setFoo:/hasFoo: type methods.
  59. /**
  60. * The set of unknown fields for this message.
  61. **/
  62. @property(nonatomic, copy, nullable) GPBUnknownFieldSet *unknownFields;
  63. /**
  64. * Whether the message, along with all submessages, have the required fields
  65. * set.
  66. **/
  67. @property(nonatomic, readonly, getter=isInitialized) BOOL initialized;
  68. /**
  69. * @return An autoreleased message with the default values set.
  70. **/
  71. + (instancetype)message;
  72. /**
  73. * Creates a new instance by parsing the provided data. This method should be
  74. * sent to the generated message class that the data should be interpreted as.
  75. * If there is an error the method returns nil and the error is returned in
  76. * errorPtr (when provided).
  77. *
  78. * @note In DEBUG builds, the parsed message is checked to be sure all required
  79. * fields were provided, and the parse will fail if some are missing.
  80. *
  81. * @note The errors returned are likely coming from the domain and codes listed
  82. * at the top of this file and GPBCodedInputStream.h.
  83. *
  84. * @param data The data to parse.
  85. * @param errorPtr An optional error pointer to fill in with a failure reason if
  86. * the data can not be parsed.
  87. *
  88. * @return A new instance of the generated class.
  89. **/
  90. + (nullable instancetype)parseFromData:(NSData *)data error:(NSError **)errorPtr;
  91. /**
  92. * Creates a new instance by parsing the data. This method should be sent to
  93. * the generated message class that the data should be interpreted as. If
  94. * there is an error the method returns nil and the error is returned in
  95. * errorPtr (when provided).
  96. *
  97. * @note In DEBUG builds, the parsed message is checked to be sure all required
  98. * fields were provided, and the parse will fail if some are missing.
  99. *
  100. * @note The errors returned are likely coming from the domain and codes listed
  101. * at the top of this file and GPBCodedInputStream.h.
  102. *
  103. * @param data The data to parse.
  104. * @param extensionRegistry The extension registry to use to look up extensions.
  105. * @param errorPtr An optional error pointer to fill in with a failure
  106. * reason if the data can not be parsed.
  107. *
  108. * @return A new instance of the generated class.
  109. **/
  110. + (nullable instancetype)parseFromData:(NSData *)data
  111. extensionRegistry:(nullable id<GPBExtensionRegistry>)extensionRegistry
  112. error:(NSError **)errorPtr;
  113. /**
  114. * Creates a new instance by parsing the data from the given input stream. This
  115. * method should be sent to the generated message class that the data should
  116. * be interpreted as. If there is an error the method returns nil and the error
  117. * is returned in errorPtr (when provided).
  118. *
  119. * @note In DEBUG builds, the parsed message is checked to be sure all required
  120. * fields were provided, and the parse will fail if some are missing.
  121. *
  122. * @note The errors returned are likely coming from the domain and codes listed
  123. * at the top of this file and GPBCodedInputStream.h.
  124. *
  125. * @param input The stream to read data from.
  126. * @param extensionRegistry The extension registry to use to look up extensions.
  127. * @param errorPtr An optional error pointer to fill in with a failure
  128. * reason if the data can not be parsed.
  129. *
  130. * @return A new instance of the generated class.
  131. **/
  132. + (nullable instancetype)parseFromCodedInputStream:(GPBCodedInputStream *)input
  133. extensionRegistry:
  134. (nullable id<GPBExtensionRegistry>)extensionRegistry
  135. error:(NSError **)errorPtr;
  136. /**
  137. * Creates a new instance by parsing the data from the given input stream. This
  138. * method should be sent to the generated message class that the data should
  139. * be interpreted as. If there is an error the method returns nil and the error
  140. * is returned in errorPtr (when provided).
  141. *
  142. * @note Unlike the parseFrom... methods, this never checks to see if all of
  143. * the required fields are set. So this method can be used to reload
  144. * messages that may not be complete.
  145. *
  146. * @note The errors returned are likely coming from the domain and codes listed
  147. * at the top of this file and GPBCodedInputStream.h.
  148. *
  149. * @param input The stream to read data from.
  150. * @param extensionRegistry The extension registry to use to look up extensions.
  151. * @param errorPtr An optional error pointer to fill in with a failure
  152. * reason if the data can not be parsed.
  153. *
  154. * @return A new instance of the generated class.
  155. **/
  156. + (nullable instancetype)parseDelimitedFromCodedInputStream:(GPBCodedInputStream *)input
  157. extensionRegistry:
  158. (nullable id<GPBExtensionRegistry>)extensionRegistry
  159. error:(NSError **)errorPtr;
  160. /**
  161. * Initializes an instance by parsing the data. This method should be sent to
  162. * the generated message class that the data should be interpreted as. If
  163. * there is an error the method returns nil and the error is returned in
  164. * errorPtr (when provided).
  165. *
  166. * @note In DEBUG builds, the parsed message is checked to be sure all required
  167. * fields were provided, and the parse will fail if some are missing.
  168. *
  169. * @note The errors returned are likely coming from the domain and codes listed
  170. * at the top of this file and GPBCodedInputStream.h.
  171. *
  172. * @param data The data to parse.
  173. * @param errorPtr An optional error pointer to fill in with a failure reason if
  174. * the data can not be parsed.
  175. *
  176. * @return An initialized instance of the generated class.
  177. **/
  178. - (nullable instancetype)initWithData:(NSData *)data error:(NSError **)errorPtr;
  179. /**
  180. * Initializes an instance by parsing the data. This method should be sent to
  181. * the generated message class that the data should be interpreted as. If
  182. * there is an error the method returns nil and the error is returned in
  183. * errorPtr (when provided).
  184. *
  185. * @note In DEBUG builds, the parsed message is checked to be sure all required
  186. * fields were provided, and the parse will fail if some are missing.
  187. *
  188. * @note The errors returned are likely coming from the domain and codes listed
  189. * at the top of this file and GPBCodedInputStream.h.
  190. *
  191. * @param data The data to parse.
  192. * @param extensionRegistry The extension registry to use to look up extensions.
  193. * @param errorPtr An optional error pointer to fill in with a failure
  194. * reason if the data can not be parsed.
  195. *
  196. * @return An initialized instance of the generated class.
  197. **/
  198. - (nullable instancetype)initWithData:(NSData *)data
  199. extensionRegistry:(nullable id<GPBExtensionRegistry>)extensionRegistry
  200. error:(NSError **)errorPtr;
  201. /**
  202. * Initializes an instance by parsing the data from the given input stream. This
  203. * method should be sent to the generated message class that the data should
  204. * be interpreted as. If there is an error the method returns nil and the error
  205. * is returned in errorPtr (when provided).
  206. *
  207. * @note Unlike the parseFrom... methods, this never checks to see if all of
  208. * the required fields are set. So this method can be used to reload
  209. * messages that may not be complete.
  210. *
  211. * @note The errors returned are likely coming from the domain and codes listed
  212. * at the top of this file and GPBCodedInputStream.h.
  213. *
  214. * @param input The stream to read data from.
  215. * @param extensionRegistry The extension registry to use to look up extensions.
  216. * @param errorPtr An optional error pointer to fill in with a failure
  217. * reason if the data can not be parsed.
  218. *
  219. * @return An initialized instance of the generated class.
  220. **/
  221. - (nullable instancetype)initWithCodedInputStream:(GPBCodedInputStream *)input
  222. extensionRegistry:
  223. (nullable id<GPBExtensionRegistry>)extensionRegistry
  224. error:(NSError **)errorPtr;
  225. /**
  226. * Parses the given data as this message's class, and merges those values into
  227. * this message.
  228. *
  229. * @param data The binary representation of the message to merge.
  230. * @param extensionRegistry The extension registry to use to look up extensions.
  231. *
  232. * @exception GPBCodedInputStreamException Exception thrown when parsing was
  233. * unsuccessful.
  234. **/
  235. - (void)mergeFromData:(NSData *)data
  236. extensionRegistry:(nullable id<GPBExtensionRegistry>)extensionRegistry
  237. __attribute__((deprecated(
  238. "Use -mergeFromData:extensionRegistry:error: instead, especaily if calling from Swift.")));
  239. /**
  240. * Parses the given data as this message's class, and merges those values into
  241. * this message.
  242. *
  243. * @param data The binary representation of the message to merge.
  244. * @param extensionRegistry The extension registry to use to look up extensions.
  245. * @param errorPtr An optional error pointer to fill in with a failure
  246. * reason if the data can not be parsed. Will only be
  247. * filled in if the data failed to be parsed.
  248. *
  249. * @return Boolean indicating success. errorPtr will only be fill in on failure.
  250. **/
  251. - (BOOL)mergeFromData:(NSData *)data
  252. extensionRegistry:(nullable id<GPBExtensionRegistry>)extensionRegistry
  253. error:(NSError **)errorPtr;
  254. /**
  255. * Merges the fields from another message (of the same type) into this
  256. * message.
  257. *
  258. * @param other Message to merge into this message.
  259. **/
  260. - (void)mergeFrom:(GPBMessage *)other;
  261. /**
  262. * Writes out the message to the given coded output stream.
  263. *
  264. * @param output The coded output stream into which to write the message.
  265. *
  266. * @note This can raise the GPBCodedOutputStreamException_* exceptions.
  267. *
  268. * @note The most common cause of this failing is from one thread calling this
  269. * while another thread has a reference to this message or a message used
  270. * within a field and that other thread mutating the message while this
  271. * serialization is taking place.
  272. **/
  273. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)output;
  274. /**
  275. * Writes out the message to the given output stream.
  276. *
  277. * @param output The output stream into which to write the message.
  278. *
  279. * @note This can raise the GPBCodedOutputStreamException_* exceptions.
  280. *
  281. * @note The most common cause of this failing is from one thread calling this
  282. * while another thread has a reference to this message or a message used
  283. * within a field and that other thread mutating the message while this
  284. * serialization is taking place.
  285. **/
  286. - (void)writeToOutputStream:(NSOutputStream *)output;
  287. /**
  288. * Writes out a varint for the message size followed by the message to
  289. * the given output stream.
  290. *
  291. * @param output The coded output stream into which to write the message.
  292. *
  293. * @note This can raise the GPBCodedOutputStreamException_* exceptions.
  294. *
  295. * @note The most common cause of this failing is from one thread calling this
  296. * while another thread has a reference to this message or a message used
  297. * within a field and that other thread mutating the message while this
  298. * serialization is taking place.
  299. **/
  300. - (void)writeDelimitedToCodedOutputStream:(GPBCodedOutputStream *)output;
  301. /**
  302. * Writes out a varint for the message size followed by the message to
  303. * the given output stream.
  304. *
  305. * @param output The output stream into which to write the message.
  306. *
  307. * @note This can raise the GPBCodedOutputStreamException_* exceptions.
  308. *
  309. * @note The most common cause of this failing is from one thread calling this
  310. * while another thread has a reference to this message or a message used
  311. * within a field and that other thread mutating the message while this
  312. * serialization is taking place.
  313. **/
  314. - (void)writeDelimitedToOutputStream:(NSOutputStream *)output;
  315. /**
  316. * Serializes the message to an NSData.
  317. *
  318. * If there is an error while generating the data, nil is returned.
  319. *
  320. * @note This value is not cached, so if you are using it repeatedly, cache
  321. * it yourself.
  322. *
  323. * @note In DEBUG ONLY, the message is also checked for all required field,
  324. * if one is missing, nil will be returned.
  325. *
  326. * @note The most common cause of this failing is from one thread calling this
  327. * while another thread has a reference to this message or a message used
  328. * within a field and that other thread mutating the message while this
  329. * serialization is taking place.
  330. *
  331. * @return The binary representation of the message.
  332. **/
  333. - (nullable NSData *)data;
  334. /**
  335. * Serializes a varint with the message size followed by the message data,
  336. * returning that as an NSData.
  337. *
  338. * @note This value is not cached, so if you are using it repeatedly, it is
  339. * recommended to keep a local copy.
  340. *
  341. * @note The most common cause of this failing is from one thread calling this
  342. * while another thread has a reference to this message or a message used
  343. * within a field and that other thread mutating the message while this
  344. * serialization is taking place.
  345. *
  346. * @return The binary representation of the size along with the message.
  347. **/
  348. - (NSData *)delimitedData;
  349. /**
  350. * Calculates the size of the object if it were serialized.
  351. *
  352. * This is not a cached value. If you are following a pattern like this:
  353. *
  354. * ```
  355. * size_t size = [aMsg serializedSize];
  356. * NSMutableData *foo = [NSMutableData dataWithCapacity:size + sizeof(size)];
  357. * [foo writeSize:size];
  358. * [foo appendData:[aMsg data]];
  359. * ```
  360. *
  361. * you would be better doing:
  362. *
  363. * ```
  364. * NSData *data = [aMsg data];
  365. * NSUInteger size = [aMsg length];
  366. * NSMutableData *foo = [NSMutableData dataWithCapacity:size + sizeof(size)];
  367. * [foo writeSize:size];
  368. * [foo appendData:data];
  369. * ```
  370. *
  371. * @return The size of the message in it's binary representation.
  372. **/
  373. - (size_t)serializedSize;
  374. /**
  375. * @return The descriptor for the message class.
  376. **/
  377. + (GPBDescriptor *)descriptor;
  378. /**
  379. * Return the descriptor for the message.
  380. **/
  381. - (GPBDescriptor *)descriptor;
  382. /**
  383. * @return An array with the extension descriptors that are currently set on the
  384. * message.
  385. **/
  386. - (NSArray *)extensionsCurrentlySet;
  387. /**
  388. * Checks whether there is an extension set on the message which matches the
  389. * given extension descriptor.
  390. *
  391. * @param extension Extension descriptor to check if it's set on the message.
  392. *
  393. * @return Whether the extension is currently set on the message.
  394. **/
  395. - (BOOL)hasExtension:(GPBExtensionDescriptor *)extension;
  396. /*
  397. * Fetches the given extension's value for this message.
  398. *
  399. * Extensions use boxed values (NSNumbers) for PODs and NSMutableArrays for
  400. * repeated fields. If the extension is a Message one will be auto created for
  401. * you and returned similar to fields.
  402. *
  403. * NOTE: For Enum extensions, if the enum was _closed_, then unknown values
  404. * were parsed into the message's unknown fields instead of ending up in the
  405. * extensions, just like what happens with singular/repeated fields. For open
  406. * enums, the _raw_ value will be in the NSNumber, meaning if one does a
  407. * `switch` on the values, a `default` case should also be included.
  408. *
  409. * @param extension The extension descriptor of the extension to fetch.
  410. *
  411. * @return The extension matching the given descriptor, or nil if none found.
  412. **/
  413. - (nullable id)getExtension:(GPBExtensionDescriptor *)extension;
  414. /**
  415. * Sets the given extension's value for this message. This only applies for
  416. * single field extensions (i.e. - not repeated fields).
  417. *
  418. * Extensions use boxed values (NSNumbers).
  419. *
  420. * @param extension The extension descriptor under which to set the value.
  421. * @param value The value to be set as the extension.
  422. **/
  423. - (void)setExtension:(GPBExtensionDescriptor *)extension value:(nullable id)value;
  424. /**
  425. * Adds the given value to the extension for this message. This only applies
  426. * to repeated field extensions. If the field is a repeated POD type, the value
  427. * should be an NSNumber.
  428. *
  429. * @param extension The extension descriptor under which to add the value.
  430. * @param value The value to be added to the repeated extension.
  431. **/
  432. - (void)addExtension:(GPBExtensionDescriptor *)extension value:(id)value;
  433. /**
  434. * Replaces the value at the given index with the given value for the extension
  435. * on this message. This only applies to repeated field extensions. If the field
  436. * is a repeated POD type, the value is should be an NSNumber.
  437. *
  438. * @param extension The extension descriptor under which to replace the value.
  439. * @param index The index of the extension to be replaced.
  440. * @param value The value to be replaced in the repeated extension.
  441. **/
  442. - (void)setExtension:(GPBExtensionDescriptor *)extension index:(NSUInteger)index value:(id)value;
  443. /**
  444. * Clears the given extension for this message.
  445. *
  446. * @param extension The extension descriptor to be cleared from this message.
  447. **/
  448. - (void)clearExtension:(GPBExtensionDescriptor *)extension;
  449. /**
  450. * Resets all of the fields of this message to their default values.
  451. **/
  452. - (void)clear;
  453. @end
  454. NS_ASSUME_NONNULL_END