mini-llvm 0.1.0
Loading...
Searching...
No Matches
Path.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2
3#pragma once
4
5#include <compare>
6#include <cstddef>
7#include <filesystem>
8#include <format>
9#include <functional>
10#include <string>
11#include <string_view>
12#include <utility>
13
15
16namespace mini_llvm {
17
18class Path : private std::filesystem::path {
19 using Base = std::filesystem::path;
20
21public:
22 static constexpr char preferred_separator = static_cast<char>(Base::preferred_separator);
23
24 Path() = default;
25
26 Path(const Base &base)
27 : Base(base) {}
28
29 Path(Base &&base) noexcept
30 : Base(std::move(base)) {}
31
32 Path(const SystemString &str)
33 : Base(str.native()) {}
34
36 : Base(std::move(str).native()) {}
37
38 Path(const char *str)
39 : Path(SystemString(str)) {}
40
41 Path(const std::string &str)
42 : Path(SystemString(str)) {}
43
44 Path(std::string &&str)
45 : Path(SystemString(std::move(str))) {}
46
47 Path(std::string_view str)
48 : Path(SystemString(str)) {}
49
50 Path(const Path &other) = default;
51 Path(Path &&other) noexcept = default;
52
53 Path &operator=(const Base &base) {
54 Base::operator=(base);
55 return *this;
56 }
57
58 Path &operator=(Base &&base) noexcept {
59 Base::operator=(std::move(base));
60 return *this;
61 }
62
64 Base::operator=(str.native());
65 return *this;
66 }
67
69 Base::operator=(std::move(str).native());
70 return *this;
71 }
72
73 Path &operator=(const char *str) {
74 return operator=(SystemString(str));
75 }
76
77 Path &operator=(const std::string &str) {
78 return operator=(SystemString(str));
79 }
80
81 Path &operator=(std::string_view str) {
82 return operator=(SystemString(str));
83 }
84
85 Path &operator=(const Path &other) = default;
86 Path &operator=(Path &&other) noexcept = default;
87
88 Base &base() noexcept {
89 return static_cast<Base &>(*this);
90 }
91
92 const Base &base() const noexcept {
93 return static_cast<const Base &>(*this);
94 }
95
97 return SystemString(Base::native());
98 }
99
100 std::string to_string() const {
101 return to_system_string().to_string();
102 }
103
104 bool empty() const {
105 return Base::empty();
106 }
107
108 bool has_root_path() const {
109 return Base::has_root_path();
110 }
111
112 bool has_root_name() const {
113 return Base::has_root_name();
114 }
115
116 bool has_root_directory() const {
117 return Base::has_root_directory();
118 }
119
120 bool has_relative_path() const {
121 return Base::has_relative_path();
122 }
123
124 bool has_parent_path() const {
125 return Base::has_parent_path();
126 }
127
128 bool has_filename() const {
129 return Base::has_filename();
130 }
131
132 bool has_stem() const {
133 return Base::has_stem();
134 }
135
136 bool has_extension() const {
137 return Base::has_extension();
138 }
139
140 bool is_absolute() const {
141 return Base::is_absolute();
142 }
143
144 bool is_relative() const {
145 return Base::is_relative();
146 }
147
148 Path root_path() const {
149 return Base::root_path();
150 }
151
152 Path root_name() const {
153 return Base::root_name();
154 }
155
157 return Base::root_directory();
158 }
159
161 return Base::relative_path();
162 }
163
165 return Base::parent_path();
166 }
167
168 Path filename() const {
169 return Base::filename();
170 }
171
172 Path stem() const {
173 return Base::stem();
174 }
175
176 Path extension() const {
177 return Base::extension();
178 }
179
181 return Base::lexically_normal();
182 }
183
185 return Base::lexically_relative(base);
186 }
187
189 return Base::lexically_proximate(base);
190 }
191
193 Base::make_preferred();
194 return *this;
195 }
196
198 Base::remove_filename();
199 return *this;
200 }
201
202 Path &replace_filename(const Path &replacement) {
203 Base::replace_filename(replacement);
204 return *this;
205 }
206
207 Path &replace_extension(const Path &replacement) {
208 Base::replace_extension(replacement);
209 return *this;
210 }
211
212 Path &operator/=(const Path &other) {
213 Base::operator/=(other);
214 return *this;
215 }
216
217 Path &operator/=(const Base &other) {
218 Base::operator/=(other);
219 return *this;
220 }
221
222 Path &operator/=(const SystemString &other) {
223 Base::operator/=(other.native());
224 return *this;
225 }
226
227 Path &operator/=(const char *str) {
228 return operator/=(SystemString(str));
229 }
230
231 Path &operator/=(const std::string &str) {
232 return operator/=(SystemString(str));
233 }
234
235 Path &operator/=(std::string_view str) {
236 return operator/=(SystemString(str));
237 }
238
239 Path &operator+=(const Path &other) {
240 Base::operator+=(other);
241 return *this;
242 }
243
244 Path &operator+=(const Base &base) {
245 Base::operator+=(base);
246 return *this;
247 }
248
250 Base::operator+=(str.native());
251 return *this;
252 }
253
254 Path &operator+=(const char *str) {
255 return operator+=(SystemString(str));
256 }
257
258 Path &operator+=(const std::string &str) {
259 return operator+=(SystemString(str));
260 }
261
262 Path &operator+=(std::string_view str) {
263 return operator+=(SystemString(str));
264 }
265
266 void clear() {
267 Base::clear();
268 }
269
270 void swap(Path &other) noexcept {
271 Base::swap(other);
272 }
273};
274
275inline void swap(Path &lhs, Path &rhs) noexcept {
276 lhs.swap(rhs);
277}
278
279inline bool operator==(const Path &lhs, const Path &rhs) noexcept {
280 return lhs.base() == rhs.base();
281}
282
283inline std::strong_ordering operator<=>(const Path &lhs, const Path &rhs) noexcept {
284 return lhs.base() <=> rhs.base();
285}
286
287inline Path operator/(Path lhs, const Path &rhs) {
288 return lhs /= rhs;
289}
290
291} // namespace mini_llvm
292
293template <>
294struct std::hash<mini_llvm::Path> {
295 size_t operator()(const mini_llvm::Path &path) const noexcept {
296 return std::hash<std::filesystem::path>()(path.base());
297 }
298};
299
300template <>
301struct std::formatter<mini_llvm::Path> {
302 constexpr auto parse(std::format_parse_context &ctx) {
303 return ctx.begin();
304 }
305
306 template <typename FormatContext>
307 auto format(const mini_llvm::Path &path, FormatContext &ctx) const {
308 return std::format_to(ctx.out(), "{}", path.to_string());
309 }
310};
Definition Path.h:18
bool has_relative_path() const
Definition Path.h:120
const Base & base() const noexcept
Definition Path.h:92
Path & operator/=(std::string_view str)
Definition Path.h:235
Path & operator=(const Base &base)
Definition Path.h:53
Path & operator+=(const Path &other)
Definition Path.h:239
Path(std::string &&str)
Definition Path.h:44
Path & make_preferred()
Definition Path.h:192
bool is_absolute() const
Definition Path.h:140
Path(const SystemString &str)
Definition Path.h:32
Path & operator=(SystemString &&str)
Definition Path.h:68
Path & operator/=(const Base &other)
Definition Path.h:217
Path(std::string_view str)
Definition Path.h:47
Path & replace_extension(const Path &replacement)
Definition Path.h:207
Path root_directory() const
Definition Path.h:156
bool has_stem() const
Definition Path.h:132
Path root_path() const
Definition Path.h:148
Path & operator=(const char *str)
Definition Path.h:73
Path relative_path() const
Definition Path.h:160
Path & remove_filename()
Definition Path.h:197
Path(SystemString &&str)
Definition Path.h:35
Path & operator/=(const char *str)
Definition Path.h:227
bool is_relative() const
Definition Path.h:144
bool empty() const
Definition Path.h:104
bool has_filename() const
Definition Path.h:128
Path(Path &&other) noexcept=default
Path & operator/=(const Path &other)
Definition Path.h:212
void clear()
Definition Path.h:266
void swap(Path &other) noexcept
Definition Path.h:270
bool has_root_path() const
Definition Path.h:108
Path & operator+=(const char *str)
Definition Path.h:254
Path(Base &&base) noexcept
Definition Path.h:29
bool has_root_name() const
Definition Path.h:112
Path & operator=(Base &&base) noexcept
Definition Path.h:58
Path(const char *str)
Definition Path.h:38
Base & base() noexcept
Definition Path.h:88
bool has_parent_path() const
Definition Path.h:124
Path & operator=(const std::string &str)
Definition Path.h:77
Path & operator=(const SystemString &str)
Definition Path.h:63
Path lexically_relative(const Path &base) const
Definition Path.h:184
Path(const Path &other)=default
Path stem() const
Definition Path.h:172
Path & operator/=(const SystemString &other)
Definition Path.h:222
SystemString to_system_string() const
Definition Path.h:96
Path(const std::string &str)
Definition Path.h:41
Path & operator+=(const Base &base)
Definition Path.h:244
Path & operator+=(const SystemString &str)
Definition Path.h:249
Path parent_path() const
Definition Path.h:164
Path & operator+=(const std::string &str)
Definition Path.h:258
Path()=default
bool has_root_directory() const
Definition Path.h:116
Path root_name() const
Definition Path.h:152
Path lexically_proximate(const Path &base) const
Definition Path.h:188
Path & operator/=(const std::string &str)
Definition Path.h:231
Path(const Base &base)
Definition Path.h:26
std::string to_string() const
Definition Path.h:100
static constexpr char preferred_separator
Definition Path.h:22
Path lexically_normal() const
Definition Path.h:180
Path & replace_filename(const Path &replacement)
Definition Path.h:202
bool has_extension() const
Definition Path.h:136
Path & operator=(std::string_view str)
Definition Path.h:81
Path & operator=(Path &&other) noexcept=default
Path & operator+=(std::string_view str)
Definition Path.h:262
Path extension() const
Definition Path.h:176
Path & operator=(const Path &other)=default
Path filename() const
Definition Path.h:168
Definition SystemString.h:19
string_type & native() &noexcept
Definition SystemString.h:81
std::string to_string() const &
Definition SystemString.h:97
Definition GraphColoringAllocator.h:13
void swap(FileHandle &lhs, FileHandle &rhs) noexcept
Definition FileHandle.h:61
MINI_LLVM_EXPORT BigInteger operator/(const BigInteger &lhs, int32_t rhs)
MINI_LLVM_EXPORT bool operator==(const BigInteger &lhs, const BigInteger &rhs) noexcept
MINI_LLVM_EXPORT std::strong_ordering operator<=>(const BigInteger &lhs, const BigInteger &rhs) noexcept
auto format(const mini_llvm::Path &path, FormatContext &ctx) const
Definition Path.h:307
constexpr auto parse(std::format_parse_context &ctx)
Definition Path.h:302
size_t operator()(const mini_llvm::Path &path) const noexcept
Definition Path.h:295