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 // Defines Message, the abstract interface implemented by non-lite 00036 // protocol message objects. Although it's possible to implement this 00037 // interface manually, most users will use the protocol compiler to 00038 // generate implementations. 00039 // 00040 // Example usage: 00041 // 00042 // Say you have a message defined as: 00043 // 00044 // message Foo { 00045 // optional string text = 1; 00046 // repeated int32 numbers = 2; 00047 // } 00048 // 00049 // Then, if you used the protocol compiler to generate a class from the above 00050 // definition, you could use it like so: 00051 // 00052 // string data; // Will store a serialized version of the message. 00053 // 00054 // { 00055 // // Create a message and serialize it. 00056 // Foo foo; 00057 // foo.set_text("Hello World!"); 00058 // foo.add_numbers(1); 00059 // foo.add_numbers(5); 00060 // foo.add_numbers(42); 00061 // 00062 // foo.SerializeToString(&data); 00063 // } 00064 // 00065 // { 00066 // // Parse the serialized message and check that it contains the 00067 // // correct data. 00068 // Foo foo; 00069 // foo.ParseFromString(data); 00070 // 00071 // assert(foo.text() == "Hello World!"); 00072 // assert(foo.numbers_size() == 3); 00073 // assert(foo.numbers(0) == 1); 00074 // assert(foo.numbers(1) == 5); 00075 // assert(foo.numbers(2) == 42); 00076 // } 00077 // 00078 // { 00079 // // Same as the last block, but do it dynamically via the Message 00080 // // reflection interface. 00081 // Message* foo = new Foo; 00082 // Descriptor* descriptor = foo->GetDescriptor(); 00083 // 00084 // // Get the descriptors for the fields we're interested in and verify 00085 // // their types. 00086 // FieldDescriptor* text_field = descriptor->FindFieldByName("text"); 00087 // assert(text_field != NULL); 00088 // assert(text_field->type() == FieldDescriptor::TYPE_STRING); 00089 // assert(text_field->label() == FieldDescriptor::TYPE_OPTIONAL); 00090 // FieldDescriptor* numbers_field = descriptor->FindFieldByName("numbers"); 00091 // assert(numbers_field != NULL); 00092 // assert(numbers_field->type() == FieldDescriptor::TYPE_INT32); 00093 // assert(numbers_field->label() == FieldDescriptor::TYPE_REPEATED); 00094 // 00095 // // Parse the message. 00096 // foo->ParseFromString(data); 00097 // 00098 // // Use the reflection interface to examine the contents. 00099 // const Reflection* reflection = foo->GetReflection(); 00100 // assert(reflection->GetString(foo, text_field) == "Hello World!"); 00101 // assert(reflection->FieldSize(foo, numbers_field) == 3); 00102 // assert(reflection->GetRepeatedInt32(foo, numbers_field, 0) == 1); 00103 // assert(reflection->GetRepeatedInt32(foo, numbers_field, 1) == 5); 00104 // assert(reflection->GetRepeatedInt32(foo, numbers_field, 2) == 42); 00105 // 00106 // delete foo; 00107 // } 00108 00109 #ifndef GOOGLE_PROTOBUF_MESSAGE_H__ 00110 #define GOOGLE_PROTOBUF_MESSAGE_H__ 00111 00112 #include <vector> 00113 #include <string> 00114 00115 #ifdef __DECCXX 00116 // HP C++'s iosfwd doesn't work. 00117 #include <iostream> 00118 #else 00119 #include <iosfwd> 00120 #endif 00121 00122 #include <google/protobuf/message_lite.h> 00123 00124 #include <google/protobuf/stubs/common.h> 00125 00126 #if defined(_WIN32) && defined(GetMessage) 00127 // windows.h defines GetMessage() as a macro. Let's re-define it as an inline 00128 // function. This is necessary because Reflection has a method called 00129 // GetMessage() which we don't want overridden. The inline function should be 00130 // equivalent for C++ users. 00131 inline BOOL GetMessage_Win32( 00132 LPMSG lpMsg, HWND hWnd, 00133 UINT wMsgFilterMin, UINT wMsgFilterMax) { 00134 return GetMessage(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax); 00135 } 00136 #undef GetMessage 00137 inline BOOL GetMessage( 00138 LPMSG lpMsg, HWND hWnd, 00139 UINT wMsgFilterMin, UINT wMsgFilterMax) { 00140 return GetMessage_Win32(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax); 00141 } 00142 #endif 00143 00144 00145 namespace google { 00146 namespace protobuf { 00147 00148 // Defined in this file. 00149 class Message; 00150 class Reflection; 00151 class MessageFactory; 00152 00153 // Defined in other files. 00154 class Descriptor; // descriptor.h 00155 class FieldDescriptor; // descriptor.h 00156 class EnumDescriptor; // descriptor.h 00157 class EnumValueDescriptor; // descriptor.h 00158 namespace io { 00159 class ZeroCopyInputStream; // zero_copy_stream.h 00160 class ZeroCopyOutputStream; // zero_copy_stream.h 00161 class CodedInputStream; // coded_stream.h 00162 class CodedOutputStream; // coded_stream.h 00163 } 00164 class UnknownFieldSet; // unknown_field_set.h 00165 00166 // A container to hold message metadata. 00167 struct Metadata { 00168 const Descriptor* descriptor; 00169 const Reflection* reflection; 00170 }; 00171 00172 // Returns the EnumDescriptor for enum type E, which must be a 00173 // proto-declared enum type. Code generated by the protocol compiler 00174 // will include specializations of this template for each enum type declared. 00175 template <typename E> 00176 const EnumDescriptor* GetEnumDescriptor(); 00177 00178 // Abstract interface for protocol messages. 00179 // 00180 // See also MessageLite, which contains most every-day operations. Message 00181 // adds descriptors and reflection on top of that. 00182 // 00183 // The methods of this class that are virtual but not pure-virtual have 00184 // default implementations based on reflection. Message classes which are 00185 // optimized for speed will want to override these with faster implementations, 00186 // but classes optimized for code size may be happy with keeping them. See 00187 // the optimize_for option in descriptor.proto. 00188 class LIBPROTOBUF_EXPORT Message : public MessageLite { 00189 public: 00190 inline Message() {} 00191 virtual ~Message(); 00192 00193 // Basic Operations ------------------------------------------------ 00194 00195 // Construct a new instance of the same type. Ownership is passed to the 00196 // caller. (This is also defined in MessageLite, but is defined again here 00197 // for return-type covariance.) 00198 virtual Message* New() const = 0; 00199 00200 // Make this message into a copy of the given message. The given message 00201 // must have the same descriptor, but need not necessarily be the same class. 00202 // By default this is just implemented as "Clear(); MergeFrom(from);". 00203 virtual void CopyFrom(const Message& from); 00204 00205 // Merge the fields from the given message into this message. Singular 00206 // fields will be overwritten, except for embedded messages which will 00207 // be merged. Repeated fields will be concatenated. The given message 00208 // must be of the same type as this message (i.e. the exact same class). 00209 virtual void MergeFrom(const Message& from); 00210 00211 // Verifies that IsInitialized() returns true. GOOGLE_CHECK-fails otherwise, with 00212 // a nice error message. 00213 void CheckInitialized() const; 00214 00215 // Slowly build a list of all required fields that are not set. 00216 // This is much, much slower than IsInitialized() as it is implemented 00217 // purely via reflection. Generally, you should not call this unless you 00218 // have already determined that an error exists by calling IsInitialized(). 00219 void FindInitializationErrors(vector<string>* errors) const; 00220 00221 // Like FindInitializationErrors, but joins all the strings, delimited by 00222 // commas, and returns them. 00223 string InitializationErrorString() const; 00224 00225 // Clears all unknown fields from this message and all embedded messages. 00226 // Normally, if unknown tag numbers are encountered when parsing a message, 00227 // the tag and value are stored in the message's UnknownFieldSet and 00228 // then written back out when the message is serialized. This allows servers 00229 // which simply route messages to other servers to pass through messages 00230 // that have new field definitions which they don't yet know about. However, 00231 // this behavior can have security implications. To avoid it, call this 00232 // method after parsing. 00233 // 00234 // See Reflection::GetUnknownFields() for more on unknown fields. 00235 virtual void DiscardUnknownFields(); 00236 00237 // Computes (an estimate of) the total number of bytes currently used for 00238 // storing the message in memory. The default implementation calls the 00239 // Reflection object's SpaceUsed() method. 00240 virtual int SpaceUsed() const; 00241 00242 // Debugging & Testing---------------------------------------------- 00243 00244 // Generates a human readable form of this message, useful for debugging 00245 // and other purposes. 00246 string DebugString() const; 00247 // Like DebugString(), but with less whitespace. 00248 string ShortDebugString() const; 00249 // Like DebugString(), but do not escape UTF-8 byte sequences. 00250 string Utf8DebugString() const; 00251 // Convenience function useful in GDB. Prints DebugString() to stdout. 00252 void PrintDebugString() const; 00253 00254 // Heavy I/O ------------------------------------------------------- 00255 // Additional parsing and serialization methods not implemented by 00256 // MessageLite because they are not supported by the lite library. 00257 00258 // Parse a protocol buffer from a file descriptor. If successful, the entire 00259 // input will be consumed. 00260 bool ParseFromFileDescriptor(int file_descriptor); 00261 // Like ParseFromFileDescriptor(), but accepts messages that are missing 00262 // required fields. 00263 bool ParsePartialFromFileDescriptor(int file_descriptor); 00264 // Parse a protocol buffer from a C++ istream. If successful, the entire 00265 // input will be consumed. 00266 bool ParseFromIstream(istream* input); 00267 // Like ParseFromIstream(), but accepts messages that are missing 00268 // required fields. 00269 bool ParsePartialFromIstream(istream* input); 00270 00271 // Serialize the message and write it to the given file descriptor. All 00272 // required fields must be set. 00273 bool SerializeToFileDescriptor(int file_descriptor) const; 00274 // Like SerializeToFileDescriptor(), but allows missing required fields. 00275 bool SerializePartialToFileDescriptor(int file_descriptor) const; 00276 // Serialize the message and write it to the given C++ ostream. All 00277 // required fields must be set. 00278 bool SerializeToOstream(ostream* output) const; 00279 // Like SerializeToOstream(), but allows missing required fields. 00280 bool SerializePartialToOstream(ostream* output) const; 00281 00282 00283 // Reflection-based methods ---------------------------------------- 00284 // These methods are pure-virtual in MessageLite, but Message provides 00285 // reflection-based default implementations. 00286 00287 virtual string GetTypeName() const; 00288 virtual void Clear(); 00289 virtual bool IsInitialized() const; 00290 virtual void CheckTypeAndMergeFrom(const MessageLite& other); 00291 virtual bool MergePartialFromCodedStream(io::CodedInputStream* input); 00292 virtual int ByteSize() const; 00293 virtual void SerializeWithCachedSizes(io::CodedOutputStream* output) const; 00294 00295 private: 00296 // This is called only by the default implementation of ByteSize(), to 00297 // update the cached size. If you override ByteSize(), you do not need 00298 // to override this. If you do not override ByteSize(), you MUST override 00299 // this; the default implementation will crash. 00300 // 00301 // The method is private because subclasses should never call it; only 00302 // override it. Yes, C++ lets you do that. Crazy, huh? 00303 virtual void SetCachedSize(int size) const; 00304 00305 public: 00306 00307 // Introspection --------------------------------------------------- 00308 00309 // Typedef for backwards-compatibility. 00310 typedef google::protobuf::Reflection Reflection; 00311 00312 // Get a Descriptor for this message's type. This describes what 00313 // fields the message contains, the types of those fields, etc. 00314 const Descriptor* GetDescriptor() const { return GetMetadata().descriptor; } 00315 00316 // Get the Reflection interface for this Message, which can be used to 00317 // read and modify the fields of the Message dynamically (in other words, 00318 // without knowing the message type at compile time). This object remains 00319 // property of the Message. 00320 // 00321 // This method remains virtual in case a subclass does not implement 00322 // reflection and wants to override the default behavior. 00323 virtual const Reflection* GetReflection() const { 00324 return GetMetadata().reflection; 00325 } 00326 00327 protected: 00328 // Get a struct containing the metadata for the Message. Most subclasses only 00329 // need to implement this method, rather than the GetDescriptor() and 00330 // GetReflection() wrappers. 00331 virtual Metadata GetMetadata() const = 0; 00332 00333 00334 private: 00335 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Message); 00336 }; 00337 00338 // This interface contains methods that can be used to dynamically access 00339 // and modify the fields of a protocol message. Their semantics are 00340 // similar to the accessors the protocol compiler generates. 00341 // 00342 // To get the Reflection for a given Message, call Message::GetReflection(). 00343 // 00344 // This interface is separate from Message only for efficiency reasons; 00345 // the vast majority of implementations of Message will share the same 00346 // implementation of Reflection (GeneratedMessageReflection, 00347 // defined in generated_message.h), and all Messages of a particular class 00348 // should share the same Reflection object (though you should not rely on 00349 // the latter fact). 00350 // 00351 // There are several ways that these methods can be used incorrectly. For 00352 // example, any of the following conditions will lead to undefined 00353 // results (probably assertion failures): 00354 // - The FieldDescriptor is not a field of this message type. 00355 // - The method called is not appropriate for the field's type. For 00356 // each field type in FieldDescriptor::TYPE_*, there is only one 00357 // Get*() method, one Set*() method, and one Add*() method that is 00358 // valid for that type. It should be obvious which (except maybe 00359 // for TYPE_BYTES, which are represented using strings in C++). 00360 // - A Get*() or Set*() method for singular fields is called on a repeated 00361 // field. 00362 // - GetRepeated*(), SetRepeated*(), or Add*() is called on a non-repeated 00363 // field. 00364 // - The Message object passed to any method is not of the right type for 00365 // this Reflection object (i.e. message.GetReflection() != reflection). 00366 // 00367 // You might wonder why there is not any abstract representation for a field 00368 // of arbitrary type. E.g., why isn't there just a "GetField()" method that 00369 // returns "const Field&", where "Field" is some class with accessors like 00370 // "GetInt32Value()". The problem is that someone would have to deal with 00371 // allocating these Field objects. For generated message classes, having to 00372 // allocate space for an additional object to wrap every field would at least 00373 // double the message's memory footprint, probably worse. Allocating the 00374 // objects on-demand, on the other hand, would be expensive and prone to 00375 // memory leaks. So, instead we ended up with this flat interface. 00376 // 00377 // TODO(kenton): Create a utility class which callers can use to read and 00378 // write fields from a Reflection without paying attention to the type. 00379 class LIBPROTOBUF_EXPORT Reflection { 00380 public: 00381 // TODO(kenton): Remove parameter. 00382 inline Reflection() {} 00383 virtual ~Reflection(); 00384 00385 // Get the UnknownFieldSet for the message. This contains fields which 00386 // were seen when the Message was parsed but were not recognized according 00387 // to the Message's definition. 00388 virtual const UnknownFieldSet& GetUnknownFields( 00389 const Message& message) const = 0; 00390 // Get a mutable pointer to the UnknownFieldSet for the message. This 00391 // contains fields which were seen when the Message was parsed but were not 00392 // recognized according to the Message's definition. 00393 virtual UnknownFieldSet* MutableUnknownFields(Message* message) const = 0; 00394 00395 // Estimate the amount of memory used by the message object. 00396 virtual int SpaceUsed(const Message& message) const = 0; 00397 00398 // Check if the given non-repeated field is set. 00399 virtual bool HasField(const Message& message, 00400 const FieldDescriptor* field) const = 0; 00401 00402 // Get the number of elements of a repeated field. 00403 virtual int FieldSize(const Message& message, 00404 const FieldDescriptor* field) const = 0; 00405 00406 // Clear the value of a field, so that HasField() returns false or 00407 // FieldSize() returns zero. 00408 virtual void ClearField(Message* message, 00409 const FieldDescriptor* field) const = 0; 00410 00411 // Remove the last element of a repeated field. 00412 // We don't provide a way to remove any element other than the last 00413 // because it invites inefficient use, such as O(n^2) filtering loops 00414 // that should have been O(n). If you want to remove an element other 00415 // than the last, the best way to do it is to re-arrange the elements 00416 // (using Swap()) so that the one you want removed is at the end, then 00417 // call RemoveLast(). 00418 virtual void RemoveLast(Message* message, 00419 const FieldDescriptor* field) const = 0; 00420 00421 // Swap the complete contents of two messages. 00422 virtual void Swap(Message* message1, Message* message2) const = 0; 00423 00424 // Swap two elements of a repeated field. 00425 virtual void SwapElements(Message* message, 00426 const FieldDescriptor* field, 00427 int index1, 00428 int index2) const = 0; 00429 00430 // List all fields of the message which are currently set. This includes 00431 // extensions. Singular fields will only be listed if HasField(field) would 00432 // return true and repeated fields will only be listed if FieldSize(field) 00433 // would return non-zero. Fields (both normal fields and extension fields) 00434 // will be listed ordered by field number. 00435 virtual void ListFields(const Message& message, 00436 vector<const FieldDescriptor*>* output) const = 0; 00437 00438 // Singular field getters ------------------------------------------ 00439 // These get the value of a non-repeated field. They return the default 00440 // value for fields that aren't set. 00441 00442 virtual int32 GetInt32 (const Message& message, 00443 const FieldDescriptor* field) const = 0; 00444 virtual int64 GetInt64 (const Message& message, 00445 const FieldDescriptor* field) const = 0; 00446 virtual uint32 GetUInt32(const Message& message, 00447 const FieldDescriptor* field) const = 0; 00448 virtual uint64 GetUInt64(const Message& message, 00449 const FieldDescriptor* field) const = 0; 00450 virtual float GetFloat (const Message& message, 00451 const FieldDescriptor* field) const = 0; 00452 virtual double GetDouble(const Message& message, 00453 const FieldDescriptor* field) const = 0; 00454 virtual bool GetBool (const Message& message, 00455 const FieldDescriptor* field) const = 0; 00456 virtual string GetString(const Message& message, 00457 const FieldDescriptor* field) const = 0; 00458 virtual const EnumValueDescriptor* GetEnum( 00459 const Message& message, const FieldDescriptor* field) const = 0; 00460 // See MutableMessage() for the meaning of the "factory" parameter. 00461 virtual const Message& GetMessage(const Message& message, 00462 const FieldDescriptor* field, 00463 MessageFactory* factory = NULL) const = 0; 00464 00465 // Get a string value without copying, if possible. 00466 // 00467 // GetString() necessarily returns a copy of the string. This can be 00468 // inefficient when the string is already stored in a string object in the 00469 // underlying message. GetStringReference() will return a reference to the 00470 // underlying string in this case. Otherwise, it will copy the string into 00471 // *scratch and return that. 00472 // 00473 // Note: It is perfectly reasonable and useful to write code like: 00474 // str = reflection->GetStringReference(field, &str); 00475 // This line would ensure that only one copy of the string is made 00476 // regardless of the field's underlying representation. When initializing 00477 // a newly-constructed string, though, it's just as fast and more readable 00478 // to use code like: 00479 // string str = reflection->GetString(field); 00480 virtual const string& GetStringReference(const Message& message, 00481 const FieldDescriptor* field, 00482 string* scratch) const = 0; 00483 00484 00485 // Singular field mutators ----------------------------------------- 00486 // These mutate the value of a non-repeated field. 00487 00488 virtual void SetInt32 (Message* message, 00489 const FieldDescriptor* field, int32 value) const = 0; 00490 virtual void SetInt64 (Message* message, 00491 const FieldDescriptor* field, int64 value) const = 0; 00492 virtual void SetUInt32(Message* message, 00493 const FieldDescriptor* field, uint32 value) const = 0; 00494 virtual void SetUInt64(Message* message, 00495 const FieldDescriptor* field, uint64 value) const = 0; 00496 virtual void SetFloat (Message* message, 00497 const FieldDescriptor* field, float value) const = 0; 00498 virtual void SetDouble(Message* message, 00499 const FieldDescriptor* field, double value) const = 0; 00500 virtual void SetBool (Message* message, 00501 const FieldDescriptor* field, bool value) const = 0; 00502 virtual void SetString(Message* message, 00503 const FieldDescriptor* field, 00504 const string& value) const = 0; 00505 virtual void SetEnum (Message* message, 00506 const FieldDescriptor* field, 00507 const EnumValueDescriptor* value) const = 0; 00508 // Get a mutable pointer to a field with a message type. If a MessageFactory 00509 // is provided, it will be used to construct instances of the sub-message; 00510 // otherwise, the default factory is used. If the field is an extension that 00511 // does not live in the same pool as the containing message's descriptor (e.g. 00512 // it lives in an overlay pool), then a MessageFactory must be provided. 00513 // If you have no idea what that meant, then you probably don't need to worry 00514 // about it (don't provide a MessageFactory). WARNING: If the 00515 // FieldDescriptor is for a compiled-in extension, then 00516 // factory->GetPrototype(field->message_type() MUST return an instance of the 00517 // compiled-in class for this type, NOT DynamicMessage. 00518 virtual Message* MutableMessage(Message* message, 00519 const FieldDescriptor* field, 00520 MessageFactory* factory = NULL) const = 0; 00521 00522 00523 // Repeated field getters ------------------------------------------ 00524 // These get the value of one element of a repeated field. 00525 00526 virtual int32 GetRepeatedInt32 (const Message& message, 00527 const FieldDescriptor* field, 00528 int index) const = 0; 00529 virtual int64 GetRepeatedInt64 (const Message& message, 00530 const FieldDescriptor* field, 00531 int index) const = 0; 00532 virtual uint32 GetRepeatedUInt32(const Message& message, 00533 const FieldDescriptor* field, 00534 int index) const = 0; 00535 virtual uint64 GetRepeatedUInt64(const Message& message, 00536 const FieldDescriptor* field, 00537 int index) const = 0; 00538 virtual float GetRepeatedFloat (const Message& message, 00539 const FieldDescriptor* field, 00540 int index) const = 0; 00541 virtual double GetRepeatedDouble(const Message& message, 00542 const FieldDescriptor* field, 00543 int index) const = 0; 00544 virtual bool GetRepeatedBool (const Message& message, 00545 const FieldDescriptor* field, 00546 int index) const = 0; 00547 virtual string GetRepeatedString(const Message& message, 00548 const FieldDescriptor* field, 00549 int index) const = 0; 00550 virtual const EnumValueDescriptor* GetRepeatedEnum( 00551 const Message& message, 00552 const FieldDescriptor* field, int index) const = 0; 00553 virtual const Message& GetRepeatedMessage( 00554 const Message& message, 00555 const FieldDescriptor* field, int index) const = 0; 00556 00557 // See GetStringReference(), above. 00558 virtual const string& GetRepeatedStringReference( 00559 const Message& message, const FieldDescriptor* field, 00560 int index, string* scratch) const = 0; 00561 00562 00563 // Repeated field mutators ----------------------------------------- 00564 // These mutate the value of one element of a repeated field. 00565 00566 virtual void SetRepeatedInt32 (Message* message, 00567 const FieldDescriptor* field, 00568 int index, int32 value) const = 0; 00569 virtual void SetRepeatedInt64 (Message* message, 00570 const FieldDescriptor* field, 00571 int index, int64 value) const = 0; 00572 virtual void SetRepeatedUInt32(Message* message, 00573 const FieldDescriptor* field, 00574 int index, uint32 value) const = 0; 00575 virtual void SetRepeatedUInt64(Message* message, 00576 const FieldDescriptor* field, 00577 int index, uint64 value) const = 0; 00578 virtual void SetRepeatedFloat (Message* message, 00579 const FieldDescriptor* field, 00580 int index, float value) const = 0; 00581 virtual void SetRepeatedDouble(Message* message, 00582 const FieldDescriptor* field, 00583 int index, double value) const = 0; 00584 virtual void SetRepeatedBool (Message* message, 00585 const FieldDescriptor* field, 00586 int index, bool value) const = 0; 00587 virtual void SetRepeatedString(Message* message, 00588 const FieldDescriptor* field, 00589 int index, const string& value) const = 0; 00590 virtual void SetRepeatedEnum(Message* message, 00591 const FieldDescriptor* field, int index, 00592 const EnumValueDescriptor* value) const = 0; 00593 // Get a mutable pointer to an element of a repeated field with a message 00594 // type. 00595 virtual Message* MutableRepeatedMessage( 00596 Message* message, const FieldDescriptor* field, int index) const = 0; 00597 00598 00599 // Repeated field adders ------------------------------------------- 00600 // These add an element to a repeated field. 00601 00602 virtual void AddInt32 (Message* message, 00603 const FieldDescriptor* field, int32 value) const = 0; 00604 virtual void AddInt64 (Message* message, 00605 const FieldDescriptor* field, int64 value) const = 0; 00606 virtual void AddUInt32(Message* message, 00607 const FieldDescriptor* field, uint32 value) const = 0; 00608 virtual void AddUInt64(Message* message, 00609 const FieldDescriptor* field, uint64 value) const = 0; 00610 virtual void AddFloat (Message* message, 00611 const FieldDescriptor* field, float value) const = 0; 00612 virtual void AddDouble(Message* message, 00613 const FieldDescriptor* field, double value) const = 0; 00614 virtual void AddBool (Message* message, 00615 const FieldDescriptor* field, bool value) const = 0; 00616 virtual void AddString(Message* message, 00617 const FieldDescriptor* field, 00618 const string& value) const = 0; 00619 virtual void AddEnum (Message* message, 00620 const FieldDescriptor* field, 00621 const EnumValueDescriptor* value) const = 0; 00622 // See MutableMessage() for comments on the "factory" parameter. 00623 virtual Message* AddMessage(Message* message, 00624 const FieldDescriptor* field, 00625 MessageFactory* factory = NULL) const = 0; 00626 00627 00628 // Extensions ------------------------------------------------------ 00629 00630 // Try to find an extension of this message type by fully-qualified field 00631 // name. Returns NULL if no extension is known for this name or number. 00632 virtual const FieldDescriptor* FindKnownExtensionByName( 00633 const string& name) const = 0; 00634 00635 // Try to find an extension of this message type by field number. 00636 // Returns NULL if no extension is known for this name or number. 00637 virtual const FieldDescriptor* FindKnownExtensionByNumber( 00638 int number) const = 0; 00639 00640 private: 00641 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Reflection); 00642 }; 00643 00644 // Abstract interface for a factory for message objects. 00645 class LIBPROTOBUF_EXPORT MessageFactory { 00646 public: 00647 inline MessageFactory() {} 00648 virtual ~MessageFactory(); 00649 00650 // Given a Descriptor, gets or constructs the default (prototype) Message 00651 // of that type. You can then call that message's New() method to construct 00652 // a mutable message of that type. 00653 // 00654 // Calling this method twice with the same Descriptor returns the same 00655 // object. The returned object remains property of the factory. Also, any 00656 // objects created by calling the prototype's New() method share some data 00657 // with the prototype, so these must be destoyed before the MessageFactory 00658 // is destroyed. 00659 // 00660 // The given descriptor must outlive the returned message, and hence must 00661 // outlive the MessageFactory. 00662 // 00663 // Some implementations do not support all types. GetPrototype() will 00664 // return NULL if the descriptor passed in is not supported. 00665 // 00666 // This method may or may not be thread-safe depending on the implementation. 00667 // Each implementation should document its own degree thread-safety. 00668 virtual const Message* GetPrototype(const Descriptor* type) = 0; 00669 00670 // Gets a MessageFactory which supports all generated, compiled-in messages. 00671 // In other words, for any compiled-in type FooMessage, the following is true: 00672 // MessageFactory::generated_factory()->GetPrototype( 00673 // FooMessage::descriptor()) == FooMessage::default_instance() 00674 // This factory supports all types which are found in 00675 // DescriptorPool::generated_pool(). If given a descriptor from any other 00676 // pool, GetPrototype() will return NULL. (You can also check if a 00677 // descriptor is for a generated message by checking if 00678 // descriptor->file()->pool() == DescriptorPool::generated_pool().) 00679 // 00680 // This factory is 100% thread-safe; calling GetPrototype() does not modify 00681 // any shared data. 00682 // 00683 // This factory is a singleton. The caller must not delete the object. 00684 static MessageFactory* generated_factory(); 00685 00686 // For internal use only: Registers a .proto file at static initialization 00687 // time, to be placed in generated_factory. The first time GetPrototype() 00688 // is called with a descriptor from this file, |register_messages| will be 00689 // called, with the file name as the parameter. It must call 00690 // InternalRegisterGeneratedMessage() (below) to register each message type 00691 // in the file. This strange mechanism is necessary because descriptors are 00692 // built lazily, so we can't register types by their descriptor until we 00693 // know that the descriptor exists. |filename| must be a permanent string. 00694 static void InternalRegisterGeneratedFile( 00695 const char* filename, void (*register_messages)(const string&)); 00696 00697 // For internal use only: Registers a message type. Called only by the 00698 // functions which are registered with InternalRegisterGeneratedFile(), 00699 // above. 00700 static void InternalRegisterGeneratedMessage(const Descriptor* descriptor, 00701 const Message* prototype); 00702 00703 private: 00704 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageFactory); 00705 }; 00706 00707 } // namespace protobuf 00708 00709 } // namespace google 00710 #endif // GOOGLE_PROTOBUF_MESSAGE_H__