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.
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
10 #ifndef BASE_MACROS_H_
11 #define BASE_MACROS_H_
13 #include <stddef.h> // For size_t.
15 // Distinguish mips32.
16 #if defined(__mips__) && (_MIPS_SIM == _ABIO32) && !defined(__mips32__)
20 // Distinguish mips64.
21 #if defined(__mips__) && (_MIPS_SIM == _ABI64) && !defined(__mips64__)
25 // Put this in the declarations for a class to be uncopyable.
26 #define DISALLOW_COPY(TypeName) \
27 TypeName(const TypeName&) = delete
29 // Put this in the declarations for a class to be unassignable.
30 #define DISALLOW_ASSIGN(TypeName) TypeName& operator=(const TypeName&) = delete
32 // Put this in the declarations for a class to be uncopyable and unassignable.
33 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
34 DISALLOW_COPY(TypeName); \
35 DISALLOW_ASSIGN(TypeName)
37 // A macro to disallow all the implicit constructors, namely the
38 // default constructor, copy constructor and operator= functions.
39 // This is especially useful for classes containing only static methods.
40 #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
41 TypeName() = delete; \
42 DISALLOW_COPY_AND_ASSIGN(TypeName)
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.
50 // This template function declaration is used in defining arraysize.
51 // Note that the function doesn't need an implementation, as we only
54 // DEPRECATED, please use base::size(array) instead.
55 // TODO(https://crbug.com/837308): Replace existing arraysize usages.
56 template <typename T, size_t N> char (&ArraySizeHelper(T (&array)[N]))[N];
57 #define arraysize(array) (sizeof(ArraySizeHelper(array)))
59 // Used to explicitly mark the return value of a function as unused. If you are
60 // really sure you don't want to do anything with the return value of a function
61 // that has been marked WARN_UNUSED_RESULT, wrap it with this. Example:
63 // std::unique_ptr<MyType> my_var = ...;
64 // if (TakeOwnership(my_var.get()) == SUCCESS)
65 // ignore_result(my_var.release());
68 inline void ignore_result(const T&) {
73 // Use these to declare and define a static local variable (static T;) so that
74 // it is leaked so that its destructors are not called at exit. This is
77 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! DEPRECATED !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
78 // Please don't use this macro. Use a function-local static of type
79 // base::NoDestructor<T> instead:
81 // Factory& Factory::GetInstance() {
82 // static base::NoDestructor<Factory> instance;
85 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
86 #define CR_DEFINE_STATIC_LOCAL(type, name, arguments) \
87 static type& name = *new type arguments
89 // Workaround for MSVC, which expands __VA_ARGS__ as one macro argument. To
90 // work around this bug, wrap the entire expression in this macro...
91 #define CR_EXPAND_ARG(arg) arg
95 #endif // BASE_MACROS_H_