GPBMessage.h 14 KB

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