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
10 #ifndef BASE_MACROS_H_
11 #define BASE_MACROS_H_
13 #include <stddef.h> // For size_t.
14 #include <string.h> // For memcpy.
16 #include "base/compiler_specific.h" // For ALLOW_UNUSED.
18 // Put this in the private: declarations for a class to be uncopyable.
19 #define DISALLOW_COPY(TypeName) \
20 TypeName(const TypeName&)
22 // Put this in the private: declarations for a class to be unassignable.
23 #define DISALLOW_ASSIGN(TypeName) \
24 void operator=(const TypeName&)
26 // A macro to disallow the copy constructor and operator= functions
27 // This should be used in the private: declarations for a class
28 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
29 TypeName(const TypeName&); \
30 void operator=(const TypeName&)
32 // An older, deprecated, politically incorrect name for the above.
33 // NOTE: The usage of this macro was banned from our code base, but some
34 // third_party libraries are yet using it.
35 // TODO(tfarina): Figure out how to fix the usage of this macro in the
36 // third_party libraries and get rid of it.
37 #define DISALLOW_EVIL_CONSTRUCTORS(TypeName) DISALLOW_COPY_AND_ASSIGN(TypeName)
39 // A macro to disallow all the implicit constructors, namely the
40 // default constructor, copy constructor and operator= functions.
42 // This should be used in the private: declarations for a class
43 // that wants to prevent anyone from instantiating it. This is
44 // especially useful for classes containing only static methods.
45 #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
47 DISALLOW_COPY_AND_ASSIGN(TypeName)
49 // The arraysize(arr) macro returns the # of elements in an array arr.
50 // The expression is a compile-time constant, and therefore can be
51 // used in defining new arrays, for example. If you use arraysize on
52 // a pointer by mistake, you will get a compile-time error.
54 // This template function declaration is used in defining arraysize.
55 // Note that the function doesn't need an implementation, as we only
57 template <typename T
, size_t N
>
58 char (&ArraySizeHelper(T (&array
)[N
]))[N
];
60 // That gcc wants both of these prototypes seems mysterious. VC, for
61 // its part, can't decide which to use (another mystery). Matching of
62 // template overloads: the final frontier.
64 template <typename T
, size_t N
>
65 char (&ArraySizeHelper(const T (&array
)[N
]))[N
];
68 #define arraysize(array) (sizeof(ArraySizeHelper(array)))
70 // DEPRECATED: Just use |arraysize()|, now that C++11 has removed the
71 // limitations that forced the use of |ARRAYSIZE_UNSAFE()|.
72 // TODO(viettrungluu): Convert all instances and delete. (The only uses are now
73 // in Blink; the ifdef is to prevent it from reappearing in Chromium.)
75 #if defined(BLINK_PLATFORM) || defined(BLINK_PLATFORM_IMPLEMENTATION)
76 #define ARRAYSIZE_UNSAFE(a) arraysize(a)
80 // Use implicit_cast as a safe version of static_cast or const_cast
81 // for upcasting in the type hierarchy (i.e. casting a pointer to Foo
82 // to a pointer to SuperclassOfFoo or casting a pointer to Foo to
83 // a const pointer to Foo).
84 // When you use implicit_cast, the compiler checks that the cast is safe.
85 // Such explicit implicit_casts are necessary in surprisingly many
86 // situations where C++ demands an exact type match instead of an
87 // argument type convertible to a target type.
89 // The From type can be inferred, so the preferred syntax for using
90 // implicit_cast is the same as for static_cast etc.:
92 // implicit_cast<ToType>(expr)
94 // implicit_cast would have been part of the C++ standard library,
95 // but the proposal was submitted too late. It will probably make
96 // its way into the language in the future.
97 template<typename To
, typename From
>
98 inline To
implicit_cast(From
const &f
) {
102 // The COMPILE_ASSERT macro can be used to verify that a compile time
103 // expression is true. For example, you could use it to verify the
104 // size of a static array:
106 // COMPILE_ASSERT(arraysize(content_type_names) == CONTENT_NUM_TYPES,
107 // content_type_names_incorrect_size);
109 // or to make sure a struct is smaller than a certain size:
111 // COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
113 // The second argument to the macro is the name of the variable. If
114 // the expression is false, most compilers will issue a warning/error
115 // containing the name of the variable.
117 #undef COMPILE_ASSERT
118 #define COMPILE_ASSERT(expr, msg) static_assert(expr, #msg)
120 // bit_cast<Dest,Source> is a template function that implements the
121 // equivalent of "*reinterpret_cast<Dest*>(&source)". We need this in
122 // very low-level functions like the protobuf library and fast math
125 // float f = 3.14159265358979;
126 // int i = bit_cast<int32>(f);
129 // The classical address-casting method is:
132 // float f = 3.14159265358979; // WRONG
133 // int i = * reinterpret_cast<int*>(&f); // WRONG
135 // The address-casting method actually produces undefined behavior
136 // according to ISO C++ specification section 3.10 -15 -. Roughly, this
137 // section says: if an object in memory has one type, and a program
138 // accesses it with a different type, then the result is undefined
139 // behavior for most values of "different type".
141 // This is true for any cast syntax, either *(int*)&f or
142 // *reinterpret_cast<int*>(&f). And it is particularly true for
143 // conversions between integral lvalues and floating-point lvalues.
145 // The purpose of 3.10 -15- is to allow optimizing compilers to assume
146 // that expressions with different types refer to different memory. gcc
147 // 4.0.1 has an optimizer that takes advantage of this. So a
148 // non-conforming program quietly produces wildly incorrect output.
150 // The problem is not the use of reinterpret_cast. The problem is type
151 // punning: holding an object in memory of one type and reading its bits
152 // back using a different type.
154 // The C++ standard is more subtle and complex than this, but that
155 // is the basic idea.
159 // bit_cast<> calls memcpy() which is blessed by the standard,
160 // especially by the example in section 3.9 . Also, of course,
161 // bit_cast<> wraps up the nasty logic in one place.
163 // Fortunately memcpy() is very fast. In optimized mode, with a
164 // constant size, gcc 2.95.3, gcc 4.0.1, and msvc 7.1 produce inline
165 // code with the minimal amount of data movement. On a 32-bit system,
166 // memcpy(d,s,4) compiles to one load and one store, and memcpy(d,s,8)
167 // compiles to two loads and two stores.
169 // I tested this code with gcc 2.95.3, gcc 4.0.1, icc 8.1, and msvc 7.1.
171 // WARNING: if Dest or Source is a non-POD type, the result of the memcpy
172 // is likely to surprise you.
174 template <class Dest
, class Source
>
175 inline Dest
bit_cast(const Source
& source
) {
176 COMPILE_ASSERT(sizeof(Dest
) == sizeof(Source
), VerifySizesAreEqual
);
179 memcpy(&dest
, &source
, sizeof(dest
));
183 // Used to explicitly mark the return value of a function as unused. If you are
184 // really sure you don't want to do anything with the return value of a function
185 // that has been marked WARN_UNUSED_RESULT, wrap it with this. Example:
187 // scoped_ptr<MyType> my_var = ...;
188 // if (TakeOwnership(my_var.get()) == SUCCESS)
189 // ignore_result(my_var.release());
192 inline void ignore_result(const T
&) {
195 // The following enum should be used only as a constructor argument to indicate
196 // that the variable has static storage class, and that the constructor should
197 // do nothing to its state. It indicates to the reader that it is legal to
198 // declare a static instance of the class, provided the constructor is given
199 // the base::LINKER_INITIALIZED argument. Normally, it is unsafe to declare a
200 // static variable that has a constructor or a destructor because invocation
201 // order is undefined. However, IF the type can be initialized by filling with
202 // zeroes (which the loader does for static variables), AND the destructor also
203 // does nothing to the storage, AND there are no virtual methods, then a
204 // constructor declared as
205 // explicit MyClass(base::LinkerInitialized x) {}
207 // static MyClass my_variable_name(base::LINKER_INITIALIZED);
209 enum LinkerInitialized
{ LINKER_INITIALIZED
};
211 // Use these to declare and define a static local variable (static T;) so that
212 // it is leaked so that its destructors are not called at exit. If you need
213 // thread-safe initialization, use base/lazy_instance.h instead.
214 #define CR_DEFINE_STATIC_LOCAL(type, name, arguments) \
215 static type& name = *new type arguments
219 #endif // BASE_MACROS_H_