blob: 91daf5b6d2ecd3a0408f7da8d351ec88153b7857 [file] [log] [blame]
[email protected]a1683a12014-01-08 21:38:301// 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]a1683a12014-01-08 21:38:3014
Wang Qing534d7ce2017-09-18 23:35:5915// Distinguish mips32.
Gordana Cmiljanovic2d4aa3a42017-09-22 14:25:1816#if defined(__mips__) && (_MIPS_SIM == _ABIO32) && !defined(__mips32__)
Wang Qing534d7ce2017-09-18 23:35:5917#define __mips32__
18#endif
19
20// Distinguish mips64.
Gordana Cmiljanovic2d4aa3a42017-09-22 14:25:1821#if defined(__mips__) && (_MIPS_SIM == _ABI64) && !defined(__mips64__)
Wang Qing534d7ce2017-09-18 23:35:5922#define __mips64__
23#endif
24
mlamouria99741d52015-05-21 22:54:0525// Put this in the declarations for a class to be uncopyable.
[email protected]a1683a12014-01-08 21:38:3026#define DISALLOW_COPY(TypeName) \
mlamouria99741d52015-05-21 22:54:0527 TypeName(const TypeName&) = delete
[email protected]a1683a12014-01-08 21:38:3028
mlamouria99741d52015-05-21 22:54:0529// Put this in the declarations for a class to be unassignable.
Clemens Hammachercec5fde2017-07-20 08:53:1630#define DISALLOW_ASSIGN(TypeName) TypeName& operator=(const TypeName&) = delete
[email protected]a1683a12014-01-08 21:38:3031
Clemens Hammachercec5fde2017-07-20 08:53:1632// Put this in the declarations for a class to be uncopyable and unassignable.
pkasting999f15f2016-05-26 22:03:3433#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
Clemens Hammachercec5fde2017-07-20 08:53:1634 DISALLOW_COPY(TypeName); \
35 DISALLOW_ASSIGN(TypeName)
[email protected]a1683a12014-01-08 21:38:3036
[email protected]a1683a12014-01-08 21:38:3037// A macro to disallow all the implicit constructors, namely the
38// default constructor, copy constructor and operator= functions.
Clemens Hammachercec5fde2017-07-20 08:53:1639// This is especially useful for classes containing only static methods.
[email protected]a1683a12014-01-08 21:38:3040#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
mlamouri8cd00292015-07-03 10:27:5041 TypeName() = delete; \
[email protected]a1683a12014-01-08 21:38:3042 DISALLOW_COPY_AND_ASSIGN(TypeName)
43
tnagelaeb66482015-12-18 02:28:1944// 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]a1683a12014-01-08 21:38:3049
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.
tfarina2e252342015-05-08 02:15:2053template <typename T, size_t N> char (&ArraySizeHelper(T (&array)[N]))[N];
54#define arraysize(array) (sizeof(ArraySizeHelper(array)))
[email protected]a1683a12014-01-08 21:38:3055
[email protected]a1683a12014-01-08 21:38:3056// 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//
dcheng093de9b2016-04-04 21:25:5160// std::unique_ptr<MyType> my_var = ...;
[email protected]a1683a12014-01-08 21:38:3061// if (TakeOwnership(my_var.get()) == SUCCESS)
62// ignore_result(my_var.release());
63//
64template<typename T>
65inline void ignore_result(const T&) {
66}
67
[email protected]a1683a12014-01-08 21:38:3068namespace base {
[email protected]a1683a12014-01-08 21:38:3069
70// Use these to declare and define a static local variable (static T;) so that
Nico Weberc5bbb482017-10-14 15:55:3171// it is leaked so that its destructors are not called at exit. This is
72// thread-safe.
Daniel Cheng73999fe62018-01-19 00:28:1573//
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]a1683a12014-01-08 21:38:3083#define CR_DEFINE_STATIC_LOCAL(type, name, arguments) \
84 static type& name = *new type arguments
85
Daniel Chengcda1df5b2018-03-30 21:30:1686// 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]a1683a12014-01-08 21:38:3090} // base
91
92#endif // BASE_MACROS_H_