OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 26 matching lines...) Expand all Loading... |
37 @class GPBCodedOutputStream; | 37 @class GPBCodedOutputStream; |
38 @class GPBExtensionDescriptor; | 38 @class GPBExtensionDescriptor; |
39 @class GPBExtensionRegistry; | 39 @class GPBExtensionRegistry; |
40 @class GPBFieldDescriptor; | 40 @class GPBFieldDescriptor; |
41 @class GPBUnknownFieldSet; | 41 @class GPBUnknownFieldSet; |
42 | 42 |
43 NS_ASSUME_NONNULL_BEGIN | 43 NS_ASSUME_NONNULL_BEGIN |
44 | 44 |
45 CF_EXTERN_C_BEGIN | 45 CF_EXTERN_C_BEGIN |
46 | 46 |
47 // NSError domain used for errors. | 47 /// NSError domain used for errors. |
48 extern NSString *const GPBMessageErrorDomain; | 48 extern NSString *const GPBMessageErrorDomain; |
49 | 49 |
| 50 /// Error code for NSError with GPBMessageErrorDomain. |
50 typedef NS_ENUM(NSInteger, GPBMessageErrorCode) { | 51 typedef NS_ENUM(NSInteger, GPBMessageErrorCode) { |
| 52 /// The data being parsed is bad and a message can not be created from it. |
51 GPBMessageErrorCodeMalformedData = -100, | 53 GPBMessageErrorCodeMalformedData = -100, |
| 54 /// A message can't be serialized because it is missing required fields. |
52 GPBMessageErrorCodeMissingRequiredField = -101, | 55 GPBMessageErrorCodeMissingRequiredField = -101, |
53 }; | 56 }; |
54 | 57 |
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 | 58 #ifdef DEBUG |
| 59 /// 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. |
59 extern NSString *const GPBExceptionMessageKey; | 62 extern NSString *const GPBExceptionMessageKey; |
60 #endif // DEBUG | 63 #endif // DEBUG |
61 | 64 |
62 CF_EXTERN_C_END | 65 CF_EXTERN_C_END |
63 | 66 |
| 67 /// Base class for all of the generated message classes. |
64 @interface GPBMessage : NSObject<NSSecureCoding, NSCopying> | 68 @interface GPBMessage : NSObject<NSSecureCoding, NSCopying> |
65 | 69 |
66 // NOTE: If you add a instance method/property to this class that may conflict | 70 // 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. | 71 // 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 | 72 // The main cases are methods that take no arguments, or setFoo:/hasFoo: type |
69 // methods. | 73 // methods. |
70 | 74 |
| 75 /// 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. |
71 @property(nonatomic, copy, nullable) GPBUnknownFieldSet *unknownFields; | 80 @property(nonatomic, copy, nullable) GPBUnknownFieldSet *unknownFields; |
72 | 81 |
73 // Are all required fields in the message and all embedded messages set. | 82 /// Are all required fields set in the message and all embedded messages. |
74 @property(nonatomic, readonly, getter=isInitialized) BOOL initialized; | 83 @property(nonatomic, readonly, getter=isInitialized) BOOL initialized; |
75 | 84 |
76 // Returns an autoreleased instance. | 85 /// Returns an autoreleased instance. |
77 + (instancetype)message; | 86 + (instancetype)message; |
78 | 87 |
79 // Create a message based on a variety of inputs. If there is a data parse | 88 /// Creates a new instance by parsing the data. This method should be sent to |
80 // error, nil is returned and if not NULL, errorPtr is filled in. | 89 /// the generated message class that the data should be interpreted as. If |
81 // NOTE: In DEBUG ONLY, the message is also checked for all required field, | 90 /// there is an error the method returns nil and the error is returned in |
82 // if one is missing, the parse will fail (returning nil, filling in errorPtr). | 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 i
f |
| 98 /// the data can not be parsed. |
| 99 /// |
| 100 /// @return A new instance of the class messaged. |
83 + (instancetype)parseFromData:(NSData *)data error:(NSError **)errorPtr; | 101 + (instancetype)parseFromData:(NSData *)data error:(NSError **)errorPtr; |
| 102 |
| 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. |
84 + (instancetype)parseFromData:(NSData *)data | 117 + (instancetype)parseFromData:(NSData *)data |
85 extensionRegistry:(nullable GPBExtensionRegistry *)extensionRegistry | 118 extensionRegistry:(nullable GPBExtensionRegistry *)extensionRegistry |
86 error:(NSError **)errorPtr; | 119 error:(NSError **)errorPtr; |
| 120 |
| 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. |
87 + (instancetype)parseFromCodedInputStream:(GPBCodedInputStream *)input | 135 + (instancetype)parseFromCodedInputStream:(GPBCodedInputStream *)input |
88 extensionRegistry: | 136 extensionRegistry: |
89 (nullable GPBExtensionRegistry *)extensionRegistry | 137 (nullable GPBExtensionRegistry *)extensionRegistry |
90 error:(NSError **)errorPtr; | 138 error:(NSError **)errorPtr; |
91 | 139 |
92 // Create a message based on delimited input. If there is a data parse | 140 /// Creates a new instance by parsing the data from the given input stream. This |
93 // error, nil is returned and if not NULL, errorPtr is filled in. | 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. |
94 + (instancetype)parseDelimitedFromCodedInputStream:(GPBCodedInputStream *)input | 155 + (instancetype)parseDelimitedFromCodedInputStream:(GPBCodedInputStream *)input |
95 extensionRegistry: | 156 extensionRegistry: |
96 (nullable GPBExtensionRegistry *)extensionR
egistry | 157 (nullable GPBExtensionRegistry *)extensionR
egistry |
97 error:(NSError **)errorPtr; | 158 error:(NSError **)errorPtr; |
98 | 159 |
99 // If there is a data parse error, nil is returned and if not NULL, errorPtr is | 160 /// Initializes an instance by parsing the data. This method should be sent to |
100 // filled in. | 161 /// the generated message class that the data should be interpreted as. If |
101 // NOTE: In DEBUG ONLY, the message is also checked for all required field, | 162 /// there is an error the method returns nil and the error is returned in |
102 // if one is missing, the parse will fail (returning nil, filling in errorPtr). | 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 i
f |
| 170 /// the data can not be parsed. |
103 - (instancetype)initWithData:(NSData *)data error:(NSError **)errorPtr; | 171 - (instancetype)initWithData:(NSData *)data error:(NSError **)errorPtr; |
| 172 |
| 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. |
104 - (instancetype)initWithData:(NSData *)data | 185 - (instancetype)initWithData:(NSData *)data |
105 extensionRegistry:(nullable GPBExtensionRegistry *)extensionRegistry | 186 extensionRegistry:(nullable GPBExtensionRegistry *)extensionRegistry |
106 error:(NSError **)errorPtr; | 187 error:(NSError **)errorPtr; |
| 188 |
| 189 /// Initializes an instance by parsing the data from the given input stream. Thi
s |
| 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. |
107 - (instancetype)initWithCodedInputStream:(GPBCodedInputStream *)input | 202 - (instancetype)initWithCodedInputStream:(GPBCodedInputStream *)input |
108 extensionRegistry: | 203 extensionRegistry: |
109 (nullable GPBExtensionRegistry *)extensionRegistry | 204 (nullable GPBExtensionRegistry *)extensionRegistry |
110 error:(NSError **)errorPtr; | 205 error:(NSError **)errorPtr; |
111 | 206 |
112 // Serializes the message and writes it to output. | 207 /// Writes out the message to the given output stream. |
113 - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)output; | 208 - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)output; |
| 209 /// Writes out the message to the given output stream. |
114 - (void)writeToOutputStream:(NSOutputStream *)output; | 210 - (void)writeToOutputStream:(NSOutputStream *)output; |
115 | 211 |
116 // Serializes the message and writes it to output, but writes the size of the | 212 /// Writes out a varint for the message size followed by the the message to |
117 // message as a variant before writing the message. | 213 /// the given output stream. |
118 - (void)writeDelimitedToCodedOutputStream:(GPBCodedOutputStream *)output; | 214 - (void)writeDelimitedToCodedOutputStream:(GPBCodedOutputStream *)output; |
| 215 /// Writes out a varint for the message size followed by the the message to |
| 216 /// the given output stream. |
119 - (void)writeDelimitedToOutputStream:(NSOutputStream *)output; | 217 - (void)writeDelimitedToOutputStream:(NSOutputStream *)output; |
120 | 218 |
121 // Serializes the message to an NSData. Note that this value is not cached, so | 219 /// Serializes the message to a @c NSData. |
122 // if you are using it repeatedly, cache it yourself. If there is an error | 220 /// |
123 // while generating the data, nil is returned. | 221 /// If there is an error while generating the data, nil is returned. |
124 // NOTE: In DEBUG ONLY, the message is also checked for all required field, | 222 /// |
125 // if one is missing, nil will be returned. | 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. |
126 - (nullable NSData *)data; | 228 - (nullable NSData *)data; |
127 | 229 |
128 // Same as -[data], except a delimiter is added to the start of the data | 230 /// Serializes a varint with the message size followed by the message data, |
129 // indicating the size of the message data that follows. | 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. |
130 - (NSData *)delimitedData; | 235 - (NSData *)delimitedData; |
131 | 236 |
132 // Returns the size of the object if it were serialized. | 237 /// Calculates the size of the object if it were serialized. |
133 // This is not a cached value. If you are following a pattern like this: | 238 /// |
134 // size_t size = [aMsg serializedSize]; | 239 /// This is not a cached value. If you are following a pattern like this: |
135 // NSMutableData *foo = [NSMutableData dataWithCapacity:size + sizeof(size)]; | 240 /// @code |
136 // [foo writeSize:size]; | 241 /// size_t size = [aMsg serializedSize]; |
137 // [foo appendData:[aMsg data]]; | 242 /// NSMutableData *foo = [NSMutableData dataWithCapacity:size + sizeof(size)]; |
138 // you would be better doing: | 243 /// [foo writeSize:size]; |
139 // NSData *data = [aMsg data]; | 244 /// [foo appendData:[aMsg data]]; |
140 // NSUInteger size = [aMsg length]; | 245 /// @endcode |
141 // NSMutableData *foo = [NSMutableData dataWithCapacity:size + sizeof(size)]; | 246 /// you would be better doing: |
142 // [foo writeSize:size]; | 247 /// @code |
143 // [foo appendData:data]; | 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 |
144 - (size_t)serializedSize; | 254 - (size_t)serializedSize; |
145 | 255 |
146 // Return the descriptor for the message | 256 /// Return the descriptor for the message class. |
147 + (GPBDescriptor *)descriptor; | 257 + (GPBDescriptor *)descriptor; |
| 258 /// Return the descriptor for the message. |
148 - (GPBDescriptor *)descriptor; | 259 - (GPBDescriptor *)descriptor; |
149 | 260 |
150 // Extensions use boxed values (NSNumbers) for PODs, NSMutableArrays for | 261 /// Test to see if the given extension is set on the message. |
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; | 262 - (BOOL)hasExtension:(GPBExtensionDescriptor *)extension; |
| 263 |
| 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. |
154 - (nullable id)getExtension:(GPBExtensionDescriptor *)extension; | 269 - (nullable id)getExtension:(GPBExtensionDescriptor *)extension; |
| 270 |
| 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). |
155 - (void)setExtension:(GPBExtensionDescriptor *)extension value:(nullable id)valu
e; | 275 - (void)setExtension:(GPBExtensionDescriptor *)extension value:(nullable id)valu
e; |
| 276 |
| 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. |
156 - (void)addExtension:(GPBExtensionDescriptor *)extension value:(id)value; | 280 - (void)addExtension:(GPBExtensionDescriptor *)extension value:(id)value; |
| 281 |
| 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. |
157 - (void)setExtension:(GPBExtensionDescriptor *)extension | 285 - (void)setExtension:(GPBExtensionDescriptor *)extension |
158 index:(NSUInteger)index | 286 index:(NSUInteger)index |
159 value:(id)value; | 287 value:(id)value; |
| 288 |
| 289 /// Clears the given extension for this message. |
160 - (void)clearExtension:(GPBExtensionDescriptor *)extension; | 290 - (void)clearExtension:(GPBExtensionDescriptor *)extension; |
161 | 291 |
162 // Resets all fields to their default values. | 292 /// Resets all of the fields of this message to their default values. |
163 - (void)clear; | 293 - (void)clear; |
164 | 294 |
165 // Parses a message of this type from the input and merges it with this | 295 /// Parses a message of this type from the input and merges it with this |
166 // message. | 296 /// message. |
167 // NOTE: This will throw if there is an error parsing the data. | 297 /// |
| 298 /// @note This will throw if there is an error parsing the data. |
168 - (void)mergeFromData:(NSData *)data | 299 - (void)mergeFromData:(NSData *)data |
169 extensionRegistry:(nullable GPBExtensionRegistry *)extensionRegistry; | 300 extensionRegistry:(nullable GPBExtensionRegistry *)extensionRegistry; |
170 | 301 |
171 // Merges the fields from another message (of the same type) into this | 302 /// Merges the fields from another message (of the same type) into this |
172 // message. | 303 /// message. |
173 - (void)mergeFrom:(GPBMessage *)other; | 304 - (void)mergeFrom:(GPBMessage *)other; |
174 | 305 |
175 @end | 306 @end |
176 | 307 |
177 NS_ASSUME_NONNULL_END | 308 NS_ASSUME_NONNULL_END |
OLD | NEW |