15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2010 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef BASE_BASICTYPES_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define BASE_BASICTYPES_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <limits.h>         // So we can set the bounds of our types
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stddef.h>         // For size_t
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string.h>         // for memcpy
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/port.h"    // Types that only need exist on certain systems
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef COMPILER_MSVC
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// stdint.h is part of C99 but MSVC doesn't have it.
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stdint.h>         // For intptr_t.
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef signed char         schar;
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef signed char         int8;
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef short               int16;
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TODO(mbelshe) Remove these type guards.  These are
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//               temporary to avoid conflicts with npapi.h.
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef _INT32
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define _INT32
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef int                 int32;
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The NSPR system headers define 64-bit as |long| when possible.  In order to
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// not have typedef mismatches, we do the same on LP64.
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if __LP64__
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef long                int64;
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef long long           int64;
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// NOTE: unsigned types are DANGEROUS in loops and other arithmetical
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// places.  Use the signed types unless your variable represents a bit
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// pattern (eg a hash value) or you really need the extra bit.  Do NOT
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// use 'unsigned' to express "this value should always be positive";
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// use assertions for this.
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef unsigned char      uint8;
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef unsigned short     uint16;
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TODO(mbelshe) Remove these type guards.  These are
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//               temporary to avoid conflicts with npapi.h.
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef _UINT32
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define _UINT32
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef unsigned int       uint32;
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// See the comment above about NSPR and 64-bit.
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if __LP64__
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef unsigned long uint64;
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef unsigned long long uint64;
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A type to represent a Unicode code-point value. As of Unicode 4.0,
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// such values require up to 21 bits.
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (For type-checking on pointers, make this explicitly signed,
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// and it should always be the signed version of whatever int32 is.)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef signed int         char32;
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const uint8  kuint8max  = (( uint8) 0xFF);
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const uint16 kuint16max = ((uint16) 0xFFFF);
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const uint32 kuint32max = ((uint32) 0xFFFFFFFF);
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const uint64 kuint64max = ((uint64) GG_LONGLONG(0xFFFFFFFFFFFFFFFF));
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const  int8  kint8min   = ((  int8) 0x80);
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const  int8  kint8max   = ((  int8) 0x7F);
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const  int16 kint16min  = (( int16) 0x8000);
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const  int16 kint16max  = (( int16) 0x7FFF);
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const  int32 kint32min  = (( int32) 0x80000000);
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const  int32 kint32max  = (( int32) 0x7FFFFFFF);
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const  int64 kint64min  = (( int64) GG_LONGLONG(0x8000000000000000));
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const  int64 kint64max  = (( int64) GG_LONGLONG(0x7FFFFFFFFFFFFFFF));
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A macro to disallow the copy constructor and operator= functions
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This should be used in the private: declarations for a class
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TypeName(const TypeName&);               \
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void operator=(const TypeName&)
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// An older, deprecated, politically incorrect name for the above.
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DISALLOW_EVIL_CONSTRUCTORS(TypeName) DISALLOW_COPY_AND_ASSIGN(TypeName)
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A macro to disallow all the implicit constructors, namely the
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// default constructor, copy constructor and operator= functions.
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This should be used in the private: declarations for a class
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// that wants to prevent anyone from instantiating it. This is
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// especially useful for classes containing only static methods.
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TypeName();                                    \
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(TypeName)
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The arraysize(arr) macro returns the # of elements in an array arr.
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The expression is a compile-time constant, and therefore can be
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// used in defining new arrays, for example.  If you use arraysize on
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// a pointer by mistake, you will get a compile-time error.
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// One caveat is that arraysize() doesn't accept any array of an
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// anonymous type or a type defined inside a function.  In these rare
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below.  This is
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// due to a limitation in C++'s template system.  The limitation might
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// eventually be removed, but it hasn't happened yet.
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This template function declaration is used in defining arraysize.
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Note that the function doesn't need an implementation, as we only
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// use its type.
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template <typename T, size_t N>
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)char (&ArraySizeHelper(T (&array)[N]))[N];
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// That gcc wants both of these prototypes seems mysterious. VC, for
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// its part, can't decide which to use (another mystery). Matching of
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// template overloads: the final frontier.
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef _MSC_VER
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template <typename T, size_t N>
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)char (&ArraySizeHelper(const T (&array)[N]))[N];
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define arraysize(array) (sizeof(ArraySizeHelper(array)))
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ARRAYSIZE_UNSAFE performs essentially the same calculation as arraysize,
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// but can be used on anonymous types or types defined inside
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// functions.  It's less safe than arraysize as it accepts some
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (although not all) pointers.  Therefore, you should use arraysize
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// whenever possible.
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The expression ARRAYSIZE_UNSAFE(a) is a compile-time constant of type
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// size_t.
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ARRAYSIZE_UNSAFE catches a few type errors.  If you see a compiler error
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   "warning: division by zero in ..."
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// when using ARRAYSIZE_UNSAFE, you are (wrongfully) giving it a pointer.
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// You should only use ARRAYSIZE_UNSAFE on statically allocated arrays.
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The following comments are on the implementation details, and can
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// be ignored by the users.
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ARRAYSIZE_UNSAFE(arr) works by inspecting sizeof(arr) (the # of bytes in
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the array) and sizeof(*(arr)) (the # of bytes in one array
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// element).  If the former is divisible by the latter, perhaps arr is
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// indeed an array, in which case the division result is the # of
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// elements in the array.  Otherwise, arr cannot possibly be an array,
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// and we generate a compiler error to prevent the code from
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// compiling.
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Since the size of bool is implementation-defined, we need to cast
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// result has type size_t.
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This macro is not perfect as it wrongfully accepts certain
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// pointers, namely where the pointer size is divisible by the pointee
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// size.  Since all our code has to go through a 32-bit compiler,
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// where a pointer is 4 bytes, this means all pointers to a type whose
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// size is 3 or greater than 4 will be (righteously) rejected.
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define ARRAYSIZE_UNSAFE(a) \
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ((sizeof(a) / sizeof(*(a))) / \
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use implicit_cast as a safe version of static_cast or const_cast
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// for upcasting in the type hierarchy (i.e. casting a pointer to Foo
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// to a pointer to SuperclassOfFoo or casting a pointer to Foo to
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// a const pointer to Foo).
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// When you use implicit_cast, the compiler checks that the cast is safe.
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Such explicit implicit_casts are necessary in surprisingly many
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// situations where C++ demands an exact type match instead of an
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// argument type convertable to a target type.
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The From type can be inferred, so the preferred syntax for using
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// implicit_cast is the same as for static_cast etc.:
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   implicit_cast<ToType>(expr)
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// implicit_cast would have been part of the C++ standard library,
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// but the proposal was submitted too late.  It will probably make
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// its way into the language in the future.
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template<typename To, typename From>
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)inline To implicit_cast(From const &f) {
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return f;
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The COMPILE_ASSERT macro can be used to verify that a compile time
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// expression is true. For example, you could use it to verify the
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// size of a static array:
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   COMPILE_ASSERT(ARRAYSIZE_UNSAFE(content_type_names) == CONTENT_NUM_TYPES,
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                  content_type_names_incorrect_size);
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// or to make sure a struct is smaller than a certain size:
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The second argument to the macro is the name of the variable. If
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the expression is false, most compilers will issue a warning/error
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// containing the name of the variable.
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template <bool>
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct CompileAssert {
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef COMPILE_ASSERT
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define COMPILE_ASSERT(expr, msg) \
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Implementation details of COMPILE_ASSERT:
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// - COMPILE_ASSERT works by defining an array type that has -1
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   elements (and thus is invalid) when the expression is false.
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// - The simpler definition
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1]
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   does not work, as gcc supports variable-length arrays whose sizes
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   are determined at run-time (this is gcc's extension and not part
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   of the C++ standard).  As a result, gcc fails to reject the
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   following code with the simple definition:
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     int foo;
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                               // not a compile-time constant.
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// - By using the type CompileAssert<(bool(expr))>, we ensures that
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   expr is a compile-time constant.  (Template arguments must be
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   determined at compile-time.)
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// - The outter parentheses in CompileAssert<(bool(expr))> are necessary
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   to work around a bug in gcc 3.4.4 and 4.0.1.  If we had written
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     CompileAssert<bool(expr)>
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   instead, these compilers will refuse to compile
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     COMPILE_ASSERT(5 > 0, some_message);
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   (They seem to think the ">" in "5 > 0" marks the end of the
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   template argument list.)
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// - The array size is (bool(expr) ? 1 : -1), instead of simply
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     ((expr) ? 1 : -1).
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   This is to avoid running into a bug in MS VC 7.1, which
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// MetatagId refers to metatag-id that we assign to
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// each metatag <name, value> pair..
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef uint32 MetatagId;
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Argument type used in interfaces that can optionally take ownership
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// of a passed in argument.  If TAKE_OWNERSHIP is passed, the called
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// object takes ownership of the argument.  Otherwise it does not.
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)enum Ownership {
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DO_NOT_TAKE_OWNERSHIP,
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TAKE_OWNERSHIP
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// bit_cast<Dest,Source> is a template function that implements the
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// equivalent of "*reinterpret_cast<Dest*>(&source)".  We need this in
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// very low-level functions like the protobuf library and fast math
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// support.
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   float f = 3.14159265358979;
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   int i = bit_cast<int32>(f);
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   // i = 0x40490fdb
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The classical address-casting method is:
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   // WRONG
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   float f = 3.14159265358979;            // WRONG
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   int i = * reinterpret_cast<int*>(&f);  // WRONG
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The address-casting method actually produces undefined behavior
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// according to ISO C++ specification section 3.10 -15 -.  Roughly, this
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// section says: if an object in memory has one type, and a program
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// accesses it with a different type, then the result is undefined
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// behavior for most values of "different type".
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This is true for any cast syntax, either *(int*)&f or
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// *reinterpret_cast<int*>(&f).  And it is particularly true for
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// conversions betweeen integral lvalues and floating-point lvalues.
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The purpose of 3.10 -15- is to allow optimizing compilers to assume
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// that expressions with different types refer to different memory.  gcc
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 4.0.1 has an optimizer that takes advantage of this.  So a
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// non-conforming program quietly produces wildly incorrect output.
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The problem is not the use of reinterpret_cast.  The problem is type
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// punning: holding an object in memory of one type and reading its bits
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// back using a different type.
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The C++ standard is more subtle and complex than this, but that
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// is the basic idea.
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Anyways ...
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// bit_cast<> calls memcpy() which is blessed by the standard,
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// especially by the example in section 3.9 .  Also, of course,
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// bit_cast<> wraps up the nasty logic in one place.
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Fortunately memcpy() is very fast.  In optimized mode, with a
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// constant size, gcc 2.95.3, gcc 4.0.1, and msvc 7.1 produce inline
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// code with the minimal amount of data movement.  On a 32-bit system,
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// memcpy(d,s,4) compiles to one load and one store, and memcpy(d,s,8)
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// compiles to two loads and two stores.
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// I tested this code with gcc 2.95.3, gcc 4.0.1, icc 8.1, and msvc 7.1.
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// WARNING: if Dest or Source is a non-POD type, the result of the memcpy
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// is likely to surprise you.
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template <class Dest, class Source>
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)inline Dest bit_cast(const Source& source) {
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Compile time assertion: sizeof(Dest) == sizeof(Source)
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A compile error here means your Dest and Source have different sizes.
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef char VerifySizesAreEqual [sizeof(Dest) == sizeof(Source) ? 1 : -1];
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Dest dest;
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  memcpy(&dest, &source, sizeof(dest));
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return dest;
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The following enum should be used only as a constructor argument to indicate
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// that the variable has static storage class, and that the constructor should
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// do nothing to its state.  It indicates to the reader that it is legal to
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// declare a static instance of the class, provided the constructor is given
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the base::LINKER_INITIALIZED argument.  Normally, it is unsafe to declare a
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static variable that has a constructor or a destructor because invocation
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// order is undefined.  However, IF the type can be initialized by filling with
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// zeroes (which the loader does for static variables), AND the destructor also
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// does nothing to the storage, AND there are no virtual methods, then a
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// constructor declared as
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//       explicit MyClass(base::LinkerInitialized x) {}
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// and invoked as
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//       static MyClass my_variable_name(base::LINKER_INITIALIZED);
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base {
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)enum LinkerInitialized { LINKER_INITIALIZED };
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // base
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// UnaligndLoad32 is put here instead of base/port.h to
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// avoid the circular dependency between port.h and basictypes.h
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ARM does not support unaligned memory access.
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(ARCH_CPU_X86_FAMILY)
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// x86 and x86-64 can perform unaligned loads/stores directly;
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)inline uint32 UnalignedLoad32(const void* p) {
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return *reinterpret_cast<const uint32*>(p);
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define NEED_ALIGNED_LOADS
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If target architecture does not support unaligned loads and stores,
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// use memcpy version of UNALIGNED_LOAD32.
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)inline uint32 UnalignedLoad32(const void* p) {
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uint32 t;
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  memcpy(&t, reinterpret_cast<const uint8*>(p), sizeof(t));
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return t;
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // BASE_BASICTYPES_H_
367