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_intf.hpp"
12#include "file_view_win32_impl.hpp"
13#include "access_mode.hpp"
14#include "../container/container.hpp"
15#include "../char_maps/char_maps.hpp" // XXX #619
16#include "../utility/utility.hpp"
17#include "../macros.hpp"
18#include <span>
19#include <filesystem>
20#include <memory>
21
22hi_export_module(hikogui.file.file_view);
23
24hi_warning_push();
25// C26490: Don't use reinterpret_cast (type.1).
26// We need to convert bytes to chars to get a string_view from the byte buffer.
27hi_warning_ignore_msvc(26490);
28
29hi_export namespace hi { inline namespace v1 {
30
39class file_view {
40public:
41 ~file_view() = default;
42 constexpr file_view() noexcept = default;
43 file_view(file_view const& other) noexcept = default;
44 file_view(file_view&& other) noexcept = default;
45 file_view& operator=(file_view const& other) noexcept = default;
46 file_view& operator=(file_view&& other) noexcept = default;
47
56 file_view(file const& file, std::size_t offset = 0, std::size_t size = 0) :
57 _pimpl(std::make_shared<detail::file_view_impl>(file.pimpl(), offset, size))
58 {
59 }
60
62 std::filesystem::path const& path,
64 std::size_t offset = 0,
65 std::size_t size = 0) :
66 file_view(file{path, access_mode}, offset, size)
67 {
68 }
69
70 [[nodiscard]] std::size_t offset() const noexcept
71 {
72 hi_assert_not_null(_pimpl);
73 return _pimpl->offset();
74 }
75
76 [[nodiscard]] std::size_t size() const noexcept
77 {
78 hi_assert_not_null(_pimpl);
79 return _pimpl->size();
80 }
81
87 [[nodiscard]] bool unmapped() const noexcept
88 {
89 if (_pimpl) {
90 if (_pimpl->unmapped()) {
91 _pimpl = nullptr;
92 return true;
93 } else {
94 return false;
95 }
96 } else {
97 return true;
98 }
99 }
100
106 explicit operator bool() const noexcept
107 {
108 return not unmapped();
109 }
110
111 void unmap() noexcept
112 {
113 if (auto pimpl = std::exchange(_pimpl, nullptr)) {
114 pimpl->unmap();
115 }
116 }
117
120 [[nodiscard]] void_span void_span() const noexcept
121 {
122 hi_assert_not_null(_pimpl);
123 return _pimpl->void_span();
124 }
125
128 [[nodiscard]] const_void_span const_void_span() const noexcept
129 {
130 hi_assert_not_null(_pimpl);
131 return _pimpl->const_void_span();
132 }
133
138 void flush(hi::void_span span) const noexcept
139 {
140 hi_assert_not_null(_pimpl);
141 return _pimpl->flush(span);
142 }
143
144 template<typename T>
145 [[nodiscard]] friend std::span<T> as_span(file_view const& view) noexcept
146 {
147 if constexpr (std::is_const_v<T>) {
148 return as_span<T>(view.const_void_span());
149 } else {
150 return as_span<T>(view.void_span());
151 }
152 }
153
154 [[nodiscard]] friend std::string_view as_string_view(file_view const& view) noexcept
155 {
156 hi_assert(view.offset() == 0);
157 return as_string_view(view.const_void_span());
158 }
159
160 [[nodiscard]] friend bstring_view as_bstring_view(file_view const& view) noexcept
161 {
162 hi_assert(view.offset() == 0);
163 return as_bstring_view(view.const_void_span());
164 }
165
166private:
168};
169
170}} // namespace hi::v1
171
172hi_warning_pop();
access_mode
The mode in which way to open a file.
Definition access_mode.hpp:17
@ open_for_read
Default open a file for reading.
@ other
The gui_event does not have associated data.
STL namespace.
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
A File object.
Definition file_intf.hpp:33
Map a file into virtual memory.
Definition file_view.hpp:39
void_span void_span() const noexcept
Span to the mapping into memory.
Definition file_view.hpp:120
const_void_span const_void_span() const noexcept
Definition file_view.hpp:128
void flush(hi::void_span span) const noexcept
Flush changes in memory to the open file view.
Definition file_view.hpp:138
file_view(file const &file, std::size_t offset=0, std::size_t size=0)
Create a file-view from a file-mapping.
Definition file_view.hpp:56
bool unmapped() const noexcept
Check if this file view is closed.
Definition file_view.hpp:87