1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
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
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.
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.
31 #import <Foundation/Foundation.h>
33 #import "GPBRuntimeTypes.h"
34 #import "GPBWireFormat.h"
37 @class GPBDoubleArray;
43 @class GPBUInt32Array;
44 @class GPBUInt64Array;
45 @class GPBUnknownFieldSet;
47 NS_ASSUME_NONNULL_BEGIN
50 * @c GPBCodedOutputStream exception names.
52 extern NSString *const GPBCodedOutputStreamException_OutOfSpace;
53 extern NSString *const GPBCodedOutputStreamException_WriteFailed;
56 * Writes out protocol message fields.
58 * The common uses of protocol buffers shouldn't need to use this class.
59 * GPBMessage's provide a -data method that will serialize the message for you.
61 * @note Any -write* api can raise the GPBCodedOutputStreamException_*
64 * @note Subclassing of GPBCodedOutputStream is NOT supported.
66 @interface GPBCodedOutputStream : NSObject
69 * Creates a stream to fill in the given data. Data must be sized to fit or
70 * an error will be raised when out of space.
72 * @param data The data where the stream will be written to.
74 * @return A newly instanced GPBCodedOutputStream.
76 + (instancetype)streamWithData:(NSMutableData *)data;
79 * Creates a stream to write into the given NSOutputStream.
81 * @param output The output stream where the stream will be written to.
83 * @return A newly instanced GPBCodedOutputStream.
85 + (instancetype)streamWithOutputStream:(NSOutputStream *)output;
88 * Initializes a stream to fill in the given data. Data must be sized to fit
89 * or an error will be raised when out of space.
91 * @param data The data where the stream will be written to.
93 * @return A newly initialized GPBCodedOutputStream.
95 - (instancetype)initWithData:(NSMutableData *)data;
98 * Initializes a stream to write into the given @c NSOutputStream.
100 * @param output The output stream where the stream will be written to.
102 * @return A newly initialized GPBCodedOutputStream.
104 - (instancetype)initWithOutputStream:(NSOutputStream *)output;
107 * Flush any buffered data out.
112 * Write the raw byte out.
114 * @param value The value to write out.
116 - (void)writeRawByte:(uint8_t)value;
119 * Write the tag for the given field number and wire format.
121 * @param fieldNumber The field number.
122 * @param format The wire format the data for the field will be in.
124 - (void)writeTag:(uint32_t)fieldNumber format:(GPBWireFormat)format;
127 * Write a 32bit value out in little endian format.
129 * @param value The value to write out.
131 - (void)writeRawLittleEndian32:(int32_t)value;
133 * Write a 64bit value out in little endian format.
135 * @param value The value to write out.
137 - (void)writeRawLittleEndian64:(int64_t)value;
140 * Write a 32bit value out in varint format.
142 * @param value The value to write out.
144 - (void)writeRawVarint32:(int32_t)value;
146 * Write a 64bit value out in varint format.
148 * @param value The value to write out.
150 - (void)writeRawVarint64:(int64_t)value;
153 * Write a size_t out as a 32bit varint value.
155 * @note This will truncate 64 bit values to 32.
157 * @param value The value to write out.
159 - (void)writeRawVarintSizeTAs32:(size_t)value;
162 * Writes the contents of an NSData out.
164 * @param data The data to write out.
166 - (void)writeRawData:(NSData *)data;
168 * Writes out the given data.
170 * @param data The data blob to write out.
171 * @param offset The offset into the blob to start writing out.
172 * @param length The number of bytes from the blob to write out.
174 - (void)writeRawPtr:(const void *)data
175 offset:(size_t)offset
176 length:(size_t)length;
178 //%PDDM-EXPAND _WRITE_DECLS()
179 // This block of code is generated, do not edit it directly.
182 * Write a double for the given field number.
184 * @param fieldNumber The field number assigned to the value.
185 * @param value The value to write out.
187 - (void)writeDouble:(int32_t)fieldNumber value:(double)value;
189 * Write a packed array of double for the given field number.
191 * @param fieldNumber The field number assigned to the values.
192 * @param values The values to write out.
193 * @param tag The tag assigned to the values.
195 - (void)writeDoubleArray:(int32_t)fieldNumber
196 values:(GPBDoubleArray *)values
199 * Write a double without any tag.
201 * @param value The value to write out.
203 - (void)writeDoubleNoTag:(double)value;
206 * Write a float for the given field number.
208 * @param fieldNumber The field number assigned to the value.
209 * @param value The value to write out.
211 - (void)writeFloat:(int32_t)fieldNumber value:(float)value;
213 * Write a packed array of float for the given field number.
215 * @param fieldNumber The field number assigned to the values.
216 * @param values The values to write out.
217 * @param tag The tag assigned to the values.
219 - (void)writeFloatArray:(int32_t)fieldNumber
220 values:(GPBFloatArray *)values
223 * Write a float without any tag.
225 * @param value The value to write out.
227 - (void)writeFloatNoTag:(float)value;
230 * Write a uint64_t for the given field number.
232 * @param fieldNumber The field number assigned to the value.
233 * @param value The value to write out.
235 - (void)writeUInt64:(int32_t)fieldNumber value:(uint64_t)value;
237 * Write a packed array of uint64_t for the given field number.
239 * @param fieldNumber The field number assigned to the values.
240 * @param values The values to write out.
241 * @param tag The tag assigned to the values.
243 - (void)writeUInt64Array:(int32_t)fieldNumber
244 values:(GPBUInt64Array *)values
247 * Write a uint64_t without any tag.
249 * @param value The value to write out.
251 - (void)writeUInt64NoTag:(uint64_t)value;
254 * Write a int64_t for the given field number.
256 * @param fieldNumber The field number assigned to the value.
257 * @param value The value to write out.
259 - (void)writeInt64:(int32_t)fieldNumber value:(int64_t)value;
261 * Write a packed array of int64_t for the given field number.
263 * @param fieldNumber The field number assigned to the values.
264 * @param values The values to write out.
265 * @param tag The tag assigned to the values.
267 - (void)writeInt64Array:(int32_t)fieldNumber
268 values:(GPBInt64Array *)values
271 * Write a int64_t without any tag.
273 * @param value The value to write out.
275 - (void)writeInt64NoTag:(int64_t)value;
278 * Write a int32_t for the given field number.
280 * @param fieldNumber The field number assigned to the value.
281 * @param value The value to write out.
283 - (void)writeInt32:(int32_t)fieldNumber value:(int32_t)value;
285 * Write a packed array of int32_t for the given field number.
287 * @param fieldNumber The field number assigned to the values.
288 * @param values The values to write out.
289 * @param tag The tag assigned to the values.
291 - (void)writeInt32Array:(int32_t)fieldNumber
292 values:(GPBInt32Array *)values
295 * Write a int32_t without any tag.
297 * @param value The value to write out.
299 - (void)writeInt32NoTag:(int32_t)value;
302 * Write a uint32_t for the given field number.
304 * @param fieldNumber The field number assigned to the value.
305 * @param value The value to write out.
307 - (void)writeUInt32:(int32_t)fieldNumber value:(uint32_t)value;
309 * Write a packed array of uint32_t for the given field number.
311 * @param fieldNumber The field number assigned to the values.
312 * @param values The values to write out.
313 * @param tag The tag assigned to the values.
315 - (void)writeUInt32Array:(int32_t)fieldNumber
316 values:(GPBUInt32Array *)values
319 * Write a uint32_t without any tag.
321 * @param value The value to write out.
323 - (void)writeUInt32NoTag:(uint32_t)value;
326 * Write a uint64_t for the given field number.
328 * @param fieldNumber The field number assigned to the value.
329 * @param value The value to write out.
331 - (void)writeFixed64:(int32_t)fieldNumber value:(uint64_t)value;
333 * Write a packed array of uint64_t for the given field number.
335 * @param fieldNumber The field number assigned to the values.
336 * @param values The values to write out.
337 * @param tag The tag assigned to the values.
339 - (void)writeFixed64Array:(int32_t)fieldNumber
340 values:(GPBUInt64Array *)values
343 * Write a uint64_t without any tag.
345 * @param value The value to write out.
347 - (void)writeFixed64NoTag:(uint64_t)value;
350 * Write a uint32_t for the given field number.
352 * @param fieldNumber The field number assigned to the value.
353 * @param value The value to write out.
355 - (void)writeFixed32:(int32_t)fieldNumber value:(uint32_t)value;
357 * Write a packed array of uint32_t for the given field number.
359 * @param fieldNumber The field number assigned to the values.
360 * @param values The values to write out.
361 * @param tag The tag assigned to the values.
363 - (void)writeFixed32Array:(int32_t)fieldNumber
364 values:(GPBUInt32Array *)values
367 * Write a uint32_t without any tag.
369 * @param value The value to write out.
371 - (void)writeFixed32NoTag:(uint32_t)value;
374 * Write a int32_t for the given field number.
376 * @param fieldNumber The field number assigned to the value.
377 * @param value The value to write out.
379 - (void)writeSInt32:(int32_t)fieldNumber value:(int32_t)value;
381 * Write a packed array of int32_t for the given field number.
383 * @param fieldNumber The field number assigned to the values.
384 * @param values The values to write out.
385 * @param tag The tag assigned to the values.
387 - (void)writeSInt32Array:(int32_t)fieldNumber
388 values:(GPBInt32Array *)values
391 * Write a int32_t without any tag.
393 * @param value The value to write out.
395 - (void)writeSInt32NoTag:(int32_t)value;
398 * Write a int64_t for the given field number.
400 * @param fieldNumber The field number assigned to the value.
401 * @param value The value to write out.
403 - (void)writeSInt64:(int32_t)fieldNumber value:(int64_t)value;
405 * Write a packed array of int64_t for the given field number.
407 * @param fieldNumber The field number assigned to the values.
408 * @param values The values to write out.
409 * @param tag The tag assigned to the values.
411 - (void)writeSInt64Array:(int32_t)fieldNumber
412 values:(GPBInt64Array *)values
415 * Write a int64_t without any tag.
417 * @param value The value to write out.
419 - (void)writeSInt64NoTag:(int64_t)value;
422 * Write a int64_t for the given field number.
424 * @param fieldNumber The field number assigned to the value.
425 * @param value The value to write out.
427 - (void)writeSFixed64:(int32_t)fieldNumber value:(int64_t)value;
429 * Write a packed array of int64_t for the given field number.
431 * @param fieldNumber The field number assigned to the values.
432 * @param values The values to write out.
433 * @param tag The tag assigned to the values.
435 - (void)writeSFixed64Array:(int32_t)fieldNumber
436 values:(GPBInt64Array *)values
439 * Write a int64_t without any tag.
441 * @param value The value to write out.
443 - (void)writeSFixed64NoTag:(int64_t)value;
446 * Write a int32_t for the given field number.
448 * @param fieldNumber The field number assigned to the value.
449 * @param value The value to write out.
451 - (void)writeSFixed32:(int32_t)fieldNumber value:(int32_t)value;
453 * Write a packed array of int32_t for the given field number.
455 * @param fieldNumber The field number assigned to the values.
456 * @param values The values to write out.
457 * @param tag The tag assigned to the values.
459 - (void)writeSFixed32Array:(int32_t)fieldNumber
460 values:(GPBInt32Array *)values
463 * Write a int32_t without any tag.
465 * @param value The value to write out.
467 - (void)writeSFixed32NoTag:(int32_t)value;
470 * Write a BOOL for the given field number.
472 * @param fieldNumber The field number assigned to the value.
473 * @param value The value to write out.
475 - (void)writeBool:(int32_t)fieldNumber value:(BOOL)value;
477 * Write a packed array of BOOL for the given field number.
479 * @param fieldNumber The field number assigned to the values.
480 * @param values The values to write out.
481 * @param tag The tag assigned to the values.
483 - (void)writeBoolArray:(int32_t)fieldNumber
484 values:(GPBBoolArray *)values
487 * Write a BOOL without any tag.
489 * @param value The value to write out.
491 - (void)writeBoolNoTag:(BOOL)value;
494 * Write a int32_t for the given field number.
496 * @param fieldNumber The field number assigned to the value.
497 * @param value The value to write out.
499 - (void)writeEnum:(int32_t)fieldNumber value:(int32_t)value;
501 * Write a packed array of int32_t for the given field number.
503 * @param fieldNumber The field number assigned to the values.
504 * @param values The values to write out.
505 * @param tag The tag assigned to the values.
507 - (void)writeEnumArray:(int32_t)fieldNumber
508 values:(GPBEnumArray *)values
511 * Write a int32_t without any tag.
513 * @param value The value to write out.
515 - (void)writeEnumNoTag:(int32_t)value;
518 * Write a NSString for the given field number.
520 * @param fieldNumber The field number assigned to the value.
521 * @param value The value to write out.
523 - (void)writeString:(int32_t)fieldNumber value:(NSString *)value;
525 * Write an array of NSString for the given field number.
527 * @param fieldNumber The field number assigned to the values.
528 * @param values The values to write out.
530 - (void)writeStringArray:(int32_t)fieldNumber values:(NSArray<NSString*> *)values;
532 * Write a NSString without any tag.
534 * @param value The value to write out.
536 - (void)writeStringNoTag:(NSString *)value;
539 * Write a GPBMessage for the given field number.
541 * @param fieldNumber The field number assigned to the value.
542 * @param value The value to write out.
544 - (void)writeMessage:(int32_t)fieldNumber value:(GPBMessage *)value;
546 * Write an array of GPBMessage for the given field number.
548 * @param fieldNumber The field number assigned to the values.
549 * @param values The values to write out.
551 - (void)writeMessageArray:(int32_t)fieldNumber values:(NSArray<GPBMessage*> *)values;
553 * Write a GPBMessage without any tag.
555 * @param value The value to write out.
557 - (void)writeMessageNoTag:(GPBMessage *)value;
560 * Write a NSData for the given field number.
562 * @param fieldNumber The field number assigned to the value.
563 * @param value The value to write out.
565 - (void)writeBytes:(int32_t)fieldNumber value:(NSData *)value;
567 * Write an array of NSData for the given field number.
569 * @param fieldNumber The field number assigned to the values.
570 * @param values The values to write out.
572 - (void)writeBytesArray:(int32_t)fieldNumber values:(NSArray<NSData*> *)values;
574 * Write a NSData without any tag.
576 * @param value The value to write out.
578 - (void)writeBytesNoTag:(NSData *)value;
581 * Write a GPBMessage for the given field number.
583 * @param fieldNumber The field number assigned to the value.
584 * @param value The value to write out.
586 - (void)writeGroup:(int32_t)fieldNumber
587 value:(GPBMessage *)value;
589 * Write an array of GPBMessage for the given field number.
591 * @param fieldNumber The field number assigned to the values.
592 * @param values The values to write out.
594 - (void)writeGroupArray:(int32_t)fieldNumber values:(NSArray<GPBMessage*> *)values;
596 * Write a GPBMessage without any tag (but does write the endGroup tag).
598 * @param fieldNumber The field number assigned to the value.
599 * @param value The value to write out.
601 - (void)writeGroupNoTag:(int32_t)fieldNumber
602 value:(GPBMessage *)value;
605 * Write a GPBUnknownFieldSet for the given field number.
607 * @param fieldNumber The field number assigned to the value.
608 * @param value The value to write out.
610 - (void)writeUnknownGroup:(int32_t)fieldNumber
611 value:(GPBUnknownFieldSet *)value;
613 * Write an array of GPBUnknownFieldSet for the given field number.
615 * @param fieldNumber The field number assigned to the values.
616 * @param values The values to write out.
618 - (void)writeUnknownGroupArray:(int32_t)fieldNumber values:(NSArray<GPBUnknownFieldSet*> *)values;
620 * Write a GPBUnknownFieldSet without any tag (but does write the endGroup tag).
622 * @param fieldNumber The field number assigned to the value.
623 * @param value The value to write out.
625 - (void)writeUnknownGroupNoTag:(int32_t)fieldNumber
626 value:(GPBUnknownFieldSet *)value;
628 //%PDDM-EXPAND-END _WRITE_DECLS()
631 Write a MessageSet extension field to the stream. For historical reasons,
632 the wire format differs from normal fields.
634 @param fieldNumber The extension field number to write out.
635 @param value The message from where to get the extension.
637 - (void)writeMessageSetExtension:(int32_t)fieldNumber value:(GPBMessage *)value;
640 Write an unparsed MessageSet extension field to the stream. For historical
641 reasons, the wire format differs from normal fields.
643 @param fieldNumber The extension field number to write out.
644 @param value The raw message from where to get the extension.
646 - (void)writeRawMessageSetExtension:(int32_t)fieldNumber value:(NSData *)value;
650 NS_ASSUME_NONNULL_END
652 // Write methods for types that can be in packed arrays.
653 //%PDDM-DEFINE _WRITE_PACKABLE_DECLS(NAME, ARRAY_TYPE, TYPE)
655 //% * Write a TYPE for the given field number.
657 //% * @param fieldNumber The field number assigned to the value.
658 //% * @param value The value to write out.
660 //%- (void)write##NAME:(int32_t)fieldNumber value:(TYPE)value;
662 //% * Write a packed array of TYPE for the given field number.
664 //% * @param fieldNumber The field number assigned to the values.
665 //% * @param values The values to write out.
666 //% * @param tag The tag assigned to the values.
668 //%- (void)write##NAME##Array:(int32_t)fieldNumber
669 //% NAME$S values:(GPB##ARRAY_TYPE##Array *)values
670 //% NAME$S tag:(uint32_t)tag;
672 //% * Write a TYPE without any tag.
674 //% * @param value The value to write out.
676 //%- (void)write##NAME##NoTag:(TYPE)value;
678 // Write methods for types that aren't in packed arrays.
679 //%PDDM-DEFINE _WRITE_UNPACKABLE_DECLS(NAME, TYPE)
681 //% * Write a TYPE for the given field number.
683 //% * @param fieldNumber The field number assigned to the value.
684 //% * @param value The value to write out.
686 //%- (void)write##NAME:(int32_t)fieldNumber value:(TYPE *)value;
688 //% * Write an array of TYPE for the given field number.
690 //% * @param fieldNumber The field number assigned to the values.
691 //% * @param values The values to write out.
693 //%- (void)write##NAME##Array:(int32_t)fieldNumber values:(NSArray<##TYPE##*> *)values;
695 //% * Write a TYPE without any tag.
697 //% * @param value The value to write out.
699 //%- (void)write##NAME##NoTag:(TYPE *)value;
701 // Special write methods for Groups.
702 //%PDDM-DEFINE _WRITE_GROUP_DECLS(NAME, TYPE)
704 //% * Write a TYPE for the given field number.
706 //% * @param fieldNumber The field number assigned to the value.
707 //% * @param value The value to write out.
709 //%- (void)write##NAME:(int32_t)fieldNumber
710 //% NAME$S value:(TYPE *)value;
712 //% * Write an array of TYPE for the given field number.
714 //% * @param fieldNumber The field number assigned to the values.
715 //% * @param values The values to write out.
717 //%- (void)write##NAME##Array:(int32_t)fieldNumber values:(NSArray<##TYPE##*> *)values;
719 //% * Write a TYPE without any tag (but does write the endGroup tag).
721 //% * @param fieldNumber The field number assigned to the value.
722 //% * @param value The value to write out.
724 //%- (void)write##NAME##NoTag:(int32_t)fieldNumber
725 //% NAME$S value:(TYPE *)value;
728 // One macro to hide it all up above.
729 //%PDDM-DEFINE _WRITE_DECLS()
730 //%_WRITE_PACKABLE_DECLS(Double, Double, double)
731 //%_WRITE_PACKABLE_DECLS(Float, Float, float)
732 //%_WRITE_PACKABLE_DECLS(UInt64, UInt64, uint64_t)
733 //%_WRITE_PACKABLE_DECLS(Int64, Int64, int64_t)
734 //%_WRITE_PACKABLE_DECLS(Int32, Int32, int32_t)
735 //%_WRITE_PACKABLE_DECLS(UInt32, UInt32, uint32_t)
736 //%_WRITE_PACKABLE_DECLS(Fixed64, UInt64, uint64_t)
737 //%_WRITE_PACKABLE_DECLS(Fixed32, UInt32, uint32_t)
738 //%_WRITE_PACKABLE_DECLS(SInt32, Int32, int32_t)
739 //%_WRITE_PACKABLE_DECLS(SInt64, Int64, int64_t)
740 //%_WRITE_PACKABLE_DECLS(SFixed64, Int64, int64_t)
741 //%_WRITE_PACKABLE_DECLS(SFixed32, Int32, int32_t)
742 //%_WRITE_PACKABLE_DECLS(Bool, Bool, BOOL)
743 //%_WRITE_PACKABLE_DECLS(Enum, Enum, int32_t)
744 //%_WRITE_UNPACKABLE_DECLS(String, NSString)
745 //%_WRITE_UNPACKABLE_DECLS(Message, GPBMessage)
746 //%_WRITE_UNPACKABLE_DECLS(Bytes, NSData)
747 //%_WRITE_GROUP_DECLS(Group, GPBMessage)
748 //%_WRITE_GROUP_DECLS(UnknownGroup, GPBUnknownFieldSet)