aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2017-07-21 09:36:05 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2017-07-21 09:36:05 +0200
commit811d4229c196c1c98ff865fdfd981ef2908f6d8b (patch)
tree7ed4833d43b575b64ebb4e300a37320e6230d8b3
parent41da9fabee47c81c1c8f59fe6616f24fc1b21294 (diff)
Improve auto_rm
-rw-r--r--libbutl/filesystem.hxx20
-rw-r--r--libbutl/filesystem.ixx13
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 <typename P>
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<path>;
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 <typename P>
inline auto_rm<P>::
auto_rm (auto_rm&& x)
- : path_ (std::move (x.path_))
+ : path (std::move (x.path)), active (x.active)
{
- x.cancel ();
+ x.active = false;
}
template <typename P>
@@ -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<path>::
- ~auto_rm () {if (!path_.empty ()) try_rmfile (path_, true);}
+ ~auto_rm () {if (active && !path.empty ()) try_rmfile (path, true);}
template <>
inline auto_rm<dir_path>::
- ~auto_rm () {if (!path_.empty ()) try_rmdir_r (path_, true);}
+ ~auto_rm () {if (active && !path.empty ()) try_rmdir_r (path, true);}
// cpflags
//