BWAPI
Undermind/proxy/cpp/include/google/protobuf/wire_format_lite_inl.h
Go to the documentation of this file.
00001 // Protocol Buffers - Google's data interchange format
00002 // Copyright 2008 Google Inc.  All rights reserved.
00003 // http://code.google.com/p/protobuf/
00004 //
00005 // Redistribution and use in source and binary forms, with or without
00006 // modification, are permitted provided that the following conditions are
00007 // met:
00008 //
00009 //     * Redistributions of source code must retain the above copyright
00010 // notice, this list of conditions and the following disclaimer.
00011 //     * Redistributions in binary form must reproduce the above
00012 // copyright notice, this list of conditions and the following disclaimer
00013 // in the documentation and/or other materials provided with the
00014 // distribution.
00015 //     * Neither the name of Google Inc. nor the names of its
00016 // contributors may be used to endorse or promote products derived from
00017 // this software without specific prior written permission.
00018 //
00019 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00022 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00023 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00024 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00025 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00026 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00027 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00029 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 
00031 // Author: kenton@google.com (Kenton Varda)
00032 //         wink@google.com (Wink Saville) (refactored from wire_format.h)
00033 //  Based on original Protocol Buffers design by
00034 //  Sanjay Ghemawat, Jeff Dean, and others.
00035 
00036 #ifndef GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_INL_H__
00037 #define GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_INL_H__
00038 
00039 #include <string>
00040 #include <google/protobuf/stubs/common.h>
00041 #include <google/protobuf/message_lite.h>
00042 #include <google/protobuf/repeated_field.h>
00043 #include <google/protobuf/wire_format_lite.h>
00044 #include <google/protobuf/generated_message_util.h>
00045 #include <google/protobuf/io/coded_stream.h>
00046 
00047 
00048 namespace google {
00049 namespace protobuf {
00050 namespace internal {
00051 
00052 // Implementation details of ReadPrimitive.
00053 
00054 template <>
00055 inline bool WireFormatLite::ReadPrimitive<int32, WireFormatLite::TYPE_INT32>(
00056     io::CodedInputStream* input,
00057     int32* value) {
00058   uint32 temp;
00059   if (!input->ReadVarint32(&temp)) return false;
00060   *value = static_cast<int32>(temp);
00061   return true;
00062 }
00063 template <>
00064 inline bool WireFormatLite::ReadPrimitive<int64, WireFormatLite::TYPE_INT64>(
00065     io::CodedInputStream* input,
00066     int64* value) {
00067   uint64 temp;
00068   if (!input->ReadVarint64(&temp)) return false;
00069   *value = static_cast<int64>(temp);
00070   return true;
00071 }
00072 template <>
00073 inline bool WireFormatLite::ReadPrimitive<uint32, WireFormatLite::TYPE_UINT32>(
00074     io::CodedInputStream* input,
00075     uint32* value) {
00076   return input->ReadVarint32(value);
00077 }
00078 template <>
00079 inline bool WireFormatLite::ReadPrimitive<uint64, WireFormatLite::TYPE_UINT64>(
00080     io::CodedInputStream* input,
00081     uint64* value) {
00082   return input->ReadVarint64(value);
00083 }
00084 template <>
00085 inline bool WireFormatLite::ReadPrimitive<int32, WireFormatLite::TYPE_SINT32>(
00086     io::CodedInputStream* input,
00087     int32* value) {
00088   uint32 temp;
00089   if (!input->ReadVarint32(&temp)) return false;
00090   *value = ZigZagDecode32(temp);
00091   return true;
00092 }
00093 template <>
00094 inline bool WireFormatLite::ReadPrimitive<int64, WireFormatLite::TYPE_SINT64>(
00095     io::CodedInputStream* input,
00096     int64* value) {
00097   uint64 temp;
00098   if (!input->ReadVarint64(&temp)) return false;
00099   *value = ZigZagDecode64(temp);
00100   return true;
00101 }
00102 template <>
00103 inline bool WireFormatLite::ReadPrimitive<uint32, WireFormatLite::TYPE_FIXED32>(
00104     io::CodedInputStream* input,
00105     uint32* value) {
00106   return input->ReadLittleEndian32(value);
00107 }
00108 template <>
00109 inline bool WireFormatLite::ReadPrimitive<uint64, WireFormatLite::TYPE_FIXED64>(
00110     io::CodedInputStream* input,
00111     uint64* value) {
00112   return input->ReadLittleEndian64(value);
00113 }
00114 template <>
00115 inline bool WireFormatLite::ReadPrimitive<int32, WireFormatLite::TYPE_SFIXED32>(
00116     io::CodedInputStream* input,
00117     int32* value) {
00118   uint32 temp;
00119   if (!input->ReadLittleEndian32(&temp)) return false;
00120   *value = static_cast<int32>(temp);
00121   return true;
00122 }
00123 template <>
00124 inline bool WireFormatLite::ReadPrimitive<int64, WireFormatLite::TYPE_SFIXED64>(
00125     io::CodedInputStream* input,
00126     int64* value) {
00127   uint64 temp;
00128   if (!input->ReadLittleEndian64(&temp)) return false;
00129   *value = static_cast<int64>(temp);
00130   return true;
00131 }
00132 template <>
00133 inline bool WireFormatLite::ReadPrimitive<float, WireFormatLite::TYPE_FLOAT>(
00134     io::CodedInputStream* input,
00135     float* value) {
00136   uint32 temp;
00137   if (!input->ReadLittleEndian32(&temp)) return false;
00138   *value = DecodeFloat(temp);
00139   return true;
00140 }
00141 template <>
00142 inline bool WireFormatLite::ReadPrimitive<double, WireFormatLite::TYPE_DOUBLE>(
00143     io::CodedInputStream* input,
00144     double* value) {
00145   uint64 temp;
00146   if (!input->ReadLittleEndian64(&temp)) return false;
00147   *value = DecodeDouble(temp);
00148   return true;
00149 }
00150 template <>
00151 inline bool WireFormatLite::ReadPrimitive<bool, WireFormatLite::TYPE_BOOL>(
00152     io::CodedInputStream* input,
00153     bool* value) {
00154   uint32 temp;
00155   if (!input->ReadVarint32(&temp)) return false;
00156   *value = temp != 0;
00157   return true;
00158 }
00159 template <>
00160 inline bool WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>(
00161     io::CodedInputStream* input,
00162     int* value) {
00163   uint32 temp;
00164   if (!input->ReadVarint32(&temp)) return false;
00165   *value = static_cast<int>(temp);
00166   return true;
00167 }
00168 
00169 template <>
00170 inline const uint8* WireFormatLite::ReadPrimitiveFromArray<
00171   uint32, WireFormatLite::TYPE_FIXED32>(
00172     const uint8* buffer,
00173     uint32* value) {
00174   return io::CodedInputStream::ReadLittleEndian32FromArray(buffer, value);
00175 }
00176 template <>
00177 inline const uint8* WireFormatLite::ReadPrimitiveFromArray<
00178   uint64, WireFormatLite::TYPE_FIXED64>(
00179     const uint8* buffer,
00180     uint64* value) {
00181   return io::CodedInputStream::ReadLittleEndian64FromArray(buffer, value);
00182 }
00183 template <>
00184 inline const uint8* WireFormatLite::ReadPrimitiveFromArray<
00185   int32, WireFormatLite::TYPE_SFIXED32>(
00186     const uint8* buffer,
00187     int32* value) {
00188   uint32 temp;
00189   buffer = io::CodedInputStream::ReadLittleEndian32FromArray(buffer, &temp);
00190   *value = static_cast<int32>(temp);
00191   return buffer;
00192 }
00193 template <>
00194 inline const uint8* WireFormatLite::ReadPrimitiveFromArray<
00195   int64, WireFormatLite::TYPE_SFIXED64>(
00196     const uint8* buffer,
00197     int64* value) {
00198   uint64 temp;
00199   buffer = io::CodedInputStream::ReadLittleEndian64FromArray(buffer, &temp);
00200   *value = static_cast<int64>(temp);
00201   return buffer;
00202 }
00203 template <>
00204 inline const uint8* WireFormatLite::ReadPrimitiveFromArray<
00205   float, WireFormatLite::TYPE_FLOAT>(
00206     const uint8* buffer,
00207     float* value) {
00208   uint32 temp;
00209   buffer = io::CodedInputStream::ReadLittleEndian32FromArray(buffer, &temp);
00210   *value = DecodeFloat(temp);
00211   return buffer;
00212 }
00213 template <>
00214 inline const uint8* WireFormatLite::ReadPrimitiveFromArray<
00215   double, WireFormatLite::TYPE_DOUBLE>(
00216     const uint8* buffer,
00217     double* value) {
00218   uint64 temp;
00219   buffer = io::CodedInputStream::ReadLittleEndian64FromArray(buffer, &temp);
00220   *value = DecodeDouble(temp);
00221   return buffer;
00222 }
00223 
00224 template <typename CType, enum WireFormatLite::FieldType DeclaredType>
00225 inline bool WireFormatLite::ReadRepeatedPrimitive(int tag_size,
00226                                                uint32 tag,
00227                                                io::CodedInputStream* input,
00228                                                RepeatedField<CType>* values) {
00229   CType value;
00230   if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false;
00231   values->Add(value);
00232   int elements_already_reserved = values->Capacity() - values->size();
00233   while (elements_already_reserved > 0 && input->ExpectTag(tag)) {
00234     if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false;
00235     values->AddAlreadyReserved(value);
00236     elements_already_reserved--;
00237   }
00238   return true;
00239 }
00240 
00241 template <typename CType, enum WireFormatLite::FieldType DeclaredType>
00242 inline bool WireFormatLite::ReadRepeatedFixedSizePrimitive(
00243     int tag_size,
00244     uint32 tag,
00245     io::CodedInputStream* input,
00246     RepeatedField<CType>* values) {
00247   GOOGLE_DCHECK_EQ(UInt32Size(tag), tag_size);
00248   CType value;
00249   if (!ReadPrimitive<CType, DeclaredType>(input, &value))
00250     return false;
00251   values->Add(value);
00252 
00253   // For fixed size values, repeated values can be read more quickly by
00254   // reading directly from a raw array.
00255   //
00256   // We can get a tight loop by only reading as many elements as can be
00257   // added to the RepeatedField without having to do any resizing. Additionally,
00258   // we only try to read as many elements as are available from the current
00259   // buffer space. Doing so avoids having to perform boundary checks when
00260   // reading the value: the maximum number of elements that can be read is
00261   // known outside of the loop.
00262   const void* void_pointer;
00263   int size;
00264   input->GetDirectBufferPointerInline(&void_pointer, &size);
00265   if (size > 0) {
00266     const uint8* buffer = reinterpret_cast<const uint8*>(void_pointer);
00267     // The number of bytes each type occupies on the wire.
00268     const int per_value_size = tag_size + sizeof(value);
00269 
00270     int elements_available = min(values->Capacity() - values->size(),
00271                                  size / per_value_size);
00272     int num_read = 0;
00273     while (num_read < elements_available &&
00274            (buffer = io::CodedInputStream::ExpectTagFromArray(
00275                buffer, tag)) != NULL) {
00276       buffer = ReadPrimitiveFromArray<CType, DeclaredType>(buffer, &value);
00277       values->AddAlreadyReserved(value);
00278       ++num_read;
00279     }
00280     const int read_bytes = num_read * per_value_size;
00281     if (read_bytes > 0) {
00282       input->Skip(read_bytes);
00283     }
00284   }
00285   return true;
00286 }
00287 
00288 // Specializations of ReadRepeatedPrimitive for the fixed size types, which use 
00289 // the optimized code path.
00290 #define READ_REPEATED_FIXED_SIZE_PRIMITIVE(CPPTYPE, DECLARED_TYPE)             \
00291 template <>                                                                    \
00292 inline bool WireFormatLite::ReadRepeatedPrimitive<                             \
00293   CPPTYPE, WireFormatLite::DECLARED_TYPE>(                                     \
00294     int tag_size,                                                              \
00295     uint32 tag,                                                                \
00296     io::CodedInputStream* input,                                               \
00297     RepeatedField<CPPTYPE>* values) {                                          \
00298   return ReadRepeatedFixedSizePrimitive<                                       \
00299     CPPTYPE, WireFormatLite::DECLARED_TYPE>(                                   \
00300       tag_size, tag, input, values);                                           \
00301 }
00302 
00303 READ_REPEATED_FIXED_SIZE_PRIMITIVE(uint32, TYPE_FIXED32);
00304 READ_REPEATED_FIXED_SIZE_PRIMITIVE(uint64, TYPE_FIXED64);
00305 READ_REPEATED_FIXED_SIZE_PRIMITIVE(int32, TYPE_SFIXED32);
00306 READ_REPEATED_FIXED_SIZE_PRIMITIVE(int64, TYPE_SFIXED64);
00307 READ_REPEATED_FIXED_SIZE_PRIMITIVE(float, TYPE_FLOAT);
00308 READ_REPEATED_FIXED_SIZE_PRIMITIVE(double, TYPE_DOUBLE);
00309 
00310 #undef READ_REPEATED_FIXED_SIZE_PRIMITIVE
00311 
00312 template <typename CType, enum WireFormatLite::FieldType DeclaredType>
00313 bool WireFormatLite::ReadRepeatedPrimitiveNoInline(
00314     int tag_size,
00315     uint32 tag,
00316     io::CodedInputStream* input,
00317     RepeatedField<CType>* value) {
00318   return ReadRepeatedPrimitive<CType, DeclaredType>(
00319       tag_size, tag, input, value);
00320 }
00321 
00322 template <typename CType, enum WireFormatLite::FieldType DeclaredType>
00323 inline bool WireFormatLite::ReadPackedPrimitive(io::CodedInputStream* input,
00324                                                 RepeatedField<CType>* values) {
00325   uint32 length;
00326   if (!input->ReadVarint32(&length)) return false;
00327   io::CodedInputStream::Limit limit = input->PushLimit(length);
00328   while (input->BytesUntilLimit() > 0) {
00329     CType value;
00330     if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false;
00331     values->Add(value);
00332   }
00333   input->PopLimit(limit);
00334   return true;
00335 }
00336 
00337 template <typename CType, enum WireFormatLite::FieldType DeclaredType>
00338 bool WireFormatLite::ReadPackedPrimitiveNoInline(io::CodedInputStream* input,
00339                                                  RepeatedField<CType>* values) {
00340   return ReadPackedPrimitive<CType, DeclaredType>(input, values);
00341 }
00342 
00343 
00344 inline bool WireFormatLite::ReadGroup(int field_number,
00345                                       io::CodedInputStream* input,
00346                                       MessageLite* value) {
00347   if (!input->IncrementRecursionDepth()) return false;
00348   if (!value->MergePartialFromCodedStream(input)) return false;
00349   input->DecrementRecursionDepth();
00350   // Make sure the last thing read was an end tag for this group.
00351   if (!input->LastTagWas(MakeTag(field_number, WIRETYPE_END_GROUP))) {
00352     return false;
00353   }
00354   return true;
00355 }
00356 inline bool WireFormatLite::ReadMessage(io::CodedInputStream* input,
00357                                         MessageLite* value) {
00358   uint32 length;
00359   if (!input->ReadVarint32(&length)) return false;
00360   if (!input->IncrementRecursionDepth()) return false;
00361   io::CodedInputStream::Limit limit = input->PushLimit(length);
00362   if (!value->MergePartialFromCodedStream(input)) return false;
00363   // Make sure that parsing stopped when the limit was hit, not at an endgroup
00364   // tag.
00365   if (!input->ConsumedEntireMessage()) return false;
00366   input->PopLimit(limit);
00367   input->DecrementRecursionDepth();
00368   return true;
00369 }
00370 
00371 template<typename MessageType>
00372 inline bool WireFormatLite::ReadGroupNoVirtual(int field_number,
00373                                                io::CodedInputStream* input,
00374                                                MessageType* value) {
00375   if (!input->IncrementRecursionDepth()) return false;
00376   if (!value->MessageType::MergePartialFromCodedStream(input)) return false;
00377   input->DecrementRecursionDepth();
00378   // Make sure the last thing read was an end tag for this group.
00379   if (!input->LastTagWas(MakeTag(field_number, WIRETYPE_END_GROUP))) {
00380     return false;
00381   }
00382   return true;
00383 }
00384 template<typename MessageType>
00385 inline bool WireFormatLite::ReadMessageNoVirtual(io::CodedInputStream* input,
00386                                                  MessageType* value) {
00387   uint32 length;
00388   if (!input->ReadVarint32(&length)) return false;
00389   if (!input->IncrementRecursionDepth()) return false;
00390   io::CodedInputStream::Limit limit = input->PushLimit(length);
00391   if (!value->MessageType::MergePartialFromCodedStream(input)) return false;
00392   // Make sure that parsing stopped when the limit was hit, not at an endgroup
00393   // tag.
00394   if (!input->ConsumedEntireMessage()) return false;
00395   input->PopLimit(limit);
00396   input->DecrementRecursionDepth();
00397   return true;
00398 }
00399 
00400 // ===================================================================
00401 
00402 inline void WireFormatLite::WriteTag(int field_number, WireType type,
00403                                      io::CodedOutputStream* output) {
00404   output->WriteTag(MakeTag(field_number, type));
00405 }
00406 
00407 inline void WireFormatLite::WriteInt32NoTag(int32 value,
00408                                             io::CodedOutputStream* output) {
00409   output->WriteVarint32SignExtended(value);
00410 }
00411 inline void WireFormatLite::WriteInt64NoTag(int64 value,
00412                                             io::CodedOutputStream* output) {
00413   output->WriteVarint64(static_cast<uint64>(value));
00414 }
00415 inline void WireFormatLite::WriteUInt32NoTag(uint32 value,
00416                                              io::CodedOutputStream* output) {
00417   output->WriteVarint32(value);
00418 }
00419 inline void WireFormatLite::WriteUInt64NoTag(uint64 value,
00420                                              io::CodedOutputStream* output) {
00421   output->WriteVarint64(value);
00422 }
00423 inline void WireFormatLite::WriteSInt32NoTag(int32 value,
00424                                              io::CodedOutputStream* output) {
00425   output->WriteVarint32(ZigZagEncode32(value));
00426 }
00427 inline void WireFormatLite::WriteSInt64NoTag(int64 value,
00428                                              io::CodedOutputStream* output) {
00429   output->WriteVarint64(ZigZagEncode64(value));
00430 }
00431 inline void WireFormatLite::WriteFixed32NoTag(uint32 value,
00432                                               io::CodedOutputStream* output) {
00433   output->WriteLittleEndian32(value);
00434 }
00435 inline void WireFormatLite::WriteFixed64NoTag(uint64 value,
00436                                               io::CodedOutputStream* output) {
00437   output->WriteLittleEndian64(value);
00438 }
00439 inline void WireFormatLite::WriteSFixed32NoTag(int32 value,
00440                                                io::CodedOutputStream* output) {
00441   output->WriteLittleEndian32(static_cast<uint32>(value));
00442 }
00443 inline void WireFormatLite::WriteSFixed64NoTag(int64 value,
00444                                                io::CodedOutputStream* output) {
00445   output->WriteLittleEndian64(static_cast<uint64>(value));
00446 }
00447 inline void WireFormatLite::WriteFloatNoTag(float value,
00448                                             io::CodedOutputStream* output) {
00449   output->WriteLittleEndian32(EncodeFloat(value));
00450 }
00451 inline void WireFormatLite::WriteDoubleNoTag(double value,
00452                                              io::CodedOutputStream* output) {
00453   output->WriteLittleEndian64(EncodeDouble(value));
00454 }
00455 inline void WireFormatLite::WriteBoolNoTag(bool value,
00456                                            io::CodedOutputStream* output) {
00457   output->WriteVarint32(value ? 1 : 0);
00458 }
00459 inline void WireFormatLite::WriteEnumNoTag(int value,
00460                                            io::CodedOutputStream* output) {
00461   output->WriteVarint32SignExtended(value);
00462 }
00463 
00464 template<typename MessageType>
00465 inline void WireFormatLite::WriteGroupNoVirtual(int field_number,
00466                                                 const MessageType& value,
00467                                                 io::CodedOutputStream* output) {
00468   WriteTag(field_number, WIRETYPE_START_GROUP, output);
00469   value.MessageType::SerializeWithCachedSizes(output);
00470   WriteTag(field_number, WIRETYPE_END_GROUP, output);
00471 }
00472 template<typename MessageType>
00473 inline void WireFormatLite::WriteMessageNoVirtual(int field_number,
00474                                                 const MessageType& value,
00475                                                 io::CodedOutputStream* output) {
00476   WriteTag(field_number, WIRETYPE_LENGTH_DELIMITED, output);
00477   output->WriteVarint32(value.MessageType::GetCachedSize());
00478   value.MessageType::SerializeWithCachedSizes(output);
00479 }
00480 
00481 // ===================================================================
00482 
00483 inline uint8* WireFormatLite::WriteTagToArray(int field_number,
00484                                               WireType type,
00485                                               uint8* target) {
00486   return io::CodedOutputStream::WriteTagToArray(MakeTag(field_number, type),
00487                                                 target);
00488 }
00489 
00490 inline uint8* WireFormatLite::WriteInt32NoTagToArray(int32 value,
00491                                                      uint8* target) {
00492   return io::CodedOutputStream::WriteVarint32SignExtendedToArray(value, target);
00493 }
00494 inline uint8* WireFormatLite::WriteInt64NoTagToArray(int64 value,
00495                                                      uint8* target) {
00496   return io::CodedOutputStream::WriteVarint64ToArray(
00497       static_cast<uint64>(value), target);
00498 }
00499 inline uint8* WireFormatLite::WriteUInt32NoTagToArray(uint32 value,
00500                                                       uint8* target) {
00501   return io::CodedOutputStream::WriteVarint32ToArray(value, target);
00502 }
00503 inline uint8* WireFormatLite::WriteUInt64NoTagToArray(uint64 value,
00504                                                       uint8* target) {
00505   return io::CodedOutputStream::WriteVarint64ToArray(value, target);
00506 }
00507 inline uint8* WireFormatLite::WriteSInt32NoTagToArray(int32 value,
00508                                                       uint8* target) {
00509   return io::CodedOutputStream::WriteVarint32ToArray(ZigZagEncode32(value),
00510                                                      target);
00511 }
00512 inline uint8* WireFormatLite::WriteSInt64NoTagToArray(int64 value,
00513                                                       uint8* target) {
00514   return io::CodedOutputStream::WriteVarint64ToArray(ZigZagEncode64(value),
00515                                                      target);
00516 }
00517 inline uint8* WireFormatLite::WriteFixed32NoTagToArray(uint32 value,
00518                                                        uint8* target) {
00519   return io::CodedOutputStream::WriteLittleEndian32ToArray(value, target);
00520 }
00521 inline uint8* WireFormatLite::WriteFixed64NoTagToArray(uint64 value,
00522                                                        uint8* target) {
00523   return io::CodedOutputStream::WriteLittleEndian64ToArray(value, target);
00524 }
00525 inline uint8* WireFormatLite::WriteSFixed32NoTagToArray(int32 value,
00526                                                         uint8* target) {
00527   return io::CodedOutputStream::WriteLittleEndian32ToArray(
00528       static_cast<uint32>(value), target);
00529 }
00530 inline uint8* WireFormatLite::WriteSFixed64NoTagToArray(int64 value,
00531                                                         uint8* target) {
00532   return io::CodedOutputStream::WriteLittleEndian64ToArray(
00533       static_cast<uint64>(value), target);
00534 }
00535 inline uint8* WireFormatLite::WriteFloatNoTagToArray(float value,
00536                                                      uint8* target) {
00537   return io::CodedOutputStream::WriteLittleEndian32ToArray(EncodeFloat(value),
00538                                                            target);
00539 }
00540 inline uint8* WireFormatLite::WriteDoubleNoTagToArray(double value,
00541                                                       uint8* target) {
00542   return io::CodedOutputStream::WriteLittleEndian64ToArray(EncodeDouble(value),
00543                                                            target);
00544 }
00545 inline uint8* WireFormatLite::WriteBoolNoTagToArray(bool value,
00546                                                     uint8* target) {
00547   return io::CodedOutputStream::WriteVarint32ToArray(value ? 1 : 0, target);
00548 }
00549 inline uint8* WireFormatLite::WriteEnumNoTagToArray(int value,
00550                                                     uint8* target) {
00551   return io::CodedOutputStream::WriteVarint32SignExtendedToArray(value, target);
00552 }
00553 
00554 inline uint8* WireFormatLite::WriteInt32ToArray(int field_number,
00555                                                 int32 value,
00556                                                 uint8* target) {
00557   target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
00558   return WriteInt32NoTagToArray(value, target);
00559 }
00560 inline uint8* WireFormatLite::WriteInt64ToArray(int field_number,
00561                                                 int64 value,
00562                                                 uint8* target) {
00563   target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
00564   return WriteInt64NoTagToArray(value, target);
00565 }
00566 inline uint8* WireFormatLite::WriteUInt32ToArray(int field_number,
00567                                                  uint32 value,
00568                                                  uint8* target) {
00569   target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
00570   return WriteUInt32NoTagToArray(value, target);
00571 }
00572 inline uint8* WireFormatLite::WriteUInt64ToArray(int field_number,
00573                                                  uint64 value,
00574                                                  uint8* target) {
00575   target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
00576   return WriteUInt64NoTagToArray(value, target);
00577 }
00578 inline uint8* WireFormatLite::WriteSInt32ToArray(int field_number,
00579                                                  int32 value,
00580                                                  uint8* target) {
00581   target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
00582   return WriteSInt32NoTagToArray(value, target);
00583 }
00584 inline uint8* WireFormatLite::WriteSInt64ToArray(int field_number,
00585                                                  int64 value,
00586                                                  uint8* target) {
00587   target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
00588   return WriteSInt64NoTagToArray(value, target);
00589 }
00590 inline uint8* WireFormatLite::WriteFixed32ToArray(int field_number,
00591                                                   uint32 value,
00592                                                   uint8* target) {
00593   target = WriteTagToArray(field_number, WIRETYPE_FIXED32, target);
00594   return WriteFixed32NoTagToArray(value, target);
00595 }
00596 inline uint8* WireFormatLite::WriteFixed64ToArray(int field_number,
00597                                                   uint64 value,
00598                                                   uint8* target) {
00599   target = WriteTagToArray(field_number, WIRETYPE_FIXED64, target);
00600   return WriteFixed64NoTagToArray(value, target);
00601 }
00602 inline uint8* WireFormatLite::WriteSFixed32ToArray(int field_number,
00603                                                    int32 value,
00604                                                    uint8* target) {
00605   target = WriteTagToArray(field_number, WIRETYPE_FIXED32, target);
00606   return WriteSFixed32NoTagToArray(value, target);
00607 }
00608 inline uint8* WireFormatLite::WriteSFixed64ToArray(int field_number,
00609                                                    int64 value,
00610                                                    uint8* target) {
00611   target = WriteTagToArray(field_number, WIRETYPE_FIXED64, target);
00612   return WriteSFixed64NoTagToArray(value, target);
00613 }
00614 inline uint8* WireFormatLite::WriteFloatToArray(int field_number,
00615                                                 float value,
00616                                                 uint8* target) {
00617   target = WriteTagToArray(field_number, WIRETYPE_FIXED32, target);
00618   return WriteFloatNoTagToArray(value, target);
00619 }
00620 inline uint8* WireFormatLite::WriteDoubleToArray(int field_number,
00621                                                  double value,
00622                                                  uint8* target) {
00623   target = WriteTagToArray(field_number, WIRETYPE_FIXED64, target);
00624   return WriteDoubleNoTagToArray(value, target);
00625 }
00626 inline uint8* WireFormatLite::WriteBoolToArray(int field_number,
00627                                                bool value,
00628                                                uint8* target) {
00629   target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
00630   return WriteBoolNoTagToArray(value, target);
00631 }
00632 inline uint8* WireFormatLite::WriteEnumToArray(int field_number,
00633                                                int value,
00634                                                uint8* target) {
00635   target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
00636   return WriteEnumNoTagToArray(value, target);
00637 }
00638 
00639 inline uint8* WireFormatLite::WriteStringToArray(int field_number,
00640                                                  const string& value,
00641                                                  uint8* target) {
00642   // String is for UTF-8 text only
00643   // WARNING:  In wire_format.cc, both strings and bytes are handled by
00644   //   WriteString() to avoid code duplication.  If the implementations become
00645   //   different, you will need to update that usage.
00646   target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target);
00647   target = io::CodedOutputStream::WriteVarint32ToArray(value.size(), target);
00648   return io::CodedOutputStream::WriteStringToArray(value, target);
00649 }
00650 inline uint8* WireFormatLite::WriteBytesToArray(int field_number,
00651                                                 const string& value,
00652                                                 uint8* target) {
00653   target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target);
00654   target = io::CodedOutputStream::WriteVarint32ToArray(value.size(), target);
00655   return io::CodedOutputStream::WriteStringToArray(value, target);
00656 }
00657 
00658 
00659 inline uint8* WireFormatLite::WriteGroupToArray(int field_number,
00660                                                 const MessageLite& value,
00661                                                 uint8* target) {
00662   target = WriteTagToArray(field_number, WIRETYPE_START_GROUP, target);
00663   target = value.SerializeWithCachedSizesToArray(target);
00664   return WriteTagToArray(field_number, WIRETYPE_END_GROUP, target);
00665 }
00666 inline uint8* WireFormatLite::WriteMessageToArray(int field_number,
00667                                                   const MessageLite& value,
00668                                                   uint8* target) {
00669   target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target);
00670   target = io::CodedOutputStream::WriteVarint32ToArray(
00671     value.GetCachedSize(), target);
00672   return value.SerializeWithCachedSizesToArray(target);
00673 }
00674 
00675 template<typename MessageType>
00676 inline uint8* WireFormatLite::WriteGroupNoVirtualToArray(
00677     int field_number, const MessageType& value, uint8* target) {
00678   target = WriteTagToArray(field_number, WIRETYPE_START_GROUP, target);
00679   target = value.MessageType::SerializeWithCachedSizesToArray(target);
00680   return WriteTagToArray(field_number, WIRETYPE_END_GROUP, target);
00681 }
00682 template<typename MessageType>
00683 inline uint8* WireFormatLite::WriteMessageNoVirtualToArray(
00684     int field_number, const MessageType& value, uint8* target) {
00685   target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target);
00686   target = io::CodedOutputStream::WriteVarint32ToArray(
00687     value.MessageType::GetCachedSize(), target);
00688   return value.MessageType::SerializeWithCachedSizesToArray(target);
00689 }
00690 
00691 // ===================================================================
00692 
00693 inline int WireFormatLite::Int32Size(int32 value) {
00694   return io::CodedOutputStream::VarintSize32SignExtended(value);
00695 }
00696 inline int WireFormatLite::Int64Size(int64 value) {
00697   return io::CodedOutputStream::VarintSize64(static_cast<uint64>(value));
00698 }
00699 inline int WireFormatLite::UInt32Size(uint32 value) {
00700   return io::CodedOutputStream::VarintSize32(value);
00701 }
00702 inline int WireFormatLite::UInt64Size(uint64 value) {
00703   return io::CodedOutputStream::VarintSize64(value);
00704 }
00705 inline int WireFormatLite::SInt32Size(int32 value) {
00706   return io::CodedOutputStream::VarintSize32(ZigZagEncode32(value));
00707 }
00708 inline int WireFormatLite::SInt64Size(int64 value) {
00709   return io::CodedOutputStream::VarintSize64(ZigZagEncode64(value));
00710 }
00711 inline int WireFormatLite::EnumSize(int value) {
00712   return io::CodedOutputStream::VarintSize32SignExtended(value);
00713 }
00714 
00715 inline int WireFormatLite::StringSize(const string& value) {
00716   return io::CodedOutputStream::VarintSize32(value.size()) +
00717          value.size();
00718 }
00719 inline int WireFormatLite::BytesSize(const string& value) {
00720   return io::CodedOutputStream::VarintSize32(value.size()) +
00721          value.size();
00722 }
00723 
00724 
00725 inline int WireFormatLite::GroupSize(const MessageLite& value) {
00726   return value.ByteSize();
00727 }
00728 inline int WireFormatLite::MessageSize(const MessageLite& value) {
00729   int size = value.ByteSize();
00730   return io::CodedOutputStream::VarintSize32(size) + size;
00731 }
00732 
00733 template<typename MessageType>
00734 inline int WireFormatLite::GroupSizeNoVirtual(const MessageType& value) {
00735   return value.MessageType::ByteSize();
00736 }
00737 template<typename MessageType>
00738 inline int WireFormatLite::MessageSizeNoVirtual(const MessageType& value) {
00739   int size = value.MessageType::ByteSize();
00740   return io::CodedOutputStream::VarintSize32(size) + size;
00741 }
00742 
00743 }  // namespace internal
00744 }  // namespace protobuf
00745 
00746 }  // namespace google
00747 #endif  // GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_INL_H__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines