cpp-toolbox  0.0.1
A toolbox library for C++
Loading...
Searching...
No Matches
toolbox::file::memory_mapped_file_t Class Reference

RAII wrapper for memory-mapped files. / 内存映射文件的 RAII 封装。 More...

#include <memory_mapped_file.hpp>

Public Member Functions

 memory_mapped_file_t ()=default
 Default constructor. / 默认构造函数。
 
 ~memory_mapped_file_t ()
 Destructor. Automatically closes the mapped file. / 析构函数。自动关闭映射的文件。
 
 memory_mapped_file_t (const memory_mapped_file_t &)=delete
 Copy constructor (deleted). / 拷贝构造函数(已删除)。
 
memory_mapped_file_toperator= (const memory_mapped_file_t &)=delete
 Copy assignment operator (deleted). / 拷贝赋值运算符(已删除)。
 
 memory_mapped_file_t (memory_mapped_file_t &&)=delete
 Move constructor (deleted). / 移动构造函数(已删除)。
 
memory_mapped_file_toperator= (memory_mapped_file_t &&)=delete
 Move assignment operator (deleted). / 移动赋值运算符(已删除)。
 
bool open (const std::filesystem::path &path)
 Opens and memory-maps the specified file. / 打开并内存映射指定的文件。
 
void close ()
 Closes the memory-mapped file and releases resources. / 关闭内存映射文件并释放资源。
 
const unsigned char * data () const
 Gets a pointer to the mapped memory region. / 获取指向映射内存区域的指针。
 
size_t size () const
 Gets the size of the mapped file. / 获取映射文件的大小。
 
bool is_open () const
 Checks if a file is currently mapped. / 检查文件当前是否已映射。
 

Detailed Description

RAII wrapper for memory-mapped files. / 内存映射文件的 RAII 封装。

This class provides a cross-platform way to map a file into memory for read-only access. It handles the opening, mapping, and closing of the file automatically using RAII.

这个类提供了一种跨平台的方式将文件映射到内存中进行只读访问。 它使用 RAII 自动处理文件的打开、映射和关闭。

#include <iostream>
#include <fstream>
int main() {
const std::filesystem::path test_file = "test_mmap.txt";
// Create a dummy file
{
std::ofstream ofs(test_file);
ofs << "Hello, Memory Map!";
}
if (mapped_file.open(test_file)) {
std::cout << "File mapped successfully. Size: " << mapped_file.size()
<< std::endl;
// Access data (example: print first 5 chars)
const unsigned char* data = mapped_file.data();
if (data && mapped_file.size() >= 5) {
std::cout << "First 5 chars: ";
for(size_t i = 0; i < 5; ++i) {
std::cout << data[i];
}
std::cout << std::endl;
}
// File is automatically unmapped and closed when mapped_file goes
out of scope } else { std::cerr << "Failed to map file: " << test_file <<
std::endl;
}
std::filesystem::remove(test_file); // Clean up
return 0;
}
RAII wrapper for memory-mapped files. / 内存映射文件的 RAII 封装。
Definition memory_mapped_file.hpp:74
size_t size() const
Gets the size of the mapped file. / 获取映射文件的大小。
Definition memory_mapped_file.hpp:237
bool open(const std::filesystem::path &path)
Opens and memory-maps the specified file. / 打开并内存映射指定的文件。
const unsigned char * data() const
Gets a pointer to the mapped memory region. / 获取指向映射内存区域的指针。
Definition memory_mapped_file.hpp:214

Constructor & Destructor Documentation

◆ memory_mapped_file_t() [1/3]

toolbox::file::memory_mapped_file_t::memory_mapped_file_t ( )
default

Default constructor. / 默认构造函数。

// Now you can call open() to map a file

◆ ~memory_mapped_file_t()

toolbox::file::memory_mapped_file_t::~memory_mapped_file_t ( )

Destructor. Automatically closes the mapped file. / 析构函数。自动关闭映射的文件。

Calls close() to ensure all resources are properly released. / 调用 close() 确保所有资源被正确释放。

◆ memory_mapped_file_t() [2/3]

