BWAPI
Undermind/proxy/cpp/include/google/protobuf/message_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 // Authors: wink@google.com (Wink Saville),
00032 //          kenton@google.com (Kenton Varda)
00033 //  Based on original Protocol Buffers design by
00034 //  Sanjay Ghemawat, Jeff Dean, and others.
00035 //
00036 // Defines MessageLite, the abstract interface implemented by all (lite
00037 // and non-lite) protocol message objects.
00038 
00039 #ifndef GOOGLE_PROTOBUF_MESSAGE_LITE_H__
00040 #define GOOGLE_PROTOBUF_MESSAGE_LITE_H__
00041 
00042 #include <google/protobuf/stubs/common.h>
00043 #include <google/protobuf/io/coded_stream.h>
00044 
00045 namespace google {
00046 namespace protobuf {
00047 
00048 // Interface to light weight protocol messages.
00049 //
00050 // This interface is implemented by all protocol message objects.  Non-lite
00051 // messages additionally implement the Message interface, which is a
00052 // subclass of MessageLite.  Use MessageLite instead when you only need
00053 // the subset of features which it supports -- namely, nothing that uses
00054 // descriptors or reflection.  You can instruct the protocol compiler
00055 // to generate classes which implement only MessageLite, not the full
00056 // Message interface, by adding the following line to the .proto file:
00057 //
00058 //   option optimize_for = LITE_RUNTIME;
00059 //
00060 // This is particularly useful on resource-constrained systems where
00061 // the full protocol buffers runtime library is too big.
00062 //
00063 // Note that on non-constrained systems (e.g. servers) when you need
00064 // to link in lots of protocol definitions, a better way to reduce
00065 // total code footprint is to use optimize_for = CODE_SIZE.  This
00066 // will make the generated code smaller while still supporting all the
00067 // same features (at the expense of speed).  optimize_for = LITE_RUNTIME
00068 // is best when you only have a small number of message types linked
00069 // into your binary, in which case the size of the protocol buffers
00070 // runtime itself is the biggest problem.
00071 class LIBPROTOBUF_EXPORT MessageLite {
00072  public:
00073   inline MessageLite() {}
00074   virtual ~MessageLite();
00075 
00076   // Basic Operations ------------------------------------------------
00077 
00078   // Get the name of this message type, e.g. "foo.bar.BazProto".
00079   virtual string GetTypeName() const = 0;
00080 
00081   // Construct a new instance of the same type.  Ownership is passed to the
00082   // caller.
00083   virtual MessageLite* New() const = 0;
00084 
00085   // Clear all fields of the message and set them to their default values.
00086   // Clear() avoids freeing memory, assuming that any memory allocated
00087   // to hold parts of the message will be needed again to hold the next
00088   // message.  If you actually want to free the memory used by a Message,
00089   // you must delete it.
00090   virtual void Clear() = 0;
00091 
00092   // Quickly check if all required fields have values set.
00093   virtual bool IsInitialized() const = 0;
00094 
00095   // This is not implemented for Lite messages -- it just returns "(cannot
00096   // determine missing fields for lite message)".  However, it is implemented
00097   // for full messages.  See message.h.
00098   virtual string InitializationErrorString() const;
00099 
00100   // If |other| is the exact same class as this, calls MergeFrom().  Otherwise,
00101   // results are undefined (probably crash).
00102   virtual void CheckTypeAndMergeFrom(const MessageLite& other) = 0;
00103 
00104   // Parsing ---------------------------------------------------------
00105   // Methods for parsing in protocol buffer format.  Most of these are
00106   // just simple wrappers around MergeFromCodedStream().
00107 
00108   // Fill the message with a protocol buffer parsed from the given input
00109   // stream.  Returns false on a read error or if the input is in the
00110   // wrong format.
00111   bool ParseFromCodedStream(io::CodedInputStream* input);
00112   // Like ParseFromCodedStream(), but accepts messages that are missing
00113   // required fields.
00114   bool ParsePartialFromCodedStream(io::CodedInputStream* input);
00115   // Read a protocol buffer from the given zero-copy input stream.  If
00116   // successful, the entire input will be consumed.
00117   bool ParseFromZeroCopyStream(io::ZeroCopyInputStream* input);
00118   // Like ParseFromZeroCopyStream(), but accepts messages that are missing
00119   // required fields.
00120   bool ParsePartialFromZeroCopyStream(io::ZeroCopyInputStream* input);
00121   // Read a protocol buffer from the given zero-copy input stream, expecting
00122   // the message to be exactly "size" bytes long.  If successful, exactly
00123   // this many bytes will have been consumed from the input.
00124   bool ParseFromBoundedZeroCopyStream(io::ZeroCopyInputStream* input, int size);
00125   // Like ParseFromBoundedZeroCopyStream(), but accepts messages that are
00126   // missing required fields.
00127   bool ParsePartialFromBoundedZeroCopyStream(io::ZeroCopyInputStream* input,
00128                                              int size);
00129   // Parse a protocol buffer contained in a string.
00130   bool ParseFromString(const string& data);
00131   // Like ParseFromString(), but accepts messages that are missing
00132   // required fields.
00133   bool ParsePartialFromString(const string& data);
00134   // Parse a protocol buffer contained in an array of bytes.
00135   bool ParseFromArray(const void* data, int size);
00136   // Like ParseFromArray(), but accepts messages that are missing
00137   // required fields.
00138   bool ParsePartialFromArray(const void* data, int size);
00139 
00140 
00141   // Reads a protocol buffer from the stream and merges it into this
00142   // Message.  Singular fields read from the input overwrite what is
00143   // already in the Message and repeated fields are appended to those
00144   // already present.
00145   //
00146   // It is the responsibility of the caller to call input->LastTagWas()
00147   // (for groups) or input->ConsumedEntireMessage() (for non-groups) after
00148   // this returns to verify that the message's end was delimited correctly.
00149   //
00150   // ParsefromCodedStream() is implemented as Clear() followed by
00151   // MergeFromCodedStream().
00152   bool MergeFromCodedStream(io::CodedInputStream* input);
00153 
00154   // Like MergeFromCodedStream(), but succeeds even if required fields are
00155   // missing in the input.
00156   //
00157   // MergeFromCodedStream() is just implemented as MergePartialFromCodedStream()
00158   // followed by IsInitialized().
00159   virtual bool MergePartialFromCodedStream(io::CodedInputStream* input) = 0;
00160 
00161   // Serialization ---------------------------------------------------
00162   // Methods for serializing in protocol buffer format.  Most of these
00163   // are just simple wrappers around ByteSize() and SerializeWithCachedSizes().
00164 
00165   // Write a protocol buffer of this message to the given output.  Returns
00166   // false on a write error.  If the message is missing required fields,
00167   // this may GOOGLE_CHECK-fail.
00168   bool SerializeToCodedStream(io::CodedOutputStream* output) const;
00169   // Like SerializeToCodedStream(), but allows missing required fields.
00170   bool SerializePartialToCodedStream(io::CodedOutputStream* output) const;
00171   // Write the message to the given zero-copy output stream.  All required
00172   // fields must be set.
00173   bool SerializeToZeroCopyStream(io::ZeroCopyOutputStream* output) const;
00174   // Like SerializeToZeroCopyStream(), but allows missing required fields.
00175   bool SerializePartialToZeroCopyStream(io::ZeroCopyOutputStream* output) const;
00176   // Serialize the message and store it in the given string.  All required
00177   // fields must be set.
00178   bool SerializeToString(string* output) const;
00179   // Like SerializeToString(), but allows missing required fields.
00180   bool SerializePartialToString(string* output) const;
00181   // Serialize the message and store it in the given byte array.  All required
00182   // fields must be set.
00183   bool SerializeToArray(void* data, int size) const;
00184   // Like SerializeToArray(), but allows missing required fields.
00185   bool SerializePartialToArray(void* data, int size) const;
00186 
00187   // Make a string encoding the message. Is equivalent to calling
00188   // SerializeToString() on a string and using that.  Returns the empty
00189   // string if SerializeToString() would have returned an error.
00190   // Note: If you intend to generate many such strings, you may
00191   // reduce heap fragmentation by instead re-using the same string
00192   // object with calls to SerializeToString().
00193   string SerializeAsString() const;
00194   // Like SerializeAsString(), but allows missing required fields.
00195   string SerializePartialAsString() const;
00196 
00197   // Like SerializeToString(), but appends to the data to the string's existing
00198   // contents.  All required fields must be set.
00199   bool AppendToString(string* output) const;
00200   // Like AppendToString(), but allows missing required fields.
00201   bool AppendPartialToString(string* output) const;
00202 
00203   // Computes the serialized size of the message.  This recursively calls
00204   // ByteSize() on all embedded messages.  If a subclass does not override
00205   // this, it MUST override SetCachedSize().
00206   virtual int ByteSize() const = 0;
00207 
00208   // Serializes the message without recomputing the size.  The message must
00209   // not have changed since the last call to ByteSize(); if it has, the results
00210   // are undefined.
00211   virtual void SerializeWithCachedSizes(
00212       io::CodedOutputStream* output) const = 0;
00213 
00214   // Like SerializeWithCachedSizes, but writes directly to *target, returning
00215   // a pointer to the byte immediately after the last byte written.  "target"
00216   // must point at a byte array of at least ByteSize() bytes.
00217   virtual uint8* SerializeWithCachedSizesToArray(uint8* target) const;
00218 
00219   // Returns the result of the last call to ByteSize().  An embedded message's
00220   // size is needed both to serialize it (because embedded messages are
00221   // length-delimited) and to compute the outer message's size.  Caching
00222   // the size avoids computing it multiple times.
00223   //
00224   // ByteSize() does not automatically use the cached size when available
00225   // because this would require invalidating it every time the message was
00226   // modified, which would be too hard and expensive.  (E.g. if a deeply-nested
00227   // sub-message is changed, all of its parents' cached sizes would need to be
00228   // invalidated, which is too much work for an otherwise inlined setter
00229   // method.)
00230   virtual int GetCachedSize() const = 0;
00231 
00232  private:
00233   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageLite);
00234 };
00235 
00236 }  // namespace protobuf
00237 
00238 }  // namespace google
00239 #endif  // GOOGLE_PROTOBUF_MESSAGE_LITE_H__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines