🛰️航天仿真算法库 SpaceAST
0.3.1
载入中...
搜索中...
未找到
TempFileGuard.hpp
浏览该文件的文档.
1
20
21
#pragma once
22
23
#include "AstGlobal.h"
24
#include "AstUtil/StringView.hpp"
25
#include "AstUtil/FileSystem.hpp"
26
#include "AstUtil/Logger.hpp"
27
#include <string>
28
#include <system_error>
29
#include <utility>
30
31
AST_NAMESPACE_BEGIN
32
42
class
TempFileGuard
43
{
44
public
:
46
TempFileGuard
() =
default
;
47
50
explicit
TempFileGuard
(
StringView
path)
51
: path_(std::string(path))
52
{}
53
54
explicit
TempFileGuard
(
const
fs::path
& path)
55
: path_(path)
56
{}
57
58
explicit
TempFileGuard(
const
std::string& path)
59
: path_(path)
60
{}
61
63
~TempFileGuard
()
64
{
65
remove();
66
}
67
69
void
remove
()
70
{
71
if
(path_.empty())
72
return
;
73
std::error_code ec;
74
fs::remove(path_, ec);
75
if
(ec) {
76
aError(
_
(
"删除临时文件 '%s' 失败: %s"
), path_.c_str(), ec.message().c_str());
77
}
78
path_ =
fs::path
();
79
}
80
82
void
release
()
83
{
84
path_ =
fs::path
();
85
}
86
88
bool
isActive
()
const
{
return
!path_.empty(); }
89
91
bool
exists
()
const
92
{
93
if
(path_.empty())
94
return
false
;
95
std::error_code ec;
96
return
fs::exists(path_, ec);
97
}
98
100
fs::path
&
path
() {
return
path_; }
101
103
operator
bool()
const
{
return
isActive(); }
104
105
protected
:
106
A_DISABLE_COPY(
TempFileGuard
);
107
108
fs::path
path_{};
109
};
110
111
114
AST_NAMESPACE_END
ast::StringViewBasic< char >
ast::TempFileGuard
临时文件守卫
定义
TempFileGuard.hpp:43
ast::TempFileGuard::exists
bool exists() const
文件当前是否存在
定义
TempFileGuard.hpp:91
ast::TempFileGuard::~TempFileGuard
~TempFileGuard()
析构时删除临时文件
定义
TempFileGuard.hpp:63
ast::TempFileGuard::TempFileGuard
TempFileGuard()=default
构造为空的守卫,不持有任何文件
ast::TempFileGuard::remove
void remove()
立即删除临时文件并解除析构时的删除
定义
TempFileGuard.hpp:69
ast::TempFileGuard::release
void release()
释放文件所有权,保留文件并解除析构时的删除
定义
TempFileGuard.hpp:82
ast::TempFileGuard::isActive
bool isActive() const
是否仍将在析构时删除文件(即是否持有待删除的文件)
定义
TempFileGuard.hpp:88
ast::TempFileGuard::TempFileGuard
TempFileGuard(StringView path)
构造并接管指定临时文件
定义
TempFileGuard.hpp:50
ast::TempFileGuard::path
fs::path & path()
获取文件路径(释放或移除后为空)
定义
TempFileGuard.hpp:100
ast::fs_simple::path
定义
FileSystemSimple.hpp:67
ast::_
A_ALWAYS_INLINE const char * _(const char *msgid)
翻译文本(无上下文)
定义
I18n.hpp:88
src
AstUtil
Util
TempFileGuard.hpp
制作者
1.12.0