GPBMessage.h 17 KB

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