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 "../void_span.hpp"
13#include <span>
14#include <variant>
15#include <functional>
16#include <cstddef>
17#include <string_view>
18#include <type_traits>
19
20namespace hi { inline namespace v1 {
21
22namespace detail {
23
25public:
26 virtual ~resource_view_base() = default;
27
28 [[nodiscard]] virtual hi::const_void_span const_void_span() const noexcept = 0;
29};
30
31template<typename T>
33public:
34 using value_type = T;
35
36 resource_view_impl(T&& other) noexcept : _value(std::move(other)) {}
37 resource_view_impl(T const& other) noexcept : _value(other) {}
38
39 [[nodiscard]] hi::const_void_span const_void_span() const noexcept override
40 {
41 return _value.const_void_span();
42 }
43
44private:
45 value_type _value;
46};
47
48} // namespace detail
49
59public:
60 const_resource_view() = default;
61 virtual ~const_resource_view() = default;
64 const_resource_view& operator=(const_resource_view const& other) = default;
65 const_resource_view& operator=(const_resource_view&& other) = default;
66
67 template<typename T>
68 const_resource_view(T&& view) noexcept requires requires
69 {
70 std::declval<std::decay_t<T>>().const_void_span();
71 } : _pimpl(std::make_shared<detail::resource_view_impl<std::decay_t<T>>>(std::forward<T>(view))) {}
72
73 const_resource_view(std::filesystem::path const& path) : const_resource_view(file_view{path}) {}
74
75 [[nodiscard]] bool empty() const noexcept
76 {
77 return _pimpl == nullptr;
78 }
79
80 explicit operator bool() const noexcept
81 {
82 return not empty();
83 }
84
87 [[nodiscard]] hi::const_void_span const_void_span() const noexcept
88 {
89 hi_assert_not_null(_pimpl);
90 return _pimpl->const_void_span();
91 }
92
93 template<typename T>
94 [[nodiscard]] friend std::span<T> as_span(const_resource_view const& view) noexcept
95 {
96 static_assert(std::is_const_v<T>);
97 return as_span<T>(view.const_void_span());
98 }
99
100 [[nodiscard]] friend std::string_view as_string_view(const_resource_view const& view) noexcept
101 {
102 return as_string_view(view.const_void_span());
103 }
104
105 [[nodiscard]] friend bstring_view as_bstring_view(const_resource_view const& view) noexcept
106 {
107 return as_bstring_view(view.const_void_span());
108 }
109
110private:
112};
113
114}} // namespace hi::v1
Defines the file_view class.
#define hi_assert_not_null(x,...)
Assert if an expression is not nullptr.
Definition assert.hpp:238
@ other
The gui_event does not have associated data.
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
Map a file into virtual memory.
Definition file_view.hpp:97
Definition resource_view.hpp:24
Definition resource_view.hpp:32
A read-only view of a resource.
Definition resource_view.hpp:58
hi::const_void_span const_void_span() const noexcept
Get a span to the memory mapping.
Definition resource_view.hpp:87
T make_shared(T... args)
T move(T... args)