Skip to content

Commit

Permalink
reduce duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
lesomnus committed Jul 23, 2023
1 parent f59de36 commit 7dd705b
Showing 1 changed file with 24 additions and 71 deletions.
95 changes: 24 additions & 71 deletions internal/vfs/impl/file_proxy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,20 @@ class TypedFileProxy
}

[[nodiscard]] std::shared_ptr<File> origin() override {
if constexpr(std::is_const_v<Storage>) {
throw std::filesystem::filesystem_error("", std::make_error_code(std::errc::read_only_file_system));
} else {
if(auto* proxy = dynamic_cast<FileProxy*>(this->origin_.get()); proxy != nullptr) {
return proxy->origin();
}

return this->origin_;
auto rst = this->mutable_origin_();
if(auto* proxy = dynamic_cast<FileProxy*>(rst.get()); proxy != nullptr) {
return proxy->origin();
}

return rst;
}

[[nodiscard]] std::filesystem::perms perms() const override {
return this->origin_->perms();
}

void perms(std::filesystem::perms prms, std::filesystem::perm_options opts) override {
if constexpr(std::is_const_v<Storage>) {
throw std::filesystem::filesystem_error("", std::make_error_code(std::errc::read_only_file_system));
} else {
return this->origin_->perms(prms, opts);
}
return this->mutable_origin_()->perms(prms, opts);
}

bool operator==(File const& other) const override {
Expand All @@ -75,14 +68,18 @@ class TypedFileProxy
}

void last_write_time(std::filesystem::file_time_type new_time) override {
return this->mutable_origin_()->last_write_time(new_time);
}

protected:
std::shared_ptr<std::remove_const_t<Storage>>& mutable_origin_() {
if constexpr(std::is_const_v<Storage>) {
throw std::filesystem::filesystem_error("", std::make_error_code(std::errc::read_only_file_system));
} else {
return this->origin_->last_write_time(new_time);
return this->origin_;
}
}

protected:
std::shared_ptr<Storage> origin_;
};

Expand All @@ -97,23 +94,15 @@ class RegularFileProxy: public TypedFileProxy<RegularFile, Storage> {
}

void resize(std::uintmax_t new_size) override {
if constexpr(std::is_const_v<Storage>) {
throw std::filesystem::filesystem_error("", std::make_error_code(std::errc::read_only_file_system));
} else {
return this->origin_->resize(new_size);
}
return this->mutable_origin_()->resize(new_size);
}

[[nodiscard]] std::shared_ptr<std::istream> open_read(std::ios_base::openmode mode) const override {
return this->origin_->open_read(mode);
}

std::shared_ptr<std::ostream> open_write(std::ios_base::openmode mode) override {
if constexpr(std::is_const_v<Storage>) {
throw std::filesystem::filesystem_error("", std::make_error_code(std::errc::read_only_file_system));
} else {
return this->origin_->open_write(mode);
}
return this->mutable_origin_()->open_write(mode);
}
};

Expand All @@ -137,75 +126,39 @@ class DirectoryProxy: public TypedFileProxy<Directory, Storage> {
}

std::pair<std::shared_ptr<RegularFile>, bool> emplace_regular_file(std::string const& name) override {
if constexpr(std::is_const_v<Storage>) {
throw std::filesystem::filesystem_error("", std::make_error_code(std::errc::read_only_file_system));
} else {
return this->origin_->emplace_regular_file(name);
}
return this->mutable_origin_()->emplace_regular_file(name);
}

std::pair<std::shared_ptr<Directory>, bool> emplace_directory(std::string const& name) override {
if constexpr(std::is_const_v<Storage>) {
throw std::filesystem::filesystem_error("", std::make_error_code(std::errc::read_only_file_system));
} else {
return this->origin_->emplace_directory(name);
}
return this->mutable_origin_()->emplace_directory(name);
}

std::pair<std::shared_ptr<Symlink>, bool> emplace_symlink(std::string const& name, std::filesystem::path origin) override {
if constexpr(std::is_const_v<Storage>) {
throw std::filesystem::filesystem_error("", std::make_error_code(std::errc::read_only_file_system));
} else {
return this->origin_->emplace_symlink(name, std::move(origin));
}
std::pair<std::shared_ptr<Symlink>, bool> emplace_symlink(std::string const& name, std::filesystem::path target) override {
return this->mutable_origin_()->emplace_symlink(name, std::move(target));
}

bool link(std::string const& name, std::shared_ptr<File> file) override {
if constexpr(std::is_const_v<Storage>) {
throw std::filesystem::filesystem_error("", std::make_error_code(std::errc::read_only_file_system));
} else {
return this->origin_->link(name, std::move(file));
}
return this->mutable_origin_()->link(name, std::move(file));
}

bool unlink(std::string const& name) override {
if constexpr(std::is_const_v<Storage>) {
throw std::filesystem::filesystem_error("", std::make_error_code(std::errc::read_only_file_system));
} else {
return this->origin_->unlink(name);
}
return this->mutable_origin_()->unlink(name);
}

void mount(std::string const& name, std::shared_ptr<File> file) override {
if constexpr(std::is_const_v<Storage>) {
throw std::filesystem::filesystem_error("", std::make_error_code(std::errc::read_only_file_system));
} else {
return this->origin_->mount(name, std::move(file));
}
return this->mutable_origin_()->mount(name, std::move(file));
}

void unmount(std::string const& name) override {
if constexpr(std::is_const_v<Storage>) {
throw std::filesystem::filesystem_error("", std::make_error_code(std::errc::read_only_file_system));
} else {
return this->origin_->unmount(name);
}
return this->mutable_origin_()->unmount(name);
}

std::uintmax_t erase(std::string const& name) override {
if constexpr(std::is_const_v<Storage>) {
throw std::filesystem::filesystem_error("", std::make_error_code(std::errc::read_only_file_system));
} else {
return this->origin_->erase(name);
}
return this->mutable_origin_()->erase(name);
}

std::uintmax_t clear() override {
if constexpr(std::is_const_v<Storage>) {
throw std::filesystem::filesystem_error("", std::make_error_code(std::errc::read_only_file_system));
} else {
return this->origin_->clear();
}
return this->mutable_origin_()->clear();
}

[[nodiscard]] std::shared_ptr<Directory::Cursor> cursor() const override {
Expand Down

0 comments on commit 7dd705b

Please sign in to comment.