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