HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
URL.hpp
1// Copyright Take Vos 2019-2020.
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
5#pragma once
6
7#include "required.hpp"
8#include "assert.hpp"
9#include <string>
10#include <string_view>
11#include <optional>
12#include <vector>
13#include <unordered_map>
14#include <memory>
15#include <ostream>
16#include <mutex>
17
18namespace hi::inline v1 {
19struct url_parts;
20class resource_view;
21
47class URL {
48public:
49 constexpr ~URL() = default;
50 constexpr URL() noexcept = default;
51 constexpr URL(URL const &) noexcept = default;
52 constexpr URL(URL &&) noexcept = default;
53 constexpr URL &operator=(URL const &) noexcept = default;
54 constexpr URL &operator=(URL &&) noexcept = default;
55
56 explicit URL(std::string_view url);
57 explicit URL(char const *url);
58 explicit URL(std::string const& url);
59 explicit URL(url_parts const& parts);
60
61 explicit operator bool() const noexcept
62 {
63 return not empty();
64 }
65
66 [[nodiscard]] bool empty() const noexcept
67 {
68 return value.empty();
69 }
70
71 [[nodiscard]] std::size_t hash() const noexcept;
72
73 [[nodiscard]] std::string_view scheme() const noexcept;
74
75 [[nodiscard]] std::string query() const noexcept;
76
77 [[nodiscard]] std::string fragment() const noexcept;
78
79 [[nodiscard]] std::string filename() const noexcept;
80
81 [[nodiscard]] std::string directory() const noexcept;
82
83 [[nodiscard]] std::string nativeDirectory() const noexcept;
84
85 [[nodiscard]] std::string extension() const noexcept;
86
87 [[nodiscard]] std::vector<std::string> pathSegments() const noexcept;
88
89 [[nodiscard]] std::string path() const noexcept;
90
91 [[nodiscard]] std::string nativePath() const noexcept;
92
93 [[nodiscard]] std::wstring nativeWPath() const noexcept;
94
95 [[nodiscard]] bool isFileScheme() const noexcept
96 {
97 return scheme() == "file";
98 }
99
100 [[nodiscard]] bool isAbsolute() const noexcept;
101 [[nodiscard]] bool isRelative() const noexcept;
102 [[nodiscard]] bool isRootDirectory() const noexcept;
103
104 [[nodiscard]] URL urlByAppendingPath(URL const &other) const noexcept;
105
106 [[nodiscard]] URL urlByAppendingPath(std::string_view const other) const noexcept;
107 [[nodiscard]] URL urlByAppendingPath(std::string const &other) const noexcept;
108 [[nodiscard]] URL urlByAppendingPath(char const *other) const noexcept;
109
110 [[nodiscard]] URL urlByAppendingPath(std::wstring_view const other) const noexcept;
111 [[nodiscard]] URL urlByAppendingPath(std::wstring const &other) const noexcept;
112 [[nodiscard]] URL urlByAppendingPath(wchar_t const *other) const noexcept;
113
114 [[nodiscard]] URL urlByAppendingExtension(std::string_view other) const noexcept;
115 [[nodiscard]] URL urlByAppendingExtension(std::string const &other) const noexcept;
116 [[nodiscard]] URL urlByAppendingExtension(char const *other) const noexcept;
117
118 [[nodiscard]] URL urlByRemovingFilename() const noexcept;
119
124
135 [[nodiscard]] std::vector<URL> urlsByScanningWithGlobPattern() const noexcept;
136
137 [[nodiscard]] static URL urlFromPath(std::string_view const path) noexcept;
138 [[nodiscard]] static URL urlFromWPath(std::wstring_view const path) noexcept;
139
140 static void setUrlForCurrentWorkingDirectory(URL url) noexcept;
141 [[nodiscard]] static URL urlFromCurrentWorkingDirectory() noexcept;
142 [[nodiscard]] static URL urlFromResourceDirectory() noexcept;
143 [[nodiscard]] static URL urlFromExecutableDirectory() noexcept;
144 [[nodiscard]] static URL urlFromExecutableFile() noexcept;
145 [[nodiscard]] static URL urlFromApplicationDataDirectory() noexcept;
146 [[nodiscard]] static URL urlFromApplicationLogDirectory() noexcept;
147 [[nodiscard]] static URL urlFromSystemfontDirectory() noexcept;
148 [[nodiscard]] static URL urlFromApplicationPreferencesFile() noexcept;
149
154 [[nodiscard]] static std::vector<std::string> filenamesByScanningDirectory(std::string_view path) noexcept;
155
156 [[nodiscard]] static std::string nativePathFromPath(std::string_view path) noexcept;
157 [[nodiscard]] static std::wstring nativeWPathFromPath(std::string_view path) noexcept;
158
159 [[nodiscard]] friend bool operator==(URL const &lhs, URL const &rhs) noexcept
160 {
161 return lhs.value == rhs.value;
162 }
163
164 [[nodiscard]] friend auto operator<=>(URL const &lhs, URL const &rhs) noexcept
165 {
166 return lhs.value <=> rhs.value;
167 }
168
169 [[nodiscard]] friend URL operator/(URL const &lhs, URL const &rhs) noexcept
170 {
171 return lhs.urlByAppendingPath(rhs);
172 }
173
174 [[nodiscard]] friend URL operator/(URL const &lhs, std::string_view const &rhs) noexcept
175 {
176 return lhs.urlByAppendingPath(URL{rhs});
177 }
178
179 [[nodiscard]] friend std::string const &to_string(URL const &url) noexcept
180 {
181 return url.value;
182 }
183
184 friend std::ostream &operator<<(std::ostream &lhs, const URL &rhs)
185 {
186 return lhs << to_string(rhs);
187 }
188
189private:
190 std::string value;
191
192 static URL _urlOfCurrentWorkingDirectory;
193};
194
195} // namespace hi::inline v1
196
197template<>
198class std::hash<hi::URL> {
199public:
200 std::size_t operator()(hi::URL const &url) const noexcept
201 {
202 return url.hash();
203 }
204};
205
206template<typename CharT>
207struct std::formatter<hi::URL, CharT> : std::formatter<std::string_view, CharT> {
208 auto format(hi::URL const &t, auto &fc)
209 {
210 return std::formatter<std::string_view, CharT>::format(to_string(t), fc);
211 }
212};
This file includes required definitions.
STL namespace.
Definition URL.hpp:47
std::vector< URL > urlsByScanningWithGlobPattern() const noexcept
std::unique_ptr< resource_view > loadView() const
Load a resource.
Definition url_parser.hpp:90
T operator()(T... args)
Definition datum.hpp:2458
T to_string(T... args)