Skip to content

Commit 0616d92

Browse files
authored
strongly typed paths (#439)
1 parent 7ddd7ee commit 0616d92

46 files changed

Lines changed: 494 additions & 444 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/odr/filesystem.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,25 +74,26 @@ Filesystem::Filesystem(
7474
Filesystem::operator bool() const { return m_impl != nullptr; }
7575

7676
bool Filesystem::exists(const std::string &path) const {
77-
return m_impl != nullptr && m_impl->exists(internal::Path(path));
77+
return m_impl != nullptr && m_impl->exists(internal::AbsPath(path));
7878
}
7979

8080
bool Filesystem::is_file(const std::string &path) const {
81-
return m_impl != nullptr && m_impl->is_file(internal::Path(path));
81+
return m_impl != nullptr && m_impl->is_file(internal::AbsPath(path));
8282
}
8383

8484
bool Filesystem::is_directory(const std::string &path) const {
85-
return m_impl != nullptr && m_impl->is_directory(internal::Path(path));
85+
return m_impl != nullptr && m_impl->is_directory(internal::AbsPath(path));
8686
}
8787

8888
FileWalker Filesystem::file_walker(const std::string &path) const {
8989
return m_impl != nullptr
90-
? FileWalker(m_impl->file_walker(internal::Path(path)))
90+
? FileWalker(m_impl->file_walker(internal::AbsPath(path)))
9191
: FileWalker();
9292
}
9393

9494
File Filesystem::open(const std::string &path) const {
95-
return m_impl != nullptr ? File(m_impl->open(internal::Path(path))) : File();
95+
return m_impl != nullptr ? File(m_impl->open(internal::AbsPath(path)))
96+
: File();
9697
}
9798

9899
} // namespace odr

src/odr/html.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ void bring_offline(const HtmlResources &resources,
4141
resource.is_external() || !resource.is_accessible()) {
4242
continue;
4343
}
44-
auto path =
45-
odr::internal::Path(output_path).join(odr::internal::Path(*location));
44+
auto path = odr::internal::Path(output_path)
45+
.join(odr::internal::RelPath(*location));
4646

47-
std::filesystem::create_directories(path.parent());
47+
std::filesystem::create_directories(path.parent().path());
4848
std::ofstream ostream = internal::util::file::create(path.string());
4949
resource.write_resource(ostream);
5050
}
@@ -116,10 +116,10 @@ Html HtmlService::bring_offline(const std::string &output_path,
116116
HtmlResources resources;
117117

118118
for (const auto &view : views) {
119-
auto path =
120-
odr::internal::Path(output_path).join(odr::internal::Path(view.path()));
119+
auto path = odr::internal::Path(output_path)
120+
.join(odr::internal::RelPath(view.path()));
121121

122-
std::filesystem::create_directories(path.parent());
122+
std::filesystem::create_directories(path.parent().path());
123123
std::ofstream ostream = internal::util::file::create(path.string());
124124
HtmlResources view_resources = view.write_html(ostream);
125125

@@ -166,11 +166,11 @@ HtmlResources HtmlView::write_html(std::ostream &out) const {
166166
Html HtmlView::bring_offline(const std::string &output_path) const {
167167
HtmlResources resources;
168168

169-
auto path =
170-
odr::internal::Path(output_path).join(odr::internal::Path(this->path()));
169+
auto path = odr::internal::Path(output_path)
170+
.join(odr::internal::RelPath(this->path()));
171171

172172
{
173-
std::filesystem::create_directories(path.parent());
173+
std::filesystem::create_directories(path.parent().path());
174174
std::ofstream ostream = internal::util::file::create(path.string());
175175
resources = write_html(ostream);
176176
}
@@ -250,7 +250,7 @@ HtmlResourceLocator html::standard_resource_locator() {
250250

251251
if (resource.is_shipped()) {
252252
auto resource_path =
253-
Path(config.resource_path).join(Path(resource.path()));
253+
Path(config.resource_path).join(RelPath(resource.path()));
254254
if (config.relative_resource_paths && config.output_path.has_value()) {
255255
resource_path = resource_path.rebase(Path(*config.output_path));
256256
}

src/odr/internal/abstract/file.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <string>
88

99
namespace odr::internal {
10-
class Path;
10+
class AbsPath;
1111
}
1212

1313
namespace odr::internal::abstract {
@@ -22,7 +22,7 @@ class File {
2222
[[nodiscard]] virtual FileLocation location() const noexcept = 0;
2323
[[nodiscard]] virtual std::size_t size() const = 0;
2424

25-
[[nodiscard]] virtual std::optional<Path> disk_path() const = 0;
25+
[[nodiscard]] virtual std::optional<AbsPath> disk_path() const = 0;
2626
[[nodiscard]] virtual const char *memory_data() const = 0;
2727

2828
[[nodiscard]] virtual std::unique_ptr<std::istream> stream() const = 0;

src/odr/internal/abstract/filesystem.hpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <memory>
55

66
namespace odr::internal {
7-
class Path;
7+
class AbsPath;
88
} // namespace odr::internal
99

1010
namespace odr::internal::abstract {
@@ -20,7 +20,7 @@ class FileWalker {
2020
[[nodiscard]] virtual bool end() const = 0;
2121
[[nodiscard]] virtual std::uint32_t depth() const = 0;
2222
// TODO by reference?
23-
[[nodiscard]] virtual Path path() const = 0;
23+
[[nodiscard]] virtual AbsPath path() const = 0;
2424
[[nodiscard]] virtual bool is_file() const = 0;
2525
[[nodiscard]] virtual bool is_directory() const = 0;
2626

@@ -33,31 +33,31 @@ class ReadableFilesystem {
3333
public:
3434
virtual ~ReadableFilesystem() = default;
3535

36-
[[nodiscard]] virtual bool exists(const Path &path) const = 0;
37-
[[nodiscard]] virtual bool is_file(const Path &path) const = 0;
38-
[[nodiscard]] virtual bool is_directory(const Path &path) const = 0;
36+
[[nodiscard]] virtual bool exists(const AbsPath &path) const = 0;
37+
[[nodiscard]] virtual bool is_file(const AbsPath &path) const = 0;
38+
[[nodiscard]] virtual bool is_directory(const AbsPath &path) const = 0;
3939

4040
[[nodiscard]] virtual std::unique_ptr<FileWalker>
41-
file_walker(const Path &path) const = 0;
41+
file_walker(const AbsPath &path) const = 0;
4242

4343
[[nodiscard]] virtual std::shared_ptr<abstract::File>
44-
open(const Path &path) const = 0;
44+
open(const AbsPath &path) const = 0;
4545
};
4646

4747
class WriteableFilesystem {
4848
public:
4949
virtual ~WriteableFilesystem() = default;
5050

51-
virtual std::unique_ptr<std::ostream> create_file(const Path &path) = 0;
52-
virtual bool create_directory(const Path &path) = 0;
51+
virtual std::unique_ptr<std::ostream> create_file(const AbsPath &path) = 0;
52+
virtual bool create_directory(const AbsPath &path) = 0;
5353

54-
virtual bool remove(const Path &path) = 0;
55-
virtual bool copy(const Path &from, const Path &to) = 0;
54+
virtual bool remove(const AbsPath &path) = 0;
55+
virtual bool copy(const AbsPath &from, const AbsPath &to) = 0;
5656
virtual std::shared_ptr<abstract::File> copy(const abstract::File &from,
57-
const Path &to) = 0;
57+
const AbsPath &to) = 0;
5858
virtual std::shared_ptr<abstract::File>
59-
copy(std::shared_ptr<abstract::File> from, const Path &to) = 0;
60-
virtual bool move(const Path &from, const Path &to) = 0;
59+
copy(std::shared_ptr<abstract::File> from, const AbsPath &to) = 0;
60+
virtual bool move(const AbsPath &from, const AbsPath &to) = 0;
6161
};
6262

6363
class Filesystem : public ReadableFilesystem, public WriteableFilesystem {};

src/odr/internal/cfb/cfb_util.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class FileInCfb final : public abstract::File {
7373
}
7474
[[nodiscard]] std::size_t size() const final { return m_entry.size; }
7575

76-
[[nodiscard]] std::optional<Path> disk_path() const final {
76+
[[nodiscard]] std::optional<AbsPath> disk_path() const final {
7777
return std::nullopt;
7878
}
7979
[[nodiscard]] const char *memory_data() const final { return nullptr; }
@@ -94,7 +94,7 @@ bool Archive::Entry::is_file() const { return m_entry->is_stream(); }
9494

9595
bool Archive::Entry::is_directory() const { return !m_entry->is_stream(); }
9696

97-
Path Archive::Entry::path() const { return m_path; }
97+
AbsPath Archive::Entry::path() const { return m_path; }
9898

9999
std::unique_ptr<abstract::File> Archive::Entry::file() const {
100100
if (!is_file()) {
@@ -204,7 +204,7 @@ Archive::Iterator Archive::begin() const {
204204

205205
Archive::Iterator Archive::end() const { return {}; }
206206

207-
Archive::Iterator Archive::find(const Path &path) const {
207+
Archive::Iterator Archive::find(const AbsPath &path) const {
208208
return std::find_if(begin(), end(), [&path](const Entry &entry) {
209209
return entry.path() == path;
210210
});

src/odr/internal/cfb/cfb_util.hpp

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Archive final : public std::enable_shared_from_this<Archive> {
3434
[[nodiscard]] Iterator begin() const;
3535
[[nodiscard]] Iterator end() const;
3636

37-
[[nodiscard]] Iterator find(const Path &path) const;
37+
[[nodiscard]] Iterator find(const AbsPath &path) const;
3838

3939
class Entry {
4040
public:
@@ -43,9 +43,9 @@ class Archive final : public std::enable_shared_from_this<Archive> {
4343
Entry(const Archive &parent, const impl::CompoundFileEntry &entry)
4444
: m_parent{&parent}, m_entry{&entry}, m_path{"/"} {}
4545
Entry(const Archive &parent, const impl::CompoundFileEntry &entry,
46-
const Path &parent_path)
46+
const AbsPath &parent_path)
4747
: m_parent{&parent}, m_entry{&entry},
48-
m_path{parent_path.join(Path(name()))} {}
48+
m_path{parent_path.join(RelPath(name()))} {}
4949
~Entry() = default;
5050
Entry &operator=(const Entry &) = default;
5151
Entry &operator=(Entry &&) noexcept = default;
@@ -59,7 +59,7 @@ class Archive final : public std::enable_shared_from_this<Archive> {
5959

6060
[[nodiscard]] bool is_file() const;
6161
[[nodiscard]] bool is_directory() const;
62-
[[nodiscard]] Path path() const;
62+
[[nodiscard]] AbsPath path() const;
6363
[[nodiscard]] std::unique_ptr<abstract::File> file() const;
6464

6565
[[nodiscard]] std::string name() const;
@@ -70,7 +70,7 @@ class Archive final : public std::enable_shared_from_this<Archive> {
7070
private:
7171
const Archive *m_parent;
7272
const impl::CompoundFileEntry *m_entry;
73-
Path m_path;
73+
AbsPath m_path;
7474

7575
friend Iterator;
7676
};
@@ -84,30 +84,22 @@ class Archive final : public std::enable_shared_from_this<Archive> {
8484
using reference = const Entry &;
8585

8686
Iterator() = default;
87-
Iterator(const Iterator &) = default;
88-
Iterator(Iterator &&) noexcept = default;
8987
Iterator(const Archive &parent, const impl::CompoundFileEntry &entry)
9088
: m_entry{Entry(parent, entry)} {
9189
dig_left_();
9290
}
9391
Iterator(const Archive &parent, const impl::CompoundFileEntry &entry,
94-
const Path &parent_path)
92+
const AbsPath &parent_path)
9593
: m_entry{Entry(parent, entry, parent_path)} {
9694
dig_left_();
9795
}
98-
~Iterator() = default;
99-
Iterator &operator=(const Iterator &) = default;
100-
Iterator &operator=(Iterator &&) noexcept = default;
10196

10297
[[nodiscard]] reference operator*() const { return *m_entry; }
10398
[[nodiscard]] pointer operator->() const { return &*m_entry; }
10499

105100
[[nodiscard]] bool operator==(const Iterator &other) const {
106101
return m_entry == other.m_entry;
107102
}
108-
[[nodiscard]] bool operator!=(const Iterator &other) const {
109-
return m_entry != other.m_entry;
110-
}
111103

112104
Iterator &operator++() {
113105
next_();

src/odr/internal/common/file.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ DiskFile::DiskFile(const char *path) : DiskFile{Path(path)} {}
1414

1515
DiskFile::DiskFile(const std::string &path) : DiskFile{Path(path)} {}
1616

17-
DiskFile::DiskFile(Path path) : m_path{std::move(path)} {
18-
if (!std::filesystem::is_regular_file(m_path)) {
17+
DiskFile::DiskFile(Path path) {
18+
if (!std::filesystem::is_regular_file(path.path())) {
1919
throw FileNotFound();
2020
}
21+
m_path = AbsPath(std::filesystem::absolute(path.path()));
2122
}
2223

2324
FileLocation DiskFile::location() const noexcept { return FileLocation::disk; }
@@ -26,7 +27,7 @@ std::size_t DiskFile::size() const {
2627
return std::filesystem::file_size(m_path.string());
2728
}
2829

29-
std::optional<Path> DiskFile::disk_path() const { return m_path; }
30+
std::optional<AbsPath> DiskFile::disk_path() const { return m_path; }
3031

3132
const char *DiskFile::memory_data() const { return nullptr; }
3233

@@ -51,7 +52,7 @@ FileLocation MemoryFile::location() const noexcept {
5152

5253
std::size_t MemoryFile::size() const { return m_data.size(); }
5354

54-
std::optional<Path> MemoryFile::disk_path() const { return {}; }
55+
std::optional<AbsPath> MemoryFile::disk_path() const { return std::nullopt; }
5556

5657
const char *MemoryFile::memory_data() const { return m_data.data(); }
5758

src/odr/internal/common/file.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ class DiskFile : public abstract::File {
2222
[[nodiscard]] FileLocation location() const noexcept final;
2323
[[nodiscard]] std::size_t size() const final;
2424

25-
[[nodiscard]] std::optional<Path> disk_path() const final;
25+
[[nodiscard]] std::optional<AbsPath> disk_path() const final;
2626
[[nodiscard]] const char *memory_data() const final;
2727

2828
[[nodiscard]] std::unique_ptr<std::istream> stream() const final;
2929

3030
private:
31-
Path m_path;
31+
AbsPath m_path;
3232
};
3333

3434
class MemoryFile final : public abstract::File {
@@ -39,7 +39,7 @@ class MemoryFile final : public abstract::File {
3939
[[nodiscard]] FileLocation location() const noexcept final;
4040
[[nodiscard]] std::size_t size() const final;
4141

42-
[[nodiscard]] std::optional<Path> disk_path() const final;
42+
[[nodiscard]] std::optional<AbsPath> disk_path() const final;
4343
[[nodiscard]] const char *memory_data() const final;
4444

4545
[[nodiscard]] std::unique_ptr<std::istream> stream() const final;

0 commit comments

Comments
 (0)