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 an implementation of Message which can emulate types which are not 00036 // known at compile-time. 00037 00038 #ifndef GOOGLE_PROTOBUF_DYNAMIC_MESSAGE_H__ 00039 #define GOOGLE_PROTOBUF_DYNAMIC_MESSAGE_H__ 00040 00041 #include <google/protobuf/message.h> 00042 #include <google/protobuf/stubs/common.h> 00043 00044 namespace google { 00045 namespace protobuf { 00046 00047 // Defined in other files. 00048 class Descriptor; // descriptor.h 00049 class DescriptorPool; // descriptor.h 00050 00051 // Constructs implementations of Message which can emulate types which are not 00052 // known at compile-time. 00053 // 00054 // Sometimes you want to be able to manipulate protocol types that you don't 00055 // know about at compile time. It would be nice to be able to construct 00056 // a Message object which implements the message type given by any arbitrary 00057 // Descriptor. DynamicMessage provides this. 00058 // 00059 // As it turns out, a DynamicMessage needs to construct extra 00060 // information about its type in order to operate. Most of this information 00061 // can be shared between all DynamicMessages of the same type. But, caching 00062 // this information in some sort of global map would be a bad idea, since 00063 // the cached information for a particular descriptor could outlive the 00064 // descriptor itself. To avoid this problem, DynamicMessageFactory 00065 // encapsulates this "cache". All DynamicMessages of the same type created 00066 // from the same factory will share the same support data. Any Descriptors 00067 // used with a particular factory must outlive the factory. 00068 class LIBPROTOBUF_EXPORT DynamicMessageFactory : public MessageFactory { 00069 public: 00070 // Construct a DynamicMessageFactory that will search for extensions in 00071 // the DescriptorPool in which the exendee is defined. 00072 DynamicMessageFactory(); 00073 00074 // Construct a DynamicMessageFactory that will search for extensions in 00075 // the given DescriptorPool. 00076 // 00077 // DEPRECATED: Use CodedInputStream::SetExtensionRegistry() to tell the 00078 // parser to look for extensions in an alternate pool. However, note that 00079 // this is almost never what you want to do. Almost all users should use 00080 // the zero-arg constructor. 00081 DynamicMessageFactory(const DescriptorPool* pool); 00082 00083 ~DynamicMessageFactory(); 00084 00085 // Call this to tell the DynamicMessageFactory that if it is given a 00086 // Descriptor d for which: 00087 // d->file()->pool() == DescriptorPool::generated_pool(), 00088 // then it should delegate to MessageFactory::generated_factory() instead 00089 // of constructing a dynamic implementation of the message. In theory there 00090 // is no down side to doing this, so it may become the default in the future. 00091 void SetDelegateToGeneratedFactory(bool enable) { 00092 delegate_to_generated_factory_ = enable; 00093 } 00094 00095 // implements MessageFactory --------------------------------------- 00096 00097 // Given a Descriptor, constructs the default (prototype) Message of that 00098 // type. You can then call that message's New() method to construct a 00099 // mutable message of that type. 00100 // 00101 // Calling this method twice with the same Descriptor returns the same 00102 // object. The returned object remains property of the factory and will 00103 // be destroyed when the factory is destroyed. Also, any objects created 00104 // by calling the prototype's New() method share some data with the 00105 // prototype, so these must be destoyed before the DynamicMessageFactory 00106 // is destroyed. 00107 // 00108 // The given descriptor must outlive the returned message, and hence must 00109 // outlive the DynamicMessageFactory. 00110 // 00111 // The method is thread-safe. 00112 const Message* GetPrototype(const Descriptor* type); 00113 00114 private: 00115 const DescriptorPool* pool_; 00116 bool delegate_to_generated_factory_; 00117 00118 // This struct just contains a hash_map. We can't #include <google/protobuf/stubs/hash.h> from 00119 // this header due to hacks needed for hash_map portability in the open source 00120 // release. Namely, stubs/hash.h, which defines hash_map portably, is not a 00121 // public header (for good reason), but dynamic_message.h is, and public 00122 // headers may only #include other public headers. 00123 struct PrototypeMap; 00124 scoped_ptr<PrototypeMap> prototypes_; 00125 mutable Mutex prototypes_mutex_; 00126 00127 friend class DynamicMessage; 00128 const Message* GetPrototypeNoLock(const Descriptor* type); 00129 00130 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DynamicMessageFactory); 00131 }; 00132 00133 } // namespace protobuf 00134 00135 } // namespace google 00136 #endif // GOOGLE_PROTOBUF_DYNAMIC_MESSAGE_H__