Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
io_fileview.h
1#ifndef SOURCEMETA_CORE_IO_FILEVIEW_H_
2#define SOURCEMETA_CORE_IO_FILEVIEW_H_
3
4#ifndef SOURCEMETA_CORE_IO_EXPORT
5#include <sourcemeta/core/io_export.h>
6#endif
7
8#include <cassert> // assert
9#include <cstddef> // std::size_t
10#include <cstdint> // std::uint8_t
11#include <filesystem> // std::filesystem::path
12
13namespace sourcemeta::core {
14
31class SOURCEMETA_CORE_IO_EXPORT FileView {
32public:
34 FileView(const std::filesystem::path &path);
35 ~FileView();
36
37 // Disable copying and moving
38 FileView(const FileView &) = delete;
39 FileView(FileView &&) = delete;
40 auto operator=(const FileView &) -> FileView & = delete;
41 auto operator=(FileView &&) -> FileView & = delete;
42
44 [[nodiscard]] auto size() const noexcept -> std::size_t;
45
49 template <typename T>
50 [[nodiscard]] auto as(const std::size_t offset = 0) const noexcept
51 -> const T * {
52 assert(offset + sizeof(T) <= this->size_);
53 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
54 return reinterpret_cast<const T *>(this->data_ + offset);
55 }
56
57private:
58 const std::uint8_t *data_{nullptr};
59 std::size_t size_{0};
60#if defined(_WIN32)
61 void *file_handle_{nullptr};
62 void *mapping_handle_{nullptr};
63#else
64 int file_descriptor_{-1};
65#endif
66};
67
68} // namespace sourcemeta::core
69
70#endif
FileView(const std::filesystem::path &path)
Memory-map the file at the given path.
auto as(const std::size_t offset=0) const noexcept -> const T *
Definition io_fileview.h:50
auto size() const noexcept -> std::size_t
The size of the memory-mapped data in bytes.