| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This file contains macros and macro-like constructs (e.g., templates) that | 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 | 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 | 7 // that are closely related to things that are commonly used that belong in this |
| 8 // file.) | 8 // file.) |
| 9 | 9 |
| 10 #ifndef BASE_MACROS_H_ | 10 #ifndef BASE_MACROS_H_ |
| 11 #define BASE_MACROS_H_ | 11 #define BASE_MACROS_H_ |
| 12 | 12 |
| 13 #include <stddef.h> // For size_t. | 13 #include <stddef.h> // For size_t. |
| 14 #include <string.h> // For memcpy. | |
| 15 | 14 |
| 16 // Put this in the declarations for a class to be uncopyable. | 15 // Put this in the declarations for a class to be uncopyable. |
| 17 #define DISALLOW_COPY(TypeName) \ | 16 #define DISALLOW_COPY(TypeName) \ |
| 18 TypeName(const TypeName&) = delete | 17 TypeName(const TypeName&) = delete |
| 19 | 18 |
| 20 // Put this in the declarations for a class to be unassignable. | 19 // Put this in the declarations for a class to be unassignable. |
| 21 #define DISALLOW_ASSIGN(TypeName) \ | 20 #define DISALLOW_ASSIGN(TypeName) \ |
| 22 void operator=(const TypeName&) = delete | 21 void operator=(const TypeName&) = delete |
| 23 | 22 |
| 24 // A macro to disallow the copy constructor and operator= functions | 23 // A macro to disallow the copy constructor and operator= functions |
| (...skipping 17 matching lines...) Expand all Loading... |
| 42 // new arrays, for example. If you use arraysize on a pointer by mistake, you | 41 // new arrays, for example. If you use arraysize on a pointer by mistake, you |
| 43 // will get a compile-time error. For the technical details, refer to | 42 // will get a compile-time error. For the technical details, refer to |
| 44 // http://blogs.msdn.com/b/the1/archive/2004/05/07/128242.aspx. | 43 // http://blogs.msdn.com/b/the1/archive/2004/05/07/128242.aspx. |
| 45 | 44 |
| 46 // This template function declaration is used in defining arraysize. | 45 // This template function declaration is used in defining arraysize. |
| 47 // Note that the function doesn't need an implementation, as we only | 46 // Note that the function doesn't need an implementation, as we only |
| 48 // use its type. | 47 // use its type. |
| 49 template <typename T, size_t N> char (&ArraySizeHelper(T (&array)[N]))[N]; | 48 template <typename T, size_t N> char (&ArraySizeHelper(T (&array)[N]))[N]; |
| 50 #define arraysize(array) (sizeof(ArraySizeHelper(array))) | 49 #define arraysize(array) (sizeof(ArraySizeHelper(array))) |
| 51 | 50 |
| 52 // bit_cast<Dest,Source> is a template function that implements the | |
| 53 // equivalent of "*reinterpret_cast<Dest*>(&source)". We need this in | |
| 54 // very low-level functions like the protobuf library and fast math | |
| 55 // support. | |
| 56 // | |
| 57 // float f = 3.14159265358979; | |
| 58 // int i = bit_cast<int32_t>(f); | |
| 59 // // i = 0x40490fdb | |
| 60 // | |
| 61 // The classical address-casting method is: | |
| 62 // | |
| 63 // // WRONG | |
| 64 // float f = 3.14159265358979; // WRONG | |
| 65 // int i = * reinterpret_cast<int*>(&f); // WRONG | |
| 66 // | |
| 67 // The address-casting method actually produces undefined behavior | |
| 68 // according to ISO C++ specification section 3.10 -15 -. Roughly, this | |
| 69 // section says: if an object in memory has one type, and a program | |
| 70 // accesses it with a different type, then the result is undefined | |
| 71 // behavior for most values of "different type". | |
| 72 // | |
| 73 // This is true for any cast syntax, either *(int*)&f or | |
| 74 // *reinterpret_cast<int*>(&f). And it is particularly true for | |
| 75 // conversions between integral lvalues and floating-point lvalues. | |
| 76 // | |
| 77 // The purpose of 3.10 -15- is to allow optimizing compilers to assume | |
| 78 // that expressions with different types refer to different memory. gcc | |
| 79 // 4.0.1 has an optimizer that takes advantage of this. So a | |
| 80 // non-conforming program quietly produces wildly incorrect output. | |
| 81 // | |
| 82 // The problem is not the use of reinterpret_cast. The problem is type | |
| 83 // punning: holding an object in memory of one type and reading its bits | |
| 84 // back using a different type. | |
| 85 // | |
| 86 // The C++ standard is more subtle and complex than this, but that | |
| 87 // is the basic idea. | |
| 88 // | |
| 89 // Anyways ... | |
| 90 // | |
| 91 // bit_cast<> calls memcpy() which is blessed by the standard, | |
| 92 // especially by the example in section 3.9 . Also, of course, | |
| 93 // bit_cast<> wraps up the nasty logic in one place. | |
| 94 // | |
| 95 // Fortunately memcpy() is very fast. In optimized mode, with a | |
| 96 // constant size, gcc 2.95.3, gcc 4.0.1, and msvc 7.1 produce inline | |
| 97 // code with the minimal amount of data movement. On a 32-bit system, | |
| 98 // memcpy(d,s,4) compiles to one load and one store, and memcpy(d,s,8) | |
| 99 // compiles to two loads and two stores. | |
| 100 // | |
| 101 // I tested this code with gcc 2.95.3, gcc 4.0.1, icc 8.1, and msvc 7.1. | |
| 102 // | |
| 103 // WARNING: if Dest or Source is a non-POD type, the result of the memcpy | |
| 104 // is likely to surprise you. | |
| 105 | |
| 106 template <class Dest, class Source> | |
| 107 inline Dest bit_cast(const Source& source) { | |
| 108 static_assert(sizeof(Dest) == sizeof(Source), | |
| 109 "bit_cast requires source and destination to be the same size"); | |
| 110 | |
| 111 Dest dest; | |
| 112 memcpy(&dest, &source, sizeof(dest)); | |
| 113 return dest; | |
| 114 } | |
| 115 | |
| 116 // Used to explicitly mark the return value of a function as unused. If you are | 51 // Used to explicitly mark the return value of a function as unused. If you are |
| 117 // really sure you don't want to do anything with the return value of a function | 52 // really sure you don't want to do anything with the return value of a function |
| 118 // that has been marked WARN_UNUSED_RESULT, wrap it with this. Example: | 53 // that has been marked WARN_UNUSED_RESULT, wrap it with this. Example: |
| 119 // | 54 // |
| 120 // scoped_ptr<MyType> my_var = ...; | 55 // scoped_ptr<MyType> my_var = ...; |
| 121 // if (TakeOwnership(my_var.get()) == SUCCESS) | 56 // if (TakeOwnership(my_var.get()) == SUCCESS) |
| 122 // ignore_result(my_var.release()); | 57 // ignore_result(my_var.release()); |
| 123 // | 58 // |
| 124 template<typename T> | 59 template<typename T> |
| 125 inline void ignore_result(const T&) { | 60 inline void ignore_result(const T&) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 143 | 78 |
| 144 // Use these to declare and define a static local variable (static T;) so that | 79 // Use these to declare and define a static local variable (static T;) so that |
| 145 // it is leaked so that its destructors are not called at exit. If you need | 80 // it is leaked so that its destructors are not called at exit. If you need |
| 146 // thread-safe initialization, use base/lazy_instance.h instead. | 81 // thread-safe initialization, use base/lazy_instance.h instead. |
| 147 #define CR_DEFINE_STATIC_LOCAL(type, name, arguments) \ | 82 #define CR_DEFINE_STATIC_LOCAL(type, name, arguments) \ |
| 148 static type& name = *new type arguments | 83 static type& name = *new type arguments |
| 149 | 84 |
| 150 } // base | 85 } // base |
| 151 | 86 |
| 152 #endif // BASE_MACROS_H_ | 87 #endif // BASE_MACROS_H_ |
| OLD | NEW |