basictypes.h (10704B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style license that can be 5 // found in the LICENSE file. 6 7 #ifndef BASE_BASICTYPES_H_ 8 #define BASE_BASICTYPES_H_ 9 10 #include <limits.h> // So we can set the bounds of our types 11 #include <stddef.h> // For size_t 12 #include <string.h> // for memcpy 13 14 #include "base/port.h" // Types that only need exist on certain systems 15 16 #include "mozilla/IntegerPrintfMacros.h" 17 18 // A type to represent a Unicode code-point value. As of Unicode 4.0, 19 // such values require up to 21 bits. 20 // (For type-checking on pointers, make this explicitly signed, 21 // and it should always be the signed version of whatever int32_t is.) 22 typedef signed int char32; 23 24 const uint8_t kuint8max = ((uint8_t)0xFF); 25 const uint16_t kuint16max = ((uint16_t)0xFFFF); 26 const uint32_t kuint32max = ((uint32_t)0xFFFFFFFF); 27 const uint64_t kuint64max = ((uint64_t)GG_LONGLONG(0xFFFFFFFFFFFFFFFF)); 28 const int8_t kint8min = ((int8_t)0x80); 29 const int8_t kint8max = ((int8_t)0x7F); 30 const int16_t kint16min = ((int16_t)0x8000); 31 const int16_t kint16max = ((int16_t)0x7FFF); 32 const int32_t kint32min = ((int32_t)0x80000000); 33 const int32_t kint32max = ((int32_t)0x7FFFFFFF); 34 const int64_t kint64min = ((int64_t)GG_LONGLONG(0x8000000000000000)); 35 const int64_t kint64max = ((int64_t)GG_LONGLONG(0x7FFFFFFFFFFFFFFF)); 36 37 // Platform- and hardware-dependent printf specifiers 38 #if defined(XP_UNIX) 39 # define PRId64L "I64d" 40 # define PRIu64L "I64u" 41 # define PRIx64L "I64x" 42 #else 43 # define PRId64L L"I64d" 44 # define PRIu64L L"I64u" 45 # define PRIx64L L"I64x" 46 #endif 47 48 // A macro to disallow the copy constructor and operator= functions 49 // This should be used in the private: declarations for a class 50 #undef DISALLOW_COPY_AND_ASSIGN 51 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ 52 TypeName(const TypeName&); \ 53 void operator=(const TypeName&) 54 55 // An older, deprecated, politically incorrect name for the above. 56 #undef DISALLOW_EVIL_CONSTRUCTORS 57 #define DISALLOW_EVIL_CONSTRUCTORS(TypeName) DISALLOW_COPY_AND_ASSIGN(TypeName) 58 59 // A macro to disallow all the implicit constructors, namely the 60 // default constructor, copy constructor and operator= functions. 61 // 62 // This should be used in the private: declarations for a class 63 // that wants to prevent anyone from instantiating it. This is 64 // especially useful for classes containing only static methods. 65 #undef DISALLOW_IMPLICIT_CONSTRUCTORS 66 #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \ 67 TypeName(); \ 68 DISALLOW_COPY_AND_ASSIGN(TypeName) 69 70 // The arraysize(arr) macro returns the # of elements in an array arr. 71 // The expression is a compile-time constant, and therefore can be 72 // used in defining new arrays, for example. If you use arraysize on 73 // a pointer by mistake, you will get a compile-time error. 74 // 75 // One caveat is that arraysize() doesn't accept any array of an 76 // anonymous type or a type defined inside a function. In these rare 77 // cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below. This is 78 // due to a limitation in C++'s template system. The limitation might 79 // eventually be removed, but it hasn't happened yet. 80 81 // This template function declaration is used in defining arraysize. 82 // Note that the function doesn't need an implementation, as we only 83 // use its type. 84 template <typename T, size_t N> 85 char (&ArraySizeHelper(T (&array)[N]))[N]; 86 87 // That gcc wants both of these prototypes seems mysterious. VC, for 88 // its part, can't decide which to use (another mystery). Matching of 89 // template overloads: the final frontier. 90 #ifndef _MSC_VER 91 template <typename T, size_t N> 92 char (&ArraySizeHelper(const T (&array)[N]))[N]; 93 #endif 94 95 #define arraysize(array) (sizeof(ArraySizeHelper(array))) 96 97 // ARRAYSIZE_UNSAFE performs essentially the same calculation as arraysize, 98 // but can be used on anonymous types or types defined inside 99 // functions. It's less safe than arraysize as it accepts some 100 // (although not all) pointers. Therefore, you should use arraysize 101 // whenever possible. 102 // 103 // The expression ARRAYSIZE_UNSAFE(a) is a compile-time constant of type 104 // size_t. 105 // 106 // ARRAYSIZE_UNSAFE catches a few type errors. If you see a compiler error 107 // 108 // "warning: division by zero in ..." 109 // 110 // when using ARRAYSIZE_UNSAFE, you are (wrongfully) giving it a pointer. 111 // You should only use ARRAYSIZE_UNSAFE on statically allocated arrays. 112 // 113 // The following comments are on the implementation details, and can 114 // be ignored by the users. 115 // 116 // ARRAYSIZE_UNSAFE(arr) works by inspecting sizeof(arr) (the # of bytes in 117 // the array) and sizeof(*(arr)) (the # of bytes in one array 118 // element). If the former is divisible by the latter, perhaps arr is 119 // indeed an array, in which case the division result is the # of 120 // elements in the array. Otherwise, arr cannot possibly be an array, 121 // and we generate a compiler error to prevent the code from 122 // compiling. 123 // 124 // Since the size of bool is implementation-defined, we need to cast 125 // !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final 126 // result has type size_t. 127 // 128 // This macro is not perfect as it wrongfully accepts certain 129 // pointers, namely where the pointer size is divisible by the pointee 130 // size. Since all our code has to go through a 32-bit compiler, 131 // where a pointer is 4 bytes, this means all pointers to a type whose 132 // size is 3 or greater than 4 will be (righteously) rejected. 133 134 #define ARRAYSIZE_UNSAFE(a) \ 135 ((sizeof(a) / sizeof(*(a))) / \ 136 static_cast<size_t>(!(sizeof(a) % sizeof(*(a))))) 137 138 // Use implicit_cast as a safe version of static_cast or const_cast 139 // for upcasting in the type hierarchy (i.e. casting a pointer to Foo 140 // to a pointer to SuperclassOfFoo or casting a pointer to Foo to 141 // a const pointer to Foo). 142 // When you use implicit_cast, the compiler checks that the cast is safe. 143 // Such explicit implicit_casts are necessary in surprisingly many 144 // situations where C++ demands an exact type match instead of an 145 // argument type convertable to a target type. 146 // 147 // The From type can be inferred, so the preferred syntax for using 148 // implicit_cast is the same as for static_cast etc.: 149 // 150 // implicit_cast<ToType>(expr) 151 // 152 // implicit_cast would have been part of the C++ standard library, 153 // but the proposal was submitted too late. It will probably make 154 // its way into the language in the future. 155 template <typename To, typename From> 156 inline To implicit_cast(From const& f) { 157 return f; 158 } 159 160 // The COMPILE_ASSERT macro (below) creates an otherwise-unused typedef. This 161 // triggers compiler warnings with gcc 4.8 and higher, so mark the typedef 162 // as permissibly-unused to disable the warnings. 163 #if defined(__GNUC__) 164 # define COMPILE_ASSERT_UNUSED_ATTRIBUTE __attribute__((unused)) 165 #else 166 # define COMPILE_ASSERT_UNUSED_ATTRIBUTE /* nothing */ 167 #endif 168 169 // The COMPILE_ASSERT macro can be used to verify that a compile time 170 // expression is true. For example, you could use it to verify the 171 // size of a static array: 172 // 173 // COMPILE_ASSERT(ARRAYSIZE_UNSAFE(content_type_names) == CONTENT_NUM_TYPES, 174 // content_type_names_incorrect_size); 175 // 176 // or to make sure a struct is smaller than a certain size: 177 // 178 // COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large); 179 // 180 // The second argument to the macro is the name of the variable. If 181 // the expression is false, most compilers will issue a warning/error 182 // containing the name of the variable. 183 184 // Avoid multiple definitions for webrtc 185 #if !defined(COMPILE_ASSERT) 186 template <bool> 187 struct CompileAssert {}; 188 189 # define COMPILE_ASSERT(expr, msg) \ 190 typedef CompileAssert<(bool(expr))> \ 191 msg[bool(expr) ? 1 : -1] COMPILE_ASSERT_UNUSED_ATTRIBUTE 192 #endif 193 194 // Implementation details of COMPILE_ASSERT: 195 // 196 // - COMPILE_ASSERT works by defining an array type that has -1 197 // elements (and thus is invalid) when the expression is false. 198 // 199 // - The simpler definition 200 // 201 // #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1] 202 // 203 // does not work, as gcc supports variable-length arrays whose sizes 204 // are determined at run-time (this is gcc's extension and not part 205 // of the C++ standard). As a result, gcc fails to reject the 206 // following code with the simple definition: 207 // 208 // int foo; 209 // COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is 210 // // not a compile-time constant. 211 // 212 // - By using the type CompileAssert<(bool(expr))>, we ensures that 213 // expr is a compile-time constant. (Template arguments must be 214 // determined at compile-time.) 215 // 216 // - The outter parentheses in CompileAssert<(bool(expr))> are necessary 217 // to work around a bug in gcc 3.4.4 and 4.0.1. If we had written 218 // 219 // CompileAssert<bool(expr)> 220 // 221 // instead, these compilers will refuse to compile 222 // 223 // COMPILE_ASSERT(5 > 0, some_message); 224 // 225 // (They seem to think the ">" in "5 > 0" marks the end of the 226 // template argument list.) 227 // 228 // - The array size is (bool(expr) ? 1 : -1), instead of simply 229 // 230 // ((expr) ? 1 : -1). 231 // 232 // This is to avoid running into a bug in MS VC 7.1, which 233 // causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1. 234 235 // MetatagId refers to metatag-id that we assign to 236 // each metatag <name, value> pair.. 237 typedef uint32_t MetatagId; 238 239 // Argument type used in interfaces that can optionally take ownership 240 // of a passed in argument. If TAKE_OWNERSHIP is passed, the called 241 // object takes ownership of the argument. Otherwise it does not. 242 enum Ownership { DO_NOT_TAKE_OWNERSHIP, TAKE_OWNERSHIP }; 243 244 // The following enum should be used only as a constructor argument to indicate 245 // that the variable has static storage class, and that the constructor should 246 // do nothing to its state. It indicates to the reader that it is legal to 247 // declare a static instance of the class, provided the constructor is given 248 // the base::LINKER_INITIALIZED argument. Normally, it is unsafe to declare a 249 // static variable that has a constructor or a destructor because invocation 250 // order is undefined. However, IF the type can be initialized by filling with 251 // zeroes (which the loader does for static variables), AND the destructor also 252 // does nothing to the storage, AND there are no virtual methods, then a 253 // constructor declared as 254 // explicit MyClass(base::LinkerInitialized x) {} 255 // and invoked as 256 // static MyClass my_variable_name(base::LINKER_INITIALIZED); 257 namespace base { 258 enum LinkerInitialized { LINKER_INITIALIZED }; 259 } // namespace base 260 261 #endif // BASE_BASICTYPES_H_