GPBMessage.h 14.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.  All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

31 32 33
#import <Foundation/Foundation.h>

#import "GPBBootstrap.h"
34 35 36 37

@class GPBDescriptor;
@class GPBCodedInputStream;
@class GPBCodedOutputStream;
38
@class GPBExtensionDescriptor;
39
@class GPBExtensionRegistry;
40 41 42
@class GPBFieldDescriptor;
@class GPBUnknownFieldSet;

43 44
NS_ASSUME_NONNULL_BEGIN

45 46
CF_EXTERN_C_BEGIN

47
/// NSError domain used for errors.
48 49
extern NSString *const GPBMessageErrorDomain;

50
/// Error code for NSError with GPBMessageErrorDomain.
51
typedef NS_ENUM(NSInteger, GPBMessageErrorCode) {
52 53
  /// Uncategorized error.
  GPBMessageErrorCodeOther = -100,
54
  /// A message can't be serialized because it is missing required fields.
55 56 57
  GPBMessageErrorCodeMissingRequiredField = -101,
};

58 59 60
/// Key under which the error's reason is stored inside the userInfo dictionary.
extern NSString *const GPBErrorReasonKey;

61 62
CF_EXTERN_C_END

63
/// Base class for all of the generated message classes.
64 65 66 67
@interface GPBMessage : NSObject<NSSecureCoding, NSCopying>

// NOTE: If you add a instance method/property to this class that may conflict
// with methods declared in protos, you need to update objective_helpers.cc.
68 69 70
// The main cases are methods that take no arguments, or setFoo:/hasFoo: type
// methods.

71 72 73 74 75
/// The unknown fields for this message.
///
/// Only messages from proto files declared with "proto2" syntax support unknown
/// fields. For "proto3" syntax, any unknown fields found while parsing are
/// dropped.
76
@property(nonatomic, copy, nullable) GPBUnknownFieldSet *unknownFields;
77

78
/// Are all required fields set in the message and all embedded messages.
79 80
@property(nonatomic, readonly, getter=isInitialized) BOOL initialized;

81
/// Returns an autoreleased instance.
82 83
+ (instancetype)message;

84 85 86 87 88 89 90 91
/// Creates a new instance by parsing the data. This method should be sent to
/// the generated message class that the data should be interpreted as. If
/// there is an error the method returns nil and the error is returned in
/// errorPtr (when provided).
///
/// @note In DEBUG builds, the parsed message is checked to be sure all required
///       fields were provided, and the parse will fail if some are missing.
///
92 93 94
/// @note The errors returned are likely coming from the domain and codes listed
///       at the top of this file and GPBCodedInputStream.h.
///
95 96 97 98 99
/// @param data     The data to parse.
/// @param errorPtr An optional error pointer to fill in with a failure reason if
///                 the data can not be parsed.
///
/// @return A new instance of the class messaged.
100
+ (instancetype)parseFromData:(NSData *)data error:(NSError **)errorPtr;
101 102 103 104 105 106 107 108 109

/// Creates a new instance by parsing the data. This method should be sent to
/// the generated message class that the data should be interpreted as. If
/// there is an error the method returns nil and the error is returned in
/// errorPtr (when provided).
///
/// @note In DEBUG builds, the parsed message is checked to be sure all required
///       fields were provided, and the parse will fail if some are missing.
///
110 111 112
/// @note The errors returned are likely coming from the domain and codes listed
///       at the top of this file and GPBCodedInputStream.h.
///
113 114 115 116 117 118
/// @param data              The data to parse.
/// @param extensionRegistry The extension registry to use to look up extensions.
/// @param errorPtr          An optional error pointer to fill in with a failure
///                          reason if the data can not be parsed.
///
/// @return A new instance of the class messaged.
119
+ (instancetype)parseFromData:(NSData *)data
120
            extensionRegistry:(nullable GPBExtensionRegistry *)extensionRegistry
121
                        error:(NSError **)errorPtr;
122 123 124 125 126 127 128 129 130

