• 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 I18N_PHONENUMBERS_BASE_BASICTYPES_H_
6 #define I18N_PHONENUMBERS_BASE_BASICTYPES_H_
7 
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
11 
12 #if !defined(_WIN32)
13 // stdint.h is part of C99 but MSVC doesn't have it.
14 #include <stdint.h>         // For intptr_t.
15 #endif
16 
17 namespace i18n {
18 namespace phonenumbers {
19 
20 #ifdef INT64_MAX
21 
22 // INT64_MAX is defined if C99 stdint.h is included; use the
23 // native types if available.
24 typedef int8_t int8;
25 typedef int16_t int16;
26 typedef int32_t int32;
27 typedef int64_t int64;
28 typedef uint8_t uint8;
29 typedef uint16_t uint16;
30 typedef uint32_t uint32;
31 typedef uint64_t uint64;
32 
33 const uint8  kuint8max  = UINT8_MAX;
34 const uint16 kuint16max = UINT16_MAX;
35 const uint32 kuint32max = UINT32_MAX;
36 const uint64 kuint64max = UINT64_MAX;
37 const  int8  kint8min   = INT8_MIN;
38 const  int8  kint8max   = INT8_MAX;
39 const  int16 kint16min  = INT16_MIN;
40 const  int16 kint16max  = INT16_MAX;
41 const  int32 kint32min  = INT32_MIN;
42 const  int32 kint32max  = INT32_MAX;
43 const  int64 kint64min  = INT64_MIN;
44 const  int64 kint64max  = INT64_MAX;
45 
46 #else // !INT64_MAX
47 
48 typedef signed char         int8;
49 typedef short               int16;
50 // TODO: Remove these type guards.  These are to avoid conflicts with
51 // obsolete/protypes.h in the Gecko SDK.
52 #ifndef _INT32
53 #define _INT32
54 typedef int                 int32;
55 #endif
56 
57 // The NSPR system headers define 64-bit as |long| when possible.  In order to
58 // not have typedef mismatches, we do the same on LP64.
59 #if __LP64__
60 typedef long                int64;
61 #else
62 typedef long long           int64;
63 #endif
64 
65 // NOTE: unsigned types are DANGEROUS in loops and other arithmetical
66 // places.  Use the signed types unless your variable represents a bit
67 // pattern (eg a hash value) or you really need the extra bit.  Do NOT
68 // use 'unsigned' to express "this value should always be positive";
69 // use assertions for this.
70 
71 typedef unsigned char      uint8;
72 typedef unsigned short     uint16;
73 // TODO: Remove these type guards.  These are to avoid conflicts with
74 // obsolete/protypes.h in the Gecko SDK.
75 #ifndef _UINT32
76 #define _UINT32
77 typedef unsigned int       uint32;
78 #endif
79 
80 // See the comment above about NSPR and 64-bit.
81 #if __LP64__
82 typedef unsigned long uint64;
83 #else
84 typedef unsigned long long uint64;
85 #endif
86 
87 #endif // !INT64_MAX
88 
89 typedef signed char         schar;
90 
91 // A type to represent a Unicode code-point value. As of Unicode 4.0,
92 // such values require up to 21 bits.
93 // (For type-checking on pointers, make this explicitly signed,
94 // and it should always be the signed version of whatever int32 is.)
95 typedef signed int         char32;
96 
97 // A macro to disallow the copy constructor and operator= functions
98 // This should be used in the private: declarations for a class
99 #if !defined(DISALLOW_COPY_AND_ASSIGN)
100 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
101   TypeName(const TypeName&);               \
102   void operator=(const TypeName&)
103 #endif
104 
105 // The arraysize(arr) macro returns the # of elements in an array arr.
106 // The expression is a compile-time constant, and therefore can be
107 // used in defining new arrays, for example.  If you use arraysize on
108 // a pointer by mistake, you will get a compile-time error.
109 //
110 // One caveat is that arraysize() doesn't accept any array of an
111 // anonymous type or a type defined inside a function.  In these rare
112 // cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below.  This is
113 // due to a limitation in C++'s template system.  The limitation might
114 // eventually be removed, but it hasn't happened yet.
115 
116 // This template function declaration is used in defining arraysize.
117 // Note that the function doesn't need an implementation, as we only
118 // use its type.
119 template <typename T, size_t N>
120 char (&ArraySizeHelper(T (&array)[N]))[N];
121 
122 // That gcc wants both of these prototypes seems mysterious. VC, for
123 // its part, can't decide which to use (another mystery). Matching of
124 // template overloads: the final frontier.
125 #ifndef _MSC_VER
126 template <typename T, size_t N>
127 char (&ArraySizeHelper(const T (&array)[N]))[N];
128 #endif
129 
130 #if !defined(arraysize)
131 #define arraysize(array) (sizeof(ArraySizeHelper(array)))
132 #endif
133 
134 // ARRAYSIZE_UNSAFE performs essentially the same calculation as arraysize,
135 // but can be used on anonymous types or types defined inside
136 // functions.  It's less safe than arraysize as it accepts some
137 // (although not all) pointers.  Therefore, you should use arraysize
138 // whenever possible.
139 //
140 // The expression ARRAYSIZE_UNSAFE(a) is a compile-time constant of type
141 // size_t.
142 //
143 // ARRAYSIZE_UNSAFE catches a few type errors.  If you see a compiler error
144 //
145 //   "warning: division by zero in ..."
146 //
147 // when using ARRAYSIZE_UNSAFE, you are (wrongfully) giving it a pointer.
148 // You should only use ARRAYSIZE_UNSAFE on statically allocated arrays.
149 //
150 // The following comments are on the implementation details, and can
151 // be ignored by the users.
152 //
153 // ARRAYSIZE_UNSAFE(arr) works by inspecting sizeof(arr) (the # of bytes in
154 // the array) and sizeof(*(arr)) (the # of bytes in one array
155 // element).  If the former is divisible by the latter, perhaps arr is
156 // indeed an array, in which case the division result is the # of
157 // elements in the array.  Otherwise, arr cannot possibly be an array,
158 // and we generate a compiler error to prevent the code from
159 // compiling.
160 //
161 // Since the size of bool is implementation-defined, we need to cast
162 // !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final
163 // result has type size_t.
164 //
165 // This macro is not perfect as it wrongfully accepts certain
166 // pointers, namely where the pointer size is divisible by the pointee
167 // size.  Since all our code has to go through a 32-bit compiler,
168 // where a pointer is 4 bytes, this means all pointers to a type whose
169 // size is 3 or greater than 4 will be (righteously) rejected.
170 
171 #if !defined(ARRAYSIZE_UNSAFE)
172 #define ARRAYSIZE_UNSAFE(a) \
173   ((sizeof(a) / sizeof(*(a))) / \
174    static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
175 #endif
176 
177 // The COMPILE_ASSERT macro can be used to verify that a compile time
178 // expression is true. For example, you could use it to verify the
179 // size of a static array:
180 //
181 //   COMPILE_ASSERT(ARRAYSIZE_UNSAFE(content_type_names) == CONTENT_NUM_TYPES,
182 //                  content_type_names_incorrect_size);
183 //
184 // or to make sure a struct is smaller than a certain size:
185 //
186 //   COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
187 //
188 // The second argument to the macro is the name of the variable. If
189 // the expression is false, most compilers will issue a warning/error
190 // containing the name of the variable.
191 
192 #if __cplusplus >= 201103L
193 
194 // Under C++11, just use static_assert.
195 #define COMPILE_ASSERT(expr, msg) static_assert(expr, #msg)
196 
197 #else
198 
199 template <bool>
200 struct CompileAssert {
201 };
202 
203 // Annotate a variable indicating it's ok if the variable is not used.
204 // (Typically used to silence a compiler warning when the assignment
205 // is important for some other reason.)
206 // Use like:
207 //   int x ALLOW_UNUSED = ...;
208 #if defined(__GNUC__)
209 #define ALLOW_UNUSED __attribute__((unused))
210 #else
211 #define ALLOW_UNUSED
212 #endif
213 
214 #define COMPILE_ASSERT(expr, msg) \
215   typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] ALLOW_UNUSED
216 
217 // Implementation details of COMPILE_ASSERT:
218 //
219 // - COMPILE_ASSERT works by defining an array type that has -1
220 //   elements (and thus is invalid) when the expression is false.
221 //
222 // - The simpler definition
223 //
224 //     #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1]
225 //
226 //   does not work, as gcc supports variable-length arrays whose sizes
227 //   are determined at run-time (this is gcc's extension and not part
228 //   of the C++ standard).  As a result, gcc fails to reject the
229 //   following code with the simple definition:
230 //
231 //     int foo;
232 //     COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is
233 //                               // not a compile-time constant.
234 //
235 // - By using the type CompileAssert<(bool(expr))>, we ensures that
236 //   expr is a compile-time constant.  (Template arguments must be
237 //   determined at compile-time.)
238 //
239 // - The outer parentheses in CompileAssert<(bool(expr))> are necessary
240 //   to work around a bug in gcc 3.4.4 and 4.0.1.  If we had written
241 //
242 //     CompileAssert<bool(expr)>
243 //
244 //   instead, these compilers will refuse to compile
245 //
246 //     COMPILE_ASSERT(5 > 0, some_message);
247 //
248 //   (They seem to think the ">" in "5 > 0" marks the end of the
249 //   template argument list.)
250 //
251 // - The array size is (bool(expr) ? 1 : -1), instead of simply
252 //
253 //     ((expr) ? 1 : -1).
254 //
255 //   This is to avoid running into a bug in MS VC 7.1, which
256 //   causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
257 
258 #endif
259 
260 }  // namespace phonenumbers
261 }  // namespace i18n
262 
263 #endif  // I18N_PHONENUMBERS_BASE_BASICTYPES_H_
264