toolkit/components/protobuf/google/protobuf/wire_format_lite_inl.h

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 // Protocol Buffers - Google's data interchange format
michael@0 2 // Copyright 2008 Google Inc. All rights reserved.
michael@0 3 // http://code.google.com/p/protobuf/
michael@0 4 //
michael@0 5 // Redistribution and use in source and binary forms, with or without
michael@0 6 // modification, are permitted provided that the following conditions are
michael@0 7 // met:
michael@0 8 //
michael@0 9 // * Redistributions of source code must retain the above copyright
michael@0 10 // notice, this list of conditions and the following disclaimer.
michael@0 11 // * Redistributions in binary form must reproduce the above
michael@0 12 // copyright notice, this list of conditions and the following disclaimer
michael@0 13 // in the documentation and/or other materials provided with the
michael@0 14 // distribution.
michael@0 15 // * Neither the name of Google Inc. nor the names of its
michael@0 16 // contributors may be used to endorse or promote products derived from
michael@0 17 // this software without specific prior written permission.
michael@0 18 //
michael@0 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 30
michael@0 31 // Author: kenton@google.com (Kenton Varda)
michael@0 32 // wink@google.com (Wink Saville) (refactored from wire_format.h)
michael@0 33 // Based on original Protocol Buffers design by
michael@0 34 // Sanjay Ghemawat, Jeff Dean, and others.
michael@0 35
michael@0 36 #ifndef GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_INL_H__
michael@0 37 #define GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_INL_H__
michael@0 38
michael@0 39 #include <algorithm>
michael@0 40 #include <string>
michael@0 41 #include <google/protobuf/stubs/common.h>
michael@0 42 #include <google/protobuf/message_lite.h>
michael@0 43 #include <google/protobuf/repeated_field.h>
michael@0 44 #include <google/protobuf/wire_format_lite.h>
michael@0 45 #include <google/protobuf/generated_message_util.h>
michael@0 46 #include <google/protobuf/io/coded_stream.h>
michael@0 47
michael@0 48
michael@0 49 namespace google {
michael@0 50 namespace protobuf {
michael@0 51 namespace internal {
michael@0 52
michael@0 53 // Implementation details of ReadPrimitive.
michael@0 54
michael@0 55 template <>
michael@0 56 inline bool WireFormatLite::ReadPrimitive<int32, WireFormatLite::TYPE_INT32>(
michael@0 57 io::CodedInputStream* input,
michael@0 58 int32* value) {
michael@0 59 uint32 temp;
michael@0 60 if (!input->ReadVarint32(&temp)) return false;
michael@0 61 *value = static_cast<int32>(temp);
michael@0 62 return true;
michael@0 63 }
michael@0 64 template <>
michael@0 65 inline bool WireFormatLite::ReadPrimitive<int64, WireFormatLite::TYPE_INT64>(
michael@0 66 io::CodedInputStream* input,
michael@0 67 int64* value) {
michael@0 68 uint64 temp;
michael@0 69 if (!input->ReadVarint64(&temp)) return false;
michael@0 70 *value = static_cast<int64>(temp);
michael@0 71 return true;
michael@0 72 }
michael@0 73 template <>
michael@0 74 inline bool WireFormatLite::ReadPrimitive<uint32, WireFormatLite::TYPE_UINT32>(
michael@0 75 io::CodedInputStream* input,
michael@0 76 uint32* value) {
michael@0 77 return input->ReadVarint32(value);
michael@0 78 }
michael@0 79 template <>
michael@0 80 inline bool WireFormatLite::ReadPrimitive<uint64, WireFormatLite::TYPE_UINT64>(
michael@0 81 io::CodedInputStream* input,
michael@0 82 uint64* value) {
michael@0 83 return input->ReadVarint64(value);
michael@0 84 }
michael@0 85 template <>
michael@0 86 inline bool WireFormatLite::ReadPrimitive<int32, WireFormatLite::TYPE_SINT32>(
michael@0 87 io::CodedInputStream* input,
michael@0 88 int32* value) {
michael@0 89 uint32 temp;
michael@0 90 if (!input->ReadVarint32(&temp)) return false;
michael@0 91 *value = ZigZagDecode32(temp);
michael@0 92 return true;
michael@0 93 }
michael@0 94 template <>
michael@0 95 inline bool WireFormatLite::ReadPrimitive<int64, WireFormatLite::TYPE_SINT64>(
michael@0 96 io::CodedInputStream* input,
michael@0 97 int64* value) {
michael@0 98 uint64 temp;
michael@0 99 if (!input->ReadVarint64(&temp)) return false;
michael@0 100 *value = ZigZagDecode64(temp);
michael@0 101 return true;
michael@0 102 }
michael@0 103 template <>
michael@0 104 inline bool WireFormatLite::ReadPrimitive<uint32, WireFormatLite::TYPE_FIXED32>(
michael@0 105 io::CodedInputStream* input,
michael@0 106 uint32* value) {
michael@0 107 return input->ReadLittleEndian32(value);
michael@0 108 }
michael@0 109 template <>
michael@0 110 inline bool WireFormatLite::ReadPrimitive<uint64, WireFormatLite::TYPE_FIXED64>(
michael@0 111 io::CodedInputStream* input,
michael@0 112 uint64* value) {
michael@0 113 return input->ReadLittleEndian64(value);
michael@0 114 }
michael@0 115 template <>
michael@0 116 inline bool WireFormatLite::ReadPrimitive<int32, WireFormatLite::TYPE_SFIXED32>(
michael@0 117 io::CodedInputStream* input,
michael@0 118 int32* value) {
michael@0 119 uint32 temp;
michael@0 120 if (!input->ReadLittleEndian32(&temp)) return false;
michael@0 121 *value = static_cast<int32>(temp);
michael@0 122 return true;
michael@0 123 }
michael@0 124 template <>
michael@0 125 inline bool WireFormatLite::ReadPrimitive<int64, WireFormatLite::TYPE_SFIXED64>(
michael@0 126 io::CodedInputStream* input,
michael@0 127 int64* value) {
michael@0 128 uint64 temp;
michael@0 129 if (!input->ReadLittleEndian64(&temp)) return false;
michael@0 130 *value = static_cast<int64>(temp);
michael@0 131 return true;
michael@0 132 }
michael@0 133 template <>
michael@0 134 inline bool WireFormatLite::ReadPrimitive<float, WireFormatLite::TYPE_FLOAT>(
michael@0 135 io::CodedInputStream* input,
michael@0 136 float* value) {
michael@0 137 uint32 temp;
michael@0 138 if (!input->ReadLittleEndian32(&temp)) return false;
michael@0 139 *value = DecodeFloat(temp);
michael@0 140 return true;
michael@0 141 }
michael@0 142 template <>
michael@0 143 inline bool WireFormatLite::ReadPrimitive<double, WireFormatLite::TYPE_DOUBLE>(
michael@0 144 io::CodedInputStream* input,
michael@0 145 double* value) {
michael@0 146 uint64 temp;
michael@0 147 if (!input->ReadLittleEndian64(&temp)) return false;
michael@0 148 *value = DecodeDouble(temp);
michael@0 149 return true;
michael@0 150 }
michael@0 151 template <>
michael@0 152 inline bool WireFormatLite::ReadPrimitive<bool, WireFormatLite::TYPE_BOOL>(
michael@0 153 io::CodedInputStream* input,
michael@0 154 bool* value) {
michael@0 155 uint32 temp;
michael@0 156 if (!input->ReadVarint32(&temp)) return false;
michael@0 157 *value = temp != 0;
michael@0 158 return true;
michael@0 159 }
michael@0 160 template <>
michael@0 161 inline bool WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>(
michael@0 162 io::CodedInputStream* input,
michael@0 163 int* value) {
michael@0 164 uint32 temp;
michael@0 165 if (!input->ReadVarint32(&temp)) return false;
michael@0 166 *value = static_cast<int>(temp);
michael@0 167 return true;
michael@0 168 }
michael@0 169
michael@0 170 template <>
michael@0 171 inline const uint8* WireFormatLite::ReadPrimitiveFromArray<
michael@0 172 uint32, WireFormatLite::TYPE_FIXED32>(
michael@0 173 const uint8* buffer,
michael@0 174 uint32* value) {
michael@0 175 return io::CodedInputStream::ReadLittleEndian32FromArray(buffer, value);
michael@0 176 }
michael@0 177 template <>
michael@0 178 inline const uint8* WireFormatLite::ReadPrimitiveFromArray<
michael@0 179 uint64, WireFormatLite::TYPE_FIXED64>(
michael@0 180 const uint8* buffer,
michael@0 181 uint64* value) {
michael@0 182 return io::CodedInputStream::ReadLittleEndian64FromArray(buffer, value);
michael@0 183 }
michael@0 184 template <>
michael@0 185 inline const uint8* WireFormatLite::ReadPrimitiveFromArray<
michael@0 186 int32, WireFormatLite::TYPE_SFIXED32>(
michael@0 187 const uint8* buffer,
michael@0 188 int32* value) {
michael@0 189 uint32 temp;
michael@0 190 buffer = io::CodedInputStream::ReadLittleEndian32FromArray(buffer, &temp);
michael@0 191 *value = static_cast<int32>(temp);
michael@0 192 return buffer;
michael@0 193 }
michael@0 194 template <>
michael@0 195 inline const uint8* WireFormatLite::ReadPrimitiveFromArray<
michael@0 196 int64, WireFormatLite::TYPE_SFIXED64>(
michael@0 197 const uint8* buffer,
michael@0 198 int64* value) {
michael@0 199 uint64 temp;
michael@0 200 buffer = io::CodedInputStream::ReadLittleEndian64FromArray(buffer, &temp);
michael@0 201 *value = static_cast<int64>(temp);
michael@0 202 return buffer;
michael@0 203 }
michael@0 204 template <>
michael@0 205 inline const uint8* WireFormatLite::ReadPrimitiveFromArray<
michael@0 206 float, WireFormatLite::TYPE_FLOAT>(
michael@0 207 const uint8* buffer,
michael@0 208 float* value) {
michael@0 209 uint32 temp;
michael@0 210 buffer = io::CodedInputStream::ReadLittleEndian32FromArray(buffer, &temp);
michael@0 211 *value = DecodeFloat(temp);
michael@0 212 return buffer;
michael@0 213 }
michael@0 214 template <>
michael@0 215 inline const uint8* WireFormatLite::ReadPrimitiveFromArray<
michael@0 216 double, WireFormatLite::TYPE_DOUBLE>(
michael@0 217 const uint8* buffer,
michael@0 218 double* value) {
michael@0 219 uint64 temp;
michael@0 220 buffer = io::CodedInputStream::ReadLittleEndian64FromArray(buffer, &temp);
michael@0 221 *value = DecodeDouble(temp);
michael@0 222 return buffer;
michael@0 223 }
michael@0 224
michael@0 225 template <typename CType, enum WireFormatLite::FieldType DeclaredType>
michael@0 226 inline bool WireFormatLite::ReadRepeatedPrimitive(int, // tag_size, unused.
michael@0 227 uint32 tag,
michael@0 228 io::CodedInputStream* input,
michael@0 229 RepeatedField<CType>* values) {
michael@0 230 CType value;
michael@0 231 if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false;
michael@0 232 values->Add(value);
michael@0 233 int elements_already_reserved = values->Capacity() - values->size();
michael@0 234 while (elements_already_reserved > 0 && input->ExpectTag(tag)) {
michael@0 235 if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false;
michael@0 236 values->AddAlreadyReserved(value);
michael@0 237 elements_already_reserved--;
michael@0 238 }
michael@0 239 return true;
michael@0 240 }
michael@0 241
michael@0 242 template <typename CType, enum WireFormatLite::FieldType DeclaredType>
michael@0 243 inline bool WireFormatLite::ReadRepeatedFixedSizePrimitive(
michael@0 244 int tag_size,
michael@0 245 uint32 tag,
michael@0 246 io::CodedInputStream* input,
michael@0 247 RepeatedField<CType>* values) {
michael@0 248 GOOGLE_DCHECK_EQ(UInt32Size(tag), tag_size);
michael@0 249 CType value;
michael@0 250 if (!ReadPrimitive<CType, DeclaredType>(input, &value))
michael@0 251 return false;
michael@0 252 values->Add(value);
michael@0 253
michael@0 254 // For fixed size values, repeated values can be read more quickly by
michael@0 255 // reading directly from a raw array.
michael@0 256 //
michael@0 257 // We can get a tight loop by only reading as many elements as can be
michael@0 258 // added to the RepeatedField without having to do any resizing. Additionally,
michael@0 259 // we only try to read as many elements as are available from the current
michael@0 260 // buffer space. Doing so avoids having to perform boundary checks when
michael@0 261 // reading the value: the maximum number of elements that can be read is
michael@0 262 // known outside of the loop.
michael@0 263 const void* void_pointer;
michael@0 264 int size;
michael@0 265 input->GetDirectBufferPointerInline(&void_pointer, &size);
michael@0 266 if (size > 0) {
michael@0 267 const uint8* buffer = reinterpret_cast<const uint8*>(void_pointer);
michael@0 268 // The number of bytes each type occupies on the wire.
michael@0 269 const int per_value_size = tag_size + sizeof(value);
michael@0 270
michael@0 271 int elements_available = min(values->Capacity() - values->size(),
michael@0 272 size / per_value_size);
michael@0 273 int num_read = 0;
michael@0 274 while (num_read < elements_available &&
michael@0 275 (buffer = io::CodedInputStream::ExpectTagFromArray(
michael@0 276 buffer, tag)) != NULL) {
michael@0 277 buffer = ReadPrimitiveFromArray<CType, DeclaredType>(buffer, &value);
michael@0 278 values->AddAlreadyReserved(value);
michael@0 279 ++num_read;
michael@0 280 }
michael@0 281 const int read_bytes = num_read * per_value_size;
michael@0 282 if (read_bytes > 0) {
michael@0 283 input->Skip(read_bytes);
michael@0 284 }
michael@0 285 }
michael@0 286 return true;
michael@0 287 }
michael@0 288
michael@0 289 // Specializations of ReadRepeatedPrimitive for the fixed size types, which use
michael@0 290 // the optimized code path.
michael@0 291 #define READ_REPEATED_FIXED_SIZE_PRIMITIVE(CPPTYPE, DECLARED_TYPE) \
michael@0 292 template <> \
michael@0 293 inline bool WireFormatLite::ReadRepeatedPrimitive< \
michael@0 294 CPPTYPE, WireFormatLite::DECLARED_TYPE>( \
michael@0 295 int tag_size, \
michael@0 296 uint32 tag, \
michael@0 297 io::CodedInputStream* input, \
michael@0 298 RepeatedField<CPPTYPE>* values) { \
michael@0 299 return ReadRepeatedFixedSizePrimitive< \
michael@0 300 CPPTYPE, WireFormatLite::DECLARED_TYPE>( \
michael@0 301 tag_size, tag, input, values); \
michael@0 302 }
michael@0 303
michael@0 304 READ_REPEATED_FIXED_SIZE_PRIMITIVE(uint32, TYPE_FIXED32);
michael@0 305 READ_REPEATED_FIXED_SIZE_PRIMITIVE(uint64, TYPE_FIXED64);
michael@0 306 READ_REPEATED_FIXED_SIZE_PRIMITIVE(int32, TYPE_SFIXED32);
michael@0 307 READ_REPEATED_FIXED_SIZE_PRIMITIVE(int64, TYPE_SFIXED64);
michael@0 308 READ_REPEATED_FIXED_SIZE_PRIMITIVE(float, TYPE_FLOAT);
michael@0 309 READ_REPEATED_FIXED_SIZE_PRIMITIVE(double, TYPE_DOUBLE);
michael@0 310
michael@0 311 #undef READ_REPEATED_FIXED_SIZE_PRIMITIVE
michael@0 312
michael@0 313 template <typename CType, enum WireFormatLite::FieldType DeclaredType>
michael@0 314 bool WireFormatLite::ReadRepeatedPrimitiveNoInline(
michael@0 315 int tag_size,
michael@0 316 uint32 tag,
michael@0 317 io::CodedInputStream* input,
michael@0 318 RepeatedField<CType>* value) {
michael@0 319 return ReadRepeatedPrimitive<CType, DeclaredType>(
michael@0 320 tag_size, tag, input, value);
michael@0 321 }
michael@0 322
michael@0 323 template <typename CType, enum WireFormatLite::FieldType DeclaredType>
michael@0 324 inline bool WireFormatLite::ReadPackedPrimitive(io::CodedInputStream* input,
michael@0 325 RepeatedField<CType>* values) {
michael@0 326 uint32 length;
michael@0 327 if (!input->ReadVarint32(&length)) return false;
michael@0 328 io::CodedInputStream::Limit limit = input->PushLimit(length);
michael@0 329 while (input->BytesUntilLimit() > 0) {
michael@0 330 CType value;
michael@0 331 if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false;
michael@0 332 values->Add(value);
michael@0 333 }
michael@0 334 input->PopLimit(limit);
michael@0 335 return true;
michael@0 336 }
michael@0 337
michael@0 338 template <typename CType, enum WireFormatLite::FieldType DeclaredType>
michael@0 339 bool WireFormatLite::ReadPackedPrimitiveNoInline(io::CodedInputStream* input,
michael@0 340 RepeatedField<CType>* values) {
michael@0 341 return ReadPackedPrimitive<CType, DeclaredType>(input, values);
michael@0 342 }
michael@0 343
michael@0 344
michael@0 345 inline bool WireFormatLite::ReadGroup(int field_number,
michael@0 346 io::CodedInputStream* input,
michael@0 347 MessageLite* value) {
michael@0 348 if (!input->IncrementRecursionDepth()) return false;
michael@0 349 if (!value->MergePartialFromCodedStream(input)) return false;
michael@0 350 input->DecrementRecursionDepth();
michael@0 351 // Make sure the last thing read was an end tag for this group.
michael@0 352 if (!input->LastTagWas(MakeTag(field_number, WIRETYPE_END_GROUP))) {
michael@0 353 return false;
michael@0 354 }
michael@0 355 return true;
michael@0 356 }
michael@0 357 inline bool WireFormatLite::ReadMessage(io::CodedInputStream* input,
michael@0 358 MessageLite* value) {
michael@0 359 uint32 length;
michael@0 360 if (!input->ReadVarint32(&length)) return false;
michael@0 361 if (!input->IncrementRecursionDepth()) return false;
michael@0 362 io::CodedInputStream::Limit limit = input->PushLimit(length);
michael@0 363 if (!value->MergePartialFromCodedStream(input)) return false;
michael@0 364 // Make sure that parsing stopped when the limit was hit, not at an endgroup
michael@0 365 // tag.
michael@0 366 if (!input->ConsumedEntireMessage()) return false;
michael@0 367 input->PopLimit(limit);
michael@0 368 input->DecrementRecursionDepth();
michael@0 369 return true;
michael@0 370 }
michael@0 371
michael@0 372 // We name the template parameter something long and extremely unlikely to occur
michael@0 373 // elsewhere because a *qualified* member access expression designed to avoid
michael@0 374 // virtual dispatch, C++03 [basic.lookup.classref] 3.4.5/4 requires that the
michael@0 375 // name of the qualifying class to be looked up both in the context of the full
michael@0 376 // expression (finding the template parameter) and in the context of the object
michael@0 377 // whose member we are accessing. This could potentially find a nested type
michael@0 378 // within that object. The standard goes on to require these names to refer to
michael@0 379 // the same entity, which this collision would violate. The lack of a safe way
michael@0 380 // to avoid this collision appears to be a defect in the standard, but until it
michael@0 381 // is corrected, we choose the name to avoid accidental collisions.
michael@0 382 template<typename MessageType_WorkAroundCppLookupDefect>
michael@0 383 inline bool WireFormatLite::ReadGroupNoVirtual(
michael@0 384 int field_number, io::CodedInputStream* input,
michael@0 385 MessageType_WorkAroundCppLookupDefect* value) {
michael@0 386 if (!input->IncrementRecursionDepth()) return false;
michael@0 387 if (!value->
michael@0 388 MessageType_WorkAroundCppLookupDefect::MergePartialFromCodedStream(input))
michael@0 389 return false;
michael@0 390 input->DecrementRecursionDepth();
michael@0 391 // Make sure the last thing read was an end tag for this group.
michael@0 392 if (!input->LastTagWas(MakeTag(field_number, WIRETYPE_END_GROUP))) {
michael@0 393 return false;
michael@0 394 }
michael@0 395 return true;
michael@0 396 }
michael@0 397 template<typename MessageType_WorkAroundCppLookupDefect>
michael@0 398 inline bool WireFormatLite::ReadMessageNoVirtual(
michael@0 399 io::CodedInputStream* input, MessageType_WorkAroundCppLookupDefect* value) {
michael@0 400 uint32 length;
michael@0 401 if (!input->ReadVarint32(&length)) return false;
michael@0 402 if (!input->IncrementRecursionDepth()) return false;
michael@0 403 io::CodedInputStream::Limit limit = input->PushLimit(length);
michael@0 404 if (!value->
michael@0 405 MessageType_WorkAroundCppLookupDefect::MergePartialFromCodedStream(input))
michael@0 406 return false;
michael@0 407 // Make sure that parsing stopped when the limit was hit, not at an endgroup
michael@0 408 // tag.
michael@0 409 if (!input->ConsumedEntireMessage()) return false;
michael@0 410 input->PopLimit(limit);
michael@0 411 input->DecrementRecursionDepth();
michael@0 412 return true;
michael@0 413 }
michael@0 414
michael@0 415 // ===================================================================
michael@0 416
michael@0 417 inline void WireFormatLite::WriteTag(int field_number, WireType type,
michael@0 418 io::CodedOutputStream* output) {
michael@0 419 output->WriteTag(MakeTag(field_number, type));
michael@0 420 }
michael@0 421
michael@0 422 inline void WireFormatLite::WriteInt32NoTag(int32 value,
michael@0 423 io::CodedOutputStream* output) {
michael@0 424 output->WriteVarint32SignExtended(value);
michael@0 425 }
michael@0 426 inline void WireFormatLite::WriteInt64NoTag(int64 value,
michael@0 427 io::CodedOutputStream* output) {
michael@0 428 output->WriteVarint64(static_cast<uint64>(value));
michael@0 429 }
michael@0 430 inline void WireFormatLite::WriteUInt32NoTag(uint32 value,
michael@0 431 io::CodedOutputStream* output) {
michael@0 432 output->WriteVarint32(value);
michael@0 433 }
michael@0 434 inline void WireFormatLite::WriteUInt64NoTag(uint64 value,
michael@0 435 io::CodedOutputStream* output) {
michael@0 436 output->WriteVarint64(value);
michael@0 437 }
michael@0 438 inline void WireFormatLite::WriteSInt32NoTag(int32 value,
michael@0 439 io::CodedOutputStream* output) {
michael@0 440 output->WriteVarint32(ZigZagEncode32(value));
michael@0 441 }
michael@0 442 inline void WireFormatLite::WriteSInt64NoTag(int64 value,
michael@0 443 io::CodedOutputStream* output) {
michael@0 444 output->WriteVarint64(ZigZagEncode64(value));
michael@0 445 }
michael@0 446 inline void WireFormatLite::WriteFixed32NoTag(uint32 value,
michael@0 447 io::CodedOutputStream* output) {
michael@0 448 output->WriteLittleEndian32(value);
michael@0 449 }
michael@0 450 inline void WireFormatLite::WriteFixed64NoTag(uint64 value,
michael@0 451 io::CodedOutputStream* output) {
michael@0 452 output->WriteLittleEndian64(value);
michael@0 453 }
michael@0 454 inline void WireFormatLite::WriteSFixed32NoTag(int32 value,
michael@0 455 io::CodedOutputStream* output) {
michael@0 456 output->WriteLittleEndian32(static_cast<uint32>(value));
michael@0 457 }
michael@0 458 inline void WireFormatLite::WriteSFixed64NoTag(int64 value,
michael@0 459 io::CodedOutputStream* output) {
michael@0 460 output->WriteLittleEndian64(static_cast<uint64>(value));
michael@0 461 }
michael@0 462 inline void WireFormatLite::WriteFloatNoTag(float value,
michael@0 463 io::CodedOutputStream* output) {
michael@0 464 output->WriteLittleEndian32(EncodeFloat(value));
michael@0 465 }
michael@0 466 inline void WireFormatLite::WriteDoubleNoTag(double value,
michael@0 467 io::CodedOutputStream* output) {
michael@0 468 output->WriteLittleEndian64(EncodeDouble(value));
michael@0 469 }
michael@0 470 inline void WireFormatLite::WriteBoolNoTag(bool value,
michael@0 471 io::CodedOutputStream* output) {
michael@0 472 output->WriteVarint32(value ? 1 : 0);
michael@0 473 }
michael@0 474 inline void WireFormatLite::WriteEnumNoTag(int value,
michael@0 475 io::CodedOutputStream* output) {
michael@0 476 output->WriteVarint32SignExtended(value);
michael@0 477 }
michael@0 478
michael@0 479 // See comment on ReadGroupNoVirtual to understand the need for this template
michael@0 480 // parameter name.
michael@0 481 template<typename MessageType_WorkAroundCppLookupDefect>
michael@0 482 inline void WireFormatLite::WriteGroupNoVirtual(
michael@0 483 int field_number, const MessageType_WorkAroundCppLookupDefect& value,
michael@0 484 io::CodedOutputStream* output) {
michael@0 485 WriteTag(field_number, WIRETYPE_START_GROUP, output);
michael@0 486 value.MessageType_WorkAroundCppLookupDefect::SerializeWithCachedSizes(output);
michael@0 487 WriteTag(field_number, WIRETYPE_END_GROUP, output);
michael@0 488 }
michael@0 489 template<typename MessageType_WorkAroundCppLookupDefect>
michael@0 490 inline void WireFormatLite::WriteMessageNoVirtual(
michael@0 491 int field_number, const MessageType_WorkAroundCppLookupDefect& value,
michael@0 492 io::CodedOutputStream* output) {
michael@0 493 WriteTag(field_number, WIRETYPE_LENGTH_DELIMITED, output);
michael@0 494 output->WriteVarint32(
michael@0 495 value.MessageType_WorkAroundCppLookupDefect::GetCachedSize());
michael@0 496 value.MessageType_WorkAroundCppLookupDefect::SerializeWithCachedSizes(output);
michael@0 497 }
michael@0 498
michael@0 499 // ===================================================================
michael@0 500
michael@0 501 inline uint8* WireFormatLite::WriteTagToArray(int field_number,
michael@0 502 WireType type,
michael@0 503 uint8* target) {
michael@0 504 return io::CodedOutputStream::WriteTagToArray(MakeTag(field_number, type),
michael@0 505 target);
michael@0 506 }
michael@0 507
michael@0 508 inline uint8* WireFormatLite::WriteInt32NoTagToArray(int32 value,
michael@0 509 uint8* target) {
michael@0 510 return io::CodedOutputStream::WriteVarint32SignExtendedToArray(value, target);
michael@0 511 }
michael@0 512 inline uint8* WireFormatLite::WriteInt64NoTagToArray(int64 value,
michael@0 513 uint8* target) {
michael@0 514 return io::CodedOutputStream::WriteVarint64ToArray(
michael@0 515 static_cast<uint64>(value), target);
michael@0 516 }
michael@0 517 inline uint8* WireFormatLite::WriteUInt32NoTagToArray(uint32 value,
michael@0 518 uint8* target) {
michael@0 519 return io::CodedOutputStream::WriteVarint32ToArray(value, target);
michael@0 520 }
michael@0 521 inline uint8* WireFormatLite::WriteUInt64NoTagToArray(uint64 value,
michael@0 522 uint8* target) {
michael@0 523 return io::CodedOutputStream::WriteVarint64ToArray(value, target);
michael@0 524 }
michael@0 525 inline uint8* WireFormatLite::WriteSInt32NoTagToArray(int32 value,
michael@0 526 uint8* target) {
michael@0 527 return io::CodedOutputStream::WriteVarint32ToArray(ZigZagEncode32(value),
michael@0 528 target);
michael@0 529 }
michael@0 530 inline uint8* WireFormatLite::WriteSInt64NoTagToArray(int64 value,
michael@0 531 uint8* target) {
michael@0 532 return io::CodedOutputStream::WriteVarint64ToArray(ZigZagEncode64(value),
michael@0 533 target);
michael@0 534 }
michael@0 535 inline uint8* WireFormatLite::WriteFixed32NoTagToArray(uint32 value,
michael@0 536 uint8* target) {
michael@0 537 return io::CodedOutputStream::WriteLittleEndian32ToArray(value, target);
michael@0 538 }
michael@0 539 inline uint8* WireFormatLite::WriteFixed64NoTagToArray(uint64 value,
michael@0 540 uint8* target) {
michael@0 541 return io::CodedOutputStream::WriteLittleEndian64ToArray(value, target);
michael@0 542 }
michael@0 543 inline uint8* WireFormatLite::WriteSFixed32NoTagToArray(int32 value,
michael@0 544 uint8* target) {
michael@0 545 return io::CodedOutputStream::WriteLittleEndian32ToArray(
michael@0 546 static_cast<uint32>(value), target);
michael@0 547 }
michael@0 548 inline uint8* WireFormatLite::WriteSFixed64NoTagToArray(int64 value,
michael@0 549 uint8* target) {
michael@0 550 return io::CodedOutputStream::WriteLittleEndian64ToArray(
michael@0 551 static_cast<uint64>(value), target);
michael@0 552 }
michael@0 553 inline uint8* WireFormatLite::WriteFloatNoTagToArray(float value,
michael@0 554 uint8* target) {
michael@0 555 return io::CodedOutputStream::WriteLittleEndian32ToArray(EncodeFloat(value),
michael@0 556 target);
michael@0 557 }
michael@0 558 inline uint8* WireFormatLite::WriteDoubleNoTagToArray(double value,
michael@0 559 uint8* target) {
michael@0 560 return io::CodedOutputStream::WriteLittleEndian64ToArray(EncodeDouble(value),
michael@0 561 target);
michael@0 562 }
michael@0 563 inline uint8* WireFormatLite::WriteBoolNoTagToArray(bool value,
michael@0 564 uint8* target) {
michael@0 565 return io::CodedOutputStream::WriteVarint32ToArray(value ? 1 : 0, target);
michael@0 566 }
michael@0 567 inline uint8* WireFormatLite::WriteEnumNoTagToArray(int value,
michael@0 568 uint8* target) {
michael@0 569 return io::CodedOutputStream::WriteVarint32SignExtendedToArray(value, target);
michael@0 570 }
michael@0 571
michael@0 572 inline uint8* WireFormatLite::WriteInt32ToArray(int field_number,
michael@0 573 int32 value,
michael@0 574 uint8* target) {
michael@0 575 target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
michael@0 576 return WriteInt32NoTagToArray(value, target);
michael@0 577 }
michael@0 578 inline uint8* WireFormatLite::WriteInt64ToArray(int field_number,
michael@0 579 int64 value,
michael@0 580 uint8* target) {
michael@0 581 target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
michael@0 582 return WriteInt64NoTagToArray(value, target);
michael@0 583 }
michael@0 584 inline uint8* WireFormatLite::WriteUInt32ToArray(int field_number,
michael@0 585 uint32 value,
michael@0 586 uint8* target) {
michael@0 587 target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
michael@0 588 return WriteUInt32NoTagToArray(value, target);
michael@0 589 }
michael@0 590 inline uint8* WireFormatLite::WriteUInt64ToArray(int field_number,
michael@0 591 uint64 value,
michael@0 592 uint8* target) {
michael@0 593 target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
michael@0 594 return WriteUInt64NoTagToArray(value, target);
michael@0 595 }
michael@0 596 inline uint8* WireFormatLite::WriteSInt32ToArray(int field_number,
michael@0 597 int32 value,
michael@0 598 uint8* target) {
michael@0 599 target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
michael@0 600 return WriteSInt32NoTagToArray(value, target);
michael@0 601 }
michael@0 602 inline uint8* WireFormatLite::WriteSInt64ToArray(int field_number,
michael@0 603 int64 value,
michael@0 604 uint8* target) {
michael@0 605 target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
michael@0 606 return WriteSInt64NoTagToArray(value, target);
michael@0 607 }
michael@0 608 inline uint8* WireFormatLite::WriteFixed32ToArray(int field_number,
michael@0 609 uint32 value,
michael@0 610 uint8* target) {
michael@0 611 target = WriteTagToArray(field_number, WIRETYPE_FIXED32, target);
michael@0 612 return WriteFixed32NoTagToArray(value, target);
michael@0 613 }
michael@0 614 inline uint8* WireFormatLite::WriteFixed64ToArray(int field_number,
michael@0 615 uint64 value,
michael@0 616 uint8* target) {
michael@0 617 target = WriteTagToArray(field_number, WIRETYPE_FIXED64, target);
michael@0 618 return WriteFixed64NoTagToArray(value, target);
michael@0 619 }
michael@0 620 inline uint8* WireFormatLite::WriteSFixed32ToArray(int field_number,
michael@0 621 int32 value,
michael@0 622 uint8* target) {
michael@0 623 target = WriteTagToArray(field_number, WIRETYPE_FIXED32, target);
michael@0 624 return WriteSFixed32NoTagToArray(value, target);
michael@0 625 }
michael@0 626 inline uint8* WireFormatLite::WriteSFixed64ToArray(int field_number,
michael@0 627 int64 value,
michael@0 628 uint8* target) {
michael@0 629 target = WriteTagToArray(field_number, WIRETYPE_FIXED64, target);
michael@0 630 return WriteSFixed64NoTagToArray(value, target);
michael@0 631 }
michael@0 632 inline uint8* WireFormatLite::WriteFloatToArray(int field_number,
michael@0 633 float value,
michael@0 634 uint8* target) {
michael@0 635 target = WriteTagToArray(field_number, WIRETYPE_FIXED32, target);
michael@0 636 return WriteFloatNoTagToArray(value, target);
michael@0 637 }
michael@0 638 inline uint8* WireFormatLite::WriteDoubleToArray(int field_number,
michael@0 639 double value,
michael@0 640 uint8* target) {
michael@0 641 target = WriteTagToArray(field_number, WIRETYPE_FIXED64, target);
michael@0 642 return WriteDoubleNoTagToArray(value, target);
michael@0 643 }
michael@0 644 inline uint8* WireFormatLite::WriteBoolToArray(int field_number,
michael@0 645 bool value,
michael@0 646 uint8* target) {
michael@0 647 target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
michael@0 648 return WriteBoolNoTagToArray(value, target);
michael@0 649 }
michael@0 650 inline uint8* WireFormatLite::WriteEnumToArray(int field_number,
michael@0 651 int value,
michael@0 652 uint8* target) {
michael@0 653 target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
michael@0 654 return WriteEnumNoTagToArray(value, target);
michael@0 655 }
michael@0 656
michael@0 657 inline uint8* WireFormatLite::WriteStringToArray(int field_number,
michael@0 658 const string& value,
michael@0 659 uint8* target) {
michael@0 660 // String is for UTF-8 text only
michael@0 661 // WARNING: In wire_format.cc, both strings and bytes are handled by
michael@0 662 // WriteString() to avoid code duplication. If the implementations become
michael@0 663 // different, you will need to update that usage.
michael@0 664 target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target);
michael@0 665 target = io::CodedOutputStream::WriteVarint32ToArray(value.size(), target);
michael@0 666 return io::CodedOutputStream::WriteStringToArray(value, target);
michael@0 667 }
michael@0 668 inline uint8* WireFormatLite::WriteBytesToArray(int field_number,
michael@0 669 const string& value,
michael@0 670 uint8* target) {
michael@0 671 target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target);
michael@0 672 target = io::CodedOutputStream::WriteVarint32ToArray(value.size(), target);
michael@0 673 return io::CodedOutputStream::WriteStringToArray(value, target);
michael@0 674 }
michael@0 675
michael@0 676
michael@0 677 inline uint8* WireFormatLite::WriteGroupToArray(int field_number,
michael@0 678 const MessageLite& value,
michael@0 679 uint8* target) {
michael@0 680 target = WriteTagToArray(field_number, WIRETYPE_START_GROUP, target);
michael@0 681 target = value.SerializeWithCachedSizesToArray(target);
michael@0 682 return WriteTagToArray(field_number, WIRETYPE_END_GROUP, target);
michael@0 683 }
michael@0 684 inline uint8* WireFormatLite::WriteMessageToArray(int field_number,
michael@0 685 const MessageLite& value,
michael@0 686 uint8* target) {
michael@0 687 target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target);
michael@0 688 target = io::CodedOutputStream::WriteVarint32ToArray(
michael@0 689 value.GetCachedSize(), target);
michael@0 690 return value.SerializeWithCachedSizesToArray(target);
michael@0 691 }
michael@0 692
michael@0 693 // See comment on ReadGroupNoVirtual to understand the need for this template
michael@0 694 // parameter name.
michael@0 695 template<typename MessageType_WorkAroundCppLookupDefect>
michael@0 696 inline uint8* WireFormatLite::WriteGroupNoVirtualToArray(
michael@0 697 int field_number, const MessageType_WorkAroundCppLookupDefect& value,
michael@0 698 uint8* target) {
michael@0 699 target = WriteTagToArray(field_number, WIRETYPE_START_GROUP, target);
michael@0 700 target = value.MessageType_WorkAroundCppLookupDefect
michael@0 701 ::SerializeWithCachedSizesToArray(target);
michael@0 702 return WriteTagToArray(field_number, WIRETYPE_END_GROUP, target);
michael@0 703 }
michael@0 704 template<typename MessageType_WorkAroundCppLookupDefect>
michael@0 705 inline uint8* WireFormatLite::WriteMessageNoVirtualToArray(
michael@0 706 int field_number, const MessageType_WorkAroundCppLookupDefect& value,
michael@0 707 uint8* target) {
michael@0 708 target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target);
michael@0 709 target = io::CodedOutputStream::WriteVarint32ToArray(
michael@0 710 value.MessageType_WorkAroundCppLookupDefect::GetCachedSize(), target);
michael@0 711 return value.MessageType_WorkAroundCppLookupDefect
michael@0 712 ::SerializeWithCachedSizesToArray(target);
michael@0 713 }
michael@0 714
michael@0 715 // ===================================================================
michael@0 716
michael@0 717 inline int WireFormatLite::Int32Size(int32 value) {
michael@0 718 return io::CodedOutputStream::VarintSize32SignExtended(value);
michael@0 719 }
michael@0 720 inline int WireFormatLite::Int64Size(int64 value) {
michael@0 721 return io::CodedOutputStream::VarintSize64(static_cast<uint64>(value));
michael@0 722 }
michael@0 723 inline int WireFormatLite::UInt32Size(uint32 value) {
michael@0 724 return io::CodedOutputStream::VarintSize32(value);
michael@0 725 }
michael@0 726 inline int WireFormatLite::UInt64Size(uint64 value) {
michael@0 727 return io::CodedOutputStream::VarintSize64(value);
michael@0 728 }
michael@0 729 inline int WireFormatLite::SInt32Size(int32 value) {
michael@0 730 return io::CodedOutputStream::VarintSize32(ZigZagEncode32(value));
michael@0 731 }
michael@0 732 inline int WireFormatLite::SInt64Size(int64 value) {
michael@0 733 return io::CodedOutputStream::VarintSize64(ZigZagEncode64(value));
michael@0 734 }
michael@0 735 inline int WireFormatLite::EnumSize(int value) {
michael@0 736 return io::CodedOutputStream::VarintSize32SignExtended(value);
michael@0 737 }
michael@0 738
michael@0 739 inline int WireFormatLite::StringSize(const string& value) {
michael@0 740 return io::CodedOutputStream::VarintSize32(value.size()) +
michael@0 741 value.size();
michael@0 742 }
michael@0 743 inline int WireFormatLite::BytesSize(const string& value) {
michael@0 744 return io::CodedOutputStream::VarintSize32(value.size()) +
michael@0 745 value.size();
michael@0 746 }
michael@0 747
michael@0 748
michael@0 749 inline int WireFormatLite::GroupSize(const MessageLite& value) {
michael@0 750 return value.ByteSize();
michael@0 751 }
michael@0 752 inline int WireFormatLite::MessageSize(const MessageLite& value) {
michael@0 753 int size = value.ByteSize();
michael@0 754 return io::CodedOutputStream::VarintSize32(size) + size;
michael@0 755 }
michael@0 756
michael@0 757 // See comment on ReadGroupNoVirtual to understand the need for this template
michael@0 758 // parameter name.
michael@0 759 template<typename MessageType_WorkAroundCppLookupDefect>
michael@0 760 inline int WireFormatLite::GroupSizeNoVirtual(
michael@0 761 const MessageType_WorkAroundCppLookupDefect& value) {
michael@0 762 return value.MessageType_WorkAroundCppLookupDefect::ByteSize();
michael@0 763 }
michael@0 764 template<typename MessageType_WorkAroundCppLookupDefect>
michael@0 765 inline int WireFormatLite::MessageSizeNoVirtual(
michael@0 766 const MessageType_WorkAroundCppLookupDefect& value) {
michael@0 767 int size = value.MessageType_WorkAroundCppLookupDefect::ByteSize();
michael@0 768 return io::CodedOutputStream::VarintSize32(size) + size;
michael@0 769 }
michael@0 770
michael@0 771 } // namespace internal
michael@0 772 } // namespace protobuf
michael@0 773
michael@0 774 } // namespace google
michael@0 775 #endif // GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_INL_H__

mercurial