Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(31)

Side by Side Diff: util/misc/initialization_state_dcheck.h

Issue 465103002: Add InitializationStateDcheck, its base class, and their tests (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Put each macro in Doxygen’s PREDEFINED tag on its own line Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Crashpad Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef CRASHPAD_UTIL_MISC_INITIALIZATION_INITIALIZATION_STATE_DCHECK_H_
16 #define CRASHPAD_UTIL_MISC_INITIALIZATION_INITIALIZATION_STATE_DCHECK_H_
17
18 //! \file
19
20 #include "base/logging.h"
21 #include "util/misc/initialization_state.h"
22
23 namespace crashpad {
24
25 #if DCHECK_IS_ON || DOXYGEN
26
27 //! \brief Tracks whether data are initialized, triggering a DCHECK assertion
28 //! on an invalid data access.
29 //!
30 //! Put an InitializationStateDcheck member into a class to help DCHECK that
31 //! it’s in the right states at the right times. This is useful for classes with
32 //! Initialize() methods. The chief advantage of InitializationStateDcheck over
33 //! having a member variable to track state is that when the only use of the
34 //! variable is to DCHECK, it wastes space (in memory and executable code) in
35 //! non-DCHECK builds unless the code is also peppered with ugly #ifdefs.
36 //!
37 //! This implementation concentrates the ugly #ifdefs in one location.
38 //!
39 //! Usage:
40 //!
41 //! \code
42 //! class Class {
43 //! public:
44 //! Class() : initialized_() {}
45 //!
46 //! void Initialize() {
47 //! INITIALIZATION_STATE_DCHECK_BEGIN(initialized_);
48 //! // Perform initialization.
49 //! INITIALIZATION_STATE_SET_VALID(initialized_);
50 //! }
51 //!
52 //! void DoSomething() {
53 //! INITIALIZATION_STATE_DCHECK_VALID(initialized_);
54 //! // Do something.
55 //! }
56 //!
57 //! private:
58 //! InitializationStateDcheck initialized_;
59 //! };
60 //! \endcode
61 class InitializationStateDcheck : public InitializationState {
62 public:
63 InitializationStateDcheck() : InitializationState() {}
64
65 //! \brief Returns the object’s state.
66 //!
67 //! Consumers of this class should not call this method. Use the
68 //! INITIALIZATION_STATE_DCHECK_BEGIN() and
69 //! INITIALIZATION_STATE_DCHECK_VALID() macros instead.
70 //
71 // The superclass’ state() accessor is protected, but it needs to be exposed
72 // to consumers of this class for the macros below to work properly. The
73 // macros prefer access to the unerlying state value over a simple boolean
74 // because with access to the state value, DCHECK_EQ can be used, which, when
75 // tripped, prints both the expected and observed values. This can aid
76 // troubleshooting.
77 State state() const { return InitializationState::state(); }
78
79 //! \brief Marks an uninitialized object as initializing.
80 //!
81 //! If the object is in the uninitialized state, marks it as initializing
82 //! (invalid) and returns the previous (uninitialized) state. Otherwise,
83 //! returns the object’s current state.
Robert Sesek 2014/08/13 20:13:37 It seems like it should be illegal to call Begin()
Mark Mentovai 2014/08/13 22:22:06 rsesek wrote:
84 //!
85 //! Consumers of this class should not call this method. Use the
86 //! INITIALIZATION_STATE_DCHECK_BEGIN() macro instead.
87 State Begin();
Robert Sesek 2014/08/13 20:13:37 private: DISALLLOW_COPY_AND_ASSIGN() ?
88 };
89
90 // Using macros enables the non-DCHECK no-op implementation below to be more
91 // compact and less intrusive.
92
93 //! \brief Changes a crashpad::InitializationStateDcheck object’s state to
94 //! crashpad::InitializationState::kStateValid.
95 //!
96 //! \param[in] initialization_state_dcheck A crashpad::InitializationStateDcheck
97 //! object.
98 //!
99 //! \sa crashpad::InitializationStateDcheck
100 #define INITIALIZATION_STATE_SET_VALID(initialization_state_dcheck) \
101 do { \
102 (initialization_state_dcheck).set_valid(); \
103 } while (false)
104
105 // INITIALIZATION_STATE_DCHECK_* are macros instead of methods that call
106 // DCHECK to enable the DCHECK failure message to point to the correct file and
107 // line number, and to allow additional messages to be streamed on failure with
108 // the << operator.
109
110 //! \brief Checks that a crashpad::InitializationStateDcheck object is in the
111 //! crashpad::InitializationState::kStateUninitialized state, and changes
112 //! its state to initializing
113 //! (crashpad::InitializationState::kStateInvalid).
114 //!
115 //! If the object is not in the correct state, a DCHECK assertion is triggered
116 //! and the object’s state remains unchanged.
117 //!
118 //! \param[in] initialization_state_dcheck A crashpad::InitializationStateDcheck
119 //! object.
120 //!
121 //! \sa crashpad::InitializationStateDcheck
122 #define INITIALIZATION_STATE_DCHECK_BEGIN(initialization_state_dcheck) \
123 DCHECK_EQ((initialization_state_dcheck).Begin(), \
124 (initialization_state_dcheck).kStateUninitialized)
125
126 //! \brief Checks that a crashpad::InitializationStateDcheck object is in the
127 //! crashpad::InitializationState::kStateValid state.
128 //!
129 //! If the object is not in the correct state, a DCHECK assertion is triggered.
130 //!
131 //! \param[in] initialization_state_dcheck A crashpad::InitializationStateDcheck
132 //! object.
133 //!
134 //! \sa crashpad::InitializationStateDcheck
135 #define INITIALIZATION_STATE_DCHECK_VALID(initialization_state_dcheck) \
136 DCHECK_EQ((initialization_state_dcheck).state(), \
137 (initialization_state_dcheck).kStateValid)
138
139 #else
140
141 // Since this is to be used as a DCHECK (for debugging), it should be
142 // non-intrusive in non-DCHECK (non-debug, release) builds. An empty struct
143 // would still have a nonzero size (rationale:
144 // http://www.stroustrup.com/bs_faq2.html#sizeof-empty). Zero-length arrays are
145 // technically invalid according to the standard, but clang and g++ accept them
146 // without complaint even with warnings turned up. They take up no space at all,
147 // and they can be “initialized” with the same () syntax used to initialize
148 // objects of the DCHECK_IS_ON StateDcheck class above.
149 typedef bool InitializationStateDcheck[0];
150
151 // The contents of these DCHECKs will never be evaluated, but they make use of
152 // initialization_state_dcheck to avoid triggering -Wunused-private-field
153 // warnings.
154 #define INITIALIZATION_STATE_SET_VALID(initialization_state_dcheck) \
155 do { \
156 } while (false)
157 #define INITIALIZATION_STATE_DCHECK_BEGIN(initialization_state_dcheck) \
158 DCHECK(&(initialization_state_dcheck))
159 #define INITIALIZATION_STATE_DCHECK_VALID(initialization_state_dcheck) \
160 DCHECK(&(initialization_state_dcheck))
161
162 #endif
163
164 } // namespace crashpad
165
166 #endif // CRASHPAD_UTIL_MISC_INITIALIZATION_INITIALIZATION_STATE_DCHECK_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698