15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This file contains macros and macro-like constructs (e.g., templates) that
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// are commonly used throughout Chromium source. (It may also contain things
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// that are closely related to things that are commonly used that belong in this
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// file.)
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifndef BASE_MACROS_H_
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define BASE_MACROS_H_
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <stddef.h>  // For size_t.
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <string.h>  // For memcpy.
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/compiler_specific.h"  // For ALLOW_UNUSED.
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Put this in the private: declarations for a class to be uncopyable.
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define DISALLOW_COPY(TypeName) \
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  TypeName(const TypeName&)
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Put this in the private: declarations for a class to be unassignable.
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define DISALLOW_ASSIGN(TypeName) \
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void operator=(const TypeName&)
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// A macro to disallow the copy constructor and operator= functions
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This should be used in the private: declarations for a class
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  TypeName(const TypeName&);               \
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void operator=(const TypeName&)
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// An older, deprecated, politically incorrect name for the above.
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// NOTE: The usage of this macro was banned from our code base, but some
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// third_party libraries are yet using it.
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// TODO(tfarina): Figure out how to fix the usage of this macro in the
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// third_party libraries and get rid of it.
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define DISALLOW_EVIL_CONSTRUCTORS(TypeName) DISALLOW_COPY_AND_ASSIGN(TypeName)
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// A macro to disallow all the implicit constructors, namely the
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// default constructor, copy constructor and operator= functions.
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This should be used in the private: declarations for a class
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// that wants to prevent anyone from instantiating it. This is
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// especially useful for classes containing only static methods.
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  TypeName();                                    \
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(TypeName)
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// The arraysize(arr) macro returns the # of elements in an array arr.
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// The expression is a compile-time constant, and therefore can be
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// used in defining new arrays, for example.  If you use arraysize on
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// a pointer by mistake, you will get a compile-time error.
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// One caveat is that arraysize() doesn't accept any array of an
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// anonymous type or a type defined inside a function.  In these rare
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below.  This is
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// due to a limitation in C++'s template system.  The limitation might
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// eventually be removed, but it hasn't happened yet.
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This template function declaration is used in defining arraysize.
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Note that the function doesn't need an implementation, as we only
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// use its type.
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)template <typename T, size_t N>
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)char (&ArraySizeHelper(T (&array)[N]))[N];
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// That gcc wants both of these prototypes seems mysterious. VC, for
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// its part, can't decide which to use (another mystery). Matching of
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// template overloads: the final frontier.
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifndef _MSC_VER
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)template <typename T, size_t N>
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)char (&ArraySizeHelper(const T (&array)[N]))[N];
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define arraysize(array) (sizeof(ArraySizeHelper(array)))
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// ARRAYSIZE_UNSAFE performs essentially the same calculation as arraysize,
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// but can be used on anonymous types or types defined inside
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// functions.  It's less safe than arraysize as it accepts some
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// (although not all) pointers.  Therefore, you should use arraysize
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// whenever possible.
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// The expression ARRAYSIZE_UNSAFE(a) is a compile-time constant of type
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// size_t.
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// ARRAYSIZE_UNSAFE catches a few type errors.  If you see a compiler error
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   "warning: division by zero in ..."
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// when using ARRAYSIZE_UNSAFE, you are (wrongfully) giving it a pointer.
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// You should only use ARRAYSIZE_UNSAFE on statically allocated arrays.
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// The following comments are on the implementation details, and can
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// be ignored by the users.
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// ARRAYSIZE_UNSAFE(arr) works by inspecting sizeof(arr) (the # of bytes in
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// the array) and sizeof(*(arr)) (the # of bytes in one array
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// element).  If the former is divisible by the latter, perhaps arr is
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// indeed an array, in which case the division result is the # of
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// elements in the array.  Otherwise, arr cannot possibly be an array,
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// and we generate a compiler error to prevent the code from
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// compiling.
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Since the size of bool is implementation-defined, we need to cast
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// result has type size_t.
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This macro is not perfect as it wrongfully accepts certain
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// pointers, namely where the pointer size is divisible by the pointee
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// size.  Since all our code has to go through a 32-bit compiler,
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// where a pointer is 4 bytes, this means all pointers to a type whose
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// size is 3 or greater than 4 will be (righteously) rejected.
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define ARRAYSIZE_UNSAFE(a) \
1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ((sizeof(a) / sizeof(*(a))) / \
1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)   static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use implicit_cast as a safe version of static_cast or const_cast
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// for upcasting in the type hierarchy (i.e. casting a pointer to Foo
1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// to a pointer to SuperclassOfFoo or casting a pointer to Foo to
1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// a const pointer to Foo).
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// When you use implicit_cast, the compiler checks that the cast is safe.
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Such explicit implicit_casts are necessary in surprisingly many
1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// situations where C++ demands an exact type match instead of an
1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// argument type convertible to a target type.
1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// The From type can be inferred, so the preferred syntax for using
1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// implicit_cast is the same as for static_cast etc.:
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   implicit_cast<ToType>(expr)
1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// implicit_cast would have been part of the C++ standard library,
1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// but the proposal was submitted too late.  It will probably make
1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// its way into the language in the future.
1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)template<typename To, typename From>
1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)inline To implicit_cast(From const &f) {
1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return f;
1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// The COMPILE_ASSERT macro can be used to verify that a compile time
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// expression is true. For example, you could use it to verify the
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// size of a static array:
1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   COMPILE_ASSERT(ARRAYSIZE_UNSAFE(content_type_names) == CONTENT_NUM_TYPES,
1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//                  content_type_names_incorrect_size);
1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// or to make sure a struct is smaller than a certain size:
1485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
1495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
1505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
1515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// The second argument to the macro is the name of the variable. If
1525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// the expression is false, most compilers will issue a warning/error
1535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// containing the name of the variable.
1545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#undef COMPILE_ASSERT
1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define COMPILE_ASSERT(expr, msg) static_assert(expr, #msg)
1575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// bit_cast<Dest,Source> is a template function that implements the
1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// equivalent of "*reinterpret_cast<Dest*>(&source)".  We need this in
1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// very low-level functions like the protobuf library and fast math
1615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// support.
1625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
1635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   float f = 3.14159265358979;
1645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   int i = bit_cast<int32>(f);
1655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   // i = 0x40490fdb
1665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
1675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// The classical address-casting method is:
1685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
1695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   // WRONG
1705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   float f = 3.14159265358979;            // WRONG
1715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   int i = * reinterpret_cast<int*>(&f);  // WRONG
1725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
1735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// The address-casting method actually produces undefined behavior
1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// according to ISO C++ specification section 3.10 -15 -.  Roughly, this
1755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// section says: if an object in memory has one type, and a program
1765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// accesses it with a different type, then the result is undefined
1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// behavior for most values of "different type".
1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
1795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This is true for any cast syntax, either *(int*)&f or
1805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// *reinterpret_cast<int*>(&f).  And it is particularly true for
1815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// conversions between integral lvalues and floating-point lvalues.
1825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
1835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// The purpose of 3.10 -15- is to allow optimizing compilers to assume
1845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// that expressions with different types refer to different memory.  gcc
1855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// 4.0.1 has an optimizer that takes advantage of this.  So a
1865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// non-conforming program quietly produces wildly incorrect output.
1875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// The problem is not the use of reinterpret_cast.  The problem is type
1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// punning: holding an object in memory of one type and reading its bits
1905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// back using a different type.
1915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
1925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// The C++ standard is more subtle and complex than this, but that
1935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// is the basic idea.
1945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
1955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Anyways ...
1965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
1975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// bit_cast<> calls memcpy() which is blessed by the standard,
1985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// especially by the example in section 3.9 .  Also, of course,
1995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// bit_cast<> wraps up the nasty logic in one place.
2005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
2015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Fortunately memcpy() is very fast.  In optimized mode, with a
2025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// constant size, gcc 2.95.3, gcc 4.0.1, and msvc 7.1 produce inline
2035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// code with the minimal amount of data movement.  On a 32-bit system,
2045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// memcpy(d,s,4) compiles to one load and one store, and memcpy(d,s,8)
2055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// compiles to two loads and two stores.
2065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
2075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// I tested this code with gcc 2.95.3, gcc 4.0.1, icc 8.1, and msvc 7.1.
2085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
2095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// WARNING: if Dest or Source is a non-POD type, the result of the memcpy
2105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// is likely to surprise you.
2115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)template <class Dest, class Source>
2135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)inline Dest bit_cast(const Source& source) {
2145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  COMPILE_ASSERT(sizeof(Dest) == sizeof(Source), VerifySizesAreEqual);
2155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  Dest dest;
2175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  memcpy(&dest, &source, sizeof(dest));
2185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return dest;
2195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Used to explicitly mark the return value of a function as unused. If you are
2225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// really sure you don't want to do anything with the return value of a function
2235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// that has been marked WARN_UNUSED_RESULT, wrap it with this. Example:
2245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
2255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   scoped_ptr<MyType> my_var = ...;
2265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//   if (TakeOwnership(my_var.get()) == SUCCESS)
2275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//     ignore_result(my_var.release());
2285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
2295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)template<typename T>
2305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)inline void ignore_result(const T&) {
2315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// The following enum should be used only as a constructor argument to indicate
2345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// that the variable has static storage class, and that the constructor should
2355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// do nothing to its state.  It indicates to the reader that it is legal to
2365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// declare a static instance of the class, provided the constructor is given
2375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// the base::LINKER_INITIALIZED argument.  Normally, it is unsafe to declare a
2385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// static variable that has a constructor or a destructor because invocation
2395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// order is undefined.  However, IF the type can be initialized by filling with
2405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// zeroes (which the loader does for static variables), AND the destructor also
2415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// does nothing to the storage, AND there are no virtual methods, then a
2425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// constructor declared as
2435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//       explicit MyClass(base::LinkerInitialized x) {}
2445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// and invoked as
2455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//       static MyClass my_variable_name(base::LINKER_INITIALIZED);
2465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace base {
2475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)enum LinkerInitialized { LINKER_INITIALIZED };
2485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use these to declare and define a static local variable (static T;) so that
2505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// it is leaked so that its destructors are not called at exit. If you need
2515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// thread-safe initialization, use base/lazy_instance.h instead.
2525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define CR_DEFINE_STATIC_LOCAL(type, name, arguments) \
2535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static type& name = *new type arguments
2545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // base
2565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif  // BASE_MACROS_H_
258