NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
temp_file类 参考

临时文件管理类 更多...

#include <temp_file.hpp>

Public 类型

enum class  delete_policy { AUTO_DELETE , MANUAL_DELETE , KEEP_ON_EXIT }
 临时文件删除策略 更多...

Public 成员函数

 temp_file (const string &prefix="tmp", const string &suffix=".tmp", file_creation mode=file_creation::CREATE_FORCE, delete_policy policy=delete_policy::AUTO_DELETE)
 构造函数:创建新的临时文件
 temp_file (const path &existing_path, delete_policy policy=delete_policy::AUTO_DELETE)
 构造函数:接管已有的文件作为临时文件
 ~temp_file ()
 析构函数
 temp_file (temp_file &&other) noexcept
 移动构造函数
temp_fileoperator= (temp_file &&other) noexcept
 移动赋值运算符
NEFORCE_NODISCARD _NEFORCE filefile () noexcept
 获取内部文件对象
NEFORCE_NODISCARD const _NEFORCE filefile () const noexcept
 获取内部常量文件对象
void keep () noexcept
 设置删除策略为 KEEP_ON_EXIT
void set_delete_policy (const delete_policy policy) noexcept
 设置删除策略
NEFORCE_NODISCARD delete_policy policy () const noexcept
 获取当前删除策略
void cleanup () noexcept
 手动清理临时文件
void release ()
 释放临时文件的所有权

静态 Public 成员函数

static NEFORCE_NODISCARD temp_file create_temp_file (const string &prefix="tmp", const string &suffix=".tmp", file_creation mode=file_creation::CREATE_FORCE)
 创建临时文件(工厂函数)
static void cleanup_all_temp_files ()
 清理所有已注册的临时文件
static void register_for_cleanup (const path &temp_path)
 注册一个路径以供程序退出时清理

详细描述

临时文件管理类

自动创建和销毁临时文件。 所有创建的临时文件会在程序退出时通过全局清理函数尝试删除。

在文件 temp_file.hpp28 行定义.

成员枚举类型说明

◆ delete_policy

enum class temp_file::delete_policy
strong

临时文件删除策略

枚举值
AUTO_DELETE 

析构自动删除

MANUAL_DELETE 

手动删除

KEEP_ON_EXIT 

程序退出时保留

在文件 temp_file.hpp34 行定义.

构造及析构函数说明

◆ temp_file() [1/3]

temp_file::temp_file ( const string & prefix = "tmp",
const string & suffix = ".tmp",
file_creation mode = file_creation::CREATE_FORCE,
delete_policy policy = delete_policy::AUTO_DELETE )
explicit

构造函数:创建新的临时文件

参数
prefix文件名前缀(默认 "tmp")
suffix文件名后缀(默认 ".tmp")
mode文件创建模式(默认 CREATE_FORCE)
policy删除策略(默认 AUTO_DELETE)
异常
system_exception文件创建失败时抛出

在系统临时目录中生成一个唯一文件名,并打开文件。 生成规则:{prefix}_{纳秒时间}_{进程ID}_{线程ID}_{随机数}{suffix}

引用了 AUTO_DELETE, CREATE_FORCE , 以及 policy().

被这些函数引用 create_temp_file(), operator=(), temp_file() , 以及 ~temp_file().

◆ temp_file() [2/3]

temp_file::temp_file ( const path & existing_path,
delete_policy policy = delete_policy::AUTO_DELETE )
explicit

构造函数:接管已有的文件作为临时文件

参数
existing_path已有文件的路径
policy删除策略(默认 AUTO_DELETE)
异常
system_exception文件打开失败时抛出

将指定的现有文件作为临时文件管理,不会生成新文件。

引用了 AUTO_DELETE , 以及 policy().

◆ ~temp_file()

temp_file::~temp_file ( )

析构函数

根据删除策略决定是否删除临时文件:

  • AUTO_DELETE: 立即删除文件并从全局注册表中移除
  • MANUAL_DELETE/KEEP_ON_EXIT: 仅关闭文件,不删除

