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