fs/scoped: add general scoped path

This commit is contained in:
Danny Robson 2022-06-01 14:55:59 +10:00
parent d5261b980c
commit 10b21cb497
3 changed files with 97 additions and 26 deletions

View File

@ -11,6 +11,55 @@
#include <cruft/util/debug/assert.hpp> #include <cruft/util/debug/assert.hpp>
using cruft::fs::scoped_dir; using cruft::fs::scoped_dir;
using cruft::fs::scoped_path;
///////////////////////////////////////////////////////////////////////////////
scoped_path::scoped_path (std::filesystem::path &&_path)
: m_path (std::move (_path))
{
CHECK (std::filesystem::exists (*m_path));
}
//-----------------------------------------------------------------------------
scoped_path::scoped_path (std::filesystem::path const &_path)
: scoped_path (std::filesystem::path (_path))
{ ; }
//-----------------------------------------------------------------------------
std::filesystem::path const&
scoped_path::get () const&
{
return *m_path;
}
//-----------------------------------------------------------------------------
std::filesystem::path
scoped_path::release ()
{
auto res = std::move (*m_path);
m_path.reset ();
return res;
}
//-----------------------------------------------------------------------------
void
scoped_path::reset (void)
{
return m_path.reset ();
}
//-----------------------------------------------------------------------------
scoped_path::~scoped_path ()
{
if (m_path)
std::filesystem::remove_all (*m_path);
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@ -21,25 +70,17 @@ scoped_dir::scoped_dir (std::filesystem::path const &_path)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
scoped_dir::scoped_dir (std::filesystem::path &&_path) scoped_dir::scoped_dir (std::filesystem::path &&_path)
: m_path (std::move (_path)) : scoped_path (std::move (_path))
{ {
CHECK (std::filesystem::is_directory (*m_path)); CHECK (std::filesystem::is_directory (get ()));
} }
/////////////////////////////////////////////////////////////////////////////// //-----------------------------------------------------------------------------
scoped_dir::~scoped_dir ()
{
if (m_path)
std::filesystem::remove_all (*m_path);
}
///////////////////////////////////////////////////////////////////////////////
std::filesystem::path const & std::filesystem::path const &
scoped_dir::operator* () const & scoped_dir::operator* () const &
{ {
return *m_path; return get ();
} }
@ -47,5 +88,5 @@ scoped_dir::operator* () const &
std::filesystem::path const* std::filesystem::path const*
scoped_dir::operator-> () const & scoped_dir::operator-> () const &
{ {
return &*m_path; return &get();
} }

View File

@ -12,26 +12,42 @@
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
namespace cruft::fs { namespace cruft::fs {
///------------------------------------------------------------------------ /// Holds a path and deletes the path (and it any contents if it's a
/// Holds the path to a directory that will be deleted (along with all /// directory) at destruction.
/// contents) when the object goes out of scope. class scoped_path {
class scoped_dir {
public: public:
explicit scoped_dir (std::filesystem::path const&); explicit scoped_path (std::filesystem::path const&);
explicit scoped_dir (std::filesystem::path &&); explicit scoped_path (std::filesystem::path &&);
scoped_dir (scoped_dir const&) = delete; scoped_path (scoped_path const&) = delete;
scoped_dir& operator= (scoped_dir const&) = delete; scoped_path& operator= (scoped_path const&) = delete;
scoped_dir (scoped_dir&&) noexcept = default; scoped_path (scoped_path&&) noexcept = default;
scoped_dir& operator= (scoped_dir&&) noexcept = default; scoped_path& operator= (scoped_path&&) noexcept = default;
~scoped_dir (); ~scoped_path ();
std::filesystem::path const& operator* () const&; std::filesystem::path const& get (void) const&;
std::filesystem::path const* operator-> () const&;
std::filesystem::path release (void);
void reset (void);
private: private:
std::optional<std::filesystem::path> m_path; std::optional<std::filesystem::path> m_path;
}; };
///------------------------------------------------------------------------
/// Holds the path to a directory that will be deleted (along with all
/// contents) when the object goes out of scope.
class scoped_dir : public scoped_path {
public:
explicit scoped_dir (std::filesystem::path const&);
explicit scoped_dir (std::filesystem::path &&);
using scoped_path::scoped_path;
std::filesystem::path const& operator* () const&;
std::filesystem::path const* operator-> () const&;
};
} }

View File

@ -22,5 +22,19 @@ main (int, char**)
tap.expect (success, "scoped_dir removes directory after scope"); tap.expect (success, "scoped_dir removes directory after scope");
} }
{
bool success = true;
auto const path = cruft::fs::mktmpdir ();
cruft::fs::scoped_path dir (path);
success = success && std::filesystem::exists (path);
dir.reset ();
success = success && !std::filesystem::exists (path);
tap.expect (true, "scoped_path");
}
return tap.status (); return tap.status ();
} }