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