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

Side by Side Diff: util/misc/initialization_state.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_H_
16 #define CRASHPAD_UTIL_MISC_INITIALIZATION_INITIALIZATION_STATE_H_
17
18 #include <stdint.h>
19
20 namespace crashpad {
21
22 //! \brief Tracks whether data are initialized.
23 //!
24 //! Objects of this type track whether the data they’re guarding are
25 //! initialized. The three possible states are uninitialized (the initial
26 //! state), initializing, and valid. As the guarded data are initialized, an
27 //! InitializationState object will normally transition through these three
28 //! states. A fourth state corresponds to the destruction of objects of this
29 //! type, making it less likely that a use-after-free of an InitializationState
30 //! object will appear in the valid state.
31 //!
32 //! If the only purpose for tracking the initialization state of guarded data is
33 //! to DCHECK when the object is in an unexpected state, use
34 //! InitializationStateDcheck instead.
35 class InitializationState {
36 public:
37 //! \brief The object’s state.
38 enum State : uint8_t {
Robert Sesek 2014/08/13 20:13:37 Is this necessary? Won't the compiler choose the b
Mark Mentovai 2014/08/13 22:22:06 rsesek wrote:
39 //! \brief The object has not yet been initialized.
40 kStateUninitialized = 0,
41
42 //! \brief The object is being initialized.
43 //!
44 //! This state protects against attempted reinitializaton of
45 //! partially-initialized objects whose initial initialization attempt
46 //! failed. This state is to be used while objects are initializing, but are
47 //! not yet fully initialized.
48 kStateInvalid,
49
50 //! \brief The object has been initialized.
51 kStateValid,
52
53 //! \brief The object has been destroyed.
54 kStateDestroyed,
55 };
56
57 InitializationState() : state_(kStateUninitialized) {}
58 ~InitializationState() { state_ = kStateDestroyed; }
59
60 //! \brief Returns `true` if the object’s state is #kStateUninitialized and it
61 //! is safe to begin initializing it.
62 bool is_uninitialized() const { return state_ == kStateUninitialized; }
63
64 //! \brief Sets the object’s state to #kStateInvalid, marking initialization
65 //! as being in process.
66 void set_invalid() { state_ = kStateInvalid; }
67
68 //! \brief Sets the object’s state to #kStateValid, marking it initialized.
69 void set_valid() { state_ = kStateValid; }
70
71 //! \brief Returns `true` if the the object’s state is #kStateValid and it has
72 //! been fully initialized and may be used.
73 bool is_valid() const { return state_ == kStateValid; }
74
75 protected:
76 //! \brief Returns the object’s state.
77 //!
78 //! Consumers of this class should use an is_state_*() method instead.
79 State state() const { return state_; }
80
81 //! \brief Sets the object’s state.
82 //!
83 //! Consumers of this class should use a set_state_*() method instead.
84 void set_state(State state) { state_ = state; }
85
86 private:
87 State state_;
Robert Sesek 2014/08/13 20:13:36 DISALLOW_COPY_AND_ASSIGN
88 };
89
90 } // namespace crashpad
91
92 #endif // CRASHPAD_UTIL_MISC_INITIALIZATION_INITIALIZATION_STATE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698