HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
URL.hpp
1// Copyright 2019 Pokitec
2// All rights reserved.
3
4#pragma once
5
6#include "TTauri/Foundation/required.hpp"
7#include <string>
8#include <string_view>
9#include <optional>
10#include <vector>
11#include <unordered_map>
12#include <memory>
13#include <ostream>
14#include <mutex>
15
16namespace tt {
17struct url_parts;
18class ResourceView;
19
45class URL {
46private:
47 std::string value;
48
49public:
50 URL() = default;
51 explicit URL(std::string_view url);
52 explicit URL(char const *url);
53 explicit URL(std::string const &url);
54 explicit URL(url_parts const &parts);
55
56 URL(URL const &other) noexcept : value(other.value) {}
57 URL(URL &&other) noexcept = default;
58 URL &operator=(URL const &other) noexcept { value = other.value; return *this; }
59 URL &operator=(URL &&other) noexcept = default;
60
61 [[nodiscard]] size_t hash() const noexcept;
62 [[nodiscard]] std::string string() const noexcept;
63
64 [[nodiscard]] std::string_view scheme() const noexcept;
65
66 [[nodiscard]] std::string query() const noexcept;
67
68 [[nodiscard]] std::string fragment() const noexcept;
69
70 [[nodiscard]] std::string filename() const noexcept;
71
72 [[nodiscard]] std::string directory() const noexcept;
73
74 [[nodiscard]] std::string nativeDirectory() const noexcept;
75
76 [[nodiscard]] std::string extension() const noexcept;
77
78 [[nodiscard]] std::vector<std::string> pathSegments() const noexcept;
79
80 [[nodiscard]] std::string path() const noexcept;
81
82 [[nodiscard]] std::string nativePath() const noexcept;
83
84 [[nodiscard]] std::wstring nativeWPath() const noexcept;
85
86 [[nodiscard]] bool isFileScheme() const noexcept {
87 return scheme() == "file";
88 }
89
90 [[nodiscard]] bool isAbsolute() const noexcept;
91 [[nodiscard]] bool isRelative() const noexcept;
92 [[nodiscard]] bool isRootDirectory() const noexcept;
93
94 [[nodiscard]] URL urlByAppendingPath(URL const &other) const noexcept;
95
96 [[nodiscard]] URL urlByAppendingPath(std::string_view const other) const noexcept;
97 [[nodiscard]] URL urlByAppendingPath(std::string const &other) const noexcept;
98 [[nodiscard]] URL urlByAppendingPath(char const *other) const noexcept;
99
100 [[nodiscard]] URL urlByAppendingPath(std::wstring_view const other) const noexcept;
101 [[nodiscard]] URL urlByAppendingPath(std::wstring const &other) const noexcept;
102 [[nodiscard]] URL urlByAppendingPath(wchar_t const *other) const noexcept;
103
104 [[nodiscard]] URL urlByRemovingFilename() const noexcept;
105
110
121 [[nodiscard]] std::vector<URL> urlsByScanningWithGlobPattern() const noexcept;
122
123 [[nodiscard]] static URL urlFromPath(std::string_view const path) noexcept;
124 [[nodiscard]] static URL urlFromWPath(std::wstring_view const path) noexcept;
125
126 [[nodiscard]] static URL urlFromCurrentWorkingDirectory() noexcept;
127 [[nodiscard]] static URL urlFromResourceDirectory() noexcept;
128 [[nodiscard]] static URL urlFromExecutableDirectory() noexcept;
129 [[nodiscard]] static URL urlFromExecutableFile() noexcept;
130 [[nodiscard]] static URL urlFromApplicationDataDirectory() noexcept;
131 [[nodiscard]] static URL urlFromApplicationLogDirectory() noexcept;
132 [[nodiscard]] static URL urlFromSystemFontDirectory() noexcept;
133
138 [[nodiscard]] static std::vector<std::string> filenamesByScanningDirectory(std::string_view path) noexcept;
139
140 [[nodiscard]] static std::string nativePathFromPath(std::string_view path) noexcept;
141 [[nodiscard]] static std::wstring nativeWPathFromPath(std::string_view path) noexcept;
142
143 friend bool operator==(URL const &lhs, URL const &rhs) noexcept;
144 friend bool operator<(URL const &lhs, URL const &rhs) noexcept;
145};
146
147inline bool operator==(URL const &lhs, URL const &rhs) noexcept { return lhs.value == rhs.value; }
148inline bool operator<(URL const &lhs, URL const &rhs) noexcept { return lhs.value < rhs.value; }
149inline bool operator>(URL const &lhs, URL const &rhs) noexcept { return rhs < lhs; }
150inline bool operator!=(URL const &lhs, URL const &rhs) noexcept { return !(lhs == rhs); }
151inline bool operator>=(URL const &lhs, URL const &rhs) noexcept { return !(lhs < rhs); }
152inline bool operator<=(URL const &lhs, URL const &rhs) noexcept { return !(lhs > rhs); }
153inline URL operator/(URL const &lhs, URL const &rhs) noexcept { return lhs.urlByAppendingPath(rhs); }
154inline URL operator/(URL const &lhs, std::string_view const &rhs) noexcept { return lhs.urlByAppendingPath(URL{rhs}); }
155inline std::string to_string(URL const &url) noexcept { return url.string(); }
156
157std::ostream& operator<<(std::ostream& lhs, const URL& rhs);
158
159template <typename T>
160inline std::unique_ptr<T> parseResource(URL const &location)
161{
162 tt_not_implemented;
163}
164
165template <typename T>
166gsl_suppress2(26489,lifetime.1)
167inline T &getResource(URL const &location)
168{
169 static std::unordered_map<URL,std::unique_ptr<T>> resourceCache = {};
170 static std::mutex mutex;
171
172 auto lock = std::scoped_lock(mutex);
173
174 ttlet oldResource = resourceCache.find(location);
175 if (oldResource != resourceCache.end()) {
176 return *(oldResource->second);
177 }
178
179 [[maybe_unused]] ttlet [newResource, dummy] = resourceCache.try_emplace(location, std::move(parseResource<T>(location)));
180
181 return *(newResource->second);
182}
183
184}
185
186namespace std {
187
188template<>
189class hash<tt::URL> {
190public:
191 size_t operator()(tt::URL const& url) const noexcept {
192 return url.hash();
193 }
194};
195
196}
STL namespace.
Definition URL.hpp:45
std::unique_ptr< ResourceView > loadView() const
Load a resource.
std::vector< URL > urlsByScanningWithGlobPattern() const noexcept
static std::vector< std::string > filenamesByScanningDirectory(std::string_view path) noexcept
Definition url_parser.hpp:81
T end(T... args)
T find(T... args)
T lock(T... args)
T move(T... args)
T operator!=(T... args)
T to_string(T... args)