• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2010 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.
4 
5 #ifndef BASE_BASICTYPES_H_
6 #define BASE_BASICTYPES_H_
7 #pragma once
8 
9 #include <limits.h>         // So we can set the bounds of our types
10 #include <stddef.h>         // For size_t
11 #include <string.h>         // for memcpy
12 
13 #ifndef COMPILER_MSVC
14 // stdint.h is part of C99 but MSVC doesn't have it.
15 #include <stdint.h>         // For intptr_t.
16 #endif
17 
18 #ifdef INT64_MAX
19 
20 // INT64_MAX is defined if C99 stdint.h is included; use the
21 // native types if available.
22 typedef int8_t int8;
23 typedef int16_t int16;
24 typedef int32_t int32;
25 typedef int64_t int64;
26 typedef uint8_t uint8;
27 typedef uint16_t uint16;
28 typedef uint32_t uint32;
29 typedef uint64_t uint64;
30 
31 const uint8  kuint8max  = UINT8_MAX;
32 const uint16 kuint16max = UINT16_MAX;
33 const uint32 kuint32max = UINT32_MAX;
34 const uint64 kuint64max = UINT64_MAX;
35 const  int8  kint8min   = INT8_MIN;
36 const  int8  kint8max   = INT8_MAX;
37 const  int16 kint16min  = INT16_MIN;
38 const  int16 kint16max  = INT16_MAX;
39 const  int32 kint32min  = INT32_MIN;
40 const  int32 kint32max  = INT32_MAX;
41 const  int64 kint64min  = INT64_MIN;
42 const  int64 kint64max  = INT64_MAX;
43 
44 #else // !INT64_MAX
45 
46 typedef signed char         int8;
47 typedef short               int16;
48 // TODO: Remove these type guards.  These are to avoid conflicts with
49 // obsolete/protypes.h in the Gecko SDK.
50 #ifndef _INT32
51 #define _INT32
52 typedef int                 int32;
53 #endif
54 
55 // The NSPR system headers define 64-bit as |long| when possible.  In order to
56 // not have typedef mismatches, we do the same on LP64.
57 #if __LP64__
58 typedef long                int64;
59 #else
60 typedef long long           int64;
61 #endif
62 
63 // NOTE: unsigned types are DANGEROUS in loops and other arithmetical
64 // places.  Use the signed types unless your variable represents a bit
65 // pattern (eg a hash value) or you really need the extra bit.  Do NOT
66 // use 'unsigned' to express "this value should always be positive";
67 // use assertions for this.
68 
69 typedef unsigned char      uint8;
70 typedef unsigned short     uint16;
71 // TODO: Remove these type guards.  These are to avoid conflicts with
72 // obsolete/protypes.h in the Gecko SDK.
73 #ifndef _UINT32
74 #define _UINT32
75 typedef unsigned int       uint32;
76 #endif
77 
78 // See the comment above about NSPR and 64-bit.
79 #if __LP64__
80 typedef unsigned long uint64;
81 #else
82 typedef unsigned long long uint64;
83 #endif
84 
85 #endif // !INT64_MAX
86 
87 typedef signed char         schar;
88 
89 // A type to represent a Unicode code-point value. As of Unicode 4.0,
90 // such values require up to 21 bits.
91 // (For type-checking on pointers, make this explicitly signed,
92 // and it should always be the signed version of whatever int32 is.)
93 typedef signed int         char32;
94 
95 // A macro to disallow the copy constructor and operator= functions
96 // This should be used in the private: declarations for a class
97 #if !defined(DISALLOW_COPY_AND_ASSIGN)
98 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
99   TypeName(const TypeName&);               \
100   void operator=(const TypeName&)
101 #endif
102 
103 // An older, deprecated, politically incorrect name for the above.
104 // NOTE: The usage of this macro was baned from our code base, but some
105 // third_party libraries are yet using it.
106 // TODO(tfarina): Figure out how to fix the usage of this macro in the
107 // third_party libraries and get rid of it.
108 #if !defined(DISALLOW_EVIL_CONSTRUCTORS)
109 #define DISALLOW_EVIL_CONSTRUCTORS(TypeName) DISALLOW_COPY_AND_ASSIGN(TypeName)
110 #endif
111 
112 // A macro to disallow all the implicit constructors, namely the
113 // default constructor, copy constructor and operator= functions.
114 //
115 // This should be used in the private: declarations for a class
116 // that wants to prevent anyone from instantiating it. This is
117 // especially useful for classes containing only static methods.
118 #if !defined(DISALLOW_IMPLICIT_CONSTRUCTORS)
119 #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
120   TypeName();                                    \
121   DISALLOW_COPY_AND_ASSIGN(TypeName)
122 #endif
123 
124 // The arraysize(arr) macro returns the # of elements in an array arr.
125 // The expression is a compile-time constant, and therefore can be
126 // used in defining new arrays, for example.  If you use arraysize on
127 // a pointer by mistake, you will get a compile-time error.
128 //
129 // One caveat is that arraysize() doesn't accept any array of an
130 // anonymous type or a type defined inside a function.  In these rare
131 // cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below.  This is
132 // due to a limitation in C++'s template system.  The limitation might
133 // eventually be removed, but it hasn't happened yet.
134 
135 // This template function declaration is used in defining arraysize.
136 // Note that the function doesn't need an implementation, as we only
137 // use its type.
138 template <typename T, size_t N>
139 char (&ArraySizeHelper(T (&array)[N]))[N];
140 
141 // That gcc wants both of these prototypes seems mysterious. VC, for
142 // its part, can't decide which to use (another mystery). Matching of
143 // template overloads: the final frontier.
144 #ifndef _MSC_VER
145 template <typename T, size_t N>
146 char (&ArraySizeHelper(const T (&array)[N]))[N];
147 #endif
148 
149 #if !defined(arraysize)
150 #define arraysize(array) (sizeof(ArraySizeHelper(array)))
151 #endif
152 
153 // ARRAYSIZE_UNSAFE performs essentially the same calculation as arraysize,
154 // but can be used on anonymous types or types defined inside
155 // functions.  It's less safe than arraysize as it accepts some
156 // (although not all) pointers.  Therefore, you should use arraysize
157 // whenever possible.
158 //
159 // The expression ARRAYSIZE_UNSAFE(a) is a compile-time constant of type
160 // size_t.
161 //
162 // ARRAYSIZE_UNSAFE catches a few type errors.  If you see a compiler error
163 //
164 //   "warning: division by zero in ..."
165 //
166 // when using ARRAYSIZE_UNSAFE, you are (wrongfully) giving it a pointer.
167 // You should only use ARRAYSIZE_UNSAFE on statically allocated arrays.
168 //
169 // The following comments are on the implementation details, and can
170 // be ignored by the users.
171 //
172 // ARRAYSIZE_UNSAFE(arr) works by inspecting sizeof(arr) (the # of bytes in
173 // the array) and sizeof(*(arr)) (the # of bytes in one array
174 // element).  If the former is divisible by the latter, perhaps arr is
175 // indeed an array, in which case the division result is the # of
176 // elements in the array.  Otherwise, arr cannot possibly be an array,
177 // and we generate a compiler error to prevent the code from
178 // compiling.
179 //
180 // Since the size of bool is implementation-defined, we need to cast
181 // !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final
182 // result has type size_t.
183 //
184 // This macro is not perfect as it wrongfully accepts certain
185 // pointers, namely where the pointer size is divisible by the pointee
186 // size.  Since all our code has to go through a 32-bit compiler,
187 // where a pointer is 4 bytes, this means all pointers to a type whose
188 // size is 3 or greater than 4 will be (righteously) rejected.
189 
190 #if !defined(ARRAYSIZE_UNSAFE)
191 #define ARRAYSIZE_UNSAFE(a) \
192   ((sizeof(a) / sizeof(*(a))) / \
193    static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
194 #endif
195 
196 // Use implicit_cast as a safe version of static_cast or const_cast
197 // for upcasting in the type hierarchy (i.e. casting a pointer to Foo
198 // to a pointer to SuperclassOfFoo or casting a pointer to Foo to
199 // a const pointer to Foo).
200 // When you use implicit_cast, the compiler checks that the cast is safe.
201 // Such explicit implicit_casts are necessary in surprisingly many
202 // situations where C++ demands an exact type match instead of an
203 // argument type convertable to a target type.
204 //
205 // The From type can be inferred, so the preferred syntax for using
206 // implicit_cast is the same as for static_cast etc.:
207 //
208 //   implicit_cast<ToType>(expr)
209 //
210 // implicit_cast would have been part of the C++ standard library,
211 // but the proposal was submitted too late.  It will probably make
212 // its way into the language in the future.
213 template<typename To, typename From>
implicit_cast(From const & f)214 inline To implicit_cast(From const &f) {
215   return f;
216 }
217 
218 // The COMPILE_ASSERT macro can be used to verify that a compile time
219 // expression is true. For example, you could use it to verify the
220 // size of a static array:
221 //
222 //   COMPILE_ASSERT(ARRAYSIZE_UNSAFE(content_type_names) == CONTENT_NUM_TYPES,
223 //                  content_type_names_incorrect_size);
224 //
225 // or to make sure a struct is smaller than a certain size:
226 //
227 //   COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
228 //
229 // The second argument to the macro is the name of the variable. If
230 // the expression is false, most compilers will issue a warning/error
231 // containing the name of the variable.
232 
233 #if __cplusplus >= 201103L
234 
235 // Under C++11, just use static_assert.
236 #define COMPILE_ASSERT(expr, msg) static_assert(expr, #msg)
237 
238 #else
239 
240 template <bool>
241 struct CompileAssert {
242 };
243 
244 // Annotate a variable indicating it's ok if the variable is not used.
245 // (Typically used to silence a compiler warning when the assignment
246 // is important for some other reason.)
247 // Use like:
248 //   int x ALLOW_UNUSED = ...;
249 #if defined(__GNUC__)
250 #define ALLOW_UNUSED __attribute__((unused))
251 #else
252 #define ALLOW_UNUSED
253 #endif
254 
255 #define COMPILE_ASSERT(expr, msg) \
256   typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] ALLOW_UNUSED
257 
258 // Implementation details of COMPILE_ASSERT:
259 //
260 // - COMPILE_ASSERT works by defining an array type that has -1
261 //   elements (and thus is invalid) when the expression is false.
262 //
263 // - The simpler definition
264 //
265 //     #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1]
266 //
267 //   does not work, as gcc supports variable-length arrays whose sizes
268 //   are determined at run-time (this is gcc's extension and not part
269 //   of the C++ standard).  As a result, gcc fails to reject the
270 //   following code with the simple definition:
271 //
272 //     int foo;
273 //     COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is
274 //                               // not a compile-time constant.
275 //
276 // - By using the type CompileAssert<(bool(expr))>, we ensures that
277 //   expr is a compile-time constant.  (Template arguments must be
278 //   determined at compile-time.)
279 //
280 // - The outer parentheses in CompileAssert<(bool(expr))> are necessary
281 //   to work around a bug in gcc 3.4.4 and 4.0.1.  If we had written
282 //
283 //     CompileAssert<bool(expr)>
284 //
285 //   instead, these compilers will refuse to compile
286 //
287 //     COMPILE_ASSERT(5 > 0, some_message);
288 //
289 //   (They seem to think the ">" in "5 > 0" marks the end of the
290 //   template argument list.)
291 //
292 // - The array size is (bool(expr) ? 1 : -1), instead of simply
293 //
294 //     ((expr) ? 1 : -1).
295 //
296 //   This is to avoid running into a bug in MS VC 7.1, which
297 //   causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
298 
299 #endif
300 
301 // Used to explicitly mark the return value of a function as unused. If you are
302 // really sure you don't want to do anything with the return value of a function
303 // that has been marked WARN_UNUSED_RESULT, wrap it with this. Example:
304 //
305 //   scoped_ptr<MyType> my_var = ...;
306 //   if (TakeOwnership(my_var.get()) == SUCCESS)
307 //     ignore_result(my_var.release());
308 //
309 template<typename T>
ignore_result(const T &)310 inline void ignore_result(const T&) {
311 }
312 
313 // The following enum should be used only as a constructor argument to indicate
314 // that the variable has static storage class, and that the constructor should
315 // do nothing to its state.  It indicates to the reader that it is legal to
316 // declare a static instance of the class, provided the constructor is given
317 // the base::LINKER_INITIALIZED argument.  Normally, it is unsafe to declare a
318 // static variable that has a constructor or a destructor because invocation
319 // order is undefined.  However, IF the type can be initialized by filling with
320 // zeroes (which the loader does for static variables), AND the destructor also
321 // does nothing to the storage, AND there are no virtual methods, then a
322 // constructor declared as
323 //       explicit MyClass(base::LinkerInitialized x) {}
324 // and invoked as
325 //       static MyClass my_variable_name(base::LINKER_INITIALIZED);
326 namespace base {
327 enum LinkerInitialized { LINKER_INITIALIZED };
328 }  // base
329 
330 #endif  // BASE_BASICTYPES_H_
331