GPBCodedOutputStream.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // 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. #import <Foundation/Foundation.h>
  31. #import "GPBRuntimeTypes.h"
  32. #import "GPBWireFormat.h"
  33. @class GPBBoolArray;
  34. @class GPBDoubleArray;
  35. @class GPBEnumArray;
  36. @class GPBFloatArray;
  37. @class GPBMessage;
  38. @class GPBInt32Array;
  39. @class GPBInt64Array;
  40. @class GPBUInt32Array;
  41. @class GPBUInt64Array;
  42. @class GPBUnknownFieldSet;
  43. NS_ASSUME_NONNULL_BEGIN
  44. /// Writes out protocol message fields.
  45. ///
  46. /// The common uses of protocol buffers shouldn't need to use this class.
  47. /// @c GPBMessage's provide a @c -data method that will serialize the message
  48. /// for you.
  49. ///
  50. /// @note Subclassing of GPBCodedOutputStream is NOT supported.
  51. @interface GPBCodedOutputStream : NSObject
  52. /// Creates a stream to fill in the given data. Data must be sized to fit or
  53. /// an error will be raised when out of space.
  54. + (instancetype)streamWithData:(NSMutableData *)data;
  55. /// Creates a stream to write into the given @c NSOutputStream.
  56. + (instancetype)streamWithOutputStream:(NSOutputStream *)output;
  57. /// Initializes a stream to fill in the given data. Data must be sized to fit
  58. /// or an error will be raised when out of space.
  59. - (instancetype)initWithData:(NSMutableData *)data;
  60. /// Initializes a stream to write into the given @c NSOutputStream.
  61. - (instancetype)initWithOutputStream:(NSOutputStream *)output;
  62. /// Flush any buffered data out.
  63. - (void)flush;
  64. /// Write the raw byte out.
  65. - (void)writeRawByte:(uint8_t)value;
  66. /// Write the tag for the given field number and wire format.
  67. ///
  68. /// @param fieldNumber The field number.
  69. /// @param format The wire format the data for the field will be in.
  70. - (void)writeTag:(uint32_t)fieldNumber format:(GPBWireFormat)format;
  71. /// Write a 32bit value out in little endian format.
  72. - (void)writeRawLittleEndian32:(int32_t)value;
  73. /// Write a 64bit value out in little endian format.
  74. - (void)writeRawLittleEndian64:(int64_t)value;
  75. /// Write a 32bit value out in varint format.
  76. - (void)writeRawVarint32:(int32_t)value;
  77. /// Write a 64bit value out in varint format.
  78. - (void)writeRawVarint64:(int64_t)value;
  79. /// Write a size_t out as a 32bit varint value.
  80. ///
  81. /// @note This will truncate 64 bit values to 32.
  82. - (void)writeRawVarintSizeTAs32:(size_t)value;
  83. /// Writes the contents of an @c NSData out.
  84. - (void)writeRawData:(NSData *)data;
  85. /// Writes out the given data.
  86. ///
  87. /// @param data The data blob to write out.
  88. /// @param offset The offset into the blob to start writing out.
  89. /// @param length The number of bytes from the blob to write out.
  90. - (void)writeRawPtr:(const void *)data
  91. offset:(size_t)offset
  92. length:(size_t)length;
  93. //%PDDM-EXPAND _WRITE_DECLS()
  94. // This block of code is generated, do not edit it directly.
  95. /// Write a double for the given field number.
  96. - (void)writeDouble:(int32_t)fieldNumber value:(double)value;
  97. /// Write a packed array of double for the given field number.
  98. - (void)writeDoubleArray:(int32_t)fieldNumber
  99. values:(GPBDoubleArray *)values
  100. tag:(uint32_t)tag;
  101. /// Write a double without any tag.
  102. - (void)writeDoubleNoTag:(double)value;
  103. /// Write a float for the given field number.
  104. - (void)writeFloat:(int32_t)fieldNumber value:(float)value;
  105. /// Write a packed array of float for the given field number.
  106. - (void)writeFloatArray:(int32_t)fieldNumber
  107. values:(GPBFloatArray *)values
  108. tag:(uint32_t)tag;
  109. /// Write a float without any tag.
  110. - (void)writeFloatNoTag:(float)value;
  111. /// Write a uint64_t for the given field number.
  112. - (void)writeUInt64:(int32_t)fieldNumber value:(uint64_t)value;
  113. /// Write a packed array of uint64_t for the given field number.
  114. - (void)writeUInt64Array:(int32_t)fieldNumber
  115. values:(GPBUInt64Array *)values
  116. tag:(uint32_t)tag;
  117. /// Write a uint64_t without any tag.
  118. - (void)writeUInt64NoTag:(uint64_t)value;
  119. /// Write a int64_t for the given field number.
  120. - (void)writeInt64:(int32_t)fieldNumber value:(int64_t)value;
  121. /// Write a packed array of int64_t for the given field number.
  122. - (void)writeInt64Array:(int32_t)fieldNumber
  123. values:(GPBInt64Array *)values
  124. tag:(uint32_t)tag;
  125. /// Write a int64_t without any tag.
  126. - (void)writeInt64NoTag:(int64_t)value;
  127. /// Write a int32_t for the given field number.
  128. - (void)writeInt32:(int32_t)fieldNumber value:(int32_t)value;
  129. /// Write a packed array of int32_t for the given field number.
  130. - (void)writeInt32Array:(int32_t)fieldNumber
  131. values:(GPBInt32Array *)values
  132. tag:(uint32_t)tag;
  133. /// Write a int32_t without any tag.
  134. - (void)writeInt32NoTag:(int32_t)value;
  135. /// Write a uint32_t for the given field number.
  136. - (void)writeUInt32:(int32_t)fieldNumber value:(uint32_t)value;
  137. /// Write a packed array of uint32_t for the given field number.
  138. - (void)writeUInt32Array:(int32_t)fieldNumber
  139. values:(GPBUInt32Array *)values
  140. tag:(uint32_t)tag;
  141. /// Write a uint32_t without any tag.
  142. - (void)writeUInt32NoTag:(uint32_t)value;
  143. /// Write a uint64_t for the given field number.
  144. - (void)writeFixed64:(int32_t)fieldNumber value:(uint64_t)value;
  145. /// Write a packed array of uint64_t for the given field number.
  146. - (void)writeFixed64Array:(int32_t)fieldNumber
  147. values:(GPBUInt64Array *)values
  148. tag:(uint32_t)tag;
  149. /// Write a uint64_t without any tag.
  150. - (void)writeFixed64NoTag:(uint64_t)value;
  151. /// Write a uint32_t for the given field number.
  152. - (void)writeFixed32:(int32_t)fieldNumber value:(uint32_t)value;
  153. /// Write a packed array of uint32_t for the given field number.
  154. - (void)writeFixed32Array:(int32_t)fieldNumber
  155. values:(GPBUInt32Array *)values
  156. tag:(uint32_t)tag;
  157. /// Write a uint32_t without any tag.
  158. - (void)writeFixed32NoTag:(uint32_t)value;
  159. /// Write a int32_t for the given field number.
  160. - (void)writeSInt32:(int32_t)fieldNumber value:(int32_t)value;
  161. /// Write a packed array of int32_t for the given field number.
  162. - (void)writeSInt32Array:(int32_t)fieldNumber
  163. values:(GPBInt32Array *)values
  164. tag:(uint32_t)tag;
  165. /// Write a int32_t without any tag.
  166. - (void)writeSInt32NoTag:(int32_t)value;
  167. /// Write a int64_t for the given field number.
  168. - (void)writeSInt64:(int32_t)fieldNumber value:(int64_t)value;
  169. /// Write a packed array of int64_t for the given field number.
  170. - (void)writeSInt64Array:(int32_t)fieldNumber
  171. values:(GPBInt64Array *)values
  172. tag:(uint32_t)tag;
  173. /// Write a int64_t without any tag.
  174. - (void)writeSInt64NoTag:(int64_t)value;
  175. /// Write a int64_t for the given field number.
  176. - (void)writeSFixed64:(int32_t)fieldNumber value:(int64_t)value;
  177. /// Write a packed array of int64_t for the given field number.
  178. - (void)writeSFixed64Array:(int32_t)fieldNumber
  179. values:(GPBInt64Array *)values
  180. tag:(uint32_t)tag;
  181. /// Write a int64_t without any tag.
  182. - (void)writeSFixed64NoTag:(int64_t)value;
  183. /// Write a int32_t for the given field number.
  184. - (void)writeSFixed32:(int32_t)fieldNumber value:(int32_t)value;
  185. /// Write a packed array of int32_t for the given field number.
  186. - (void)writeSFixed32Array:(int32_t)fieldNumber
  187. values:(GPBInt32Array *)values
  188. tag:(uint32_t)tag;
  189. /// Write a int32_t without any tag.
  190. - (void)writeSFixed32NoTag:(int32_t)value;
  191. /// Write a BOOL for the given field number.
  192. - (void)writeBool:(int32_t)fieldNumber value:(BOOL)value;
  193. /// Write a packed array of BOOL for the given field number.
  194. - (void)writeBoolArray:(int32_t)fieldNumber
  195. values:(GPBBoolArray *)values
  196. tag:(uint32_t)tag;
  197. /// Write a BOOL without any tag.
  198. - (void)writeBoolNoTag:(BOOL)value;
  199. /// Write a int32_t for the given field number.
  200. - (void)writeEnum:(int32_t)fieldNumber value:(int32_t)value;
  201. /// Write a packed array of int32_t for the given field number.
  202. - (void)writeEnumArray:(int32_t)fieldNumber
  203. values:(GPBEnumArray *)values
  204. tag:(uint32_t)tag;
  205. /// Write a int32_t without any tag.
  206. - (void)writeEnumNoTag:(int32_t)value;
  207. /// Write a NSString for the given field number.
  208. - (void)writeString:(int32_t)fieldNumber value:(NSString *)value;
  209. /// Write an array of NSString for the given field number.
  210. - (void)writeStringArray:(int32_t)fieldNumber values:(NSArray<NSString*> *)values;
  211. /// Write a NSString without any tag.
  212. - (void)writeStringNoTag:(NSString *)value;
  213. /// Write a GPBMessage for the given field number.
  214. - (void)writeMessage:(int32_t)fieldNumber value:(GPBMessage *)value;
  215. /// Write an array of GPBMessage for the given field number.
  216. - (void)writeMessageArray:(int32_t)fieldNumber values:(NSArray<GPBMessage*> *)values;
  217. /// Write a GPBMessage without any tag.
  218. - (void)writeMessageNoTag:(GPBMessage *)value;
  219. /// Write a NSData for the given field number.
  220. - (void)writeBytes:(int32_t)fieldNumber value:(NSData *)value;
  221. /// Write an array of NSData for the given field number.
  222. - (void)writeBytesArray:(int32_t)fieldNumber values:(NSArray<NSData*> *)values;
  223. /// Write a NSData without any tag.
  224. - (void)writeBytesNoTag:(NSData *)value;
  225. /// Write a GPBMessage for the given field number.
  226. - (void)writeGroup:(int32_t)fieldNumber
  227. value:(GPBMessage *)value;
  228. /// Write an array of GPBMessage for the given field number.
  229. - (void)writeGroupArray:(int32_t)fieldNumber values:(NSArray<GPBMessage*> *)values;
  230. /// Write a GPBMessage without any tag (but does write the endGroup tag).
  231. - (void)writeGroupNoTag:(int32_t)fieldNumber
  232. value:(GPBMessage *)value;
  233. /// Write a GPBUnknownFieldSet for the given field number.
  234. - (void)writeUnknownGroup:(int32_t)fieldNumber
  235. value:(GPBUnknownFieldSet *)value;
  236. /// Write an array of GPBUnknownFieldSet for the given field number.
  237. - (void)writeUnknownGroupArray:(int32_t)fieldNumber values:(NSArray<GPBUnknownFieldSet*> *)values;
  238. /// Write a GPBUnknownFieldSet without any tag (but does write the endGroup tag).
  239. - (void)writeUnknownGroupNoTag:(int32_t)fieldNumber
  240. value:(GPBUnknownFieldSet *)value;
  241. //%PDDM-EXPAND-END _WRITE_DECLS()
  242. /// Write a MessageSet extension field to the stream. For historical reasons,
  243. /// the wire format differs from normal fields.
  244. - (void)writeMessageSetExtension:(int32_t)fieldNumber value:(GPBMessage *)value;
  245. /// Write an unparsed MessageSet extension field to the stream. For
  246. /// historical reasons, the wire format differs from normal fields.
  247. - (void)writeRawMessageSetExtension:(int32_t)fieldNumber value:(NSData *)value;
  248. @end
  249. NS_ASSUME_NONNULL_END
  250. // Write methods for types that can be in packed arrays.
  251. //%PDDM-DEFINE _WRITE_PACKABLE_DECLS(NAME, ARRAY_TYPE, TYPE)
  252. //%/// Write a TYPE for the given field number.
  253. //%- (void)write##NAME:(int32_t)fieldNumber value:(TYPE)value;
  254. //%/// Write a packed array of TYPE for the given field number.
  255. //%- (void)write##NAME##Array:(int32_t)fieldNumber
  256. //% NAME$S values:(GPB##ARRAY_TYPE##Array *)values
  257. //% NAME$S tag:(uint32_t)tag;
  258. //%/// Write a TYPE without any tag.
  259. //%- (void)write##NAME##NoTag:(TYPE)value;
  260. //%
  261. // Write methods for types that aren't in packed arrays.
  262. //%PDDM-DEFINE _WRITE_UNPACKABLE_DECLS(NAME, TYPE)
  263. //%/// Write a TYPE for the given field number.
  264. //%- (void)write##NAME:(int32_t)fieldNumber value:(TYPE *)value;
  265. //%/// Write an array of TYPE for the given field number.
  266. //%- (void)write##NAME##Array:(int32_t)fieldNumber values:(NSArray<##TYPE##*> *)values;
  267. //%/// Write a TYPE without any tag.
  268. //%- (void)write##NAME##NoTag:(TYPE *)value;
  269. //%
  270. // Special write methods for Groups.
  271. //%PDDM-DEFINE _WRITE_GROUP_DECLS(NAME, TYPE)
  272. //%/// Write a TYPE for the given field number.
  273. //%- (void)write##NAME:(int32_t)fieldNumber
  274. //% NAME$S value:(TYPE *)value;
  275. //%/// Write an array of TYPE for the given field number.
  276. //%- (void)write##NAME##Array:(int32_t)fieldNumber values:(NSArray<##TYPE##*> *)values;
  277. //%/// Write a TYPE without any tag (but does write the endGroup tag).
  278. //%- (void)write##NAME##NoTag:(int32_t)fieldNumber
  279. //% NAME$S value:(TYPE *)value;
  280. //%
  281. // One macro to hide it all up above.
  282. //%PDDM-DEFINE _WRITE_DECLS()
  283. //%_WRITE_PACKABLE_DECLS(Double, Double, double)
  284. //%_WRITE_PACKABLE_DECLS(Float, Float, float)
  285. //%_WRITE_PACKABLE_DECLS(UInt64, UInt64, uint64_t)
  286. //%_WRITE_PACKABLE_DECLS(Int64, Int64, int64_t)
  287. //%_WRITE_PACKABLE_DECLS(Int32, Int32, int32_t)
  288. //%_WRITE_PACKABLE_DECLS(UInt32, UInt32, uint32_t)
  289. //%_WRITE_PACKABLE_DECLS(Fixed64, UInt64, uint64_t)
  290. //%_WRITE_PACKABLE_DECLS(Fixed32, UInt32, uint32_t)
  291. //%_WRITE_PACKABLE_DECLS(SInt32, Int32, int32_t)
  292. //%_WRITE_PACKABLE_DECLS(SInt64, Int64, int64_t)
  293. //%_WRITE_PACKABLE_DECLS(SFixed64, Int64, int64_t)
  294. //%_WRITE_PACKABLE_DECLS(SFixed32, Int32, int32_t)
  295. //%_WRITE_PACKABLE_DECLS(Bool, Bool, BOOL)
  296. //%_WRITE_PACKABLE_DECLS(Enum, Enum, int32_t)
  297. //%_WRITE_UNPACKABLE_DECLS(String, NSString)
  298. //%_WRITE_UNPACKABLE_DECLS(Message, GPBMessage)
  299. //%_WRITE_UNPACKABLE_DECLS(Bytes, NSData)
  300. //%_WRITE_GROUP_DECLS(Group, GPBMessage)
  301. //%_WRITE_GROUP_DECLS(UnknownGroup, GPBUnknownFieldSet)