BWAPI
|
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 // atenasio@google.com (Chris Atenasio) (ZigZag transform) 00033 // wink@google.com (Wink Saville) (refactored from wire_format.h) 00034 // Based on original Protocol Buffers design by 00035 // Sanjay Ghemawat, Jeff Dean, and others. 00036 // 00037 // This header is logically internal, but is made public because it is used 00038 // from protocol-compiler-generated code, which may reside in other components. 00039 00040 #ifndef GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_H__ 00041 #define GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_H__ 00042 00043 #include <string> 00044 #include <google/protobuf/message_lite.h> 00045 00046 namespace google { 00047 00048 namespace protobuf { 00049 template <typename T> class RepeatedField; // repeated_field.h 00050 namespace io { 00051 class CodedInputStream; // coded_stream.h 00052 class CodedOutputStream; // coded_stream.h 00053 } 00054 } 00055 00056 namespace protobuf { 00057 namespace internal { 00058 00059 class StringPieceField; 00060 00061 // This class is for internal use by the protocol buffer library and by 00062 // protocol-complier-generated message classes. It must not be called 00063 // directly by clients. 00064 // 00065 // This class contains helpers for implementing the binary protocol buffer 00066 // wire format without the need for reflection. Use WireFormat when using 00067 // reflection. 00068 // 00069 // This class is really a namespace that contains only static methods. 00070 class LIBPROTOBUF_EXPORT WireFormatLite { 00071 public: 00072 00073 // ----------------------------------------------------------------- 00074 // Helper constants and functions related to the format. These are 00075 // mostly meant for internal and generated code to use. 00076 00077 // The wire format is composed of a sequence of tag/value pairs, each 00078 // of which contains the value of one field (or one element of a repeated 00079 // field). Each tag is encoded as a varint. The lower bits of the tag 00080 // identify its wire type, which specifies the format of the data to follow. 00081 // The rest of the bits contain the field number. Each type of field (as 00082 // declared by FieldDescriptor::Type, in descriptor.h) maps to one of 00083 // these wire types. Immediately following each tag is the field's value, 00084 // encoded in the format specified by the wire type. Because the tag 00085 // identifies the encoding of this data, it is possible to skip 00086 // unrecognized fields for forwards compatibility. 00087 00088 enum WireType { 00089 WIRETYPE_VARINT = 0, 00090 WIRETYPE_FIXED64 = 1, 00091 WIRETYPE_LENGTH_DELIMITED = 2, 00092 WIRETYPE_START_GROUP = 3, 00093 WIRETYPE_END_GROUP = 4, 00094 WIRETYPE_FIXED32 = 5, 00095 }; 00096 00097 // Lite alternative to FieldDescriptor::Type. Must be kept in sync. 00098 enum FieldType { 00099 TYPE_DOUBLE = 1, 00100 TYPE_FLOAT = 2, 00101 TYPE_INT64 = 3, 00102 TYPE_UINT64 = 4, 00103 TYPE_INT32 = 5, 00104 TYPE_FIXED64 = 6, 00105 TYPE_FIXED32 = 7, 00106 TYPE_BOOL = 8, 00107 TYPE_STRING = 9, 00108 TYPE_GROUP = 10, 00109 TYPE_MESSAGE = 11, 00110 TYPE_BYTES = 12, 00111 TYPE_UINT32 = 13, 00112 TYPE_ENUM = 14, 00113 TYPE_SFIXED32 = 15, 00114 TYPE_SFIXED64 = 16, 00115 TYPE_SINT32 = 17, 00116 TYPE_SINT64 = 18, 00117 MAX_FIELD_TYPE = 18, 00118 }; 00119 00120 // Lite alternative to FieldDescriptor::CppType. Must be kept in sync. 00121 enum CppType { 00122 CPPTYPE_INT32 = 1, 00123 CPPTYPE_INT64 = 2, 00124 CPPTYPE_UINT32 = 3, 00125 CPPTYPE_UINT64 = 4, 00126 CPPTYPE_DOUBLE = 5, 00127 CPPTYPE_FLOAT = 6, 00128 CPPTYPE_BOOL = 7, 00129 CPPTYPE_ENUM = 8, 00130 CPPTYPE_STRING = 9, 00131 CPPTYPE_MESSAGE = 10, 00132 MAX_CPPTYPE = 10, 00133 }; 00134 00135 // Helper method to get the CppType for a particular Type. 00136 static CppType FieldTypeToCppType(FieldType type); 00137 00138 // Given a FieldSescriptor::Type return its WireType 00139 static inline WireFormatLite::WireType WireTypeForFieldType( 00140 WireFormatLite::FieldType type) { 00141 return kWireTypeForFieldType[type]; 00142 } 00143 00144 // Number of bits in a tag which identify the wire type. 00145 static const int kTagTypeBits = 3; 00146 // Mask for those bits. 00147 static const uint32 kTagTypeMask = (1 << kTagTypeBits) - 1; 00148 00149 // Helper functions for encoding and decoding tags. (Inlined below and in 00150 // _inl.h) 00151 // 00152 // This is different from MakeTag(field->number(), field->type()) in the case 00153 // of packed repeated fields. 00154 static uint32 MakeTag(int field_number, WireType type); 00155 static WireType GetTagWireType(uint32 tag); 00156 static int GetTagFieldNumber(uint32 tag); 00157 00158 // Compute the byte size of a tag. For groups, this includes both the start 00159 // and end tags. 00160 static inline int TagSize(int field_number, WireFormatLite::FieldType type); 00161 00162 // Skips a field value with the given tag. The input should start 00163 // positioned immediately after the tag. Skipped values are simply discarded, 00164 // not recorded anywhere. See WireFormat::SkipField() for a version that 00165 // records to an UnknownFieldSet. 00166 static bool SkipField(io::CodedInputStream* input, uint32 tag); 00167 00168 // Reads and ignores a message from the input. Skipped values are simply 00169 // discarded, not recorded anywhere. See WireFormat::SkipMessage() for a 00170 // version that records to an UnknownFieldSet. 00171 static bool SkipMessage(io::CodedInputStream* input); 00172 00173 // This macro does the same thing as WireFormatLite::MakeTag(), but the 00174 // result is usable as a compile-time constant, which makes it usable 00175 // as a switch case or a template input. WireFormatLite::MakeTag() is more 00176 // type-safe, though, so prefer it if possible. 00177 #define GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(FIELD_NUMBER, TYPE) \ 00178 static_cast<uint32>( \ 00179 ((FIELD_NUMBER) << ::google::protobuf::internal::WireFormatLite::kTagTypeBits) \ 00180 | (TYPE)) 00181 00182 // These are the tags for the old MessageSet format, which was defined as: 00183 // message MessageSet { 00184 // repeated group Item = 1 { 00185 // required int32 type_id = 2; 00186 // required string message = 3; 00187 // } 00188 // } 00189 static const int kMessageSetItemNumber = 1; 00190 static const int kMessageSetTypeIdNumber = 2; 00191 static const int kMessageSetMessageNumber = 3; 00192 static const int kMessageSetItemStartTag = 00193 GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(kMessageSetItemNumber, 00194 WireFormatLite::WIRETYPE_START_GROUP); 00195 static const int kMessageSetItemEndTag = 00196 GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(kMessageSetItemNumber, 00197 WireFormatLite::WIRETYPE_END_GROUP); 00198 static const int kMessageSetTypeIdTag = 00199 GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(kMessageSetTypeIdNumber, 00200 WireFormatLite::WIRETYPE_VARINT); 00201 static const int kMessageSetMessageTag = 00202 GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(kMessageSetMessageNumber, 00203 WireFormatLite::WIRETYPE_LENGTH_DELIMITED); 00204 00205 // Byte size of all tags of a MessageSet::Item combined. 00206 static const int kMessageSetItemTagsSize; 00207 00208 // Helper functions for converting between floats/doubles and IEEE-754 00209 // uint32s/uint64s so that they can be written. (Assumes your platform 00210 // uses IEEE-754 floats.) 00211 static uint32 EncodeFloat(float value); 00212 static float DecodeFloat(uint32 value); 00213 static uint64 EncodeDouble(double value); 00214 static double DecodeDouble(uint64 value); 00215 00216 // Helper functions for mapping signed integers to unsigned integers in 00217 // such a way that numbers with small magnitudes will encode to smaller 00218 // varints. If you simply static_cast a negative number to an unsigned 00219 // number and varint-encode it, it will always take 10 bytes, defeating 00220 // the purpose of varint. So, for the "sint32" and "sint64" field types, 00221 // we ZigZag-encode the values. 00222 static uint32 ZigZagEncode32(int32 n); 00223 static int32 ZigZagDecode32(uint32 n); 00224 static uint64 ZigZagEncode64(int64 n); 00225 static int64 ZigZagDecode64(uint64 n); 00226 00227 // ================================================================= 00228 // Methods for reading/writing individual field. The implementations 00229 // of these methods are defined in wire_format_lite_inl.h; you must #include 00230 // that file to use these. 00231 00232 // Avoid ugly line wrapping 00233 #define input io::CodedInputStream* input 00234 #define output io::CodedOutputStream* output 00235 #define field_number int field_number 00236 #define INL GOOGLE_ATTRIBUTE_ALWAYS_INLINE 00237 00238 // Read fields, not including tags. The assumption is that you already 00239 // read the tag to determine what field to read. 00240 00241 // For primitive fields, we just use a templatized routine parameterized by 00242 // the represented type and the FieldType. These are specialized with the 00243 // appropriate definition for each declared type. 00244 template <typename CType, enum FieldType DeclaredType> 00245 static inline bool ReadPrimitive(input, CType* value) INL; 00246 00247 // Reads repeated primitive values, with optimizations for repeats. 00248 // tag_size and tag should both be compile-time constants provided by the 00249 // protocol compiler. 00250 template <typename CType, enum FieldType DeclaredType> 00251 static inline bool ReadRepeatedPrimitive(int tag_size, 00252 uint32 tag, 00253 input, 00254 RepeatedField<CType>* value) INL; 00255 00256 // Identical to ReadRepeatedPrimitive, except will not inline the 00257 // implementation. 00258 template <typename CType, enum FieldType DeclaredType> 00259 static bool ReadRepeatedPrimitiveNoInline(int tag_size, 00260 uint32 tag, 00261 input, 00262 RepeatedField<CType>* value); 00263 00264 // Reads a primitive value directly from the provided buffer. It returns a 00265 // pointer past the segment of data that was read. 00266 // 00267 // This is only implemented for the types with fixed wire size, e.g. 00268 // float, double, and the (s)fixed* types. 00269 template <typename CType, enum FieldType DeclaredType> 00270 static inline const uint8* ReadPrimitiveFromArray(const uint8* buffer, 00271 CType* value) INL; 00272 00273 // Reads a primitive packed field. 00274 // 00275 // This is only implemented for packable types. 00276 template <typename CType, enum FieldType DeclaredType> 00277 static inline bool ReadPackedPrimitive(input, 00278 RepeatedField<CType>* value) INL; 00279 00280 // Identical to ReadPackedPrimitive, except will not inline the 00281 // implementation. 00282 template <typename CType, enum FieldType DeclaredType> 00283 static bool ReadPackedPrimitiveNoInline(input, RepeatedField<CType>* value); 00284 00285 // Read a packed enum field. Values for which is_valid() returns false are 00286 // dropped. 00287 static bool ReadPackedEnumNoInline(input, 00288 bool (*is_valid)(int), 00289 RepeatedField<int>* value); 00290 00291 static bool ReadString(input, string* value); 00292 static bool ReadBytes (input, string* value); 00293 00294 static inline bool ReadGroup (field_number, input, MessageLite* value); 00295 static inline bool ReadMessage(input, MessageLite* value); 00296 00297 // Like above, but de-virtualize the call to MergePartialFromCodedStream(). 00298 // The pointer must point at an instance of MessageType, *not* a subclass (or 00299 // the subclass must not override MergePartialFromCodedStream()). 00300 template<typename MessageType> 00301 static inline bool ReadGroupNoVirtual(field_number, input, 00302 MessageType* value); 00303 template<typename MessageType> 00304 static inline bool ReadMessageNoVirtual(input, MessageType* value); 00305 00306 // Write a tag. The Write*() functions typically include the tag, so 00307 // normally there's no need to call this unless using the Write*NoTag() 00308 // variants. 00309 static inline void WriteTag(field_number, WireType type, output) INL; 00310 00311 // Write fields, without tags. 00312 static inline void WriteInt32NoTag (int32 value, output) INL; 00313 static inline void WriteInt64NoTag (int64 value, output) INL; 00314 static inline void WriteUInt32NoTag (uint32 value, output) INL; 00315 static inline void WriteUInt64NoTag (uint64 value, output) INL; 00316 static inline void WriteSInt32NoTag (int32 value, output) INL; 00317 static inline void WriteSInt64NoTag (int64 value, output) INL; 00318 static inline void WriteFixed32NoTag (uint32 value, output) INL; 00319 static inline void WriteFixed64NoTag (uint64 value, output) INL; 00320 static inline void WriteSFixed32NoTag(int32 value, output) INL; 00321 static inline void WriteSFixed64NoTag(int64 value, output) INL; 00322 static inline void WriteFloatNoTag (float value, output) INL; 00323 static inline void WriteDoubleNoTag (double value, output) INL; 00324 static inline void WriteBoolNoTag (bool value, output) INL; 00325 static inline void WriteEnumNoTag (int value, output) INL; 00326 00327 // Write fields, including tags. 00328 static void WriteInt32 (field_number, int32 value, output); 00329 static void WriteInt64 (field_number, int64 value, output); 00330 static void WriteUInt32 (field_number, uint32 value, output); 00331 static void WriteUInt64 (field_number, uint64 value, output); 00332 static void WriteSInt32 (field_number, int32 value, output); 00333 static void WriteSInt64 (field_number, int64 value, output); 00334 static void WriteFixed32 (field_number, uint32 value, output); 00335 static void WriteFixed64 (field_number, uint64 value, output); 00336 static void WriteSFixed32(field_number, int32 value, output); 00337 static void WriteSFixed64(field_number, int64 value, output); 00338 static void WriteFloat (field_number, float value, output); 00339 static void WriteDouble (field_number, double value, output); 00340 static void WriteBool (field_number, bool value, output); 00341 static void WriteEnum (field_number, int value, output); 00342 00343 static void WriteString(field_number, const string& value, output); 00344 static void WriteBytes (field_number, const string& value, output); 00345 00346 static void WriteGroup( 00347 field_number, const MessageLite& value, output); 00348 static void WriteMessage( 00349 field_number, const MessageLite& value, output); 00350 // Like above, but these will check if the output stream has enough 00351 // space to write directly to a flat array. 00352 static void WriteGroupMaybeToArray( 00353 field_number, const MessageLite& value, output); 00354 static void WriteMessageMaybeToArray( 00355 field_number, const MessageLite& value, output); 00356 00357 // Like above, but de-virtualize the call to SerializeWithCachedSizes(). The 00358 // pointer must point at an instance of MessageType, *not* a subclass (or 00359 // the subclass must not override SerializeWithCachedSizes()). 00360 template<typename MessageType> 00361 static inline void WriteGroupNoVirtual( 00362 field_number, const MessageType& value, output); 00363 template<typename MessageType> 00364 static inline void WriteMessageNoVirtual( 00365 field_number, const MessageType& value, output); 00366 00367 #undef output 00368 #define output uint8* target 00369 00370 // Like above, but use only *ToArray methods of CodedOutputStream. 00371 static inline uint8* WriteTagToArray(field_number, WireType type, output) INL; 00372 00373 // Write fields, without tags. 00374 static inline uint8* WriteInt32NoTagToArray (int32 value, output) INL; 00375 static inline uint8* WriteInt64NoTagToArray (int64 value, output) INL; 00376 static inline uint8* WriteUInt32NoTagToArray (uint32 value, output) INL; 00377 static inline uint8* WriteUInt64NoTagToArray (uint64 value, output) INL; 00378 static inline uint8* WriteSInt32NoTagToArray (int32 value, output) INL; 00379 static inline uint8* WriteSInt64NoTagToArray (int64 value, output) INL; 00380 static inline uint8* WriteFixed32NoTagToArray (uint32 value, output) INL; 00381 static inline uint8* WriteFixed64NoTagToArray (uint64 value, output) INL; 00382 static inline uint8* WriteSFixed32NoTagToArray(int32 value, output) INL; 00383 static inline uint8* WriteSFixed64NoTagToArray(int64 value, output) INL; 00384 static inline uint8* WriteFloatNoTagToArray (float value, output) INL; 00385 static inline uint8* WriteDoubleNoTagToArray (double value, output) INL; 00386 static inline uint8* WriteBoolNoTagToArray (bool value, output) INL; 00387 static inline uint8* WriteEnumNoTagToArray (int value, output) INL; 00388 00389 // Write fields, including tags. 00390 static inline uint8* WriteInt32ToArray( 00391 field_number, int32 value, output) INL; 00392 static inline uint8* WriteInt64ToArray( 00393 field_number, int64 value, output) INL; 00394 static inline uint8* WriteUInt32ToArray( 00395 field_number, uint32 value, output) INL; 00396 static inline uint8* WriteUInt64ToArray( 00397 field_number, uint64 value, output) INL; 00398 static inline uint8* WriteSInt32ToArray( 00399 field_number, int32 value, output) INL; 00400 static inline uint8* WriteSInt64ToArray( 00401 field_number, int64 value, output) INL; 00402 static inline uint8* WriteFixed32ToArray( 00403 field_number, uint32 value, output) INL; 00404 static inline uint8* WriteFixed64ToArray( 00405 field_number, uint64 value, output) INL; 00406 static inline uint8* WriteSFixed32ToArray( 00407 field_number, int32 value, output) INL; 00408 static inline uint8* WriteSFixed64ToArray( 00409 field_number, int64 value, output) INL; 00410 static inline uint8* WriteFloatToArray( 00411 field_number, float value, output) INL; 00412 static inline uint8* WriteDoubleToArray( 00413 field_number, double value, output) INL; 00414 static inline uint8* WriteBoolToArray( 00415 field_number, bool value, output) INL; 00416 static inline uint8* WriteEnumToArray( 00417 field_number, int value, output) INL; 00418 00419 static inline uint8* WriteStringToArray( 00420 field_number, const string& value, output) INL; 00421 static inline uint8* WriteBytesToArray( 00422 field_number, const string& value, output) INL; 00423 00424 static inline uint8* WriteGroupToArray( 00425 field_number, const MessageLite& value, output) INL; 00426 static inline uint8* WriteMessageToArray( 00427 field_number, const MessageLite& value, output) INL; 00428 00429 // Like above, but de-virtualize the call to SerializeWithCachedSizes(). The 00430 // pointer must point at an instance of MessageType, *not* a subclass (or 00431 // the subclass must not override SerializeWithCachedSizes()). 00432 template<typename MessageType> 00433 static inline uint8* WriteGroupNoVirtualToArray( 00434 field_number, const MessageType& value, output) INL; 00435 template<typename MessageType> 00436 static inline uint8* WriteMessageNoVirtualToArray( 00437 field_number, const MessageType& value, output) INL; 00438 00439 #undef output 00440 #undef input 00441 #undef INL 00442 00443 #undef field_number 00444 00445 // Compute the byte size of a field. The XxSize() functions do NOT include 00446 // the tag, so you must also call TagSize(). (This is because, for repeated 00447 // fields, you should only call TagSize() once and multiply it by the element 00448 // count, but you may have to call XxSize() for each individual element.) 00449 static inline int Int32Size ( int32 value); 00450 static inline int Int64Size ( int64 value); 00451 static inline int UInt32Size (uint32 value); 00452 static inline int UInt64Size (uint64 value); 00453 static inline int SInt32Size ( int32 value); 00454 static inline int SInt64Size ( int64 value); 00455 static inline int EnumSize ( int value); 00456 00457 // These types always have the same size. 00458 static const int kFixed32Size = 4; 00459 static const int kFixed64Size = 8; 00460 static const int kSFixed32Size = 4; 00461 static const int kSFixed64Size = 8; 00462 static const int kFloatSize = 4; 00463 static const int kDoubleSize = 8; 00464 static const int kBoolSize = 1; 00465 00466 static inline int StringSize(const string& value); 00467 static inline int BytesSize (const string& value); 00468 00469 static inline int GroupSize (const MessageLite& value); 00470 static inline int MessageSize(const MessageLite& value); 00471 00472 // Like above, but de-virtualize the call to ByteSize(). The 00473 // pointer must point at an instance of MessageType, *not* a subclass (or 00474 // the subclass must not override ByteSize()). 00475 template<typename MessageType> 00476 static inline int GroupSizeNoVirtual (const MessageType& value); 00477 template<typename MessageType> 00478 static inline int MessageSizeNoVirtual(const MessageType& value); 00479 00480 private: 00481 // A helper method for the repeated primitive reader. This method has 00482 // optimizations for primitive types that have fixed size on the wire, and 00483 // can be read using potentially faster paths. 00484 template <typename CType, enum FieldType DeclaredType> 00485 static inline bool ReadRepeatedFixedSizePrimitive( 00486 int tag_size, 00487 uint32 tag, 00488 google::protobuf::io::CodedInputStream* input, 00489 RepeatedField<CType>* value) GOOGLE_ATTRIBUTE_ALWAYS_INLINE; 00490 00491 static const CppType kFieldTypeToCppTypeMap[]; 00492 static const WireFormatLite::WireType kWireTypeForFieldType[]; 00493 00494 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(WireFormatLite); 00495 }; 00496 00497 // A class which deals with unknown values. The default implementation just 00498 // discards them. WireFormat defines a subclass which writes to an 00499 // UnknownFieldSet. This class is used by ExtensionSet::ParseField(), since 00500 // ExtensionSet is part of the lite library but UnknownFieldSet is not. 00501 class LIBPROTOBUF_EXPORT FieldSkipper { 00502 public: 00503 FieldSkipper() {} 00504 virtual ~FieldSkipper() {} 00505 00506 // Skip a field whose tag has already been consumed. 00507 virtual bool SkipField(io::CodedInputStream* input, uint32 tag); 00508 00509 // Skip an entire message or group, up to an end-group tag (which is consumed) 00510 // or end-of-stream. 00511 virtual bool SkipMessage(io::CodedInputStream* input); 00512 00513 // Deal with an already-parsed unrecognized enum value. The default 00514 // implementation does nothing, but the UnknownFieldSet-based implementation 00515 // saves it as an unknown varint. 00516 virtual void SkipUnknownEnum(int field_number, int value); 00517 }; 00518 00519 // inline methods ==================================================== 00520 00521 inline WireFormatLite::CppType 00522 WireFormatLite::FieldTypeToCppType(FieldType type) { 00523 return kFieldTypeToCppTypeMap[type]; 00524 } 00525 00526 inline uint32 WireFormatLite::MakeTag(int field_number, WireType type) { 00527 return GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(field_number, type); 00528 } 00529 00530 inline WireFormatLite::WireType WireFormatLite::GetTagWireType(uint32 tag) { 00531 return static_cast<WireType>(tag & kTagTypeMask); 00532 } 00533 00534 inline int WireFormatLite::GetTagFieldNumber(uint32 tag) { 00535 return static_cast<int>(tag >> kTagTypeBits); 00536 } 00537 00538 inline int WireFormatLite::TagSize(int field_number, 00539 WireFormatLite::FieldType type) { 00540 int result = io::CodedOutputStream::VarintSize32( 00541 field_number << kTagTypeBits); 00542 if (type == TYPE_GROUP) { 00543 // Groups have both a start and an end tag. 00544 return result * 2; 00545 } else { 00546 return result; 00547 } 00548 } 00549 00550 inline uint32 WireFormatLite::EncodeFloat(float value) { 00551 union {float f; uint32 i;}; 00552 f = value; 00553 return i; 00554 } 00555 00556 inline float WireFormatLite::DecodeFloat(uint32 value) { 00557 union {float f; uint32 i;}; 00558 i = value; 00559 return f; 00560 } 00561 00562 inline uint64 WireFormatLite::EncodeDouble(double value) { 00563 union {double f; uint64 i;}; 00564 f = value; 00565 return i; 00566 } 00567 00568 inline double WireFormatLite::DecodeDouble(uint64 value) { 00569 union {double f; uint64 i;}; 00570 i = value; 00571 return f; 00572 } 00573 00574 // ZigZag Transform: Encodes signed integers so that they can be 00575 // effectively used with varint encoding. 00576 // 00577 // varint operates on unsigned integers, encoding smaller numbers into 00578 // fewer bytes. If you try to use it on a signed integer, it will treat 00579 // this number as a very large unsigned integer, which means that even 00580 // small signed numbers like -1 will take the maximum number of bytes 00581 // (10) to encode. ZigZagEncode() maps signed integers to unsigned 00582 // in such a way that those with a small absolute value will have smaller 00583 // encoded values, making them appropriate for encoding using varint. 00584 // 00585 // int32 -> uint32 00586 // ------------------------- 00587 // 0 -> 0 00588 // -1 -> 1 00589 // 1 -> 2 00590 // -2 -> 3 00591 // ... -> ... 00592 // 2147483647 -> 4294967294 00593 // -2147483648 -> 4294967295 00594 // 00595 // >> encode >> 00596 // << decode << 00597 00598 inline uint32 WireFormatLite::ZigZagEncode32(int32 n) { 00599 // Note: the right-shift must be arithmetic 00600 return (n << 1) ^ (n >> 31); 00601 } 00602 00603 inline int32 WireFormatLite::ZigZagDecode32(uint32 n) { 00604 return (n >> 1) ^ -static_cast<int32>(n & 1); 00605 } 00606 00607 inline uint64 WireFormatLite::ZigZagEncode64(int64 n) { 00608 // Note: the right-shift must be arithmetic 00609 return (n << 1) ^ (n >> 63); 00610 } 00611 00612 inline int64 WireFormatLite::ZigZagDecode64(uint64 n) { 00613 return (n >> 1) ^ -static_cast<int64>(n & 1); 00614 } 00615 00616 } // namespace internal 00617 } // namespace protobuf 00618 00619 } // namespace google 00620 #endif // GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_H__