libcmo21/LibCmo/VxMath/VxMemoryMappedFile.hpp

76 lines
2.0 KiB
C++
Raw Normal View History

2023-02-25 17:39:39 +08:00
#pragma once
#include "../VTInternal.hpp"
#if YYCC_OS == YYCC_OS_WINDOWS
#include <YYCC/WinImportPrefix.hpp>
2023-02-25 17:39:39 +08:00
#include <Windows.h>
#include <YYCC/WinImportSuffix.hpp>
2023-02-25 17:39:39 +08:00
#else
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
2023-02-25 17:39:39 +08:00
#endif
#include <string>
2023-02-26 21:48:03 +08:00
namespace LibCmo::VxMath {
2023-02-25 17:39:39 +08:00
/**
* @brief Utility class for memory mapped file reading.
* @details The VxMemoryMappedFile can be used have a mapping of a file into a memory buffer for reading purposes.
* @remarks Due to Virtools limitation, we disallow opening any file whose size exceed the maximum value of CKDWORD.
*/
2023-02-25 17:39:39 +08:00
class VxMemoryMappedFile {
private:
#if YYCC_OS == YYCC_OS_WINDOWS
2023-02-25 17:39:39 +08:00
HANDLE m_hFile;
LARGE_INTEGER m_dwFileSize;
2023-02-25 17:39:39 +08:00
HANDLE m_hFileMapping;
LPVOID m_hFileMapView;
#else
int m_hFile;
off_t m_offFileSize;
void* m_pFileAddr;
2023-02-25 17:39:39 +08:00
#endif
std::u8string m_szFilePath;
2023-02-25 17:39:39 +08:00
void* m_pMemoryMappedFileBase;
CKDWORD m_cbFile;
2023-02-25 17:39:39 +08:00
bool m_bIsValid;
2023-02-25 17:39:39 +08:00
public:
/**
* @brief Create a new VxMemoryMappedFile
* @param[in] u8_filepath The path to file to be opened. nullptr is allowed but not suggested.
* Because it must create an invalid VxMemoryMappedFile.
*/
2023-09-17 10:38:46 +08:00
VxMemoryMappedFile(CKSTRING u8_filepath);
~VxMemoryMappedFile();
YYCC_DEL_CLS_COPY_MOVE(VxMemoryMappedFile);
2023-02-25 17:39:39 +08:00
/**
* @brief Returns a pointer to the mapped memory buffer.
* @return The pointer to the mapped memory buffer for reading.
* @remarks
* The returned pointer should not be deleted nor should it be used for writing purpose.
* If you did, it will cause undefined behavior.
* @exception RuntimeException Raised when calling this on an invalid file mapping.
*/
const void* GetBase() const;
/**
* @brief Returns the file size in bytes.
* @return The file size in bytes.
* @exception RuntimeException Raised when calling this on an invalid file mapping.
*/
CKDWORD GetFileSize() const;
/**
* @brief Check whether this file mapping is valid to use.
* @return True if it is, otherwise false.
*/
bool IsValid() const;
2023-02-25 17:39:39 +08:00
};
}