GPBMessage.h 18 KB

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