diff options
Diffstat (limited to 'include/cru/base/Guard.h')
| -rw-r--r-- | include/cru/base/Guard.h | 18 |
1 files changed, 6 insertions, 12 deletions
diff --git a/include/cru/base/Guard.h b/include/cru/base/Guard.h index ae120f5a..6097b4d3 100644 --- a/include/cru/base/Guard.h +++ b/include/cru/base/Guard.h @@ -46,26 +46,21 @@ inline AutoFreePtr<T> MakeAutoFree(T* ptr) { template <typename T, typename Destructor> class AutoDestruct { public: - AutoDestruct() : value_(std::nullopt), auto_destruct_(false) {} + AutoDestruct() : value_(std::nullopt) {} - explicit AutoDestruct(T value, bool auto_destruct = true) - : value_(std::move(value)), auto_destruct_(auto_destruct) {} + explicit AutoDestruct(T value) : value_(std::move(value)) {} CRU_DELETE_COPY(AutoDestruct) AutoDestruct(AutoDestruct&& other) noexcept - : value_(std::move(other.value_)), auto_destruct_(other.auto_destruct_) { + : value_(std::move(other.value_)) { other.value_ = std::nullopt; - other.auto_destruct_ = false; } AutoDestruct& operator=(AutoDestruct&& other) noexcept { if (this != &other) { DoDestruct(); - value_ = other.value_; - auto_destruct_ = other.auto_destruct_; - other.value_ = std::nullopt; - other.auto_destruct_ = false; + value_.swap(other.value_); } return *this; } @@ -96,7 +91,6 @@ class AutoDestruct { CheckValid(); auto value = std::move(*value_); value_ = std::nullopt; - auto_destruct_ = false; return value; } @@ -109,13 +103,13 @@ class AutoDestruct { private: void DoDestruct() { - if (auto_destruct_ && value_) { + if (value_) { Destructor{}(*value_); + value_ = std::nullopt; } } private: std::optional<T> value_; - bool auto_destruct_; }; } // namespace cru |
