cpp-toolbox  0.0.1
A toolbox library for C++
Loading...
Searching...
No Matches
memory_mapped_file.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstring> // For memcpy, memset
4#include <filesystem> // For file size
5
6#include <cpp-toolbox/cpp-toolbox_export.hpp>
7#include <cpp-toolbox/macro.hpp> // For platform detection CPP_TOOLBOX_PLATFORM_*
8
9// Platform-specific includes for memory mapping / 内存映射的平台特定包含文件
10#ifdef CPP_TOOLBOX_PLATFORM_WINDOWS
11// Define NOMINMAX before including windows.h to prevent min/max macro
12// definitions
13# define NOMINMAX
14# define WIN32_LEAN_AND_MEAN
15# include <windows.h>
16#else // Assuming POSIX-like systems (Linux, macOS) / 假设为类 POSIX
17 // 系统(Linux, macOS)
18# include <fcntl.h> // For open() / 用于 open() 函数
19# include <sys/mman.h> // For mmap(), munmap() / 用于 mmap(), munmap() 函数
20# include <sys/stat.h> // For fstat() / 用于 fstat() 函数
21# include <unistd.h> // For close() / 用于 close() 函数
22#endif
23
24namespace toolbox::file
25{
26
73class CPP_TOOLBOX_EXPORT memory_mapped_file_t
74{
78 void* m_mapped_ptr = nullptr;
79
83 size_t m_mapped_size = 0;
84
85#ifdef CPP_TOOLBOX_PLATFORM_WINDOWS
89 HANDLE m_file_handle = INVALID_HANDLE_VALUE;
90
94 HANDLE m_mapping_handle = NULL;
95#else
99 int m_fd = -1;
100#endif
101
102public:
112
121
122 // Disable copy / 禁用拷贝
127
132
133 // Disable move constructor and assignment / 禁用移动构造和赋值
138
143
172 bool open(const std::filesystem::path& path);
173
195 void close();
196
214 [[nodiscard]] const unsigned char* data() const
215 {
216 return static_cast<const unsigned char*>(m_mapped_ptr);
217 }
218
237 [[nodiscard]] size_t size() const { return m_mapped_size; }
238
258 [[nodiscard]] bool is_open() const { return m_mapped_ptr != nullptr; }
259};
260
261} // namespace toolbox::file
RAII wrapper for memory-mapped files. / 内存映射文件的 RAII 封装。
Definition memory_mapped_file.hpp:74
memory_mapped_file_t & operator=(memory_mapped_file_t &&)=delete
Move assignment operator (deleted). / 移动赋值运算符(已删除)。
~memory_mapped_file_t()
Destructor. Automatically closes the mapped file. / 析构函数。自动关闭映射的文件。
size_t size() const
Gets the size of the mapped file. / 获取映射文件的大小。
Definition memory_mapped_file.hpp:237
memory_mapped_file_t()=default
Default constructor. / 默认构造函数。
memory_mapped_file_t(memory_mapped_file_t &&)=delete
Move constructor (deleted). / 移动构造函数(已删除)。
bool open(const std::filesystem::path &path)
Opens and memory-maps the specified file. / 打开并内存映射指定的文件。
memory_mapped_file_t(const memory_mapped_file_t &)=delete
Copy constructor (deleted). / 拷贝构造函数(已删除)。
bool is_open() const
Checks if a file is currently mapped. / 检查文件当前是否已映射。
Definition memory_mapped_file.hpp:258
void close()
Closes the memory-mapped file and releases resources. / 关闭内存映射文件并释放资源。
memory_mapped_file_t & operator=(const memory_mapped_file_t &)=delete
Copy assignment operator (deleted). / 拷贝赋值运算符(已删除)。
const unsigned char * data() const
Gets a pointer to the mapped memory region. / 获取指向映射内存区域的指针。
Definition memory_mapped_file.hpp:214
通用的编译器、平台、架构检测和实用宏定义 / Common macros for compiler, platform, architecture detection and utility macro...
Definition file.hpp:11