1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#pragma once
#include "cmConfigure.h" // IWYU pragma: keep
#include <cstddef>
#include <string>
#include <utility>
#include <cm/filesystem>
#include <cm/string_view>
#include <cm/type_traits>
#include <cmext/string_view>
namespace detail {
#if defined(__SUNPRO_CC) && defined(__sparc)
// Oracle DeveloperStudio C++ compiler on Solaris/Sparc fails to compile
// the full 'is_pathable' and 'is_move_pathable' checks. We use it only to
// improve error messages via 'enable_if' when calling methods with incorrect
// types. Just pretend all types are allowed so we can at least compile valid
// code.
template <typename T>
struct is_pathable : std::true_type
{
};
template <typename T>
struct is_move_pathable : std::true_type
{
};
#else
template <typename T, typename = void>
struct is_pathable : std::false_type
{
};
template <>
struct is_pathable<cm::filesystem::path> : std::true_type
{
};
template <>
struct is_pathable<std::string> : std::true_type
{
};
template <>
struct is_pathable<cm::string_view> : std::true_type
{
};
template <>
struct is_pathable<cm::static_string_view> : std::true_type
{
};
template <typename T>
struct is_pathable<
T,
cm::enable_if_t<std::is_same<char*, typename std::decay<T>::type>::value,
void>>
: cm::bool_constant<std::is_same<char*, typename std::decay<T>::type>::value>
{
};
template <typename T>
struct is_move_pathable : std::false_type
{
};
template <>
struct is_move_pathable<cm::filesystem::path> : std::true_type
{
};
template <>
struct is_move_pathable<std::string> : std::true_type
{
};
#endif
}
class cmCMakePath
{
private:
template <typename Source>
using enable_if_move_pathable =
cm::enable_if_t<detail::is_move_pathable<Source>::value, cmCMakePath&>;
template <typename Source>
using enable_if_pathable =
cm::enable_if_t<detail::is_pathable<Source>::value, cmCMakePath&>;
public:
using value_type = cm::filesystem::path::value_type;
using string_type = cm::filesystem::path::string_type;
enum format : unsigned char
{
auto_format =
static_cast<unsigned char>(cm::filesystem::path::format::auto_format),
native_format =
static_cast<unsigned char>(cm::filesystem::path::format::native_format),
generic_format =
static_cast<unsigned char>(cm::filesystem::path::format::generic_format)
};
class iterator;
using const_iterator = iterator;
cmCMakePath() noexcept = default;
cmCMakePath(const cmCMakePath&) = default;
cmCMakePath(cmCMakePath&& path) noexcept
: Path(std::forward<cm::filesystem::path>(path.Path))
{
}
cmCMakePath(cm::filesystem::path path) noexcept
: Path(std::move(path))
{
}
cmCMakePath(cm::string_view source, format fmt = generic_format) noexcept
: Path(FormatPath(source, fmt))
{
}
template <typename Source, typename = enable_if_move_pathable<Source>>
cmCMakePath(Source source, format fmt = generic_format)
: Path(FormatPath(std::move(source), fmt))
{
}
template <typename Source, typename = enable_if_move_pathable<Source>>
cmCMakePath& Assign(Source&& source)
{
this->Path = std::forward<Source>(source);
return *this;
}
template <typename Source, typename = enable_if_pathable<Source>>
cmCMakePath& Assign(const Source& source)
{
this->Path = source;
return *this;
}
cmCMakePath& operator=(const cmCMakePath& path)
{
if (this != &path) {
this->Path = path.Path;
}
return *this;
}
cmCMakePath& operator=(cmCMakePath&& path) noexcept
{
if (this != &path) {
this->Path = std::move(path.Path);
}
return *this;
}
template <typename Source, typename = enable_if_move_pathable<Source>>
cmCMakePath& operator=(Source&& source)
{
this->Assign(std::forward<Source>(source));
return *this;
}
template <typename Source, typename = enable_if_pathable<Source>>
cmCMakePath& operator=(const Source& source)
{
this->Assign(source);
return *this;
}
// Concatenation
cmCMakePath& Append(const cmCMakePath& path)
{
return this->Append(path.Path);
}
cmCMakePath& Append(const cm::filesystem::path& path)
{
this->Path /= path;
// filesystem::path::append use preferred_separator ('\' on Windows)
// so convert back to '/'
this->Path = this->Path.generic_string();
return *this;
}
template <typename Source, typename = enable_if_pathable<Source>>
cmCMakePath& Append(const Source& source)
{
return this->Append(cm::filesystem::path(source));
}
cmCMakePath& operator/=(const cmCMakePath& path)
{
return this->Append(path);
}
template <typename Source, typename = enable_if_pathable<Source>>
cmCMakePath& operator/=(const Source& source)
{
return this->Append(source);
}
cmCMakePath& Concat(const cmCMakePath& path)
{
this->Path += path.Path;
return *this;
}
cmCMakePath& Concat(cm::static_string_view source)
{
this->Path.concat(std::string(source));
return *this;
}
template <typename Source, typename = enable_if_pathable<Source>>
cmCMakePath& Concat(const Source& source)
{
this->Path.concat(source);
return *this;
}
cmCMakePath& operator+=(const cmCMakePath& path)
{
return this->Concat(path);
}
template <typename Source, typename = enable_if_pathable<Source>>
cmCMakePath& operator+=(const Source& source)
{
return this->Concat(source);
}
// Manipulation
void Clear() noexcept { this->Path.clear(); }
cmCMakePath& RemoveFileName()
{
this->Path.remove_filename();
return *this;
}
cmCMakePath& ReplaceFileName(const cmCMakePath& filename)
{
if (this->Path.has_filename()) {
this->Path.replace_filename(filename.Path);
}
return *this;
}
template <typename Source, typename = enable_if_pathable<Source>>
cmCMakePath& ReplaceFileName(const Source& filename)
{
if (this->Path.has_filename()) {
this->Path.replace_filename(filename);
}
return *this;
}
cmCMakePath& ReplaceExtension(const cmCMakePath& extension = cmCMakePath())
{
this->Path.replace_extension(extension.Path);
return *this;
}
template <typename Source, typename = enable_if_pathable<Source>>
cmCMakePath& ReplaceExtension(const Source& extension)
{
this->Path.replace_extension(extension);
return *this;
}
cmCMakePath& ReplaceWideExtension(
const cmCMakePath& extension = cmCMakePath())
{
return this->ReplaceWideExtension(
static_cast<cm::string_view>(extension.Path.string()));
}
template <typename Source, typename = enable_if_pathable<Source>>
cmCMakePath& ReplaceWideExtension(const Source& extension)
{
return this->ReplaceWideExtension(cm::string_view(extension));
}
cmCMakePath& ReplaceWideExtension(cm::string_view extension);
cmCMakePath& RemoveExtension()
{
if (this->Path.has_extension()) {
this->ReplaceExtension(cm::string_view(""));
}
return *this;
}
cmCMakePath& RemoveWideExtension()
{
if (this->Path.has_extension()) {
this->ReplaceWideExtension(cm::string_view(""));
}
return *this;
}
void swap(cmCMakePath& other) noexcept { this->Path.swap(other.Path); }
// Observers
std::string String() const { return this->Path.string(); }
std::wstring WString() const { return this->Path.wstring(); }
string_type Native() const
{
string_type path;
this->GetNativePath(path);
return path;
}
std::string NativeString() const
{
std::string path;
this->GetNativePath(path);
return path;
}
std::wstring NativeWString() const
{
std::wstring path;
this->GetNativePath(path);
return path;
}
std::string GenericString() const { return this->Path.generic_string(); }
std::wstring GenericWString() const { return this->Path.generic_wstring(); }
// Decomposition
cmCMakePath GetRootName() const { return this->Path.root_name(); }
cmCMakePath GetRootDirectory() const { return this->Path.root_directory(); }
cmCMakePath GetRootPath() const { return this->Path.root_path(); }
cmCMakePath GetFileName() const { return this->Path.filename(); }
cmCMakePath GetExtension() const { return this->Path.extension(); }
cmCMakePath GetWideExtension() const;
cmCMakePath GetStem() const { return this->Path.stem(); }
cmCMakePath GetNarrowStem() const;
cmCMakePath GetRelativePath() const { return this->Path.relative_path(); }
cmCMakePath GetParentPath() const { return this->Path.parent_path(); }
// Generation
cmCMakePath Normal() const
{
auto path = this->Path.lexically_normal();
// filesystem::path:lexically_normal use preferred_separator ('\') on
// Windows) so convert back to '/'
return path.generic_string();
}
cmCMakePath Relative(const cmCMakePath& base) const
{
return this->Relative(base.Path);
}
cmCMakePath Relative(const cm::filesystem::path& base) const
{
auto path = this->Path.lexically_relative(base);
// filesystem::path:lexically_relative use preferred_separator ('\') on
// Windows) so convert back to '/'
return path.generic_string();
}
template <typename Source, typename = enable_if_pathable<Source>>
cmCMakePath Relative(const Source& base) const
{
return this->Relative(cm::filesystem::path(base));
}
cmCMakePath Proximate(const cmCMakePath& base) const
{
return this->Proximate(base.Path);
}
cmCMakePath Proximate(const cm::filesystem::path& base) const
{
auto path = this->Path.lexically_proximate(base);
// filesystem::path::lexically_proximate use preferred_separator ('\') on
// Windows) so convert back to '/'
return path.generic_string();
}
template <typename Source, typename = enable_if_pathable<Source>>
cmCMakePath Proximate(const Source& base) const
{
return this->Proximate(cm::filesystem::path(base));
}
cmCMakePath Absolute(const cmCMakePath& base) const
{
return this->Absolute(base.Path);
}
template <typename Source, typename = enable_if_pathable<Source>>
cmCMakePath Absolute(const Source& base) const
{
return this->Absolute(cm::filesystem::path(base));
}
cmCMakePath Absolute(const cm::filesystem::path& base) const;
// Comparison
int Compare(const cmCMakePath& path) const noexcept
{
return this->Path.compare(path.Path);
}
// Query
bool IsEmpty() const noexcept { return this->Path.empty(); }
bool HasRootPath() const { return this->Path.has_root_path(); }
bool HasRootName() const { return this->Path.has_root_name(); }
bool HasRootDirectory() const { return this->Path.has_root_directory(); }
bool HasRelativePath() const { return this->Path.has_relative_path(); }
bool HasParentPath() const { return this->Path.has_parent_path(); }
bool HasFileName() const { return this->Path.has_filename(); }
bool HasStem() const { return this->Path.has_stem(); }
bool HasExtension() const { return this->Path.has_extension(); }
bool IsAbsolute() const { return this->Path.is_absolute(); }
bool IsRelative() const { return this->Path.is_relative(); }
bool IsPrefix(const cmCMakePath& path) const;
// Iterators
// =========
inline iterator begin() const;
inline iterator end() const;
// Non-members
// ===========
friend inline bool operator==(const cmCMakePath& lhs,
const cmCMakePath& rhs) noexcept
{
return lhs.Compare(rhs) == 0;
}
friend inline bool operator!=(const cmCMakePath& lhs,
const cmCMakePath& rhs) noexcept
{
return lhs.Compare(rhs) != 0;
}
friend inline cmCMakePath operator/(const cmCMakePath& lhs,
const cmCMakePath& rhs)
{
cmCMakePath result(lhs);
result /= rhs;
return result;
}
private:
friend std::size_t hash_value(const cmCMakePath& path) noexcept;
static std::string FormatPath(std::string path, format fmt = generic_format);
static std::string FormatPath(cm::string_view path,
format fmt = generic_format)
{
return FormatPath(std::string(path), fmt);
}
void GetNativePath(std::string& path) const;
void GetNativePath(std::wstring& path) const;
cm::filesystem::path Path;
};
class cmCMakePath::iterator
{
public:
using iterator_category = cm::filesystem::path::iterator::iterator_category;
using value_type = cmCMakePath;
using difference_type = cm::filesystem::path::iterator::difference_type;
using pointer = const cmCMakePath*;
using reference = const cmCMakePath&;
iterator() = default;
iterator(const iterator& other)
: Iterator(other.Iterator)
, Path(other.Path)
, PathElement(*this->Iterator)
{
}
~iterator() = default;
iterator& operator=(const iterator& other)
{
if (this != &other) {
this->Iterator = other.Iterator;
this->Path = other.Path;
this->PathElement = *this->Iterator;
}
return *this;
}
reference operator*() const { return this->PathElement; }
pointer operator->() const { return &this->PathElement; }
iterator& operator++()
{
++this->Iterator;
this->PathElement = *this->Iterator;
return *this;
}
iterator operator++(int)
{
iterator it(*this);
this->operator++();
return it;
}
iterator& operator--()
{
--this->Iterator;
this->PathElement = *this->Iterator;
return *this;
}
iterator operator--(int)
{
iterator it(*this);
this->operator--();
return it;
}
private:
friend class cmCMakePath;
friend bool operator==(const iterator&, const iterator&);
iterator(const cmCMakePath* path, const cm::filesystem::path::iterator& it)
: Iterator(it)
, Path(path)
, PathElement(*this->Iterator)
{
}
cm::filesystem::path::iterator Iterator;
const cmCMakePath* Path = nullptr;
cmCMakePath PathElement;
};
inline cmCMakePath::iterator cmCMakePath::begin() const
{
return iterator(this, this->Path.begin());
}
inline cmCMakePath::iterator cmCMakePath::end() const
{
return iterator(this, this->Path.end());
}
// Non-member functions
// ====================
inline bool operator==(const cmCMakePath::iterator& lhs,
const cmCMakePath::iterator& rhs)
{
return lhs.Path == rhs.Path && lhs.Path != nullptr &&
lhs.Iterator == rhs.Iterator;
}
inline bool operator!=(const cmCMakePath::iterator& lhs,
const cmCMakePath::iterator& rhs)
{
return !(lhs == rhs);
}
inline void swap(cmCMakePath& lhs, cmCMakePath& rhs) noexcept
{
lhs.swap(rhs);
}
inline std::size_t hash_value(const cmCMakePath& path) noexcept
{
return cm::filesystem::hash_value(path.Path);
}
|