HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
file_view.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2019, 2021-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
9#pragma once
10
11#include "file.hpp"
12#include "URL.hpp"
13#include "../void_span.hpp"
14#include <span>
15#include <filesystem>
16#include <memory>
17
18hi_warning_push();
19// C26490: Don't use reinterpret_cast (type.1).
20// We need to convert bytes to chars to get a string_view from the byte buffer.
21hi_warning_ignore_msvc(26490);
22
23namespace hi { inline namespace v1 {
24
25namespace detail {
26
28public:
29 file_view_impl() = delete;
30 file_view_impl(file_view_impl const&) = delete;
32 file_view_impl& operator=(file_view_impl const&) = delete;
33 file_view_impl& operator=(file_view_impl&&) = delete;
34
35 virtual ~file_view_impl() = default;
37 _file(std::move(file)), _offset(offset), _size(size)
38 {
39 if (_size == 0) {
40 _size = _file->size() - _offset;
41 }
42 }
43
44 [[nodiscard]] std::size_t offset() const noexcept
45 {
46 return _offset;
47 }
48
49 [[nodiscard]] std::size_t size() const noexcept
50 {
51 return _size;
52 }
53
54 [[nodiscard]] hi::access_mode access_mode() const noexcept
55 {
56 hi_assert_not_null(_file);
57 return _file->access_mode();
58 }
59
60 [[nodiscard]] void_span void_span() const noexcept
61 {
62 hi_assert_not_null(_file);
63 hi_assert(to_bool(_file->access_mode() & access_mode::write));
64 return {_data, _size};
65 }
66
67 [[nodiscard]] const_void_span const_void_span() const noexcept
68 {
69 return {_data, _size};
70 }
71
72 [[nodiscard]] virtual bool unmapped() const noexcept = 0;
73 virtual void flush(hi::void_span span) const noexcept = 0;
74 virtual void unmap() = 0;
75
76protected:
77 mutable std::shared_ptr<file_impl> _file;
78 std::size_t _offset;
79 std::size_t _size;
80 void *_data = nullptr;
81};
82
83} // namespace detail
84
97class file_view {
98public:
99 ~file_view() = default;
100 constexpr file_view() noexcept = default;
101 constexpr file_view(file_view const& other) noexcept = default;
102 constexpr file_view(file_view&& other) noexcept = default;
103 constexpr file_view& operator=(file_view const& other) noexcept = default;
104 constexpr file_view& operator=(file_view&& other) noexcept = default;
105
114 file_view(file const& file, std::size_t offset = 0, std::size_t size = 0);
115
116 file_view(
117 std::filesystem::path const& path,
119 std::size_t offset = 0,
120 std::size_t size = 0) :
121 file_view(file{path, access_mode}, offset, size)
122 {
123 }
124
125 [[nodiscard]] std::size_t offset() const noexcept
126 {
127 hi_assert_not_null(_pimpl);
128 return _pimpl->offset();
129 }
130
131 [[nodiscard]] std::size_t size() const noexcept
132 {
133 hi_assert_not_null(_pimpl);
134 return _pimpl->size();
135 }
136
142 [[nodiscard]] bool unmapped() const noexcept
143 {
144 if (_pimpl) {
145 if (_pimpl->unmapped()) {
146 _pimpl = nullptr;
147 return true;
148 } else {
149 return false;
150 }
151 } else {
152 return true;
153 }
154 }
155
161 explicit operator bool() const noexcept
162 {
163 return not unmapped();
164 }
165
166 void unmap() noexcept
167 {
168 if (auto pimpl = std::exchange(_pimpl, nullptr)) {
169 pimpl->unmap();
170 }
171 }
172
175 [[nodiscard]] void_span void_span() const noexcept
176 {
177 hi_assert_not_null(_pimpl);
178 return _pimpl->void_span();
179 }
180
183 [[nodiscard]] const_void_span const_void_span() const noexcept
184 {
185 hi_assert_not_null(_pimpl);
186 return _pimpl->const_void_span();
187 }
188
193 void flush(hi::void_span span) const noexcept
194 {
195 hi_assert_not_null(_pimpl);
196 return _pimpl->flush(span);
197 }
198
199 template<typename T>
200 [[nodiscard]] friend std::span<T> as_span(file_view const& view) noexcept
201 {
202 if constexpr (std::is_const_v<T>) {
203 return as_span<T>(view.const_void_span());
204 } else {
205 return as_span<T>(view.void_span());
206 }
207 }
208
209 [[nodiscard]] friend std::string_view as_string_view(file_view const& view) noexcept
210 {
211 hi_assert(view.offset() == 0);
212 return as_string_view(view.const_void_span());
213 }
214
215 [[nodiscard]] friend bstring_view as_bstring_view(file_view const& view) noexcept
216 {
217 hi_assert(view.offset() == 0);
218 return as_bstring_view(view.const_void_span());
219 }
220
221private:
223};
224
225}} // namespace hi::v1
226
227hi_warning_pop();
Defines the file class.
Defines the URL class.
#define hi_assert(expression,...)
Assert if expression is true.
Definition assert.hpp:184
#define hi_assert_not_null(x,...)
Assert if an expression is not nullptr.
Definition assert.hpp:223
access_mode
The mode in which way to open a file.
Definition file.hpp:35
@ open_for_read
Default open a file for reading.
@ write
Allow write access to a file.
@ other
The gui_event does not have associated data.
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
A File object.
Definition file.hpp:105
Definition file_view.hpp:27
Map a file into virtual memory.
Definition file_view.hpp:97
void_span void_span() const noexcept
Span to the mapping into memory.
Definition file_view.hpp:175
const_void_span const_void_span() const noexcept
Definition file_view.hpp:183
void flush(hi::void_span span) const noexcept
Flush changes in memory to the open file view.
Definition file_view.hpp:193
file_view(file const &file, std::size_t offset=0, std::size_t size=0)
Create a file-view from a file-mapping.
bool unmapped() const noexcept
Check if this file view is closed.
Definition file_view.hpp:142
T move(T... args)