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 I18N_PHONENUMBERS_BASE_BASICTYPES_H_
6 #define I18N_PHONENUMBERS_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
13 // stdint.h is part of C99 but MSVC doesn't have it.
14 #include <stdint.h> // For intptr_t.
18 namespace phonenumbers {
22 // INT64_MAX is defined if C99 stdint.h is included; use the
23 // native types if available.
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;
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;
48 typedef signed char int8;
50 // TODO: Remove these type guards. These are to avoid conflicts with
51 // obsolete/protypes.h in the Gecko SDK.
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.
62 typedef long long int64;
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.
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.
77 typedef unsigned int uint32;
80 // See the comment above about NSPR and 64-bit.
82 typedef unsigned long uint64;
84 typedef unsigned long long uint64;
89 typedef signed char schar;
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;
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&)
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.
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.
116 // This template function declaration is used in defining arraysize.
117 // Note that the function doesn't need an implementation, as we only
119 template <typename T, size_t N>
120 char (&ArraySizeHelper(T (&array)[N]))[N];
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.
126 template <typename T, size_t N>
127 char (&ArraySizeHelper(const T (&array)[N]))[N];
130 #if !defined(arraysize)
131 #define arraysize(array) (sizeof(ArraySizeHelper(array)))
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.
140 // The expression ARRAYSIZE_UNSAFE(a) is a compile-time constant of type
143 // ARRAYSIZE_UNSAFE catches a few type errors. If you see a compiler error
145 // "warning: division by zero in ..."
147 // when using ARRAYSIZE_UNSAFE, you are (wrongfully) giving it a pointer.
148 // You should only use ARRAYSIZE_UNSAFE on statically allocated arrays.
150 // The following comments are on the implementation details, and can
151 // be ignored by the users.
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
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.
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.
171 #if !defined(ARRAYSIZE_UNSAFE)
172 #define ARRAYSIZE_UNSAFE(a) \
173 ((sizeof(a) / sizeof(*(a))) / \
174 static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
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:
181 // COMPILE_ASSERT(ARRAYSIZE_UNSAFE(content_type_names) == CONTENT_NUM_TYPES,
182 // content_type_names_incorrect_size);
184 // or to make sure a struct is smaller than a certain size:
186 // COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
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.
193 struct CompileAssert {
196 #if !defined(COMPILE_ASSERT)
197 #if __cplusplus >= 201103L
198 #define COMPILE_ASSERT(expr, msg) static_assert(expr, #msg)
200 #define COMPILE_ASSERT(expr, msg) \
201 typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
205 } // namespace phonenumbers
208 #endif // I18N_PHONENUMBERS_BASE_BASICTYPES_H_