[email protected] | a1683a1 | 2014-01-08 21:38:30 | [diff] [blame] | 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 BASE_MACROS_H_ |
| 11 | #define BASE_MACROS_H_ |
| 12 | |
| 13 | #include <stddef.h> // For size_t. |
[email protected] | a1683a1 | 2014-01-08 21:38:30 | [diff] [blame] | 14 | |
Wang Qing | 534d7ce | 2017-09-18 23:35:59 | [diff] [blame] | 15 | // Distinguish mips32. |
Gordana Cmiljanovic | 2d4aa3a4 | 2017-09-22 14:25:18 | [diff] [blame] | 16 | #if defined(__mips__) && (_MIPS_SIM == _ABIO32) && !defined(__mips32__) |
Wang Qing | 534d7ce | 2017-09-18 23:35:59 | [diff] [blame] | 17 | #define __mips32__ |
| 18 | #endif |
| 19 | |
| 20 | // Distinguish mips64. |
Gordana Cmiljanovic | 2d4aa3a4 | 2017-09-22 14:25:18 | [diff] [blame] | 21 | #if defined(__mips__) && (_MIPS_SIM == _ABI64) && !defined(__mips64__) |
Wang Qing | 534d7ce | 2017-09-18 23:35:59 | [diff] [blame] | 22 | #define __mips64__ |
| 23 | #endif |
| 24 | |
mlamouri | a99741d5 | 2015-05-21 22:54:05 | [diff] [blame] | 25 | // Put this in the declarations for a class to be uncopyable. |
[email protected] | a1683a1 | 2014-01-08 21:38:30 | [diff] [blame] | 26 | #define DISALLOW_COPY(TypeName) \ |
mlamouri | a99741d5 | 2015-05-21 22:54:05 | [diff] [blame] | 27 | TypeName(const TypeName&) = delete |
[email protected] | a1683a1 | 2014-01-08 21:38:30 | [diff] [blame] | 28 | |
mlamouri | a99741d5 | 2015-05-21 22:54:05 | [diff] [blame] | 29 | // Put this in the declarations for a class to be unassignable. |
Clemens Hammacher | cec5fde | 2017-07-20 08:53:16 | [diff] [blame] | 30 | #define DISALLOW_ASSIGN(TypeName) TypeName& operator=(const TypeName&) = delete |
[email protected] | a1683a1 | 2014-01-08 21:38:30 | [diff] [blame] | 31 | |
Clemens Hammacher | cec5fde | 2017-07-20 08:53:16 | [diff] [blame] | 32 | // Put this in the declarations for a class to be uncopyable and unassignable. |
pkasting | 999f15f | 2016-05-26 22:03:34 | [diff] [blame] | 33 | #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ |
Clemens Hammacher | cec5fde | 2017-07-20 08:53:16 | [diff] [blame] | 34 | DISALLOW_COPY(TypeName); \ |
| 35 | DISALLOW_ASSIGN(TypeName) |
[email protected] | a1683a1 | 2014-01-08 21:38:30 | [diff] [blame] | 36 | |
[email protected] | a1683a1 | 2014-01-08 21:38:30 | [diff] [blame] | 37 | // A macro to disallow all the implicit constructors, namely the |
| 38 | // default constructor, copy constructor and operator= functions. |
Clemens Hammacher | cec5fde | 2017-07-20 08:53:16 | [diff] [blame] | 39 | // This is especially useful for classes containing only static methods. |
[email protected] | a1683a1 | 2014-01-08 21:38:30 | [diff] [blame] | 40 | #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \ |
mlamouri | 8cd0029 | 2015-07-03 10:27:50 | [diff] [blame] | 41 | TypeName() = delete; \ |
[email protected] | a1683a1 | 2014-01-08 21:38:30 | [diff] [blame] | 42 | DISALLOW_COPY_AND_ASSIGN(TypeName) |
| 43 | |
tnagel | aeb6648 | 2015-12-18 02:28:19 | [diff] [blame] | 44 | // The arraysize(arr) macro returns the # of elements in an array arr. The |
| 45 | // expression is a compile-time constant, and therefore can be used in defining |
| 46 | // new arrays, for example. If you use arraysize on a pointer by mistake, you |
| 47 | // will get a compile-time error. For the technical details, refer to |
| 48 | // http://blogs.msdn.com/b/the1/archive/2004/05/07/128242.aspx. |
[email protected] | a1683a1 | 2014-01-08 21:38:30 | [diff] [blame] | 49 | |
| 50 | // This template function declaration is used in defining arraysize. |
| 51 | // Note that the function doesn't need an implementation, as we only |
| 52 | // use its type. |
tfarina | 2e25234 | 2015-05-08 02:15:20 | [diff] [blame] | 53 | template <typename T, size_t N> char (&ArraySizeHelper(T (&array)[N]))[N]; |
| 54 | #define arraysize(array) (sizeof(ArraySizeHelper(array))) |
[email protected] | a1683a1 | 2014-01-08 21:38:30 | [diff] [blame] | 55 | |
[email protected] | a1683a1 | 2014-01-08 21:38:30 | [diff] [blame] | 56 | // Used to explicitly mark the return value of a function as unused. If you are |
| 57 | // really sure you don't want to do anything with the return value of a function |
| 58 | // that has been marked WARN_UNUSED_RESULT, wrap it with this. Example: |
| 59 | // |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 60 | // std::unique_ptr<MyType> my_var = ...; |
[email protected] | a1683a1 | 2014-01-08 21:38:30 | [diff] [blame] | 61 | // if (TakeOwnership(my_var.get()) == SUCCESS) |
| 62 | // ignore_result(my_var.release()); |
| 63 | // |
| 64 | template<typename T> |
| 65 | inline void ignore_result(const T&) { |
| 66 | } |
| 67 | |
[email protected] | a1683a1 | 2014-01-08 21:38:30 | [diff] [blame] | 68 | namespace base { |
[email protected] | a1683a1 | 2014-01-08 21:38:30 | [diff] [blame] | 69 | |
| 70 | // Use these to declare and define a static local variable (static T;) so that |
Nico Weber | c5bbb48 | 2017-10-14 15:55:31 | [diff] [blame] | 71 | // it is leaked so that its destructors are not called at exit. This is |
| 72 | // thread-safe. |
Daniel Cheng | 73999fe6 | 2018-01-19 00:28:15 | [diff] [blame] | 73 | // |
| 74 | // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! DEPRECATED !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 75 | // Please don't use this macro. Use a function-local static of type |
| 76 | // base::NoDestructor<T> instead: |
| 77 | // |
| 78 | // Factory& Factory::GetInstance() { |
| 79 | // static base::NoDestructor<Factory> instance; |
| 80 | // return *instance; |
| 81 | // } |
| 82 | // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
[email protected] | a1683a1 | 2014-01-08 21:38:30 | [diff] [blame] | 83 | #define CR_DEFINE_STATIC_LOCAL(type, name, arguments) \ |
| 84 | static type& name = *new type arguments |
| 85 | |
Daniel Cheng | cda1df5b | 2018-03-30 21:30:16 | [diff] [blame] | 86 | // Workaround for MSVC, which expands __VA_ARGS__ as one macro argument. To |
| 87 | // work around this bug, wrap the entire expression in this macro... |
| 88 | #define CR_EXPAND_ARG(arg) arg |
| 89 | |
[email protected] | a1683a1 | 2014-01-08 21:38:30 | [diff] [blame] | 90 | } // base |
| 91 | |
| 92 | #endif // BASE_MACROS_H_ |