引用了 temp_file().

◆ temp_file() [3/3]

temp_file::temp_file ( temp_file && other)
noexcept

移动构造函数

参数
other被移动的对象

转移文件所有权,原对象的删除策略被设置为 KEEP_ON_EXIT。

引用了 temp_file().

成员函数说明

◆ cleanup()

void temp_file::cleanup ( )
noexcept

手动清理临时文件

根据当前删除策略执行清理:

  • 如果策略为 AUTO_DELETE,则删除文件并从注册表中移除
  • 其他策略仅关闭文件 此操作后文件对象将处于关闭状态。

◆ cleanup_all_temp_files()

void temp_file::cleanup_all_temp_files ( )
static

清理所有已注册的临时文件

遍历全局注册表,删除所有记录的临时文件。 通常在程序退出时由atexit注册的函数调用。

引用了 cleanup_all_temp_files().

被这些函数引用 cleanup_all_temp_files().

◆ create_temp_file()

NEFORCE_NODISCARD temp_file temp_file::create_temp_file ( const string & prefix = "tmp",
const string & suffix = ".tmp",
file_creation mode = file_creation::CREATE_FORCE )
static

创建临时文件(工厂函数)

参数
prefix文件名前缀(默认 "tmp")
suffix文件名后缀(默认 ".tmp")
mode文件创建模式(默认 CREATE_FORCE)
返回
新创建的临时文件对象

等价于调用带 AUTO_DELETE 策略的构造函数。

引用了 create_temp_file() , 以及 temp_file().

被这些函数引用 create_temp_file().

◆ file() [1/2]

NEFORCE_NODISCARD const _NEFORCE file & temp_file::file ( ) const
inlinenoexcept

获取内部常量文件对象

返回
文件对象常量引用

在文件 temp_file.hpp109 行定义.

引用了 file().

◆ file() [2/2]

NEFORCE_NODISCARD _NEFORCE file & temp_file::file ( )
inlinenoexcept

获取内部文件对象

返回
文件对象引用

在文件 temp_file.hpp103 行定义.

引用了 file().

被这些函数引用 file() , 以及 file().

◆ keep()

void temp_file::keep ( )
inlinenoexcept

设置删除策略为 KEEP_ON_EXIT

调用后,析构时不会删除文件,也不从全局注册表中移除。

在文件 temp_file.hpp116 行定义.

引用了 KEEP_ON_EXIT.

◆ operator=()

temp_file & temp_file::operator= ( temp_file && other)
noexcept

移动赋值运算符

参数
other被移动的对象
返回
自身引用

先清理当前资源,再转移所有权。

引用了 temp_file().

◆ policy()

NEFORCE_NODISCARD delete_policy temp_file::policy ( ) const
inlinenoexcept

获取当前删除策略

返回
删除策略

在文件 temp_file.hpp128 行定义.

被这些函数引用 set_delete_policy(), temp_file() , 以及 temp_file().

◆ register_for_cleanup()

void temp_file::register_for_cleanup ( const path & temp_path)
static

注册一个路径以供程序退出时清理

参数
temp_path临时文件路径

将路径添加到全局注册表,在 cleanup_all_temp_files 中被清理。 通常由 temp_file 构造函数自动调用。

引用了 register_for_cleanup().

被这些函数引用 register_for_cleanup().

◆ release()

void temp_file::release ( )

释放临时文件的所有权

将删除策略设置为 MANUAL_DELETE,并从全局注册表中移除, 但保持文件打开状态,析构时不会自动删除。

引用了 release().

被这些函数引用 release().

◆ set_delete_policy()

void temp_file::set_delete_policy ( const delete_policy policy)
inlinenoexcept

设置删除策略

参数
policy新的删除策略

在文件 temp_file.hpp122 行定义.

引用了 policy().


该类的文档由以下文件生成: