1
// Protocol Buffers - Google's data interchange format
2
// Copyright 2008 Google Inc. All rights reserved.
3
// http://code.google.com/p/protobuf/
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions are
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
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.
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.
31
// Author: kenton@google.com (Kenton Varda) and others
33
// Contains basic types and utilities used by the rest of the library.
35
#ifndef GOOGLE_PROTOBUF_COMMON_H__
36
#define GOOGLE_PROTOBUF_COMMON_H__
44
// Tru64 lacks stdint.h, but has inttypes.h which defines a superset of
45
// what stdint.h would define.
47
#elif !defined(_MSC_VER)
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
57
#define PROTOBUF_USE_EXCEPTIONS 0
61
#if PROTOBUF_USE_EXCEPTIONS
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);
75
inline BOOL GetMessage(
76
LPMSG lpMsg, HWND hWnd,
77
UINT wMsgFilterMin, UINT wMsgFilterMax) {
78
return GetMessage_Win32(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax);
88
#undef GOOGLE_DISALLOW_EVIL_CONSTRUCTORS
89
#define GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeName) \
90
TypeName(const TypeName&); \
91
void operator=(const TypeName&)
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)
97
#ifdef LIBPROTOBUF_EXPORTS
98
#define LIBPROTOBUF_EXPORT __declspec(dllexport)
100
#define LIBPROTOBUF_EXPORT __declspec(dllimport)
102
#ifdef LIBPROTOC_EXPORTS
103
#define LIBPROTOC_EXPORT __declspec(dllexport)
105
#define LIBPROTOC_EXPORT __declspec(dllimport)
107
#else // defined(_MSC_VER)
108
#ifdef LIBPROTOBUF_EXPORTS
109
#define LIBPROTOBUF_EXPORT __attribute__((visibility("default")))
111
#define LIBPROTOBUF_EXPORT
113
#ifdef LIBPROTOC_EXPORTS
114
#define LIBPROTOC_EXPORT __attribute__((visibility("default")))
116
#define LIBPROTOC_EXPORT
119
#else // defined(COMPONENT_BUILD) && defined(PROTOBUF_USE_DLLS)
120
#define LIBPROTOBUF_EXPORT
121
#define LIBPROTOC_EXPORT
126
// Some of these constants are macros rather than const ints so that they can
127
// be used in #if directives.
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
133
// The minimum library version which works with the current version of the
135
#define GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION 2005000
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
140
static const int kMinHeaderVersionForLibrary = 2005000;
142
// The minimum protoc version which works with the current version of the
144
#define GOOGLE_PROTOBUF_MIN_PROTOC_VERSION 2005000
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;
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);
155
// Converts a numeric version number to a string.
156
std::string LIBPROTOBUF_EXPORT VersionString(int version);
158
} // namespace internal
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, \
169
// ===================================================================
170
// from google3/base/port.h
172
typedef unsigned int uint;
176
typedef __int16 int16;
177
typedef __int32 int32;
178
typedef __int64 int64;
180
typedef unsigned __int8 uint8;
181
typedef unsigned __int16 uint16;
182
typedef unsigned __int32 uint32;
183
typedef unsigned __int64 uint64;
186
typedef int16_t int16;
187
typedef int32_t int32;
188
typedef int64_t int64;
190
typedef uint8_t uint8;
191
typedef uint16_t uint16;
192
typedef uint32_t uint32;
193
typedef uint64_t uint64;
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
203
#define GOOGLE_LONGLONG(x) x##I64
204
#define GOOGLE_ULONGLONG(x) x##UI64
205
#define GOOGLE_LL_FORMAT "I64" // As in printf("%I64d", ...)
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.
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);
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.
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))
231
// Other compilers will have to figure it out for themselves.
232
#define GOOGLE_ATTRIBUTE_ALWAYS_INLINE
236
#ifndef GOOGLE_ATTRIBUTE_DEPRECATED
238
// If the method/variable/type is used anywhere, produce a warning.
239
#define GOOGLE_ATTRIBUTE_DEPRECATED __attribute__((deprecated))
241
#define GOOGLE_ATTRIBUTE_DEPRECATED
245
#ifndef GOOGLE_PREDICT_TRUE
247
// Provided at least since GCC 3.0.
248
#define GOOGLE_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
250
#define GOOGLE_PREDICT_TRUE
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()
260
#ifndef GOOGLE_SAFE_CONCURRENT_WRITES_END
261
#define GOOGLE_SAFE_CONCURRENT_WRITES_END()
264
// ===================================================================
265
// from google3/base/basictypes.h
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.
271
// GOOGLE_ARRAYSIZE catches a few type errors. If you see a compiler error
273
// "warning: division by zero in ..."
275
// when using GOOGLE_ARRAYSIZE, you are (wrongfully) giving it a pointer.
276
// You should only use GOOGLE_ARRAYSIZE on statically allocated arrays.
278
// The following comments are on the implementation details, and can
279
// be ignored by the users.
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
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.
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.
299
// Kudos to Jorg Brown for this simple and elegant implementation.
301
#undef GOOGLE_ARRAYSIZE
302
#define GOOGLE_ARRAYSIZE(a) \
303
((sizeof(a) / sizeof(*(a))) / \
304
static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
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.
317
// The From type can be inferred, so the preferred syntax for using
318
// implicit_cast is the same as for static_cast etc.:
320
// implicit_cast<ToType>(expr)
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) {
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.
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
355
implicit_cast<From*, To>(0);
358
#if !defined(NDEBUG) && !defined(GOOGLE_PROTOBUF_NO_RTTI)
359
assert(f == NULL || dynamic_cast<To>(f) != NULL); // RTTI: debug mode only!
361
return static_cast<To>(f);
364
} // namespace internal
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;
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:
375
// COMPILE_ASSERT(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES,
376
// content_type_names_incorrect_size);
378
// or to make sure a struct is smaller than a certain size:
380
// COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
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.
389
struct CompileAssert {
392
} // namespace internal
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]
400
// Implementation details of COMPILE_ASSERT:
402
// - COMPILE_ASSERT works by defining an array type that has -1
403
// elements (and thus is invalid) when the expression is false.
405
// - The simpler definition
407
// #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1]
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:
415
// COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is
416
// // not a compile-time constant.
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.)
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
425
// CompileAssert<bool(expr)>
427
// instead, these compilers will refuse to compile
429
// COMPILE_ASSERT(5 > 0, some_message);
431
// (They seem to think the ">" in "5 > 0" marks the end of the
432
// template argument list.)
434
// - The array size is (bool(expr) ? 1 : -1), instead of simply
436
// ((expr) ? 1 : -1).
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.
441
// ===================================================================
442
// from google3/base/memory/scoped_ptr.h
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.
450
template <class C> class scoped_ptr;
451
template <class C> class scoped_array;
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.
458
// The size of a scoped_ptr is small:
459
// sizeof(scoped_ptr<C>) == sizeof(C*)
465
typedef C element_type;
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) { }
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.
475
enum { type_must_be_complete = sizeof(C) };
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) {
484
enum { type_must_be_complete = sizeof(C) };
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);
496
C* operator->() const {
497
assert(ptr_ != NULL);
500
C* get() const { return ptr_; }
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; }
508
// Swap two scoped pointers.
509
void swap(scoped_ptr& p2) {
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.
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;
535
// Disallow evil constructors
536
scoped_ptr(const scoped_ptr&);
537
void operator=(const scoped_ptr&);
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 [].
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.
546
// Size: sizeof(scoped_array<C>) == sizeof(C*)
552
typedef C element_type;
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) { }
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.
562
enum { type_must_be_complete = sizeof(C) };
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) {
571
enum { type_must_be_complete = sizeof(C) };
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 {
581
assert(array_ != NULL);
585
// Get a pointer to the zeroth element of the current object.
586
// If there is no current object, return NULL.
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; }
597
// Swap two scoped arrays.
598
void swap(scoped_array& p2) {
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.
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;
622
// Disallow evil constructors
623
scoped_array(const scoped_array&);
624
void operator=(const scoped_array&);
627
} // namespace internal
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;
634
// ===================================================================
635
// emulates google3/base/logging.h
638
LOGLEVEL_INFO, // Informational. This is never actually used by
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
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.
652
LOGLEVEL_DFATAL = LOGLEVEL_ERROR
654
LOGLEVEL_DFATAL = LOGLEVEL_FATAL
662
class LIBPROTOBUF_EXPORT LogMessage {
664
LogMessage(LogLevel level, const char* filename, int line);
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);
677
friend class LogFinisher;
681
const char* filename_;
683
std::string message_;
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 {
690
void operator=(LogMessage& other);
693
} // namespace internal
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.
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
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
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)
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))
738
T* CheckNotNull(const char *file, int line, const char *name, T* val) {
740
GOOGLE_LOG(FATAL) << name;
744
} // namespace internal
745
#define GOOGLE_CHECK_NOTNULL(A) \
746
internal::CheckNotNull(__FILE__, __LINE__, "'" #A "' must not be NULL", (A))
750
#define GOOGLE_DLOG GOOGLE_LOG_IF(INFO, false)
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))
762
#define GOOGLE_DLOG GOOGLE_LOG
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
774
typedef void LogHandler(LogLevel level, const char* filename, int line,
775
const std::string& message);
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).
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
790
LIBPROTOBUF_EXPORT LogHandler* SetLogHandler(LogHandler* new_func);
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 {
805
// ===================================================================
806
// emulates google3/base/callback.h
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
812
// To automatically construct a Closure which calls a particular function or
813
// method with a particular set of parameters, use the NewCallback() function.
815
// void FooDone(const FooResponse* response) {
821
// // When done, call FooDone() and pass it a pointer to the response.
822
// Closure* callback = NewCallback(&FooDone, response);
824
// service->Foo(controller, request, response, callback);
827
// Example that calls a method:
832
// void FooDone(const FooResponse* response) {
838
// // When done, call FooDone() and pass it a pointer to the response.
839
// Closure* callback = NewCallback(this, &Handler::FooDone, response);
841
// service->Foo(controller, request, response, callback);
845
// Currently NewCallback() supports binding zero, one, or two arguments.
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).
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);
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 {
870
virtual void Run() = 0;
873
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Closure);
878
class LIBPROTOBUF_EXPORT FunctionClosure0 : public Closure {
880
typedef void (*FunctionType)();
882
FunctionClosure0(FunctionType function, bool self_deleting)
883
: function_(function), self_deleting_(self_deleting) {}
887
bool needs_delete = self_deleting_; // read in case callback deletes
889
if (needs_delete) delete this;
893
FunctionType function_;
897
template <typename Class>
898
class MethodClosure0 : public Closure {
900
typedef void (Class::*MethodType)();
902
MethodClosure0(Class* object, MethodType method, bool self_deleting)
903
: object_(object), method_(method), self_deleting_(self_deleting) {}
907
bool needs_delete = self_deleting_; // read in case callback deletes
908
(object_->*method_)();
909
if (needs_delete) delete this;
918
template <typename Arg1>
919
class FunctionClosure1 : public Closure {
921
typedef void (*FunctionType)(Arg1 arg1);
923
FunctionClosure1(FunctionType function, bool self_deleting,
925
: function_(function), self_deleting_(self_deleting),
927
~FunctionClosure1() {}
930
bool needs_delete = self_deleting_; // read in case callback deletes
932
if (needs_delete) delete this;
936
FunctionType function_;
941
template <typename Class, typename Arg1>
942
class MethodClosure1 : public Closure {
944
typedef void (Class::*MethodType)(Arg1 arg1);
946
MethodClosure1(Class* object, MethodType method, bool self_deleting,
948
: object_(object), method_(method), self_deleting_(self_deleting),
953
bool needs_delete = self_deleting_; // read in case callback deletes
954
(object_->*method_)(arg1_);
955
if (needs_delete) delete this;
965
template <typename Arg1, typename Arg2>
966
class FunctionClosure2 : public Closure {
968
typedef void (*FunctionType)(Arg1 arg1, Arg2 arg2);
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() {}
977
bool needs_delete = self_deleting_; // read in case callback deletes
978
function_(arg1_, arg2_);
979
if (needs_delete) delete this;
983
FunctionType function_;
989
template <typename Class, typename Arg1, typename Arg2>
990
class MethodClosure2 : public Closure {
992
typedef void (Class::*MethodType)(Arg1 arg1, Arg2 arg2);
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) {}
1001
bool needs_delete = self_deleting_; // read in case callback deletes
1002
(object_->*method_)(arg1_, arg2_);
1003
if (needs_delete) delete this;
1009
bool self_deleting_;
1014
} // namespace internal
1017
inline Closure* NewCallback(void (*function)()) {
1018
return new internal::FunctionClosure0(function, true);
1022
inline Closure* NewPermanentCallback(void (*function)()) {
1023
return new internal::FunctionClosure0(function, false);
1027
template <typename Class>
1028
inline Closure* NewCallback(Class* object, void (Class::*method)()) {
1029
return new internal::MethodClosure0<Class>(object, method, true);
1033
template <typename Class>
1034
inline Closure* NewPermanentCallback(Class* object, void (Class::*method)()) {
1035
return new internal::MethodClosure0<Class>(object, method, false);
1039
template <typename Arg1>
1040
inline Closure* NewCallback(void (*function)(Arg1),
1042
return new internal::FunctionClosure1<Arg1>(function, true, arg1);
1046
template <typename Arg1>
1047
inline Closure* NewPermanentCallback(void (*function)(Arg1),
1049
return new internal::FunctionClosure1<Arg1>(function, false, arg1);
1053
template <typename Class, typename Arg1>
1054
inline Closure* NewCallback(Class* object, void (Class::*method)(Arg1),
1056
return new internal::MethodClosure1<Class, Arg1>(object, method, true, arg1);
1060
template <typename Class, typename Arg1>
1061
inline Closure* NewPermanentCallback(Class* object, void (Class::*method)(Arg1),
1063
return new internal::MethodClosure1<Class, Arg1>(object, method, false, arg1);
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);
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);
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);
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);
1099
// A function which does nothing. Useful for creating no-op callbacks, e.g.:
1100
// Closure* nothing = NewCallback(&DoNothing);
1101
void LIBPROTOBUF_EXPORT DoNothing();
1103
// ===================================================================
1104
// emulates google3/base/mutex.h
1106
namespace internal {
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 {
1113
// Create a Mutex that is not held by anybody.
1119
// Block if necessary until this Mutex is free, then acquire it exclusively.
1122
// Release this Mutex. Caller must hold it exclusively.
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.
1131
Internal* mInternal;
1133
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Mutex);
1136
// MutexLock(mu) acquires mu when constructed and releases it when destroyed.
1137
class LIBPROTOBUF_EXPORT MutexLock {
1139
explicit MutexLock(Mutex *mu) : mu_(mu) { this->mu_->Lock(); }
1140
~MutexLock() { this->mu_->Unlock(); }
1143
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MutexLock);
1146
// TODO(kenton): Implement these? Hard to implement portably.
1147
typedef MutexLock ReaderMutexLock;
1148
typedef MutexLock WriterMutexLock;
1150
// MutexLockMaybe is like MutexLock, but is a no-op when mu is NULL.
1151
class LIBPROTOBUF_EXPORT MutexLockMaybe {
1153
explicit MutexLockMaybe(Mutex *mu) :
1154
mu_(mu) { if (this->mu_ != NULL) { this->mu_->Lock(); } }
1155
~MutexLockMaybe() { if (this->mu_ != NULL) { this->mu_->Unlock(); } }
1158
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MutexLockMaybe);
1161
} // namespace internal
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;
1171
// ===================================================================
1172
// from google3/util/utf8/public/unilib.h
1174
namespace internal {
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);
1180
} // namespace internal
1182
// ===================================================================
1183
// from google3/util/endian/endian.h
1184
LIBPROTOBUF_EXPORT uint32 ghtonl(uint32 x);
1186
// ===================================================================
1187
// Shutdown support.
1189
// Shut down the entire protocol buffers library, deleting all static-duration
1190
// objects allocated by the library or by generated .pb.cc files.
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.
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();
1204
namespace internal {
1206
// Register a function to be called when ShutdownProtocolBuffers() is called.
1207
LIBPROTOBUF_EXPORT void OnShutdown(void (*func)());
1209
} // namespace internal
1211
#if PROTOBUF_USE_EXCEPTIONS
1212
class FatalException : public std::exception {
1214
FatalException(const char* filename, int line, const std::string& message)
1215
: filename_(filename), line_(line), message_(message) {}
1216
virtual ~FatalException() throw();
1218
virtual const char* what() const throw();
1220
const char* filename() const { return filename_; }
1221
int line() const { return line_; }
1222
const std::string& message() const { return message_; }
1225
const char* filename_;
1227
const std::string message_;
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.
1235
} // namespace protobuf
1236
} // namespace google
1238
#endif // GOOGLE_PROTOBUF_COMMON_H__