Remove linux_chromium_gn_dbg from the chromium CQ.
[chromium-blink-merge.git] / base / macros.h
blobc5f503fbc3ae9158f82ecd3f487ac0e13bb7ae77
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.
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.)
10 #ifndef BASE_MACROS_H_
11 #define BASE_MACROS_H_
13 #include <stddef.h> // For size_t.
14 #include <string.h> // For memcpy.
16 // Put this in the declarations for a class to be uncopyable.
17 #define DISALLOW_COPY(TypeName) \
18 TypeName(const TypeName&) = delete
20 // Put this in the declarations for a class to be unassignable.
21 #define DISALLOW_ASSIGN(TypeName) \
22 void operator=(const TypeName&) = delete
24 // A macro to disallow the copy constructor and operator= functions
25 // This should be used in the private: declarations for a class
26 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
27 TypeName(const TypeName&); \
28 void operator=(const TypeName&)
30 // An older, deprecated, politically incorrect name for the above.
31 // NOTE: The usage of this macro was banned from our code base, but some
32 // third_party libraries are yet using it.
33 // TODO(tfarina): Figure out how to fix the usage of this macro in the
34 // third_party libraries and get rid of it.
35 #define DISALLOW_EVIL_CONSTRUCTORS(TypeName) DISALLOW_COPY_AND_ASSIGN(TypeName)
37 // A macro to disallow all the implicit constructors, namely the
38 // default constructor, copy constructor and operator= functions.
40 // This should be used in the private: declarations for a class
41 // that wants to prevent anyone from instantiating it. This is
42 // especially useful for classes containing only static methods.
43 #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
44 TypeName() = delete; \
45 DISALLOW_COPY_AND_ASSIGN(TypeName)
47 // The arraysize(arr) macro returns the # of elements in an array arr.
48 // The expression is a compile-time constant, and therefore can be
49 // used in defining new arrays, for example. If you use arraysize on
50 // a pointer by mistake, you will get a compile-time error.
52 // This template function declaration is used in defining arraysize.
53 // Note that the function doesn't need an implementation, as we only
54 // use its type.
55 template <typename T, size_t N> char (&ArraySizeHelper(T (&array)[N]))[N];
56 #define arraysize(array) (sizeof(ArraySizeHelper(array)))
58 // The COMPILE_ASSERT macro can be used to verify that a compile time
59 // expression is true. For example, you could use it to verify the
60 // size of a static array:
62 // COMPILE_ASSERT(arraysize(content_type_names) == CONTENT_NUM_TYPES,
63 // content_type_names_incorrect_size);
65 // or to make sure a struct is smaller than a certain size:
67 // COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
69 // The second argument to the macro is the name of the variable. If
70 // the expression is false, most compilers will issue a warning/error
71 // containing the name of the variable.
73 #undef COMPILE_ASSERT
74 #define COMPILE_ASSERT(expr, msg) static_assert(expr, #msg)
76 // bit_cast<Dest,Source> is a template function that implements the
77 // equivalent of "*reinterpret_cast<Dest*>(&source)". We need this in
78 // very low-level functions like the protobuf library and fast math
79 // support.
81 // float f = 3.14159265358979;
82 // int i = bit_cast<int32>(f);
83 // // i = 0x40490fdb
85 // The classical address-casting method is:
87 // // WRONG
88 // float f = 3.14159265358979; // WRONG
89 // int i = * reinterpret_cast<int*>(&f); // WRONG
91 // The address-casting method actually produces undefined behavior
92 // according to ISO C++ specification section 3.10 -15 -. Roughly, this
93 // section says: if an object in memory has one type, and a program
94 // accesses it with a different type, then the result is undefined
95 // behavior for most values of "different type".
97 // This is true for any cast syntax, either *(int*)&f or
98 // *reinterpret_cast<int*>(&f). And it is particularly true for
99 // conversions between integral lvalues and floating-point lvalues.
101 // The purpose of 3.10 -15- is to allow optimizing compilers to assume
102 // that expressions with different types refer to different memory. gcc
103 // 4.0.1 has an optimizer that takes advantage of this. So a
104 // non-conforming program quietly produces wildly incorrect output.
106 // The problem is not the use of reinterpret_cast. The problem is type
107 // punning: holding an object in memory of one type and reading its bits
108 // back using a different type.
110 // The C++ standard is more subtle and complex than this, but that
111 // is the basic idea.
113 // Anyways ...
115 // bit_cast<> calls memcpy() which is blessed by the standard,
116 // especially by the example in section 3.9 . Also, of course,
117 // bit_cast<> wraps up the nasty logic in one place.
119 // Fortunately memcpy() is very fast. In optimized mode, with a
120 // constant size, gcc 2.95.3, gcc 4.0.1, and msvc 7.1 produce inline
121 // code with the minimal amount of data movement. On a 32-bit system,
122 // memcpy(d,s,4) compiles to one load and one store, and memcpy(d,s,8)
123 // compiles to two loads and two stores.
125 // I tested this code with gcc 2.95.3, gcc 4.0.1, icc 8.1, and msvc 7.1.
127 // WARNING: if Dest or Source is a non-POD type, the result of the memcpy
128 // is likely to surprise you.
130 template <class Dest, class Source>
131 inline Dest bit_cast(const Source& source) {
132 COMPILE_ASSERT(sizeof(Dest) == sizeof(Source), VerifySizesAreEqual);
134 Dest dest;
135 memcpy(&dest, &source, sizeof(dest));
136 return dest;
139 // Used to explicitly mark the return value of a function as unused. If you are
140 // really sure you don't want to do anything with the return value of a function
141 // that has been marked WARN_UNUSED_RESULT, wrap it with this. Example:
143 // scoped_ptr<MyType> my_var = ...;
144 // if (TakeOwnership(my_var.get()) == SUCCESS)
145 // ignore_result(my_var.release());
147 template<typename T>
148 inline void ignore_result(const T&) {
151 // The following enum should be used only as a constructor argument to indicate
152 // that the variable has static storage class, and that the constructor should
153 // do nothing to its state. It indicates to the reader that it is legal to
154 // declare a static instance of the class, provided the constructor is given
155 // the base::LINKER_INITIALIZED argument. Normally, it is unsafe to declare a
156 // static variable that has a constructor or a destructor because invocation
157 // order is undefined. However, IF the type can be initialized by filling with
158 // zeroes (which the loader does for static variables), AND the destructor also
159 // does nothing to the storage, AND there are no virtual methods, then a
160 // constructor declared as
161 // explicit MyClass(base::LinkerInitialized x) {}
162 // and invoked as
163 // static MyClass my_variable_name(base::LINKER_INITIALIZED);
164 namespace base {
165 enum LinkerInitialized { LINKER_INITIALIZED };
167 // Use these to declare and define a static local variable (static T;) so that
168 // it is leaked so that its destructors are not called at exit. If you need
169 // thread-safe initialization, use base/lazy_instance.h instead.
170 #define CR_DEFINE_STATIC_LOCAL(type, name, arguments) \
171 static type& name = *new type arguments
173 } // base
175 #endif // BASE_MACROS_H_