cleanup.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2021 The Abseil Authors.
  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. // https://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 ABSL_CLEANUP_INTERNAL_CLEANUP_H_
  15. #define ABSL_CLEANUP_INTERNAL_CLEANUP_H_
  16. #include <new>
  17. #include <type_traits>
  18. #include <utility>
  19. #include "absl/base/internal/invoke.h"
  20. #include "absl/base/macros.h"
  21. #include "absl/base/thread_annotations.h"
  22. #include "absl/utility/utility.h"
  23. namespace absl {
  24. ABSL_NAMESPACE_BEGIN
  25. namespace cleanup_internal {
  26. struct Tag {};
  27. template <typename Arg, typename... Args>
  28. constexpr bool WasDeduced() {
  29. return (std::is_same<cleanup_internal::Tag, Arg>::value) &&
  30. (sizeof...(Args) == 0);
  31. }
  32. template <typename Callback>
  33. constexpr bool ReturnsVoid() {
  34. return (std::is_same<base_internal::invoke_result_t<Callback>, void>::value);
  35. }
  36. template <typename Callback>
  37. class Storage {
  38. public:
  39. Storage() = delete;
  40. explicit Storage(Callback callback) {
  41. // Placement-new into a character buffer is used for eager destruction when
  42. // the cleanup is invoked or cancelled. To ensure this optimizes well, the
  43. // behavior is implemented locally instead of using an absl::optional.
  44. ::new (GetCallbackBuffer()) Callback(std::move(callback));
  45. is_callback_engaged_ = true;
  46. }
  47. Storage(Storage&& other) {
  48. ABSL_HARDENING_ASSERT(other.IsCallbackEngaged());
  49. ::new (GetCallbackBuffer()) Callback(std::move(other.GetCallback()));
  50. is_callback_engaged_ = true;
  51. other.DestroyCallback();
  52. }
  53. Storage(const Storage& other) = delete;
  54. Storage& operator=(Storage&& other) = delete;
  55. Storage& operator=(const Storage& other) = delete;
  56. void* GetCallbackBuffer() { return static_cast<void*>(+callback_buffer_); }
  57. Callback& GetCallback() {
  58. return *reinterpret_cast<Callback*>(GetCallbackBuffer());
  59. }
  60. bool IsCallbackEngaged() const { return is_callback_engaged_; }
  61. void DestroyCallback() {
  62. is_callback_engaged_ = false;
  63. GetCallback().~Callback();
  64. }
  65. void InvokeCallback() ABSL_NO_THREAD_SAFETY_ANALYSIS {
  66. std::move(GetCallback())();
  67. }
  68. private:
  69. bool is_callback_engaged_;
  70. alignas(Callback) char callback_buffer_[sizeof(Callback)];
  71. };
  72. } // namespace cleanup_internal
  73. ABSL_NAMESPACE_END
  74. } // namespace absl
  75. #endif // ABSL_CLEANUP_INTERNAL_CLEANUP_H_