Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(371)

Side by Side Diff: third_party/chrome/base/basictypes.h

Issue 624713003: Keep only base/extractor.[cc|h]. (Closed) Base URL: https://chromium.googlesource.com/external/omaha.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/c99/include/inttypes.h ('k') | third_party/chrome/base/port.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2006-2008 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
8 #include <assert.h> // for use with down_cast<>
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 #include "base/port.h" // Types that only need exist on certain systems
14
15 #ifndef COMPILER_MSVC
16 // stdint.h is part of C99 but MSVC doesn't have it.
17 #include <stdint.h> // For intptr_t.
18 #endif
19
20 typedef signed char schar;
21 typedef signed char int8;
22 typedef short int16;
23 // TODO(mbelshe) Remove these type guards. These are
24 // temporary to avoid conflicts with npapi.h.
25 #ifndef _INT32
26 #define _INT32
27 typedef int int32;
28 #endif
29 typedef long long int64;
30
31 // NOTE: unsigned types are DANGEROUS in loops and other arithmetical
32 // places. Use the signed types unless your variable represents a bit
33 // pattern (eg a hash value) or you really need the extra bit. Do NOT
34 // use 'unsigned' to express "this value should always be positive";
35 // use assertions for this.
36
37 typedef unsigned char uint8;
38 typedef unsigned short uint16;
39 // TODO(mbelshe) Remove these type guards. These are
40 // temporary to avoid conflicts with npapi.h.
41 #ifndef _UINT32
42 #define _UINT32
43 typedef unsigned int uint32;
44 #endif
45 typedef unsigned long long uint64;
46
47 // A type to represent a Unicode code-point value. As of Unicode 4.0,
48 // such values require up to 21 bits.
49 // (For type-checking on pointers, make this explicitly signed,
50 // and it should always be the signed version of whatever int32 is.)
51 typedef signed int char32;
52
53 const uint8 kuint8max = (( uint8) 0xFF);
54 const uint16 kuint16max = ((uint16) 0xFFFF);
55 const uint32 kuint32max = ((uint32) 0xFFFFFFFF);
56 const uint64 kuint64max = ((uint64) GG_LONGLONG(0xFFFFFFFFFFFFFFFF));
57 const int8 kint8min = (( int8) 0x80);
58 const int8 kint8max = (( int8) 0x7F);
59 const int16 kint16min = (( int16) 0x8000);
60 const int16 kint16max = (( int16) 0x7FFF);
61 const int32 kint32min = (( int32) 0x80000000);
62 const int32 kint32max = (( int32) 0x7FFFFFFF);
63 const int64 kint64min = (( int64) GG_LONGLONG(0x8000000000000000));
64 const int64 kint64max = (( int64) GG_LONGLONG(0x7FFFFFFFFFFFFFFF));
65
66 // id for odp categories
67 typedef uint32 CatId;
68 const CatId kIllegalCatId = static_cast<CatId>(0);
69
70 typedef uint32 TermId;
71 const TermId kIllegalTermId = static_cast<TermId>(0);
72
73 typedef uint32 HostId;
74 const HostId kIllegalHostId = static_cast<HostId>(0);
75
76 typedef uint32 DomainId;
77 const DomainId kIllegalDomainId = static_cast<DomainId>(0);
78
79 // A macro to disallow the copy constructor and operator= functions
80 // This should be used in the private: declarations for a class
81 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
82 TypeName(const TypeName&); \
83 void operator=(const TypeName&)
84
85 // An older, deprecated, politically incorrect name for the above.
86 #define DISALLOW_EVIL_CONSTRUCTORS(TypeName) DISALLOW_COPY_AND_ASSIGN(TypeName)
87
88 // A macro to disallow all the implicit constructors, namely the
89 // default constructor, copy constructor and operator= functions.
90 //
91 // This should be used in the private: declarations for a class
92 // that wants to prevent anyone from instantiating it. This is
93 // especially useful for classes containing only static methods.
94 #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
95 TypeName(); \
96 DISALLOW_COPY_AND_ASSIGN(TypeName)
97
98 // The arraysize(arr) macro returns the # of elements in an array arr.
99 // The expression is a compile-time constant, and therefore can be
100 // used in defining new arrays, for example. If you use arraysize on
101 // a pointer by mistake, you will get a compile-time error.
102 //
103 // One caveat is that arraysize() doesn't accept any array of an
104 // anonymous type or a type defined inside a function. In these rare
105 // cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below. This is
106 // due to a limitation in C++'s template system. The limitation might
107 // eventually be removed, but it hasn't happened yet.
108
109 // This template function declaration is used in defining arraysize.
110 // Note that the function doesn't need an implementation, as we only
111 // use its type.
112 template <typename T, size_t N>
113 char (&ArraySizeHelper(T (&array)[N]))[N];
114
115 // That gcc wants both of these prototypes seems mysterious. VC, for
116 // its part, can't decide which to use (another mystery). Matching of
117 // template overloads: the final frontier.
118 #ifndef _MSC_VER
119 template <typename T, size_t N>
120 char (&ArraySizeHelper(const T (&array)[N]))[N];
121 #endif
122
123 #define arraysize(array) (sizeof(ArraySizeHelper(array)))
124
125 // ARRAYSIZE_UNSAFE performs essentially the same calculation as arraysize,
126 // but can be used on anonymous types or types defined inside
127 // functions. It's less safe than arraysize as it accepts some
128 // (although not all) pointers. Therefore, you should use arraysize
129 // whenever possible.
130 //
131 // The expression ARRAYSIZE_UNSAFE(a) is a compile-time constant of type
132 // size_t.
133 //
134 // ARRAYSIZE_UNSAFE catches a few type errors. If you see a compiler error
135 //
136 // "warning: division by zero in ..."
137 //
138 // when using ARRAYSIZE_UNSAFE, you are (wrongfully) giving it a pointer.
139 // You should only use ARRAYSIZE_UNSAFE on statically allocated arrays.
140 //
141 // The following comments are on the implementation details, and can
142 // be ignored by the users.
143 //
144 // ARRAYSIZE_UNSAFE(arr) works by inspecting sizeof(arr) (the # of bytes in
145 // the array) and sizeof(*(arr)) (the # of bytes in one array
146 // element). If the former is divisible by the latter, perhaps arr is
147 // indeed an array, in which case the division result is the # of
148 // elements in the array. Otherwise, arr cannot possibly be an array,
149 // and we generate a compiler error to prevent the code from
150 // compiling.
151 //
152 // Since the size of bool is implementation-defined, we need to cast
153 // !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final
154 // result has type size_t.
155 //
156 // This macro is not perfect as it wrongfully accepts certain
157 // pointers, namely where the pointer size is divisible by the pointee
158 // size. Since all our code has to go through a 32-bit compiler,
159 // where a pointer is 4 bytes, this means all pointers to a type whose
160 // size is 3 or greater than 4 will be (righteously) rejected.
161
162 #define ARRAYSIZE_UNSAFE(a) \
163 ((sizeof(a) / sizeof(*(a))) / \
164 static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
165
166
167 // Use implicit_cast as a safe version of static_cast or const_cast
168 // for upcasting in the type hierarchy (i.e. casting a pointer to Foo
169 // to a pointer to SuperclassOfFoo or casting a pointer to Foo to
170 // a const pointer to Foo).
171 // When you use implicit_cast, the compiler checks that the cast is safe.
172 // Such explicit implicit_casts are necessary in surprisingly many
173 // situations where C++ demands an exact type match instead of an
174 // argument type convertable to a target type.
175 //
176 // The From type can be inferred, so the preferred syntax for using
177 // implicit_cast is the same as for static_cast etc.:
178 //
179 // implicit_cast<ToType>(expr)
180 //
181 // implicit_cast would have been part of the C++ standard library,
182 // but the proposal was submitted too late. It will probably make
183 // its way into the language in the future.
184 template<typename To, typename From>
185 inline To implicit_cast(From const &f) {
186 return f;
187 }
188
189
190 // When you upcast (that is, cast a pointer from type Foo to type
191 // SuperclassOfFoo), it's fine to use implicit_cast<>, since upcasts
192 // always succeed. When you downcast (that is, cast a pointer from
193 // type Foo to type SubclassOfFoo), static_cast<> isn't safe, because
194 // how do you know the pointer is really of type SubclassOfFoo? It
195 // could be a bare Foo, or of type DifferentSubclassOfFoo. Thus,
196 // when you downcast, you should use this macro. In debug mode, we
197 // use dynamic_cast<> to double-check the downcast is legal (we die
198 // if it's not). In normal mode, we do the efficient static_cast<>
199 // instead. Thus, it's important to test in debug mode to make sure
200 // the cast is legal!
201 // This is the only place in the code we should use dynamic_cast<>.
202 // In particular, you SHOULDN'T be using dynamic_cast<> in order to
203 // do RTTI (eg code like this:
204 // if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);
205 // if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);
206 // You should design the code some other way not to need this.
207
208 template<typename To, typename From> // use like this: down_cast<T*>(foo);
209 inline To down_cast(From* f) { // so we only accept pointers
210 // Ensures that To is a sub-type of From *. This test is here only
211 // for compile-time type checking, and has no overhead in an
212 // optimized build at run-time, as it will be optimized away
213 // completely.
214 if (false) {
215 implicit_cast<From*, To>(0);
216 }
217
218 assert(f == NULL || dynamic_cast<To>(f) != NULL); // RTTI: debug mode only!
219 return static_cast<To>(f);
220 }
221
222 // The COMPILE_ASSERT macro can be used to verify that a compile time
223 // expression is true. For example, you could use it to verify the
224 // size of a static array:
225 //
226 // COMPILE_ASSERT(ARRAYSIZE_UNSAFE(content_type_names) == CONTENT_NUM_TYPES,
227 // content_type_names_incorrect_size);
228 //
229 // or to make sure a struct is smaller than a certain size:
230 //
231 // COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
232 //
233 // The second argument to the macro is the name of the variable. If
234 // the expression is false, most compilers will issue a warning/error
235 // containing the name of the variable.
236
237 template <bool>
238 struct CompileAssert {
239 };
240
241 #undef COMPILE_ASSERT
242 #define COMPILE_ASSERT(expr, msg) \
243 typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
244
245 // Implementation details of COMPILE_ASSERT:
246 //
247 // - COMPILE_ASSERT works by defining an array type that has -1
248 // elements (and thus is invalid) when the expression is false.
249 //
250 // - The simpler definition
251 //
252 // #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1]
253 //
254 // does not work, as gcc supports variable-length arrays whose sizes
255 // are determined at run-time (this is gcc's extension and not part
256 // of the C++ standard). As a result, gcc fails to reject the
257 // following code with the simple definition:
258 //
259 // int foo;
260 // COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is
261 // // not a compile-time constant.
262 //
263 // - By using the type CompileAssert<(bool(expr))>, we ensures that
264 // expr is a compile-time constant. (Template arguments must be
265 // determined at compile-time.)
266 //
267 // - The outter parentheses in CompileAssert<(bool(expr))> are necessary
268 // to work around a bug in gcc 3.4.4 and 4.0.1. If we had written
269 //
270 // CompileAssert<bool(expr)>
271 //
272 // instead, these compilers will refuse to compile
273 //
274 // COMPILE_ASSERT(5 > 0, some_message);
275 //
276 // (They seem to think the ">" in "5 > 0" marks the end of the
277 // template argument list.)
278 //
279 // - The array size is (bool(expr) ? 1 : -1), instead of simply
280 //
281 // ((expr) ? 1 : -1).
282 //
283 // This is to avoid running into a bug in MS VC 7.1, which
284 // causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
285
286
287 // MetatagId refers to metatag-id that we assign to
288 // each metatag <name, value> pair..
289 typedef uint32 MetatagId;
290
291 // Argument type used in interfaces that can optionally take ownership
292 // of a passed in argument. If TAKE_OWNERSHIP is passed, the called
293 // object takes ownership of the argument. Otherwise it does not.
294 enum Ownership {
295 DO_NOT_TAKE_OWNERSHIP,
296 TAKE_OWNERSHIP
297 };
298
299 // Use these as the mlock_bytes parameter to MLock and MLockGeneral
300 enum { MLOCK_ALL = -1, MLOCK_NONE = 0 };
301
302 // Helper routine to avoid buggy code like the following:
303 // if (pos + N < end) ...
304 // If pos is large enough, "pos + N" may overflow. For example,
305 // pos==0xfffff000 and N==1MB.
306 //
307 // PointerRangeSize(a,b) returns the size of the range [a,b-1]
308 inline size_t PointerRangeSize(const char* start, const char* end) {
309 assert(start <= end);
310 return end - start;
311 }
312
313 // bit_cast<Dest,Source> is a template function that implements the
314 // equivalent of "*reinterpret_cast<Dest*>(&source)". We need this in
315 // very low-level functions like the protobuf library and fast math
316 // support.
317 //
318 // float f = 3.14159265358979;
319 // int i = bit_cast<int32>(f);
320 // // i = 0x40490fdb
321 //
322 // The classical address-casting method is:
323 //
324 // // WRONG
325 // float f = 3.14159265358979; // WRONG
326 // int i = * reinterpret_cast<int*>(&f); // WRONG
327 //
328 // The address-casting method actually produces undefined behavior
329 // according to ISO C++ specification section 3.10 -15 -. Roughly, this
330 // section says: if an object in memory has one type, and a program
331 // accesses it with a different type, then the result is undefined
332 // behavior for most values of "different type".
333 //
334 // This is true for any cast syntax, either *(int*)&f or
335 // *reinterpret_cast<int*>(&f). And it is particularly true for
336 // conversions betweeen integral lvalues and floating-point lvalues.
337 //
338 // The purpose of 3.10 -15- is to allow optimizing compilers to assume
339 // that expressions with different types refer to different memory. gcc
340 // 4.0.1 has an optimizer that takes advantage of this. So a
341 // non-conforming program quietly produces wildly incorrect output.
342 //
343 // The problem is not the use of reinterpret_cast. The problem is type
344 // punning: holding an object in memory of one type and reading its bits
345 // back using a different type.
346 //
347 // The C++ standard is more subtle and complex than this, but that
348 // is the basic idea.
349 //
350 // Anyways ...
351 //
352 // bit_cast<> calls memcpy() which is blessed by the standard,
353 // especially by the example in section 3.9 . Also, of course,
354 // bit_cast<> wraps up the nasty logic in one place.
355 //
356 // Fortunately memcpy() is very fast. In optimized mode, with a
357 // constant size, gcc 2.95.3, gcc 4.0.1, and msvc 7.1 produce inline
358 // code with the minimal amount of data movement. On a 32-bit system,
359 // memcpy(d,s,4) compiles to one load and one store, and memcpy(d,s,8)
360 // compiles to two loads and two stores.
361 //
362 // I tested this code with gcc 2.95.3, gcc 4.0.1, icc 8.1, and msvc 7.1.
363 //
364 // WARNING: if Dest or Source is a non-POD type, the result of the memcpy
365 // is likely to surprise you.
366
367 template <class Dest, class Source>
368 inline Dest bit_cast(const Source& source) {
369 // Compile time assertion: sizeof(Dest) == sizeof(Source)
370 // A compile error here means your Dest and Source have different sizes.
371 typedef char VerifySizesAreEqual [sizeof(Dest) == sizeof(Source) ? 1 : -1];
372
373 Dest dest;
374 memcpy(&dest, &source, sizeof(dest));
375 return dest;
376 }
377
378 // The following enum should be used only as a constructor argument to indicate
379 // that the variable has static storage class, and that the constructor should
380 // do nothing to its state. It indicates to the reader that it is legal to
381 // declare a static instance of the class, provided the constructor is given
382 // the base::LINKER_INITIALIZED argument. Normally, it is unsafe to declare a
383 // static variable that has a constructor or a destructor because invocation
384 // order is undefined. However, IF the type can be initialized by filling with
385 // zeroes (which the loader does for static variables), AND the destructor also
386 // does nothing to the storage, AND there are no virtual methods, then a
387 // constructor declared as
388 // explicit MyClass(base::LinkerInitialized x) {}
389 // and invoked as
390 // static MyClass my_variable_name(base::LINKER_INITIALIZED);
391 namespace base {
392 enum LinkerInitialized { LINKER_INITIALIZED };
393 } // base
394
395
396 #endif // BASE_BASICTYPES_H_
OLDNEW
« no previous file with comments | « third_party/c99/include/inttypes.h ('k') | third_party/chrome/base/port.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698