/// Creates a new instance by parsing the data from the given input stream. This
/// method should be sent to the generated message class that the data should
/// be interpreted as. If there is an error the method returns nil and the error
/// is returned in errorPtr (when provided).
///
/// @note In DEBUG builds, the parsed message is checked to be sure all required
///       fields were provided, and the parse will fail if some are missing.
///
131 132 133
/// @note The errors returned are likely coming from the domain and codes listed
///       at the top of this file and GPBCodedInputStream.h.
///
134 135 136 137 138 139
/// @param input             The stream to read data from.
/// @param extensionRegistry The extension registry to use to look up extensions.
/// @param errorPtr          An optional error pointer to fill in with a failure
///                          reason if the data can not be parsed.
///
/// @return A new instance of the class messaged.
140 141
+ (instancetype)parseFromCodedInputStream:(GPBCodedInputStream *)input
                        extensionRegistry:
142
                            (nullable GPBExtensionRegistry *)extensionRegistry
143
                                    error:(NSError **)errorPtr;
144

145 146 147 148 149 150 151 152 153
/// Creates a new instance by parsing the data from the given input stream. This
/// method should be sent to the generated message class that the data should
/// be interpreted as. If there is an error the method returns nil and the error
/// is returned in errorPtr (when provided).
///
/// @note Unlike the parseFrom... methods, this never checks to see if all of
///       the required fields are set. So this method can be used to reload
///       messages that may not be complete.
///
154 155 156
/// @note The errors returned are likely coming from the domain and codes listed
///       at the top of this file and GPBCodedInputStream.h.
///
157 158 159 160 161 162
/// @param input             The stream to read data from.
/// @param extensionRegistry The extension registry to use to look up extensions.
/// @param errorPtr          An optional error pointer to fill in with a failure
///                          reason if the data can not be parsed.
///
/// @return A new instance of the class messaged.
163 164
+ (instancetype)parseDelimitedFromCodedInputStream:(GPBCodedInputStream *)input
                                 extensionRegistry:
165
                                     (nullable GPBExtensionRegistry *)extensionRegistry
166 167
                                             error:(NSError **)errorPtr;

168 169 170 171 172 173 174 175
/// Initializes an instance by parsing the data. This method should be sent to
/// the generated message class that the data should be interpreted as. If
/// there is an error the method returns nil and the error is returned in
/// errorPtr (when provided).
///
/// @note In DEBUG builds, the parsed message is checked to be sure all required
///       fields were provided, and the parse will fail if some are missing.
///
176 177 178
/// @note The errors returned are likely coming from the domain and codes listed
///       at the top of this file and GPBCodedInputStream.h.
///
179 180 181
/// @param data     The data to parse.
/// @param errorPtr An optional error pointer to fill in with a failure reason if
///                 the data can not be parsed.
182
- (instancetype)initWithData:(NSData *)data error:(NSError **)errorPtr;
183 184 185 186 187 188 189 190 191

/// Initializes an instance by parsing the data. This method should be sent to
/// the generated message class that the data should be interpreted as. If
/// there is an error the method returns nil and the error is returned in
/// errorPtr (when provided).
///
/// @note In DEBUG builds, the parsed message is checked to be sure all required
///       fields were provided, and the parse will fail if some are missing.
///
192 193 194
/// @note The errors returned are likely coming from the domain and codes listed
///       at the top of this file and GPBCodedInputStream.h.
///
195 196 197 198
/// @param data              The data to parse.
/// @param extensionRegistry The extension registry to use to look up extensions.
/// @param errorPtr          An optional error pointer to fill in with a failure
///                          reason if the data can not be parsed.
199
- (instancetype)initWithData:(NSData *)data
200
           extensionRegistry:(nullable GPBExtensionRegistry *)extensionRegistry
201
                       error:(NSError **)errorPtr;
202 203 204 205 206 207 208 209 210 211

/// Initializes an instance by parsing the data from the given input stream. This
/// method should be sent to the generated message class that the data should
/// be interpreted as. If there is an error the method returns nil and the error
/// is returned in errorPtr (when provided).
///
/// @note Unlike the parseFrom... methods, this never checks to see if all of
///       the required fields are set. So this method can be used to reload
///       messages that may not be complete.
///
212 213 214
/// @note The errors returned are likely coming from the domain and codes listed
///       at the top of this file and GPBCodedInputStream.h.
///
215 216 217 218
/// @param input             The stream to read data from.
/// @param extensionRegistry The extension registry to use to look up extensions.
/// @param errorPtr          An optional error pointer to fill in with a failure
///                          reason if the data can not be parsed.
219 220
- (instancetype)initWithCodedInputStream:(GPBCodedInputStream *)input
                       extensionRegistry:
