GPBMessage.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 code for NSError with GPBMessageErrorDomain.
  44. typedef NS_ENUM(NSInteger, GPBMessageErrorCode) {
  45. /// Uncategorized error.
  46. GPBMessageErrorCodeOther = -100,
  47. /// A message can't be serialized because it is missing required fields.
  48. GPBMessageErrorCodeMissingRequiredField = -101,
  49. };
  50. /// Key under which the error's reason is stored inside the userInfo dictionary.
  51. extern NSString *const GPBErrorReasonKey;
  52. CF_EXTERN_C_END
  53. /// Base class for all of the generated message classes.
  54. @interface GPBMessage : NSObject<NSSecureCoding, NSCopying>
  55. // NOTE: If you add a instance method/property to this class that may conflict
  56. // with methods declared in protos, you need to update objective_helpers.cc.
  57. // The main cases are methods that take no arguments, or setFoo:/hasFoo: type
  58. // methods.
  59. /// The unknown fields for this message.
  60. ///
  61. /// Only messages from proto files declared with "proto2" syntax support unknown
  62. /// fields. For "proto3" syntax, any unknown fields found while parsing are
  63. /// dropped.
  64. @property(nonatomic, copy, nullable) GPBUnknownFieldSet *unknownFields;
  65. /// Are all required fields set in the message and all embedded messages.
  66. @property(nonatomic, readonly, getter=isInitialized) BOOL initialized;
  67. /// Returns an autoreleased instance.
  68. + (instancetype)message;
  69. /// Creates a new instance by parsing the data. This method should be sent to
  70. /// the generated message class that the data should be interpreted as. If
  71. /// there is an error the method returns nil and the error is returned in
  72. /// errorPtr (when provided).
  73. ///
  74. /// @note In DEBUG builds, the parsed message is checked to be sure all required
  75. /// fields were provided, and the parse will fail if some are missing.
  76. ///
  77. /// @note The errors returned are likely coming from the domain and codes listed
  78. /// at the top of this file and GPBCodedInputStream.h.
  79. ///
  80. /// @param data The data to parse.
  81. /// @param errorPtr An optional error pointer to fill in with a failure reason if
  82. /// the data can not be parsed.
  83. ///
  84. /// @return A new instance of the class messaged.
  85. + (nullable instancetype)parseFromData:(NSData *)data error:(NSError **)errorPtr;
  86. /// Creates a new instance by parsing the data. This method should be sent to
  87. /// the generated message class that the data should be interpreted as. If
  88. /// there is an error the method returns nil and the error is returned in
  89. /// errorPtr (when provided).
  90. ///
  91. /// @note In DEBUG builds, the parsed message is checked to be sure all required
  92. /// fields were provided, and the parse will fail if some are missing.
  93. ///
  94. /// @note The errors returned are likely coming from the domain and codes listed
  95. /// at the top of this file and GPBCodedInputStream.h.
  96. ///
  97. /// @param data The data to parse.
  98. /// @param extensionRegistry The extension registry to use to look up extensions.
  99. /// @param errorPtr An optional error pointer to fill in with a failure
  100. /// reason if the data can not be parsed.
  101. ///
  102. /// @return A new instance of the class messaged.
  103. + (nullable instancetype)parseFromData:(NSData *)data
  104. extensionRegistry:(nullable GPBExtensionRegistry *)extensionRegistry
  105. error:(NSError **)errorPtr;
  106. /// Creates a new instance by parsing the data from the given input stream. This
  107. /// method should be sent to the generated message class that the data should
  108. /// be interpreted as. If there is an error the method returns nil and the error
  109. /// is returned in errorPtr (when provided).
  110. ///
  111. /// @note In DEBUG builds, the parsed message is checked to be sure all required
  112. /// fields were provided, and the parse will fail if some are missing.
  113. ///
  114. /// @note The errors returned are likely coming from the domain and codes listed
  115. /// at the top of this file and GPBCodedInputStream.h.
  116. ///
  117. /// @param input The stream to read data from.
  118. /// @param extensionRegistry The extension registry to use to look up extensions.
  119. /// @param errorPtr An optional error pointer to fill in with a failure
  120. /// reason if the data can not be parsed.
  121. ///
  122. /// @return A new instance of the class messaged.
  123. + (nullable instancetype)parseFromCodedInputStream:(GPBCodedInputStream *)input
  124. extensionRegistry:
  125. (nullable GPBExtensionRegistry *)extensionRegistry
  126. error:(NSError **)errorPtr;
  127. /// Creates a new instance by parsing the data from the given input stream. This
  128. /// method should be sent to the generated message class that the data should
  129. /// be interpreted as. If there is an error the method returns nil and the error
  130. /// is returned in errorPtr (when provided).
  131. ///
  132. /// @note Unlike the parseFrom... methods, this never checks to see if all of
  133. /// the required fields are set. So this method can be used to reload
  134. /// messages that may not be complete.
  135. ///
  136. /// @note The errors returned are likely coming from the domain and codes listed
  137. /// at the top of this file and GPBCodedInputStream.h.
  138. ///
  139. /// @param input The stream to read data from.
  140. /// @param extensionRegistry The extension registry to use to look up extensions.
  141. /// @param errorPtr An optional error pointer to fill in with a failure
  142. /// reason if the data can not be parsed.
  143. ///
  144. /// @return A new instance of the class messaged.
  145. + (nullable instancetype)parseDelimitedFromCodedInputStream:(GPBCodedInputStream *)input
  146. extensionRegistry:
  147. (nullable GPBExtensionRegistry *)extensionRegistry
  148. error:(NSError **)errorPtr;
  149. /// Initializes an instance by parsing the data. This method should be sent to
  150. /// the generated message class that the data should be interpreted as. If
  151. /// there is an error the method returns nil and the error is returned in
  152. /// errorPtr (when provided).
  153. ///
  154. /// @note In DEBUG builds, the parsed message is checked to be sure all required
  155. /// fields were provided, and the parse will fail if some are missing.
  156. ///
  157. /// @note The errors returned are likely coming from the domain and codes listed
  158. /// at the top of this file and GPBCodedInputStream.h.
  159. ///
  160. /// @param data The data to parse.
  161. /// @param errorPtr An optional error pointer to fill in with a failure reason if
  162. /// the data can not be parsed.
  163. - (nullable instancetype)initWithData:(NSData *)data error:(NSError **)errorPtr;
  164. /// Initializes an instance by parsing the data. This method should be sent to
  165. /// the generated message class that the data should be interpreted as. If
  166. /// there is an error the method returns nil and the error is returned in
  167. /// errorPtr (when provided).
  168. ///
  169. /// @note In DEBUG builds, the parsed message is checked to be sure all required
  170. /// fields were provided, and the parse will fail if some are missing.
  171. ///
  172. /// @note The errors returned are likely coming from the domain and codes listed
  173. /// at the top of this file and GPBCodedInputStream.h.
  174. ///
  175. /// @param data The data to parse.
  176. /// @param extensionRegistry The extension registry to use to look up extensions.
  177. /// @param errorPtr An optional error pointer to fill in with a failure
  178. /// reason if the data can not be parsed.
  179. - (nullable instancetype)initWithData:(NSData *)data
  180. extensionRegistry:(nullable GPBExtensionRegistry *)extensionRegistry
  181. error:(NSError **)errorPtr;
  182. /// Initializes an instance by parsing the data from the given input stream. This
  183. /// method should be sent to the generated message class that the data should
  184. /// be interpreted as. If there is an error the method returns nil and the error
  185. /// is returned in errorPtr (when provided).
  186. ///
  187. /// @note Unlike the parseFrom... methods, this never checks to see if all of
  188. /// the required fields are set. So this method can be used to reload
  189. /// messages that may not be complete.
  190. ///
  191. /// @note The errors returned are likely coming from the domain and codes listed
  192. /// at the top of this file and GPBCodedInputStream.h.
  193. ///
  194. /// @param input The stream to read data from.
  195. /// @param extensionRegistry The extension registry to use to look up extensions.
  196. /// @param errorPtr An optional error pointer to fill in with a failure
  197. /// reason if the data can not be parsed.
  198. - (nullable instancetype)initWithCodedInputStream:(GPBCodedInputStream *)input
  199. extensionRegistry:
  200. (nullable GPBExtensionRegistry *)extensionRegistry
  201. error:(NSError **)errorPtr;
  202. /// Writes out the message to the given output stream.
  203. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)output;
  204. /// Writes out the message to the given output stream.
  205. - (void)writeToOutputStream:(NSOutputStream *)output;
  206. /// Writes out a varint for the message size followed by the the message to
  207. /// the given output stream.
  208. - (void)writeDelimitedToCodedOutputStream:(GPBCodedOutputStream *)output;
  209. /// Writes out a varint for the message size followed by the the message to
  210. /// the given output stream.
  211. - (void)writeDelimitedToOutputStream:(NSOutputStream *)output;
  212. /// Serializes the message to a @c NSData.
  213. ///
  214. /// If there is an error while generating the data, nil is returned.
  215. ///
  216. /// @note This value is not cached, so if you are using it repeatedly, cache
  217. /// it yourself.
  218. ///
  219. /// @note In DEBUG ONLY, the message is also checked for all required field,
  220. /// if one is missing, nil will be returned.
  221. - (nullable NSData *)data;
  222. /// Serializes a varint with the message size followed by the message data,
  223. /// returning that as a @c NSData.
  224. ///
  225. /// @note This value is not cached, so if you are using it repeatedly, cache
  226. /// it yourself.
  227. - (NSData *)delimitedData;
  228. /// Calculates the size of the object if it were serialized.
  229. ///
  230. /// This is not a cached value. If you are following a pattern like this:
  231. /// @code
  232. /// size_t size = [aMsg serializedSize];
  233. /// NSMutableData *foo = [NSMutableData dataWithCapacity:size + sizeof(size)];
  234. /// [foo writeSize:size];
  235. /// [foo appendData:[aMsg data]];
  236. /// @endcode
  237. /// you would be better doing:
  238. /// @code
  239. /// NSData *data = [aMsg data];
  240. /// NSUInteger size = [aMsg length];
  241. /// NSMutableData *foo = [NSMutableData dataWithCapacity:size + sizeof(size)];
  242. /// [foo writeSize:size];
  243. /// [foo appendData:data];
  244. /// @endcode
  245. - (size_t)serializedSize;
  246. /// Return the descriptor for the message class.
  247. + (GPBDescriptor *)descriptor;
  248. /// Return the descriptor for the message.
  249. - (GPBDescriptor *)descriptor;
  250. /// Test to see if the given extension is set on the message.
  251. - (BOOL)hasExtension:(GPBExtensionDescriptor *)extension;
  252. /// Fetches the given extension's value for this message.
  253. ///
  254. /// Extensions use boxed values (NSNumbers) for PODs and NSMutableArrays for
  255. /// repeated fields. If the extension is a Message one will be auto created for you
  256. /// and returned similar to fields.
  257. - (nullable id)getExtension:(GPBExtensionDescriptor *)extension;
  258. /// Sets the given extension's value for this message. This is only for single
  259. /// field extensions (i.e. - not repeated fields).
  260. ///
  261. /// Extensions use boxed values (@c NSNumbers).
  262. - (void)setExtension:(GPBExtensionDescriptor *)extension value:(nullable id)value;
  263. /// Adds the given value to the extension for this message. This is only for
  264. /// repeated field extensions. If the field is a repeated POD type the @c value
  265. /// is a @c NSNumber.
  266. - (void)addExtension:(GPBExtensionDescriptor *)extension value:(id)value;
  267. /// Replaces the given value at an index for the extension on this message. This
  268. /// is only for repeated field extensions. If the field is a repeated POD type
  269. /// the @c value is a @c NSNumber.
  270. - (void)setExtension:(GPBExtensionDescriptor *)extension
  271. index:(NSUInteger)index
  272. value:(id)value;
  273. /// Clears the given extension for this message.
  274. - (void)clearExtension:(GPBExtensionDescriptor *)extension;
  275. /// Resets all of the fields of this message to their default values.
  276. - (void)clear;
  277. /// Parses a message of this type from the input and merges it with this
  278. /// message.
  279. ///
  280. /// @note This will throw if there is an error parsing the data.
  281. - (void)mergeFromData:(NSData *)data
  282. extensionRegistry:(nullable GPBExtensionRegistry *)extensionRegistry;
  283. /// Merges the fields from another message (of the same type) into this
  284. /// message.
  285. - (void)mergeFrom:(GPBMessage *)other;
  286. @end
  287. NS_ASSUME_NONNULL_END