BWAPI
Undermind/proxy/cpp/include/google/protobuf/stubs/common.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) and others
00032 //
00033 // Contains basic types and utilities used by the rest of the library.
00034 
00035 #ifndef GOOGLE_PROTOBUF_COMMON_H__
00036 #define GOOGLE_PROTOBUF_COMMON_H__
00037 
00038 #include <assert.h>
00039 #include <stdlib.h>
00040 #include <cstddef>
00041 #include <string>
00042 #include <string.h>
00043 #if defined(__osf__)
00044 // Tru64 lacks stdint.h, but has inttypes.h which defines a superset of
00045 // what stdint.h would define.
00046 #include <inttypes.h>
00047 #elif !defined(_MSC_VER)
00048 #include <stdint.h>
00049 #endif
00050 
00051 namespace std {}
00052 
00053 namespace google {
00054 namespace protobuf {
00055 
00056 using namespace std;  // Don't do this at home, kids.
00057 
00058 #undef GOOGLE_DISALLOW_EVIL_CONSTRUCTORS
00059 #define GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeName)    \
00060   TypeName(const TypeName&);                           \
00061   void operator=(const TypeName&)
00062 
00063 #if defined(_MSC_VER) && defined(PROTOBUF_USE_DLLS)
00064   #ifdef LIBPROTOBUF_EXPORTS
00065     #define LIBPROTOBUF_EXPORT __declspec(dllexport)
00066   #else
00067     #define LIBPROTOBUF_EXPORT __declspec(dllimport)
00068   #endif
00069   #ifdef LIBPROTOC_EXPORTS
00070     #define LIBPROTOC_EXPORT   __declspec(dllexport)
00071   #else
00072     #define LIBPROTOC_EXPORT   __declspec(dllimport)
00073   #endif
00074 #else
00075   #define LIBPROTOBUF_EXPORT
00076   #define LIBPROTOC_EXPORT
00077 #endif
00078 
00079 namespace internal {
00080 
00081 // Some of these constants are macros rather than const ints so that they can
00082 // be used in #if directives.
00083 
00084 // The current version, represented as a single integer to make comparison
00085 // easier:  major * 10^6 + minor * 10^3 + micro
00086 #define GOOGLE_PROTOBUF_VERSION 2003000
00087 
00088 // The minimum library version which works with the current version of the
00089 // headers.
00090 #define GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION 2003000
00091 
00092 // The minimum header version which works with the current version of
00093 // the library.  This constant should only be used by protoc's C++ code
00094 // generator.
00095 static const int kMinHeaderVersionForLibrary = 2003000;
00096 
00097 // The minimum protoc version which works with the current version of the
00098 // headers.
00099 #define GOOGLE_PROTOBUF_MIN_PROTOC_VERSION 2003000
00100 
00101 // The minimum header version which works with the current version of
00102 // protoc.  This constant should only be used in VerifyVersion().
00103 static const int kMinHeaderVersionForProtoc = 2003000;
00104 
00105 // Verifies that the headers and libraries are compatible.  Use the macro
00106 // below to call this.
00107 void LIBPROTOBUF_EXPORT VerifyVersion(int headerVersion, int minLibraryVersion,
00108                                       const char* filename);
00109 
00110 // Converts a numeric version number to a string.
00111 string LIBPROTOBUF_EXPORT VersionString(int version);
00112 
00113 }  // namespace internal
00114 
00115 // Place this macro in your main() function (or somewhere before you attempt
00116 // to use the protobuf library) to verify that the version you link against
00117 // matches the headers you compiled against.  If a version mismatch is
00118 // detected, the process will abort.
00119 #define GOOGLE_PROTOBUF_VERIFY_VERSION                                    \
00120   ::google::protobuf::internal::VerifyVersion(                            \
00121     GOOGLE_PROTOBUF_VERSION, GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION,         \
00122     __FILE__)
00123 
00124 // ===================================================================
00125 // from google3/base/port.h
00126 
00127 typedef unsigned int uint;
00128 
00129 #ifdef _MSC_VER
00130 typedef __int8  int8;
00131 typedef __int16 int16;
00132 typedef __int32 int32;
00133 typedef __int64 int64;
00134 
00135 typedef unsigned __int8  uint8;
00136 typedef unsigned __int16 uint16;
00137 typedef unsigned __int32 uint32;
00138 typedef unsigned __int64 uint64;
00139 #else
00140 typedef int8_t  int8;
00141 typedef int16_t int16;
00142 typedef int32_t int32;
00143 typedef int64_t int64;
00144 
00145 typedef uint8_t  uint8;
00146 typedef uint16_t uint16;
00147 typedef uint32_t uint32;
00148 typedef uint64_t uint64;
00149 #endif
00150 
00151 // long long macros to be used because gcc and vc++ use different suffixes,
00152 // and different size specifiers in format strings
00153 #undef GOOGLE_LONGLONG
00154 #undef GOOGLE_ULONGLONG
00155 #undef GOOGLE_LL_FORMAT
00156 
00157 #ifdef _MSC_VER
00158 #define GOOGLE_LONGLONG(x) x##I64
00159 #define GOOGLE_ULONGLONG(x) x##UI64
00160 #define GOOGLE_LL_FORMAT "I64"  // As in printf("%I64d", ...)
00161 #else
00162 #define GOOGLE_LONGLONG(x) x##LL
00163 #define GOOGLE_ULONGLONG(x) x##ULL
00164 #define GOOGLE_LL_FORMAT "ll"  // As in "%lld". Note that "q" is poor form also.
00165 #endif
00166 
00167 static const int32 kint32max = 0x7FFFFFFF;
00168 static const int32 kint32min = -kint32max - 1;
00169 static const int64 kint64max = GOOGLE_LONGLONG(0x7FFFFFFFFFFFFFFF);
00170 static const int64 kint64min = -kint64max - 1;
00171 static const uint32 kuint32max = 0xFFFFFFFFu;
00172 static const uint64 kuint64max = GOOGLE_ULONGLONG(0xFFFFFFFFFFFFFFFF);
00173 
00174 // -------------------------------------------------------------------
00175 // Annotations:  Some parts of the code have been annotated in ways that might
00176 //   be useful to some compilers or tools, but are not supported universally.
00177 //   You can #define these annotations yourself if the default implementation
00178 //   is not right for you.
00179 
00180 #ifndef GOOGLE_ATTRIBUTE_ALWAYS_INLINE
00181 #if defined(__GNUC__) && (__GNUC__ > 3 ||(__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
00182 // For functions we want to force inline.
00183 // Introduced in gcc 3.1.
00184 #define GOOGLE_ATTRIBUTE_ALWAYS_INLINE __attribute__ ((always_inline))
00185 #else
00186 // Other compilers will have to figure it out for themselves.
00187 #define GOOGLE_ATTRIBUTE_ALWAYS_INLINE
00188 #endif
00189 #endif
00190 
00191 #ifndef GOOGLE_ATTRIBUTE_DEPRECATED
00192 #ifdef __GNUC__
00193 // If the method/variable/type is used anywhere, produce a warning.
00194 #define GOOGLE_ATTRIBUTE_DEPRECATED __attribute__((deprecated))
00195 #else
00196 #define GOOGLE_ATTRIBUTE_DEPRECATED
00197 #endif
00198 #endif
00199 
00200 #ifndef GOOGLE_PREDICT_TRUE
00201 #ifdef __GNUC__
00202 // Provided at least since GCC 3.0.
00203 #define GOOGLE_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
00204 #else
00205 #define GOOGLE_PREDICT_TRUE
00206 #endif
00207 #endif
00208 
00209 // Delimits a block of code which may write to memory which is simultaneously
00210 // written by other threads, but which has been determined to be thread-safe
00211 // (e.g. because it is an idempotent write).
00212 #ifndef GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN
00213 #define GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN()
00214 #endif
00215 #ifndef GOOGLE_SAFE_CONCURRENT_WRITES_END
00216 #define GOOGLE_SAFE_CONCURRENT_WRITES_END()
00217 #endif
00218 
00219 // ===================================================================
00220 // from google3/base/basictypes.h
00221 
00222 // The GOOGLE_ARRAYSIZE(arr) macro returns the # of elements in an array arr.
00223 // The expression is a compile-time constant, and therefore can be
00224 // used in defining new arrays, for example.
00225 //
00226 // GOOGLE_ARRAYSIZE catches a few type errors.  If you see a compiler error
00227 //
00228 //   "warning: division by zero in ..."
00229 //
00230 // when using GOOGLE_ARRAYSIZE, you are (wrongfully) giving it a pointer.
00231 // You should only use GOOGLE_ARRAYSIZE on statically allocated arrays.
00232 //
00233 // The following comments are on the implementation details, and can
00234 // be ignored by the users.
00235 //
00236 // ARRAYSIZE(arr) works by inspecting sizeof(arr) (the # of bytes in
00237 // the array) and sizeof(*(arr)) (the # of bytes in one array
00238 // element).  If the former is divisible by the latter, perhaps arr is
00239 // indeed an array, in which case the division result is the # of
00240 // elements in the array.  Otherwise, arr cannot possibly be an array,
00241 // and we generate a compiler error to prevent the code from
00242 // compiling.
00243 //
00244 // Since the size of bool is implementation-defined, we need to cast
00245 // !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final
00246 // result has type size_t.
00247 //
00248 // This macro is not perfect as it wrongfully accepts certain
00249 // pointers, namely where the pointer size is divisible by the pointee
00250 // size.  Since all our code has to go through a 32-bit compiler,
00251 // where a pointer is 4 bytes, this means all pointers to a type whose
00252 // size is 3 or greater than 4 will be (righteously) rejected.
00253 //
00254 // Kudos to Jorg Brown for this simple and elegant implementation.
00255 
00256 #undef GOOGLE_ARRAYSIZE
00257 #define GOOGLE_ARRAYSIZE(a) \
00258   ((sizeof(a) / sizeof(*(a))) / \
00259    static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
00260 
00261 namespace internal {
00262 
00263 // Use implicit_cast as a safe version of static_cast or const_cast
00264 // for upcasting in the type hierarchy (i.e. casting a pointer to Foo
00265 // to a pointer to SuperclassOfFoo or casting a pointer to Foo to
00266 // a const pointer to Foo).
00267 // When you use implicit_cast, the compiler checks that the cast is safe.
00268 // Such explicit implicit_casts are necessary in surprisingly many
00269 // situations where C++ demands an exact type match instead of an
00270 // argument type convertable to a target type.
00271 //
00272 // The From type can be inferred, so the preferred syntax for using
00273 // implicit_cast is the same as for static_cast etc.:
00274 //
00275 //   implicit_cast<ToType>(expr)
00276 //
00277 // implicit_cast would have been part of the C++ standard library,
00278 // but the proposal was submitted too late.  It will probably make
00279 // its way into the language in the future.
00280 template<typename To, typename From>
00281 inline To implicit_cast(From const &f) {
00282   return f;
00283 }
00284 
00285 // When you upcast (that is, cast a pointer from type Foo to type
00286 // SuperclassOfFoo), it's fine to use implicit_cast<>, since upcasts
00287 // always succeed.  When you downcast (that is, cast a pointer from
00288 // type Foo to type SubclassOfFoo), static_cast<> isn't safe, because
00289 // how do you know the pointer is really of type SubclassOfFoo?  It
00290 // could be a bare Foo, or of type DifferentSubclassOfFoo.  Thus,
00291 // when you downcast, you should use this macro.  In debug mode, we
00292 // use dynamic_cast<> to double-check the downcast is legal (we die
00293 // if it's not).  In normal mode, we do the efficient static_cast<>
00294 // instead.  Thus, it's important to test in debug mode to make sure
00295 // the cast is legal!
00296 //    This is the only place in the code we should use dynamic_cast<>.
00297 // In particular, you SHOULDN'T be using dynamic_cast<> in order to
00298 // do RTTI (eg code like this:
00299 //    if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);
00300 //    if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);
00301 // You should design the code some other way not to need this.
00302 
00303 template<typename To, typename From>     // use like this: down_cast<T*>(foo);
00304 inline To down_cast(From* f) {                   // so we only accept pointers
00305   // Ensures that To is a sub-type of From *.  This test is here only
00306   // for compile-time type checking, and has no overhead in an
00307   // optimized build at run-time, as it will be optimized away
00308   // completely.
00309   if (false) {
00310     implicit_cast<From*, To>(0);
00311   }
00312 
00313 #if !defined(NDEBUG) && !defined(GOOGLE_PROTOBUF_NO_RTTI)
00314   assert(f == NULL || dynamic_cast<To>(f) != NULL);  // RTTI: debug mode only!
00315 #endif
00316   return static_cast<To>(f);
00317 }
00318 
00319 }  // namespace internal
00320 
00321 // We made these internal so that they would show up as such in the docs,
00322 // but we don't want to stick "internal::" in front of them everywhere.
00323 using internal::implicit_cast;
00324 using internal::down_cast;
00325 
00326 // The COMPILE_ASSERT macro can be used to verify that a compile time
00327 // expression is true. For example, you could use it to verify the
00328 // size of a static array:
00329 //
00330 //   COMPILE_ASSERT(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES,
00331 //                  content_type_names_incorrect_size);
00332 //
00333 // or to make sure a struct is smaller than a certain size:
00334 //
00335 //   COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
00336 //
00337 // The second argument to the macro is the name of the variable. If
00338 // the expression is false, most compilers will issue a warning/error
00339 // containing the name of the variable.
00340 
00341 namespace internal {
00342 
00343 template <bool>
00344 struct CompileAssert {
00345 };
00346 
00347 }  // namespace internal
00348 
00349 #undef GOOGLE_COMPILE_ASSERT
00350 #define GOOGLE_COMPILE_ASSERT(expr, msg) \
00351   typedef ::google::protobuf::internal::CompileAssert<(bool(expr))> \
00352           msg[bool(expr) ? 1 : -1]
00353 
00354 // Implementation details of COMPILE_ASSERT:
00355 //
00356 // - COMPILE_ASSERT works by defining an array type that has -1
00357 //   elements (and thus is invalid) when the expression is false.
00358 //
00359 // - The simpler definition
00360 //
00361 //     #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1]
00362 //
00363 //   does not work, as gcc supports variable-length arrays whose sizes
00364 //   are determined at run-time (this is gcc's extension and not part
00365 //   of the C++ standard).  As a result, gcc fails to reject the
00366 //   following code with the simple definition:
00367 //
00368 //     int foo;
00369 //     COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is
00370 //                               // not a compile-time constant.
00371 //
00372 // - By using the type CompileAssert<(bool(expr))>, we ensures that
00373 //   expr is a compile-time constant.  (Template arguments must be
00374 //   determined at compile-time.)
00375 //
00376 // - The outter parentheses in CompileAssert<(bool(expr))> are necessary
00377 //   to work around a bug in gcc 3.4.4 and 4.0.1.  If we had written
00378 //
00379 //     CompileAssert<bool(expr)>
00380 //
00381 //   instead, these compilers will refuse to compile
00382 //
00383 //     COMPILE_ASSERT(5 > 0, some_message);
00384 //
00385 //   (They seem to think the ">" in "5 > 0" marks the end of the
00386 //   template argument list.)
00387 //
00388 // - The array size is (bool(expr) ? 1 : -1), instead of simply
00389 //
00390 //     ((expr) ? 1 : -1).
00391 //
00392 //   This is to avoid running into a bug in MS VC 7.1, which
00393 //   causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
00394 
00395 // ===================================================================
00396 // from google3/base/scoped_ptr.h
00397 
00398 namespace internal {
00399 
00400 //  This is an implementation designed to match the anticipated future TR2
00401 //  implementation of the scoped_ptr class, and its closely-related brethren,
00402 //  scoped_array, scoped_ptr_malloc, and make_scoped_ptr.
00403 
00404 template <class C> class scoped_ptr;
00405 template <class C> class scoped_array;
00406 
00407 // A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T>
00408 // automatically deletes the pointer it holds (if any).
00409 // That is, scoped_ptr<T> owns the T object that it points to.
00410 // Like a T*, a scoped_ptr<T> may hold either NULL or a pointer to a T object.
00411 //
00412 // The size of a scoped_ptr is small:
00413 // sizeof(scoped_ptr<C>) == sizeof(C*)
00414 template <class C>
00415 class scoped_ptr {
00416  public:
00417 
00418   // The element type
00419   typedef C element_type;
00420 
00421   // Constructor.  Defaults to intializing with NULL.
00422   // There is no way to create an uninitialized scoped_ptr.
00423   // The input parameter must be allocated with new.
00424   explicit scoped_ptr(C* p = NULL) : ptr_(p) { }
00425 
00426   // Destructor.  If there is a C object, delete it.
00427   // We don't need to test ptr_ == NULL because C++ does that for us.
00428   ~scoped_ptr() {
00429     enum { type_must_be_complete = sizeof(C) };
00430     delete ptr_;
00431   }
00432 
00433   // Reset.  Deletes the current owned object, if any.
00434   // Then takes ownership of a new object, if given.
00435   // this->reset(this->get()) works.
00436   void reset(C* p = NULL) {
00437     if (p != ptr_) {
00438       enum { type_must_be_complete = sizeof(C) };
00439       delete ptr_;
00440       ptr_ = p;
00441     }
00442   }
00443 
00444   // Accessors to get the owned object.
00445   // operator* and operator-> will assert() if there is no current object.
00446   C& operator*() const {
00447     assert(ptr_ != NULL);
00448     return *ptr_;
00449   }
00450   C* operator->() const  {
00451     assert(ptr_ != NULL);
00452     return ptr_;
00453   }
00454   C* get() const { return ptr_; }
00455 
00456   // Comparison operators.
00457   // These return whether two scoped_ptr refer to the same object, not just to
00458   // two different but equal objects.
00459   bool operator==(C* p) const { return ptr_ == p; }
00460   bool operator!=(C* p) const { return ptr_ != p; }
00461 
00462   // Swap two scoped pointers.
00463   void swap(scoped_ptr& p2) {
00464     C* tmp = ptr_;
00465     ptr_ = p2.ptr_;
00466     p2.ptr_ = tmp;
00467   }
00468 
00469   // Release a pointer.
00470   // The return value is the current pointer held by this object.
00471   // If this object holds a NULL pointer, the return value is NULL.
00472   // After this operation, this object will hold a NULL pointer,
00473   // and will not own the object any more.
00474   C* release() {
00475     C* retVal = ptr_;
00476     ptr_ = NULL;
00477     return retVal;
00478   }
00479 
00480  private:
00481   C* ptr_;
00482 
00483   // Forbid comparison of scoped_ptr types.  If C2 != C, it totally doesn't
00484   // make sense, and if C2 == C, it still doesn't make sense because you should
00485   // never have the same object owned by two different scoped_ptrs.
00486   template <class C2> bool operator==(scoped_ptr<C2> const& p2) const;
00487   template <class C2> bool operator!=(scoped_ptr<C2> const& p2) const;
00488 
00489   // Disallow evil constructors
00490   scoped_ptr(const scoped_ptr&);
00491   void operator=(const scoped_ptr&);
00492 };
00493 
00494 // scoped_array<C> is like scoped_ptr<C>, except that the caller must allocate
00495 // with new [] and the destructor deletes objects with delete [].
00496 //
00497 // As with scoped_ptr<C>, a scoped_array<C> either points to an object
00498 // or is NULL.  A scoped_array<C> owns the object that it points to.
00499 //
00500 // Size: sizeof(scoped_array<C>) == sizeof(C*)
00501 template <class C>
00502 class scoped_array {
00503  public:
00504 
00505   // The element type
00506   typedef C element_type;
00507 
00508   // Constructor.  Defaults to intializing with NULL.
00509   // There is no way to create an uninitialized scoped_array.
00510   // The input parameter must be allocated with new [].
00511   explicit scoped_array(C* p = NULL) : array_(p) { }
00512 
00513   // Destructor.  If there is a C object, delete it.
00514   // We don't need to test ptr_ == NULL because C++ does that for us.
00515   ~scoped_array() {
00516     enum { type_must_be_complete = sizeof(C) };
00517     delete[] array_;
00518   }
00519 
00520   // Reset.  Deletes the current owned object, if any.
00521   // Then takes ownership of a new object, if given.
00522   // this->reset(this->get()) works.
00523   void reset(C* p = NULL) {
00524     if (p != array_) {
00525       enum { type_must_be_complete = sizeof(C) };
00526       delete[] array_;
00527       array_ = p;
00528     }
00529   }
00530 
00531   // Get one element of the current object.
00532   // Will assert() if there is no current object, or index i is negative.
00533   C& operator[](std::ptrdiff_t i) const {
00534     assert(i >= 0);
00535     assert(array_ != NULL);
00536     return array_[i];
00537   }
00538 
00539   // Get a pointer to the zeroth element of the current object.
00540   // If there is no current object, return NULL.
00541   C* get() const {
00542     return array_;
00543   }
00544 
00545   // Comparison operators.
00546   // These return whether two scoped_array refer to the same object, not just to
00547   // two different but equal objects.
00548   bool operator==(C* p) const { return array_ == p; }
00549   bool operator!=(C* p) const { return array_ != p; }
00550 
00551   // Swap two scoped arrays.
00552   void swap(scoped_array& p2) {
00553     C* tmp = array_;
00554     array_ = p2.array_;
00555     p2.array_ = tmp;
00556   }
00557 
00558   // Release an array.
00559   // The return value is the current pointer held by this object.
00560   // If this object holds a NULL pointer, the return value is NULL.
00561   // After this operation, this object will hold a NULL pointer,
00562   // and will not own the object any more.
00563   C* release() {
00564     C* retVal = array_;
00565     array_ = NULL;
00566     return retVal;
00567   }
00568 
00569  private:
00570   C* array_;
00571 
00572   // Forbid comparison of different scoped_array types.
00573   template <class C2> bool operator==(scoped_array<C2> const& p2) const;
00574   template <class C2> bool operator!=(scoped_array<C2> const& p2) const;
00575 
00576   // Disallow evil constructors
00577   scoped_array(const scoped_array&);
00578   void operator=(const scoped_array&);
00579 };
00580 
00581 }  // namespace internal
00582 
00583 // We made these internal so that they would show up as such in the docs,
00584 // but we don't want to stick "internal::" in front of them everywhere.
00585 using internal::scoped_ptr;
00586 using internal::scoped_array;
00587 
00588 // ===================================================================
00589 // emulates google3/base/logging.h
00590 
00591 enum LogLevel {
00592   LOGLEVEL_INFO,     // Informational.  This is never actually used by
00593                      // libprotobuf.
00594   LOGLEVEL_WARNING,  // Warns about issues that, although not technically a
00595                      // problem now, could cause problems in the future.  For
00596                      // example, a // warning will be printed when parsing a
00597                      // message that is near the message size limit.
00598   LOGLEVEL_ERROR,    // An error occurred which should never happen during
00599                      // normal use.
00600   LOGLEVEL_FATAL,    // An error occurred from which the library cannot
00601                      // recover.  This usually indicates a programming error
00602                      // in the code which calls the library, especially when
00603                      // compiled in debug mode.
00604 
00605 #ifdef NDEBUG
00606   LOGLEVEL_DFATAL = LOGLEVEL_ERROR
00607 #else
00608   LOGLEVEL_DFATAL = LOGLEVEL_FATAL
00609 #endif
00610 };
00611 
00612 namespace internal {
00613 
00614 class LogFinisher;
00615 
00616 class LIBPROTOBUF_EXPORT LogMessage {
00617  public:
00618   LogMessage(LogLevel level, const char* filename, int line);
00619   ~LogMessage();
00620 
00621   LogMessage& operator<<(const string& value);
00622   LogMessage& operator<<(const char* value);
00623   LogMessage& operator<<(char value);
00624   LogMessage& operator<<(int value);
00625   LogMessage& operator<<(uint value);
00626   LogMessage& operator<<(long value);
00627   LogMessage& operator<<(unsigned long value);
00628   LogMessage& operator<<(double value);
00629 
00630  private:
00631   friend class LogFinisher;
00632   void Finish();
00633 
00634   LogLevel level_;
00635   const char* filename_;
00636   int line_;
00637   string message_;
00638 };
00639 
00640 // Used to make the entire "LOG(BLAH) << etc." expression have a void return
00641 // type and print a newline after each message.
00642 class LIBPROTOBUF_EXPORT LogFinisher {
00643  public:
00644   void operator=(LogMessage& other);
00645 };
00646 
00647 }  // namespace internal
00648 
00649 // Undef everything in case we're being mixed with some other Google library
00650 // which already defined them itself.  Presumably all Google libraries will
00651 // support the same syntax for these so it should not be a big deal if they
00652 // end up using our definitions instead.
00653 #undef GOOGLE_LOG
00654 #undef GOOGLE_LOG_IF
00655 
00656 #undef GOOGLE_CHECK
00657 #undef GOOGLE_CHECK_EQ
00658 #undef GOOGLE_CHECK_NE
00659 #undef GOOGLE_CHECK_LT
00660 #undef GOOGLE_CHECK_LE
00661 #undef GOOGLE_CHECK_GT
00662 #undef GOOGLE_CHECK_GE
00663 
00664 #undef GOOGLE_DLOG
00665 #undef GOOGLE_DCHECK
00666 #undef GOOGLE_DCHECK_EQ
00667 #undef GOOGLE_DCHECK_NE
00668 #undef GOOGLE_DCHECK_LT
00669 #undef GOOGLE_DCHECK_LE
00670 #undef GOOGLE_DCHECK_GT
00671 #undef GOOGLE_DCHECK_GE
00672 
00673 #define GOOGLE_LOG(LEVEL)                                                 \
00674   ::google::protobuf::internal::LogFinisher() =                           \
00675     ::google::protobuf::internal::LogMessage(                             \
00676       ::google::protobuf::LOGLEVEL_##LEVEL, __FILE__, __LINE__)
00677 #define GOOGLE_LOG_IF(LEVEL, CONDITION) \
00678   !(CONDITION) ? (void)0 : GOOGLE_LOG(LEVEL)
00679 
00680 #define GOOGLE_CHECK(EXPRESSION) \
00681   GOOGLE_LOG_IF(FATAL, !(EXPRESSION)) << "CHECK failed: " #EXPRESSION ": "
00682 #define GOOGLE_CHECK_EQ(A, B) GOOGLE_CHECK((A) == (B))
00683 #define GOOGLE_CHECK_NE(A, B) GOOGLE_CHECK((A) != (B))
00684 #define GOOGLE_CHECK_LT(A, B) GOOGLE_CHECK((A) <  (B))
00685 #define GOOGLE_CHECK_LE(A, B) GOOGLE_CHECK((A) <= (B))
00686 #define GOOGLE_CHECK_GT(A, B) GOOGLE_CHECK((A) >  (B))
00687 #define GOOGLE_CHECK_GE(A, B) GOOGLE_CHECK((A) >= (B))
00688 
00689 #ifdef NDEBUG
00690 
00691 #define GOOGLE_DLOG GOOGLE_LOG_IF(INFO, false)
00692 
00693 #define GOOGLE_DCHECK(EXPRESSION) while(false) GOOGLE_CHECK(EXPRESSION)
00694 #define GOOGLE_DCHECK_EQ(A, B) GOOGLE_DCHECK((A) == (B))
00695 #define GOOGLE_DCHECK_NE(A, B) GOOGLE_DCHECK((A) != (B))
00696 #define GOOGLE_DCHECK_LT(A, B) GOOGLE_DCHECK((A) <  (B))
00697 #define GOOGLE_DCHECK_LE(A, B) GOOGLE_DCHECK((A) <= (B))
00698 #define GOOGLE_DCHECK_GT(A, B) GOOGLE_DCHECK((A) >  (B))
00699 #define GOOGLE_DCHECK_GE(A, B) GOOGLE_DCHECK((A) >= (B))
00700 
00701 #else  // NDEBUG
00702 
00703 #define GOOGLE_DLOG GOOGLE_LOG
00704 
00705 #define GOOGLE_DCHECK    GOOGLE_CHECK
00706 #define GOOGLE_DCHECK_EQ GOOGLE_CHECK_EQ
00707 #define GOOGLE_DCHECK_NE GOOGLE_CHECK_NE
00708 #define GOOGLE_DCHECK_LT GOOGLE_CHECK_LT
00709 #define GOOGLE_DCHECK_LE GOOGLE_CHECK_LE
00710 #define GOOGLE_DCHECK_GT GOOGLE_CHECK_GT
00711 #define GOOGLE_DCHECK_GE GOOGLE_CHECK_GE
00712 
00713 #endif  // !NDEBUG
00714 
00715 typedef void LogHandler(LogLevel level, const char* filename, int line,
00716                         const string& message);
00717 
00718 // The protobuf library sometimes writes warning and error messages to
00719 // stderr.  These messages are primarily useful for developers, but may
00720 // also help end users figure out a problem.  If you would prefer that
00721 // these messages be sent somewhere other than stderr, call SetLogHandler()
00722 // to set your own handler.  This returns the old handler.  Set the handler
00723 // to NULL to ignore log messages (but see also LogSilencer, below).
00724 //
00725 // Obviously, SetLogHandler is not thread-safe.  You should only call it
00726 // at initialization time, and probably not from library code.  If you
00727 // simply want to suppress log messages temporarily (e.g. because you
00728 // have some code that tends to trigger them frequently and you know
00729 // the warnings are not important to you), use the LogSilencer class
00730 // below.
00731 LIBPROTOBUF_EXPORT LogHandler* SetLogHandler(LogHandler* new_func);
00732 
00733 // Create a LogSilencer if you want to temporarily suppress all log
00734 // messages.  As long as any LogSilencer objects exist, non-fatal
00735 // log messages will be discarded (the current LogHandler will *not*
00736 // be called).  Constructing a LogSilencer is thread-safe.  You may
00737 // accidentally suppress log messages occurring in another thread, but
00738 // since messages are generally for debugging purposes only, this isn't
00739 // a big deal.  If you want to intercept log messages, use SetLogHandler().
00740 class LIBPROTOBUF_EXPORT LogSilencer {
00741  public:
00742   LogSilencer();
00743   ~LogSilencer();
00744 };
00745 
00746 // ===================================================================
00747 // emulates google3/base/callback.h
00748 
00749 // Abstract interface for a callback.  When calling an RPC, you must provide
00750 // a Closure to call when the procedure completes.  See the Service interface
00751 // in service.h.
00752 //
00753 // To automatically construct a Closure which calls a particular function or
00754 // method with a particular set of parameters, use the NewCallback() function.
00755 // Example:
00756 //   void FooDone(const FooResponse* response) {
00757 //     ...
00758 //   }
00759 //
00760 //   void CallFoo() {
00761 //     ...
00762 //     // When done, call FooDone() and pass it a pointer to the response.
00763 //     Closure* callback = NewCallback(&FooDone, response);
00764 //     // Make the call.
00765 //     service->Foo(controller, request, response, callback);
00766 //   }
00767 //
00768 // Example that calls a method:
00769 //   class Handler {
00770 //    public:
00771 //     ...
00772 //
00773 //     void FooDone(const FooResponse* response) {
00774 //       ...
00775 //     }
00776 //
00777 //     void CallFoo() {
00778 //       ...
00779 //       // When done, call FooDone() and pass it a pointer to the response.
00780 //       Closure* callback = NewCallback(this, &Handler::FooDone, response);
00781 //       // Make the call.
00782 //       service->Foo(controller, request, response, callback);
00783 //     }
00784 //   };
00785 //
00786 // Currently NewCallback() supports binding zero, one, or two arguments.
00787 //
00788 // Callbacks created with NewCallback() automatically delete themselves when
00789 // executed.  They should be used when a callback is to be called exactly
00790 // once (usually the case with RPC callbacks).  If a callback may be called
00791 // a different number of times (including zero), create it with
00792 // NewPermanentCallback() instead.  You are then responsible for deleting the
00793 // callback (using the "delete" keyword as normal).
00794 //
00795 // Note that NewCallback() is a bit touchy regarding argument types.  Generally,
00796 // the values you provide for the parameter bindings must exactly match the
00797 // types accepted by the callback function.  For example:
00798 //   void Foo(string s);
00799 //   NewCallback(&Foo, "foo");          // WON'T WORK:  const char* != string
00800 //   NewCallback(&Foo, string("foo"));  // WORKS
00801 // Also note that the arguments cannot be references:
00802 //   void Foo(const string& s);
00803 //   string my_str;
00804 //   NewCallback(&Foo, my_str);  // WON'T WORK:  Can't use referecnes.
00805 // However, correctly-typed pointers will work just fine.
00806 class LIBPROTOBUF_EXPORT Closure {
00807  public:
00808   Closure() {}
00809   virtual ~Closure();
00810 
00811   virtual void Run() = 0;
00812 
00813  private:
00814   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Closure);
00815 };
00816 
00817 namespace internal {
00818 
00819 class LIBPROTOBUF_EXPORT FunctionClosure0 : public Closure {
00820  public:
00821   typedef void (*FunctionType)();
00822 
00823   FunctionClosure0(FunctionType function, bool self_deleting)
00824     : function_(function), self_deleting_(self_deleting) {}
00825   ~FunctionClosure0();
00826 
00827   void Run() {
00828     function_();
00829     if (self_deleting_) delete this;
00830   }
00831 
00832  private:
00833   FunctionType function_;
00834   bool self_deleting_;
00835 };
00836 
00837 template <typename Class>
00838 class MethodClosure0 : public Closure {
00839  public:
00840   typedef void (Class::*MethodType)();
00841 
00842   MethodClosure0(Class* object, MethodType method, bool self_deleting)
00843     : object_(object), method_(method), self_deleting_(self_deleting) {}
00844   ~MethodClosure0() {}
00845 
00846   void Run() {
00847     (object_->*method_)();
00848     if (self_deleting_) delete this;
00849   }
00850 
00851  private:
00852   Class* object_;
00853   MethodType method_;
00854   bool self_deleting_;
00855 };
00856 
00857 template <typename Arg1>
00858 class FunctionClosure1 : public Closure {
00859  public:
00860   typedef void (*FunctionType)(Arg1 arg1);
00861 
00862   FunctionClosure1(FunctionType function, bool self_deleting,
00863                    Arg1 arg1)
00864     : function_(function), self_deleting_(self_deleting),
00865       arg1_(arg1) {}
00866   ~FunctionClosure1() {}
00867 
00868   void Run() {
00869     function_(arg1_);
00870     if (self_deleting_) delete this;
00871   }
00872 
00873  private:
00874   FunctionType function_;
00875   bool self_deleting_;
00876   Arg1 arg1_;
00877 };
00878 
00879 template <typename Class, typename Arg1>
00880 class MethodClosure1 : public Closure {
00881  public:
00882   typedef void (Class::*MethodType)(Arg1 arg1);
00883 
00884   MethodClosure1(Class* object, MethodType method, bool self_deleting,
00885                  Arg1 arg1)
00886     : object_(object), method_(method), self_deleting_(self_deleting),
00887       arg1_(arg1) {}
00888   ~MethodClosure1() {}
00889 
00890   void Run() {
00891     (object_->*method_)(arg1_);
00892     if (self_deleting_) delete this;
00893   }
00894 
00895  private:
00896   Class* object_;
00897   MethodType method_;
00898   bool self_deleting_;
00899   Arg1 arg1_;
00900 };
00901 
00902 template <typename Arg1, typename Arg2>
00903 class FunctionClosure2 : public Closure {
00904  public:
00905   typedef void (*FunctionType)(Arg1 arg1, Arg2 arg2);
00906 
00907   FunctionClosure2(FunctionType function, bool self_deleting,
00908                    Arg1 arg1, Arg2 arg2)
00909     : function_(function), self_deleting_(self_deleting),
00910       arg1_(arg1), arg2_(arg2) {}
00911   ~FunctionClosure2() {}
00912 
00913   void Run() {
00914     function_(arg1_, arg2_);
00915     if (self_deleting_) delete this;
00916   }
00917 
00918  private:
00919   FunctionType function_;
00920   bool self_deleting_;
00921   Arg1 arg1_;
00922   Arg2 arg2_;
00923 };
00924 
00925 template <typename Class, typename Arg1, typename Arg2>
00926 class MethodClosure2 : public Closure {
00927  public:
00928   typedef void (Class::*MethodType)(Arg1 arg1, Arg2 arg2);
00929 
00930   MethodClosure2(Class* object, MethodType method, bool self_deleting,
00931                  Arg1 arg1, Arg2 arg2)
00932     : object_(object), method_(method), self_deleting_(self_deleting),
00933       arg1_(arg1), arg2_(arg2) {}
00934   ~MethodClosure2() {}
00935 
00936   void Run() {
00937     (object_->*method_)(arg1_, arg2_);
00938     if (self_deleting_) delete this;
00939   }
00940 
00941  private:
00942   Class* object_;
00943   MethodType method_;
00944   bool self_deleting_;
00945   Arg1 arg1_;
00946   Arg2 arg2_;
00947 };
00948 
00949 }  // namespace internal
00950 
00951 // See Closure.
00952 inline Closure* NewCallback(void (*function)()) {
00953   return new internal::FunctionClosure0(function, true);
00954 }
00955 
00956 // See Closure.
00957 inline Closure* NewPermanentCallback(void (*function)()) {
00958   return new internal::FunctionClosure0(function, false);
00959 }
00960 
00961 // See Closure.
00962 template <typename Class>
00963 inline Closure* NewCallback(Class* object, void (Class::*method)()) {
00964   return new internal::MethodClosure0<Class>(object, method, true);
00965 }
00966 
00967 // See Closure.
00968 template <typename Class>
00969 inline Closure* NewPermanentCallback(Class* object, void (Class::*method)()) {
00970   return new internal::MethodClosure0<Class>(object, method, false);
00971 }
00972 
00973 // See Closure.
00974 template <typename Arg1>
00975 inline Closure* NewCallback(void (*function)(Arg1),
00976                             Arg1 arg1) {
00977   return new internal::FunctionClosure1<Arg1>(function, true, arg1);
00978 }
00979 
00980 // See Closure.
00981 template <typename Arg1>
00982 inline Closure* NewPermanentCallback(void (*function)(Arg1),
00983                                      Arg1 arg1) {
00984   return new internal::FunctionClosure1<Arg1>(function, false, arg1);
00985 }
00986 
00987 // See Closure.
00988 template <typename Class, typename Arg1>
00989 inline Closure* NewCallback(Class* object, void (Class::*method)(Arg1),
00990                             Arg1 arg1) {
00991   return new internal::MethodClosure1<Class, Arg1>(object, method, true, arg1);
00992 }
00993 
00994 // See Closure.
00995 template <typename Class, typename Arg1>
00996 inline Closure* NewPermanentCallback(Class* object, void (Class::*method)(Arg1),
00997                                      Arg1 arg1) {
00998   return new internal::MethodClosure1<Class, Arg1>(object, method, false, arg1);
00999 }
01000 
01001 // See Closure.
01002 template <typename Arg1, typename Arg2>
01003 inline Closure* NewCallback(void (*function)(Arg1, Arg2),
01004                             Arg1 arg1, Arg2 arg2) {
01005   return new internal::FunctionClosure2<Arg1, Arg2>(
01006     function, true, arg1, arg2);
01007 }
01008 
01009 // See Closure.
01010 template <typename Arg1, typename Arg2>
01011 inline Closure* NewPermanentCallback(void (*function)(Arg1, Arg2),
01012                                      Arg1 arg1, Arg2 arg2) {
01013   return new internal::FunctionClosure2<Arg1, Arg2>(
01014     function, false, arg1, arg2);
01015 }
01016 
01017 // See Closure.
01018 template <typename Class, typename Arg1, typename Arg2>
01019 inline Closure* NewCallback(Class* object, void (Class::*method)(Arg1, Arg2),
01020                             Arg1 arg1, Arg2 arg2) {
01021   return new internal::MethodClosure2<Class, Arg1, Arg2>(
01022     object, method, true, arg1, arg2);
01023 }
01024 
01025 // See Closure.
01026 template <typename Class, typename Arg1, typename Arg2>
01027 inline Closure* NewPermanentCallback(
01028     Class* object, void (Class::*method)(Arg1, Arg2),
01029     Arg1 arg1, Arg2 arg2) {
01030   return new internal::MethodClosure2<Class, Arg1, Arg2>(
01031     object, method, false, arg1, arg2);
01032 }
01033 
01034 // A function which does nothing.  Useful for creating no-op callbacks, e.g.:
01035 //   Closure* nothing = NewCallback(&DoNothing);
01036 void LIBPROTOBUF_EXPORT DoNothing();
01037 
01038 // ===================================================================
01039 // emulates google3/base/mutex.h
01040 
01041 namespace internal {
01042 
01043 // A Mutex is a non-reentrant (aka non-recursive) mutex.  At most one thread T
01044 // may hold a mutex at a given time.  If T attempts to Lock() the same Mutex
01045 // while holding it, T will deadlock.
01046 class LIBPROTOBUF_EXPORT Mutex {
01047  public:
01048   // Create a Mutex that is not held by anybody.
01049   Mutex();
01050 
01051   // Destructor
01052   ~Mutex();
01053 
01054   // Block if necessary until this Mutex is free, then acquire it exclusively.
01055   void Lock();
01056 
01057   // Release this Mutex.  Caller must hold it exclusively.
01058   void Unlock();
01059 
01060   // Crash if this Mutex is not held exclusively by this thread.
01061   // May fail to crash when it should; will never crash when it should not.
01062   void AssertHeld();
01063 
01064  private:
01065   struct Internal;
01066   Internal* mInternal;
01067 
01068   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Mutex);
01069 };
01070 
01071 // MutexLock(mu) acquires mu when constructed and releases it when destroyed.
01072 class LIBPROTOBUF_EXPORT MutexLock {
01073  public:
01074   explicit MutexLock(Mutex *mu) : mu_(mu) { this->mu_->Lock(); }
01075   ~MutexLock() { this->mu_->Unlock(); }
01076  private:
01077   Mutex *const mu_;
01078   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MutexLock);
01079 };
01080 
01081 // TODO(kenton):  Implement these?  Hard to implement portably.
01082 typedef MutexLock ReaderMutexLock;
01083 typedef MutexLock WriterMutexLock;
01084 
01085 // MutexLockMaybe is like MutexLock, but is a no-op when mu is NULL.
01086 class LIBPROTOBUF_EXPORT MutexLockMaybe {
01087  public:
01088   explicit MutexLockMaybe(Mutex *mu) :
01089     mu_(mu) { if (this->mu_ != NULL) { this->mu_->Lock(); } }
01090   ~MutexLockMaybe() { if (this->mu_ != NULL) { this->mu_->Unlock(); } }
01091  private:
01092   Mutex *const mu_;
01093   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MutexLockMaybe);
01094 };
01095 
01096 }  // namespace internal
01097 
01098 // We made these internal so that they would show up as such in the docs,
01099 // but we don't want to stick "internal::" in front of them everywhere.
01100 using internal::Mutex;
01101 using internal::MutexLock;
01102 using internal::ReaderMutexLock;
01103 using internal::WriterMutexLock;
01104 using internal::MutexLockMaybe;
01105 
01106 // ===================================================================
01107 // from google3/base/type_traits.h
01108 
01109 namespace internal {
01110 
01111 // Specified by TR1 [4.7.4] Pointer modifications.
01112 template<typename T> struct remove_pointer { typedef T type; };
01113 template<typename T> struct remove_pointer<T*> { typedef T type; };
01114 template<typename T> struct remove_pointer<T* const> { typedef T type; };
01115 template<typename T> struct remove_pointer<T* volatile> { typedef T type; };
01116 template<typename T> struct remove_pointer<T* const volatile> {
01117   typedef T type; };
01118 
01119 // ===================================================================
01120 
01121 // Checks if the buffer contains structurally-valid UTF-8.  Implemented in
01122 // structurally_valid.cc.
01123 LIBPROTOBUF_EXPORT bool IsStructurallyValidUTF8(const char* buf, int len);
01124 
01125 }  // namespace internal
01126 
01127 // ===================================================================
01128 // Shutdown support.
01129 
01130 // Shut down the entire protocol buffers library, deleting all static-duration
01131 // objects allocated by the library or by generated .pb.cc files.
01132 //
01133 // There are two reasons you might want to call this:
01134 // * You use a draconian definition of "memory leak" in which you expect
01135 //   every single malloc() to have a corresponding free(), even for objects
01136 //   which live until program exit.
01137 // * You are writing a dynamically-loaded library which needs to clean up
01138 //   after itself when the library is unloaded.
01139 //
01140 // It is safe to call this multiple times.  However, it is not safe to use
01141 // any other part of the protocol buffers library after
01142 // ShutdownProtobufLibrary() has been called.
01143 LIBPROTOBUF_EXPORT void ShutdownProtobufLibrary();
01144 
01145 namespace internal {
01146 
01147 // Register a function to be called when ShutdownProtocolBuffers() is called.
01148 LIBPROTOBUF_EXPORT void OnShutdown(void (*func)());
01149 
01150 }  // namespace internal
01151 
01152 }  // namespace protobuf
01153 }  // namespace google
01154 
01155 #endif  // GOOGLE_PROTOBUF_COMMON_H__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines