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// Put this in the declarations for a class to be uncopyable. 16#define DISALLOW_COPY(TypeName) \ 17 TypeName(const TypeName&) = delete 18 19// Put this in the declarations for a class to be unassignable. 20#define DISALLOW_ASSIGN(TypeName) \ 21 void operator=(const TypeName&) = delete 22 23// A macro to disallow the copy constructor and operator= functions 24// This should be used in the private: declarations for a class 25#define DISALLOW_COPY_AND_ASSIGN(TypeName) \ 26 TypeName(const TypeName&); \ 27 void operator=(const TypeName&) 28 29// 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) \ 36 TypeName() = delete; \ 37 DISALLOW_COPY_AND_ASSIGN(TypeName) 38 39// 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. 44 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. 48template <typename T, size_t N> char (&ArraySizeHelper(T (&array)[N]))[N]; 49#define arraysize(array) (sizeof(ArraySizeHelper(array))) 50 51// 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// 55// scoped_ptr<MyType> my_var = ...; 56// 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_ 88