SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
seqan3::detail::safe_filesystem_entry Class Reference

A safe guard to manage a filesystem entry, e.g. a file or a directory. More...

#include <seqan3/io/detail/safe_filesystem_entry.hpp>

Public Member Functions

bool remove ()
 Removes a file or empty directory.
 
std::uintmax_t remove_all ()
 Removes a file or directory and all its contents, recursively.
 
bool remove_no_throw () const noexcept
 Removes a file or empty directory.
 
Constructors, destructor and assignment
 safe_filesystem_entry ()=delete
 Deleted.
 
 safe_filesystem_entry (safe_filesystem_entry const &)=delete
 Deleted.
 
 safe_filesystem_entry (safe_filesystem_entry &&)=default
 Defaulted.
 
safe_filesystem_entryoperator= (safe_filesystem_entry const &)=delete
 Deleted.
 
safe_filesystem_entryoperator= (safe_filesystem_entry &&)=default
 Defaulted.
 
 safe_filesystem_entry (std::filesystem::path p)
 Constructs the safe guard from a std::filesystem::path.
 
 ~safe_filesystem_entry ()
 Calls std::filesystem::remove_all on the wrapped entry.
 

Private Attributes

std::filesystem::path entry
 The managed resource.
 

Detailed Description

A safe guard to manage a filesystem entry, e.g. a file or a directory.

This raii-wrapper class allows for a safe removal of a created filesystem entry such as a directory or file. This wrapper class assumes owning semantics. It is not copy-constructible or copy-assignable. In order to prevent misuse also the default constructor is deleted.

The following example demonstrates the use case.

// SPDX-FileCopyrightText: 2006-2024 Knut Reinert & Freie Universität Berlin
// SPDX-FileCopyrightText: 2016-2024 Knut Reinert & MPI für molekulare Genetik
// SPDX-License-Identifier: CC0-1.0
#include <filesystem>
#include <fstream>
int main()
{
std::ifstream file{my_file}; // Create the file.
seqan3::detail::safe_filesystem_entry file_guard{my_file}; // Safe cleanup in case of errors.
// Do something on the file, that can possibly throw.
// If an unhandled exception is thrown, the file guard destructor safely removes the file from the filesystem.
file_guard.remove(); // Explicitly remove the file.
}
A safe guard to manage a filesystem entry, e.g. a file or a directory.
Definition safe_filesystem_entry.hpp:35
Provides seqan3::detail::safe_filesystem_entry.
T temp_directory_path(T... args)

Constructor & Destructor Documentation

◆ safe_filesystem_entry()

seqan3::detail::safe_filesystem_entry::safe_filesystem_entry ( std::filesystem::path  p)
inline

Constructs the safe guard from a std::filesystem::path.

Parameters
pThe path pointing to a filesystem entry.

Member Function Documentation

◆ remove()

bool seqan3::detail::safe_filesystem_entry::remove ( )
inline

Removes a file or empty directory.

Returns
true if the file was deleted, false if it did not exist.
Exceptions
std::filesystem::filesystem_erroron underlying OS API errors.

Internally calls std::filesystem::remove on the stored std::filesystem::path.

◆ remove_all()

std::uintmax_t seqan3::detail::safe_filesystem_entry::remove_all ( )
inline

Removes a file or directory and all its contents, recursively.

Returns
Returns the number of files and directories that were deleted (which may be zero if p did not exist to begin with).
Exceptions
std::filesystem::filesystem_erroron underlying OS API errors.

Internally calls std::filesystem::remove_all on the stored std::filesystem::path.

◆ remove_no_throw()

bool seqan3::detail::safe_filesystem_entry::remove_no_throw ( ) const
inlinenoexcept

Removes a file or empty directory.

Returns
true if the file was deleted, false if it did not exist.
Exceptions
std::filesystem::filesystem_erroron underlying OS API errors.

Internally calls std::filesystem::remove on the stored std::filesystem::path.


The documentation for this class was generated from the following file:
Hide me