HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
file_view_intf.hpp
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_file.hpp"
12#include "file_view_win32_impl.hpp"
13#include "../container/module.hpp"
14#include "../macros.hpp"
15#include <span>
16#include <filesystem>
17#include <memory>
18
19hi_export_module(hikogui.file.file_view : intf);
20
21hi_warning_push();
22// C26490: Don't use reinterpret_cast (type.1).
23// We need to convert bytes to chars to get a string_view from the byte buffer.
24hi_warning_ignore_msvc(26490);
25
26namespace hi { inline namespace v1 {
27
36class file_view {
37public:
38 ~file_view() = default;
39 constexpr file_view() noexcept = default;
40 constexpr file_view(file_view const& other) noexcept = default;
41 constexpr file_view(file_view&& other) noexcept = default;
42 constexpr file_view& operator=(file_view const& other) noexcept = default;
43 constexpr file_view& operator=(file_view&& other) noexcept = default;
44
53 file_view(file const& file, std::size_t offset = 0, std::size_t size = 0) :
54 _pimpl(std::make_shared<detail::file_view_impl>(file.pimpl(), offset, size))
55 {
56 }
57
59 std::filesystem::path const& path,
61 std::size_t offset = 0,
62 std::size_t size = 0) :
63 file_view(file{path, access_mode}, offset, size)
64 {
65 }
66
68 {
69 hi_assert_not_null(_pimpl);
70 return _pimpl->offset();
71 }
72
74 {
75 hi_assert_not_null(_pimpl);
76 return _pimpl->size();
77 }
78
85 {
86 if (_pimpl) {
87 if (_pimpl->unmapped()) {
88 _pimpl = nullptr;
89 return true;
90 } else {
91 return false;
92 }
93 } else {
94 return true;
95 }
96 }
97
103 explicit operator bool() const noexcept
104 {
105 return not unmapped();
106 }
107
108 void unmap() noexcept
109 {
110 if (auto pimpl = std::exchange(_pimpl, nullptr)) {
111 pimpl->unmap();
112 }
113 }
114
118 {
119 hi_assert_not_null(_pimpl);
120 return _pimpl->void_span();
121 }
122
126 {
127 hi_assert_not_null(_pimpl);
128 return _pimpl->const_void_span();
129 }
130
135 void flush(hi::void_span span) const noexcept
136 {
137 hi_assert_not_null(_pimpl);
138 return _pimpl->flush(span);
139 }
140
141 template<typename T>
142 [[nodiscard]] friend std::span<T> as_span(file_view const& view) noexcept
143 {
144 if constexpr (std::is_const_v<T>) {
145 return as_span<T>(view.const_void_span());
146 } else {
147 return as_span<T>(view.void_span());
148 }
149 }
150
151 [[nodiscard]] friend std::string_view as_string_view(file_view const& view) noexcept
152 {
153 hi_assert(view.offset() == 0);
154 return as_string_view(view.const_void_span());
155 }
156
157 [[nodiscard]] friend bstring_view as_bstring_view(file_view const& view) noexcept
158 {
159 hi_assert(view.offset() == 0);
160 return as_bstring_view(view.const_void_span());
161 }
162
163private:
165};
166
167}} // namespace hi::v1
168
169hi_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.
DOXYGEN BUG.
Definition algorithm.hpp:16
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
A File object.
Definition file_file_intf.hpp:30
Map a file into virtual memory.
Definition file_view_intf.hpp:36
void_span void_span() const noexcept
Span to the mapping into memory.
Definition file_view_intf.hpp:117
const_void_span const_void_span() const noexcept
Definition file_view_intf.hpp:125
void flush(hi::void_span span) const noexcept
Flush changes in memory to the open file view.
Definition file_view_intf.hpp:135
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_intf.hpp:53
bool unmapped() const noexcept
Check if this file view is closed.
Definition file_view_intf.hpp:84