libcosmos
Linux C++ System Programming Library
Loading...
Searching...
No Matches
TempDir.hxx
1#pragma once
2
3// cosmos
4#include <cosmos/fs/filesystem.hxx>
5
6namespace cosmos {
7
9
15class COSMOS_API TempDir {
16public: // functions
17
18 TempDir() = default;
19
20 explicit TempDir(const SysString _template) {
21 create(_template);
22 }
23
24 ~TempDir();
25
26 // Prevent copying due to the path deletion responsibility.
27 TempDir(const TempDir&) = delete;
28 TempDir& operator=(const TempDir&) = delete;
29
30 TempDir(TempDir &&other) noexcept {
31 *this = std::move(other);
32 }
33
34 TempDir& operator=(TempDir &&other) noexcept {
35 m_tmp_path = other.m_tmp_path;
36 other.m_tmp_path.clear();
37 return *this;
38 }
39
40 void close() {
41 if (!m_tmp_path.empty()) {
42 fs::remove_tree(m_tmp_path);
43 m_tmp_path.clear();
44 }
45 }
46
47 void create(const SysString _template);
48
50
54 const std::string& path() const;
55
56protected: // data
57
58 std::string m_tmp_path;
59};
60
61} // end ns
Creation and lifetime management for temporary directories.
Definition TempDir.hxx:15
Wrapper type around a C-style string for use with system APIs.
Definition SysString.hxx:33