blob: 332393edc930b2deeb4eedbe7529d5d274f14bb4 [file] [log] [blame]
Brian Silverman9c614bc2016-02-15 20:20:02 -05001// 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
31#import <Foundation/Foundation.h>
32
33#import "GPBBootstrap.h"
34
35@class GPBDescriptor;
36@class GPBCodedInputStream;
37@class GPBCodedOutputStream;
38@class GPBExtensionDescriptor;
39@class GPBExtensionRegistry;
40@class GPBFieldDescriptor;
41@class GPBUnknownFieldSet;
42
43NS_ASSUME_NONNULL_BEGIN
44
45CF_EXTERN_C_BEGIN
46
47// NSError domain used for errors.
48extern NSString *const GPBMessageErrorDomain;
49
50typedef NS_ENUM(NSInteger, GPBMessageErrorCode) {
51 GPBMessageErrorCodeMalformedData = -100,
52 GPBMessageErrorCodeMissingRequiredField = -101,
53};
54
55// In DEBUG ONLY, an NSException is thrown when a parsed message doesn't
56// contain required fields. This key allows you to retrieve the parsed message
57// from the exception's |userInfo| dictionary.
58#ifdef DEBUG
59extern NSString *const GPBExceptionMessageKey;
60#endif // DEBUG
61
62CF_EXTERN_C_END
63
64@interface GPBMessage : NSObject<NSSecureCoding, NSCopying>
65
66// NOTE: If you add a instance method/property to this class that may conflict
67// with methods declared in protos, you need to update objective_helpers.cc.
68// The main cases are methods that take no arguments, or setFoo:/hasFoo: type
69// methods.
70
71@property(nonatomic, copy, nullable) GPBUnknownFieldSet *unknownFields;
72
73// Are all required fields in the message and all embedded messages set.
74@property(nonatomic, readonly, getter=isInitialized) BOOL initialized;
75
76// Returns an autoreleased instance.
77+ (instancetype)message;
78
79// Create a message based on a variety of inputs. If there is a data parse
80// error, nil is returned and if not NULL, errorPtr is filled in.
81// NOTE: In DEBUG ONLY, the message is also checked for all required field,
82// if one is missing, the parse will fail (returning nil, filling in errorPtr).
83+ (instancetype)parseFromData:(NSData *)data error:(NSError **)errorPtr;
84+ (instancetype)parseFromData:(NSData *)data
85 extensionRegistry:(nullable GPBExtensionRegistry *)extensionRegistry
86 error:(NSError **)errorPtr;
87+ (instancetype)parseFromCodedInputStream:(GPBCodedInputStream *)input
88 extensionRegistry:
89 (nullable GPBExtensionRegistry *)extensionRegistry
90 error:(NSError **)errorPtr;
91
92// Create a message based on delimited input. If there is a data parse
93// error, nil is returned and if not NULL, errorPtr is filled in.
94+ (instancetype)parseDelimitedFromCodedInputStream:(GPBCodedInputStream *)input
95 extensionRegistry:
96 (nullable GPBExtensionRegistry *)extensionRegistry
97 error:(NSError **)errorPtr;
98
99// If there is a data parse error, nil is returned and if not NULL, errorPtr is
100// filled in.
101// NOTE: In DEBUG ONLY, the message is also checked for all required field,
102// if one is missing, the parse will fail (returning nil, filling in errorPtr).
103- (instancetype)initWithData:(NSData *)data error:(NSError **)errorPtr;
104- (instancetype)initWithData:(NSData *)data
105 extensionRegistry:(nullable GPBExtensionRegistry *)extensionRegistry
106 error:(NSError **)errorPtr;
107- (instancetype)initWithCodedInputStream:(GPBCodedInputStream *)input
108 extensionRegistry:
109 (nullable GPBExtensionRegistry *)extensionRegistry
110 error:(NSError **)errorPtr;
111
112// Serializes the message and writes it to output.
113- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)output;
114- (void)writeToOutputStream:(NSOutputStream *)output;
115
116// Serializes the message and writes it to output, but writes the size of the
117// message as a variant before writing the message.
118- (void)writeDelimitedToCodedOutputStream:(GPBCodedOutputStream *)output;
119- (void)writeDelimitedToOutputStream:(NSOutputStream *)output;
120
121// Serializes the message to an NSData. Note that this value is not cached, so
122// if you are using it repeatedly, cache it yourself. If there is an error
123// while generating the data, nil is returned.
124// NOTE: In DEBUG ONLY, the message is also checked for all required field,
125// if one is missing, nil will be returned.
126- (nullable NSData *)data;
127
128// Same as -[data], except a delimiter is added to the start of the data
129// indicating the size of the message data that follows.
130- (NSData *)delimitedData;
131
132// Returns the size of the object if it were serialized.
133// This is not a cached value. If you are following a pattern like this:
134// size_t size = [aMsg serializedSize];
135// NSMutableData *foo = [NSMutableData dataWithCapacity:size + sizeof(size)];
136// [foo writeSize:size];
137// [foo appendData:[aMsg data]];
138// you would be better doing:
139// NSData *data = [aMsg data];
140// NSUInteger size = [aMsg length];
141// NSMutableData *foo = [NSMutableData dataWithCapacity:size + sizeof(size)];
142// [foo writeSize:size];
143// [foo appendData:data];
144- (size_t)serializedSize;
145
146// Return the descriptor for the message
147+ (GPBDescriptor *)descriptor;
148- (GPBDescriptor *)descriptor;
149
150// Extensions use boxed values (NSNumbers) for PODs, NSMutableArrays for
151// repeated. If the extension is a Message one will be auto created for you
152// and returned similar to fields.
153- (BOOL)hasExtension:(GPBExtensionDescriptor *)extension;
154- (nullable id)getExtension:(GPBExtensionDescriptor *)extension;
155- (void)setExtension:(GPBExtensionDescriptor *)extension value:(nullable id)value;
156- (void)addExtension:(GPBExtensionDescriptor *)extension value:(id)value;
157- (void)setExtension:(GPBExtensionDescriptor *)extension
158 index:(NSUInteger)index
159 value:(id)value;
160- (void)clearExtension:(GPBExtensionDescriptor *)extension;
161
162// Resets all fields to their default values.
163- (void)clear;
164
165// Parses a message of this type from the input and merges it with this
166// message.
167// NOTE: This will throw if there is an error parsing the data.
168- (void)mergeFromData:(NSData *)data
169 extensionRegistry:(nullable GPBExtensionRegistry *)extensionRegistry;
170
171// Merges the fields from another message (of the same type) into this
172// message.
173- (void)mergeFrom:(GPBMessage *)other;
174
175@end
176
177NS_ASSUME_NONNULL_END