1// Copyright 2001 - 2003 Google Inc. All Rights Reserved 2 3#ifndef BASE_BASICTYPES_H__ 4#define BASE_BASICTYPES_H__ 5 6typedef unsigned char uint8; 7typedef unsigned short uint16; 8typedef unsigned int uint32; 9 10const uint8 kuint8max = (( uint8) 0xFF); 11const uint32 kuint32max = ((uint32) 0xFFFFFFFF); 12 13// The arraysize(arr) macro returns the # of elements in an array arr. 14// The expression is a compile-time constant, and therefore can be 15// used in defining new arrays, for example. If you use arraysize on 16// a pointer by mistake, you will get a compile-time error. 17// 18// One caveat is that arraysize() doesn't accept any array of an 19// anonymous type or a type defined inside a function. In these rare 20// cases, you have to use the unsafe ARRAYSIZE() macro below. This is 21// due to a limitation in C++'s template system. The limitation might 22// eventually be removed, but it hasn't happened yet. 23 24// This template function declaration is used in defining arraysize. 25// Note that the function doesn't need an implementation, as we only 26// use its type. 27template <typename T, size_t N> 28char (&ArraySizeHelper(T (&array)[N]))[N]; 29 30// That gcc wants both of these prototypes seems mysterious. VC, for 31// its part, can't decide which to use (another mystery). Matching of 32// template overloads: the final frontier. 33#ifndef _MSC_VER 34template <typename T, size_t N> 35char (&ArraySizeHelper(const T (&array)[N]))[N]; 36#endif 37 38#define arraysize(array) (sizeof(ArraySizeHelper(array))) 39 40// ARRAYSIZE performs essentially the same calculation as arraysize, 41// but can be used on anonymous types or types defined inside 42// functions. It's less safe than arraysize as it accepts some 43// (although not all) pointers. Therefore, you should use arraysize 44// whenever possible. 45// 46// The expression ARRAYSIZE(a) is a compile-time constant of type 47// size_t. 48// 49// ARRAYSIZE catches a few type errors. If you see a compiler error 50// 51// "warning: division by zero in ..." 52// 53// when using ARRAYSIZE, you are (wrongfully) giving it a pointer. 54// You should only use ARRAYSIZE on statically allocated arrays. 55// 56// The following comments are on the implementation details, and can 57// be ignored by the users. 58// 59// ARRAYSIZE(arr) works by inspecting sizeof(arr) (the # of bytes in 60// the array) and sizeof(*(arr)) (the # of bytes in one array 61// element). If the former is divisible by the latter, perhaps arr is 62// indeed an array, in which case the division result is the # of 63// elements in the array. Otherwise, arr cannot possibly be an array, 64// and we generate a compiler error to prevent the code from 65// compiling. 66// 67// Since the size of bool is implementation-defined, we need to cast 68// !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final 69// result has type size_t. 70// 71// This macro is not perfect as it wrongfully accepts certain 72// pointers, namely where the pointer size is divisible by the pointee 73// size. Since all our code has to go through a 32-bit compiler, 74// where a pointer is 4 bytes, this means all pointers to a type whose 75// size is 3 or greater than 4 will be (righteously) rejected. 76// 77// Starting with Visual C++ 2005, WinNT.h includes ARRAYSIZE. 78#define ARRAYSIZE_UNSAFE(a) \ 79 ((sizeof(a) / sizeof(*(a))) / \ 80 static_cast<size_t>(!(sizeof(a) % sizeof(*(a))))) 81 82// A macro to disallow the evil copy constructor and operator= functions 83// This should be used in the private: declarations for a class 84#define DISALLOW_EVIL_CONSTRUCTORS(TypeName) \ 85 TypeName(const TypeName&); \ 86 void operator=(const TypeName&) 87 88#endif // BASE_BASICTYPES_H__ 89