Update V8 to version 4.7.53.
[chromium-blink-merge.git] / third_party / cld / base / basictypes.h
blobaf28256b5d3055fadda942d7be1e1a2dc0ee19be
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.
5 #ifndef BASE_BASICTYPES_H_
6 #define BASE_BASICTYPES_H_
8 #include <limits.h> // So we can set the bounds of our types
9 #include <stddef.h> // For size_t
10 #include <string.h> // for memcpy
12 #include "base/port.h" // Types that only need exist on certain systems
14 #ifndef COMPILER_MSVC
15 // stdint.h is part of C99 but MSVC doesn't have it.
16 #include <stdint.h> // For intptr_t.
17 #endif
19 typedef signed char schar;
20 typedef signed char int8;
21 typedef short int16;
22 // TODO(mbelshe) Remove these type guards. These are
23 // temporary to avoid conflicts with npapi.h.
24 #ifndef _INT32
25 #define _INT32
26 typedef int int32;
27 #endif
29 // The NSPR system headers define 64-bit as |long| when possible. In order to
30 // not have typedef mismatches, we do the same on LP64.
31 #if __LP64__
32 typedef long int64;
33 #else
34 typedef long long int64;
35 #endif
37 // NOTE: unsigned types are DANGEROUS in loops and other arithmetical
38 // places. Use the signed types unless your variable represents a bit
39 // pattern (eg a hash value) or you really need the extra bit. Do NOT
40 // use 'unsigned' to express "this value should always be positive";
41 // use assertions for this.
43 typedef unsigned char uint8;
44 typedef unsigned short uint16;
45 // TODO(mbelshe) Remove these type guards. These are
46 // temporary to avoid conflicts with npapi.h.
47 #ifndef _UINT32
48 #define _UINT32
49 typedef unsigned int uint32;
50 #endif
52 // See the comment above about NSPR and 64-bit.
53 #if __LP64__
54 typedef unsigned long uint64;
55 #else
56 typedef unsigned long long uint64;
57 #endif
59 // A type to represent a Unicode code-point value. As of Unicode 4.0,
60 // such values require up to 21 bits.
61 // (For type-checking on pointers, make this explicitly signed,
62 // and it should always be the signed version of whatever int32 is.)
63 typedef signed int char32;
65 const uint8 kuint8max = (( uint8) 0xFF);
66 const uint16 kuint16max = ((uint16) 0xFFFF);
67 const uint32 kuint32max = ((uint32) 0xFFFFFFFF);
68 const uint64 kuint64max = ((uint64) GG_LONGLONG(0xFFFFFFFFFFFFFFFF));
69 const int8 kint8min = (( int8) 0x80);
70 const int8 kint8max = (( int8) 0x7F);
71 const int16 kint16min = (( int16) 0x8000);
72 const int16 kint16max = (( int16) 0x7FFF);
73 const int32 kint32min = (( int32) 0x80000000);
74 const int32 kint32max = (( int32) 0x7FFFFFFF);
75 const int64 kint64min = (( int64) GG_LONGLONG(0x8000000000000000));
76 const int64 kint64max = (( int64) GG_LONGLONG(0x7FFFFFFFFFFFFFFF));
78 // A macro to disallow the copy constructor and operator= functions
79 // This should be used in the private: declarations for a class
80 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
81 TypeName(const TypeName&); \
82 void operator=(const TypeName&)
84 // An older, deprecated, politically incorrect name for the above.
85 #define DISALLOW_EVIL_CONSTRUCTORS(TypeName) DISALLOW_COPY_AND_ASSIGN(TypeName)
87 // A macro to disallow all the implicit constructors, namely the
88 // default constructor, copy constructor and operator= functions.
90 // This should be used in the private: declarations for a class
91 // that wants to prevent anyone from instantiating it. This is
92 // especially useful for classes containing only static methods.
93 #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
94 TypeName(); \
95 DISALLOW_COPY_AND_ASSIGN(TypeName)
97 // The arraysize(arr) macro returns the # of elements in an array arr.
98 // The expression is a compile-time constant, and therefore can be
99 // used in defining new arrays, for example. If you use arraysize on
100 // a pointer by mistake, you will get a compile-time error.
102 // This template function declaration is used in defining arraysize.
103 // Note that the function doesn't need an implementation, as we only
104 // use its type.
105 template <typename T, size_t N>
106 char (&ArraySizeHelper(T (&array)[N]))[N];
108 // That gcc wants both of these prototypes seems mysterious. VC, for
109 // its part, can't decide which to use (another mystery). Matching of
110 // template overloads: the final frontier.
111 #ifndef _MSC_VER
112 template <typename T, size_t N>
113 char (&ArraySizeHelper(const T (&array)[N]))[N];
114 #endif
116 #define arraysize(array) (sizeof(ArraySizeHelper(array)))
119 // Use implicit_cast as a safe version of static_cast or const_cast
120 // for upcasting in the type hierarchy (i.e. casting a pointer to Foo
121 // to a pointer to SuperclassOfFoo or casting a pointer to Foo to
122 // a const pointer to Foo).
123 // When you use implicit_cast, the compiler checks that the cast is safe.
124 // Such explicit implicit_casts are necessary in surprisingly many
125 // situations where C++ demands an exact type match instead of an
126 // argument type convertable to a target type.
128 // The From type can be inferred, so the preferred syntax for using
129 // implicit_cast is the same as for static_cast etc.:
131 // implicit_cast<ToType>(expr)
133 // implicit_cast would have been part of the C++ standard library,
134 // but the proposal was submitted too late. It will probably make
135 // its way into the language in the future.
136 template<typename To, typename From>
137 inline To implicit_cast(From const &f) {
138 return f;
141 // The COMPILE_ASSERT macro can be used to verify that a compile time
142 // expression is true. For example, you could use it to verify the
143 // size of a static array:
145 // COMPILE_ASSERT(arraysize(content_type_names) == CONTENT_NUM_TYPES,
146 // content_type_names_incorrect_size);
148 // or to make sure a struct is smaller than a certain size:
150 // COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
152 // The second argument to the macro is the name of the variable. If
153 // the expression is false, most compilers will issue a warning/error
154 // containing the name of the variable.
156 template <bool>
157 struct CompileAssert {
160 #undef COMPILE_ASSERT
161 #define COMPILE_ASSERT(expr, msg) \
162 typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
164 // Implementation details of COMPILE_ASSERT:
166 // - COMPILE_ASSERT works by defining an array type that has -1
167 // elements (and thus is invalid) when the expression is false.
169 // - The simpler definition
171 // #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1]
173 // does not work, as gcc supports variable-length arrays whose sizes
174 // are determined at run-time (this is gcc's extension and not part
175 // of the C++ standard). As a result, gcc fails to reject the
176 // following code with the simple definition:
178 // int foo;
179 // COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is
180 // // not a compile-time constant.
182 // - By using the type CompileAssert<(bool(expr))>, we ensures that
183 // expr is a compile-time constant. (Template arguments must be
184 // determined at compile-time.)
186 // - The outter parentheses in CompileAssert<(bool(expr))> are necessary
187 // to work around a bug in gcc 3.4.4 and 4.0.1. If we had written
189 // CompileAssert<bool(expr)>
191 // instead, these compilers will refuse to compile
193 // COMPILE_ASSERT(5 > 0, some_message);
195 // (They seem to think the ">" in "5 > 0" marks the end of the
196 // template argument list.)
198 // - The array size is (bool(expr) ? 1 : -1), instead of simply
200 // ((expr) ? 1 : -1).
202 // This is to avoid running into a bug in MS VC 7.1, which
203 // causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
206 // MetatagId refers to metatag-id that we assign to
207 // each metatag <name, value> pair..
208 typedef uint32 MetatagId;
210 // Argument type used in interfaces that can optionally take ownership
211 // of a passed in argument. If TAKE_OWNERSHIP is passed, the called
212 // object takes ownership of the argument. Otherwise it does not.
213 enum Ownership {
214 DO_NOT_TAKE_OWNERSHIP,
215 TAKE_OWNERSHIP
218 // bit_cast<Dest,Source> is a template function that implements the
219 // equivalent of "*reinterpret_cast<Dest*>(&source)". We need this in
220 // very low-level functions like the protobuf library and fast math
221 // support.
223 // float f = 3.14159265358979;
224 // int i = bit_cast<int32>(f);
225 // // i = 0x40490fdb
227 // The classical address-casting method is:
229 // // WRONG
230 // float f = 3.14159265358979; // WRONG
231 // int i = * reinterpret_cast<int*>(&f); // WRONG
233 // The address-casting method actually produces undefined behavior
234 // according to ISO C++ specification section 3.10 -15 -. Roughly, this
235 // section says: if an object in memory has one type, and a program
236 // accesses it with a different type, then the result is undefined
237 // behavior for most values of "different type".
239 // This is true for any cast syntax, either *(int*)&f or
240 // *reinterpret_cast<int*>(&f). And it is particularly true for
241 // conversions betweeen integral lvalues and floating-point lvalues.
243 // The purpose of 3.10 -15- is to allow optimizing compilers to assume
244 // that expressions with different types refer to different memory. gcc
245 // 4.0.1 has an optimizer that takes advantage of this. So a
246 // non-conforming program quietly produces wildly incorrect output.
248 // The problem is not the use of reinterpret_cast. The problem is type
249 // punning: holding an object in memory of one type and reading its bits
250 // back using a different type.
252 // The C++ standard is more subtle and complex than this, but that
253 // is the basic idea.
255 // Anyways ...
257 // bit_cast<> calls memcpy() which is blessed by the standard,
258 // especially by the example in section 3.9 . Also, of course,
259 // bit_cast<> wraps up the nasty logic in one place.
261 // Fortunately memcpy() is very fast. In optimized mode, with a
262 // constant size, gcc 2.95.3, gcc 4.0.1, and msvc 7.1 produce inline
263 // code with the minimal amount of data movement. On a 32-bit system,
264 // memcpy(d,s,4) compiles to one load and one store, and memcpy(d,s,8)
265 // compiles to two loads and two stores.
267 // I tested this code with gcc 2.95.3, gcc 4.0.1, icc 8.1, and msvc 7.1.
269 // WARNING: if Dest or Source is a non-POD type, the result of the memcpy
270 // is likely to surprise you.
272 template <class Dest, class Source>
273 inline Dest bit_cast(const Source& source) {
274 // Compile time assertion: sizeof(Dest) == sizeof(Source)
275 // A compile error here means your Dest and Source have different sizes.
276 typedef char VerifySizesAreEqual [sizeof(Dest) == sizeof(Source) ? 1 : -1];
278 Dest dest;
279 memcpy(&dest, &source, sizeof(dest));
280 return dest;
283 // The following enum should be used only as a constructor argument to indicate
284 // that the variable has static storage class, and that the constructor should
285 // do nothing to its state. It indicates to the reader that it is legal to
286 // declare a static instance of the class, provided the constructor is given
287 // the base::LINKER_INITIALIZED argument. Normally, it is unsafe to declare a
288 // static variable that has a constructor or a destructor because invocation
289 // order is undefined. However, IF the type can be initialized by filling with
290 // zeroes (which the loader does for static variables), AND the destructor also
291 // does nothing to the storage, AND there are no virtual methods, then a
292 // constructor declared as
293 // explicit MyClass(base::LinkerInitialized x) {}
294 // and invoked as
295 // static MyClass my_variable_name(base::LINKER_INITIALIZED);
296 namespace base {
297 enum LinkerInitialized { LINKER_INITIALIZED };
298 } // base
300 // UnaligndLoad32 is put here instead of base/port.h to
301 // avoid the circular dependency between port.h and basictypes.h
302 // ARM does not support unaligned memory access.
303 #if defined(ARCH_CPU_X86_FAMILY)
304 // x86 and x86-64 can perform unaligned loads/stores directly;
305 inline uint32 UnalignedLoad32(const void* p) {
306 return *reinterpret_cast<const uint32*>(p);
308 #else
309 #define NEED_ALIGNED_LOADS
310 // If target architecture does not support unaligned loads and stores,
311 // use memcpy version of UNALIGNED_LOAD32.
312 inline uint32 UnalignedLoad32(const void* p) {
313 uint32 t;
314 memcpy(&t, reinterpret_cast<const uint8*>(p), sizeof(t));
315 return t;
318 #endif
319 #endif // BASE_BASICTYPES_H_