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 #if defined(ANDROID) 16 // Prefer Android's libbase definitions to our own. 17 #include <android-base/macros.h> 18 #endif // defined(ANDROID) 19 20 // Put this in the declarations for a class to be uncopyable. 21 #if !defined(DISALLOW_COPY) 22 #define DISALLOW_COPY(TypeName) \ 23 TypeName(const TypeName&) = delete 24 #endif 25 26 // Put this in the declarations for a class to be unassignable. 27 #if !defined(DISALLOW_ASSIGN) 28 #define DISALLOW_ASSIGN(TypeName) \ 29 void operator=(const TypeName&) = delete 30 #endif 31 32 // A macro to disallow the copy constructor and operator= functions 33 // This should be used in the private: declarations for a class 34 // We define this macro conditionally as it may be defined by another libraries. 35 #if !defined(DISALLOW_COPY_AND_ASSIGN) 36 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ 37 TypeName(const TypeName&) = delete; \ 38 void operator=(const TypeName&) = delete 39 #endif 40 41 // A macro to disallow all the implicit constructors, namely the 42 // default constructor, copy constructor and operator= functions. 43 // 44 // This should be used in the private: declarations for a class 45 // that wants to prevent anyone from instantiating it. This is 46 // especially useful for classes containing only static methods. 47 #if !defined(DISALLOW_IMPLICIT_CONSTRUCTORS) 48 #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \ 49 TypeName() = delete; \ 50 DISALLOW_COPY_AND_ASSIGN(TypeName) 51 #endif 52 53 // The arraysize(arr) macro returns the # of elements in an array arr. The 54 // expression is a compile-time constant, and therefore can be used in defining 55 // new arrays, for example. If you use arraysize on a pointer by mistake, you 56 // will get a compile-time error. For the technical details, refer to 57 // http://blogs.msdn.com/b/the1/archive/2004/05/07/128242.aspx. 58 59 // This template function declaration is used in defining arraysize. 60 // Note that the function doesn't need an implementation, as we only 61 // use its type. 62 #if !defined(arraysize) 63 template <typename T, size_t N> char (&ArraySizeHelper(T (&array)[N]))[N]; 64 #define arraysize(array) (sizeof(ArraySizeHelper(array))) 65 #endif 66 67 // Used to explicitly mark the return value of a function as unused. If you are 68 // really sure you don't want to do anything with the return value of a function 69 // that has been marked WARN_UNUSED_RESULT, wrap it with this. Example: 70 // 71 // std::unique_ptr<MyType> my_var = ...; 72 // if (TakeOwnership(my_var.get()) == SUCCESS) 73 // ignore_result(my_var.release()); 74 // 75 template<typename T> 76 inline void ignore_result(const T&) { 77 } 78 79 // The following enum should be used only as a constructor argument to indicate 80 // that the variable has static storage class, and that the constructor should 81 // do nothing to its state. It indicates to the reader that it is legal to 82 // declare a static instance of the class, provided the constructor is given 83 // the base::LINKER_INITIALIZED argument. Normally, it is unsafe to declare a 84 // static variable that has a constructor or a destructor because invocation 85 // order is undefined. However, IF the type can be initialized by filling with 86 // zeroes (which the loader does for static variables), AND the destructor also 87 // does nothing to the storage, AND there are no virtual methods, then a 88 // constructor declared as 89 // explicit MyClass(base::LinkerInitialized x) {} 90 // and invoked as 91 // static MyClass my_variable_name(base::LINKER_INITIALIZED); 92 namespace base { 93 enum LinkerInitialized { LINKER_INITIALIZED }; 94 95 // Use these to declare and define a static local variable (static T;) so that 96 // it is leaked so that its destructors are not called at exit. If you need 97 // thread-safe initialization, use base/lazy_instance.h instead. 98 #if !defined(CR_DEFINE_STATIC_LOCAL) 99 #define CR_DEFINE_STATIC_LOCAL(type, name, arguments) \ 100 static type& name = *new type arguments 101 #endif 102 103 } // base 104 105 #endif // BASE_MACROS_H_ 106