fs: add scoped_dir container
This commit is contained in:
parent
74700363ab
commit
9b11cc92ff
@ -344,6 +344,8 @@ list (
|
||||
float.hpp
|
||||
fourcc.cpp
|
||||
fourcc.hpp
|
||||
fs/scoped.cpp
|
||||
fs/scoped.hpp
|
||||
fs/tmp_posix.cpp
|
||||
fs/tmp.hpp
|
||||
functor.hpp
|
||||
|
51
fs/scoped.cpp
Normal file
51
fs/scoped.cpp
Normal file
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Copyright 2022, Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#include "./scoped.hpp"
|
||||
|
||||
#include <cruft/util/debug/assert.hpp>
|
||||
|
||||
using cruft::fs::scoped_dir;
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
scoped_dir::scoped_dir (std::filesystem::path const &_path)
|
||||
: scoped_dir (std::filesystem::path (_path))
|
||||
{}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
scoped_dir::scoped_dir (std::filesystem::path &&_path)
|
||||
: m_path (std::move (_path))
|
||||
{
|
||||
CHECK (std::filesystem::is_directory (*m_path));
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
scoped_dir::~scoped_dir ()
|
||||
{
|
||||
if (m_path)
|
||||
std::filesystem::remove_all (*m_path);
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
std::filesystem::path const &
|
||||
scoped_dir::operator* () const &
|
||||
{
|
||||
return *m_path;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
std::filesystem::path const*
|
||||
scoped_dir::operator-> () const &
|
||||
{
|
||||
return &*m_path;
|
||||
}
|
37
fs/scoped.hpp
Normal file
37
fs/scoped.hpp
Normal file
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Copyright 2022, Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#include <optional>
|
||||
#include <filesystem>
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
namespace cruft::fs {
|
||||
///------------------------------------------------------------------------
|
||||
/// 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:
|
||||
explicit scoped_dir (std::filesystem::path const&);
|
||||
explicit scoped_dir (std::filesystem::path &&);
|
||||
|
||||
scoped_dir (scoped_dir const&) = delete;
|
||||
scoped_dir& operator= (scoped_dir const&) = delete;
|
||||
|
||||
scoped_dir (scoped_dir&&) noexcept = default;
|
||||
scoped_dir& operator= (scoped_dir&&) noexcept = default;
|
||||
|
||||
~scoped_dir ();
|
||||
|
||||
std::filesystem::path const& operator* () const&;
|
||||
std::filesystem::path const* operator-> () const&;
|
||||
|
||||
private:
|
||||
std::optional<std::filesystem::path> m_path;
|
||||
};
|
||||
}
|
26
test/fs/scoped.cpp
Normal file
26
test/fs/scoped.cpp
Normal file
@ -0,0 +1,26 @@
|
||||
#include <cruft/util/tap.hpp>
|
||||
#include <cruft/util/fs/scoped.hpp>
|
||||
#include <cruft/util/fs/tmp.hpp>
|
||||
|
||||
|
||||
int
|
||||
main (int, char**)
|
||||
{
|
||||
cruft::TAP::logger tap;
|
||||
|
||||
{
|
||||
bool success = true;
|
||||
auto const path = cruft::fs::mktmpdir ();
|
||||
|
||||
{
|
||||
cruft::fs::scoped_dir dir (path);
|
||||
success = success && std::filesystem::is_directory (path);
|
||||
}
|
||||
|
||||
success = success && !exists(path);
|
||||
|
||||
tap.expect (success, "scoped_dir removes directory after scope");
|
||||
}
|
||||
|
||||
return tap.status ();
|
||||
}
|
Loading…
Reference in New Issue
Block a user