HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
resource_view.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2019, 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_view.hpp"
12#include "../container/container.hpp"
13#include "../utility/utility.hpp"
14#include "../macros.hpp"
15#include <span>
16#include <variant>
17#include <functional>
18#include <cstddef>
19#include <string_view>
20#include <type_traits>
21#include <filesystem>
22#include <memory>
23
24hi_export_module(hikogui.file.resource_view);
25
26hi_export namespace hi { inline namespace v1 {
27
28namespace detail {
29
31public:
32 virtual ~resource_view_base() = default;
33
34 [[nodiscard]] virtual hi::const_void_span const_void_span() const noexcept = 0;
35};
36
37template<typename T>
39public:
40 using value_type = T;
41
42 resource_view_impl(T&& other) noexcept : _value(std::move(other)) {}
43 resource_view_impl(T const& other) noexcept : _value(other) {}
44
45 [[nodiscard]] hi::const_void_span const_void_span() const noexcept override
46 {
47 return _value.const_void_span();
48 }
49
50private:
51 value_type _value;
52};
53
54} // namespace detail
55
65public:
66 const_resource_view() = default;
67 virtual ~const_resource_view() = default;
70 const_resource_view& operator=(const_resource_view const& other) = default;
71 const_resource_view& operator=(const_resource_view&& other) = default;
72
73 template<typename T>
74 const_resource_view(T&& view) noexcept requires requires
75 {
76 std::declval<std::decay_t<T>>().const_void_span();
77 } : _pimpl(std::make_shared<detail::resource_view_impl<std::decay_t<T>>>(std::forward<T>(view))) {}
78
79 const_resource_view(std::filesystem::path const& path) : const_resource_view(file_view{path}) {}
80
81 [[nodiscard]] bool empty() const noexcept
82 {
83 return _pimpl == nullptr;
84 }
85
86 explicit operator bool() const noexcept
87 {
88 return not empty();
89 }
90
93 [[nodiscard]] hi::const_void_span const_void_span() const noexcept
94 {
95 hi_assert_not_null(_pimpl);
96 return _pimpl->const_void_span();
97 }
98
99 template<typename T>
100 [[nodiscard]] friend std::span<T> as_span(const_resource_view const& view) noexcept
101 {
102 static_assert(std::is_const_v<T>);
103 return as_span<T>(view.const_void_span());
104 }
105
106 [[nodiscard]] friend std::string_view as_string_view(const_resource_view const& view) noexcept
107 {
108 return as_string_view(view.const_void_span());
109 }
110
111 [[nodiscard]] friend bstring_view as_bstring_view(const_resource_view const& view) noexcept
112 {
113 return as_bstring_view(view.const_void_span());
114 }
115
116private:
118};
119
120}} // namespace hi::v1
Defines the file_view class.
@ other
The gui_event does not have associated data.
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Map a file into virtual memory.
Definition file_view.hpp:39
Definition resource_view.hpp:30
Definition resource_view.hpp:38
A read-only view of a resource.
Definition resource_view.hpp:64
hi::const_void_span const_void_span() const noexcept
Get a span to the memory mapping.
Definition resource_view.hpp:93
T make_shared(T... args)
T move(T... args)