From 811d4229c196c1c98ff865fdfd981ef2908f6d8b Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 21 Jul 2017 09:36:05 +0200 Subject: Improve auto_rm --- libbutl/filesystem.hxx | 20 +++++++++----------- libbutl/filesystem.ixx | 13 +++++++------ 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/libbutl/filesystem.hxx b/libbutl/filesystem.hxx index e23fcd3..9bf6f0c 100644 --- a/libbutl/filesystem.hxx +++ b/libbutl/filesystem.hxx @@ -157,21 +157,22 @@ namespace butl LIBBUTL_SYMEXPORT rmfile_status try_rmfile (const path&, bool ignore_error = false); - // Automatically try to remove the path on destruction unless cancelled. - // Since the non-cancelled destruction will normally happen as a result - // of an exception, the failure to remove the path is silently ignored. + // Automatically try to remove a non-empty the path on destruction unless + // cancelled. Since the non-cancelled destruction will normally happen as a + // result of an exception, the failure to remove the path is silently + // ignored. // template struct auto_rm { + P path; + bool active; + explicit - auto_rm (P p = P ()): path_ (std::move (p)) {} + auto_rm (P p = P (), bool a = true): path (std::move (p)), active (a) {} void - cancel () {path_ = P ();} - - const P& - path () const {return path_;} + cancel () {active = false;} // Movable-only type. Move-assignment cancels the lhs object. // @@ -181,9 +182,6 @@ namespace butl auto_rm& operator= (const auto_rm&) = delete; ~auto_rm (); - - private: - P path_; }; using auto_rmfile = auto_rm; diff --git a/libbutl/filesystem.ixx b/libbutl/filesystem.ixx index a53aa47..a618d5b 100644 --- a/libbutl/filesystem.ixx +++ b/libbutl/filesystem.ixx @@ -44,9 +44,9 @@ namespace butl template inline auto_rm

:: auto_rm (auto_rm&& x) - : path_ (std::move (x.path_)) + : path (std::move (x.path)), active (x.active) { - x.cancel (); + x.active = false; } template @@ -55,8 +55,9 @@ namespace butl { if (this != &x) { - path_ = std::move (x.path_); - x.cancel (); + path = std::move (x.path); + active = x.active; + x.active = false; } return *this; @@ -64,11 +65,11 @@ namespace butl template <> inline auto_rm:: - ~auto_rm () {if (!path_.empty ()) try_rmfile (path_, true);} + ~auto_rm () {if (active && !path.empty ()) try_rmfile (path, true);} template <> inline auto_rm:: - ~auto_rm () {if (!path_.empty ()) try_rmdir_r (path_, true);} + ~auto_rm () {if (active && !path.empty ()) try_rmdir_r (path, true);} // cpflags // -- cgit v1.1