Doxygen XLinks
by
V: 2511R0
Website: doxygen
Loading...
Searching...
No Matches
mappedfile.hpp
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of \dxl - A doxygen post-processor that allows to define smarter
4/// <b>Doxygen</b>-links.
5///
6/// \emoji :copyright: 2025-2026 A-Worx GmbH, Germany.
7/// Published under \ref mainpage_license "Boost Software License".
8//==================================================================================================
9#ifndef HPP_DXL_SEQUENTIAL_FILE_VIEW_TODORENAME
10#define HPP_DXL_SEQUENTIAL_FILE_VIEW_TODORENAME
11#pragma once
12//#include "dxl.hpp"
13#include "ALib.EnumRecords.H"
14#include "ALib.App.CLI.H"
15
16#if !defined(_WIN32)
17 #include <unistd.h>
18#endif
19
20
21namespace dxl {
22
23/// Todox: ai-generated.
24/// RAII file-to-memory loader with optional mmap backend.
25///
26/// This class loads a file into memory and provides a stable pointer + length.
27///
28/// Backend options:
29/// - Portable "read-all" into an internal buffer (always available).
30/// - Optional mmap mapping on non-Windows builds
31///
32/// \warning Performance note:
33/// This class is designed for *sequential* processing of the returned data.
34/// For best results (especially with mmap + MADV_SEQUENTIAL), treat `data()`
35/// as a forward-only byte stream: read from begin to end without repeated
36/// random jumps backwards/forwards.
37///
38/// todos:
39/// - move to alib::files
40/// - change exceptions to ALib or don't throw even.
41/// - use with classes TextFile and TextFileLineReader. Those are using
42/// std::basic_istream<CharT,Traits>::getline
43/// and that's quite slow
44/// - add option to switch between MADV_SEQUENTIAL and MADV_WILLNEED
45///
47 public:
48 /// A minimal sequential-only cursor over the file data.
49 /// Enforces the "forward-only" usage pattern in calling code.
50 class Data {
51 protected:
52 const std::byte* p ; ///< Current pointer position.
53 const std::byte* end; ///< Pointer to the end of the data.
54
55 public:
56 /// Defaulted default-constructor.
57 Data() : p(nullptr), end(nullptr) {}
58
59 /// Constructor.
60 /// @param pData Pointer to the start of the data.
61 /// @param n Size of the data in bytes.
62 Data(const std::byte* pData, std::size_t n) : p(pData), end(p + n) {}
63
64 /// Remaining bytes.
65 /// @return The number of bytes from current position to end.
66 std::size_t Remaining() const noexcept { return static_cast<std::size_t>(end - p); }
67
68 /// True if at end.
69 /// @return \c true if the current position reached #end, \c false otherwise.
70 bool IsEOF() const noexcept { return p >= end; }
71
72 /// Get the next byte and advance.
73 ///
74 /// @tparam TCheck Defaults to #alib::CHK, which is the normal invocation mode.
75 /// If #alib::NC is given, no range check is performed.
76 /// In debug builds, an assertion is raised if the cursor is at end.
77 /// @return The byte at current position.
78 template <typename TCheck = alib::CHK>
79 std::byte Next() {
80 if constexpr ( TCheck::value ) {
81 if (p>= end) return std::byte{0};
82 } else
83 ALIB_ASSERT_ERROR( p < end, "DXL", "NC-Data::Next: @ end." )
84
85 return *p++;
86 }
87
88 /// Get the next byte as a \b char and advance.
89 ///
90 /// @tparam TCheck Defaults to #alib::CHK, which is the normal invocation mode.
91 /// If #alib::NC is given, no range check is performed.
92 /// In debug builds, an assertion is raised if the cursor is at end.
93 /// @return The byte at current position.
94 template <typename TCheck = alib::CHK>
95 char operator()() {
96 if constexpr ( TCheck::value ) {
97 if (p>= end) return char{0};
98 } else
99 ALIB_ASSERT_ERROR( p < end, "DXL", "NC-Data::Next: @ end." )
100
101 return char(*p++);
102 }
103
104 /// Get pointer to current position (read-only).
105 /// @return Pointer to current position.
106 const std::byte* Current() const noexcept { return p; }
107
108 /// Advance by n bytes (must stay within range).
109 ///
110 /// @tparam TCheck Defaults to #alib::CHK, which is the normal invocation mode.
111 /// If #alib::NC is given, no range check is performed.
112 /// In debug builds, an assertion is raised if advancing past end.
113 /// @param n Number of bytes to advance.
114 /// @throws std::out_of_range if advancing past end and \p{TCheck} is #alib::CHK.
115 template <typename TCheck = alib::CHK>
116 void Skip(std::size_t n) {
117 if constexpr ( TCheck::value ) {
118 if (n > Remaining()) throw std::out_of_range("Data advance past end");
119 } else
120 ALIB_ASSERT_ERROR( n <= Remaining(), "DXL", "NC-Data::Skip: past end." )
121 p += n;
122 }
123 };
124
125 protected:
126 std::size_t size = 0; ///< Size of the loaded data.
127 std::vector<std::byte> noMMapBuf; ///< Internal buffer used for fallback read mode.
128 #if defined(_POSIX_MAPPED_FILES) && _POSIX_MAPPED_FILES > 0
129 void* mapAddr = nullptr; ///< Address of the memory-mapped region.
130 #endif
131
132 public:
133 /// Default constructor. Creates an empty view.
134 MappedFile() = default;
135
136 /// Construct and load a file.
137 /// @param path File path.
138 /// @param knownSize File size in bytes if known. Defaults to \c max(), which
139 /// triggers size detection.
140 /// @param preferMmap If true, tries mmap first (when supported), else uses ReadAll.
141 ///
142 /// @throws std::runtime_error on failure to open/read/map the file.
143 MappedFile( const char* path, std::size_t knownSize= std::numeric_limits<std::size_t>::max(),
144 bool preferMmap = true)
145 { Open(path, knownSize, preferMmap); }
146
147 MappedFile(const MappedFile&) = delete; ///< Deleted copy constructor.
148 MappedFile(MappedFile&&) = delete; ///< Deleted move constructor.
149 MappedFile& operator=(const MappedFile&)= delete; ///< Deleted copy assignment operator.
150 MappedFile& operator=(MappedFile&& ) = delete; ///< Deleted assignment operator.
151 ///< @return Void (deleted).
152 ~MappedFile() { Close(); } ///< Destructor. Calls #Close.
153
154 /// Load a file (replaces previous contents).
155 /// @param path The file's path.
156 /// @param knownSize File size in bytes if known. Defaults to \c max() , which
157 /// triggers size detection.
158 /// @param disableMMap If \c true, skips \e mmap mode and uses standard read methods.
159 ///
160 /// @throws std::runtime_error on failure to open/read/map the file.
161 /// @return A #Data object.
162 Data Open( const char* path,
163 std::size_t knownSize= std::numeric_limits<std::size_t>::max(),
164 bool disableMMap = false);
165
166 /// Release resources (unmap / free buffer).
167 void Close() noexcept;
168
169 /// @return \c true if the file was read with \e mmap mode, \c false otherwise.
170 bool IsMMap() const noexcept { return mapAddr != nullptr; }
171
172 /// @return File size in bytes.
173 std::size_t Size() const noexcept { return size; }
174
175 /// @return \c true if the view is empty.
176 bool IsEmpty() const noexcept { return size == 0; }
177};
178
179
180
181} //namespace [dxl]
182
183#endif // HPP_DXL_SEQUENTIAL_FILE_VIEW_TODORENAME
#define ALIB_ASSERT_ERROR(cond, domain,...)
bool IsEOF() const noexcept
const std::byte * p
Current pointer position.
Data()
Defaulted default-constructor.
Data(const std::byte *pData, std::size_t n)
const std::byte * end
Pointer to the end of the data.
std::size_t Remaining() const noexcept
const std::byte * Current() const noexcept
void Skip(std::size_t n)
std::size_t size
Size of the loaded data.
MappedFile(MappedFile &&)=delete
Deleted move constructor.
bool IsEmpty() const noexcept
MappedFile(const MappedFile &)=delete
Deleted copy constructor.
MappedFile(const char *path, std::size_t knownSize=std::numeric_limits< std::size_t >::max(), bool preferMmap=true)
MappedFile & operator=(const MappedFile &)=delete
Deleted copy assignment operator.
std::vector< std::byte > noMMapBuf
Internal buffer used for fallback read mode.
Data Open(const char *path, std::size_t knownSize=std::numeric_limits< std::size_t >::max(), bool disableMMap=false)
~MappedFile()
Destructor. Calls Close.
MappedFile & operator=(MappedFile &&)=delete
void Close() noexcept
Release resources (unmap / free buffer).
bool IsMMap() const noexcept
MappedFile()=default
Default constructor. Creates an empty view.
std::size_t Size() const noexcept
todox
Definition doxyfile.cpp:20