221
                           (nullable GPBExtensionRegistry *)extensionRegistry
222
                                   error:(NSError **)errorPtr;
223

224
/// Writes out the message to the given output stream.
225
- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)output;
226
/// Writes out the message to the given output stream.
227 228
- (void)writeToOutputStream:(NSOutputStream *)output;

229 230
/// Writes out a varint for the message size followed by the the message to
/// the given output stream.
231
- (void)writeDelimitedToCodedOutputStream:(GPBCodedOutputStream *)output;
232 233
/// Writes out a varint for the message size followed by the the message to
/// the given output stream.
234 235
- (void)writeDelimitedToOutputStream:(NSOutputStream *)output;

236 237 238 239 240 241 242 243 244
/// Serializes the message to a @c NSData.
///
/// If there is an error while generating the data, nil is returned.
///
/// @note This value is not cached, so if you are using it repeatedly, cache
///       it yourself.
///
/// @note In DEBUG ONLY, the message is also checked for all required field,
///       if one is missing, nil will be returned.
245
- (nullable NSData *)data;
246

247 248 249 250 251
/// Serializes a varint with the message size followed by the message data,
/// returning that as a @c NSData.
///
/// @note This value is not cached, so if you are using it repeatedly, cache
///       it yourself.
252
- (NSData *)delimitedData;
253

254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
/// Calculates the size of the object if it were serialized.
///
/// This is not a cached value. If you are following a pattern like this:
/// @code
///   size_t size = [aMsg serializedSize];
///   NSMutableData *foo = [NSMutableData dataWithCapacity:size + sizeof(size)];
///   [foo writeSize:size];
///   [foo appendData:[aMsg data]];
/// @endcode
/// you would be better doing:
/// @code
///   NSData *data = [aMsg data];
///   NSUInteger size = [aMsg length];
///   NSMutableData *foo = [NSMutableData dataWithCapacity:size + sizeof(size)];
///   [foo writeSize:size];
///   [foo appendData:data];
/// @endcode
271 272
- (size_t)serializedSize;

273
/// Return the descriptor for the message class.
274
+ (GPBDescriptor *)descriptor;
275
/// Return the descriptor for the message.
276 277
- (GPBDescriptor *)descriptor;

278
/// Test to see if the given extension is set on the message.
279
- (BOOL)hasExtension:(GPBExtensionDescriptor *)extension;
280 281 282 283 284 285

/// Fetches the given extension's value for this message.
///
/// Extensions use boxed values (NSNumbers) for PODs and NSMutableArrays for
/// repeated fields. If the extension is a Message one will be auto created for you
/// and returned similar to fields.
286
- (nullable id)getExtension:(GPBExtensionDescriptor *)extension;
287 288 289 290 291

/// Sets the given extension's value for this message. This is only for single
/// field extensions (i.e. - not repeated fields).
///
/// Extensions use boxed values (@c NSNumbers).
292
- (void)setExtension:(GPBExtensionDescriptor *)extension value:(nullable id)value;
293 294 295 296

/// Adds the given value to the extension for this message. This is only for
/// repeated field extensions. If the field is a repeated POD type the @c value
/// is a @c NSNumber.
297
- (void)addExtension:(GPBExtensionDescriptor *)extension value:(id)value;
298 299 300 301

/// Replaces the given value at an index for the extension on this message. This
/// is only for repeated field extensions. If the field is a repeated POD type
/// the @c value is a @c NSNumber.
302
- (void)setExtension:(GPBExtensionDescriptor *)extension
303 304
               index:(NSUInteger)index
               value:(id)value;
305 306

/// Clears the given extension for this message.
307
- (void)clearExtension:(GPBExtensionDescriptor *)extension;
308

309
/// Resets all of the fields of this message to their default values.
310 311
- (void)clear;

312 313 314 315
/// Parses a message of this type from the input and merges it with this
/// message.
///
/// @note This will throw if there is an error parsing the data.
316
- (void)mergeFromData:(NSData *)data
317
    extensionRegistry:(nullable GPBExtensionRegistry *)extensionRegistry;
318

319 320
/// Merges the fields from another message (of the same type) into this
/// message.
321 322 323
- (void)mergeFrom:(GPBMessage *)other;

@end
324 325

NS_ASSUME_NONNULL_END