1// Copyright 2014 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// This file contains macros and macro-like constructs (e.g., templates) that
6// are commonly used throughout Chromium source. (It may also contain things
7// that are closely related to things that are commonly used that belong in this
8// file.)
9
10#ifndef BASE_MACROS_H_
11#define BASE_MACROS_H_
12
13#include <stddef.h>  // For size_t.
14
15#if defined(ANDROID)
16// Prefer Android's libbase definitions to our own.
17#include <android-base/macros.h>
18#endif  // defined(ANDROID)
19
20// We define following macros conditionally as they may be defined by another libraries.
21
22// Put this in the declarations for a class to be uncopyable.
23#if !defined(DISALLOW_COPY)
24#define DISALLOW_COPY(TypeName) \
25  TypeName(const TypeName&) = delete
26#endif
27
28// Put this in the declarations for a class to be unassignable.
29#if !defined(DISALLOW_ASSIGN)
30#define DISALLOW_ASSIGN(TypeName) \
31  void operator=(const TypeName&) = delete
32#endif
33
34// A macro to disallow the copy constructor and operator= functions.
35// This should be used in the private: declarations for a class.
36#if !defined(DISALLOW_COPY_AND_ASSIGN)
37#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
38  TypeName(const TypeName&) = delete;      \
39  void operator=(const TypeName&) = delete
40#endif
41
42// A macro to disallow all the implicit constructors, namely the
43// default constructor, copy constructor and operator= functions.
44//
45// This should be used in the private: declarations for a class
46// that wants to prevent anyone from instantiating it. This is
47// especially useful for classes containing only static methods.
48#if !defined(DISALLOW_IMPLICIT_CONSTRUCTORS)
49#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
50  TypeName() = delete;                           \
51  DISALLOW_COPY_AND_ASSIGN(TypeName)
52#endif
53
54// The arraysize(arr) macro returns the # of elements in an array arr.  The
55// expression is a compile-time constant, and therefore can be used in defining
56// new arrays, for example.  If you use arraysize on a pointer by mistake, you
57// will get a compile-time error.  For the technical details, refer to
58// http://blogs.msdn.com/b/the1/archive/2004/05/07/128242.aspx.
59
60// This template function declaration is used in defining arraysize.
61// Note that the function doesn't need an implementation, as we only
62// use its type.
63#if !defined(arraysize)
64template <typename T, size_t N> char (&ArraySizeHelper(T (&array)[N]))[N];
65#define arraysize(array) (sizeof(ArraySizeHelper(array)))
66#endif
67
68// Used to explicitly mark the return value of a function as unused. If you are
69// really sure you don't want to do anything with the return value of a function
70// that has been marked WARN_UNUSED_RESULT, wrap it with this. Example:
71//
72//   std::unique_ptr<MyType> my_var = ...;
73//   if (TakeOwnership(my_var.get()) == SUCCESS)
74//     ignore_result(my_var.release());
75//
76template<typename T>
77inline void ignore_result(const T&) {
78}
79
80// The following enum should be used only as a constructor argument to indicate
81// that the variable has static storage class, and that the constructor should
82// do nothing to its state.  It indicates to the reader that it is legal to
83// declare a static instance of the class, provided the constructor is given
84// the base::LINKER_INITIALIZED argument.  Normally, it is unsafe to declare a
85// static variable that has a constructor or a destructor because invocation
86// order is undefined.  However, IF the type can be initialized by filling with
87// zeroes (which the loader does for static variables), AND the destructor also
88// does nothing to the storage, AND there are no virtual methods, then a
89// constructor declared as
90//       explicit MyClass(base::LinkerInitialized x) {}
91// and invoked as
92//       static MyClass my_variable_name(base::LINKER_INITIALIZED);
93namespace base {
94enum LinkerInitialized { LINKER_INITIALIZED };
95
96// Use these to declare and define a static local variable (static T;) so that
97// it is leaked so that its destructors are not called at exit. If you need
98// thread-safe initialization, use base/lazy_instance.h instead.
99#if !defined(CR_DEFINE_STATIC_LOCAL)
100#define CR_DEFINE_STATIC_LOCAL(type, name, arguments) \
101  static type& name = *new type arguments
102#endif
103
104}  // base
105
106#endif  // BASE_MACROS_H_
107