From 5409489c632fcaa3868a0767cecef65f079e5a94 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sat, 19 Sep 2009 08:41:33 +0200 Subject: Add RAII-based file auto-remover --- cutl/fs/auto-remove.hxx | 81 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 cutl/fs/auto-remove.hxx (limited to 'cutl/fs/auto-remove.hxx') diff --git a/cutl/fs/auto-remove.hxx b/cutl/fs/auto-remove.hxx new file mode 100644 index 0000000..0ca7f8f --- /dev/null +++ b/cutl/fs/auto-remove.hxx @@ -0,0 +1,81 @@ +// file : cutl/fs/auto-remove.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +#ifndef CUTL_FS_AUTO_REMOVE_HXX +#define CUTL_FS_AUTO_REMOVE_HXX + +#include + +#include +#include + +namespace cutl +{ + namespace fs + { + // Remove a file or an empty directory on destruction unless canceled. + // + struct auto_remove + { + explicit + auto_remove (path const& p) + : path_ (p), canceled_ (false) + { + } + + ~auto_remove (); + + void + cancel () + { + canceled_ = true; + } + + private: + auto_remove (auto_remove const&); + + auto_remove& + operator= (auto_remove const&); + + private: + path path_; + bool canceled_; + }; + + // Remove a list of file or aempty directories on destruction unless + // canceled. + // + struct auto_removes + { + ~auto_removes (); + + void + add (path const& p) + { + paths_.push_back (p); + } + + void + cancel () + { + canceled_ = true; + } + + private: + auto_removes (auto_removes const&); + + auto_removes& + operator= (auto_removes const&); + + private: + typedef std::vector paths; + + paths paths_; + bool canceled_; + }; + } +} + +#endif // CUTL_FS_AUTO_REMOVE_HXX -- cgit v1.1