1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // http://code.google.com/p/protobuf/
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 // Author: kenton@google.com (Kenton Varda)
32 // wink@google.com (Wink Saville) (refactored from wire_format.h)
33 // Based on original Protocol Buffers design by
34 // Sanjay Ghemawat, Jeff Dean, and others.
36 #ifndef GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_INL_H__
37 #define GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_INL_H__
40 #include <google/protobuf/stubs/common.h>
41 #include <google/protobuf/message_lite.h>
42 #include <google/protobuf/repeated_field.h>
43 #include <google/protobuf/wire_format_lite.h>
44 #include <google/protobuf/io/coded_stream.h>
51 // Implementation details of ReadPrimitive.
54 inline bool WireFormatLite::ReadPrimitive<int32, WireFormatLite::TYPE_INT32>(
55 io::CodedInputStream* input,
58 if (!input->ReadVarint32(&temp)) return false;
59 *value = static_cast<int32>(temp);
63 inline bool WireFormatLite::ReadPrimitive<int64, WireFormatLite::TYPE_INT64>(
64 io::CodedInputStream* input,
67 if (!input->ReadVarint64(&temp)) return false;
68 *value = static_cast<int64>(temp);
72 inline bool WireFormatLite::ReadPrimitive<uint32, WireFormatLite::TYPE_UINT32>(
73 io::CodedInputStream* input,
75 return input->ReadVarint32(value);
78 inline bool WireFormatLite::ReadPrimitive<uint64, WireFormatLite::TYPE_UINT64>(
79 io::CodedInputStream* input,
81 return input->ReadVarint64(value);
84 inline bool WireFormatLite::ReadPrimitive<int32, WireFormatLite::TYPE_SINT32>(
85 io::CodedInputStream* input,
88 if (!input->ReadVarint32(&temp)) return false;
89 *value = ZigZagDecode32(temp);
93 inline bool WireFormatLite::ReadPrimitive<int64, WireFormatLite::TYPE_SINT64>(
94 io::CodedInputStream* input,
97 if (!input->ReadVarint64(&temp)) return false;
98 *value = ZigZagDecode64(temp);
102 inline bool WireFormatLite::ReadPrimitive<uint32, WireFormatLite::TYPE_FIXED32>(
103 io::CodedInputStream* input,
105 return input->ReadLittleEndian32(value);
108 inline bool WireFormatLite::ReadPrimitive<uint64, WireFormatLite::TYPE_FIXED64>(
109 io::CodedInputStream* input,
111 return input->ReadLittleEndian64(value);
114 inline bool WireFormatLite::ReadPrimitive<int32, WireFormatLite::TYPE_SFIXED32>(
115 io::CodedInputStream* input,
118 if (!input->ReadLittleEndian32(&temp)) return false;
119 *value = static_cast<int32>(temp);
123 inline bool WireFormatLite::ReadPrimitive<int64, WireFormatLite::TYPE_SFIXED64>(
124 io::CodedInputStream* input,
127 if (!input->ReadLittleEndian64(&temp)) return false;
128 *value = static_cast<int64>(temp);
132 inline bool WireFormatLite::ReadPrimitive<float, WireFormatLite::TYPE_FLOAT>(
133 io::CodedInputStream* input,
136 if (!input->ReadLittleEndian32(&temp)) return false;
137 *value = DecodeFloat(temp);
141 inline bool WireFormatLite::ReadPrimitive<double, WireFormatLite::TYPE_DOUBLE>(
142 io::CodedInputStream* input,
145 if (!input->ReadLittleEndian64(&temp)) return false;
146 *value = DecodeDouble(temp);
150 inline bool WireFormatLite::ReadPrimitive<bool, WireFormatLite::TYPE_BOOL>(
151 io::CodedInputStream* input,
154 if (!input->ReadVarint32(&temp)) return false;
159 inline bool WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>(
160 io::CodedInputStream* input,
163 if (!input->ReadVarint32(&temp)) return false;
164 *value = static_cast<int>(temp);
169 inline const uint8* WireFormatLite::ReadPrimitiveFromArray<
170 uint32, WireFormatLite::TYPE_FIXED32>(
173 return io::CodedInputStream::ReadLittleEndian32FromArray(buffer, value);
176 inline const uint8* WireFormatLite::ReadPrimitiveFromArray<
177 uint64, WireFormatLite::TYPE_FIXED64>(
180 return io::CodedInputStream::ReadLittleEndian64FromArray(buffer, value);
183 inline const uint8* WireFormatLite::ReadPrimitiveFromArray<
184 int32, WireFormatLite::TYPE_SFIXED32>(
188 buffer = io::CodedInputStream::ReadLittleEndian32FromArray(buffer, &temp);
189 *value = static_cast<int32>(temp);
193 inline const uint8* WireFormatLite::ReadPrimitiveFromArray<
194 int64, WireFormatLite::TYPE_SFIXED64>(
198 buffer = io::CodedInputStream::ReadLittleEndian64FromArray(buffer, &temp);
199 *value = static_cast<int64>(temp);
203 inline const uint8* WireFormatLite::ReadPrimitiveFromArray<
204 float, WireFormatLite::TYPE_FLOAT>(
208 buffer = io::CodedInputStream::ReadLittleEndian32FromArray(buffer, &temp);
209 *value = DecodeFloat(temp);
213 inline const uint8* WireFormatLite::ReadPrimitiveFromArray<
214 double, WireFormatLite::TYPE_DOUBLE>(
218 buffer = io::CodedInputStream::ReadLittleEndian64FromArray(buffer, &temp);
219 *value = DecodeDouble(temp);
223 template <typename CType, enum WireFormatLite::FieldType DeclaredType>
224 inline bool WireFormatLite::ReadRepeatedPrimitive(int, // tag_size, unused.
226 io::CodedInputStream* input,
227 RepeatedField<CType>* values) {
229 if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false;
231 int elements_already_reserved = values->Capacity() - values->size();
232 while (elements_already_reserved > 0 && input->ExpectTag(tag)) {
233 if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false;
234 values->AddAlreadyReserved(value);
235 elements_already_reserved--;
240 template <typename CType, enum WireFormatLite::FieldType DeclaredType>
241 inline bool WireFormatLite::ReadRepeatedFixedSizePrimitive(
244 io::CodedInputStream* input,
245 RepeatedField<CType>* values) {
246 GOOGLE_DCHECK_EQ(UInt32Size(tag), tag_size);
248 if (!ReadPrimitive<CType, DeclaredType>(input, &value))
252 // For fixed size values, repeated values can be read more quickly by
253 // reading directly from a raw array.
255 // We can get a tight loop by only reading as many elements as can be
256 // added to the RepeatedField without having to do any resizing. Additionally,
257 // we only try to read as many elements as are available from the current
258 // buffer space. Doing so avoids having to perform boundary checks when
259 // reading the value: the maximum number of elements that can be read is
260 // known outside of the loop.
261 const void* void_pointer;
263 input->GetDirectBufferPointerInline(&void_pointer, &size);
265 const uint8* buffer = reinterpret_cast<const uint8*>(void_pointer);
266 // The number of bytes each type occupies on the wire.
267 const int per_value_size = tag_size + sizeof(value);
269 int elements_available = min(values->Capacity() - values->size(),
270 size / per_value_size);
272 while (num_read < elements_available &&
273 (buffer = io::CodedInputStream::ExpectTagFromArray(
274 buffer, tag)) != NULL) {
275 buffer = ReadPrimitiveFromArray<CType, DeclaredType>(buffer, &value);
276 values->AddAlreadyReserved(value);
279 const int read_bytes = num_read * per_value_size;
280 if (read_bytes > 0) {
281 input->Skip(read_bytes);
287 // Specializations of ReadRepeatedPrimitive for the fixed size types, which use
288 // the optimized code path.
289 #define READ_REPEATED_FIXED_SIZE_PRIMITIVE(CPPTYPE, DECLARED_TYPE) \
291 inline bool WireFormatLite::ReadRepeatedPrimitive< \
292 CPPTYPE, WireFormatLite::DECLARED_TYPE>( \
295 io::CodedInputStream* input, \
296 RepeatedField<CPPTYPE>* values) { \
297 return ReadRepeatedFixedSizePrimitive< \
298 CPPTYPE, WireFormatLite::DECLARED_TYPE>( \
299 tag_size, tag, input, values); \
302 READ_REPEATED_FIXED_SIZE_PRIMITIVE(uint32, TYPE_FIXED32)
303 READ_REPEATED_FIXED_SIZE_PRIMITIVE(uint64, TYPE_FIXED64)
304 READ_REPEATED_FIXED_SIZE_PRIMITIVE(int32, TYPE_SFIXED32)
305 READ_REPEATED_FIXED_SIZE_PRIMITIVE(int64, TYPE_SFIXED64)
306 READ_REPEATED_FIXED_SIZE_PRIMITIVE(float, TYPE_FLOAT)
307 READ_REPEATED_FIXED_SIZE_PRIMITIVE(double, TYPE_DOUBLE)
309 #undef READ_REPEATED_FIXED_SIZE_PRIMITIVE
311 template <typename CType, enum WireFormatLite::FieldType DeclaredType>
312 bool WireFormatLite::ReadRepeatedPrimitiveNoInline(
315 io::CodedInputStream* input,
316 RepeatedField<CType>* value) {
317 return ReadRepeatedPrimitive<CType, DeclaredType>(
318 tag_size, tag, input, value);
321 template <typename CType, enum WireFormatLite::FieldType DeclaredType>
322 inline bool WireFormatLite::ReadPackedPrimitive(io::CodedInputStream* input,
323 RepeatedField<CType>* values) {
325 if (!input->ReadVarint32(&length)) return false;
326 io::CodedInputStream::Limit limit = input->PushLimit(length);
327 while (input->BytesUntilLimit() > 0) {
329 if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false;
332 input->PopLimit(limit);
336 template <typename CType, enum WireFormatLite::FieldType DeclaredType>
337 bool WireFormatLite::ReadPackedPrimitiveNoInline(io::CodedInputStream* input,
338 RepeatedField<CType>* values) {
339 return ReadPackedPrimitive<CType, DeclaredType>(input, values);
343 inline bool WireFormatLite::ReadGroup(int field_number,
344 io::CodedInputStream* input,
345 MessageLite* value) {
346 if (!input->IncrementRecursionDepth()) return false;
347 if (!value->MergePartialFromCodedStream(input)) return false;
348 input->DecrementRecursionDepth();
349 // Make sure the last thing read was an end tag for this group.
350 if (!input->LastTagWas(MakeTag(field_number, WIRETYPE_END_GROUP))) {
355 inline bool WireFormatLite::ReadMessage(io::CodedInputStream* input,
356 MessageLite* value) {
358 if (!input->ReadVarint32(&length)) return false;
359 if (!input->IncrementRecursionDepth()) return false;
360 io::CodedInputStream::Limit limit = input->PushLimit(length);
361 if (!value->MergePartialFromCodedStream(input)) return false;
362 // Make sure that parsing stopped when the limit was hit, not at an endgroup
364 if (!input->ConsumedEntireMessage()) return false;
365 input->PopLimit(limit);
366 input->DecrementRecursionDepth();
370 // We name the template parameter something long and extremely unlikely to occur
371 // elsewhere because a *qualified* member access expression designed to avoid
372 // virtual dispatch, C++03 [basic.lookup.classref] 3.4.5/4 requires that the
373 // name of the qualifying class to be looked up both in the context of the full
374 // expression (finding the template parameter) and in the context of the object
375 // whose member we are accessing. This could potentially find a nested type
376 // within that object. The standard goes on to require these names to refer to
377 // the same entity, which this collision would violate. The lack of a safe way
378 // to avoid this collision appears to be a defect in the standard, but until it
379 // is corrected, we choose the name to avoid accidental collisions.
380 template<typename MessageType_WorkAroundCppLookupDefect>
381 inline bool WireFormatLite::ReadGroupNoVirtual(
382 int field_number, io::CodedInputStream* input,
383 MessageType_WorkAroundCppLookupDefect* value) {
384 if (!input->IncrementRecursionDepth()) return false;
386 MessageType_WorkAroundCppLookupDefect::MergePartialFromCodedStream(input))
388 input->DecrementRecursionDepth();
389 // Make sure the last thing read was an end tag for this group.
390 if (!input->LastTagWas(MakeTag(field_number, WIRETYPE_END_GROUP))) {
395 template<typename MessageType_WorkAroundCppLookupDefect>
396 inline bool WireFormatLite::ReadMessageNoVirtual(
397 io::CodedInputStream* input, MessageType_WorkAroundCppLookupDefect* value) {
399 if (!input->ReadVarint32(&length)) return false;
400 if (!input->IncrementRecursionDepth()) return false;
401 io::CodedInputStream::Limit limit = input->PushLimit(length);
403 MessageType_WorkAroundCppLookupDefect::MergePartialFromCodedStream(input))
405 // Make sure that parsing stopped when the limit was hit, not at an endgroup
407 if (!input->ConsumedEntireMessage()) return false;
408 input->PopLimit(limit);
409 input->DecrementRecursionDepth();
413 // ===================================================================
415 inline void WireFormatLite::WriteTag(int field_number, WireType type,
416 io::CodedOutputStream* output) {
417 output->WriteTag(MakeTag(field_number, type));
420 inline void WireFormatLite::WriteInt32NoTag(int32 value,
421 io::CodedOutputStream* output) {
422 output->WriteVarint32SignExtended(value);
424 inline void WireFormatLite::WriteInt64NoTag(int64 value,
425 io::CodedOutputStream* output) {
426 output->WriteVarint64(static_cast<uint64>(value));
428 inline void WireFormatLite::WriteUInt32NoTag(uint32 value,
429 io::CodedOutputStream* output) {
430 output->WriteVarint32(value);
432 inline void WireFormatLite::WriteUInt64NoTag(uint64 value,
433 io::CodedOutputStream* output) {
434 output->WriteVarint64(value);
436 inline void WireFormatLite::WriteSInt32NoTag(int32 value,
437 io::CodedOutputStream* output) {
438 output->WriteVarint32(ZigZagEncode32(value));
440 inline void WireFormatLite::WriteSInt64NoTag(int64 value,
441 io::CodedOutputStream* output) {
442 output->WriteVarint64(ZigZagEncode64(value));
444 inline void WireFormatLite::WriteFixed32NoTag(uint32 value,
445 io::CodedOutputStream* output) {
446 output->WriteLittleEndian32(value);
448 inline void WireFormatLite::WriteFixed64NoTag(uint64 value,
449 io::CodedOutputStream* output) {
450 output->WriteLittleEndian64(value);
452 inline void WireFormatLite::WriteSFixed32NoTag(int32 value,
453 io::CodedOutputStream* output) {
454 output->WriteLittleEndian32(static_cast<uint32>(value));
456 inline void WireFormatLite::WriteSFixed64NoTag(int64 value,
457 io::CodedOutputStream* output) {
458 output->WriteLittleEndian64(static_cast<uint64>(value));
460 inline void WireFormatLite::WriteFloatNoTag(float value,
461 io::CodedOutputStream* output) {
462 output->WriteLittleEndian32(EncodeFloat(value));
464 inline void WireFormatLite::WriteDoubleNoTag(double value,
465 io::CodedOutputStream* output) {
466 output->WriteLittleEndian64(EncodeDouble(value));
468 inline void WireFormatLite::WriteBoolNoTag(bool value,
469 io::CodedOutputStream* output) {
470 output->WriteVarint32(value ? 1 : 0);
472 inline void WireFormatLite::WriteEnumNoTag(int value,
473 io::CodedOutputStream* output) {
474 output->WriteVarint32SignExtended(value);
477 // See comment on ReadGroupNoVirtual to understand the need for this template
479 template<typename MessageType_WorkAroundCppLookupDefect>
480 inline void WireFormatLite::WriteGroupNoVirtual(
481 int field_number, const MessageType_WorkAroundCppLookupDefect& value,
482 io::CodedOutputStream* output) {
483 WriteTag(field_number, WIRETYPE_START_GROUP, output);
484 value.MessageType_WorkAroundCppLookupDefect::SerializeWithCachedSizes(output);
485 WriteTag(field_number, WIRETYPE_END_GROUP, output);
487 template<typename MessageType_WorkAroundCppLookupDefect>
488 inline void WireFormatLite::WriteMessageNoVirtual(
489 int field_number, const MessageType_WorkAroundCppLookupDefect& value,
490 io::CodedOutputStream* output) {
491 WriteTag(field_number, WIRETYPE_LENGTH_DELIMITED, output);
492 output->WriteVarint32(
493 value.MessageType_WorkAroundCppLookupDefect::GetCachedSize());
494 value.MessageType_WorkAroundCppLookupDefect::SerializeWithCachedSizes(output);
497 // ===================================================================
499 inline uint8* WireFormatLite::WriteTagToArray(int field_number,
502 return io::CodedOutputStream::WriteTagToArray(MakeTag(field_number, type),
506 inline uint8* WireFormatLite::WriteInt32NoTagToArray(int32 value,
508 return io::CodedOutputStream::WriteVarint32SignExtendedToArray(value, target);
510 inline uint8* WireFormatLite::WriteInt64NoTagToArray(int64 value,
512 return io::CodedOutputStream::WriteVarint64ToArray(
513 static_cast<uint64>(value), target);
515 inline uint8* WireFormatLite::WriteUInt32NoTagToArray(uint32 value,
517 return io::CodedOutputStream::WriteVarint32ToArray(value, target);
519 inline uint8* WireFormatLite::WriteUInt64NoTagToArray(uint64 value,
521 return io::CodedOutputStream::WriteVarint64ToArray(value, target);
523 inline uint8* WireFormatLite::WriteSInt32NoTagToArray(int32 value,
525 return io::CodedOutputStream::WriteVarint32ToArray(ZigZagEncode32(value),
528 inline uint8* WireFormatLite::WriteSInt64NoTagToArray(int64 value,
530 return io::CodedOutputStream::WriteVarint64ToArray(ZigZagEncode64(value),
533 inline uint8* WireFormatLite::WriteFixed32NoTagToArray(uint32 value,
535 return io::CodedOutputStream::WriteLittleEndian32ToArray(value, target);
537 inline uint8* WireFormatLite::WriteFixed64NoTagToArray(uint64 value,
539 return io::CodedOutputStream::WriteLittleEndian64ToArray(value, target);
541 inline uint8* WireFormatLite::WriteSFixed32NoTagToArray(int32 value,
543 return io::CodedOutputStream::WriteLittleEndian32ToArray(
544 static_cast<uint32>(value), target);
546 inline uint8* WireFormatLite::WriteSFixed64NoTagToArray(int64 value,
548 return io::CodedOutputStream::WriteLittleEndian64ToArray(
549 static_cast<uint64>(value), target);
551 inline uint8* WireFormatLite::WriteFloatNoTagToArray(float value,
553 return io::CodedOutputStream::WriteLittleEndian32ToArray(EncodeFloat(value),
556 inline uint8* WireFormatLite::WriteDoubleNoTagToArray(double value,
558 return io::CodedOutputStream::WriteLittleEndian64ToArray(EncodeDouble(value),
561 inline uint8* WireFormatLite::WriteBoolNoTagToArray(bool value,
563 return io::CodedOutputStream::WriteVarint32ToArray(value ? 1 : 0, target);
565 inline uint8* WireFormatLite::WriteEnumNoTagToArray(int value,
567 return io::CodedOutputStream::WriteVarint32SignExtendedToArray(value, target);
570 inline uint8* WireFormatLite::WriteInt32ToArray(int field_number,
573 target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
574 return WriteInt32NoTagToArray(value, target);
576 inline uint8* WireFormatLite::WriteInt64ToArray(int field_number,
579 target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
580 return WriteInt64NoTagToArray(value, target);
582 inline uint8* WireFormatLite::WriteUInt32ToArray(int field_number,
585 target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
586 return WriteUInt32NoTagToArray(value, target);
588 inline uint8* WireFormatLite::WriteUInt64ToArray(int field_number,
591 target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
592 return WriteUInt64NoTagToArray(value, target);
594 inline uint8* WireFormatLite::WriteSInt32ToArray(int field_number,
597 target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
598 return WriteSInt32NoTagToArray(value, target);
600 inline uint8* WireFormatLite::WriteSInt64ToArray(int field_number,
603 target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
604 return WriteSInt64NoTagToArray(value, target);
606 inline uint8* WireFormatLite::WriteFixed32ToArray(int field_number,
609 target = WriteTagToArray(field_number, WIRETYPE_FIXED32, target);
610 return WriteFixed32NoTagToArray(value, target);
612 inline uint8* WireFormatLite::WriteFixed64ToArray(int field_number,
615 target = WriteTagToArray(field_number, WIRETYPE_FIXED64, target);
616 return WriteFixed64NoTagToArray(value, target);
618 inline uint8* WireFormatLite::WriteSFixed32ToArray(int field_number,
621 target = WriteTagToArray(field_number, WIRETYPE_FIXED32, target);
622 return WriteSFixed32NoTagToArray(value, target);
624 inline uint8* WireFormatLite::WriteSFixed64ToArray(int field_number,
627 target = WriteTagToArray(field_number, WIRETYPE_FIXED64, target);
628 return WriteSFixed64NoTagToArray(value, target);
630 inline uint8* WireFormatLite::WriteFloatToArray(int field_number,
633 target = WriteTagToArray(field_number, WIRETYPE_FIXED32, target);
634 return WriteFloatNoTagToArray(value, target);
636 inline uint8* WireFormatLite::WriteDoubleToArray(int field_number,
639 target = WriteTagToArray(field_number, WIRETYPE_FIXED64, target);
640 return WriteDoubleNoTagToArray(value, target);
642 inline uint8* WireFormatLite::WriteBoolToArray(int field_number,
645 target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
646 return WriteBoolNoTagToArray(value, target);
648 inline uint8* WireFormatLite::WriteEnumToArray(int field_number,
651 target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
652 return WriteEnumNoTagToArray(value, target);
655 inline uint8* WireFormatLite::WriteStringToArray(int field_number,
658 // String is for UTF-8 text only
659 // WARNING: In wire_format.cc, both strings and bytes are handled by
660 // WriteString() to avoid code duplication. If the implementations become
661 // different, you will need to update that usage.
662 target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target);
663 target = io::CodedOutputStream::WriteVarint32ToArray(value.size(), target);
664 return io::CodedOutputStream::WriteStringToArray(value, target);
666 inline uint8* WireFormatLite::WriteBytesToArray(int field_number,
669 target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target);
670 target = io::CodedOutputStream::WriteVarint32ToArray(value.size(), target);
671 return io::CodedOutputStream::WriteStringToArray(value, target);
675 inline uint8* WireFormatLite::WriteGroupToArray(int field_number,
676 const MessageLite& value,
678 target = WriteTagToArray(field_number, WIRETYPE_START_GROUP, target);
679 target = value.SerializeWithCachedSizesToArray(target);
680 return WriteTagToArray(field_number, WIRETYPE_END_GROUP, target);
682 inline uint8* WireFormatLite::WriteMessageToArray(int field_number,
683 const MessageLite& value,
685 target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target);
686 target = io::CodedOutputStream::WriteVarint32ToArray(
687 value.GetCachedSize(), target);
688 return value.SerializeWithCachedSizesToArray(target);
691 // See comment on ReadGroupNoVirtual to understand the need for this template
693 template<typename MessageType_WorkAroundCppLookupDefect>
694 inline uint8* WireFormatLite::WriteGroupNoVirtualToArray(
695 int field_number, const MessageType_WorkAroundCppLookupDefect& value,
697 target = WriteTagToArray(field_number, WIRETYPE_START_GROUP, target);
698 target = value.MessageType_WorkAroundCppLookupDefect
699 ::SerializeWithCachedSizesToArray(target);
700 return WriteTagToArray(field_number, WIRETYPE_END_GROUP, target);
702 template<typename MessageType_WorkAroundCppLookupDefect>
703 inline uint8* WireFormatLite::WriteMessageNoVirtualToArray(
704 int field_number, const MessageType_WorkAroundCppLookupDefect& value,
706 target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target);
707 target = io::CodedOutputStream::WriteVarint32ToArray(
708 value.MessageType_WorkAroundCppLookupDefect::GetCachedSize(), target);
709 return value.MessageType_WorkAroundCppLookupDefect
710 ::SerializeWithCachedSizesToArray(target);
713 // ===================================================================
715 inline int WireFormatLite::Int32Size(int32 value) {
716 return io::CodedOutputStream::VarintSize32SignExtended(value);
718 inline int WireFormatLite::Int64Size(int64 value) {
719 return io::CodedOutputStream::VarintSize64(static_cast<uint64>(value));
721 inline int WireFormatLite::UInt32Size(uint32 value) {
722 return io::CodedOutputStream::VarintSize32(value);
724 inline int WireFormatLite::UInt64Size(uint64 value) {
725 return io::CodedOutputStream::VarintSize64(value);
727 inline int WireFormatLite::SInt32Size(int32 value) {
728 return io::CodedOutputStream::VarintSize32(ZigZagEncode32(value));
730 inline int WireFormatLite::SInt64Size(int64 value) {
731 return io::CodedOutputStream::VarintSize64(ZigZagEncode64(value));
733 inline int WireFormatLite::EnumSize(int value) {
734 return io::CodedOutputStream::VarintSize32SignExtended(value);
737 inline int WireFormatLite::StringSize(const string& value) {
738 return io::CodedOutputStream::VarintSize32(value.size()) +
741 inline int WireFormatLite::BytesSize(const string& value) {
742 return io::CodedOutputStream::VarintSize32(value.size()) +
747 inline int WireFormatLite::GroupSize(const MessageLite& value) {
748 return value.ByteSize();
750 inline int WireFormatLite::MessageSize(const MessageLite& value) {
751 return LengthDelimitedSize(value.ByteSize());
754 // See comment on ReadGroupNoVirtual to understand the need for this template
756 template<typename MessageType_WorkAroundCppLookupDefect>
757 inline int WireFormatLite::GroupSizeNoVirtual(
758 const MessageType_WorkAroundCppLookupDefect& value) {
759 return value.MessageType_WorkAroundCppLookupDefect::ByteSize();
761 template<typename MessageType_WorkAroundCppLookupDefect>
762 inline int WireFormatLite::MessageSizeNoVirtual(
763 const MessageType_WorkAroundCppLookupDefect& value) {
764 return LengthDelimitedSize(
765 value.MessageType_WorkAroundCppLookupDefect::ByteSize());
768 inline int WireFormatLite::LengthDelimitedSize(int length) {
769 return io::CodedOutputStream::VarintSize32(length) + length;
772 } // namespace internal
773 } // namespace protobuf
775 } // namespace google
776 #endif // GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_INL_H__