Home | History | Annotate | Download | only in stubs
      1 // Protocol Buffers - Google's data interchange format
      2 // Copyright 2008 Google Inc.  All rights reserved.
      3 // http://code.google.com/p/protobuf/
      4 //
      5 // Redistribution and use in source and binary forms, with or without
      6 // modification, are permitted provided that the following conditions are
      7 // met:
      8 //
      9 //     * Redistributions of source code must retain the above copyright
     10 // notice, this list of conditions and the following disclaimer.
     11 //     * Redistributions in binary form must reproduce the above
     12 // copyright notice, this list of conditions and the following disclaimer
     13 // in the documentation and/or other materials provided with the
     14 // distribution.
     15 //     * Neither the name of Google Inc. nor the names of its
     16 // contributors may be used to endorse or promote products derived from
     17 // this software without specific prior written permission.
     18 //
     19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30 
     31 // Author: kenton (at) google.com (Kenton Varda) and others
     32 //
     33 // Contains basic types and utilities used by the rest of the library.
     34 
     35 #ifndef GOOGLE_PROTOBUF_COMMON_H__
     36 #define GOOGLE_PROTOBUF_COMMON_H__
     37 
     38 #include <assert.h>
     39 #include <stdlib.h>
     40 #include <cstddef>
     41 #include <string>
     42 #include <string.h>
     43 #if defined(__osf__)
     44 // Tru64 lacks stdint.h, but has inttypes.h which defines a superset of
     45 // what stdint.h would define.
     46 #include <inttypes.h>
     47 #elif !defined(_MSC_VER)
     48 #include <stdint.h>
     49 #endif
     50 
     51 #ifndef PROTOBUF_USE_EXCEPTIONS
     52 #if defined(_MSC_VER) && defined(_CPPUNWIND)
     53   #define PROTOBUF_USE_EXCEPTIONS 1
     54 #elif defined(__EXCEPTIONS)
     55   #define PROTOBUF_USE_EXCEPTIONS 1
     56 #else
     57   #define PROTOBUF_USE_EXCEPTIONS 0
     58 #endif
     59 #endif
     60 
     61 #if PROTOBUF_USE_EXCEPTIONS
     62 #include <exception>
     63 #endif
     64 
     65 #if defined(_WIN32) && defined(GetMessage)
     66 // Allow GetMessage to be used as a valid method name in protobuf classes.
     67 // windows.h defines GetMessage() as a macro.  Let's re-define it as an inline
     68 // function.  The inline function should be equivalent for C++ users.
     69 inline BOOL GetMessage_Win32(
     70     LPMSG lpMsg, HWND hWnd,
     71     UINT wMsgFilterMin, UINT wMsgFilterMax) {
     72   return GetMessage(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax);
     73 }
     74 #undef GetMessage
     75 inline BOOL GetMessage(
     76     LPMSG lpMsg, HWND hWnd,
     77     UINT wMsgFilterMin, UINT wMsgFilterMax) {
     78   return GetMessage_Win32(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax);
     79 }
     80 #endif
     81 
     82 
     83 namespace std {}
     84 
     85 namespace google {
     86 namespace protobuf {
     87 
     88 #undef GOOGLE_DISALLOW_EVIL_CONSTRUCTORS
     89 #define GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeName)    \
     90   TypeName(const TypeName&);                           \
     91   void operator=(const TypeName&)
     92 
     93 // The macros defined below are required in order to make protobuf_lite a
     94 // component on all platforms. See http://crbug.com/172800.
     95 #if defined(COMPONENT_BUILD) && defined(PROTOBUF_USE_DLLS)
     96   #if defined(_MSC_VER)
     97     #ifdef LIBPROTOBUF_EXPORTS
     98       #define LIBPROTOBUF_EXPORT __declspec(dllexport)
     99     #else
    100       #define LIBPROTOBUF_EXPORT __declspec(dllimport)
    101     #endif
    102     #ifdef LIBPROTOC_EXPORTS
    103       #define LIBPROTOC_EXPORT   __declspec(dllexport)
    104     #else
    105       #define LIBPROTOC_EXPORT   __declspec(dllimport)
    106     #endif
    107   #else  // defined(_MSC_VER)
    108     #ifdef LIBPROTOBUF_EXPORTS
    109       #define LIBPROTOBUF_EXPORT __attribute__((visibility("default")))
    110     #else
    111       #define LIBPROTOBUF_EXPORT
    112     #endif
    113     #ifdef LIBPROTOC_EXPORTS
    114       #define LIBPROTOC_EXPORT   __attribute__((visibility("default")))
    115     #else
    116       #define LIBPROTOC_EXPORT
    117     #endif
    118   #endif
    119 #else  // defined(COMPONENT_BUILD) && defined(PROTOBUF_USE_DLLS)
    120   #define LIBPROTOBUF_EXPORT
    121   #define LIBPROTOC_EXPORT
    122 #endif
    123 
    124 namespace internal {
    125 
    126 // Some of these constants are macros rather than const ints so that they can
    127 // be used in #if directives.
    128 
    129 // The current version, represented as a single integer to make comparison
    130 // easier:  major * 10^6 + minor * 10^3 + micro
    131 #define GOOGLE_PROTOBUF_VERSION 2005000
    132 
    133 // The minimum library version which works with the current version of the
    134 // headers.
    135 #define GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION 2005000
    136 
    137 // The minimum header version which works with the current version of
    138 // the library.  This constant should only be used by protoc's C++ code
    139 // generator.
    140 static const int kMinHeaderVersionForLibrary = 2005000;
    141 
    142 // The minimum protoc version which works with the current version of the
    143 // headers.
    144 #define GOOGLE_PROTOBUF_MIN_PROTOC_VERSION 2005000
    145 
    146 // The minimum header version which works with the current version of
    147 // protoc.  This constant should only be used in VerifyVersion().
    148 static const int kMinHeaderVersionForProtoc = 2005000;
    149 
    150 // Verifies that the headers and libraries are compatible.  Use the macro
    151 // below to call this.
    152 void LIBPROTOBUF_EXPORT VerifyVersion(int headerVersion, int minLibraryVersion,
    153                                       const char* filename);
    154 
    155 // Converts a numeric version number to a string.
    156 std::string LIBPROTOBUF_EXPORT VersionString(int version);
    157 
    158 }  // namespace internal
    159 
    160 // Place this macro in your main() function (or somewhere before you attempt
    161 // to use the protobuf library) to verify that the version you link against
    162 // matches the headers you compiled against.  If a version mismatch is
    163 // detected, the process will abort.
    164 #define GOOGLE_PROTOBUF_VERIFY_VERSION                                    \
    165   ::google::protobuf::internal::VerifyVersion(                            \
    166     GOOGLE_PROTOBUF_VERSION, GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION,         \
    167     __FILE__)
    168 
    169 // ===================================================================
    170 // from google3/base/port.h
    171 
    172 typedef unsigned int uint;
    173 
    174 #ifdef _MSC_VER
    175 typedef __int8  int8;
    176 typedef __int16 int16;
    177 typedef __int32 int32;
    178 typedef __int64 int64;
    179 
    180 typedef unsigned __int8  uint8;
    181 typedef unsigned __int16 uint16;
    182 typedef unsigned __int32 uint32;
    183 typedef unsigned __int64 uint64;
    184 #else
    185 typedef int8_t  int8;
    186 typedef int16_t int16;
    187 typedef int32_t int32;
    188 typedef int64_t int64;
    189 
    190 typedef uint8_t  uint8;
    191 typedef uint16_t uint16;
    192 typedef uint32_t uint32;
    193 typedef uint64_t uint64;
    194 #endif
    195 
    196 // long long macros to be used because gcc and vc++ use different suffixes,
    197 // and different size specifiers in format strings
    198 #undef GOOGLE_LONGLONG
    199 #undef GOOGLE_ULONGLONG
    200 #undef GOOGLE_LL_FORMAT
    201 
    202 #ifdef _MSC_VER
    203 #define GOOGLE_LONGLONG(x) x##I64
    204 #define GOOGLE_ULONGLONG(x) x##UI64
    205 #define GOOGLE_LL_FORMAT "I64"  // As in printf("%I64d", ...)
    206 #else
    207 #define GOOGLE_LONGLONG(x) x##LL
    208 #define GOOGLE_ULONGLONG(x) x##ULL
    209 #define GOOGLE_LL_FORMAT "ll"  // As in "%lld". Note that "q" is poor form also.
    210 #endif
    211 
    212 static const int32 kint32max = 0x7FFFFFFF;
    213 static const int32 kint32min = -kint32max - 1;
    214 static const int64 kint64max = GOOGLE_LONGLONG(0x7FFFFFFFFFFFFFFF);
    215 static const int64 kint64min = -kint64max - 1;
    216 static const uint32 kuint32max = 0xFFFFFFFFu;
    217 static const uint64 kuint64max = GOOGLE_ULONGLONG(0xFFFFFFFFFFFFFFFF);
    218 
    219 // -------------------------------------------------------------------
    220 // Annotations:  Some parts of the code have been annotated in ways that might
    221 //   be useful to some compilers or tools, but are not supported universally.
    222 //   You can #define these annotations yourself if the default implementation
    223 //   is not right for you.
    224 
    225 #ifndef GOOGLE_ATTRIBUTE_ALWAYS_INLINE
    226 #if defined(__GNUC__) && (__GNUC__ > 3 ||(__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
    227 // For functions we want to force inline.
    228 // Introduced in gcc 3.1.
    229 #define GOOGLE_ATTRIBUTE_ALWAYS_INLINE __attribute__ ((always_inline))
    230 #else
    231 // Other compilers will have to figure it out for themselves.
    232 #define GOOGLE_ATTRIBUTE_ALWAYS_INLINE
    233 #endif
    234 #endif
    235 
    236 #ifndef GOOGLE_ATTRIBUTE_DEPRECATED
    237 #ifdef __GNUC__
    238 // If the method/variable/type is used anywhere, produce a warning.
    239 #define GOOGLE_ATTRIBUTE_DEPRECATED __attribute__((deprecated))
    240 #else
    241 #define GOOGLE_ATTRIBUTE_DEPRECATED
    242 #endif
    243 #endif
    244 
    245 #ifndef GOOGLE_PREDICT_TRUE
    246 #ifdef __GNUC__
    247 // Provided at least since GCC 3.0.
    248 #define GOOGLE_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
    249 #else
    250 #define GOOGLE_PREDICT_TRUE
    251 #endif
    252 #endif
    253 
    254 // Delimits a block of code which may write to memory which is simultaneously
    255 // written by other threads, but which has been determined to be thread-safe
    256 // (e.g. because it is an idempotent write).
    257 #ifndef GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN
    258 #define GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN()
    259 #endif
    260 #ifndef GOOGLE_SAFE_CONCURRENT_WRITES_END
    261 #define GOOGLE_SAFE_CONCURRENT_WRITES_END()
    262 #endif
    263 
    264 // ===================================================================
    265 // from google3/base/basictypes.h
    266 
    267 // The GOOGLE_ARRAYSIZE(arr) macro returns the # of elements in an array arr.
    268 // The expression is a compile-time constant, and therefore can be
    269 // used in defining new arrays, for example.
    270 //
    271 // GOOGLE_ARRAYSIZE catches a few type errors.  If you see a compiler error
    272 //
    273 //   "warning: division by zero in ..."
    274 //
    275 // when using GOOGLE_ARRAYSIZE, you are (wrongfully) giving it a pointer.
    276 // You should only use GOOGLE_ARRAYSIZE on statically allocated arrays.
    277 //
    278 // The following comments are on the implementation details, and can
    279 // be ignored by the users.
    280 //
    281 // ARRAYSIZE(arr) works by inspecting sizeof(arr) (the # of bytes in
    282 // the array) and sizeof(*(arr)) (the # of bytes in one array
    283 // element).  If the former is divisible by the latter, perhaps arr is
    284 // indeed an array, in which case the division result is the # of
    285 // elements in the array.  Otherwise, arr cannot possibly be an array,
    286 // and we generate a compiler error to prevent the code from
    287 // compiling.
    288 //
    289 // Since the size of bool is implementation-defined, we need to cast
    290 // !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final
    291 // result has type size_t.
    292 //
    293 // This macro is not perfect as it wrongfully accepts certain
    294 // pointers, namely where the pointer size is divisible by the pointee
    295 // size.  Since all our code has to go through a 32-bit compiler,
    296 // where a pointer is 4 bytes, this means all pointers to a type whose
    297 // size is 3 or greater than 4 will be (righteously) rejected.
    298 //
    299 // Kudos to Jorg Brown for this simple and elegant implementation.
    300 
    301 #undef GOOGLE_ARRAYSIZE
    302 #define GOOGLE_ARRAYSIZE(a) \
    303   ((sizeof(a) / sizeof(*(a))) / \
    304    static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
    305 
    306 namespace internal {
    307 
    308 // Use implicit_cast as a safe version of static_cast or const_cast
    309 // for upcasting in the type hierarchy (i.e. casting a pointer to Foo
    310 // to a pointer to SuperclassOfFoo or casting a pointer to Foo to
    311 // a const pointer to Foo).
    312 // When you use implicit_cast, the compiler checks that the cast is safe.
    313 // Such explicit implicit_casts are necessary in surprisingly many
    314 // situations where C++ demands an exact type match instead of an
    315 // argument type convertable to a target type.
    316 //
    317 // The From type can be inferred, so the preferred syntax for using
    318 // implicit_cast is the same as for static_cast etc.:
    319 //
    320 //   implicit_cast<ToType>(expr)
    321 //
    322 // implicit_cast would have been part of the C++ standard library,
    323 // but the proposal was submitted too late.  It will probably make
    324 // its way into the language in the future.
    325 template<typename To, typename From>
    326 inline To implicit_cast(From const &f) {
    327   return f;
    328 }
    329 
    330 // When you upcast (that is, cast a pointer from type Foo to type
    331 // SuperclassOfFoo), it's fine to use implicit_cast<>, since upcasts
    332 // always succeed.  When you downcast (that is, cast a pointer from
    333 // type Foo to type SubclassOfFoo), static_cast<> isn't safe, because
    334 // how do you know the pointer is really of type SubclassOfFoo?  It
    335 // could be a bare Foo, or of type DifferentSubclassOfFoo.  Thus,
    336 // when you downcast, you should use this macro.  In debug mode, we
    337 // use dynamic_cast<> to double-check the downcast is legal (we die
    338 // if it's not).  In normal mode, we do the efficient static_cast<>
    339 // instead.  Thus, it's important to test in debug mode to make sure
    340 // the cast is legal!
    341 //    This is the only place in the code we should use dynamic_cast<>.
    342 // In particular, you SHOULDN'T be using dynamic_cast<> in order to
    343 // do RTTI (eg code like this:
    344 //    if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);
    345 //    if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);
    346 // You should design the code some other way not to need this.
    347 
    348 template<typename To, typename From>     // use like this: down_cast<T*>(foo);
    349 inline To down_cast(From* f) {                   // so we only accept pointers
    350   // Ensures that To is a sub-type of From *.  This test is here only
    351   // for compile-time type checking, and has no overhead in an
    352   // optimized build at run-time, as it will be optimized away
    353   // completely.
    354   if (false) {
    355     implicit_cast<From*, To>(0);
    356   }
    357 
    358 #if !defined(NDEBUG) && !defined(GOOGLE_PROTOBUF_NO_RTTI)
    359   assert(f == NULL || dynamic_cast<To>(f) != NULL);  // RTTI: debug mode only!
    360 #endif
    361   return static_cast<To>(f);
    362 }
    363 
    364 }  // namespace internal
    365 
    366 // We made these internal so that they would show up as such in the docs,
    367 // but we don't want to stick "internal::" in front of them everywhere.
    368 using internal::implicit_cast;
    369 using internal::down_cast;
    370 
    371 // The COMPILE_ASSERT macro can be used to verify that a compile time
    372 // expression is true. For example, you could use it to verify the
    373 // size of a static array:
    374 //
    375 //   COMPILE_ASSERT(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES,
    376 //                  content_type_names_incorrect_size);
    377 //
    378 // or to make sure a struct is smaller than a certain size:
    379 //
    380 //   COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
    381 //
    382 // The second argument to the macro is the name of the variable. If
    383 // the expression is false, most compilers will issue a warning/error
    384 // containing the name of the variable.
    385 
    386 namespace internal {
    387 
    388 template <bool>
    389 struct CompileAssert {
    390 };
    391 
    392 }  // namespace internal
    393 
    394 #undef GOOGLE_COMPILE_ASSERT
    395 #define GOOGLE_COMPILE_ASSERT(expr, msg) \
    396   typedef ::google::protobuf::internal::CompileAssert<(bool(expr))> \
    397           msg[bool(expr) ? 1 : -1]
    398 
    399 
    400 // Implementation details of COMPILE_ASSERT:
    401 //
    402 // - COMPILE_ASSERT works by defining an array type that has -1
    403 //   elements (and thus is invalid) when the expression is false.
    404 //
    405 // - The simpler definition
    406 //
    407 //     #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1]
    408 //
    409 //   does not work, as gcc supports variable-length arrays whose sizes
    410 //   are determined at run-time (this is gcc's extension and not part
    411 //   of the C++ standard).  As a result, gcc fails to reject the
    412 //   following code with the simple definition:
    413 //
    414 //     int foo;
    415 //     COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is
    416 //                               // not a compile-time constant.
    417 //
    418 // - By using the type CompileAssert<(bool(expr))>, we ensures that
    419 //   expr is a compile-time constant.  (Template arguments must be
    420 //   determined at compile-time.)
    421 //
    422 // - The outter parentheses in CompileAssert<(bool(expr))> are necessary
    423 //   to work around a bug in gcc 3.4.4 and 4.0.1.  If we had written
    424 //
    425 //     CompileAssert<bool(expr)>
    426 //
    427 //   instead, these compilers will refuse to compile
    428 //
    429 //     COMPILE_ASSERT(5 > 0, some_message);
    430 //
    431 //   (They seem to think the ">" in "5 > 0" marks the end of the
    432 //   template argument list.)
    433 //
    434 // - The array size is (bool(expr) ? 1 : -1), instead of simply
    435 //
    436 //     ((expr) ? 1 : -1).
    437 //
    438 //   This is to avoid running into a bug in MS VC 7.1, which
    439 //   causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
    440 
    441 // ===================================================================
    442 // from google3/base/memory/scoped_ptr.h
    443 
    444 namespace internal {
    445 
    446 //  This is an implementation designed to match the anticipated future TR2
    447 //  implementation of the scoped_ptr class, and its closely-related brethren,
    448 //  scoped_array, scoped_ptr_malloc, and make_scoped_ptr.
    449 
    450 template <class C> class scoped_ptr;
    451 template <class C> class scoped_array;
    452 
    453 // A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T>
    454 // automatically deletes the pointer it holds (if any).
    455 // That is, scoped_ptr<T> owns the T object that it points to.
    456 // Like a T*, a scoped_ptr<T> may hold either NULL or a pointer to a T object.
    457 //
    458 // The size of a scoped_ptr is small:
    459 // sizeof(scoped_ptr<C>) == sizeof(C*)
    460 template <class C>
    461 class scoped_ptr {
    462  public:
    463 
    464   // The element type
    465   typedef C element_type;
    466 
    467   // Constructor.  Defaults to intializing with NULL.
    468   // There is no way to create an uninitialized scoped_ptr.
    469   // The input parameter must be allocated with new.
    470   explicit scoped_ptr(C* p = NULL) : ptr_(p) { }
    471 
    472   // Destructor.  If there is a C object, delete it.
    473   // We don't need to test ptr_ == NULL because C++ does that for us.
    474   ~scoped_ptr() {
    475     enum { type_must_be_complete = sizeof(C) };
    476     delete ptr_;
    477   }
    478 
    479   // Reset.  Deletes the current owned object, if any.
    480   // Then takes ownership of a new object, if given.
    481   // this->reset(this->get()) works.
    482   void reset(C* p = NULL) {
    483     if (p != ptr_) {
    484       enum { type_must_be_complete = sizeof(C) };
    485       delete ptr_;
    486       ptr_ = p;
    487     }
    488   }
    489 
    490   // Accessors to get the owned object.
    491   // operator* and operator-> will assert() if there is no current object.
    492   C& operator*() const {
    493     assert(ptr_ != NULL);
    494     return *ptr_;
    495   }
    496   C* operator->() const  {
    497     assert(ptr_ != NULL);
    498     return ptr_;
    499   }
    500   C* get() const { return ptr_; }
    501 
    502   // Comparison operators.
    503   // These return whether two scoped_ptr refer to the same object, not just to
    504   // two different but equal objects.
    505   bool operator==(C* p) const { return ptr_ == p; }
    506   bool operator!=(C* p) const { return ptr_ != p; }
    507 
    508   // Swap two scoped pointers.
    509   void swap(scoped_ptr& p2) {
    510     C* tmp = ptr_;
    511     ptr_ = p2.ptr_;
    512     p2.ptr_ = tmp;
    513   }
    514 
    515   // Release a pointer.
    516   // The return value is the current pointer held by this object.
    517   // If this object holds a NULL pointer, the return value is NULL.
    518   // After this operation, this object will hold a NULL pointer,
    519   // and will not own the object any more.
    520   C* release() {
    521     C* retVal = ptr_;
    522     ptr_ = NULL;
    523     return retVal;
    524   }
    525 
    526  private:
    527   C* ptr_;
    528 
    529   // Forbid comparison of scoped_ptr types.  If C2 != C, it totally doesn't
    530   // make sense, and if C2 == C, it still doesn't make sense because you should
    531   // never have the same object owned by two different scoped_ptrs.
    532   template <class C2> bool operator==(scoped_ptr<C2> const& p2) const;
    533   template <class C2> bool operator!=(scoped_ptr<C2> const& p2) const;
    534 
    535   // Disallow evil constructors
    536   scoped_ptr(const scoped_ptr&);
    537   void operator=(const scoped_ptr&);
    538 };
    539 
    540 // scoped_array<C> is like scoped_ptr<C>, except that the caller must allocate
    541 // with new [] and the destructor deletes objects with delete [].
    542 //
    543 // As with scoped_ptr<C>, a scoped_array<C> either points to an object
    544 // or is NULL.  A scoped_array<C> owns the object that it points to.
    545 //
    546 // Size: sizeof(scoped_array<C>) == sizeof(C*)
    547 template <class C>
    548 class scoped_array {
    549  public:
    550 
    551   // The element type
    552   typedef C element_type;
    553 
    554   // Constructor.  Defaults to intializing with NULL.
    555   // There is no way to create an uninitialized scoped_array.
    556   // The input parameter must be allocated with new [].
    557   explicit scoped_array(C* p = NULL) : array_(p) { }
    558 
    559   // Destructor.  If there is a C object, delete it.
    560   // We don't need to test ptr_ == NULL because C++ does that for us.
    561   ~scoped_array() {
    562     enum { type_must_be_complete = sizeof(C) };
    563     delete[] array_;
    564   }
    565 
    566   // Reset.  Deletes the current owned object, if any.
    567   // Then takes ownership of a new object, if given.
    568   // this->reset(this->get()) works.
    569   void reset(C* p = NULL) {
    570     if (p != array_) {
    571       enum { type_must_be_complete = sizeof(C) };
    572       delete[] array_;
    573       array_ = p;
    574     }
    575   }
    576 
    577   // Get one element of the current object.
    578   // Will assert() if there is no current object, or index i is negative.
    579   C& operator[](std::ptrdiff_t i) const {
    580     assert(i >= 0);
    581     assert(array_ != NULL);
    582     return array_[i];
    583   }
    584 
    585   // Get a pointer to the zeroth element of the current object.
    586   // If there is no current object, return NULL.
    587   C* get() const {
    588     return array_;
    589   }
    590 
    591   // Comparison operators.
    592   // These return whether two scoped_array refer to the same object, not just to
    593   // two different but equal objects.
    594   bool operator==(C* p) const { return array_ == p; }
    595   bool operator!=(C* p) const { return array_ != p; }
    596 
    597   // Swap two scoped arrays.
    598   void swap(scoped_array& p2) {
    599     C* tmp = array_;
    600     array_ = p2.array_;
    601     p2.array_ = tmp;
    602   }
    603 
    604   // Release an array.
    605   // The return value is the current pointer held by this object.
    606   // If this object holds a NULL pointer, the return value is NULL.
    607   // After this operation, this object will hold a NULL pointer,
    608   // and will not own the object any more.
    609   C* release() {
    610     C* retVal = array_;
    611     array_ = NULL;
    612     return retVal;
    613   }
    614 
    615  private:
    616   C* array_;
    617 
    618   // Forbid comparison of different scoped_array types.
    619   template <class C2> bool operator==(scoped_array<C2> const& p2) const;
    620   template <class C2> bool operator!=(scoped_array<C2> const& p2) const;
    621 
    622   // Disallow evil constructors
    623   scoped_array(const scoped_array&);
    624   void operator=(const scoped_array&);
    625 };
    626 
    627 }  // namespace internal
    628 
    629 // We made these internal so that they would show up as such in the docs,
    630 // but we don't want to stick "internal::" in front of them everywhere.
    631 using internal::scoped_ptr;
    632 using internal::scoped_array;
    633 
    634 // ===================================================================
    635 // emulates google3/base/logging.h
    636 
    637 enum LogLevel {
    638   LOGLEVEL_INFO,     // Informational.  This is never actually used by
    639                      // libprotobuf.
    640   LOGLEVEL_WARNING,  // Warns about issues that, although not technically a
    641                      // problem now, could cause problems in the future.  For
    642                      // example, a // warning will be printed when parsing a
    643                      // message that is near the message size limit.
    644   LOGLEVEL_ERROR,    // An error occurred which should never happen during
    645                      // normal use.
    646   LOGLEVEL_FATAL,    // An error occurred from which the library cannot
    647                      // recover.  This usually indicates a programming error
    648                      // in the code which calls the library, especially when
    649                      // compiled in debug mode.
    650 
    651 #ifdef NDEBUG
    652   LOGLEVEL_DFATAL = LOGLEVEL_ERROR
    653 #else
    654   LOGLEVEL_DFATAL = LOGLEVEL_FATAL
    655 #endif
    656 };
    657 
    658 namespace internal {
    659 
    660 class LogFinisher;
    661 
    662 class LIBPROTOBUF_EXPORT LogMessage {
    663  public:
    664   LogMessage(LogLevel level, const char* filename, int line);
    665   ~LogMessage();
    666 
    667   LogMessage& operator<<(const std::string& value);
    668   LogMessage& operator<<(const char* value);
    669   LogMessage& operator<<(char value);
    670   LogMessage& operator<<(int value);
    671   LogMessage& operator<<(uint value);
    672   LogMessage& operator<<(long value);
    673   LogMessage& operator<<(unsigned long value);
    674   LogMessage& operator<<(double value);
    675 
    676  private:
    677   friend class LogFinisher;
    678   void Finish();
    679 
    680   LogLevel level_;
    681   const char* filename_;
    682   int line_;
    683   std::string message_;
    684 };
    685 
    686 // Used to make the entire "LOG(BLAH) << etc." expression have a void return
    687 // type and print a newline after each message.
    688 class LIBPROTOBUF_EXPORT LogFinisher {
    689  public:
    690   void operator=(LogMessage& other);
    691 };
    692 
    693 }  // namespace internal
    694 
    695 // Undef everything in case we're being mixed with some other Google library
    696 // which already defined them itself.  Presumably all Google libraries will
    697 // support the same syntax for these so it should not be a big deal if they
    698 // end up using our definitions instead.
    699 #undef GOOGLE_LOG
    700 #undef GOOGLE_LOG_IF
    701 
    702 #undef GOOGLE_CHECK
    703 #undef GOOGLE_CHECK_EQ
    704 #undef GOOGLE_CHECK_NE
    705 #undef GOOGLE_CHECK_LT
    706 #undef GOOGLE_CHECK_LE
    707 #undef GOOGLE_CHECK_GT
    708 #undef GOOGLE_CHECK_GE
    709 #undef GOOGLE_CHECK_NOTNULL
    710 
    711 #undef GOOGLE_DLOG
    712 #undef GOOGLE_DCHECK
    713 #undef GOOGLE_DCHECK_EQ
    714 #undef GOOGLE_DCHECK_NE
    715 #undef GOOGLE_DCHECK_LT
    716 #undef GOOGLE_DCHECK_LE
    717 #undef GOOGLE_DCHECK_GT
    718 #undef GOOGLE_DCHECK_GE
    719 
    720 #define GOOGLE_LOG(LEVEL)                                                 \
    721   ::google::protobuf::internal::LogFinisher() =                           \
    722     ::google::protobuf::internal::LogMessage(                             \
    723       ::google::protobuf::LOGLEVEL_##LEVEL, __FILE__, __LINE__)
    724 #define GOOGLE_LOG_IF(LEVEL, CONDITION) \
    725   !(CONDITION) ? (void)0 : GOOGLE_LOG(LEVEL)
    726 
    727 #define GOOGLE_CHECK(EXPRESSION) \
    728   GOOGLE_LOG_IF(FATAL, !(EXPRESSION)) << "CHECK failed: " #EXPRESSION ": "
    729 #define GOOGLE_CHECK_EQ(A, B) GOOGLE_CHECK((A) == (B))
    730 #define GOOGLE_CHECK_NE(A, B) GOOGLE_CHECK((A) != (B))
    731 #define GOOGLE_CHECK_LT(A, B) GOOGLE_CHECK((A) <  (B))
    732 #define GOOGLE_CHECK_LE(A, B) GOOGLE_CHECK((A) <= (B))
    733 #define GOOGLE_CHECK_GT(A, B) GOOGLE_CHECK((A) >  (B))
    734 #define GOOGLE_CHECK_GE(A, B) GOOGLE_CHECK((A) >= (B))
    735 
    736 namespace internal {
    737 template<typename T>
    738 T* CheckNotNull(const char *file, int line, const char *name, T* val) {
    739   if (val == NULL) {
    740     GOOGLE_LOG(FATAL) << name;
    741   }
    742   return val;
    743 }
    744 }  // namespace internal
    745 #define GOOGLE_CHECK_NOTNULL(A) \
    746   internal::CheckNotNull(__FILE__, __LINE__, "'" #A "' must not be NULL", (A))
    747 
    748 #ifdef NDEBUG
    749 
    750 #define GOOGLE_DLOG GOOGLE_LOG_IF(INFO, false)
    751 
    752 #define GOOGLE_DCHECK(EXPRESSION) while(false) GOOGLE_CHECK(EXPRESSION)
    753 #define GOOGLE_DCHECK_EQ(A, B) GOOGLE_DCHECK((A) == (B))
    754 #define GOOGLE_DCHECK_NE(A, B) GOOGLE_DCHECK((A) != (B))
    755 #define GOOGLE_DCHECK_LT(A, B) GOOGLE_DCHECK((A) <  (B))
    756 #define GOOGLE_DCHECK_LE(A, B) GOOGLE_DCHECK((A) <= (B))
    757 #define GOOGLE_DCHECK_GT(A, B) GOOGLE_DCHECK((A) >  (B))
    758 #define GOOGLE_DCHECK_GE(A, B) GOOGLE_DCHECK((A) >= (B))
    759 
    760 #else  // NDEBUG
    761 
    762 #define GOOGLE_DLOG GOOGLE_LOG
    763 
    764 #define GOOGLE_DCHECK    GOOGLE_CHECK
    765 #define GOOGLE_DCHECK_EQ GOOGLE_CHECK_EQ
    766 #define GOOGLE_DCHECK_NE GOOGLE_CHECK_NE
    767 #define GOOGLE_DCHECK_LT GOOGLE_CHECK_LT
    768 #define GOOGLE_DCHECK_LE GOOGLE_CHECK_LE
    769 #define GOOGLE_DCHECK_GT GOOGLE_CHECK_GT
    770 #define GOOGLE_DCHECK_GE GOOGLE_CHECK_GE
    771 
    772 #endif  // !NDEBUG
    773 
    774 typedef void LogHandler(LogLevel level, const char* filename, int line,
    775                         const std::string& message);
    776 
    777 // The protobuf library sometimes writes warning and error messages to
    778 // stderr.  These messages are primarily useful for developers, but may
    779 // also help end users figure out a problem.  If you would prefer that
    780 // these messages be sent somewhere other than stderr, call SetLogHandler()
    781 // to set your own handler.  This returns the old handler.  Set the handler
    782 // to NULL to ignore log messages (but see also LogSilencer, below).
    783 //
    784 // Obviously, SetLogHandler is not thread-safe.  You should only call it
    785 // at initialization time, and probably not from library code.  If you
    786 // simply want to suppress log messages temporarily (e.g. because you
    787 // have some code that tends to trigger them frequently and you know
    788 // the warnings are not important to you), use the LogSilencer class
    789 // below.
    790 LIBPROTOBUF_EXPORT LogHandler* SetLogHandler(LogHandler* new_func);
    791 
    792 // Create a LogSilencer if you want to temporarily suppress all log
    793 // messages.  As long as any LogSilencer objects exist, non-fatal
    794 // log messages will be discarded (the current LogHandler will *not*
    795 // be called).  Constructing a LogSilencer is thread-safe.  You may
    796 // accidentally suppress log messages occurring in another thread, but
    797 // since messages are generally for debugging purposes only, this isn't
    798 // a big deal.  If you want to intercept log messages, use SetLogHandler().
    799 class LIBPROTOBUF_EXPORT LogSilencer {
    800  public:
    801   LogSilencer();
    802   ~LogSilencer();
    803 };
    804 
    805 // ===================================================================
    806 // emulates google3/base/callback.h
    807 
    808 // Abstract interface for a callback.  When calling an RPC, you must provide
    809 // a Closure to call when the procedure completes.  See the Service interface
    810 // in service.h.
    811 //
    812 // To automatically construct a Closure which calls a particular function or
    813 // method with a particular set of parameters, use the NewCallback() function.
    814 // Example:
    815 //   void FooDone(const FooResponse* response) {
    816 //     ...
    817 //   }
    818 //
    819 //   void CallFoo() {
    820 //     ...
    821 //     // When done, call FooDone() and pass it a pointer to the response.
    822 //     Closure* callback = NewCallback(&FooDone, response);
    823 //     // Make the call.
    824 //     service->Foo(controller, request, response, callback);
    825 //   }
    826 //
    827 // Example that calls a method:
    828 //   class Handler {
    829 //    public:
    830 //     ...
    831 //
    832 //     void FooDone(const FooResponse* response) {
    833 //       ...
    834 //     }
    835 //
    836 //     void CallFoo() {
    837 //       ...
    838 //       // When done, call FooDone() and pass it a pointer to the response.
    839 //       Closure* callback = NewCallback(this, &Handler::FooDone, response);
    840 //       // Make the call.
    841 //       service->Foo(controller, request, response, callback);
    842 //     }
    843 //   };
    844 //
    845 // Currently NewCallback() supports binding zero, one, or two arguments.
    846 //
    847 // Callbacks created with NewCallback() automatically delete themselves when
    848 // executed.  They should be used when a callback is to be called exactly
    849 // once (usually the case with RPC callbacks).  If a callback may be called
    850 // a different number of times (including zero), create it with
    851 // NewPermanentCallback() instead.  You are then responsible for deleting the
    852 // callback (using the "delete" keyword as normal).
    853 //
    854 // Note that NewCallback() is a bit touchy regarding argument types.  Generally,
    855 // the values you provide for the parameter bindings must exactly match the
    856 // types accepted by the callback function.  For example:
    857 //   void Foo(string s);
    858 //   NewCallback(&Foo, "foo");          // WON'T WORK:  const char* != string
    859 //   NewCallback(&Foo, string("foo"));  // WORKS
    860 // Also note that the arguments cannot be references:
    861 //   void Foo(const string& s);
    862 //   string my_str;
    863 //   NewCallback(&Foo, my_str);  // WON'T WORK:  Can't use referecnes.
    864 // However, correctly-typed pointers will work just fine.
    865 class LIBPROTOBUF_EXPORT Closure {
    866  public:
    867   Closure() {}
    868   virtual ~Closure();
    869 
    870   virtual void Run() = 0;
    871 
    872  private:
    873   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Closure);
    874 };
    875 
    876 namespace internal {
    877 
    878 class LIBPROTOBUF_EXPORT FunctionClosure0 : public Closure {
    879  public:
    880   typedef void (*FunctionType)();
    881 
    882   FunctionClosure0(FunctionType function, bool self_deleting)
    883     : function_(function), self_deleting_(self_deleting) {}
    884   ~FunctionClosure0();
    885 
    886   void Run() {
    887     bool needs_delete = self_deleting_;  // read in case callback deletes
    888     function_();
    889     if (needs_delete) delete this;
    890   }
    891 
    892  private:
    893   FunctionType function_;
    894   bool self_deleting_;
    895 };
    896 
    897 template <typename Class>
    898 class MethodClosure0 : public Closure {
    899  public:
    900   typedef void (Class::*MethodType)();
    901 
    902   MethodClosure0(Class* object, MethodType method, bool self_deleting)
    903     : object_(object), method_(method), self_deleting_(self_deleting) {}
    904   ~MethodClosure0() {}
    905 
    906   void Run() {
    907     bool needs_delete = self_deleting_;  // read in case callback deletes
    908     (object_->*method_)();
    909     if (needs_delete) delete this;
    910   }
    911 
    912  private:
    913   Class* object_;
    914   MethodType method_;
    915   bool self_deleting_;
    916 };
    917 
    918 template <typename Arg1>
    919 class FunctionClosure1 : public Closure {
    920  public:
    921   typedef void (*FunctionType)(Arg1 arg1);
    922 
    923   FunctionClosure1(FunctionType function, bool self_deleting,
    924                    Arg1 arg1)
    925     : function_(function), self_deleting_(self_deleting),
    926       arg1_(arg1) {}
    927   ~FunctionClosure1() {}
    928 
    929   void Run() {
    930     bool needs_delete = self_deleting_;  // read in case callback deletes
    931     function_(arg1_);
    932     if (needs_delete) delete this;
    933   }
    934 
    935  private:
    936   FunctionType function_;
    937   bool self_deleting_;
    938   Arg1 arg1_;
    939 };
    940 
    941 template <typename Class, typename Arg1>
    942 class MethodClosure1 : public Closure {
    943  public:
    944   typedef void (Class::*MethodType)(Arg1 arg1);
    945 
    946   MethodClosure1(Class* object, MethodType method, bool self_deleting,
    947                  Arg1 arg1)
    948     : object_(object), method_(method), self_deleting_(self_deleting),
    949       arg1_(arg1) {}
    950   ~MethodClosure1() {}
    951 
    952   void Run() {
    953     bool needs_delete = self_deleting_;  // read in case callback deletes
    954     (object_->*method_)(arg1_);
    955     if (needs_delete) delete this;
    956   }
    957 
    958  private:
    959   Class* object_;
    960   MethodType method_;
    961   bool self_deleting_;
    962   Arg1 arg1_;
    963 };
    964 
    965 template <typename Arg1, typename Arg2>
    966 class FunctionClosure2 : public Closure {
    967  public:
    968   typedef void (*FunctionType)(Arg1 arg1, Arg2 arg2);
    969 
    970   FunctionClosure2(FunctionType function, bool self_deleting,
    971                    Arg1 arg1, Arg2 arg2)
    972     : function_(function), self_deleting_(self_deleting),
    973       arg1_(arg1), arg2_(arg2) {}
    974   ~FunctionClosure2() {}
    975 
    976   void Run() {
    977     bool needs_delete = self_deleting_;  // read in case callback deletes
    978     function_(arg1_, arg2_);
    979     if (needs_delete) delete this;
    980   }
    981 
    982  private:
    983   FunctionType function_;
    984   bool self_deleting_;
    985   Arg1 arg1_;
    986   Arg2 arg2_;
    987 };
    988 
    989 template <typename Class, typename Arg1, typename Arg2>
    990 class MethodClosure2 : public Closure {
    991  public:
    992   typedef void (Class::*MethodType)(Arg1 arg1, Arg2 arg2);
    993 
    994   MethodClosure2(Class* object, MethodType method, bool self_deleting,
    995                  Arg1 arg1, Arg2 arg2)
    996     : object_(object), method_(method), self_deleting_(self_deleting),
    997       arg1_(arg1), arg2_(arg2) {}
    998   ~MethodClosure2() {}
    999 
   1000   void Run() {
   1001     bool needs_delete = self_deleting_;  // read in case callback deletes
   1002     (object_->*method_)(arg1_, arg2_);
   1003     if (needs_delete) delete this;
   1004   }
   1005 
   1006  private:
   1007   Class* object_;
   1008   MethodType method_;
   1009   bool self_deleting_;
   1010   Arg1 arg1_;
   1011   Arg2 arg2_;
   1012 };
   1013 
   1014 }  // namespace internal
   1015 
   1016 // See Closure.
   1017 inline Closure* NewCallback(void (*function)()) {
   1018   return new internal::FunctionClosure0(function, true);
   1019 }
   1020 
   1021 // See Closure.
   1022 inline Closure* NewPermanentCallback(void (*function)()) {
   1023   return new internal::FunctionClosure0(function, false);
   1024 }
   1025 
   1026 // See Closure.
   1027 template <typename Class>
   1028 inline Closure* NewCallback(Class* object, void (Class::*method)()) {
   1029   return new internal::MethodClosure0<Class>(object, method, true);
   1030 }
   1031 
   1032 // See Closure.
   1033 template <typename Class>
   1034 inline Closure* NewPermanentCallback(Class* object, void (Class::*method)()) {
   1035   return new internal::MethodClosure0<Class>(object, method, false);
   1036 }
   1037 
   1038 // See Closure.
   1039 template <typename Arg1>
   1040 inline Closure* NewCallback(void (*function)(Arg1),
   1041                             Arg1 arg1) {
   1042   return new internal::FunctionClosure1<Arg1>(function, true, arg1);
   1043 }
   1044 
   1045 // See Closure.
   1046 template <typename Arg1>
   1047 inline Closure* NewPermanentCallback(void (*function)(Arg1),
   1048                                      Arg1 arg1) {
   1049   return new internal::FunctionClosure1<Arg1>(function, false, arg1);
   1050 }
   1051 
   1052 // See Closure.
   1053 template <typename Class, typename Arg1>
   1054 inline Closure* NewCallback(Class* object, void (Class::*method)(Arg1),
   1055                             Arg1 arg1) {
   1056   return new internal::MethodClosure1<Class, Arg1>(object, method, true, arg1);
   1057 }
   1058 
   1059 // See Closure.
   1060 template <typename Class, typename Arg1>
   1061 inline Closure* NewPermanentCallback(Class* object, void (Class::*method)(Arg1),
   1062                                      Arg1 arg1) {
   1063   return new internal::MethodClosure1<Class, Arg1>(object, method, false, arg1);
   1064 }
   1065 
   1066 // See Closure.
   1067 template <typename Arg1, typename Arg2>
   1068 inline Closure* NewCallback(void (*function)(Arg1, Arg2),
   1069                             Arg1 arg1, Arg2 arg2) {
   1070   return new internal::FunctionClosure2<Arg1, Arg2>(
   1071     function, true, arg1, arg2);
   1072 }
   1073 
   1074 // See Closure.
   1075 template <typename Arg1, typename Arg2>
   1076 inline Closure* NewPermanentCallback(void (*function)(Arg1, Arg2),
   1077                                      Arg1 arg1, Arg2 arg2) {
   1078   return new internal::FunctionClosure2<Arg1, Arg2>(
   1079     function, false, arg1, arg2);
   1080 }
   1081 
   1082 // See Closure.
   1083 template <typename Class, typename Arg1, typename Arg2>
   1084 inline Closure* NewCallback(Class* object, void (Class::*method)(Arg1, Arg2),
   1085                             Arg1 arg1, Arg2 arg2) {
   1086   return new internal::MethodClosure2<Class, Arg1, Arg2>(
   1087     object, method, true, arg1, arg2);
   1088 }
   1089 
   1090 // See Closure.
   1091 template <typename Class, typename Arg1, typename Arg2>
   1092 inline Closure* NewPermanentCallback(
   1093     Class* object, void (Class::*method)(Arg1, Arg2),
   1094     Arg1 arg1, Arg2 arg2) {
   1095   return new internal::MethodClosure2<Class, Arg1, Arg2>(
   1096     object, method, false, arg1, arg2);
   1097 }
   1098 
   1099 // A function which does nothing.  Useful for creating no-op callbacks, e.g.:
   1100 //   Closure* nothing = NewCallback(&DoNothing);
   1101 void LIBPROTOBUF_EXPORT DoNothing();
   1102 
   1103 // ===================================================================
   1104 // emulates google3/base/mutex.h
   1105 
   1106 namespace internal {
   1107 
   1108 // A Mutex is a non-reentrant (aka non-recursive) mutex.  At most one thread T
   1109 // may hold a mutex at a given time.  If T attempts to Lock() the same Mutex
   1110 // while holding it, T will deadlock.
   1111 class LIBPROTOBUF_EXPORT Mutex {
   1112  public:
   1113   // Create a Mutex that is not held by anybody.
   1114   Mutex();
   1115 
   1116   // Destructor
   1117   ~Mutex();
   1118 
   1119   // Block if necessary until this Mutex is free, then acquire it exclusively.
   1120   void Lock();
   1121 
   1122   // Release this Mutex.  Caller must hold it exclusively.
   1123   void Unlock();
   1124 
   1125   // Crash if this Mutex is not held exclusively by this thread.
   1126   // May fail to crash when it should; will never crash when it should not.
   1127   void AssertHeld();
   1128 
   1129  private:
   1130   struct Internal;
   1131   Internal* mInternal;
   1132 
   1133   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Mutex);
   1134 };
   1135 
   1136 // MutexLock(mu) acquires mu when constructed and releases it when destroyed.
   1137 class LIBPROTOBUF_EXPORT MutexLock {
   1138  public:
   1139   explicit MutexLock(Mutex *mu) : mu_(mu) { this->mu_->Lock(); }
   1140   ~MutexLock() { this->mu_->Unlock(); }
   1141  private:
   1142   Mutex *const mu_;
   1143   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MutexLock);
   1144 };
   1145 
   1146 // TODO(kenton):  Implement these?  Hard to implement portably.
   1147 typedef MutexLock ReaderMutexLock;
   1148 typedef MutexLock WriterMutexLock;
   1149 
   1150 // MutexLockMaybe is like MutexLock, but is a no-op when mu is NULL.
   1151 class LIBPROTOBUF_EXPORT MutexLockMaybe {
   1152  public:
   1153   explicit MutexLockMaybe(Mutex *mu) :
   1154     mu_(mu) { if (this->mu_ != NULL) { this->mu_->Lock(); } }
   1155   ~MutexLockMaybe() { if (this->mu_ != NULL) { this->mu_->Unlock(); } }
   1156  private:
   1157   Mutex *const mu_;
   1158   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MutexLockMaybe);
   1159 };
   1160 
   1161 }  // namespace internal
   1162 
   1163 // We made these internal so that they would show up as such in the docs,
   1164 // but we don't want to stick "internal::" in front of them everywhere.
   1165 using internal::Mutex;
   1166 using internal::MutexLock;
   1167 using internal::ReaderMutexLock;
   1168 using internal::WriterMutexLock;
   1169 using internal::MutexLockMaybe;
   1170 
   1171 // ===================================================================
   1172 // from google3/util/utf8/public/unilib.h
   1173 
   1174 namespace internal {
   1175 
   1176 // Checks if the buffer contains structurally-valid UTF-8.  Implemented in
   1177 // structurally_valid.cc.
   1178 LIBPROTOBUF_EXPORT bool IsStructurallyValidUTF8(const char* buf, int len);
   1179 
   1180 }  // namespace internal
   1181 
   1182 // ===================================================================
   1183 // from google3/util/endian/endian.h
   1184 LIBPROTOBUF_EXPORT uint32 ghtonl(uint32 x);
   1185 
   1186 // ===================================================================
   1187 // Shutdown support.
   1188 
   1189 // Shut down the entire protocol buffers library, deleting all static-duration
   1190 // objects allocated by the library or by generated .pb.cc files.
   1191 //
   1192 // There are two reasons you might want to call this:
   1193 // * You use a draconian definition of "memory leak" in which you expect
   1194 //   every single malloc() to have a corresponding free(), even for objects
   1195 //   which live until program exit.
   1196 // * You are writing a dynamically-loaded library which needs to clean up
   1197 //   after itself when the library is unloaded.
   1198 //
   1199 // It is safe to call this multiple times.  However, it is not safe to use
   1200 // any other part of the protocol buffers library after
   1201 // ShutdownProtobufLibrary() has been called.
   1202 LIBPROTOBUF_EXPORT void ShutdownProtobufLibrary();
   1203 
   1204 namespace internal {
   1205 
   1206 // Register a function to be called when ShutdownProtocolBuffers() is called.
   1207 LIBPROTOBUF_EXPORT void OnShutdown(void (*func)());
   1208 
   1209 }  // namespace internal
   1210 
   1211 #if PROTOBUF_USE_EXCEPTIONS
   1212 class FatalException : public std::exception {
   1213  public:
   1214   FatalException(const char* filename, int line, const std::string& message)
   1215       : filename_(filename), line_(line), message_(message) {}
   1216   virtual ~FatalException() throw();
   1217 
   1218   virtual const char* what() const throw();
   1219 
   1220   const char* filename() const { return filename_; }
   1221   int line() const { return line_; }
   1222   const std::string& message() const { return message_; }
   1223 
   1224  private:
   1225   const char* filename_;
   1226   const int line_;
   1227   const std::string message_;
   1228 };
   1229 #endif
   1230 
   1231 // This is at the end of the file instead of the beginning to work around a bug
   1232 // in some versions of MSVC.
   1233 using namespace std;  // Don't do this at home, kids.
   1234 
   1235 }  // namespace protobuf
   1236 }  // namespace google
   1237 
   1238 #endif  // GOOGLE_PROTOBUF_COMMON_H__
   1239