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 // Based on original Protocol Buffers design by 00033 // Sanjay Ghemawat, Jeff Dean, and others. 00034 // 00035 // Contains classes used to keep track of unrecognized fields seen while 00036 // parsing a protocol message. 00037 00038 #ifndef GOOGLE_PROTOBUF_UNKNOWN_FIELD_SET_H__ 00039 #define GOOGLE_PROTOBUF_UNKNOWN_FIELD_SET_H__ 00040 00041 #include <string> 00042 #include <vector> 00043 #include <google/protobuf/repeated_field.h> 00044 00045 namespace google { 00046 namespace protobuf { 00047 00048 class Message; // message.h 00049 class UnknownField; // below 00050 00051 // An UnknownFieldSet contains fields that were encountered while parsing a 00052 // message but were not defined by its type. Keeping track of these can be 00053 // useful, especially in that they may be written if the message is serialized 00054 // again without being cleared in between. This means that software which 00055 // simply receives messages and forwards them to other servers does not need 00056 // to be updated every time a new field is added to the message definition. 00057 // 00058 // To get the UnknownFieldSet attached to any message, call 00059 // Reflection::GetUnknownFields(). 00060 // 00061 // This class is necessarily tied to the protocol buffer wire format, unlike 00062 // the Reflection interface which is independent of any serialization scheme. 00063 class LIBPROTOBUF_EXPORT UnknownFieldSet { 00064 public: 00065 UnknownFieldSet(); 00066 ~UnknownFieldSet(); 00067 00068 // Remove all fields. 00069 inline void Clear(); 00070 00071 // Is this set empty? 00072 inline bool empty() const; 00073 00074 // Merge the contents of some other UnknownFieldSet with this one. 00075 void MergeFrom(const UnknownFieldSet& other); 00076 00077 // Swaps the contents of some other UnknownFieldSet with this one. 00078 inline void Swap(UnknownFieldSet* x); 00079 00080 // Computes (an estimate of) the total number of bytes currently used for 00081 // storing the unknown fields in memory. Does NOT include 00082 // sizeof(*this) in the calculation. 00083 int SpaceUsedExcludingSelf() const; 00084 00085 // Version of SpaceUsed() including sizeof(*this). 00086 int SpaceUsed() const; 00087 00088 // Returns the number of fields present in the UnknownFieldSet. 00089 inline int field_count() const; 00090 // Get a field in the set, where 0 <= index < field_count(). The fields 00091 // appear in the order in which they were added. 00092 inline const UnknownField& field(int index) const; 00093 // Get a mutable pointer to a field in the set, where 00094 // 0 <= index < field_count(). The fields appear in the order in which 00095 // they were added. 00096 inline UnknownField* mutable_field(int index); 00097 00098 // Adding fields --------------------------------------------------- 00099 00100 void AddVarint(int number, uint64 value); 00101 void AddFixed32(int number, uint32 value); 00102 void AddFixed64(int number, uint64 value); 00103 void AddLengthDelimited(int number, const string& value); 00104 string* AddLengthDelimited(int number); 00105 UnknownFieldSet* AddGroup(int number); 00106 00107 // Adds an unknown field from another set. 00108 void AddField(const UnknownField& field); 00109 00110 // Parsing helpers ------------------------------------------------- 00111 // These work exactly like the similarly-named methods of Message. 00112 00113 bool MergeFromCodedStream(io::CodedInputStream* input); 00114 bool ParseFromCodedStream(io::CodedInputStream* input); 00115 bool ParseFromZeroCopyStream(io::ZeroCopyInputStream* input); 00116 bool ParseFromArray(const void* data, int size); 00117 inline bool ParseFromString(const string& data) { 00118 return ParseFromArray(data.data(), data.size()); 00119 } 00120 00121 private: 00122 void ClearFallback(); 00123 00124 vector<UnknownField>* fields_; 00125 00126 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(UnknownFieldSet); 00127 }; 00128 00129 // Represents one field in an UnknownFieldSet. 00130 class LIBPROTOBUF_EXPORT UnknownField { 00131 public: 00132 enum Type { 00133 TYPE_VARINT, 00134 TYPE_FIXED32, 00135 TYPE_FIXED64, 00136 TYPE_LENGTH_DELIMITED, 00137 TYPE_GROUP 00138 }; 00139 00140 // The field's tag number, as seen on the wire. 00141 inline int number() const; 00142 00143 // The field type. 00144 inline Type type() const; 00145 00146 // Accessors ------------------------------------------------------- 00147 // Each method works only for UnknownFields of the corresponding type. 00148 00149 inline uint64 varint() const; 00150 inline uint32 fixed32() const; 00151 inline uint64 fixed64() const; 00152 inline const string& length_delimited() const; 00153 inline const UnknownFieldSet& group() const; 00154 00155 inline void set_varint(uint64 value); 00156 inline void set_fixed32(uint32 value); 00157 inline void set_fixed64(uint64 value); 00158 inline void set_length_delimited(const string& value); 00159 inline string* mutable_length_delimited(); 00160 inline UnknownFieldSet* mutable_group(); 00161 00162 private: 00163 friend class UnknownFieldSet; 00164 00165 // If this UnknownField contains a pointer, delete it. 00166 void Delete(); 00167 00168 // Make a deep copy of any pointers in this UnknownField. 00169 void DeepCopy(); 00170 00171 unsigned int number_ : 29; 00172 unsigned int type_ : 3; 00173 union { 00174 uint64 varint_; 00175 uint32 fixed32_; 00176 uint64 fixed64_; 00177 string* length_delimited_; 00178 UnknownFieldSet* group_; 00179 }; 00180 }; 00181 00182 // =================================================================== 00183 // inline implementations 00184 00185 inline void UnknownFieldSet::Clear() { 00186 if (fields_ != NULL) { 00187 ClearFallback(); 00188 } 00189 } 00190 00191 inline bool UnknownFieldSet::empty() const { 00192 return fields_ == NULL || fields_->empty(); 00193 } 00194 00195 inline void UnknownFieldSet::Swap(UnknownFieldSet* x) { 00196 std::swap(fields_, x->fields_); 00197 } 00198 00199 inline int UnknownFieldSet::field_count() const { 00200 return (fields_ == NULL) ? 0 : fields_->size(); 00201 } 00202 inline const UnknownField& UnknownFieldSet::field(int index) const { 00203 return (*fields_)[index]; 00204 } 00205 inline UnknownField* UnknownFieldSet::mutable_field(int index) { 00206 return &(*fields_)[index]; 00207 } 00208 00209 inline void UnknownFieldSet::AddLengthDelimited( 00210 int number, const string& value) { 00211 AddLengthDelimited(number)->assign(value); 00212 } 00213 00214 inline int UnknownField::number() const { return number_; } 00215 inline UnknownField::Type UnknownField::type() const { 00216 return static_cast<Type>(type_); 00217 } 00218 00219 inline uint64 UnknownField::varint () const { 00220 GOOGLE_DCHECK_EQ(type_, TYPE_VARINT); 00221 return varint_; 00222 } 00223 inline uint32 UnknownField::fixed32() const { 00224 GOOGLE_DCHECK_EQ(type_, TYPE_FIXED32); 00225 return fixed32_; 00226 } 00227 inline uint64 UnknownField::fixed64() const { 00228 GOOGLE_DCHECK_EQ(type_, TYPE_FIXED64); 00229 return fixed64_; 00230 } 00231 inline const string& UnknownField::length_delimited() const { 00232 GOOGLE_DCHECK_EQ(type_, TYPE_LENGTH_DELIMITED); 00233 return *length_delimited_; 00234 } 00235 inline const UnknownFieldSet& UnknownField::group() const { 00236 GOOGLE_DCHECK_EQ(type_, TYPE_GROUP); 00237 return *group_; 00238 } 00239 00240 inline void UnknownField::set_varint(uint64 value) { 00241 GOOGLE_DCHECK_EQ(type_, TYPE_VARINT); 00242 varint_ = value; 00243 } 00244 inline void UnknownField::set_fixed32(uint32 value) { 00245 GOOGLE_DCHECK_EQ(type_, TYPE_FIXED32); 00246 fixed32_ = value; 00247 } 00248 inline void UnknownField::set_fixed64(uint64 value) { 00249 GOOGLE_DCHECK_EQ(type_, TYPE_FIXED64); 00250 fixed64_ = value; 00251 } 00252 inline void UnknownField::set_length_delimited(const string& value) { 00253 GOOGLE_DCHECK_EQ(type_, TYPE_LENGTH_DELIMITED); 00254 length_delimited_->assign(value); 00255 } 00256 inline string* UnknownField::mutable_length_delimited() { 00257 GOOGLE_DCHECK_EQ(type_, TYPE_LENGTH_DELIMITED); 00258 return length_delimited_; 00259 } 00260 inline UnknownFieldSet* UnknownField::mutable_group() { 00261 GOOGLE_DCHECK_EQ(type_, TYPE_GROUP); 00262 return group_; 00263 } 00264 00265 } // namespace protobuf 00266 00267 } // namespace google 00268 #endif // GOOGLE_PROTOBUF_UNKNOWN_FIELD_SET_H__