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