Bumping gaia.json for 7 gaia revision(s) a=gaia-bump
[gecko.git] / mfbt / GuardObjects.h
blobb8841564daace2fe8a94e066927748c6d1c8865c
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/Types.h"
15 #ifdef __cplusplus
17 #ifdef DEBUG
19 namespace mozilla {
20 namespace detail {
23 * The following classes are designed to cause assertions to detect
24 * inadvertent use of guard objects as temporaries. In other words,
25 * when we have a guard object whose only purpose is its constructor and
26 * destructor (and is never otherwise referenced), the intended use
27 * might be:
29 * AutoRestore savePainting(mIsPainting);
31 * but is is easy to accidentally write:
33 * AutoRestore(mIsPainting);
35 * which compiles just fine, but runs the destructor well before the
36 * intended time.
38 * They work by adding (#ifdef DEBUG) an additional parameter to the
39 * guard object's constructor, with a default value, so that users of
40 * the guard object's API do not need to do anything. The default value
41 * of this parameter is a temporary object. C++ (ISO/IEC 14882:1998),
42 * section 12.2 [class.temporary], clauses 4 and 5 seem to assume a
43 * guarantee that temporaries are destroyed in the reverse of their
44 * construction order, but I actually can't find a statement that that
45 * is true in the general case (beyond the two specific cases mentioned
46 * there). However, it seems to be true.
48 * These classes are intended to be used only via the macros immediately
49 * below them:
51 * MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER declares (ifdef DEBUG) a member
52 * variable, and should be put where a declaration of a private
53 * member variable would be placed.
54 * MOZ_GUARD_OBJECT_NOTIFIER_PARAM should be placed at the end of the
55 * parameters to each constructor of the guard object; it declares
56 * (ifdef DEBUG) an additional parameter. (But use the *_ONLY_PARAM
57 * variant for constructors that take no other parameters.)
58 * MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL should likewise be used in
59 * the implementation of such constructors when they are not inline.
60 * MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT should be used in
61 * the implementation of such constructors to pass the parameter to
62 * a base class that also uses these macros
63 * MOZ_GUARD_OBJECT_NOTIFIER_INIT is a statement that belongs in each
64 * constructor. It uses the parameter declared by
65 * MOZ_GUARD_OBJECT_NOTIFIER_PARAM.
67 * For more details, and examples of using these macros, see
68 * https://developer.mozilla.org/en/Using_RAII_classes_in_Mozilla
70 class GuardObjectNotifier
72 private:
73 bool* mStatementDone;
75 public:
76 GuardObjectNotifier() : mStatementDone(nullptr) { }
78 ~GuardObjectNotifier() { *mStatementDone = true; }
80 void setStatementDone(bool* aStatementIsDone)
82 mStatementDone = aStatementIsDone;
86 class GuardObjectNotificationReceiver
88 private:
89 bool mStatementDone;
91 public:
92 GuardObjectNotificationReceiver() : mStatementDone(false) { }
94 ~GuardObjectNotificationReceiver() {
96 * Assert that the guard object was not used as a temporary. (Note that
97 * this assert might also fire if init is not called because the guard
98 * object's implementation is not using the above macros correctly.)
100 MOZ_ASSERT(mStatementDone);
103 void init(const GuardObjectNotifier& aConstNotifier)
106 * aConstNotifier is passed as a const reference so that we can pass a
107 * temporary, but we really intend it as non-const.
109 GuardObjectNotifier& notifier =
110 const_cast<GuardObjectNotifier&>(aConstNotifier);
111 notifier.setStatementDone(&mStatementDone);
115 } /* namespace detail */
116 } /* namespace mozilla */
118 #endif /* DEBUG */
120 #ifdef DEBUG
121 # define MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER \
122 mozilla::detail::GuardObjectNotificationReceiver _mCheckNotUsedAsTemporary;
123 # define MOZ_GUARD_OBJECT_NOTIFIER_PARAM \
124 , const mozilla::detail::GuardObjectNotifier& _notifier = \
125 mozilla::detail::GuardObjectNotifier()
126 # define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM \
127 const mozilla::detail::GuardObjectNotifier& _notifier = \
128 mozilla::detail::GuardObjectNotifier()
129 # define MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL \
130 , const mozilla::detail::GuardObjectNotifier& _notifier
131 # define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_IN_IMPL \
132 const mozilla::detail::GuardObjectNotifier& _notifier
133 # define MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT \
134 , _notifier
135 # define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_TO_PARENT \
136 _notifier
137 # define MOZ_GUARD_OBJECT_NOTIFIER_INIT \
138 do { _mCheckNotUsedAsTemporary.init(_notifier); } while (0)
139 #else
140 # define MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
141 # define MOZ_GUARD_OBJECT_NOTIFIER_PARAM
142 # define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM
143 # define MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL
144 # define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_IN_IMPL
145 # define MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_TO_PARENT
146 # define MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT
147 # define MOZ_GUARD_OBJECT_NOTIFIER_INIT do { } while (0)
148 #endif
150 #endif /* __cplusplus */
152 #endif /* mozilla_GuardObjects_h */