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