libcruft-util/fs/scoped.hpp

37 lines
1.2 KiB
C++

/*
* 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;
};
}