Allow TempDir::remove()

This commit is contained in:
Sebastian Messmer 2015-10-29 15:51:16 +01:00
parent 62fcd1a3fd
commit cc99cb0ea5
3 changed files with 15 additions and 6 deletions

View File

@ -5,12 +5,19 @@ namespace bf = boost::filesystem;
namespace cpputils { namespace cpputils {
TempDir::TempDir() TempDir::TempDir()
: _path(bf::unique_path(bf::temp_directory_path() / "%%%%-%%%%-%%%%-%%%%")) { : _path(bf::unique_path(bf::temp_directory_path() / "%%%%-%%%%-%%%%-%%%%")), _existing(true) {
bf::create_directory(_path); bf::create_directory(_path);
} }
TempDir::~TempDir() { TempDir::~TempDir() {
bf::remove_all(_path); remove();
}
void TempDir::remove() {
if (_existing) {
bf::remove_all(_path);
_existing = false;
}
} }
const bf::path &TempDir::path() const { const bf::path &TempDir::path() const {

View File

@ -7,14 +7,16 @@
namespace cpputils { namespace cpputils {
class TempDir { class TempDir final {
public: public:
TempDir(); TempDir();
virtual ~TempDir(); ~TempDir();
const boost::filesystem::path &path() const; const boost::filesystem::path &path() const;
void remove();
private: private:
const boost::filesystem::path _path; const boost::filesystem::path _path;
bool _existing;
DISALLOW_COPY_AND_ASSIGN(TempDir); DISALLOW_COPY_AND_ASSIGN(TempDir);
}; };

View File

@ -7,11 +7,11 @@
namespace cpputils { namespace cpputils {
class TempFile { class TempFile final {
public: public:
explicit TempFile(const boost::filesystem::path &path, bool create = true); explicit TempFile(const boost::filesystem::path &path, bool create = true);
explicit TempFile(bool create = true); explicit TempFile(bool create = true);
virtual ~TempFile(); ~TempFile();
const boost::filesystem::path &path() const; const boost::filesystem::path &path() const;
//TODO Test exists() //TODO Test exists()
bool exists() const; bool exists() const;