blob: a5aef1706aac89c962b1e5e7a942273aba3de9e3 [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 "GPBRuntimeTypes.h"
34#import "GPBWireFormat.h"
35
36@class GPBBoolArray;
37@class GPBDoubleArray;
38@class GPBEnumArray;
39@class GPBFloatArray;
40@class GPBMessage;
41@class GPBInt32Array;
42@class GPBInt64Array;
43@class GPBUInt32Array;
44@class GPBUInt64Array;
45@class GPBUnknownFieldSet;
46
47NS_ASSUME_NONNULL_BEGIN
48
49@interface GPBCodedOutputStream : NSObject
50
51// Creates a new stream to write into data. Data must be sized to fit or it
52// will error when it runs out of space.
53+ (instancetype)streamWithData:(NSMutableData *)data;
54+ (instancetype)streamWithOutputStream:(NSOutputStream *)output;
55+ (instancetype)streamWithOutputStream:(NSOutputStream *)output
56 bufferSize:(size_t)bufferSize;
57
58- (instancetype)initWithData:(NSMutableData *)data;
59- (instancetype)initWithOutputStream:(NSOutputStream *)output;
60- (instancetype)initWithOutputStream:(NSOutputStream *)output
61 bufferSize:(size_t)bufferSize;
62
63- (void)flush;
64
65- (void)writeRawByte:(uint8_t)value;
66
67- (void)writeTag:(uint32_t)fieldNumber format:(GPBWireFormat)format;
68
69- (void)writeRawLittleEndian32:(int32_t)value;
70- (void)writeRawLittleEndian64:(int64_t)value;
71
72- (void)writeRawVarint32:(int32_t)value;
73- (void)writeRawVarint64:(int64_t)value;
74
75// Note that this will truncate 64 bit values to 32.
76- (void)writeRawVarintSizeTAs32:(size_t)value;
77
78- (void)writeRawData:(NSData *)data;
79- (void)writeRawPtr:(const void *)data
80 offset:(size_t)offset
81 length:(size_t)length;
82
83//%PDDM-EXPAND _WRITE_DECLS()
84// This block of code is generated, do not edit it directly.
85
86- (void)writeDouble:(int32_t)fieldNumber value:(double)value;
87- (void)writeDoubleArray:(int32_t)fieldNumber
88 values:(GPBDoubleArray *)values
89 tag:(uint32_t)tag;
90- (void)writeDoubleNoTag:(double)value;
91
92- (void)writeFloat:(int32_t)fieldNumber value:(float)value;
93- (void)writeFloatArray:(int32_t)fieldNumber
94 values:(GPBFloatArray *)values
95 tag:(uint32_t)tag;
96- (void)writeFloatNoTag:(float)value;
97
98- (void)writeUInt64:(int32_t)fieldNumber value:(uint64_t)value;
99- (void)writeUInt64Array:(int32_t)fieldNumber
100 values:(GPBUInt64Array *)values
101 tag:(uint32_t)tag;
102- (void)writeUInt64NoTag:(uint64_t)value;
103
104- (void)writeInt64:(int32_t)fieldNumber value:(int64_t)value;
105- (void)writeInt64Array:(int32_t)fieldNumber
106 values:(GPBInt64Array *)values
107 tag:(uint32_t)tag;
108- (void)writeInt64NoTag:(int64_t)value;
109
110- (void)writeInt32:(int32_t)fieldNumber value:(int32_t)value;
111- (void)writeInt32Array:(int32_t)fieldNumber
112 values:(GPBInt32Array *)values
113 tag:(uint32_t)tag;
114- (void)writeInt32NoTag:(int32_t)value;
115
116- (void)writeUInt32:(int32_t)fieldNumber value:(uint32_t)value;
117- (void)writeUInt32Array:(int32_t)fieldNumber
118 values:(GPBUInt32Array *)values
119 tag:(uint32_t)tag;
120- (void)writeUInt32NoTag:(uint32_t)value;
121
122- (void)writeFixed64:(int32_t)fieldNumber value:(uint64_t)value;
123- (void)writeFixed64Array:(int32_t)fieldNumber
124 values:(GPBUInt64Array *)values
125 tag:(uint32_t)tag;
126- (void)writeFixed64NoTag:(uint64_t)value;
127
128- (void)writeFixed32:(int32_t)fieldNumber value:(uint32_t)value;
129- (void)writeFixed32Array:(int32_t)fieldNumber
130 values:(GPBUInt32Array *)values
131 tag:(uint32_t)tag;
132- (void)writeFixed32NoTag:(uint32_t)value;
133
134- (void)writeSInt32:(int32_t)fieldNumber value:(int32_t)value;
135- (void)writeSInt32Array:(int32_t)fieldNumber
136 values:(GPBInt32Array *)values
137 tag:(uint32_t)tag;
138- (void)writeSInt32NoTag:(int32_t)value;
139
140- (void)writeSInt64:(int32_t)fieldNumber value:(int64_t)value;
141- (void)writeSInt64Array:(int32_t)fieldNumber
142 values:(GPBInt64Array *)values
143 tag:(uint32_t)tag;
144- (void)writeSInt64NoTag:(int64_t)value;
145
146- (void)writeSFixed64:(int32_t)fieldNumber value:(int64_t)value;
147- (void)writeSFixed64Array:(int32_t)fieldNumber
148 values:(GPBInt64Array *)values
149 tag:(uint32_t)tag;
150- (void)writeSFixed64NoTag:(int64_t)value;
151
152- (void)writeSFixed32:(int32_t)fieldNumber value:(int32_t)value;
153- (void)writeSFixed32Array:(int32_t)fieldNumber
154 values:(GPBInt32Array *)values
155 tag:(uint32_t)tag;
156- (void)writeSFixed32NoTag:(int32_t)value;
157
158- (void)writeBool:(int32_t)fieldNumber value:(BOOL)value;
159- (void)writeBoolArray:(int32_t)fieldNumber
160 values:(GPBBoolArray *)values
161 tag:(uint32_t)tag;
162- (void)writeBoolNoTag:(BOOL)value;
163
164- (void)writeEnum:(int32_t)fieldNumber value:(int32_t)value;
165- (void)writeEnumArray:(int32_t)fieldNumber
166 values:(GPBEnumArray *)values
167 tag:(uint32_t)tag;
168- (void)writeEnumNoTag:(int32_t)value;
169
170- (void)writeString:(int32_t)fieldNumber value:(NSString *)value;
171- (void)writeStringArray:(int32_t)fieldNumber values:(NSArray *)values;
172- (void)writeStringNoTag:(NSString *)value;
173
174- (void)writeMessage:(int32_t)fieldNumber value:(GPBMessage *)value;
175- (void)writeMessageArray:(int32_t)fieldNumber values:(NSArray *)values;
176- (void)writeMessageNoTag:(GPBMessage *)value;
177
178- (void)writeBytes:(int32_t)fieldNumber value:(NSData *)value;
179- (void)writeBytesArray:(int32_t)fieldNumber values:(NSArray *)values;
180- (void)writeBytesNoTag:(NSData *)value;
181
182- (void)writeGroup:(int32_t)fieldNumber
183 value:(GPBMessage *)value;
184- (void)writeGroupArray:(int32_t)fieldNumber values:(NSArray *)values;
185- (void)writeGroupNoTag:(int32_t)fieldNumber
186 value:(GPBMessage *)value;
187
188- (void)writeUnknownGroup:(int32_t)fieldNumber
189 value:(GPBUnknownFieldSet *)value;
190- (void)writeUnknownGroupArray:(int32_t)fieldNumber values:(NSArray *)values;
191- (void)writeUnknownGroupNoTag:(int32_t)fieldNumber
192 value:(GPBUnknownFieldSet *)value;
193
194//%PDDM-EXPAND-END _WRITE_DECLS()
195
196// Write a MessageSet extension field to the stream. For historical reasons,
197// the wire format differs from normal fields.
198- (void)writeMessageSetExtension:(int32_t)fieldNumber value:(GPBMessage *)value;
199
200// Write an unparsed MessageSet extension field to the stream. For
201// historical reasons, the wire format differs from normal fields.
202- (void)writeRawMessageSetExtension:(int32_t)fieldNumber value:(NSData *)value;
203
204@end
205
206CF_EXTERN_C_BEGIN
207
208size_t GPBComputeDoubleSize(int32_t fieldNumber, double value)
209 __attribute__((const));
210size_t GPBComputeFloatSize(int32_t fieldNumber, float value)
211 __attribute__((const));
212size_t GPBComputeUInt64Size(int32_t fieldNumber, uint64_t value)
213 __attribute__((const));
214size_t GPBComputeInt64Size(int32_t fieldNumber, int64_t value)
215 __attribute__((const));
216size_t GPBComputeInt32Size(int32_t fieldNumber, int32_t value)
217 __attribute__((const));
218size_t GPBComputeFixed64Size(int32_t fieldNumber, uint64_t value)
219 __attribute__((const));
220size_t GPBComputeFixed32Size(int32_t fieldNumber, uint32_t value)
221 __attribute__((const));
222size_t GPBComputeBoolSize(int32_t fieldNumber, BOOL value)
223 __attribute__((const));
224size_t GPBComputeStringSize(int32_t fieldNumber, NSString *value)
225 __attribute__((const));
226size_t GPBComputeGroupSize(int32_t fieldNumber, GPBMessage *value)
227 __attribute__((const));
228size_t GPBComputeUnknownGroupSize(int32_t fieldNumber,
229 GPBUnknownFieldSet *value)
230 __attribute__((const));
231size_t GPBComputeMessageSize(int32_t fieldNumber, GPBMessage *value)
232 __attribute__((const));
233size_t GPBComputeBytesSize(int32_t fieldNumber, NSData *value)
234 __attribute__((const));
235size_t GPBComputeUInt32Size(int32_t fieldNumber, uint32_t value)
236 __attribute__((const));
237size_t GPBComputeSFixed32Size(int32_t fieldNumber, int32_t value)
238 __attribute__((const));
239size_t GPBComputeSFixed64Size(int32_t fieldNumber, int64_t value)
240 __attribute__((const));
241size_t GPBComputeSInt32Size(int32_t fieldNumber, int32_t value)
242 __attribute__((const));
243size_t GPBComputeSInt64Size(int32_t fieldNumber, int64_t value)
244 __attribute__((const));
245size_t GPBComputeTagSize(int32_t fieldNumber) __attribute__((const));
246size_t GPBComputeWireFormatTagSize(int field_number, GPBDataType dataType)
247 __attribute__((const));
248
249size_t GPBComputeDoubleSizeNoTag(double value) __attribute__((const));
250size_t GPBComputeFloatSizeNoTag(float value) __attribute__((const));
251size_t GPBComputeUInt64SizeNoTag(uint64_t value) __attribute__((const));
252size_t GPBComputeInt64SizeNoTag(int64_t value) __attribute__((const));
253size_t GPBComputeInt32SizeNoTag(int32_t value) __attribute__((const));
254size_t GPBComputeFixed64SizeNoTag(uint64_t value) __attribute__((const));
255size_t GPBComputeFixed32SizeNoTag(uint32_t value) __attribute__((const));
256size_t GPBComputeBoolSizeNoTag(BOOL value) __attribute__((const));
257size_t GPBComputeStringSizeNoTag(NSString *value) __attribute__((const));
258size_t GPBComputeGroupSizeNoTag(GPBMessage *value) __attribute__((const));
259size_t GPBComputeUnknownGroupSizeNoTag(GPBUnknownFieldSet *value)
260 __attribute__((const));
261size_t GPBComputeMessageSizeNoTag(GPBMessage *value) __attribute__((const));
262size_t GPBComputeBytesSizeNoTag(NSData *value) __attribute__((const));
263size_t GPBComputeUInt32SizeNoTag(int32_t value) __attribute__((const));
264size_t GPBComputeEnumSizeNoTag(int32_t value) __attribute__((const));
265size_t GPBComputeSFixed32SizeNoTag(int32_t value) __attribute__((const));
266size_t GPBComputeSFixed64SizeNoTag(int64_t value) __attribute__((const));
267size_t GPBComputeSInt32SizeNoTag(int32_t value) __attribute__((const));
268size_t GPBComputeSInt64SizeNoTag(int64_t value) __attribute__((const));
269
270// Note that this will calculate the size of 64 bit values truncated to 32.
271size_t GPBComputeSizeTSizeAsInt32NoTag(size_t value) __attribute__((const));
272
273size_t GPBComputeRawVarint32Size(int32_t value) __attribute__((const));
274size_t GPBComputeRawVarint64Size(int64_t value) __attribute__((const));
275
276// Note that this will calculate the size of 64 bit values truncated to 32.
277size_t GPBComputeRawVarint32SizeForInteger(NSInteger value)
278 __attribute__((const));
279
280// Compute the number of bytes that would be needed to encode a
281// MessageSet extension to the stream. For historical reasons,
282// the wire format differs from normal fields.
283size_t GPBComputeMessageSetExtensionSize(int32_t fieldNumber, GPBMessage *value)
284 __attribute__((const));
285
286// Compute the number of bytes that would be needed to encode an
287// unparsed MessageSet extension field to the stream. For
288// historical reasons, the wire format differs from normal fields.
289size_t GPBComputeRawMessageSetExtensionSize(int32_t fieldNumber, NSData *value)
290 __attribute__((const));
291
292size_t GPBComputeEnumSize(int32_t fieldNumber, int32_t value)
293 __attribute__((const));
294
295CF_EXTERN_C_END
296
297NS_ASSUME_NONNULL_END
298
299// Write methods for types that can be in packed arrays.
300//%PDDM-DEFINE _WRITE_PACKABLE_DECLS(NAME, ARRAY_TYPE, TYPE)
301//%- (void)write##NAME:(int32_t)fieldNumber value:(TYPE)value;
302//%- (void)write##NAME##Array:(int32_t)fieldNumber
303//% NAME$S values:(GPB##ARRAY_TYPE##Array *)values
304//% NAME$S tag:(uint32_t)tag;
305//%- (void)write##NAME##NoTag:(TYPE)value;
306//%
307// Write methods for types that aren't in packed arrays.
308//%PDDM-DEFINE _WRITE_UNPACKABLE_DECLS(NAME, TYPE)
309//%- (void)write##NAME:(int32_t)fieldNumber value:(TYPE)value;
310//%- (void)write##NAME##Array:(int32_t)fieldNumber values:(NSArray *)values;
311//%- (void)write##NAME##NoTag:(TYPE)value;
312//%
313// Special write methods for Groups.
314//%PDDM-DEFINE _WRITE_GROUP_DECLS(NAME, TYPE)
315//%- (void)write##NAME:(int32_t)fieldNumber
316//% NAME$S value:(TYPE)value;
317//%- (void)write##NAME##Array:(int32_t)fieldNumber values:(NSArray *)values;
318//%- (void)write##NAME##NoTag:(int32_t)fieldNumber
319//% NAME$S value:(TYPE)value;
320//%
321
322// One macro to hide it all up above.
323//%PDDM-DEFINE _WRITE_DECLS()
324//%_WRITE_PACKABLE_DECLS(Double, Double, double)
325//%_WRITE_PACKABLE_DECLS(Float, Float, float)
326//%_WRITE_PACKABLE_DECLS(UInt64, UInt64, uint64_t)
327//%_WRITE_PACKABLE_DECLS(Int64, Int64, int64_t)
328//%_WRITE_PACKABLE_DECLS(Int32, Int32, int32_t)
329//%_WRITE_PACKABLE_DECLS(UInt32, UInt32, uint32_t)
330//%_WRITE_PACKABLE_DECLS(Fixed64, UInt64, uint64_t)
331//%_WRITE_PACKABLE_DECLS(Fixed32, UInt32, uint32_t)
332//%_WRITE_PACKABLE_DECLS(SInt32, Int32, int32_t)
333//%_WRITE_PACKABLE_DECLS(SInt64, Int64, int64_t)
334//%_WRITE_PACKABLE_DECLS(SFixed64, Int64, int64_t)
335//%_WRITE_PACKABLE_DECLS(SFixed32, Int32, int32_t)
336//%_WRITE_PACKABLE_DECLS(Bool, Bool, BOOL)
337//%_WRITE_PACKABLE_DECLS(Enum, Enum, int32_t)
338//%_WRITE_UNPACKABLE_DECLS(String, NSString *)
339//%_WRITE_UNPACKABLE_DECLS(Message, GPBMessage *)
340//%_WRITE_UNPACKABLE_DECLS(Bytes, NSData *)
341//%_WRITE_GROUP_DECLS(Group, GPBMessage *)
342//%_WRITE_GROUP_DECLS(UnknownGroup, GPBUnknownFieldSet *)