diff --git a/tempfile/TempDir.cpp b/tempfile/TempDir.cpp index 8daa399f..f5545dc0 100644 --- a/tempfile/TempDir.cpp +++ b/tempfile/TempDir.cpp @@ -5,12 +5,19 @@ namespace bf = boost::filesystem; namespace cpputils { TempDir::TempDir() - : _path(bf::unique_path(bf::temp_directory_path() / "%%%%-%%%%-%%%%-%%%%")) { + : _path(bf::unique_path(bf::temp_directory_path() / "%%%%-%%%%-%%%%-%%%%")), _existing(true) { bf::create_directory(_path); } TempDir::~TempDir() { - bf::remove_all(_path); + remove(); +} + +void TempDir::remove() { + if (_existing) { + bf::remove_all(_path); + _existing = false; + } } const bf::path &TempDir::path() const { diff --git a/tempfile/TempDir.h b/tempfile/TempDir.h index cd465810..bde0b6b6 100644 --- a/tempfile/TempDir.h +++ b/tempfile/TempDir.h @@ -7,14 +7,16 @@ namespace cpputils { -class TempDir { +class TempDir final { public: TempDir(); - virtual ~TempDir(); + ~TempDir(); const boost::filesystem::path &path() const; + void remove(); private: const boost::filesystem::path _path; + bool _existing; DISALLOW_COPY_AND_ASSIGN(TempDir); }; diff --git a/tempfile/TempFile.h b/tempfile/TempFile.h index 8d2cd1c4..bf7641a1 100644 --- a/tempfile/TempFile.h +++ b/tempfile/TempFile.h @@ -7,11 +7,11 @@ namespace cpputils { -class TempFile { +class TempFile final { public: explicit TempFile(const boost::filesystem::path &path, bool create = true); explicit TempFile(bool create = true); - virtual ~TempFile(); + ~TempFile(); const boost::filesystem::path &path() const; //TODO Test exists() bool exists() const;