/src/mozilla-central/security/sandbox/chromium/base/macros.h
Line | Count | Source (jump to first uncovered line) |
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 | | // Put this in the declarations for a class to be uncopyable. |
16 | | #define DISALLOW_COPY(TypeName) \ |
17 | | TypeName(const TypeName&) = delete |
18 | | |
19 | | // Put this in the declarations for a class to be unassignable. |
20 | | #define DISALLOW_ASSIGN(TypeName) TypeName& operator=(const TypeName&) = delete |
21 | | |
22 | | // Put this in the declarations for a class to be uncopyable and unassignable. |
23 | | #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ |
24 | | DISALLOW_COPY(TypeName); \ |
25 | | DISALLOW_ASSIGN(TypeName) |
26 | | |
27 | | // A macro to disallow all the implicit constructors, namely the |
28 | | // default constructor, copy constructor and operator= functions. |
29 | | // This is especially useful for classes containing only static methods. |
30 | | #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \ |
31 | | TypeName() = delete; \ |
32 | | DISALLOW_COPY_AND_ASSIGN(TypeName) |
33 | | |
34 | | // The arraysize(arr) macro returns the # of elements in an array arr. The |
35 | | // expression is a compile-time constant, and therefore can be used in defining |
36 | | // new arrays, for example. If you use arraysize on a pointer by mistake, you |
37 | | // will get a compile-time error. For the technical details, refer to |
38 | | // http://blogs.msdn.com/b/the1/archive/2004/05/07/128242.aspx. |
39 | | |
40 | | // This template function declaration is used in defining arraysize. |
41 | | // Note that the function doesn't need an implementation, as we only |
42 | | // use its type. |
43 | | template <typename T, size_t N> char (&ArraySizeHelper(T (&array)[N]))[N]; |
44 | 0 | #define arraysize(array) (sizeof(ArraySizeHelper(array))) |
45 | | |
46 | | // Used to explicitly mark the return value of a function as unused. If you are |
47 | | // really sure you don't want to do anything with the return value of a function |
48 | | // that has been marked WARN_UNUSED_RESULT, wrap it with this. Example: |
49 | | // |
50 | | // std::unique_ptr<MyType> my_var = ...; |
51 | | // if (TakeOwnership(my_var.get()) == SUCCESS) |
52 | | // ignore_result(my_var.release()); |
53 | | // |
54 | | template<typename T> |
55 | 0 | inline void ignore_result(const T&) { |
56 | 0 | } Unexecuted instantiation: platform_thread_posix.cc:void ignore_result<base::(anonymous namespace)::ThreadParams*>(base::(anonymous namespace)::ThreadParams* const&) Unexecuted instantiation: void ignore_result<long>(long const&) |
57 | | |
58 | | // The following enum should be used only as a constructor argument to indicate |
59 | | // that the variable has static storage class, and that the constructor should |
60 | | // do nothing to its state. It indicates to the reader that it is legal to |
61 | | // declare a static instance of the class, provided the constructor is given |
62 | | // the base::LINKER_INITIALIZED argument. Normally, it is unsafe to declare a |
63 | | // static variable that has a constructor or a destructor because invocation |
64 | | // order is undefined. However, IF the type can be initialized by filling with |
65 | | // zeroes (which the loader does for static variables), AND the destructor also |
66 | | // does nothing to the storage, AND there are no virtual methods, then a |
67 | | // constructor declared as |
68 | | // explicit MyClass(base::LinkerInitialized x) {} |
69 | | // and invoked as |
70 | | // static MyClass my_variable_name(base::LINKER_INITIALIZED); |
71 | | namespace base { |
72 | | enum LinkerInitialized { LINKER_INITIALIZED }; |
73 | | |
74 | | // Use these to declare and define a static local variable (static T;) so that |
75 | | // it is leaked so that its destructors are not called at exit. If you need |
76 | | // thread-safe initialization, use base/lazy_instance.h instead. |
77 | | #define CR_DEFINE_STATIC_LOCAL(type, name, arguments) \ |
78 | | static type& name = *new type arguments |
79 | | |
80 | | } // base |
81 | | |
82 | | #endif // BASE_MACROS_H_ |