-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
112 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,69 @@ | ||
#pragma once | ||
|
||
#include <filesystem> | ||
#include <unistd.h> | ||
#include <utility> | ||
|
||
namespace tmp { | ||
|
||
class path { | ||
protected: | ||
std::filesystem::path p; ///< This file path | ||
|
||
void remove() const noexcept { | ||
if (!this->p.empty()) { | ||
std::error_code ec; | ||
std::filesystem::remove_all(this->p, ec); | ||
} | ||
} | ||
|
||
public: | ||
template<typename C> | ||
explicit path(std::string_view prefix, C constructor) { | ||
const auto parent = std::filesystem::temp_directory_path() / prefix; | ||
std::string arg = parent / "XXXXXX"; | ||
|
||
std::filesystem::create_directories(parent); | ||
constructor(arg.data()); | ||
this->p = arg; | ||
} | ||
|
||
/// Creates a path from a moved @p other | ||
path(path&& other) noexcept: p(std::move(other.p)) { | ||
other.p.clear(); | ||
} | ||
|
||
/// Deletes this path and assigns to it a moved @p other | ||
path& operator=(path&& other) noexcept { | ||
this->remove(); | ||
this->p = std::move(other.p); | ||
other.p.clear(); | ||
return *this; | ||
} | ||
|
||
path(const path&) = delete; ///< not copy-constructible | ||
auto operator=(const path&) = delete; ///< not copy-assignable | ||
|
||
/// Deletes this path when the enclosing scope is exited | ||
~path() noexcept { | ||
this->remove(); | ||
} | ||
|
||
operator const std::filesystem::path&() const noexcept { | ||
return this->p; | ||
} | ||
|
||
const std::filesystem::path* operator->() const noexcept { | ||
return std::addressof(this->p); | ||
} | ||
|
||
friend bool operator==(const path& lhs, const path& rhs) noexcept { | ||
return lhs.p == rhs.p; | ||
} | ||
|
||
friend bool operator!=(const path& lhs, const path& rhs) noexcept { | ||
return lhs.p != rhs.p; | ||
} | ||
}; | ||
|
||
} // namespace tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters