• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 #import <Foundation/Foundation.h>
9 
10 #import "GPBBootstrap.h"
11 #import "GPBCodedInputStream.h"
12 #import "GPBCodedOutputStream.h"
13 #import "GPBDescriptor.h"
14 #import "GPBExtensionRegistry.h"
15 #import "GPBUnknownFieldSet.h"
16 #import "GPBUnknownFields.h"
17 
18 @class GPBCodedInputStream;
19 @class GPBCodedOutputStream;
20 @class GPBUnknownFieldSet;
21 @class GPBUnknownFields;
22 
23 NS_ASSUME_NONNULL_BEGIN
24 
25 CF_EXTERN_C_BEGIN
26 
27 /** NSError domain used for errors. */
28 extern NSString *const GPBMessageErrorDomain;
29 
30 /** Error codes for NSErrors originated in GPBMessage. */
31 typedef NS_ENUM(NSInteger, GPBMessageErrorCode) {
32   /** Uncategorized error. */
33   GPBMessageErrorCodeOther = -100,
34   /** Message couldn't be serialized because it is missing required fields. */
35   GPBMessageErrorCodeMissingRequiredField = -101,
36 };
37 
38 /**
39  * Key under which the GPBMessage error's reason is stored inside the userInfo
40  * dictionary.
41  **/
42 extern NSString *const GPBErrorReasonKey;
43 
44 /**
45  * An exception name raised during serialization when the message would be
46  * larger than the 2GB limit.
47  **/
48 extern NSString *const GPBMessageExceptionMessageTooLarge;
49 
50 CF_EXTERN_C_END
51 
52 /**
53  * Base class that each generated message subclasses from.
54  *
55  * @note @c NSCopying support is a "deep copy", in that all sub objects are
56  *       copied.  Just like you wouldn't want a UIView/NSView trying to
57  *       exist in two places, you don't want a sub message to be a property
58  *       property of two other messages.
59  *
60  * @note While the class supports NSSecureCoding, if the message has any
61  *       extensions, they will end up reloaded in the unknown fields as there is
62  *       no way for the @c NSCoding plumbing to pass through a
63  *       @c GPBExtensionRegistry. To support extensions, instead of passing the
64  *       calls off to the Message, simple store the result of @c data, and then
65  *       when loading, fetch the data and use
66  *       @c +parseFromData:extensionRegistry:error: to provide an extension
67  *       registry.
68  **/
69 @interface GPBMessage : NSObject <NSSecureCoding, NSCopying>
70 
71 // If you add an instance method/property to this class that may conflict with
72 // fields declared in protos, you need to update objective_helpers.cc. The main
73 // cases are methods that take no arguments, or setFoo:/hasFoo: type methods.
74 
75 /**
76  * The set of unknown fields for this message.
77  **/
78 @property(nonatomic, copy, nullable) GPBUnknownFieldSet *unknownFields __attribute__((
79     deprecated("Use GPBUnknownFields and the -initFromMessage: initializer and "
80                "mergeUnknownFields:extensionRegistry:error: to add the data back to a message.")));
81 
82 /**
83  * Whether the message, along with all submessages, have the required fields
84  * set.
85  **/
86 @property(nonatomic, readonly, getter=isInitialized) BOOL initialized;
87 
88 /**
89  * @return An autoreleased message with the default values set.
90  **/
91 + (instancetype)message;
92 
93 /**
94  * Creates a new instance by parsing the provided data. This method should be
95  * sent to the generated message class that the data should be interpreted as.
96  * If there is an error the method returns nil and the error is returned in
97  * errorPtr (when provided).
98  *
99  * @note In DEBUG builds, the parsed message is checked to be sure all required
100  *       fields were provided, and the parse will fail if some are missing.
101  *
102  * @note The errors returned are likely coming from the domain and codes listed
103  *       at the top of this file and GPBCodedInputStream.h.
104  *
105  * @param data     The data to parse.
106  * @param errorPtr An optional error pointer to fill in with a failure reason if
107  *                 the data can not be parsed.
108  *
109  * @return A new instance of the generated class.
110  **/
111 + (nullable instancetype)parseFromData:(NSData *)data error:(NSError **)errorPtr;
112 
113 /**
114  * Creates a new instance by parsing the data. This method should be sent to
115  * the generated message class that the data should be interpreted as. If
116  * there is an error the method returns nil and the error is returned in
117  * errorPtr (when provided).
118  *
119  * @note In DEBUG builds, the parsed message is checked to be sure all required
120  *       fields were provided, and the parse will fail if some are missing.
121  *
122  * @note The errors returned are likely coming from the domain and codes listed
123  *       at the top of this file and GPBCodedInputStream.h.
124  *
125  * @param data              The data to parse.
126  * @param extensionRegistry The extension registry to use to look up extensions.
127  * @param errorPtr          An optional error pointer to fill in with a failure
128  *                          reason if the data can not be parsed.
129  *
130  * @return A new instance of the generated class.
131  **/
132 + (nullable instancetype)parseFromData:(NSData *)data
133                      extensionRegistry:(nullable id<GPBExtensionRegistry>)extensionRegistry
134                                  error:(NSError **)errorPtr;
135 
136 /**
137  * Creates a new instance by parsing the data from the given input stream. This
138  * method should be sent to the generated message class that the data should
139  * be interpreted as. If there is an error the method returns nil and the error
140  * is returned in errorPtr (when provided).
141  *
142  * @note In DEBUG builds, the parsed message is checked to be sure all required
143  *       fields were provided, and the parse will fail if some are missing.
144  *
145  * @note The errors returned are likely coming from the domain and codes listed
146  *       at the top of this file and GPBCodedInputStream.h.
147  *
148  * @param input             The stream to read data from.
149  * @param extensionRegistry The extension registry to use to look up extensions.
150  * @param errorPtr          An optional error pointer to fill in with a failure
151  *                          reason if the data can not be parsed.
152  *
153  * @return A new instance of the generated class.
154  **/
155 + (nullable instancetype)parseFromCodedInputStream:(GPBCodedInputStream *)input
156                                  extensionRegistry:
157                                      (nullable id<GPBExtensionRegistry>)extensionRegistry
158                                              error:(NSError **)errorPtr;
159 
160 /**
161  * Creates a new instance by parsing the data from the given input stream. This
162  * method should be sent to the generated message class that the data should
163  * be interpreted as. If there is an error the method returns nil and the error
164  * is returned in errorPtr (when provided).
165  *
166  * @note Unlike the parseFrom... methods, this never checks to see if all of
167  *       the required fields are set. So this method can be used to reload
168  *       messages that may not be complete.
169  *
170  * @note The errors returned are likely coming from the domain and codes listed
171  *       at the top of this file and GPBCodedInputStream.h.
172  *
173  * @param input             The stream to read data from.
174  * @param extensionRegistry The extension registry to use to look up extensions.
175  * @param errorPtr          An optional error pointer to fill in with a failure
176  *                          reason if the data can not be parsed.
177  *
178  * @return A new instance of the generated class.
179  **/
180 + (nullable instancetype)parseDelimitedFromCodedInputStream:(GPBCodedInputStream *)input
181                                           extensionRegistry:
182                                               (nullable id<GPBExtensionRegistry>)extensionRegistry
183                                                       error:(NSError **)errorPtr;
184 
185 /**
186  * Initializes an instance by parsing the data. This method should be sent to
187  * the generated message class that the data should be interpreted as. If
188  * there is an error the method returns nil and the error is returned in
189  * errorPtr (when provided).
190  *
191  * @note In DEBUG builds, the parsed message is checked to be sure all required
192  *       fields were provided, and the parse will fail if some are missing.
193  *
194  * @note The errors returned are likely coming from the domain and codes listed
195  *       at the top of this file and GPBCodedInputStream.h.
196  *
197  * @param data     The data to parse.
198  * @param errorPtr An optional error pointer to fill in with a failure reason if
199  *                 the data can not be parsed.
200  *
201  * @return An initialized instance of the generated class.
202  **/
203 - (nullable instancetype)initWithData:(NSData *)data error:(NSError **)errorPtr;
204 
205 /**
206  * Initializes an instance by parsing the data. This method should be sent to
207  * the generated message class that the data should be interpreted as. If
208  * there is an error the method returns nil and the error is returned in
209  * errorPtr (when provided).
210  *
211  * @note In DEBUG builds, the parsed message is checked to be sure all required
212  *       fields were provided, and the parse will fail if some are missing.
213  *
214  * @note The errors returned are likely coming from the domain and codes listed
215  *       at the top of this file and GPBCodedInputStream.h.
216  *
217  * @param data              The data to parse.
218  * @param extensionRegistry The extension registry to use to look up extensions.
219  * @param errorPtr          An optional error pointer to fill in with a failure
220  *                          reason if the data can not be parsed.
221  *
222  * @return An initialized instance of the generated class.
223  **/
224 - (nullable instancetype)initWithData:(NSData *)data
225                     extensionRegistry:(nullable id<GPBExtensionRegistry>)extensionRegistry
226                                 error:(NSError **)errorPtr;
227 
228 /**
229  * Initializes an instance by parsing the data from the given input stream. This
230  * method should be sent to the generated message class that the data should
231  * be interpreted as. If there is an error the method returns nil and the error
232  * is returned in errorPtr (when provided).
233  *
234  * @note Unlike the parseFrom... methods, this never checks to see if all of
235  *       the required fields are set. So this method can be used to reload
236  *       messages that may not be complete.
237  *
238  * @note The errors returned are likely coming from the domain and codes listed
239  *       at the top of this file and GPBCodedInputStream.h.
240  *
241  * @param input             The stream to read data from.
242  * @param extensionRegistry The extension registry to use to look up extensions.
243  * @param errorPtr          An optional error pointer to fill in with a failure
244  *                          reason if the data can not be parsed.
245  *
246  * @return An initialized instance of the generated class.
247  **/
248 - (nullable instancetype)initWithCodedInputStream:(GPBCodedInputStream *)input
249                                 extensionRegistry:
250                                     (nullable id<GPBExtensionRegistry>)extensionRegistry
251                                             error:(NSError **)errorPtr;
252 
253 /**
254  * Parses the given data as this message's class, and merges those values into
255  * this message.
256  *
257  * @param data              The binary representation of the message to merge.
258  * @param extensionRegistry The extension registry to use to look up extensions.
259  *
260  * @exception GPBCodedInputStreamException Exception thrown when parsing was
261  *                                         unsuccessful.
262  **/
263 - (void)mergeFromData:(NSData *)data
264     extensionRegistry:(nullable id<GPBExtensionRegistry>)extensionRegistry
265     __attribute__((deprecated(
266         "Use -mergeFromData:extensionRegistry:error: instead, especaily if calling from Swift.")));
267 
268 /**
269  * Parses the given data as this message's class, and merges those values into
270  * this message.
271  *
272  * @param data              The binary representation of the message to merge.
273  * @param extensionRegistry The extension registry to use to look up extensions.
274  * @param errorPtr          An optional error pointer to fill in with a failure
275  *                          reason if the data can not be parsed. Will only be
276  *                          filled in if the data failed to be parsed.
277  *
278  * @return Boolean indicating success. errorPtr will only be fill in on failure.
279  **/
280 - (BOOL)mergeFromData:(NSData *)data
281     extensionRegistry:(nullable id<GPBExtensionRegistry>)extensionRegistry
282                 error:(NSError **)errorPtr;
283 
284 /**
285  * Merges the fields from another message (of the same type) into this
286  * message.
287  *
288  * @param other Message to merge into this message.
289  **/
290 - (void)mergeFrom:(GPBMessage *)other;
291 
292 /**
293  * Writes out the message to the given coded output stream.
294  *
295  * @param output The coded output stream into which to write the message.
296  *
297  * @note This can raise the GPBCodedOutputStreamException_* exceptions.
298  *
299  * @note The most common cause of this failing is from one thread calling this
300  *       while another thread has a reference to this message or a message used
301  *       within a field and that other thread mutating the message while this
302  *       serialization is taking place.
303  **/
304 - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)output;
305 
306 /**
307  * Writes out the message to the given output stream.
308  *
309  * @param output The output stream into which to write the message.
310  *
311  * @note This can raise the GPBCodedOutputStreamException_* exceptions.
312  *
313  * @note The most common cause of this failing is from one thread calling this
314  *       while another thread has a reference to this message or a message used
315  *       within a field and that other thread mutating the message while this
316  *       serialization is taking place.
317  **/
318 - (void)writeToOutputStream:(NSOutputStream *)output;
319 
320 /**
321  * Writes out a varint for the message size followed by the message to
322  * the given output stream.
323  *
324  * @param output The coded output stream into which to write the message.
325  *
326  * @note This can raise the GPBCodedOutputStreamException_* exceptions.
327  *
328  * @note The most common cause of this failing is from one thread calling this
329  *       while another thread has a reference to this message or a message used
330  *       within a field and that other thread mutating the message while this
331  *       serialization is taking place.
332  **/
333 - (void)writeDelimitedToCodedOutputStream:(GPBCodedOutputStream *)output;
334 
335 /**
336  * Writes out a varint for the message size followed by the message to
337  * the given output stream.
338  *
339  * @param output The output stream into which to write the message.
340  *
341  * @note This can raise the GPBCodedOutputStreamException_* exceptions.
342  *
343  * @note The most common cause of this failing is from one thread calling this
344  *       while another thread has a reference to this message or a message used
345  *       within a field and that other thread mutating the message while this
346  *       serialization is taking place.
347  **/
348 - (void)writeDelimitedToOutputStream:(NSOutputStream *)output;
349 
350 /**
351  * Serializes the message to an NSData.
352  *
353  * If there is an error while generating the data, nil is returned.
354  *
355  * @note This value is not cached, so if you are using it repeatedly, cache
356  *       it yourself.
357  *
358  * @note In DEBUG ONLY, the message is also checked for all required field,
359  *       if one is missing, nil will be returned.
360  *
361  * @note The most common cause of this failing is from one thread calling this
362  *       while another thread has a reference to this message or a message used
363  *       within a field and that other thread mutating the message while this
364  *       serialization is taking place.
365  *
366  * @return The binary representation of the message.
367  **/
368 - (nullable NSData *)data;
369 
370 /**
371  * Serializes a varint with the message size followed by the message data,
372  * returning that as an NSData.
373  *
374  * @note This value is not cached, so if you are using it repeatedly, it is
375  *       recommended to keep a local copy.
376  *
377  * @note The most common cause of this failing is from one thread calling this
378  *       while another thread has a reference to this message or a message used
379  *       within a field and that other thread mutating the message while this
380  *       serialization is taking place.
381  *
382  * @return The binary representation of the size along with the message.
383  **/
384 - (NSData *)delimitedData;
385 
386 /**
387  * Calculates the size of the object if it were serialized.
388  *
389  * This is not a cached value. If you are following a pattern like this:
390  *
391  * ```
392  * size_t size = [aMsg serializedSize];
393  * NSMutableData *foo = [NSMutableData dataWithCapacity:size + sizeof(size)];
394  * [foo writeSize:size];
395  * [foo appendData:[aMsg data]];
396  * ```
397  *
398  * you would be better doing:
399  *
400  * ```
401  * NSData *data = [aMsg data];
402  * NSUInteger size = [aMsg length];
403  * NSMutableData *foo = [NSMutableData dataWithCapacity:size + sizeof(size)];
404  * [foo writeSize:size];
405  * [foo appendData:data];
406  * ```
407  *
408  * @return The size of the message in it's binary representation.
409  **/
410 - (size_t)serializedSize;
411 
412 /**
413  * @return The descriptor for the message class.
414  **/
415 + (GPBDescriptor *)descriptor;
416 
417 /**
418  * Return the descriptor for the message.
419  **/
420 - (GPBDescriptor *)descriptor;
421 
422 /**
423  * @return An array with the extension descriptors that are currently set on the
424  * message.
425  **/
426 - (NSArray *)extensionsCurrentlySet;
427 
428 /**
429  * Checks whether there is an extension set on the message which matches the
430  * given extension descriptor.
431  *
432  * @param extension Extension descriptor to check if it's set on the message.
433  *
434  * @return Whether the extension is currently set on the message.
435  **/
436 - (BOOL)hasExtension:(GPBExtensionDescriptor *)extension;
437 
438 /*
439  * Fetches the given extension's value for this message.
440  *
441  * Extensions use boxed values (NSNumbers) for PODs and NSMutableArrays for
442  * repeated fields. If the extension is a Message one will be auto created for
443  * you and returned similar to fields.
444  *
445  * NOTE: For Enum extensions, if the enum was _closed_, then unknown values
446  * were parsed into the message's unknown fields instead of ending up in the
447  * extensions, just like what happens with singular/repeated fields. For open
448  * enums, the _raw_ value will be in the NSNumber, meaning if one does a
449  * `switch` on the values, a `default` case should also be included.
450  *
451  * @param extension The extension descriptor of the extension to fetch.
452  *
453  * @return The extension matching the given descriptor, or nil if none found.
454  **/
455 - (nullable id)getExtension:(GPBExtensionDescriptor *)extension;
456 
457 /**
458  * Sets the given extension's value for this message. This only applies for
459  * single field extensions (i.e. - not repeated fields).
460  *
461  * Extensions use boxed values (NSNumbers).
462  *
463  * @param extension The extension descriptor under which to set the value.
464  * @param value     The value to be set as the extension.
465  **/
466 - (void)setExtension:(GPBExtensionDescriptor *)extension value:(nullable id)value;
467 
468 /**
469  * Adds the given value to the extension for this message. This only applies
470  * to repeated field extensions. If the field is a repeated POD type, the value
471  * should be an NSNumber.
472  *
473  * @param extension The extension descriptor under which to add the value.
474  * @param value     The value to be added to the repeated extension.
475  **/
476 - (void)addExtension:(GPBExtensionDescriptor *)extension value:(id)value;
477 
478 /**
479  * Replaces the value at the given index with the given value for the extension
480  * on this message. This only applies to repeated field extensions. If the field
481  * is a repeated POD type, the value is should be an NSNumber.
482  *
483  * @param extension The extension descriptor under which to replace the value.
484  * @param index     The index of the extension to be replaced.
485  * @param value     The value to be replaced in the repeated extension.
486  **/
487 - (void)setExtension:(GPBExtensionDescriptor *)extension index:(NSUInteger)index value:(id)value;
488 
489 /**
490  * Clears the given extension for this message.
491  *
492  * @param extension The extension descriptor to be cleared from this message.
493  **/
494 - (void)clearExtension:(GPBExtensionDescriptor *)extension;
495 
496 /**
497  * Resets all of the fields of this message to their default values.
498  **/
499 - (void)clear;
500 
501 /**
502  * Clears any unknown fields on this message.
503  *
504  * Note: To clear this message's unknown field and all the unknown fields of the
505  * messages within the fields of this message, use
506  * `GPBMessageDropUnknownFieldsRecursively()`.
507  **/
508 - (void)clearUnknownFields;
509 
510 /**
511  * Merges in the data from an `GPBUnknownFields`, meaning the data from the unknown fields gets
512  * re-parsed so any known fields will be properly set.
513  *
514  * If the intent is to *replace* the message's unknown fields, call `-clearUnknownFields` first.
515  *
516  * Since the data from the GPBUnknownFields will always be well formed, this call will almost never
517  * fail. What could cause it to fail is if the GPBUnknownFields contains a field value that is
518  * an error for the message's schema - i.e.: if it contains a length delimited field where the
519  * field number for the message is defined to be a _string_ field, however the length delimited
520  * data provide is not a valid UTF8 string, or if the field is a _packed_ number field, but the
521  * data provided is not a valid for that field.
522  *
523  * @param unknownFields     The unknown fields to merge the data from.
524  * @param extensionRegistry The extension registry to use to look up extensions, can be `nil`.
525  * @param errorPtr          An optional error pointer to fill in with a failure
526  *                          reason if the data can not be parsed. Will only be
527  *                          filled in if the data failed to be parsed.
528  *
529  * @return Boolean indicating success. errorPtr will only be fill in on failure.
530  **/
531 - (BOOL)mergeUnknownFields:(GPBUnknownFields *)unknownFields
532          extensionRegistry:(nullable id<GPBExtensionRegistry>)extensionRegistry
533                      error:(NSError **)errorPtr;
534 
535 @end
536 
537 NS_ASSUME_NONNULL_END
538