blob: 58c42d02ee1d63875a30d2698f188a5a5394269e [file] [log] [blame]
Thomas Van Lenten30650d82015-05-01 08:57:16 -04001// 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
Thomas Van Lenten1dcc3292015-05-21 17:14:52 -040031#import <Foundation/Foundation.h>
32
33#import "GPBBootstrap.h"
Thomas Van Lenten30650d82015-05-01 08:57:16 -040034
35@class GPBDescriptor;
36@class GPBCodedInputStream;
37@class GPBCodedOutputStream;
Thomas Van Lentend846b0b2015-06-08 16:24:57 -040038@class GPBExtensionDescriptor;
Thomas Van Lenten1dcc3292015-05-21 17:14:52 -040039@class GPBExtensionRegistry;
Thomas Van Lenten30650d82015-05-01 08:57:16 -040040@class GPBFieldDescriptor;
41@class GPBUnknownFieldSet;
42
Thomas Van Lenten8c889572015-06-16 16:45:14 -040043NS_ASSUME_NONNULL_BEGIN
44
Thomas Van Lenten1dcc3292015-05-21 17:14:52 -040045CF_EXTERN_C_BEGIN
46
Thomas Van Lenten36650a02016-03-07 12:07:03 -050047/// NSError domain used for errors.
Thomas Van Lenten1dcc3292015-05-21 17:14:52 -040048extern NSString *const GPBMessageErrorDomain;
49
Thomas Van Lenten36650a02016-03-07 12:07:03 -050050/// Error code for NSError with GPBMessageErrorDomain.
Thomas Van Lenten1dcc3292015-05-21 17:14:52 -040051typedef NS_ENUM(NSInteger, GPBMessageErrorCode) {
Thomas Van Lenten36650a02016-03-07 12:07:03 -050052 /// The data being parsed is bad and a message can not be created from it.
Thomas Van Lenten1dcc3292015-05-21 17:14:52 -040053 GPBMessageErrorCodeMalformedData = -100,
Thomas Van Lenten36650a02016-03-07 12:07:03 -050054 /// A message can't be serialized because it is missing required fields.
Thomas Van Lenten1dcc3292015-05-21 17:14:52 -040055 GPBMessageErrorCodeMissingRequiredField = -101,
56};
57
Thomas Van Lenten30650d82015-05-01 08:57:16 -040058#ifdef DEBUG
Thomas Van Lenten36650a02016-03-07 12:07:03 -050059/// In DEBUG ONLY, an NSException is thrown when a parsed message doesn't
60/// contain required fields. This key allows you to retrieve the parsed message
61/// from the exception's @c userInfo dictionary.
Thomas Van Lenten30650d82015-05-01 08:57:16 -040062extern NSString *const GPBExceptionMessageKey;
63#endif // DEBUG
64
Thomas Van Lenten1dcc3292015-05-21 17:14:52 -040065CF_EXTERN_C_END
66
Thomas Van Lenten36650a02016-03-07 12:07:03 -050067/// Base class for all of the generated message classes.
Thomas Van Lenten1dcc3292015-05-21 17:14:52 -040068@interface GPBMessage : NSObject<NSSecureCoding, NSCopying>
69
70// NOTE: If you add a instance method/property to this class that may conflict
71// with methods declared in protos, you need to update objective_helpers.cc.
Thomas Van Lenten30650d82015-05-01 08:57:16 -040072// The main cases are methods that take no arguments, or setFoo:/hasFoo: type
73// methods.
Thomas Van Lenten30650d82015-05-01 08:57:16 -040074
Thomas Van Lenten36650a02016-03-07 12:07:03 -050075/// The unknown fields for this message.
76///
77/// Only messages from proto files declared with "proto2" syntax support unknown
78/// fields. For "proto3" syntax, any unknown fields found while parsing are
79/// dropped.
Thomas Van Lenten8c889572015-06-16 16:45:14 -040080@property(nonatomic, copy, nullable) GPBUnknownFieldSet *unknownFields;
Thomas Van Lenten30650d82015-05-01 08:57:16 -040081
Thomas Van Lenten36650a02016-03-07 12:07:03 -050082/// Are all required fields set in the message and all embedded messages.
Thomas Van Lenten30650d82015-05-01 08:57:16 -040083@property(nonatomic, readonly, getter=isInitialized) BOOL initialized;
84
Thomas Van Lenten36650a02016-03-07 12:07:03 -050085/// Returns an autoreleased instance.
Thomas Van Lenten30650d82015-05-01 08:57:16 -040086+ (instancetype)message;
87
Thomas Van Lenten36650a02016-03-07 12:07:03 -050088/// Creates a new instance by parsing the data. This method should be sent to
89/// the generated message class that the data should be interpreted as. If
90/// there is an error the method returns nil and the error is returned in
91/// errorPtr (when provided).
92///
93/// @note In DEBUG builds, the parsed message is checked to be sure all required
94/// fields were provided, and the parse will fail if some are missing.
95///
96/// @param data The data to parse.
97/// @param errorPtr An optional error pointer to fill in with a failure reason if
98/// the data can not be parsed.
99///
100/// @return A new instance of the class messaged.
Thomas Van Lenten1dcc3292015-05-21 17:14:52 -0400101+ (instancetype)parseFromData:(NSData *)data error:(NSError **)errorPtr;
Thomas Van Lenten36650a02016-03-07 12:07:03 -0500102
103/// Creates a new instance by parsing the data. This method should be sent to
104/// the generated message class that the data should be interpreted as. If
105/// there is an error the method returns nil and the error is returned in
106/// errorPtr (when provided).
107///
108/// @note In DEBUG builds, the parsed message is checked to be sure all required
109/// fields were provided, and the parse will fail if some are missing.
110///
111/// @param data The data to parse.
112/// @param extensionRegistry The extension registry to use to look up extensions.
113/// @param errorPtr An optional error pointer to fill in with a failure
114/// reason if the data can not be parsed.
115///
116/// @return A new instance of the class messaged.
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400117+ (instancetype)parseFromData:(NSData *)data
Thomas Van Lenten8c889572015-06-16 16:45:14 -0400118 extensionRegistry:(nullable GPBExtensionRegistry *)extensionRegistry
Thomas Van Lenten1dcc3292015-05-21 17:14:52 -0400119 error:(NSError **)errorPtr;
Thomas Van Lenten36650a02016-03-07 12:07:03 -0500120
121/// Creates a new instance by parsing the data from the given input stream. This
122/// method should be sent to the generated message class that the data should
123/// be interpreted as. If there is an error the method returns nil and the error
124/// is returned in errorPtr (when provided).
125///
126/// @note In DEBUG builds, the parsed message is checked to be sure all required
127/// fields were provided, and the parse will fail if some are missing.
128///
129/// @param input The stream to read data from.
130/// @param extensionRegistry The extension registry to use to look up extensions.
131/// @param errorPtr An optional error pointer to fill in with a failure
132/// reason if the data can not be parsed.
133///
134/// @return A new instance of the class messaged.
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400135+ (instancetype)parseFromCodedInputStream:(GPBCodedInputStream *)input
136 extensionRegistry:
Thomas Van Lenten8c889572015-06-16 16:45:14 -0400137 (nullable GPBExtensionRegistry *)extensionRegistry
Thomas Van Lenten1dcc3292015-05-21 17:14:52 -0400138 error:(NSError **)errorPtr;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400139
Thomas Van Lenten36650a02016-03-07 12:07:03 -0500140/// Creates a new instance by parsing the data from the given input stream. This
141/// method should be sent to the generated message class that the data should
142/// be interpreted as. If there is an error the method returns nil and the error
143/// is returned in errorPtr (when provided).
144///
145/// @note Unlike the parseFrom... methods, this never checks to see if all of
146/// the required fields are set. So this method can be used to reload
147/// messages that may not be complete.
148///
149/// @param input The stream to read data from.
150/// @param extensionRegistry The extension registry to use to look up extensions.
151/// @param errorPtr An optional error pointer to fill in with a failure
152/// reason if the data can not be parsed.
153///
154/// @return A new instance of the class messaged.
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400155+ (instancetype)parseDelimitedFromCodedInputStream:(GPBCodedInputStream *)input
156 extensionRegistry:
Thomas Van Lenten8c889572015-06-16 16:45:14 -0400157 (nullable GPBExtensionRegistry *)extensionRegistry
Thomas Van Lenten1dcc3292015-05-21 17:14:52 -0400158 error:(NSError **)errorPtr;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400159
Thomas Van Lenten36650a02016-03-07 12:07:03 -0500160/// Initializes an instance by parsing the data. This method should be sent to
161/// the generated message class that the data should be interpreted as. If
162/// there is an error the method returns nil and the error is returned in
163/// errorPtr (when provided).
164///
165/// @note In DEBUG builds, the parsed message is checked to be sure all required
166/// fields were provided, and the parse will fail if some are missing.
167///
168/// @param data The data to parse.
169/// @param errorPtr An optional error pointer to fill in with a failure reason if
170/// the data can not be parsed.
Thomas Van Lenten1dcc3292015-05-21 17:14:52 -0400171- (instancetype)initWithData:(NSData *)data error:(NSError **)errorPtr;
Thomas Van Lenten36650a02016-03-07 12:07:03 -0500172
173/// Initializes an instance by parsing the data. This method should be sent to
174/// the generated message class that the data should be interpreted as. If
175/// there is an error the method returns nil and the error is returned in
176/// errorPtr (when provided).
177///
178/// @note In DEBUG builds, the parsed message is checked to be sure all required
179/// fields were provided, and the parse will fail if some are missing.
180///
181/// @param data The data to parse.
182/// @param extensionRegistry The extension registry to use to look up extensions.
183/// @param errorPtr An optional error pointer to fill in with a failure
184/// reason if the data can not be parsed.
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400185- (instancetype)initWithData:(NSData *)data
Thomas Van Lenten8c889572015-06-16 16:45:14 -0400186 extensionRegistry:(nullable GPBExtensionRegistry *)extensionRegistry
Thomas Van Lenten1dcc3292015-05-21 17:14:52 -0400187 error:(NSError **)errorPtr;
Thomas Van Lenten36650a02016-03-07 12:07:03 -0500188
189/// Initializes an instance by parsing the data from the given input stream. This
190/// method should be sent to the generated message class that the data should
191/// be interpreted as. If there is an error the method returns nil and the error
192/// is returned in errorPtr (when provided).
193///
194/// @note Unlike the parseFrom... methods, this never checks to see if all of
195/// the required fields are set. So this method can be used to reload
196/// messages that may not be complete.
197///
198/// @param input The stream to read data from.
199/// @param extensionRegistry The extension registry to use to look up extensions.
200/// @param errorPtr An optional error pointer to fill in with a failure
201/// reason if the data can not be parsed.
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400202- (instancetype)initWithCodedInputStream:(GPBCodedInputStream *)input
203 extensionRegistry:
Thomas Van Lenten8c889572015-06-16 16:45:14 -0400204 (nullable GPBExtensionRegistry *)extensionRegistry
Thomas Van Lenten1dcc3292015-05-21 17:14:52 -0400205 error:(NSError **)errorPtr;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400206
Thomas Van Lenten36650a02016-03-07 12:07:03 -0500207/// Writes out the message to the given output stream.
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400208- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)output;
Thomas Van Lenten36650a02016-03-07 12:07:03 -0500209/// Writes out the message to the given output stream.
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400210- (void)writeToOutputStream:(NSOutputStream *)output;
211
Thomas Van Lenten36650a02016-03-07 12:07:03 -0500212/// Writes out a varint for the message size followed by the the message to
213/// the given output stream.
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400214- (void)writeDelimitedToCodedOutputStream:(GPBCodedOutputStream *)output;
Thomas Van Lenten36650a02016-03-07 12:07:03 -0500215/// Writes out a varint for the message size followed by the the message to
216/// the given output stream.
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400217- (void)writeDelimitedToOutputStream:(NSOutputStream *)output;
218
Thomas Van Lenten36650a02016-03-07 12:07:03 -0500219/// Serializes the message to a @c NSData.
220///
221/// If there is an error while generating the data, nil is returned.
222///
223/// @note This value is not cached, so if you are using it repeatedly, cache
224/// it yourself.
225///
226/// @note In DEBUG ONLY, the message is also checked for all required field,
227/// if one is missing, nil will be returned.
Thomas Van Lenten8c889572015-06-16 16:45:14 -0400228- (nullable NSData *)data;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400229
Thomas Van Lenten36650a02016-03-07 12:07:03 -0500230/// Serializes a varint with the message size followed by the message data,
231/// returning that as a @c NSData.
232///
233/// @note This value is not cached, so if you are using it repeatedly, cache
234/// it yourself.
Thomas Van Lentenc27833b2015-12-07 10:49:30 -0500235- (NSData *)delimitedData;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400236
Thomas Van Lenten36650a02016-03-07 12:07:03 -0500237/// Calculates the size of the object if it were serialized.
238///
239/// This is not a cached value. If you are following a pattern like this:
240/// @code
241/// size_t size = [aMsg serializedSize];
242/// NSMutableData *foo = [NSMutableData dataWithCapacity:size + sizeof(size)];
243/// [foo writeSize:size];
244/// [foo appendData:[aMsg data]];
245/// @endcode
246/// you would be better doing:
247/// @code
248/// NSData *data = [aMsg data];
249/// NSUInteger size = [aMsg length];
250/// NSMutableData *foo = [NSMutableData dataWithCapacity:size + sizeof(size)];
251/// [foo writeSize:size];
252/// [foo appendData:data];
253/// @endcode
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400254- (size_t)serializedSize;
255
Thomas Van Lenten36650a02016-03-07 12:07:03 -0500256/// Return the descriptor for the message class.
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400257+ (GPBDescriptor *)descriptor;
Thomas Van Lenten36650a02016-03-07 12:07:03 -0500258/// Return the descriptor for the message.
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400259- (GPBDescriptor *)descriptor;
260
Thomas Van Lenten36650a02016-03-07 12:07:03 -0500261/// Test to see if the given extension is set on the message.
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400262- (BOOL)hasExtension:(GPBExtensionDescriptor *)extension;
Thomas Van Lenten36650a02016-03-07 12:07:03 -0500263
264/// Fetches the given extension's value for this message.
265///
266/// Extensions use boxed values (NSNumbers) for PODs and NSMutableArrays for
267/// repeated fields. If the extension is a Message one will be auto created for you
268/// and returned similar to fields.
Thomas Van Lenten8c889572015-06-16 16:45:14 -0400269- (nullable id)getExtension:(GPBExtensionDescriptor *)extension;
Thomas Van Lenten36650a02016-03-07 12:07:03 -0500270
271/// Sets the given extension's value for this message. This is only for single
272/// field extensions (i.e. - not repeated fields).
273///
274/// Extensions use boxed values (@c NSNumbers).
Thomas Van Lenten8c889572015-06-16 16:45:14 -0400275- (void)setExtension:(GPBExtensionDescriptor *)extension value:(nullable id)value;
Thomas Van Lenten36650a02016-03-07 12:07:03 -0500276
277/// Adds the given value to the extension for this message. This is only for
278/// repeated field extensions. If the field is a repeated POD type the @c value
279/// is a @c NSNumber.
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400280- (void)addExtension:(GPBExtensionDescriptor *)extension value:(id)value;
Thomas Van Lenten36650a02016-03-07 12:07:03 -0500281
282/// Replaces the given value at an index for the extension on this message. This
283/// is only for repeated field extensions. If the field is a repeated POD type
284/// the @c value is a @c NSNumber.
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400285- (void)setExtension:(GPBExtensionDescriptor *)extension
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400286 index:(NSUInteger)index
287 value:(id)value;
Thomas Van Lenten36650a02016-03-07 12:07:03 -0500288
289/// Clears the given extension for this message.
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400290- (void)clearExtension:(GPBExtensionDescriptor *)extension;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400291
Thomas Van Lenten36650a02016-03-07 12:07:03 -0500292/// Resets all of the fields of this message to their default values.
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400293- (void)clear;
294
Thomas Van Lenten36650a02016-03-07 12:07:03 -0500295/// Parses a message of this type from the input and merges it with this
296/// message.
297///
298/// @note This will throw if there is an error parsing the data.
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400299- (void)mergeFromData:(NSData *)data
Thomas Van Lenten8c889572015-06-16 16:45:14 -0400300 extensionRegistry:(nullable GPBExtensionRegistry *)extensionRegistry;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400301
Thomas Van Lenten36650a02016-03-07 12:07:03 -0500302/// Merges the fields from another message (of the same type) into this
303/// message.
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400304- (void)mergeFrom:(GPBMessage *)other;
305
306@end
Thomas Van Lenten8c889572015-06-16 16:45:14 -0400307
308NS_ASSUME_NONNULL_END