GPBMessage.h 17 KB

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