You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

cleanup.h 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. {
  25. ABSL_NAMESPACE_BEGIN
  26. namespace cleanup_internal
  27. {
  28. struct Tag
  29. {
  30. };
  31. template<typename Arg, typename... Args>
  32. constexpr bool WasDeduced()
  33. {
  34. return (std::is_same<cleanup_internal::Tag, Arg>::value) &&
  35. (sizeof...(Args) == 0);
  36. }
  37. template<typename Callback>
  38. constexpr bool ReturnsVoid()
  39. {
  40. return (std::is_same<base_internal::invoke_result_t<Callback>, void>::value);
  41. }
  42. template<typename Callback>
  43. class Storage
  44. {
  45. public:
  46. Storage() = delete;
  47. explicit Storage(Callback callback)
  48. {
  49. // Placement-new into a character buffer is used for eager destruction when
  50. // the cleanup is invoked or cancelled. To ensure this optimizes well, the
  51. // behavior is implemented locally instead of using an absl::optional.
  52. ::new (GetCallbackBuffer()) Callback(std::move(callback));
  53. is_callback_engaged_ = true;
  54. }
  55. Storage(Storage&& other)
  56. {
  57. ABSL_HARDENING_ASSERT(other.IsCallbackEngaged());
  58. ::new (GetCallbackBuffer()) Callback(std::move(other.GetCallback()));
  59. is_callback_engaged_ = true;
  60. other.DestroyCallback();
  61. }
  62. Storage(const Storage& other) = delete;
  63. Storage& operator=(Storage&& other) = delete;
  64. Storage& operator=(const Storage& other) = delete;
  65. void* GetCallbackBuffer()
  66. {
  67. return static_cast<void*>(+callback_buffer_);
  68. }
  69. Callback& GetCallback()
  70. {
  71. return *reinterpret_cast<Callback*>(GetCallbackBuffer());
  72. }
  73. bool IsCallbackEngaged() const
  74. {
  75. return is_callback_engaged_;
  76. }
  77. void DestroyCallback()
  78. {
  79. is_callback_engaged_ = false;
  80. GetCallback().~Callback();
  81. }
  82. void InvokeCallback() ABSL_NO_THREAD_SAFETY_ANALYSIS
  83. {
  84. std::move(GetCallback())();
  85. }
  86. private:
  87. bool is_callback_engaged_;
  88. alignas(Callback) char callback_buffer_[sizeof(Callback)];
  89. };
  90. } // namespace cleanup_internal
  91. ABSL_NAMESPACE_END
  92. } // namespace absl
  93. #endif // ABSL_CLEANUP_INTERNAL_CLEANUP_H_