blob: 60c5cbd8cb7f7ca2ece2087704aa81a4a4945ea2 [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
mlamouria99741d52015-05-21 22:54:0515// Put this in the declarations for a class to be uncopyable.
[email protected]a1683a12014-01-08 21:38:3016#define DISALLOW_COPY(TypeName) \
mlamouria99741d52015-05-21 22:54:0517 TypeName(const TypeName&) = delete
[email protected]a1683a12014-01-08 21:38:3018
mlamouria99741d52015-05-21 22:54:0519// Put this in the declarations for a class to be unassignable.
[email protected]a1683a12014-01-08 21:38:3020#define DISALLOW_ASSIGN(TypeName) \
mlamouria99741d52015-05-21 22:54:0521 void operator=(const TypeName&) = delete
[email protected]a1683a12014-01-08 21:38:3022
pkasting999f15f2016-05-26 22:03:3423// A macro to disallow the copy constructor and operator= functions.
24// This should be used in the private: declarations for a class.
pkasting999f15f2016-05-26 22:03:3425#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
26 TypeName(const TypeName&) = delete; \
27 void operator=(const TypeName&) = delete
[email protected]a1683a12014-01-08 21:38:3028
[email protected]a1683a12014-01-08 21:38:3029// A macro to disallow all the implicit constructors, namely the
30// default constructor, copy constructor and operator= functions.
31//
32// This should be used in the private: declarations for a class
33// that wants to prevent anyone from instantiating it. This is
34// especially useful for classes containing only static methods.
35#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
mlamouri8cd00292015-07-03 10:27:5036 TypeName() = delete; \
[email protected]a1683a12014-01-08 21:38:3037 DISALLOW_COPY_AND_ASSIGN(TypeName)
38
tnagelaeb66482015-12-18 02:28:1939// The arraysize(arr) macro returns the # of elements in an array arr. The
40// expression is a compile-time constant, and therefore can be used in defining
41// new arrays, for example. If you use arraysize on a pointer by mistake, you
42// will get a compile-time error. For the technical details, refer to
43// http://blogs.msdn.com/b/the1/archive/2004/05/07/128242.aspx.
[email protected]a1683a12014-01-08 21:38:3044
45// This template function declaration is used in defining arraysize.
46// Note that the function doesn't need an implementation, as we only
47// use its type.
tfarina2e252342015-05-08 02:15:2048template <typename T, size_t N> char (&ArraySizeHelper(T (&array)[N]))[N];
49#define arraysize(array) (sizeof(ArraySizeHelper(array)))
[email protected]a1683a12014-01-08 21:38:3050
[email protected]a1683a12014-01-08 21:38:3051// Used to explicitly mark the return value of a function as unused. If you are
52// really sure you don't want to do anything with the return value of a function
53// that has been marked WARN_UNUSED_RESULT, wrap it with this. Example:
54//
dcheng093de9b2016-04-04 21:25:5155// std::unique_ptr<MyType> my_var = ...;
[email protected]a1683a12014-01-08 21:38:3056// if (TakeOwnership(my_var.get()) == SUCCESS)
57// ignore_result(my_var.release());
58//
59template<typename T>
60inline void ignore_result(const T&) {
61}
62
63// The following enum should be used only as a constructor argument to indicate
64// that the variable has static storage class, and that the constructor should
65// do nothing to its state. It indicates to the reader that it is legal to
66// declare a static instance of the class, provided the constructor is given
67// the base::LINKER_INITIALIZED argument. Normally, it is unsafe to declare a
68// static variable that has a constructor or a destructor because invocation
69// order is undefined. However, IF the type can be initialized by filling with
70// zeroes (which the loader does for static variables), AND the destructor also
71// does nothing to the storage, AND there are no virtual methods, then a
72// constructor declared as
73// explicit MyClass(base::LinkerInitialized x) {}
74// and invoked as
75// static MyClass my_variable_name(base::LINKER_INITIALIZED);
76namespace base {
77enum LinkerInitialized { LINKER_INITIALIZED };
78
79// Use these to declare and define a static local variable (static T;) so that
80// it is leaked so that its destructors are not called at exit. If you need
81// thread-safe initialization, use base/lazy_instance.h instead.
82#define CR_DEFINE_STATIC_LOCAL(type, name, arguments) \
83 static type& name = *new type arguments
84
85} // base
86
87#endif // BASE_MACROS_H_