ScopeExit.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /* RAII class for executing arbitrary actions at scope end. */
  6. #ifndef mozilla_ScopeExit_h
  7. #define mozilla_ScopeExit_h
  8. /*
  9. * See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4189.pdf for a
  10. * standards-track version of this.
  11. *
  12. * Error handling can be complex when various actions need to be performed that
  13. * need to be undone if an error occurs midway. This can be handled with a
  14. * collection of boolean state variables and gotos, which can get clunky and
  15. * error-prone:
  16. *
  17. * {
  18. * if (!a.setup())
  19. * goto fail;
  20. * isASetup = true;
  21. *
  22. * if (!b.setup())
  23. * goto fail;
  24. * isBSetup = true;
  25. *
  26. * ...
  27. * return true;
  28. *
  29. * fail:
  30. * if (isASetup)
  31. * a.teardown();
  32. * if (isBSetup)
  33. * b.teardown();
  34. * return false;
  35. * }
  36. *
  37. * ScopeExit is a mechanism to simplify this pattern by keeping an RAII guard
  38. * class that will perform the teardown on destruction, unless released. So the
  39. * above would become:
  40. *
  41. * {
  42. * if (!a.setup()) {
  43. * return false;
  44. * }
  45. * auto guardA = MakeScopeExit([&] {
  46. * a.teardown();
  47. * });
  48. *
  49. * if (!b.setup()) {
  50. * return false;
  51. * }
  52. * auto guardB = MakeScopeExit([&] {
  53. * b.teardown();
  54. * });
  55. *
  56. * ...
  57. * guardA.release();
  58. * guardB.release();
  59. * return true;
  60. * }
  61. *
  62. * This header provides:
  63. *
  64. * - |ScopeExit| - a container for a cleanup call, automically called at the
  65. * end of the scope;
  66. * - |MakeScopeExit| - a convenience function for constructing a |ScopeExit|
  67. * with a given cleanup routine, commonly used with a lambda function.
  68. *
  69. * Note that the RAII classes defined in this header do _not_ perform any form
  70. * of reference-counting or garbage-collection. These classes have exactly two
  71. * behaviors:
  72. *
  73. * - if |release()| has not been called, the cleanup is always performed at
  74. * the end of the scope;
  75. * - if |release()| has been called, nothing will happen at the end of the
  76. * scope.
  77. */
  78. #include "mozilla/GuardObjects.h"
  79. #include "mozilla/Move.h"
  80. namespace mozilla {
  81. template <typename ExitFunction>
  82. class MOZ_STACK_CLASS ScopeExit {
  83. ExitFunction mExitFunction;
  84. bool mExecuteOnDestruction;
  85. MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
  86. public:
  87. explicit ScopeExit(ExitFunction&& cleanup
  88. MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
  89. : mExitFunction(cleanup)
  90. , mExecuteOnDestruction(true)
  91. {
  92. MOZ_GUARD_OBJECT_NOTIFIER_INIT;
  93. }
  94. ScopeExit(ScopeExit&& rhs)
  95. : mExitFunction(mozilla::Move(rhs.mExitFunction))
  96. , mExecuteOnDestruction(rhs.mExecuteOnDestruction)
  97. {
  98. rhs.release();
  99. }
  100. ~ScopeExit() {
  101. if (mExecuteOnDestruction) {
  102. mExitFunction();
  103. }
  104. }
  105. void release() {
  106. mExecuteOnDestruction = false;
  107. }
  108. private:
  109. explicit ScopeExit(const ScopeExit&) = delete;
  110. ScopeExit& operator=(const ScopeExit&) = delete;
  111. ScopeExit& operator=(ScopeExit&&) = delete;
  112. };
  113. template <typename ExitFunction>
  114. ScopeExit<ExitFunction>
  115. MakeScopeExit(ExitFunction&& exitFunction)
  116. {
  117. return ScopeExit<ExitFunction>(mozilla::Move(exitFunction));
  118. }
  119. } /* namespace mozilla */
  120. #endif /* mozilla_ScopeExit_h */