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 // http://code.google.com/p/protobuf/ | 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. |
11 // * Redistributions in binary form must reproduce the above | 11 // * Redistributions in binary form must reproduce the above |
12 // copyright notice, this list of conditions and the following disclaimer | 12 // copyright notice, this list of conditions and the following disclaimer |
13 // in the documentation and/or other materials provided with the | 13 // in the documentation and/or other materials provided with the |
(...skipping 28 matching lines...) Expand all Loading... |
42 | 42 |
43 #include <string> | 43 #include <string> |
44 #include <google/protobuf/stubs/common.h> | 44 #include <google/protobuf/stubs/common.h> |
45 #include <google/protobuf/message_lite.h> | 45 #include <google/protobuf/message_lite.h> |
46 #include <google/protobuf/io/coded_stream.h> // for CodedOutputStream::Varint32
Size | 46 #include <google/protobuf/io/coded_stream.h> // for CodedOutputStream::Varint32
Size |
47 | 47 |
48 namespace google { | 48 namespace google { |
49 | 49 |
50 namespace protobuf { | 50 namespace protobuf { |
51 template <typename T> class RepeatedField; // repeated_field.h | 51 template <typename T> class RepeatedField; // repeated_field.h |
52 class UnknownFieldSet; | |
53 } | 52 } |
54 | 53 |
55 namespace protobuf { | 54 namespace protobuf { |
56 namespace internal { | 55 namespace internal { |
57 | 56 |
58 class StringPieceField; | 57 class StringPieceField; |
59 | 58 |
60 // This class is for internal use by the protocol buffer library and by | 59 // This class is for internal use by the protocol buffer library and by |
61 // protocol-complier-generated message classes. It must not be called | 60 // protocol-complier-generated message classes. It must not be called |
62 // directly by clients. | 61 // directly by clients. |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 // This is different from MakeTag(field->number(), field->type()) in the case | 150 // This is different from MakeTag(field->number(), field->type()) in the case |
152 // of packed repeated fields. | 151 // of packed repeated fields. |
153 static uint32 MakeTag(int field_number, WireType type); | 152 static uint32 MakeTag(int field_number, WireType type); |
154 static WireType GetTagWireType(uint32 tag); | 153 static WireType GetTagWireType(uint32 tag); |
155 static int GetTagFieldNumber(uint32 tag); | 154 static int GetTagFieldNumber(uint32 tag); |
156 | 155 |
157 // Compute the byte size of a tag. For groups, this includes both the start | 156 // Compute the byte size of a tag. For groups, this includes both the start |
158 // and end tags. | 157 // and end tags. |
159 static inline int TagSize(int field_number, WireFormatLite::FieldType type); | 158 static inline int TagSize(int field_number, WireFormatLite::FieldType type); |
160 | 159 |
161 // ----------------------------------------------------------------- | |
162 // Helpers for dealing with unknown fields | |
163 | |
164 // Skips a field value with the given tag. The input should start | 160 // Skips a field value with the given tag. The input should start |
165 // positioned immediately after the tag. Skipped values are simply discarded, | 161 // positioned immediately after the tag. Skipped values are simply discarded, |
166 // not recorded anywhere. See WireFormat::SkipField() for a version that | 162 // not recorded anywhere. See WireFormat::SkipField() for a version that |
167 // records to an UnknownFieldSet. | 163 // records to an UnknownFieldSet. |
| 164 static bool SkipField(io::CodedInputStream* input, uint32 tag); |
| 165 |
| 166 // Skips a field value with the given tag. The input should start |
| 167 // positioned immediately after the tag. Skipped values are recorded to a |
| 168 // CodedOutputStream. |
168 static bool SkipField(io::CodedInputStream* input, uint32 tag, | 169 static bool SkipField(io::CodedInputStream* input, uint32 tag, |
169 UnknownFieldSet *unknown_fields); | 170 io::CodedOutputStream* output); |
170 | 171 |
171 // Reads and ignores a message from the input. If unknown_fields is non-NULL, | 172 // Reads and ignores a message from the input. Skipped values are simply |
172 // the contents will be added to it. | 173 // discarded, not recorded anywhere. See WireFormat::SkipMessage() for a |
| 174 // version that records to an UnknownFieldSet. |
| 175 static bool SkipMessage(io::CodedInputStream* input); |
| 176 |
| 177 // Reads and ignores a message from the input. Skipped values are recorded |
| 178 // to a CodedOutputStream. |
173 static bool SkipMessage(io::CodedInputStream* input, | 179 static bool SkipMessage(io::CodedInputStream* input, |
174 UnknownFieldSet* unknown_fields); | 180 io::CodedOutputStream* output); |
175 | |
176 | |
177 // Reads and ignores a message from the input. Skipped values may be stored | |
178 // in the UnknownFieldSet if it exists. | |
179 static bool SkipMessage(io::CodedInputStream* input); | |
180 | 181 |
181 // This macro does the same thing as WireFormatLite::MakeTag(), but the | 182 // This macro does the same thing as WireFormatLite::MakeTag(), but the |
182 // result is usable as a compile-time constant, which makes it usable | 183 // result is usable as a compile-time constant, which makes it usable |
183 // as a switch case or a template input. WireFormatLite::MakeTag() is more | 184 // as a switch case or a template input. WireFormatLite::MakeTag() is more |
184 // type-safe, though, so prefer it if possible. | 185 // type-safe, though, so prefer it if possible. |
185 #define GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(FIELD_NUMBER, TYPE)
\ | 186 #define GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(FIELD_NUMBER, TYPE)
\ |
186 static_cast<uint32>( \ | 187 static_cast<uint32>( \ |
187 ((FIELD_NUMBER) << ::google::protobuf::internal::WireFormatLite::kTagTypeBit
s) \ | 188 ((FIELD_NUMBER) << ::google::protobuf::internal::WireFormatLite::kTagTypeBit
s) \ |
188 | (TYPE)) | 189 | (TYPE)) |
189 | 190 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 // such a way that numbers with small magnitudes will encode to smaller | 226 // such a way that numbers with small magnitudes will encode to smaller |
226 // varints. If you simply static_cast a negative number to an unsigned | 227 // varints. If you simply static_cast a negative number to an unsigned |
227 // number and varint-encode it, it will always take 10 bytes, defeating | 228 // number and varint-encode it, it will always take 10 bytes, defeating |
228 // the purpose of varint. So, for the "sint32" and "sint64" field types, | 229 // the purpose of varint. So, for the "sint32" and "sint64" field types, |
229 // we ZigZag-encode the values. | 230 // we ZigZag-encode the values. |
230 static uint32 ZigZagEncode32(int32 n); | 231 static uint32 ZigZagEncode32(int32 n); |
231 static int32 ZigZagDecode32(uint32 n); | 232 static int32 ZigZagDecode32(uint32 n); |
232 static uint64 ZigZagEncode64(int64 n); | 233 static uint64 ZigZagEncode64(int64 n); |
233 static int64 ZigZagDecode64(uint64 n); | 234 static int64 ZigZagDecode64(uint64 n); |
234 | 235 |
235 // Write the contents of an UnknownFieldSet to the output. | |
236 static void SerializeUnknownFields(const UnknownFieldSet& unknown_fields, | |
237 io::CodedOutputStream* output); | |
238 // Same as above, except writing directly to the provided buffer. | |
239 // Requires that the buffer have sufficient capacity for | |
240 // ComputeUnknownFieldsSize(unknown_fields). | |
241 // | |
242 // Returns a pointer past the last written byte. | |
243 static uint8* SerializeUnknownFieldsToArray( | |
244 const UnknownFieldSet& unknown_fields, | |
245 uint8* target); | |
246 | |
247 // Same thing except for messages that have the message_set_wire_format | |
248 // option. | |
249 static void SerializeUnknownMessageSetItems( | |
250 const UnknownFieldSet& unknown_fields, | |
251 io::CodedOutputStream* output); | |
252 // Same as above, except writing directly to the provided buffer. | |
253 // Requires that the buffer have sufficient capacity for | |
254 // ComputeUnknownMessageSetItemsSize(unknown_fields). | |
255 // | |
256 // Returns a pointer past the last written byte. | |
257 static uint8* SerializeUnknownMessageSetItemsToArray( | |
258 const UnknownFieldSet& unknown_fields, | |
259 uint8* target); | |
260 | |
261 // Compute the size of the UnknownFieldSet on the wire. | |
262 static int ComputeUnknownFieldsSize(const UnknownFieldSet& unknown_fields); | |
263 | |
264 // Same thing except for messages that have the message_set_wire_format | |
265 // option. | |
266 static int ComputeUnknownMessageSetItemsSize( | |
267 const UnknownFieldSet& unknown_fields); | |
268 | |
269 // ================================================================= | 236 // ================================================================= |
270 // Methods for reading/writing individual field. The implementations | 237 // Methods for reading/writing individual field. The implementations |
271 // of these methods are defined in wire_format_lite_inl.h; you must #include | 238 // of these methods are defined in wire_format_lite_inl.h; you must #include |
272 // that file to use these. | 239 // that file to use these. |
273 | 240 |
274 // Avoid ugly line wrapping | 241 // Avoid ugly line wrapping |
275 #define input io::CodedInputStream* input | 242 #define input io::CodedInputStream* input_arg |
276 #define output io::CodedOutputStream* output | 243 #define output io::CodedOutputStream* output_arg |
277 #define field_number int field_number | 244 #define field_number int field_number_arg |
278 #define INL GOOGLE_ATTRIBUTE_ALWAYS_INLINE | 245 #define INL GOOGLE_ATTRIBUTE_ALWAYS_INLINE |
279 | 246 |
280 // Read fields, not including tags. The assumption is that you already | 247 // Read fields, not including tags. The assumption is that you already |
281 // read the tag to determine what field to read. | 248 // read the tag to determine what field to read. |
282 | 249 |
283 // For primitive fields, we just use a templatized routine parameterized by | 250 // For primitive fields, we just use a templatized routine parameterized by |
284 // the represented type and the FieldType. These are specialized with the | 251 // the represented type and the FieldType. These are specialized with the |
285 // appropriate definition for each declared type. | 252 // appropriate definition for each declared type. |
286 template <typename CType, enum FieldType DeclaredType> | 253 template <typename CType, enum FieldType DeclaredType> INL |
287 static inline bool ReadPrimitive(input, CType* value) INL; | 254 static bool ReadPrimitive(input, CType* value); |
288 | 255 |
289 // Reads repeated primitive values, with optimizations for repeats. | 256 // Reads repeated primitive values, with optimizations for repeats. |
290 // tag_size and tag should both be compile-time constants provided by the | 257 // tag_size and tag should both be compile-time constants provided by the |
291 // protocol compiler. | 258 // protocol compiler. |
292 template <typename CType, enum FieldType DeclaredType> | 259 template <typename CType, enum FieldType DeclaredType> INL |
293 static inline bool ReadRepeatedPrimitive(int tag_size, | 260 static bool ReadRepeatedPrimitive(int tag_size, |
294 uint32 tag, | 261 uint32 tag, |
295 input, | 262 input, |
296 RepeatedField<CType>* value) INL; | 263 RepeatedField<CType>* value); |
297 | 264 |
298 // Identical to ReadRepeatedPrimitive, except will not inline the | 265 // Identical to ReadRepeatedPrimitive, except will not inline the |
299 // implementation. | 266 // implementation. |
300 template <typename CType, enum FieldType DeclaredType> | 267 template <typename CType, enum FieldType DeclaredType> |
301 static bool ReadRepeatedPrimitiveNoInline(int tag_size, | 268 static bool ReadRepeatedPrimitiveNoInline(int tag_size, |
302 uint32 tag, | 269 uint32 tag, |
303 input, | 270 input, |
304 RepeatedField<CType>* value); | 271 RepeatedField<CType>* value); |
305 | 272 |
306 // Reads a primitive value directly from the provided buffer. It returns a | 273 // Reads a primitive value directly from the provided buffer. It returns a |
307 // pointer past the segment of data that was read. | 274 // pointer past the segment of data that was read. |
308 // | 275 // |
309 // This is only implemented for the types with fixed wire size, e.g. | 276 // This is only implemented for the types with fixed wire size, e.g. |
310 // float, double, and the (s)fixed* types. | 277 // float, double, and the (s)fixed* types. |
311 template <typename CType, enum FieldType DeclaredType> | 278 template <typename CType, enum FieldType DeclaredType> INL |
312 static inline const uint8* ReadPrimitiveFromArray(const uint8* buffer, | 279 static const uint8* ReadPrimitiveFromArray(const uint8* buffer, CType* value); |
313 CType* value) INL; | |
314 | 280 |
315 // Reads a primitive packed field. | 281 // Reads a primitive packed field. |
316 // | 282 // |
317 // This is only implemented for packable types. | 283 // This is only implemented for packable types. |
318 template <typename CType, enum FieldType DeclaredType> | 284 template <typename CType, enum FieldType DeclaredType> INL |
319 static inline bool ReadPackedPrimitive(input, | 285 static bool ReadPackedPrimitive(input, RepeatedField<CType>* value); |
320 RepeatedField<CType>* value) INL; | |
321 | 286 |
322 // Identical to ReadPackedPrimitive, except will not inline the | 287 // Identical to ReadPackedPrimitive, except will not inline the |
323 // implementation. | 288 // implementation. |
324 template <typename CType, enum FieldType DeclaredType> | 289 template <typename CType, enum FieldType DeclaredType> |
325 static bool ReadPackedPrimitiveNoInline(input, RepeatedField<CType>* value); | 290 static bool ReadPackedPrimitiveNoInline(input, RepeatedField<CType>* value); |
326 | 291 |
327 // Read a packed enum field. Values for which is_valid() returns false are | 292 // Read a packed enum field. If the is_valid function is not NULL, values for |
328 // dropped. | 293 // which is_valid(value) returns false are silently dropped. |
329 static bool ReadPackedEnumNoInline(input, | 294 static bool ReadPackedEnumNoInline(input, |
330 bool (*is_valid)(int), | 295 bool (*is_valid)(int), |
331 RepeatedField<int>* value); | 296 RepeatedField<int>* values); |
332 | 297 |
333 static bool ReadString(input, string* value); | 298 // Read a packed enum field. If the is_valid function is not NULL, values for |
334 static bool ReadBytes (input, string* value); | 299 // which is_valid(value) returns false are appended to unknown_fields_stream. |
| 300 static bool ReadPackedEnumPreserveUnknowns( |
| 301 input, |
| 302 field_number, |
| 303 bool (*is_valid)(int), |
| 304 io::CodedOutputStream* unknown_fields_stream, |
| 305 RepeatedField<int>* values); |
| 306 |
| 307 // Read a string. ReadString(..., string* value) requires an existing string. |
| 308 static inline bool ReadString(input, string* value); |
| 309 // ReadString(..., string** p) is internal-only, and should only be called |
| 310 // from generated code. It starts by setting *p to "new string" |
| 311 // if *p == &GetEmptyStringAlreadyInited(). It then invokes |
| 312 // ReadString(input, *p). This is useful for reducing code size. |
| 313 static inline bool ReadString(input, string** p); |
| 314 // Analogous to ReadString(). |
| 315 static bool ReadBytes(input, string* value); |
| 316 static bool ReadBytes(input, string** p); |
| 317 |
| 318 |
| 319 enum Operation { |
| 320 PARSE = 0, |
| 321 SERIALIZE = 1, |
| 322 }; |
| 323 |
| 324 // Returns true if the data is valid UTF-8. |
| 325 static bool VerifyUtf8String(const char* data, int size, |
| 326 Operation op, |
| 327 const char* field_name); |
335 | 328 |
336 static inline bool ReadGroup (field_number, input, MessageLite* value); | 329 static inline bool ReadGroup (field_number, input, MessageLite* value); |
337 static inline bool ReadMessage(input, MessageLite* value); | 330 static inline bool ReadMessage(input, MessageLite* value); |
338 | 331 |
339 // Like above, but de-virtualize the call to MergePartialFromCodedStream(). | 332 // Like above, but de-virtualize the call to MergePartialFromCodedStream(). |
340 // The pointer must point at an instance of MessageType, *not* a subclass (or | 333 // The pointer must point at an instance of MessageType, *not* a subclass (or |
341 // the subclass must not override MergePartialFromCodedStream()). | 334 // the subclass must not override MergePartialFromCodedStream()). |
342 template<typename MessageType> | 335 template<typename MessageType> |
343 static inline bool ReadGroupNoVirtual(field_number, input, | 336 static inline bool ReadGroupNoVirtual(field_number, input, |
344 MessageType* value); | 337 MessageType* value); |
345 template<typename MessageType> | 338 template<typename MessageType> |
346 static inline bool ReadMessageNoVirtual(input, MessageType* value); | 339 static inline bool ReadMessageNoVirtual(input, MessageType* value); |
347 | 340 |
| 341 // The same, but do not modify input's recursion depth. This is useful |
| 342 // when reading a bunch of groups or messages in a loop, because then the |
| 343 // recursion depth can be incremented before the loop and decremented after. |
| 344 template<typename MessageType> |
| 345 static inline bool ReadGroupNoVirtualNoRecursionDepth(field_number, input, |
| 346 MessageType* value); |
| 347 |
| 348 template<typename MessageType> |
| 349 static inline bool ReadMessageNoVirtualNoRecursionDepth(input, |
| 350 MessageType* value); |
| 351 |
348 // Write a tag. The Write*() functions typically include the tag, so | 352 // Write a tag. The Write*() functions typically include the tag, so |
349 // normally there's no need to call this unless using the Write*NoTag() | 353 // normally there's no need to call this unless using the Write*NoTag() |
350 // variants. | 354 // variants. |
351 static inline void WriteTag(field_number, WireType type, output) INL; | 355 INL static void WriteTag(field_number, WireType type, output); |
352 | 356 |
353 // Write fields, without tags. | 357 // Write fields, without tags. |
354 static inline void WriteInt32NoTag (int32 value, output) INL; | 358 INL static void WriteInt32NoTag (int32 value, output); |
355 static inline void WriteInt64NoTag (int64 value, output) INL; | 359 INL static void WriteInt64NoTag (int64 value, output); |
356 static inline void WriteUInt32NoTag (uint32 value, output) INL; | 360 INL static void WriteUInt32NoTag (uint32 value, output); |
357 static inline void WriteUInt64NoTag (uint64 value, output) INL; | 361 INL static void WriteUInt64NoTag (uint64 value, output); |
358 static inline void WriteSInt32NoTag (int32 value, output) INL; | 362 INL static void WriteSInt32NoTag (int32 value, output); |
359 static inline void WriteSInt64NoTag (int64 value, output) INL; | 363 INL static void WriteSInt64NoTag (int64 value, output); |
360 static inline void WriteFixed32NoTag (uint32 value, output) INL; | 364 INL static void WriteFixed32NoTag (uint32 value, output); |
361 static inline void WriteFixed64NoTag (uint64 value, output) INL; | 365 INL static void WriteFixed64NoTag (uint64 value, output); |
362 static inline void WriteSFixed32NoTag(int32 value, output) INL; | 366 INL static void WriteSFixed32NoTag(int32 value, output); |
363 static inline void WriteSFixed64NoTag(int64 value, output) INL; | 367 INL static void WriteSFixed64NoTag(int64 value, output); |
364 static inline void WriteFloatNoTag (float value, output) INL; | 368 INL static void WriteFloatNoTag (float value, output); |
365 static inline void WriteDoubleNoTag (double value, output) INL; | 369 INL static void WriteDoubleNoTag (double value, output); |
366 static inline void WriteBoolNoTag (bool value, output) INL; | 370 INL static void WriteBoolNoTag (bool value, output); |
367 static inline void WriteEnumNoTag (int value, output) INL; | 371 INL static void WriteEnumNoTag (int value, output); |
368 | 372 |
369 // Write fields, including tags. | 373 // Write fields, including tags. |
370 static void WriteInt32 (field_number, int32 value, output); | 374 static void WriteInt32 (field_number, int32 value, output); |
371 static void WriteInt64 (field_number, int64 value, output); | 375 static void WriteInt64 (field_number, int64 value, output); |
372 static void WriteUInt32 (field_number, uint32 value, output); | 376 static void WriteUInt32 (field_number, uint32 value, output); |
373 static void WriteUInt64 (field_number, uint64 value, output); | 377 static void WriteUInt64 (field_number, uint64 value, output); |
374 static void WriteSInt32 (field_number, int32 value, output); | 378 static void WriteSInt32 (field_number, int32 value, output); |
375 static void WriteSInt64 (field_number, int64 value, output); | 379 static void WriteSInt64 (field_number, int64 value, output); |
376 static void WriteFixed32 (field_number, uint32 value, output); | 380 static void WriteFixed32 (field_number, uint32 value, output); |
377 static void WriteFixed64 (field_number, uint64 value, output); | 381 static void WriteFixed64 (field_number, uint64 value, output); |
378 static void WriteSFixed32(field_number, int32 value, output); | 382 static void WriteSFixed32(field_number, int32 value, output); |
379 static void WriteSFixed64(field_number, int64 value, output); | 383 static void WriteSFixed64(field_number, int64 value, output); |
380 static void WriteFloat (field_number, float value, output); | 384 static void WriteFloat (field_number, float value, output); |
381 static void WriteDouble (field_number, double value, output); | 385 static void WriteDouble (field_number, double value, output); |
382 static void WriteBool (field_number, bool value, output); | 386 static void WriteBool (field_number, bool value, output); |
383 static void WriteEnum (field_number, int value, output); | 387 static void WriteEnum (field_number, int value, output); |
384 | 388 |
385 static void WriteString(field_number, const string& value, output); | 389 static void WriteString(field_number, const string& value, output); |
386 static void WriteBytes (field_number, const string& value, output); | 390 static void WriteBytes (field_number, const string& value, output); |
| 391 static void WriteStringMaybeAliased( |
| 392 field_number, const string& value, output); |
| 393 static void WriteBytesMaybeAliased( |
| 394 field_number, const string& value, output); |
387 | 395 |
388 static void WriteGroup( | 396 static void WriteGroup( |
389 field_number, const MessageLite& value, output); | 397 field_number, const MessageLite& value, output); |
390 static void WriteMessage( | 398 static void WriteMessage( |
391 field_number, const MessageLite& value, output); | 399 field_number, const MessageLite& value, output); |
392 // Like above, but these will check if the output stream has enough | 400 // Like above, but these will check if the output stream has enough |
393 // space to write directly to a flat array. | 401 // space to write directly to a flat array. |
394 static void WriteGroupMaybeToArray( | 402 static void WriteGroupMaybeToArray( |
395 field_number, const MessageLite& value, output); | 403 field_number, const MessageLite& value, output); |
396 static void WriteMessageMaybeToArray( | 404 static void WriteMessageMaybeToArray( |
397 field_number, const MessageLite& value, output); | 405 field_number, const MessageLite& value, output); |
398 | 406 |
399 // Like above, but de-virtualize the call to SerializeWithCachedSizes(). The | 407 // Like above, but de-virtualize the call to SerializeWithCachedSizes(). The |
400 // pointer must point at an instance of MessageType, *not* a subclass (or | 408 // pointer must point at an instance of MessageType, *not* a subclass (or |
401 // the subclass must not override SerializeWithCachedSizes()). | 409 // the subclass must not override SerializeWithCachedSizes()). |
402 template<typename MessageType> | 410 template<typename MessageType> |
403 static inline void WriteGroupNoVirtual( | 411 static inline void WriteGroupNoVirtual( |
404 field_number, const MessageType& value, output); | 412 field_number, const MessageType& value, output); |
405 template<typename MessageType> | 413 template<typename MessageType> |
406 static inline void WriteMessageNoVirtual( | 414 static inline void WriteMessageNoVirtual( |
407 field_number, const MessageType& value, output); | 415 field_number, const MessageType& value, output); |
408 | 416 |
409 #undef output | 417 #undef output |
410 #define output uint8* target | 418 #define output uint8* target |
411 | 419 |
412 // Like above, but use only *ToArray methods of CodedOutputStream. | 420 // Like above, but use only *ToArray methods of CodedOutputStream. |
413 static inline uint8* WriteTagToArray(field_number, WireType type, output) INL; | 421 INL static uint8* WriteTagToArray(field_number, WireType type, output); |
414 | 422 |
415 // Write fields, without tags. | 423 // Write fields, without tags. |
416 static inline uint8* WriteInt32NoTagToArray (int32 value, output) INL; | 424 INL static uint8* WriteInt32NoTagToArray (int32 value, output); |
417 static inline uint8* WriteInt64NoTagToArray (int64 value, output) INL; | 425 INL static uint8* WriteInt64NoTagToArray (int64 value, output); |
418 static inline uint8* WriteUInt32NoTagToArray (uint32 value, output) INL; | 426 INL static uint8* WriteUInt32NoTagToArray (uint32 value, output); |
419 static inline uint8* WriteUInt64NoTagToArray (uint64 value, output) INL; | 427 INL static uint8* WriteUInt64NoTagToArray (uint64 value, output); |
420 static inline uint8* WriteSInt32NoTagToArray (int32 value, output) INL; | 428 INL static uint8* WriteSInt32NoTagToArray (int32 value, output); |
421 static inline uint8* WriteSInt64NoTagToArray (int64 value, output) INL; | 429 INL static uint8* WriteSInt64NoTagToArray (int64 value, output); |
422 static inline uint8* WriteFixed32NoTagToArray (uint32 value, output) INL; | 430 INL static uint8* WriteFixed32NoTagToArray (uint32 value, output); |
423 static inline uint8* WriteFixed64NoTagToArray (uint64 value, output) INL; | 431 INL static uint8* WriteFixed64NoTagToArray (uint64 value, output); |
424 static inline uint8* WriteSFixed32NoTagToArray(int32 value, output) INL; | 432 INL static uint8* WriteSFixed32NoTagToArray(int32 value, output); |
425 static inline uint8* WriteSFixed64NoTagToArray(int64 value, output) INL; | 433 INL static uint8* WriteSFixed64NoTagToArray(int64 value, output); |
426 static inline uint8* WriteFloatNoTagToArray (float value, output) INL; | 434 INL static uint8* WriteFloatNoTagToArray (float value, output); |
427 static inline uint8* WriteDoubleNoTagToArray (double value, output) INL; | 435 INL static uint8* WriteDoubleNoTagToArray (double value, output); |
428 static inline uint8* WriteBoolNoTagToArray (bool value, output) INL; | 436 INL static uint8* WriteBoolNoTagToArray (bool value, output); |
429 static inline uint8* WriteEnumNoTagToArray (int value, output) INL; | 437 INL static uint8* WriteEnumNoTagToArray (int value, output); |
430 | 438 |
431 // Write fields, including tags. | 439 // Write fields, including tags. |
432 static inline uint8* WriteInt32ToArray( | 440 INL static uint8* WriteInt32ToArray(field_number, int32 value, output); |
433 field_number, int32 value, output) INL; | 441 INL static uint8* WriteInt64ToArray(field_number, int64 value, output); |
434 static inline uint8* WriteInt64ToArray( | 442 INL static uint8* WriteUInt32ToArray(field_number, uint32 value, output); |
435 field_number, int64 value, output) INL; | 443 INL static uint8* WriteUInt64ToArray(field_number, uint64 value, output); |
436 static inline uint8* WriteUInt32ToArray( | 444 INL static uint8* WriteSInt32ToArray(field_number, int32 value, output); |
437 field_number, uint32 value, output) INL; | 445 INL static uint8* WriteSInt64ToArray(field_number, int64 value, output); |
438 static inline uint8* WriteUInt64ToArray( | 446 INL static uint8* WriteFixed32ToArray(field_number, uint32 value, output); |
439 field_number, uint64 value, output) INL; | 447 INL static uint8* WriteFixed64ToArray(field_number, uint64 value, output); |
440 static inline uint8* WriteSInt32ToArray( | 448 INL static uint8* WriteSFixed32ToArray(field_number, int32 value, output); |
441 field_number, int32 value, output) INL; | 449 INL static uint8* WriteSFixed64ToArray(field_number, int64 value, output); |
442 static inline uint8* WriteSInt64ToArray( | 450 INL static uint8* WriteFloatToArray(field_number, float value, output); |
443 field_number, int64 value, output) INL; | 451 INL static uint8* WriteDoubleToArray(field_number, double value, output); |
444 static inline uint8* WriteFixed32ToArray( | 452 INL static uint8* WriteBoolToArray(field_number, bool value, output); |
445 field_number, uint32 value, output) INL; | 453 INL static uint8* WriteEnumToArray(field_number, int value, output); |
446 static inline uint8* WriteFixed64ToArray( | |
447 field_number, uint64 value, output) INL; | |
448 static inline uint8* WriteSFixed32ToArray( | |
449 field_number, int32 value, output) INL; | |
450 static inline uint8* WriteSFixed64ToArray( | |
451 field_number, int64 value, output) INL; | |
452 static inline uint8* WriteFloatToArray( | |
453 field_number, float value, output) INL; | |
454 static inline uint8* WriteDoubleToArray( | |
455 field_number, double value, output) INL; | |
456 static inline uint8* WriteBoolToArray( | |
457 field_number, bool value, output) INL; | |
458 static inline uint8* WriteEnumToArray( | |
459 field_number, int value, output) INL; | |
460 | 454 |
461 static inline uint8* WriteStringToArray( | 455 INL static uint8* WriteStringToArray( |
462 field_number, const string& value, output) INL; | 456 field_number, const string& value, output); |
463 static inline uint8* WriteBytesToArray( | 457 INL static uint8* WriteBytesToArray( |
464 field_number, const string& value, output) INL; | 458 field_number, const string& value, output); |
465 | 459 |
466 static inline uint8* WriteGroupToArray( | 460 INL static uint8* WriteGroupToArray( |
467 field_number, const MessageLite& value, output) INL; | 461 field_number, const MessageLite& value, output); |
468 static inline uint8* WriteMessageToArray( | 462 INL static uint8* WriteMessageToArray( |
469 field_number, const MessageLite& value, output) INL; | 463 field_number, const MessageLite& value, output); |
470 | 464 |
471 // Like above, but de-virtualize the call to SerializeWithCachedSizes(). The | 465 // Like above, but de-virtualize the call to SerializeWithCachedSizes(). The |
472 // pointer must point at an instance of MessageType, *not* a subclass (or | 466 // pointer must point at an instance of MessageType, *not* a subclass (or |
473 // the subclass must not override SerializeWithCachedSizes()). | 467 // the subclass must not override SerializeWithCachedSizes()). |
474 template<typename MessageType> | 468 template<typename MessageType> |
475 static inline uint8* WriteGroupNoVirtualToArray( | 469 INL static uint8* WriteGroupNoVirtualToArray( |
476 field_number, const MessageType& value, output) INL; | 470 field_number, const MessageType& value, output); |
477 template<typename MessageType> | 471 template<typename MessageType> |
478 static inline uint8* WriteMessageNoVirtualToArray( | 472 INL static uint8* WriteMessageNoVirtualToArray( |
479 field_number, const MessageType& value, output) INL; | 473 field_number, const MessageType& value, output); |
480 | 474 |
481 #undef output | 475 #undef output |
482 #undef input | 476 #undef input |
483 #undef INL | 477 #undef INL |
484 | 478 |
485 #undef field_number | 479 #undef field_number |
486 | 480 |
487 // Compute the byte size of a field. The XxSize() functions do NOT include | 481 // Compute the byte size of a field. The XxSize() functions do NOT include |
488 // the tag, so you must also call TagSize(). (This is because, for repeated | 482 // the tag, so you must also call TagSize(). (This is because, for repeated |
489 // fields, you should only call TagSize() once and multiply it by the element | 483 // fields, you should only call TagSize() once and multiply it by the element |
(...skipping 30 matching lines...) Expand all Loading... |
520 static inline int MessageSizeNoVirtual(const MessageType& value); | 514 static inline int MessageSizeNoVirtual(const MessageType& value); |
521 | 515 |
522 // Given the length of data, calculate the byte size of the data on the | 516 // Given the length of data, calculate the byte size of the data on the |
523 // wire if we encode the data as a length delimited field. | 517 // wire if we encode the data as a length delimited field. |
524 static inline int LengthDelimitedSize(int length); | 518 static inline int LengthDelimitedSize(int length); |
525 | 519 |
526 private: | 520 private: |
527 // A helper method for the repeated primitive reader. This method has | 521 // A helper method for the repeated primitive reader. This method has |
528 // optimizations for primitive types that have fixed size on the wire, and | 522 // optimizations for primitive types that have fixed size on the wire, and |
529 // can be read using potentially faster paths. | 523 // can be read using potentially faster paths. |
530 template <typename CType, enum FieldType DeclaredType> | 524 template <typename CType, enum FieldType DeclaredType> GOOGLE_ATTRIBUTE_ALWAYS
_INLINE |
531 static inline bool ReadRepeatedFixedSizePrimitive( | 525 static bool ReadRepeatedFixedSizePrimitive( |
532 int tag_size, | 526 int tag_size, |
533 uint32 tag, | 527 uint32 tag, |
534 google::protobuf::io::CodedInputStream* input, | 528 google::protobuf::io::CodedInputStream* input, |
535 RepeatedField<CType>* value) GOOGLE_ATTRIBUTE_ALWAYS_INLINE; | 529 RepeatedField<CType>* value); |
| 530 |
| 531 // Like ReadRepeatedFixedSizePrimitive but for packed primitive fields. |
| 532 template <typename CType, enum FieldType DeclaredType> GOOGLE_ATTRIBUTE_ALWAYS
_INLINE |
| 533 static bool ReadPackedFixedSizePrimitive(google::protobuf::io::CodedInputStrea
m* input, |
| 534 RepeatedField<CType>* value); |
536 | 535 |
537 static const CppType kFieldTypeToCppTypeMap[]; | 536 static const CppType kFieldTypeToCppTypeMap[]; |
538 static const WireFormatLite::WireType kWireTypeForFieldType[]; | 537 static const WireFormatLite::WireType kWireTypeForFieldType[]; |
539 | 538 |
540 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(WireFormatLite); | 539 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(WireFormatLite); |
541 }; | 540 }; |
542 | 541 |
543 // A class which deals with unknown values by saving them to an UnknownFieldSet. | 542 // A class which deals with unknown values. The default implementation just |
| 543 // discards them. WireFormat defines a subclass which writes to an |
| 544 // UnknownFieldSet. This class is used by ExtensionSet::ParseField(), since |
| 545 // ExtensionSet is part of the lite library but UnknownFieldSet is not. |
544 class LIBPROTOBUF_EXPORT FieldSkipper { | 546 class LIBPROTOBUF_EXPORT FieldSkipper { |
545 public: | 547 public: |
546 FieldSkipper(UnknownFieldSet* unknown_fields) | 548 FieldSkipper() {} |
547 : unknown_fields_(unknown_fields) {} | 549 virtual ~FieldSkipper() {} |
548 | 550 |
549 // Skip a field whose tag has already been consumed. | 551 // Skip a field whose tag has already been consumed. |
550 bool SkipField(io::CodedInputStream* input, uint32 tag); | 552 virtual bool SkipField(io::CodedInputStream* input, uint32 tag); |
551 | 553 |
552 // Skip an entire message or group, up to an end-group tag (which is consumed) | 554 // Skip an entire message or group, up to an end-group tag (which is consumed) |
553 // or end-of-stream. | 555 // or end-of-stream. |
554 bool SkipMessage(io::CodedInputStream* input); | 556 virtual bool SkipMessage(io::CodedInputStream* input); |
555 | 557 |
556 // Deal with an already-parsed unrecognized enum value. The default | 558 // Deal with an already-parsed unrecognized enum value. The default |
557 // implementation does nothing, but the UnknownFieldSet-based implementation | 559 // implementation does nothing, but the UnknownFieldSet-based implementation |
558 // saves it as an unknown varint. | 560 // saves it as an unknown varint. |
559 void SkipUnknownEnum(int field_number, int value); | 561 virtual void SkipUnknownEnum(int field_number, int value); |
| 562 }; |
| 563 |
| 564 // Subclass of FieldSkipper which saves skipped fields to a CodedOutputStream. |
| 565 |
| 566 class LIBPROTOBUF_EXPORT CodedOutputStreamFieldSkipper : public FieldSkipper { |
| 567 public: |
| 568 explicit CodedOutputStreamFieldSkipper(io::CodedOutputStream* unknown_fields) |
| 569 : unknown_fields_(unknown_fields) {} |
| 570 virtual ~CodedOutputStreamFieldSkipper() {} |
| 571 |
| 572 // implements FieldSkipper ----------------------------------------- |
| 573 virtual bool SkipField(io::CodedInputStream* input, uint32 tag); |
| 574 virtual bool SkipMessage(io::CodedInputStream* input); |
| 575 virtual void SkipUnknownEnum(int field_number, int value); |
560 | 576 |
561 protected: | 577 protected: |
562 UnknownFieldSet* unknown_fields_; | 578 io::CodedOutputStream* unknown_fields_; |
563 }; | 579 }; |
564 | 580 |
| 581 |
565 // inline methods ==================================================== | 582 // inline methods ==================================================== |
566 | 583 |
567 inline WireFormatLite::CppType | 584 inline WireFormatLite::CppType |
568 WireFormatLite::FieldTypeToCppType(FieldType type) { | 585 WireFormatLite::FieldTypeToCppType(FieldType type) { |
569 return kFieldTypeToCppTypeMap[type]; | 586 return kFieldTypeToCppTypeMap[type]; |
570 } | 587 } |
571 | 588 |
572 inline uint32 WireFormatLite::MakeTag(int field_number, WireType type) { | 589 inline uint32 WireFormatLite::MakeTag(int field_number, WireType type) { |
573 return GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(field_number, type); | 590 return GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(field_number, type); |
574 } | 591 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
636 // -2 -> 3 | 653 // -2 -> 3 |
637 // ... -> ... | 654 // ... -> ... |
638 // 2147483647 -> 4294967294 | 655 // 2147483647 -> 4294967294 |
639 // -2147483648 -> 4294967295 | 656 // -2147483648 -> 4294967295 |
640 // | 657 // |
641 // >> encode >> | 658 // >> encode >> |
642 // << decode << | 659 // << decode << |
643 | 660 |
644 inline uint32 WireFormatLite::ZigZagEncode32(int32 n) { | 661 inline uint32 WireFormatLite::ZigZagEncode32(int32 n) { |
645 // Note: the right-shift must be arithmetic | 662 // Note: the right-shift must be arithmetic |
646 return (n << 1) ^ (n >> 31); | 663 return (static_cast<uint32>(n) << 1) ^ (n >> 31); |
647 } | 664 } |
648 | 665 |
649 inline int32 WireFormatLite::ZigZagDecode32(uint32 n) { | 666 inline int32 WireFormatLite::ZigZagDecode32(uint32 n) { |
650 return (n >> 1) ^ -static_cast<int32>(n & 1); | 667 return (n >> 1) ^ -static_cast<int32>(n & 1); |
651 } | 668 } |
652 | 669 |
653 inline uint64 WireFormatLite::ZigZagEncode64(int64 n) { | 670 inline uint64 WireFormatLite::ZigZagEncode64(int64 n) { |
654 // Note: the right-shift must be arithmetic | 671 // Note: the right-shift must be arithmetic |
655 return (n << 1) ^ (n >> 63); | 672 return (static_cast<uint64>(n) << 1) ^ (n >> 63); |
656 } | 673 } |
657 | 674 |
658 inline int64 WireFormatLite::ZigZagDecode64(uint64 n) { | 675 inline int64 WireFormatLite::ZigZagDecode64(uint64 n) { |
659 return (n >> 1) ^ -static_cast<int64>(n & 1); | 676 return (n >> 1) ^ -static_cast<int64>(n & 1); |
660 } | 677 } |
661 | 678 |
| 679 // String is for UTF-8 text only, but, even so, ReadString() can simply |
| 680 // call ReadBytes(). |
| 681 |
| 682 inline bool WireFormatLite::ReadString(io::CodedInputStream* input, |
| 683 string* value) { |
| 684 return ReadBytes(input, value); |
| 685 } |
| 686 |
| 687 inline bool WireFormatLite::ReadString(io::CodedInputStream* input, |
| 688 string** p) { |
| 689 return ReadBytes(input, p); |
| 690 } |
| 691 |
662 } // namespace internal | 692 } // namespace internal |
663 } // namespace protobuf | 693 } // namespace protobuf |
664 | 694 |
665 } // namespace google | 695 } // namespace google |
666 #endif // GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_H__ | 696 #endif // GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_H__ |
OLD | NEW |