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 tt {
19struct url_parts;
20class resource_view;
21
47class URL {
48public:
49 constexpr URL() noexcept = default;
50 explicit URL(std::string_view url);
51 explicit URL(char const *url);
52 explicit URL(std::string const &url);
53 explicit URL(url_parts const &parts);
54
55 URL(URL const &other) noexcept : value(other.value)
56 {
57 tt_axiom(&other != this);
58 }
59 URL(URL &&other) noexcept = default;
60 URL &operator=(URL const &other) noexcept
61 {
62 // Self-assignment is allowed.
63 value = other.value;
64 return *this;
65 }
66
67 URL &operator=(URL &&other) noexcept = default;
68
69 explicit operator bool() const noexcept
70 {
71 return not empty();
72 }
73
74 [[nodiscard]] bool empty() const noexcept
75 {
76 return value.empty();
77 }
78
79 [[nodiscard]] size_t hash() const noexcept;
80
81 [[nodiscard]] std::string_view scheme() const noexcept;
82
83 [[nodiscard]] std::string query() const noexcept;
84
85 [[nodiscard]] std::string fragment() const noexcept;
86
87 [[nodiscard]] std::string filename() const noexcept;
88
89 [[nodiscard]] std::string directory() const noexcept;
90
91 [[nodiscard]] std::string nativeDirectory() const noexcept;
92
93 [[nodiscard]] std::string extension() const noexcept;
94
95 [[nodiscard]] std::vector<std::string> pathSegments() const noexcept;
96
97 [[nodiscard]] std::string path() const noexcept;
98
99 [[nodiscard]] std::string nativePath() const noexcept;
100
101 [[nodiscard]] std::wstring nativeWPath() const noexcept;
102
103 [[nodiscard]] bool isFileScheme() const noexcept
104 {
105 return scheme() == "file";
106 }
107
108 [[nodiscard]] bool isAbsolute() const noexcept;
109 [[nodiscard]] bool isRelative() const noexcept;
110 [[nodiscard]] bool isRootDirectory() const noexcept;
111
112 [[nodiscard]] URL urlByAppendingPath(URL const &other) const noexcept;
113
114 [[nodiscard]] URL urlByAppendingPath(std::string_view const other) const noexcept;
115 [[nodiscard]] URL urlByAppendingPath(std::string const &other) const noexcept;
116 [[nodiscard]] URL urlByAppendingPath(char const *other) const noexcept;
117
118 [[nodiscard]] URL urlByAppendingPath(std::wstring_view const other) const noexcept;
119 [[nodiscard]] URL urlByAppendingPath(std::wstring const &other) const noexcept;
120 [[nodiscard]] URL urlByAppendingPath(wchar_t const *other) const noexcept;
121
122 [[nodiscard]] URL urlByAppendingExtension(std::string_view other) const noexcept;
123 [[nodiscard]] URL urlByAppendingExtension(std::string const &other) const noexcept;
124 [[nodiscard]] URL urlByAppendingExtension(char const *other) const noexcept;
125
126 [[nodiscard]] URL urlByRemovingFilename() const noexcept;
127
132
143 [[nodiscard]] std::vector<URL> urlsByScanningWithGlobPattern() const noexcept;
144
145 [[nodiscard]] static URL urlFromPath(std::string_view const path) noexcept;
146 [[nodiscard]] static URL urlFromWPath(std::wstring_view const path) noexcept;
147
148 static void setUrlForCurrentWorkingDirectory(URL url) noexcept;
149 [[nodiscard]] static URL urlFromCurrentWorkingDirectory() noexcept;
150 [[nodiscard]] static URL urlFromResourceDirectory() noexcept;
151 [[nodiscard]] static URL urlFromExecutableDirectory() noexcept;
152 [[nodiscard]] static URL urlFromExecutableFile() noexcept;
153 [[nodiscard]] static URL urlFromApplicationDataDirectory() noexcept;
154 [[nodiscard]] static URL urlFromApplicationLogDirectory() noexcept;
155 [[nodiscard]] static URL urlFromSystemfontDirectory() noexcept;
156 [[nodiscard]] static URL urlFromApplicationPreferencesFile() noexcept;
157
162 [[nodiscard]] static std::vector<std::string> filenamesByScanningDirectory(std::string_view path) noexcept;
163
164 [[nodiscard]] static std::string nativePathFromPath(std::string_view path) noexcept;
165 [[nodiscard]] static std::wstring nativeWPathFromPath(std::string_view path) noexcept;
166
167 [[nodiscard]] friend bool operator==(URL const &lhs, URL const &rhs) noexcept
168 {
169 return lhs.value == rhs.value;
170 }
171
172 [[nodiscard]] friend auto operator<=>(URL const &lhs, URL const &rhs) noexcept
173 {
174 return lhs.value <=> rhs.value;
175 }
176
177 [[nodiscard]] friend URL operator/(URL const &lhs, URL const &rhs) noexcept
178 {
179 return lhs.urlByAppendingPath(rhs);
180 }
181
182 [[nodiscard]] friend URL operator/(URL const &lhs, std::string_view const &rhs) noexcept
183 {
184 return lhs.urlByAppendingPath(URL{rhs});
185 }
186
187 [[nodiscard]] friend std::string const &to_string(URL const &url) noexcept
188 {
189 return url.value;
190 }
191
192 friend std::ostream &operator<<(std::ostream &lhs, const URL &rhs)
193 {
194 return lhs << to_string(rhs);
195 }
196
197private:
198 std::string value;
199
200 static URL _urlOfCurrentWorkingDirectory;
201};
202
203} // namespace tt
204
205namespace std {
206
207template<>
208class hash<tt::URL> {
209public:
210 size_t operator()(tt::URL const &url) const noexcept
211 {
212 return url.hash();
213 }
214};
215
216template<typename CharT>
217struct std::formatter<tt::URL, CharT> : std::formatter<std::string_view, CharT> {
218 auto format(tt::URL const &t, auto &fc)
219 {
220 return std::formatter<std::string_view, CharT>::format(to_string(t), fc);
221 }
222};
223
224} // namespace std
STL namespace.
Definition URL.hpp:47
std::vector< URL > urlsByScanningWithGlobPattern() const noexcept
static std::vector< std::string > filenamesByScanningDirectory(std::string_view path) noexcept
std::unique_ptr< resource_view > loadView() const
Load a resource.
Definition url_parser.hpp:82
T empty(T... args)
T operator()(T... args)
T to_string(T... args)