toolbox::file::memory_mapped_file_t::memory_mapped_file_t ( const memory_mapped_file_t )
delete

Copy constructor (deleted). / 拷贝构造函数(已删除)。

◆ memory_mapped_file_t() [3/3]

toolbox::file::memory_mapped_file_t::memory_mapped_file_t ( memory_mapped_file_t &&  )
delete

Move constructor (deleted). / 移动构造函数(已删除)。

Member Function Documentation

◆ close()

void toolbox::file::memory_mapped_file_t::close ( )

Closes the memory-mapped file and releases resources. / 关闭内存映射文件并释放资源。

This is called automatically by the destructor, but can be called manually. / 这个函数由析构函数自动调用,但也可以手动调用。

if (mf.open("data.bin")) {
// Process the file
// ...
// Explicitly close when done
mf.close();
// Can now open another file
mf.open("another_file.bin");
}
void close()
Closes the memory-mapped file and releases resources. / 关闭内存映射文件并释放资源。

◆ data()

const unsigned char * toolbox::file::memory_mapped_file_t::data ( ) const
inline

Gets a pointer to the mapped memory region. / 获取指向映射内存区域的指针。

Returns
A const pointer to the start of the mapped data, or nullptr if not open. / 指向映射数据起始位置的 const 指针,如果未打开则为 nullptr。
if (mf.open("image.dat") && mf.size() >= 1024) {
const unsigned char* img_data = mf.data();
// Now you can read the image data without loading it all into memory
unsigned int header_value = img_data[0] | (img_data[1] << 8) |
(img_data[2] << 16) | (img_data[3] << 24);
}

◆ is_open()

bool toolbox::file::memory_mapped_file_t::is_open ( ) const
inline

Checks if a file is currently mapped. / 检查文件当前是否已映射。

Returns
True if a file is mapped, false otherwise. / 如果文件已映射,返回 true,否则返回 false。
// Check before using
if (!mf.is_open()) {
if (!mf.open("config.dat")) {
std::cerr << "Failed to open config file" << std::endl;
return -1;
}
}
// Now safe to use mf.data() and mf.size()
bool is_open() const
Checks if a file is currently mapped. / 检查文件当前是否已映射。
Definition memory_mapped_file.hpp:258

◆ open()

bool toolbox::file::memory_mapped_file_t::open ( const std::filesystem::path &  path)

Opens and memory-maps the specified file. / 打开并内存映射指定的文件。

If a file is already open, it will be closed first. / 如果已有文件打开,会先关闭。

Parameters
pathThe path to the file to map. / 要映射的文件的路径。
Returns
True if the file was successfully opened and mapped, false otherwise. / 如果文件成功打开并映射,返回 true,否则返回 false。
if (mf.open("my_data.bin")) {
// Use mf.data() and mf.size()
const unsigned char* buffer = mf.data();
size_t file_size = mf.size();
// Process the data
for (size_t i = 0; i < file_size; ++i) {
// Do something with buffer[i]
}
} else {
// Handle error
std::cerr << "Failed to open file" << std::endl;
}

◆ operator=() [1/2]

memory_mapped_file_t & toolbox::file::memory_mapped_file_t::operator= ( const memory_mapped_file_t )
delete

Copy assignment operator (deleted). / 拷贝赋值运算符(已删除)。

◆ operator=() [2/2]

memory_mapped_file_t & toolbox::file::memory_mapped_file_t::operator= ( memory_mapped_file_t &&  )
delete

Move assignment operator (deleted). / 移动赋值运算符(已删除)。

◆ size()

size_t toolbox::file::memory_mapped_file_t::size ( ) const
inline

Gets the size of the mapped file. / 获取映射文件的大小。

Returns
The size of the mapped region in bytes, or 0 if not open. / 映射区域的大小(字节),如果未打开则为 0。
if (mf.open("data.bin")) {
std::cout << "File size: " << mf.size() << " bytes" << std::endl;
// Check if file is large enough for our needs
if (mf.size() < required_size) {
std::cerr << "File is too small" << std::endl;
}
}

The documentation for this class was generated from the following file: