From cc99cb0ea5dc22b02069ea37cf8a15660b37b5c7 Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Thu, 29 Oct 2015 15:51:16 +0100 Subject: [PATCH] Allow TempDir::remove() --- tempfile/TempDir.cpp | 11 +++++++++-- tempfile/TempDir.h | 6 ++++-- tempfile/TempFile.h | 4 ++-- 3 files changed, 15 insertions(+), 6 deletions(-) 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;