/src/brpc/src/butil/macros.h
Line | Count | Source |
1 | | // Copyright 2014 The Chromium Authors. All rights reserved. |
2 | | // Use of this source code is governed by a BSD-style license that can be |
3 | | // found in the LICENSE file. |
4 | | |
5 | | // This file contains macros and macro-like constructs (e.g., templates) that |
6 | | // are commonly used throughout Chromium source. (It may also contain things |
7 | | // that are closely related to things that are commonly used that belong in this |
8 | | // file.) |
9 | | |
10 | | #ifndef BUTIL_MACROS_H_ |
11 | | #define BUTIL_MACROS_H_ |
12 | | |
13 | | #include <stddef.h> // For size_t. |
14 | | #include <string.h> // For memcpy. |
15 | | #include <stdlib.h> |
16 | | |
17 | | #include "butil/compiler_specific.h" // For ALLOW_UNUSED. |
18 | | #include "butil/string_printf.h" // For butil::string_printf(). |
19 | | |
20 | | // There must be many copy-paste versions of these macros which are same |
21 | | // things, undefine them to avoid conflict. |
22 | | #undef DISALLOW_COPY |
23 | | #undef DISALLOW_MOVE |
24 | | #undef DISALLOW_ASSIGN |
25 | | #undef DISALLOW_MOVE_ASSIGN |
26 | | #undef DISALLOW_COPY_AND_ASSIGN |
27 | | #undef DISALLOW_COPY_AND_MOVE |
28 | | #undef DISALLOW_EVIL_CONSTRUCTORS |
29 | | #undef DISALLOW_IMPLICIT_CONSTRUCTORS |
30 | | |
31 | | #if !defined(BUTIL_CXX11_ENABLED) |
32 | | #define BUTIL_DELETE_FUNCTION(decl) decl |
33 | | #else |
34 | | #define BUTIL_DELETE_FUNCTION(decl) decl = delete |
35 | | #endif |
36 | | |
37 | | // Declarations for a class to be uncopyable. |
38 | | #define DISALLOW_COPY(TypeName) \ |
39 | | BUTIL_DELETE_FUNCTION(TypeName(const TypeName&)) |
40 | | |
41 | | // Declarations for a class to be unmovable. |
42 | | #define DISALLOW_MOVE(TypeName) \ |
43 | | BUTIL_DELETE_FUNCTION(TypeName(TypeName&&)) |
44 | | |
45 | | // Declarations for a class to be unassignable. |
46 | | #define DISALLOW_ASSIGN(TypeName) \ |
47 | | BUTIL_DELETE_FUNCTION(TypeName& operator=(const TypeName&)) |
48 | | |
49 | | // Declarations for a class to be move-unassignable. |
50 | | #define DISALLOW_MOVE_ASSIGN(TypeName) \ |
51 | | BUTIL_DELETE_FUNCTION(TypeName& operator=(TypeName&&)) |
52 | | |
53 | | // A macro to disallow the copy constructor and operator= functions. |
54 | | #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ |
55 | | DISALLOW_COPY(TypeName); \ |
56 | | DISALLOW_ASSIGN(TypeName) |
57 | | |
58 | | // A macro to disallow the move constructor and operator= functions. |
59 | | #define DISALLOW_MOVE_AND_ASSIGN(TypeName) \ |
60 | | DISALLOW_MOVE(TypeName); \ |
61 | | DISALLOW_MOVE_ASSIGN(TypeName) |
62 | | |
63 | | // A macro to disallow the copy constructor, |
64 | | // the move constructor and operator= functions. |
65 | | #define DISALLOW_COPY_AND_MOVE(TypeName) \ |
66 | | DISALLOW_COPY_AND_ASSIGN(TypeName); \ |
67 | | DISALLOW_MOVE(TypeName) |
68 | | |
69 | | // An older, deprecated, politically incorrect name for the above. |
70 | | // NOTE: The usage of this macro was banned from our code base, but some |
71 | | // third_party libraries are yet using it. |
72 | | // TODO(tfarina): Figure out how to fix the usage of this macro in the |
73 | | // third_party libraries and get rid of it. |
74 | | #define DISALLOW_EVIL_CONSTRUCTORS(TypeName) DISALLOW_COPY_AND_ASSIGN(TypeName) |
75 | | |
76 | | // A macro to disallow all the implicit constructors, namely the |
77 | | // default constructor, copy constructor and operator= functions. |
78 | | // |
79 | | // This should be used in the private: declarations for a class |
80 | | // that wants to prevent anyone from instantiating it. This is |
81 | | // especially useful for classes containing only static methods. |
82 | | #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \ |
83 | | BUTIL_DELETE_FUNCTION(TypeName()); \ |
84 | | DISALLOW_COPY_AND_ASSIGN(TypeName) |
85 | | |
86 | | // Concatenate numbers in c/c++ macros. |
87 | | #ifndef BAIDU_CONCAT |
88 | 162 | # define BAIDU_CONCAT(a, b) BAIDU_CONCAT_HELPER(a, b) |
89 | 162 | # define BAIDU_CONCAT_HELPER(a, b) a##b |
90 | | #endif |
91 | | |
92 | | #undef arraysize |
93 | | // The arraysize(arr) macro returns the # of elements in an array arr. |
94 | | // The expression is a compile-time constant, and therefore can be |
95 | | // used in defining new arrays, for example. If you use arraysize on |
96 | | // a pointer by mistake, you will get a compile-time error. |
97 | | // |
98 | | // One caveat is that arraysize() doesn't accept any array of an |
99 | | // anonymous type or a type defined inside a function. In these rare |
100 | | // cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below. This is |
101 | | // due to a limitation in C++'s template system. The limitation might |
102 | | // eventually be removed, but it hasn't happened yet. |
103 | | |
104 | | // This template function declaration is used in defining arraysize. |
105 | | // Note that the function doesn't need an implementation, as we only |
106 | | // use its type. |
107 | | namespace butil { |
108 | | template <typename T, size_t N> |
109 | | char (&ArraySizeHelper(T (&array)[N]))[N]; |
110 | | } |
111 | | |
112 | | // That gcc wants both of these prototypes seems mysterious. VC, for |
113 | | // its part, can't decide which to use (another mystery). Matching of |
114 | | // template overloads: the final frontier. |
115 | | #ifndef _MSC_VER |
116 | | namespace butil { |
117 | | template <typename T, size_t N> |
118 | | char (&ArraySizeHelper(const T (&array)[N]))[N]; |
119 | | } |
120 | | #endif |
121 | | |
122 | 2 | #define arraysize(array) (sizeof(::butil::ArraySizeHelper(array))) |
123 | | |
124 | | // gejun: Following macro was used in other modules. |
125 | | #undef ARRAY_SIZE |
126 | 0 | #define ARRAY_SIZE(array) arraysize(array) |
127 | | |
128 | | // ARRAYSIZE_UNSAFE performs essentially the same calculation as arraysize, |
129 | | // but can be used on anonymous types or types defined inside |
130 | | // functions. It's less safe than arraysize as it accepts some |
131 | | // (although not all) pointers. Therefore, you should use arraysize |
132 | | // whenever possible. |
133 | | // |
134 | | // The expression ARRAYSIZE_UNSAFE(a) is a compile-time constant of type |
135 | | // size_t. |
136 | | // |
137 | | // ARRAYSIZE_UNSAFE catches a few type errors. If you see a compiler error |
138 | | // |
139 | | // "warning: division by zero in ..." |
140 | | // |
141 | | // when using ARRAYSIZE_UNSAFE, you are (wrongfully) giving it a pointer. |
142 | | // You should only use ARRAYSIZE_UNSAFE on statically allocated arrays. |
143 | | // |
144 | | // The following comments are on the implementation details, and can |
145 | | // be ignored by the users. |
146 | | // |
147 | | // ARRAYSIZE_UNSAFE(arr) works by inspecting sizeof(arr) (the # of bytes in |
148 | | // the array) and sizeof(*(arr)) (the # of bytes in one array |
149 | | // element). If the former is divisible by the latter, perhaps arr is |
150 | | // indeed an array, in which case the division result is the # of |
151 | | // elements in the array. Otherwise, arr cannot possibly be an array, |
152 | | // and we generate a compiler error to prevent the code from |
153 | | // compiling. |
154 | | // |
155 | | // Since the size of bool is implementation-defined, we need to cast |
156 | | // !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final |
157 | | // result has type size_t. |
158 | | // |
159 | | // This macro is not perfect as it wrongfully accepts certain |
160 | | // pointers, namely where the pointer size is divisible by the pointee |
161 | | // size. Since all our code has to go through a 32-bit compiler, |
162 | | // where a pointer is 4 bytes, this means all pointers to a type whose |
163 | | // size is 3 or greater than 4 will be (righteously) rejected. |
164 | | #undef ARRAYSIZE_UNSAFE |
165 | | #define ARRAYSIZE_UNSAFE(a) \ |
166 | 0 | ((sizeof(a) / sizeof(*(a))) / \ |
167 | 0 | static_cast<size_t>(!(sizeof(a) % sizeof(*(a))))) |
168 | | |
169 | | // Use implicit_cast as a safe version of static_cast or const_cast |
170 | | // for upcasting in the type hierarchy (i.e. casting a pointer to Foo |
171 | | // to a pointer to SuperclassOfFoo or casting a pointer to Foo to |
172 | | // a const pointer to Foo). |
173 | | // When you use implicit_cast, the compiler checks that the cast is safe. |
174 | | // Such explicit implicit_casts are necessary in surprisingly many |
175 | | // situations where C++ demands an exact type match instead of an |
176 | | // argument type convertible to a target type. |
177 | | // |
178 | | // The From type can be inferred, so the preferred syntax for using |
179 | | // implicit_cast is the same as for static_cast etc.: |
180 | | // |
181 | | // implicit_cast<ToType>(expr) |
182 | | // |
183 | | // implicit_cast would have been part of the C++ standard library, |
184 | | // but the proposal was submitted too late. It will probably make |
185 | | // its way into the language in the future. |
186 | | namespace butil { |
187 | | template<typename To, typename From> |
188 | | inline To implicit_cast(From const &f) { |
189 | | return f; |
190 | | } |
191 | | } |
192 | | |
193 | | #if defined(BUTIL_CXX11_ENABLED) |
194 | | |
195 | | // C++11 supports compile-time assertion directly |
196 | 10 | #define BAIDU_CASSERT(expr, msg) static_assert(expr, #msg) |
197 | | |
198 | | #else |
199 | | |
200 | | // Assert constant boolean expressions at compile-time |
201 | | // Params: |
202 | | // expr the constant expression to be checked |
203 | | // msg an error infomation conforming name conventions of C/C++ |
204 | | // variables(alphabets/numbers/underscores, no blanks). For |
205 | | // example "cannot_accept_a_number_bigger_than_128" is valid |
206 | | // while "this number is out-of-range" is illegal. |
207 | | // |
208 | | // when an asssertion like "BAIDU_CASSERT(false, you_should_not_be_here)" |
209 | | // breaks, a compilation error is printed: |
210 | | // |
211 | | // foo.cpp:401: error: enumerator value for `you_should_not_be_here___19' not |
212 | | // integer constant |
213 | | // |
214 | | // You can call BAIDU_CASSERT at global scope, inside a class or a function |
215 | | // |
216 | | // BAIDU_CASSERT(false, you_should_not_be_here); |
217 | | // int main () { ... } |
218 | | // |
219 | | // struct Foo { |
220 | | // BAIDU_CASSERT(1 == 0, Never_equals); |
221 | | // }; |
222 | | // |
223 | | // int bar(...) |
224 | | // { |
225 | | // BAIDU_CASSERT (value < 10, invalid_value); |
226 | | // } |
227 | | // |
228 | | namespace butil { |
229 | | template <bool> struct CAssert { static const int x = 1; }; |
230 | | template <> struct CAssert<false> { static const char * x; }; |
231 | | } |
232 | | |
233 | | #define BAIDU_CASSERT(expr, msg) \ |
234 | | enum { BAIDU_CONCAT(BAIDU_CONCAT(LINE_, __LINE__), __##msg) \ |
235 | | = ::butil::CAssert<!!(expr)>::x }; |
236 | | |
237 | | #endif // BUTIL_CXX11_ENABLED |
238 | | |
239 | | // The impl. of chrome does not work for offsetof(Object, private_filed) |
240 | | #undef COMPILE_ASSERT |
241 | 4 | #define COMPILE_ASSERT(expr, msg) BAIDU_CASSERT(expr, msg) |
242 | | |
243 | | // bit_cast<Dest,Source> is a template function that implements the |
244 | | // equivalent of "*reinterpret_cast<Dest*>(&source)". We need this in |
245 | | // very low-level functions like the protobuf library and fast math |
246 | | // support. |
247 | | // |
248 | | // float f = 3.14159265358979; |
249 | | // int i = bit_cast<int32_t>(f); |
250 | | // // i = 0x40490fdb |
251 | | // |
252 | | // The classical address-casting method is: |
253 | | // |
254 | | // // WRONG |
255 | | // float f = 3.14159265358979; // WRONG |
256 | | // int i = * reinterpret_cast<int*>(&f); // WRONG |
257 | | // |
258 | | // The address-casting method actually produces undefined behavior |
259 | | // according to ISO C++ specification section 3.10 -15 -. Roughly, this |
260 | | // section says: if an object in memory has one type, and a program |
261 | | // accesses it with a different type, then the result is undefined |
262 | | // behavior for most values of "different type". |
263 | | // |
264 | | // This is true for any cast syntax, either *(int*)&f or |
265 | | // *reinterpret_cast<int*>(&f). And it is particularly true for |
266 | | // conversions between integral lvalues and floating-point lvalues. |
267 | | // |
268 | | // The purpose of 3.10 -15- is to allow optimizing compilers to assume |
269 | | // that expressions with different types refer to different memory. gcc |
270 | | // 4.0.1 has an optimizer that takes advantage of this. So a |
271 | | // non-conforming program quietly produces wildly incorrect output. |
272 | | // |
273 | | // The problem is not the use of reinterpret_cast. The problem is type |
274 | | // punning: holding an object in memory of one type and reading its bits |
275 | | // back using a different type. |
276 | | // |
277 | | // The C++ standard is more subtle and complex than this, but that |
278 | | // is the basic idea. |
279 | | // |
280 | | // Anyways ... |
281 | | // |
282 | | // bit_cast<> calls memcpy() which is blessed by the standard, |
283 | | // especially by the example in section 3.9 . Also, of course, |
284 | | // bit_cast<> wraps up the nasty logic in one place. |
285 | | // |
286 | | // Fortunately memcpy() is very fast. In optimized mode, with a |
287 | | // constant size, gcc 2.95.3, gcc 4.0.1, and msvc 7.1 produce inline |
288 | | // code with the minimal amount of data movement. On a 32-bit system, |
289 | | // memcpy(d,s,4) compiles to one load and one store, and memcpy(d,s,8) |
290 | | // compiles to two loads and two stores. |
291 | | // |
292 | | // I tested this code with gcc 2.95.3, gcc 4.0.1, icc 8.1, and msvc 7.1. |
293 | | // |
294 | | // WARNING: if Dest or Source is a non-POD type, the result of the memcpy |
295 | | // is likely to surprise you. |
296 | | namespace butil { |
297 | | template <class Dest, class Source> |
298 | | inline Dest bit_cast(const Source& source) { |
299 | | COMPILE_ASSERT(sizeof(Dest) == sizeof(Source), VerifySizesAreEqual); |
300 | | |
301 | | Dest dest; |
302 | | memcpy(&dest, &source, sizeof(dest)); |
303 | | return dest; |
304 | | } |
305 | | } // namespace butil |
306 | | |
307 | | // Used to explicitly mark the return value of a function as unused. If you are |
308 | | // really sure you don't want to do anything with the return value of a function |
309 | | // that has been marked WARN_UNUSED_RESULT, wrap it with this. Example: |
310 | | // |
311 | | // scoped_ptr<MyType> my_var = ...; |
312 | | // if (TakeOwnership(my_var.get()) == SUCCESS) |
313 | | // ignore_result(my_var.release()); |
314 | | // |
315 | | namespace butil { |
316 | | template<typename T> |
317 | 0 | inline void ignore_result(const T&) { |
318 | 0 | } Unexecuted instantiation: void butil::ignore_result<long>(long const&) Unexecuted instantiation: void butil::ignore_result<int>(int const&) |
319 | | } // namespace butil |
320 | | |
321 | | // The following enum should be used only as a constructor argument to indicate |
322 | | // that the variable has static storage class, and that the constructor should |
323 | | // do nothing to its state. It indicates to the reader that it is legal to |
324 | | // declare a static instance of the class, provided the constructor is given |
325 | | // the butil::LINKER_INITIALIZED argument. Normally, it is unsafe to declare a |
326 | | // static variable that has a constructor or a destructor because invocation |
327 | | // order is undefined. However, IF the type can be initialized by filling with |
328 | | // zeroes (which the loader does for static variables), AND the destructor also |
329 | | // does nothing to the storage, AND there are no virtual methods, then a |
330 | | // constructor declared as |
331 | | // explicit MyClass(butil::LinkerInitialized x) {} |
332 | | // and invoked as |
333 | | // static MyClass my_variable_name(butil::LINKER_INITIALIZED); |
334 | | namespace butil { |
335 | | enum LinkerInitialized { LINKER_INITIALIZED }; |
336 | | |
337 | | // Use these to declare and define a static local variable (static T;) so that |
338 | | // it is leaked so that its destructors are not called at exit. If you need |
339 | | // thread-safe initialization, use butil/lazy_instance.h instead. |
340 | | #undef CR_DEFINE_STATIC_LOCAL |
341 | | #define CR_DEFINE_STATIC_LOCAL(type, name, arguments) \ |
342 | | static type& name = *new type arguments |
343 | | |
344 | | } // namespace butil |
345 | | |
346 | | // Convert symbol to string |
347 | | #ifndef BAIDU_SYMBOLSTR |
348 | 0 | # define BAIDU_SYMBOLSTR(a) BAIDU_SYMBOLSTR_HELPER(a) |
349 | 0 | # define BAIDU_SYMBOLSTR_HELPER(a) #a |
350 | | #endif |
351 | | |
352 | | #ifndef BAIDU_TYPEOF |
353 | | # if defined(BUTIL_CXX11_ENABLED) |
354 | 0 | # define BAIDU_TYPEOF decltype |
355 | | # else |
356 | | # ifdef _MSC_VER |
357 | | # include <boost/typeof/typeof.hpp> |
358 | | # define BAIDU_TYPEOF BOOST_TYPEOF |
359 | | # else |
360 | | # define BAIDU_TYPEOF typeof |
361 | | # endif |
362 | | # endif // BUTIL_CXX11_ENABLED |
363 | | #endif // BAIDU_TYPEOF |
364 | | |
365 | | // ptr: the pointer to the member. |
366 | | // type: the type of the container struct this is embedded in. |
367 | | // member: the name of the member within the struct. |
368 | | #ifndef container_of |
369 | 0 | # define container_of(ptr, type, member) ({ \ |
370 | 0 | const BAIDU_TYPEOF( ((type *)0)->member ) *__mptr = (ptr); \ |
371 | 0 | (type *)( (char *)__mptr - offsetof(type,member) );}) |
372 | | #endif |
373 | | |
374 | | // DEFINE_SMALL_ARRAY(MyType, my_array, size, 64); |
375 | | // my_array is typed `MyType*' and as long as `size'. If `size' is not |
376 | | // greater than 64, the array is allocated on stack. |
377 | | // |
378 | | // NOTE: NEVER use ARRAY_SIZE(my_array) which is always 1. |
379 | | |
380 | | #if defined(__cplusplus) |
381 | | namespace butil { |
382 | | namespace internal { |
383 | | template <typename T> struct ArrayDeleter { |
384 | 0 | ArrayDeleter() : arr(0) {} |
385 | 0 | ~ArrayDeleter() { delete[] arr; } |
386 | | T* arr; |
387 | | }; |
388 | | }} |
389 | | |
390 | | // Many versions of clang does not support variable-length array with non-pod |
391 | | // types, have to implement the macro differently. |
392 | | #if !defined(__clang__) |
393 | | # define DEFINE_SMALL_ARRAY(Tp, name, size, maxsize) \ |
394 | | Tp* name = 0; \ |
395 | | const unsigned name##_size = (size); \ |
396 | | const unsigned name##_stack_array_size = (name##_size <= (maxsize) ? name##_size : 0); \ |
397 | | Tp name##_stack_array[name##_stack_array_size]; \ |
398 | | ::butil::internal::ArrayDeleter<Tp> name##_array_deleter; \ |
399 | | if (name##_stack_array_size) { \ |
400 | | name = name##_stack_array; \ |
401 | | } else { \ |
402 | | name = new (::std::nothrow) Tp[name##_size]; \ |
403 | | name##_array_deleter.arr = name; \ |
404 | | } |
405 | | #else |
406 | | // This implementation works for GCC as well, however it needs extra 16 bytes |
407 | | // for ArrayCtorDtor. |
408 | | namespace butil { |
409 | | namespace internal { |
410 | | template <typename T> struct ArrayCtorDtor { |
411 | 0 | ArrayCtorDtor(void* arr, unsigned size) : _arr((T*)arr), _size(size) { |
412 | 0 | for (unsigned i = 0; i < size; ++i) { new (_arr + i) T; } |
413 | 0 | } Unexecuted instantiation: butil::internal::ArrayCtorDtor<unsigned int>::ArrayCtorDtor(void*, unsigned int) Unexecuted instantiation: butil::internal::ArrayCtorDtor<int>::ArrayCtorDtor(void*, unsigned int) Unexecuted instantiation: butil::internal::ArrayCtorDtor<butil::IOBuf*>::ArrayCtorDtor(void*, unsigned int) Unexecuted instantiation: butil::internal::ArrayCtorDtor<brpc::SubCall>::ArrayCtorDtor(void*, unsigned int) Unexecuted instantiation: butil::internal::ArrayCtorDtor<char>::ArrayCtorDtor(void*, unsigned int) |
414 | 0 | ~ArrayCtorDtor() { |
415 | 0 | for (unsigned i = 0; i < _size; ++i) { _arr[i].~T(); } |
416 | 0 | } Unexecuted instantiation: butil::internal::ArrayCtorDtor<unsigned int>::~ArrayCtorDtor() Unexecuted instantiation: butil::internal::ArrayCtorDtor<int>::~ArrayCtorDtor() Unexecuted instantiation: butil::internal::ArrayCtorDtor<butil::IOBuf*>::~ArrayCtorDtor() Unexecuted instantiation: butil::internal::ArrayCtorDtor<brpc::SubCall>::~ArrayCtorDtor() Unexecuted instantiation: butil::internal::ArrayCtorDtor<char>::~ArrayCtorDtor() |
417 | | private: |
418 | | T* _arr; |
419 | | unsigned _size; |
420 | | }; |
421 | | }} |
422 | | # define DEFINE_SMALL_ARRAY(Tp, name, size, maxsize) \ |
423 | 0 | Tp* name = 0; \ |
424 | 0 | const unsigned name##_size = (size); \ |
425 | 0 | const unsigned name##_stack_array_size = (name##_size <= (maxsize) ? name##_size : 0); \ |
426 | 0 | char name##_stack_array[sizeof(Tp) * name##_stack_array_size]; \ |
427 | 0 | ::butil::internal::ArrayDeleter<char> name##_array_deleter; \ |
428 | 0 | if (name##_stack_array_size) { \ |
429 | 0 | name = (Tp*)name##_stack_array; \ |
430 | 0 | } else { \ |
431 | 0 | name = (Tp*)new (::std::nothrow) char[sizeof(Tp) * name##_size];\ |
432 | 0 | name##_array_deleter.arr = (char*)name; \ |
433 | 0 | } \ |
434 | 0 | const ::butil::internal::ArrayCtorDtor<Tp> name##_array_ctor_dtor(name, name##_size); |
435 | | #endif // !defined(__clang__) |
436 | | #endif // defined(__cplusplus) |
437 | | |
438 | | // Put following code somewhere global to run it before main(): |
439 | | // |
440 | | // BAIDU_GLOBAL_INIT() |
441 | | // { |
442 | | // ... your code ... |
443 | | // } |
444 | | // |
445 | | // Your can: |
446 | | // * Write any code and access global variables. |
447 | | // * Use ASSERT_*. |
448 | | // * Have multiple BAIDU_GLOBAL_INIT() in one scope. |
449 | | // |
450 | | // Since the code run in global scope, quit with exit() or similar functions. |
451 | | |
452 | | #if defined(__cplusplus) |
453 | | # define BAIDU_GLOBAL_INIT \ |
454 | | namespace { /*anonymous namespace */ \ |
455 | | struct BAIDU_CONCAT(BaiduGlobalInit, __LINE__) { \ |
456 | | BAIDU_CONCAT(BaiduGlobalInit, __LINE__)() { init(); } \ |
457 | | void init(); \ |
458 | | } BAIDU_CONCAT(baidu_global_init_dummy_, __LINE__); \ |
459 | | } /* anonymous namespace */ \ |
460 | | void BAIDU_CONCAT(BaiduGlobalInit, __LINE__)::init |
461 | | #else |
462 | | # define BAIDU_GLOBAL_INIT \ |
463 | | static void __attribute__((constructor)) \ |
464 | | BAIDU_CONCAT(baidu_global_init_, __LINE__) |
465 | | |
466 | | #endif // __cplusplus |
467 | | |
468 | | #define ASSERT_LOG(fmt, ...) \ |
469 | 0 | do { \ |
470 | 0 | std::string log = butil::string_printf(fmt, ## __VA_ARGS__); \ |
471 | 0 | LOG(FATAL) << log; \ |
472 | 0 | } while (false) |
473 | | |
474 | | // Assert macro that can crash the process to generate a dump. |
475 | | #define RELEASE_ASSERT(condition) \ |
476 | 5 | do { \ |
477 | 5 | if (!(condition)) { \ |
478 | 0 | ::abort(); \ |
479 | 0 | } \ |
480 | 5 | } while (false) |
481 | | |
482 | | // Assert macro that can crash the process to generate a dump and |
483 | | // supply a verbose explanation of what went wrong. |
484 | | // For example: |
485 | | // std::vector<int> v; |
486 | | // ... |
487 | | // RELEASE_ASSERT_VERBOSE(v.empty(), "v should be empty, but with size=%zu", v.size()); |
488 | | #define RELEASE_ASSERT_VERBOSE(condition, fmt, ...) \ |
489 | 0 | do { \ |
490 | 0 | if (!(condition)) { \ |
491 | 0 | ASSERT_LOG("Assert failure: " #condition ". " #fmt, ## __VA_ARGS__); \ |
492 | 0 | ::abort(); \ |
493 | 0 | } \ |
494 | 0 | } while (false) |
495 | | |
496 | | #endif // BUTIL_MACROS_H_ |