initialization_state_dcheck.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. #ifndef CRASHPAD_UTIL_MISC_INITIALIZATION_INITIALIZATION_STATE_DCHECK_H_
  15. #define CRASHPAD_UTIL_MISC_INITIALIZATION_INITIALIZATION_STATE_DCHECK_H_
  16. //! \file
  17. #include "base/compiler_specific.h"
  18. #include "base/logging.h"
  19. #include "base/macros.h"
  20. #include "build/build_config.h"
  21. #include "util/misc/initialization_state.h"
  22. namespace crashpad {
  23. #if DCHECK_IS_ON() || DOXYGEN
  24. //! \brief Tracks whether data are initialized, triggering a DCHECK assertion
  25. //! on an invalid data access.
  26. //!
  27. //! Put an InitializationStateDcheck member into a class to help DCHECK that
  28. //! it’s in the right states at the right times. This is useful for classes with
  29. //! Initialize() methods. The chief advantage of InitializationStateDcheck over
  30. //! having a member variable to track state is that when the only use of the
  31. //! variable is to DCHECK, it wastes space (in memory and executable code) in
  32. //! non-DCHECK builds unless the code is also peppered with ugly `#%ifdef`s.
  33. //!
  34. //! This implementation concentrates the ugly `#%ifdef`s in one location.
  35. //!
  36. //! Usage:
  37. //!
  38. //! \code
  39. //! class Class {
  40. //! public:
  41. //! Class() : initialized_() {}
  42. //!
  43. //! void Initialize() {
  44. //! INITIALIZATION_STATE_SET_INITIALIZING(initialized_);
  45. //! // Perform initialization.
  46. //! INITIALIZATION_STATE_SET_VALID(initialized_);
  47. //! }
  48. //!
  49. //! void DoSomething() {
  50. //! INITIALIZATION_STATE_DCHECK_VALID(initialized_);
  51. //! // Do something.
  52. //! }
  53. //!
  54. //! private:
  55. //! InitializationStateDcheck initialized_;
  56. //! };
  57. //! \endcode
  58. class InitializationStateDcheck : public InitializationState {
  59. public:
  60. InitializationStateDcheck() : InitializationState() {}
  61. //! \brief Returns the object’s state.
  62. //!
  63. //! Consumers of this class should not call this method. Use the
  64. //! INITIALIZATION_STATE_SET_INITIALIZING(), INITIALIZATION_STATE_SET_VALID(),
  65. //! and INITIALIZATION_STATE_DCHECK_VALID() macros instead.
  66. //
  67. // The superclass’ state() accessor is protected, but it needs to be exposed
  68. // to consumers of this class for the macros below to work properly. The
  69. // macros prefer access to the unerlying state value over a simple boolean
  70. // because with access to the state value, DCHECK_EQ can be used, which, when
  71. // tripped, prints both the expected and observed values. This can aid
  72. // troubleshooting.
  73. State state() const { return InitializationState::state(); }
  74. //! \brief Marks an uninitialized object as initializing.
  75. //!
  76. //! If the object is in the #kStateUninitialized state, changes its state to
  77. //! #kStateInvalid (initializing) and returns the previous
  78. //! (#kStateUninitialized) state. Otherwise, returns the object’s current
  79. //! state.
  80. //!
  81. //! Consumers of this class should not call this method. Use the
  82. //! INITIALIZATION_STATE_SET_INITIALIZING() macro instead.
  83. State SetInitializing();
  84. //! \brief Marks an initializing object as valid.
  85. //!
  86. //! If the object is in the #kStateInvalid (initializing) state, changes its
  87. //! state to #kStateValid and returns the previous (#kStateInvalid) state.
  88. //! Otherwise, returns the object’s current state.
  89. //!
  90. //! Consumers of this class should not call this method. Use the
  91. //! INITIALIZATION_STATE_SET_VALID() macro instead.
  92. State SetValid();
  93. private:
  94. DISALLOW_COPY_AND_ASSIGN(InitializationStateDcheck);
  95. };
  96. // Using macros enables the non-DCHECK no-op implementation below to be more
  97. // compact and less intrusive. These are macros instead of methods that call
  98. // DCHECK to enable the DCHECK failure message to point to the correct file and
  99. // line number, and to allow additional messages to be streamed on failure with
  100. // the << operator.
  101. //! \brief Checks that a crashpad::InitializationStateDcheck object is in the
  102. //! crashpad::InitializationState::kStateUninitialized state, and changes
  103. //! its state to initializing
  104. //! (crashpad::InitializationState::kStateInvalid).
  105. //!
  106. //! If the object is not in the correct state, a DCHECK assertion is triggered
  107. //! and the object’s state remains unchanged.
  108. //!
  109. //! \param[in] initialization_state_dcheck A crashpad::InitializationStateDcheck
  110. //! object.
  111. //!
  112. //! \sa crashpad::InitializationStateDcheck
  113. #define INITIALIZATION_STATE_SET_INITIALIZING(initialization_state_dcheck) \
  114. DCHECK_EQ((initialization_state_dcheck).SetInitializing(), \
  115. (initialization_state_dcheck).kStateUninitialized)
  116. //! \brief Checks that a crashpad::InitializationStateDcheck object is in the
  117. //! initializing (crashpad::InitializationState::kStateInvalid) state, and
  118. //! changes its state to crashpad::InitializationState::kStateValid.
  119. //!
  120. //! If the object is not in the correct state, a DCHECK assertion is triggered
  121. //! and the object’s state remains unchanged.
  122. //!
  123. //! \param[in] initialization_state_dcheck A crashpad::InitializationStateDcheck
  124. //! object.
  125. //!
  126. //! \sa crashpad::InitializationStateDcheck
  127. #define INITIALIZATION_STATE_SET_VALID(initialization_state_dcheck) \
  128. DCHECK_EQ((initialization_state_dcheck).SetValid(), \
  129. (initialization_state_dcheck).kStateInvalid)
  130. //! \brief Checks that a crashpad::InitializationStateDcheck object is in the
  131. //! crashpad::InitializationState::kStateValid state.
  132. //!
  133. //! If the object is not in the correct state, a DCHECK assertion is triggered.
  134. //!
  135. //! \param[in] initialization_state_dcheck A crashpad::InitializationStateDcheck
  136. //! object.
  137. //!
  138. //! \sa crashpad::InitializationStateDcheck
  139. #define INITIALIZATION_STATE_DCHECK_VALID(initialization_state_dcheck) \
  140. DCHECK_EQ((initialization_state_dcheck).state(), \
  141. (initialization_state_dcheck).kStateValid)
  142. #else
  143. #if defined(COMPILER_MSVC)
  144. // bool[0] (below) is not accepted by MSVC.
  145. struct InitializationStateDcheck {
  146. };
  147. #else
  148. // Since this is to be used as a DCHECK (for debugging), it should be
  149. // non-intrusive in non-DCHECK (non-debug, release) builds. An empty struct
  150. // would still have a nonzero size (rationale:
  151. // http://www.stroustrup.com/bs_faq2.html#sizeof-empty). Zero-length arrays are
  152. // technically invalid according to the standard, but clang and g++ accept them
  153. // without complaint even with warnings turned up. They take up no space at all,
  154. // and they can be “initialized” with the same () syntax used to initialize
  155. // objects of the DCHECK_IS_ON() InitializationStateDcheck class above.
  156. using InitializationStateDcheck = bool[0];
  157. #endif // COMPILER_MSVC
  158. // Avoid triggering warnings by repurposing these macros when DCHECKs are
  159. // disabled.
  160. #define INITIALIZATION_STATE_SET_INITIALIZING(initialization_state_dcheck) \
  161. ALLOW_UNUSED_LOCAL(initialization_state_dcheck)
  162. #define INITIALIZATION_STATE_SET_VALID(initialization_state_dcheck) \
  163. ALLOW_UNUSED_LOCAL(initialization_state_dcheck)
  164. #define INITIALIZATION_STATE_DCHECK_VALID(initialization_state_dcheck) \
  165. ALLOW_UNUSED_LOCAL(initialization_state_dcheck)
  166. #endif
  167. } // namespace crashpad
  168. #endif // CRASHPAD_UTIL_MISC_INITIALIZATION_INITIALIZATION_STATE_DCHECK_H_