1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
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
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.
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.
31 #import <Foundation/Foundation.h>
33 #import "GPBBootstrap.h"
36 @class GPBCodedInputStream;
37 @class GPBCodedOutputStream;
38 @class GPBExtensionDescriptor;
39 @class GPBExtensionRegistry;
40 @class GPBFieldDescriptor;
41 @class GPBUnknownFieldSet;
43 NS_ASSUME_NONNULL_BEGIN
47 /** NSError domain used for errors. */
48 extern NSString *const GPBMessageErrorDomain;
50 /** Error codes for NSErrors originated in GPBMessage. */
51 typedef NS_ENUM(NSInteger, GPBMessageErrorCode) {
52 /** Uncategorized error. */
53 GPBMessageErrorCodeOther = -100,
54 /** Message couldn't be serialized because it is missing required fields. */
55 GPBMessageErrorCodeMissingRequiredField = -101,
59 * Key under which the GPBMessage error's reason is stored inside the userInfo
62 extern NSString *const GPBErrorReasonKey;
67 * Base class that each generated message subclasses from.
69 * @note @c NSCopying support is a "deep copy", in that all sub objects are
70 * copied. Just like you wouldn't want a UIView/NSView trying to
71 * exist in two places, you don't want a sub message to be a property
72 * property of two other messages.
74 * @note While the class support NSSecureCoding, if the message has any
75 * extensions, they will end up reloaded in @c unknownFields as there is
76 * no way for the @c NSCoding plumbing to pass through a
77 * @c GPBExtensionRegistry. To support extensions, instead of passing the
78 * calls off to the Message, simple store the result of @c data, and then
79 * when loading, fetch the data and use
80 * @c +parseFromData:extensionRegistry:error: to provide an extension
83 @interface GPBMessage : NSObject<NSSecureCoding, NSCopying>
85 // If you add an instance method/property to this class that may conflict with
86 // fields declared in protos, you need to update objective_helpers.cc. The main
87 // cases are methods that take no arguments, or setFoo:/hasFoo: type methods.
90 * The set of unknown fields for this message.
92 * Only messages from proto files declared with "proto2" syntax support unknown
93 * fields. For "proto3" syntax, any unknown fields found while parsing are
96 @property(nonatomic, copy, nullable) GPBUnknownFieldSet *unknownFields;
99 * Whether the message, along with all submessages, have the required fields
100 * set. This is only applicable for files declared with "proto2" syntax, as
101 * there are no required fields for "proto3" syntax.
103 @property(nonatomic, readonly, getter=isInitialized) BOOL initialized;
106 * @return An autoreleased message with the default values set.
108 + (instancetype)message;
111 * Creates a new instance by parsing the provided data. This method should be
112 * sent to the generated message class that the data should be interpreted as.
113 * If there is an error the method returns nil and the error is returned in
114 * errorPtr (when provided).
116 * @note In DEBUG builds, the parsed message is checked to be sure all required
117 * fields were provided, and the parse will fail if some are missing.
119 * @note The errors returned are likely coming from the domain and codes listed
120 * at the top of this file and GPBCodedInputStream.h.
122 * @param data The data to parse.
123 * @param errorPtr An optional error pointer to fill in with a failure reason if
124 * the data can not be parsed.
126 * @return A new instance of the generated class.
128 + (nullable instancetype)parseFromData:(NSData *)data error:(NSError **)errorPtr;
131 * Creates a new instance by parsing the data. This method should be sent to
132 * the generated message class that the data should be interpreted as. If
133 * there is an error the method returns nil and the error is returned in
134 * errorPtr (when provided).
136 * @note In DEBUG builds, the parsed message is checked to be sure all required
137 * fields were provided, and the parse will fail if some are missing.
139 * @note The errors returned are likely coming from the domain and codes listed
140 * at the top of this file and GPBCodedInputStream.h.
142 * @param data The data to parse.
143 * @param extensionRegistry The extension registry to use to look up extensions.
144 * @param errorPtr An optional error pointer to fill in with a failure
145 * reason if the data can not be parsed.
147 * @return A new instance of the generated class.
149 + (nullable instancetype)parseFromData:(NSData *)data
150 extensionRegistry:(nullable GPBExtensionRegistry *)extensionRegistry
151 error:(NSError **)errorPtr;
154 * Creates a new instance by parsing the data from the given input stream. This
155 * method should be sent to the generated message class that the data should
156 * be interpreted as. If there is an error the method returns nil and the error
157 * is returned in errorPtr (when provided).
159 * @note In DEBUG builds, the parsed message is checked to be sure all required
160 * fields were provided, and the parse will fail if some are missing.
162 * @note The errors returned are likely coming from the domain and codes listed
163 * at the top of this file and GPBCodedInputStream.h.
165 * @param input The stream to read data from.
166 * @param extensionRegistry The extension registry to use to look up extensions.
167 * @param errorPtr An optional error pointer to fill in with a failure
168 * reason if the data can not be parsed.
170 * @return A new instance of the generated class.
172 + (nullable instancetype)parseFromCodedInputStream:(GPBCodedInputStream *)input
174 (nullable GPBExtensionRegistry *)extensionRegistry
175 error:(NSError **)errorPtr;
178 * Creates a new instance by parsing the data from the given input stream. This
179 * method should be sent to the generated message class that the data should
180 * be interpreted as. If there is an error the method returns nil and the error
181 * is returned in errorPtr (when provided).
183 * @note Unlike the parseFrom... methods, this never checks to see if all of
184 * the required fields are set. So this method can be used to reload
185 * messages that may not be complete.
187 * @note The errors returned are likely coming from the domain and codes listed
188 * at the top of this file and GPBCodedInputStream.h.
190 * @param input The stream to read data from.
191 * @param extensionRegistry The extension registry to use to look up extensions.
192 * @param errorPtr An optional error pointer to fill in with a failure
193 * reason if the data can not be parsed.
195 * @return A new instance of the generated class.
197 + (nullable instancetype)parseDelimitedFromCodedInputStream:(GPBCodedInputStream *)input
199 (nullable GPBExtensionRegistry *)extensionRegistry
200 error:(NSError **)errorPtr;
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).
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.
211 * @note The errors returned are likely coming from the domain and codes listed
212 * at the top of this file and GPBCodedInputStream.h.
214 * @param data The data to parse.
215 * @param errorPtr An optional error pointer to fill in with a failure reason if
216 * the data can not be parsed.
218 * @return An initialized instance of the generated class.
220 - (nullable instancetype)initWithData:(NSData *)data error:(NSError **)errorPtr;
223 * Initializes an instance by parsing the data. This method should be sent to
224 * the generated message class that the data should be interpreted as. If
225 * there is an error the method returns nil and the error is returned in
226 * errorPtr (when provided).
228 * @note In DEBUG builds, the parsed message is checked to be sure all required
229 * fields were provided, and the parse will fail if some are missing.
231 * @note The errors returned are likely coming from the domain and codes listed
232 * at the top of this file and GPBCodedInputStream.h.
234 * @param data The data to parse.
235 * @param extensionRegistry The extension registry to use to look up extensions.
236 * @param errorPtr An optional error pointer to fill in with a failure
237 * reason if the data can not be parsed.
239 * @return An initialized instance of the generated class.
241 - (nullable instancetype)initWithData:(NSData *)data
242 extensionRegistry:(nullable GPBExtensionRegistry *)extensionRegistry
243 error:(NSError **)errorPtr;
246 * Initializes an instance by parsing the data from the given input stream. This
247 * method should be sent to the generated message class that the data should
248 * be interpreted as. If there is an error the method returns nil and the error
249 * is returned in errorPtr (when provided).
251 * @note Unlike the parseFrom... methods, this never checks to see if all of
252 * the required fields are set. So this method can be used to reload
253 * messages that may not be complete.
255 * @note The errors returned are likely coming from the domain and codes listed
256 * at the top of this file and GPBCodedInputStream.h.
258 * @param input The stream to read data from.
259 * @param extensionRegistry The extension registry to use to look up extensions.
260 * @param errorPtr An optional error pointer to fill in with a failure
261 * reason if the data can not be parsed.
263 * @return An initialized instance of the generated class.
265 - (nullable instancetype)initWithCodedInputStream:(GPBCodedInputStream *)input
267 (nullable GPBExtensionRegistry *)extensionRegistry
268 error:(NSError **)errorPtr;
271 * Parses the given data as this message's class, and merges those values into
274 * @param data The binary representation of the message to merge.
275 * @param extensionRegistry The extension registry to use to look up extensions.
277 * @exception GPBCodedInputStreamException Exception thrown when parsing was
280 - (void)mergeFromData:(NSData *)data
281 extensionRegistry:(nullable GPBExtensionRegistry *)extensionRegistry;
284 * Merges the fields from another message (of the same type) into this
287 * @param other Message to merge into this message.
289 - (void)mergeFrom:(GPBMessage *)other;
292 * Writes out the message to the given coded output stream.
294 * @param output The coded output stream into which to write the message.
296 * @note This can raise the GPBCodedOutputStreamException_* exceptions.
299 - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)output;
302 * Writes out the message to the given output stream.
304 * @param output The output stream into which to write the message.
306 * @note This can raise the GPBCodedOutputStreamException_* exceptions.
308 - (void)writeToOutputStream:(NSOutputStream *)output;
311 * Writes out a varint for the message size followed by the the message to
312 * the given output stream.
314 * @param output The coded output stream into which to write the message.
316 * @note This can raise the GPBCodedOutputStreamException_* exceptions.
318 - (void)writeDelimitedToCodedOutputStream:(GPBCodedOutputStream *)output;
321 * Writes out a varint for the message size followed by the the message to
322 * the given output stream.
324 * @param output The output stream into which to write the message.
326 * @note This can raise the GPBCodedOutputStreamException_* exceptions.
328 - (void)writeDelimitedToOutputStream:(NSOutputStream *)output;
331 * Serializes the message to an NSData.
333 * If there is an error while generating the data, nil is returned.
335 * @note This value is not cached, so if you are using it repeatedly, cache
338 * @note In DEBUG ONLY, the message is also checked for all required field,
339 * if one is missing, nil will be returned.
341 * @return The binary representation of the message.
343 - (nullable NSData *)data;
346 * Serializes a varint with the message size followed by the message data,
347 * returning that as an NSData.
349 * @note This value is not cached, so if you are using it repeatedly, it is
350 * recommended to keep a local copy.
352 * @return The binary representation of the size along with the message.
354 - (NSData *)delimitedData;
357 * Calculates the size of the object if it were serialized.
359 * This is not a cached value. If you are following a pattern like this:
362 * size_t size = [aMsg serializedSize];
363 * NSMutableData *foo = [NSMutableData dataWithCapacity:size + sizeof(size)];
364 * [foo writeSize:size];
365 * [foo appendData:[aMsg data]];
368 * you would be better doing:
371 * NSData *data = [aMsg data];
372 * NSUInteger size = [aMsg length];
373 * NSMutableData *foo = [NSMutableData dataWithCapacity:size + sizeof(size)];
374 * [foo writeSize:size];
375 * [foo appendData:data];
378 * @return The size of the message in it's binary representation.
380 - (size_t)serializedSize;
383 * @return The descriptor for the message class.
385 + (GPBDescriptor *)descriptor;
388 * Return the descriptor for the message.
390 - (GPBDescriptor *)descriptor;
393 * @return An array with the extension descriptors that are currently set on the
396 - (NSArray *)extensionsCurrentlySet;
399 * Checks whether there is an extension set on the message which matches the
400 * given extension descriptor.
402 * @param extension Extension descriptor to check if it's set on the message.
404 * @return Whether the extension is currently set on the message.
406 - (BOOL)hasExtension:(GPBExtensionDescriptor *)extension;
409 * Fetches the given extension's value for this message.
411 * Extensions use boxed values (NSNumbers) for PODs and NSMutableArrays for
412 * repeated fields. If the extension is a Message one will be auto created for
413 * you and returned similar to fields.
415 * @param extension The extension descriptor of the extension to fetch.
417 * @return The extension matching the given descriptor, or nil if none found.
419 - (nullable id)getExtension:(GPBExtensionDescriptor *)extension;
422 * Sets the given extension's value for this message. This only applies for
423 * single field extensions (i.e. - not repeated fields).
425 * Extensions use boxed values (NSNumbers).
427 * @param extension The extension descriptor under which to set the value.
428 * @param value The value to be set as the extension.
430 - (void)setExtension:(GPBExtensionDescriptor *)extension
431 value:(nullable id)value;
434 * Adds the given value to the extension for this message. This only applies
435 * to repeated field extensions. If the field is a repeated POD type, the value
436 * should be an NSNumber.
438 * @param extension The extension descriptor under which to add the value.
439 * @param value The value to be added to the repeated extension.
441 - (void)addExtension:(GPBExtensionDescriptor *)extension value:(id)value;
444 * Replaces the value at the given index with the given value for the extension
445 * on this message. This only applies to repeated field extensions. If the field
446 * is a repeated POD type, the value is should be an NSNumber.
448 * @param extension The extension descriptor under which to replace the value.
449 * @param index The index of the extension to be replaced.
450 * @param value The value to be replaced in the repeated extension.
452 - (void)setExtension:(GPBExtensionDescriptor *)extension
453 index:(NSUInteger)index
457 * Clears the given extension for this message.
459 * @param extension The extension descriptor to be cleared from this message.
461 - (void)clearExtension:(GPBExtensionDescriptor *)extension;
464 * Resets all of the fields of this message to their default values.
470 NS_ASSUME_NONNULL_END