• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
14 
15 #if defined(ANDROID)
16 // Prefer Android's libbase definitions to our own.
17 #include <android-base/macros.h>
18 #endif  // defined(ANDROID)
19 
20 // We define following macros conditionally as they may be defined by another libraries.
21 
22 // Distinguish mips32.
23 #if defined(__mips__) && (_MIPS_SIM == _ABIO32) && !defined(__mips32__)
24 #define __mips32__
25 #endif
26 
27 // Distinguish mips64.
28 #if defined(__mips__) && (_MIPS_SIM == _ABI64) && !defined(__mips64__)
29 #define __mips64__
30 #endif
31 
32 // Put this in the declarations for a class to be uncopyable.
33 #if !defined(DISALLOW_COPY)
34 #define DISALLOW_COPY(TypeName) \
35   TypeName(const TypeName&) = delete
36 #endif
37 
38 // Put this in the declarations for a class to be unassignable.
39 #if !defined(DISALLOW_ASSIGN)
40 #define DISALLOW_ASSIGN(TypeName) TypeName& operator=(const TypeName&) = delete
41 #endif
42 
43 // Put this in the declarations for a class to be uncopyable and unassignable.
44 #if !defined(DISALLOW_COPY_AND_ASSIGN)
45 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
46   DISALLOW_COPY(TypeName);                 \
47   DISALLOW_ASSIGN(TypeName)
48 #endif
49 
50 // A macro to disallow all the implicit constructors, namely the
51 // default constructor, copy constructor and operator= functions.
52 // This is especially useful for classes containing only static methods.
53 #if !defined(DISALLOW_IMPLICIT_CONSTRUCTORS)
54 #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
55   TypeName() = delete;                           \
56   DISALLOW_COPY_AND_ASSIGN(TypeName)
57 #endif
58 
59 // The arraysize(arr) macro returns the # of elements in an array arr.  The
60 // expression is a compile-time constant, and therefore can be used in defining
61 // new arrays, for example.  If you use arraysize on a pointer by mistake, you
62 // will get a compile-time error.  For the technical details, refer to
63 // http://blogs.msdn.com/b/the1/archive/2004/05/07/128242.aspx.
64 
65 // This template function declaration is used in defining arraysize.
66 // Note that the function doesn't need an implementation, as we only
67 // use its type.
68 //
69 // DEPRECATED, please use base::size(array) instead.
70 // TODO(https://crbug.com/837308): Replace existing arraysize usages.
71 #if !defined(arraysize)
72 template <typename T, size_t N> char (&ArraySizeHelper(T (&array)[N]))[N];
73 #define arraysize(array) (sizeof(ArraySizeHelper(array)))
74 #endif
75 
76 // Used to explicitly mark the return value of a function as unused. If you are
77 // really sure you don't want to do anything with the return value of a function
78 // that has been marked WARN_UNUSED_RESULT, wrap it with this. Example:
79 //
80 //   std::unique_ptr<MyType> my_var = ...;
81 //   if (TakeOwnership(my_var.get()) == SUCCESS)
82 //     ignore_result(my_var.release());
83 //
84 template<typename T>
ignore_result(const T &)85 inline void ignore_result(const T&) {
86 }
87 
88 namespace base {
89 
90 // Use these to declare and define a static local variable (static T;) so that
91 // it is leaked so that its destructors are not called at exit.  This is
92 // thread-safe.
93 //
94 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! DEPRECATED !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
95 // Please don't use this macro. Use a function-local static of type
96 // base::NoDestructor<T> instead:
97 //
98 // Factory& Factory::GetInstance() {
99 //   static base::NoDestructor<Factory> instance;
100 //   return *instance;
101 // }
102 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
103 #if !defined(CR_DEFINE_STATIC_LOCAL)
104 #define CR_DEFINE_STATIC_LOCAL(type, name, arguments) \
105   static type& name = *new type arguments
106 #endif
107 
108 // Workaround for MSVC, which expands __VA_ARGS__ as one macro argument. To
109 // work around this bug, wrap the entire expression in this macro...
110 #define CR_EXPAND_ARG(arg) arg
111 
112 }  // base
113 
114 #endif  // BASE_MACROS_H_
115