Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 definitions of our old basic integral types | |
| 6 // ((u)int{8,16,32,64}) and further includes. I recommend that you use the C99 | |
| 7 // standard types instead, and include <stdint.h>/<stddef.h>/etc. as needed. | |
| 8 // Note that the macros and macro-like constructs that were formerly defined in | |
| 9 // this file are now available separately in base/macros.h. | |
| 10 | |
| 5 #ifndef BASE_BASICTYPES_H_ | 11 #ifndef BASE_BASICTYPES_H_ |
| 6 #define BASE_BASICTYPES_H_ | 12 #define BASE_BASICTYPES_H_ |
| 7 | 13 |
| 8 #include <limits.h> // So we can set the bounds of our types. | 14 #include <limits.h> // So we can set the bounds of our types. |
| 9 #include <stddef.h> // For size_t. | 15 #include <stddef.h> // For size_t. |
| 10 #include <stdint.h> // For intptr_t. | 16 #include <stdint.h> // For intptr_t. |
| 11 #include <string.h> // For memcpy. | |
| 12 | 17 |
| 13 #include "base/compiler_specific.h" | 18 #include "base/macros.h" |
| 14 #include "base/port.h" // Types that only need exist on certain systems. | 19 #include "base/port.h" // Types that only need exist on certain systems. |
| 15 | 20 |
| 21 // DEPRECATED: Please use (u)int{8,16,32,64}_t instead (and include <stdint.h>). | |
| 16 typedef int8_t int8; | 22 typedef int8_t int8; |
| 17 typedef uint8_t uint8; | 23 typedef uint8_t uint8; |
| 18 typedef int16_t int16; | 24 typedef int16_t int16; |
| 19 typedef int32_t int32; | 25 typedef int32_t int32; |
| 20 typedef uint16_t uint16; | 26 typedef uint16_t uint16; |
| 21 typedef uint32_t uint32; | 27 typedef uint32_t uint32; |
| 22 | 28 |
| 23 // TODO(vtl): Figure what's up with the 64-bit types. Can we just define them as | 29 // TODO(vtl): Figure what's up with the 64-bit types. Can we just define them as |
| 24 // |int64_t|/|uint64_t|? | 30 // |int64_t|/|uint64_t|? |
| 25 // The NSPR system headers define 64-bit as |long| when possible, except on | 31 // The NSPR system headers define 64-bit as |long| when possible, except on |
| 26 // Mac OS X. In order to not have typedef mismatches, we do the same on LP64. | 32 // Mac OS X. In order to not have typedef mismatches, we do the same on LP64. |
| 27 // | 33 // |
| 28 // On Mac OS X, |long long| is used for 64-bit types for compatibility with | 34 // On Mac OS X, |long long| is used for 64-bit types for compatibility with |
| 29 // <inttypes.h> format macros even in the LP64 model. | 35 // <inttypes.h> format macros even in the LP64 model. |
| 30 #if defined(__LP64__) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) | 36 #if defined(__LP64__) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) |
| 31 typedef long int64; | 37 typedef long int64; |
| 32 typedef unsigned long uint64; | 38 typedef unsigned long uint64; |
| 33 #else | 39 #else |
| 34 typedef long long int64; | 40 typedef long long int64; |
| 35 typedef unsigned long long uint64; | 41 typedef unsigned long long uint64; |
| 36 #endif | 42 #endif |
| 37 | 43 |
| 44 // DEPRECATED: Please use std::numeric_limits (from <limits>) instead. | |
|
brettw
2014/01/08 21:24:03
BTW the problem with numeric_limits is that it int
Nico
2014/04/04 01:07:14
+1, just came by to say "this is bad advice as it
| |
| 38 const uint8 kuint8max = (( uint8) 0xFF); | 45 const uint8 kuint8max = (( uint8) 0xFF); |
| 39 const uint16 kuint16max = ((uint16) 0xFFFF); | 46 const uint16 kuint16max = ((uint16) 0xFFFF); |
| 40 const uint32 kuint32max = ((uint32) 0xFFFFFFFF); | 47 const uint32 kuint32max = ((uint32) 0xFFFFFFFF); |
| 41 const uint64 kuint64max = ((uint64) GG_LONGLONG(0xFFFFFFFFFFFFFFFF)); | 48 const uint64 kuint64max = ((uint64) GG_LONGLONG(0xFFFFFFFFFFFFFFFF)); |
| 42 const int8 kint8min = (( int8) 0x80); | 49 const int8 kint8min = (( int8) 0x80); |
| 43 const int8 kint8max = (( int8) 0x7F); | 50 const int8 kint8max = (( int8) 0x7F); |
| 44 const int16 kint16min = (( int16) 0x8000); | 51 const int16 kint16min = (( int16) 0x8000); |
| 45 const int16 kint16max = (( int16) 0x7FFF); | 52 const int16 kint16max = (( int16) 0x7FFF); |
| 46 const int32 kint32min = (( int32) 0x80000000); | 53 const int32 kint32min = (( int32) 0x80000000); |
| 47 const int32 kint32max = (( int32) 0x7FFFFFFF); | 54 const int32 kint32max = (( int32) 0x7FFFFFFF); |
| 48 const int64 kint64min = (( int64) GG_LONGLONG(0x8000000000000000)); | 55 const int64 kint64min = (( int64) GG_LONGLONG(0x8000000000000000)); |
| 49 const int64 kint64max = (( int64) GG_LONGLONG(0x7FFFFFFFFFFFFFFF)); | 56 const int64 kint64max = (( int64) GG_LONGLONG(0x7FFFFFFFFFFFFFFF)); |
| 50 | 57 |
| 51 // Put this in the private: declarations for a class to be uncopyable. | |
| 52 #define DISALLOW_COPY(TypeName) \ | |
| 53 TypeName(const TypeName&) | |
| 54 | |
| 55 // Put this in the private: declarations for a class to be unassignable. | |
| 56 #define DISALLOW_ASSIGN(TypeName) \ | |
| 57 void operator=(const TypeName&) | |
| 58 | |
| 59 // A macro to disallow the copy constructor and operator= functions | |
| 60 // This should be used in the private: declarations for a class | |
| 61 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ | |
| 62 TypeName(const TypeName&); \ | |
| 63 void operator=(const TypeName&) | |
| 64 | |
| 65 // An older, deprecated, politically incorrect name for the above. | |
| 66 // NOTE: The usage of this macro was banned from our code base, but some | |
| 67 // third_party libraries are yet using it. | |
| 68 // TODO(tfarina): Figure out how to fix the usage of this macro in the | |
| 69 // third_party libraries and get rid of it. | |
| 70 #define DISALLOW_EVIL_CONSTRUCTORS(TypeName) DISALLOW_COPY_AND_ASSIGN(TypeName) | |
| 71 | |
| 72 // A macro to disallow all the implicit constructors, namely the | |
| 73 // default constructor, copy constructor and operator= functions. | |
| 74 // | |
| 75 // This should be used in the private: declarations for a class | |
| 76 // that wants to prevent anyone from instantiating it. This is | |
| 77 // especially useful for classes containing only static methods. | |
| 78 #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \ | |
| 79 TypeName(); \ | |
| 80 DISALLOW_COPY_AND_ASSIGN(TypeName) | |
| 81 | |
| 82 // The arraysize(arr) macro returns the # of elements in an array arr. | |
| 83 // The expression is a compile-time constant, and therefore can be | |
| 84 // used in defining new arrays, for example. If you use arraysize on | |
| 85 // a pointer by mistake, you will get a compile-time error. | |
| 86 // | |
| 87 // One caveat is that arraysize() doesn't accept any array of an | |
| 88 // anonymous type or a type defined inside a function. In these rare | |
| 89 // cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below. This is | |
| 90 // due to a limitation in C++'s template system. The limitation might | |
| 91 // eventually be removed, but it hasn't happened yet. | |
| 92 | |
| 93 // This template function declaration is used in defining arraysize. | |
| 94 // Note that the function doesn't need an implementation, as we only | |
| 95 // use its type. | |
| 96 template <typename T, size_t N> | |
| 97 char (&ArraySizeHelper(T (&array)[N]))[N]; | |
| 98 | |
| 99 // That gcc wants both of these prototypes seems mysterious. VC, for | |
| 100 // its part, can't decide which to use (another mystery). Matching of | |
| 101 // template overloads: the final frontier. | |
| 102 #ifndef _MSC_VER | |
| 103 template <typename T, size_t N> | |
| 104 char (&ArraySizeHelper(const T (&array)[N]))[N]; | |
| 105 #endif | |
| 106 | |
| 107 #define arraysize(array) (sizeof(ArraySizeHelper(array))) | |
| 108 | |
| 109 // ARRAYSIZE_UNSAFE performs essentially the same calculation as arraysize, | |
| 110 // but can be used on anonymous types or types defined inside | |
| 111 // functions. It's less safe than arraysize as it accepts some | |
| 112 // (although not all) pointers. Therefore, you should use arraysize | |
| 113 // whenever possible. | |
| 114 // | |
| 115 // The expression ARRAYSIZE_UNSAFE(a) is a compile-time constant of type | |
| 116 // size_t. | |
| 117 // | |
| 118 // ARRAYSIZE_UNSAFE catches a few type errors. If you see a compiler error | |
| 119 // | |
| 120 // "warning: division by zero in ..." | |
| 121 // | |
| 122 // when using ARRAYSIZE_UNSAFE, you are (wrongfully) giving it a pointer. | |
| 123 // You should only use ARRAYSIZE_UNSAFE on statically allocated arrays. | |
| 124 // | |
| 125 // The following comments are on the implementation details, and can | |
| 126 // be ignored by the users. | |
| 127 // | |
| 128 // ARRAYSIZE_UNSAFE(arr) works by inspecting sizeof(arr) (the # of bytes in | |
| 129 // the array) and sizeof(*(arr)) (the # of bytes in one array | |
| 130 // element). If the former is divisible by the latter, perhaps arr is | |
| 131 // indeed an array, in which case the division result is the # of | |
| 132 // elements in the array. Otherwise, arr cannot possibly be an array, | |
| 133 // and we generate a compiler error to prevent the code from | |
| 134 // compiling. | |
| 135 // | |
| 136 // Since the size of bool is implementation-defined, we need to cast | |
| 137 // !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final | |
| 138 // result has type size_t. | |
| 139 // | |
| 140 // This macro is not perfect as it wrongfully accepts certain | |
| 141 // pointers, namely where the pointer size is divisible by the pointee | |
| 142 // size. Since all our code has to go through a 32-bit compiler, | |
| 143 // where a pointer is 4 bytes, this means all pointers to a type whose | |
| 144 // size is 3 or greater than 4 will be (righteously) rejected. | |
| 145 | |
| 146 #define ARRAYSIZE_UNSAFE(a) \ | |
| 147 ((sizeof(a) / sizeof(*(a))) / \ | |
| 148 static_cast<size_t>(!(sizeof(a) % sizeof(*(a))))) | |
| 149 | |
| 150 | |
| 151 // Use implicit_cast as a safe version of static_cast or const_cast | |
| 152 // for upcasting in the type hierarchy (i.e. casting a pointer to Foo | |
| 153 // to a pointer to SuperclassOfFoo or casting a pointer to Foo to | |
| 154 // a const pointer to Foo). | |
| 155 // When you use implicit_cast, the compiler checks that the cast is safe. | |
| 156 // Such explicit implicit_casts are necessary in surprisingly many | |
| 157 // situations where C++ demands an exact type match instead of an | |
| 158 // argument type convertible to a target type. | |
| 159 // | |
| 160 // The From type can be inferred, so the preferred syntax for using | |
| 161 // implicit_cast is the same as for static_cast etc.: | |
| 162 // | |
| 163 // implicit_cast<ToType>(expr) | |
| 164 // | |
| 165 // implicit_cast would have been part of the C++ standard library, | |
| 166 // but the proposal was submitted too late. It will probably make | |
| 167 // its way into the language in the future. | |
| 168 template<typename To, typename From> | |
| 169 inline To implicit_cast(From const &f) { | |
| 170 return f; | |
| 171 } | |
| 172 | |
| 173 // The COMPILE_ASSERT macro can be used to verify that a compile time | |
| 174 // expression is true. For example, you could use it to verify the | |
| 175 // size of a static array: | |
| 176 // | |
| 177 // COMPILE_ASSERT(ARRAYSIZE_UNSAFE(content_type_names) == CONTENT_NUM_TYPES, | |
| 178 // content_type_names_incorrect_size); | |
| 179 // | |
| 180 // or to make sure a struct is smaller than a certain size: | |
| 181 // | |
| 182 // COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large); | |
| 183 // | |
| 184 // The second argument to the macro is the name of the variable. If | |
| 185 // the expression is false, most compilers will issue a warning/error | |
| 186 // containing the name of the variable. | |
| 187 | |
| 188 #undef COMPILE_ASSERT | |
| 189 | |
| 190 #if __cplusplus >= 201103L | |
| 191 | |
| 192 // Under C++11, just use static_assert. | |
| 193 #define COMPILE_ASSERT(expr, msg) static_assert(expr, #msg) | |
| 194 | |
| 195 #else | |
| 196 | |
| 197 template <bool> | |
| 198 struct CompileAssert { | |
| 199 }; | |
| 200 | |
| 201 #define COMPILE_ASSERT(expr, msg) \ | |
| 202 typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] ALLOW_UNUSED | |
| 203 | |
| 204 // Implementation details of COMPILE_ASSERT: | |
| 205 // | |
| 206 // - COMPILE_ASSERT works by defining an array type that has -1 | |
| 207 // elements (and thus is invalid) when the expression is false. | |
| 208 // | |
| 209 // - The simpler definition | |
| 210 // | |
| 211 // #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1] | |
| 212 // | |
| 213 // does not work, as gcc supports variable-length arrays whose sizes | |
| 214 // are determined at run-time (this is gcc's extension and not part | |
| 215 // of the C++ standard). As a result, gcc fails to reject the | |
| 216 // following code with the simple definition: | |
| 217 // | |
| 218 // int foo; | |
| 219 // COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is | |
| 220 // // not a compile-time constant. | |
| 221 // | |
| 222 // - By using the type CompileAssert<(bool(expr))>, we ensures that | |
| 223 // expr is a compile-time constant. (Template arguments must be | |
| 224 // determined at compile-time.) | |
| 225 // | |
| 226 // - The outer parentheses in CompileAssert<(bool(expr))> are necessary | |
| 227 // to work around a bug in gcc 3.4.4 and 4.0.1. If we had written | |
| 228 // | |
| 229 // CompileAssert<bool(expr)> | |
| 230 // | |
| 231 // instead, these compilers will refuse to compile | |
| 232 // | |
| 233 // COMPILE_ASSERT(5 > 0, some_message); | |
| 234 // | |
| 235 // (They seem to think the ">" in "5 > 0" marks the end of the | |
| 236 // template argument list.) | |
| 237 // | |
| 238 // - The array size is (bool(expr) ? 1 : -1), instead of simply | |
| 239 // | |
| 240 // ((expr) ? 1 : -1). | |
| 241 // | |
| 242 // This is to avoid running into a bug in MS VC 7.1, which | |
| 243 // causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1. | |
| 244 | |
| 245 #endif | |
| 246 | |
| 247 // bit_cast<Dest,Source> is a template function that implements the | |
| 248 // equivalent of "*reinterpret_cast<Dest*>(&source)". We need this in | |
| 249 // very low-level functions like the protobuf library and fast math | |
| 250 // support. | |
| 251 // | |
| 252 // float f = 3.14159265358979; | |
| 253 // int i = bit_cast<int32>(f); | |
| 254 // // i = 0x40490fdb | |
| 255 // | |
| 256 // The classical address-casting method is: | |
| 257 // | |
| 258 // // WRONG | |
| 259 // float f = 3.14159265358979; // WRONG | |
| 260 // int i = * reinterpret_cast<int*>(&f); // WRONG | |
| 261 // | |
| 262 // The address-casting method actually produces undefined behavior | |
| 263 // according to ISO C++ specification section 3.10 -15 -. Roughly, this | |
| 264 // section says: if an object in memory has one type, and a program | |
| 265 // accesses it with a different type, then the result is undefined | |
| 266 // behavior for most values of "different type". | |
| 267 // | |
| 268 // This is true for any cast syntax, either *(int*)&f or | |
| 269 // *reinterpret_cast<int*>(&f). And it is particularly true for | |
| 270 // conversions between integral lvalues and floating-point lvalues. | |
| 271 // | |
| 272 // The purpose of 3.10 -15- is to allow optimizing compilers to assume | |
| 273 // that expressions with different types refer to different memory. gcc | |
| 274 // 4.0.1 has an optimizer that takes advantage of this. So a | |
| 275 // non-conforming program quietly produces wildly incorrect output. | |
| 276 // | |
| 277 // The problem is not the use of reinterpret_cast. The problem is type | |
| 278 // punning: holding an object in memory of one type and reading its bits | |
| 279 // back using a different type. | |
| 280 // | |
| 281 // The C++ standard is more subtle and complex than this, but that | |
| 282 // is the basic idea. | |
| 283 // | |
| 284 // Anyways ... | |
| 285 // | |
| 286 // bit_cast<> calls memcpy() which is blessed by the standard, | |
| 287 // especially by the example in section 3.9 . Also, of course, | |
| 288 // bit_cast<> wraps up the nasty logic in one place. | |
| 289 // | |
| 290 // Fortunately memcpy() is very fast. In optimized mode, with a | |
| 291 // constant size, gcc 2.95.3, gcc 4.0.1, and msvc 7.1 produce inline | |
| 292 // code with the minimal amount of data movement. On a 32-bit system, | |
| 293 // memcpy(d,s,4) compiles to one load and one store, and memcpy(d,s,8) | |
| 294 // compiles to two loads and two stores. | |
| 295 // | |
| 296 // I tested this code with gcc 2.95.3, gcc 4.0.1, icc 8.1, and msvc 7.1. | |
| 297 // | |
| 298 // WARNING: if Dest or Source is a non-POD type, the result of the memcpy | |
| 299 // is likely to surprise you. | |
| 300 | |
| 301 template <class Dest, class Source> | |
| 302 inline Dest bit_cast(const Source& source) { | |
| 303 COMPILE_ASSERT(sizeof(Dest) == sizeof(Source), VerifySizesAreEqual); | |
| 304 | |
| 305 Dest dest; | |
| 306 memcpy(&dest, &source, sizeof(dest)); | |
| 307 return dest; | |
| 308 } | |
| 309 | |
| 310 // Used to explicitly mark the return value of a function as unused. If you are | |
| 311 // really sure you don't want to do anything with the return value of a function | |
| 312 // that has been marked WARN_UNUSED_RESULT, wrap it with this. Example: | |
| 313 // | |
| 314 // scoped_ptr<MyType> my_var = ...; | |
| 315 // if (TakeOwnership(my_var.get()) == SUCCESS) | |
| 316 // ignore_result(my_var.release()); | |
| 317 // | |
| 318 template<typename T> | |
| 319 inline void ignore_result(const T&) { | |
| 320 } | |
| 321 | |
| 322 // The following enum should be used only as a constructor argument to indicate | |
| 323 // that the variable has static storage class, and that the constructor should | |
| 324 // do nothing to its state. It indicates to the reader that it is legal to | |
| 325 // declare a static instance of the class, provided the constructor is given | |
| 326 // the base::LINKER_INITIALIZED argument. Normally, it is unsafe to declare a | |
| 327 // static variable that has a constructor or a destructor because invocation | |
| 328 // order is undefined. However, IF the type can be initialized by filling with | |
| 329 // zeroes (which the loader does for static variables), AND the destructor also | |
| 330 // does nothing to the storage, AND there are no virtual methods, then a | |
| 331 // constructor declared as | |
| 332 // explicit MyClass(base::LinkerInitialized x) {} | |
| 333 // and invoked as | |
| 334 // static MyClass my_variable_name(base::LINKER_INITIALIZED); | |
| 335 namespace base { | |
| 336 enum LinkerInitialized { LINKER_INITIALIZED }; | |
| 337 | |
| 338 // Use these to declare and define a static local variable (static T;) so that | |
| 339 // it is leaked so that its destructors are not called at exit. If you need | |
| 340 // thread-safe initialization, use base/lazy_instance.h instead. | |
| 341 #define CR_DEFINE_STATIC_LOCAL(type, name, arguments) \ | |
| 342 static type& name = *new type arguments | |
| 343 | |
| 344 } // base | |
| 345 | |
| 346 #endif // BASE_BASICTYPES_H_ | 58 #endif // BASE_BASICTYPES_H_ |
| OLD | NEW |