Bug 1159042 - p1. Replace rootFrame variable with isRoot boolean in PresShell::DoRefl...
[gecko.git] / mfbt / GuardObjects.h
blobf4a5727b16a40ba7c4a5a6cd2c62d7b17f391769
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* Implementation of macros to ensure correct use of RAII Auto* objects. */
9 #ifndef mozilla_GuardObjects_h
10 #define mozilla_GuardObjects_h
12 #include "mozilla/Assertions.h"
13 #include "mozilla/Move.h"
14 #include "mozilla/Types.h"
16 #ifdef __cplusplus
18 #ifdef DEBUG
20 /**
21 * A custom define is used rather than |mozPoisonValue()| due to cascading
22 * build failures relating to how mfbt is linked on different operating
23 * systems. See bug 1160253.
25 #define MOZ_POISON uintptr_t(-1)
27 namespace mozilla {
28 namespace detail {
31 * The following classes are designed to cause assertions to detect
32 * inadvertent use of guard objects as temporaries. In other words,
33 * when we have a guard object whose only purpose is its constructor and
34 * destructor (and is never otherwise referenced), the intended use
35 * might be:
37 * AutoRestore savePainting(mIsPainting);
39 * but is is easy to accidentally write:
41 * AutoRestore(mIsPainting);
43 * which compiles just fine, but runs the destructor well before the
44 * intended time.
46 * They work by adding (#ifdef DEBUG) an additional parameter to the
47 * guard object's constructor, with a default value, so that users of
48 * the guard object's API do not need to do anything. The default value
49 * of this parameter is a temporary object. C++ (ISO/IEC 14882:1998),
50 * section 12.2 [class.temporary], clauses 4 and 5 seem to assume a
51 * guarantee that temporaries are destroyed in the reverse of their
52 * construction order, but I actually can't find a statement that that
53 * is true in the general case (beyond the two specific cases mentioned
54 * there). However, it seems to be true.
56 * These classes are intended to be used only via the macros immediately
57 * below them:
59 * MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER declares (ifdef DEBUG) a member
60 * variable, and should be put where a declaration of a private
61 * member variable would be placed.
62 * MOZ_GUARD_OBJECT_NOTIFIER_PARAM should be placed at the end of the
63 * parameters to each constructor of the guard object; it declares
64 * (ifdef DEBUG) an additional parameter. (But use the *_ONLY_PARAM
65 * variant for constructors that take no other parameters.)
66 * MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL should likewise be used in
67 * the implementation of such constructors when they are not inline.
68 * MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT should be used in
69 * the implementation of such constructors to pass the parameter to
70 * a base class that also uses these macros
71 * MOZ_GUARD_OBJECT_NOTIFIER_INIT is a statement that belongs in each
72 * constructor. It uses the parameter declared by
73 * MOZ_GUARD_OBJECT_NOTIFIER_PARAM.
75 * For more details, and examples of using these macros, see
76 * https://developer.mozilla.org/en/Using_RAII_classes_in_Mozilla
78 class GuardObjectNotifier {
79 private:
80 bool* mStatementDone;
82 public:
83 GuardObjectNotifier() : mStatementDone(reinterpret_cast<bool*>(MOZ_POISON)) {}
85 ~GuardObjectNotifier() {
86 // Assert that the GuardObjectNotifier has been properly initialized by
87 // using the |MOZ_GUARD_OBJECT_NOTIFIER_INIT| macro. A poison value is
88 // used rather than a null check to appease static analyzers that were
89 // (incorrectly) detecting null pointer dereferences.
90 MOZ_ASSERT(mStatementDone != reinterpret_cast<bool*>(MOZ_POISON));
91 *mStatementDone = true;
94 void setStatementDone(bool* aStatementIsDone) {
95 mStatementDone = aStatementIsDone;
99 class GuardObjectNotificationReceiver {
100 private:
101 bool mStatementDone;
103 public:
104 GuardObjectNotificationReceiver() : mStatementDone(false) {}
106 ~GuardObjectNotificationReceiver() {
108 * Assert that the guard object was not used as a temporary. (Note that
109 * this assert might also fire if init is not called because the guard
110 * object's implementation is not using the above macros correctly.)
112 MOZ_ASSERT(mStatementDone,
113 "Guard object should not be used as a temporary.");
116 void init(GuardObjectNotifier& aNotifier) {
117 aNotifier.setStatementDone(&mStatementDone);
121 } /* namespace detail */
122 } /* namespace mozilla */
124 #undef MOZ_POISON
126 #endif /* DEBUG */
128 #ifdef DEBUG
129 #define MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER \
130 ::mozilla::detail::GuardObjectNotificationReceiver _mCheckNotUsedAsTemporary;
131 #define MOZ_GUARD_OBJECT_NOTIFIER_PARAM \
132 , ::mozilla::detail::GuardObjectNotifier&& _notifier = \
133 ::mozilla::detail::GuardObjectNotifier()
134 #define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM \
135 ::mozilla::detail::GuardObjectNotifier&& _notifier = \
136 ::mozilla::detail::GuardObjectNotifier()
137 #define MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL \
138 , ::mozilla::detail::GuardObjectNotifier&& _notifier
139 #define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_IN_IMPL \
140 ::mozilla::detail::GuardObjectNotifier&& _notifier
141 #define MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT , ::std::move(_notifier)
142 #define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_TO_PARENT ::std::move(_notifier)
143 #define MOZ_GUARD_OBJECT_NOTIFIER_INIT \
144 do { \
145 _mCheckNotUsedAsTemporary.init(_notifier); \
146 } while (0)
147 #else
148 #define MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
149 #define MOZ_GUARD_OBJECT_NOTIFIER_PARAM
150 #define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM
151 #define MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL
152 #define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_IN_IMPL
153 #define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_TO_PARENT
154 #define MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT
155 #define MOZ_GUARD_OBJECT_NOTIFIER_INIT \
156 do { \
157 } while (0)
158 #endif
160 #endif /* __cplusplus */
162 #endif /* mozilla_GuardObjects_h */