• 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 // The original source code is from:
6 // https://code.google.com/p/libphonenumber/source/browse/trunk/cpp/src/phonenumbers/base/basictypes.h?r=621
7 
8 #if I18N_ADDRESSINPUT_USE_BASICTYPES_OVERRIDE
9 
10 // If building libaddressinput in an environment where there already is another
11 // implementation of the basictypes.h header file (like in Chromium), then pass
12 // the command line flag -DI18N_ADDRESSINPUT_USE_BASICTYPES_OVERRIDE=1 to the
13 // compiler and provide a file named basictypes_override.h, in a location where
14 // the compiler will look for it, which provides the desired implementation.
15 
16 #include "basictypes_override.h"
17 
18 #else
19 
20 #ifndef I18N_ADDRESSINPUT_UTIL_BASICTYPES_H_
21 #define I18N_ADDRESSINPUT_UTIL_BASICTYPES_H_
22 
23 #include <climits>         // So we can set the bounds of our types
24 #include <cstddef>         // For size_t
25 
26 #if !defined(_WIN32)
27 // stdint.h is part of C99 but MSVC doesn't have it.
28 #include <stdint.h>         // For intptr_t.
29 #endif
30 
31 #ifdef INT64_MAX
32 
33 // INT64_MAX is defined if C99 stdint.h is included; use the
34 // native types if available.
35 typedef int8_t int8;
36 typedef int16_t int16;
37 typedef int32_t int32;
38 typedef int64_t int64;
39 typedef uint8_t uint8;
40 typedef uint16_t uint16;
41 typedef uint32_t uint32;
42 typedef uint64_t uint64;
43 
44 const uint8  kuint8max  = UINT8_MAX;
45 const uint16 kuint16max = UINT16_MAX;
46 const uint32 kuint32max = UINT32_MAX;
47 const uint64 kuint64max = UINT64_MAX;
48 const  int8  kint8min   = INT8_MIN;
49 const  int8  kint8max   = INT8_MAX;
50 const  int16 kint16min  = INT16_MIN;
51 const  int16 kint16max  = INT16_MAX;
52 const  int32 kint32min  = INT32_MIN;
53 const  int32 kint32max  = INT32_MAX;
54 const  int64 kint64min  = INT64_MIN;
55 const  int64 kint64max  = INT64_MAX;
56 
57 #else // !INT64_MAX
58 
59 typedef signed char         int8;
60 typedef short               int16;
61 // TODO: Remove these type guards.  These are to avoid conflicts with
62 // obsolete/protypes.h in the Gecko SDK.
63 #ifndef _INT32
64 #define _INT32
65 typedef int                 int32;
66 #endif
67 
68 // The NSPR system headers define 64-bit as |long| when possible.  In order to
69 // not have typedef mismatches, we do the same on LP64.
70 #if __LP64__
71 typedef long                int64;
72 #else
73 typedef long long           int64;
74 #endif
75 
76 // NOTE: unsigned types are DANGEROUS in loops and other arithmetical
77 // places.  Use the signed types unless your variable represents a bit
78 // pattern (eg a hash value) or you really need the extra bit.  Do NOT
79 // use 'unsigned' to express "this value should always be positive";
80 // use assertions for this.
81 
82 typedef unsigned char      uint8;
83 typedef unsigned short     uint16;
84 // TODO: Remove these type guards.  These are to avoid conflicts with
85 // obsolete/protypes.h in the Gecko SDK.
86 #ifndef _UINT32
87 #define _UINT32
88 typedef unsigned int       uint32;
89 #endif
90 
91 // See the comment above about NSPR and 64-bit.
92 #if __LP64__
93 typedef unsigned long uint64;
94 #else
95 typedef unsigned long long uint64;
96 #endif
97 
98 #endif // !INT64_MAX
99 
100 typedef signed char         schar;
101 
102 // A type to represent a Unicode code-point value. As of Unicode 4.0,
103 // such values require up to 21 bits.
104 // (For type-checking on pointers, make this explicitly signed,
105 // and it should always be the signed version of whatever int32 is.)
106 typedef signed int         char32;
107 
108 // A macro to disallow the copy constructor and operator= functions
109 // This should be used in the private: declarations for a class
110 #if !defined(DISALLOW_COPY_AND_ASSIGN)
111 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
112   TypeName(const TypeName&);               \
113   void operator=(const TypeName&)
114 #endif
115 
116 // The arraysize(arr) macro returns the # of elements in an array arr.
117 // The expression is a compile-time constant, and therefore can be
118 // used in defining new arrays, for example.  If you use arraysize on
119 // a pointer by mistake, you will get a compile-time error.
120 //
121 // One caveat is that arraysize() doesn't accept any array of an
122 // anonymous type or a type defined inside a function.  In these rare
123 // cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below.  This is
124 // due to a limitation in C++'s template system.  The limitation might
125 // eventually be removed, but it hasn't happened yet.
126 
127 // This template function declaration is used in defining arraysize.
128 // Note that the function doesn't need an implementation, as we only
129 // use its type.
130 template <typename T, size_t N>
131 char (&ArraySizeHelper(T (&array)[N]))[N];
132 
133 // That gcc wants both of these prototypes seems mysterious. VC, for
134 // its part, can't decide which to use (another mystery). Matching of
135 // template overloads: the final frontier.
136 #ifndef _MSC_VER
137 template <typename T, size_t N>
138 char (&ArraySizeHelper(const T (&array)[N]))[N];
139 #endif
140 
141 #if !defined(arraysize)
142 #define arraysize(array) (sizeof(ArraySizeHelper(array)))
143 #endif
144 
145 // ARRAYSIZE_UNSAFE performs essentially the same calculation as arraysize,
146 // but can be used on anonymous types or types defined inside
147 // functions.  It's less safe than arraysize as it accepts some
148 // (although not all) pointers.  Therefore, you should use arraysize
149 // whenever possible.
150 //
151 // The expression ARRAYSIZE_UNSAFE(a) is a compile-time constant of type
152 // size_t.
153 //
154 // ARRAYSIZE_UNSAFE catches a few type errors.  If you see a compiler error
155 //
156 //   "warning: division by zero in ..."
157 //
158 // when using ARRAYSIZE_UNSAFE, you are (wrongfully) giving it a pointer.
159 // You should only use ARRAYSIZE_UNSAFE on statically allocated arrays.
160 //
161 // The following comments are on the implementation details, and can
162 // be ignored by the users.
163 //
164 // ARRAYSIZE_UNSAFE(arr) works by inspecting sizeof(arr) (the # of bytes in
165 // the array) and sizeof(*(arr)) (the # of bytes in one array
166 // element).  If the former is divisible by the latter, perhaps arr is
167 // indeed an array, in which case the division result is the # of
168 // elements in the array.  Otherwise, arr cannot possibly be an array,
169 // and we generate a compiler error to prevent the code from
170 // compiling.
171 //
172 // Since the size of bool is implementation-defined, we need to cast
173 // !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final
174 // result has type size_t.
175 //
176 // This macro is not perfect as it wrongfully accepts certain
177 // pointers, namely where the pointer size is divisible by the pointee
178 // size.  Since all our code has to go through a 32-bit compiler,
179 // where a pointer is 4 bytes, this means all pointers to a type whose
180 // size is 3 or greater than 4 will be (righteously) rejected.
181 
182 #if !defined(ARRAYSIZE_UNSAFE)
183 #define ARRAYSIZE_UNSAFE(a) \
184   ((sizeof(a) / sizeof(*(a))) / \
185    static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
186 #endif
187 
188 // The COMPILE_ASSERT macro can be used to verify that a compile time
189 // expression is true. For example, you could use it to verify the
190 // size of a static array:
191 //
192 //   COMPILE_ASSERT(ARRAYSIZE_UNSAFE(content_type_names) == CONTENT_NUM_TYPES,
193 //                  content_type_names_incorrect_size);
194 //
195 // or to make sure a struct is smaller than a certain size:
196 //
197 //   COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
198 //
199 // The second argument to the macro is the name of the variable. If
200 // the expression is false, most compilers will issue a warning/error
201 // containing the name of the variable.
202 
203 template <bool>
204 struct CompileAssert {
205 };
206 
207 #if !defined(COMPILE_ASSERT)
208 #define COMPILE_ASSERT(expr, msg) \
209   typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
210 #endif
211 
212 #endif  // I18N_ADDRESSINPUT_UTIL_BASICTYPES_H_
213 #endif  // I18N_ADDRESSINPUT_USE_BASICTYPES_OVERRIDE
214