Skip to content

Commit

Permalink
respect const
Browse files Browse the repository at this point in the history
  • Loading branch information
lesomnus committed Jul 25, 2023
1 parent 70351b5 commit bb72baf
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 86 deletions.
8 changes: 4 additions & 4 deletions include/vfs/directory_entry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ class directory_entry {
directory_entry(directory_entry&&) noexcept = default;

directory_entry(Fs const& fs) noexcept
: fs_(fs.current_path(fs.current_path())) { }
: fs_(fs.shared_from_this()) { }

explicit directory_entry(Fs const& fs, std::filesystem::path p)
: fs_(fs.current_path(fs.current_path()))
: fs_(fs.shared_from_this())
, path_(std::move(p)) {
this->refresh();
}

directory_entry(Fs const& fs, std::filesystem::path p, std::error_code& ec)
: fs_(fs.current_path(fs.current_path()))
: fs_(fs.shared_from_this())
, path_(std::move(p)) {
this->refresh(ec);
}
Expand Down Expand Up @@ -415,7 +415,7 @@ class directory_entry {
return this->status().type();
}

std::shared_ptr<Fs> fs_;
std::shared_ptr<Fs const> fs_;
std::filesystem::path path_;
std::filesystem::file_type type_ = std::filesystem::file_type::none;
};
Expand Down
25 changes: 22 additions & 3 deletions include/vfs/fs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class directory_entry;
class directory_iterator;
class recursive_directory_iterator;

class Fs {
class Fs: public std::enable_shared_from_this<Fs> {
public:
virtual ~Fs() = default;

Expand Down Expand Up @@ -481,7 +481,7 @@ class Fs {
* @param[in] p Path to change the current working directory to.
* @return Same file system where the working directory is \p p.
*/
[[nodiscard]] virtual std::shared_ptr<Fs> current_path(std::filesystem::path const& p) const = 0;
[[nodiscard]] virtual std::shared_ptr<Fs const> current_path(std::filesystem::path const& p) const = 0;

/**
* @brief Creates a new Fs that share the same file system but have different working directory.
Expand All @@ -490,7 +490,24 @@ class Fs {
* @param[out] ec Error code to store error status to.
* @return Same file system where the working directory is \p p.
*/
[[nodiscard]] virtual std::shared_ptr<Fs> current_path(std::filesystem::path const& p, std::error_code& ec) const noexcept = 0;
[[nodiscard]] virtual std::shared_ptr<Fs const> current_path(std::filesystem::path const& p, std::error_code& ec) const noexcept = 0;

/**
* @brief Creates a new Fs that share the same file system but have different working directory.
*
* @param[in] p Path to change the current working directory to.
* @return Same file system where the working directory is \p p.
*/
[[nodiscard]] virtual std::shared_ptr<Fs> current_path(std::filesystem::path const& p) = 0;

/**
* @brief Creates a new Fs that share the same file system but have different working directory.
*
* @param[in] p Path to change the current working directory to.
* @param[out] ec Error code to store error status to.
* @return Same file system where the working directory is \p p.
*/
[[nodiscard]] virtual std::shared_ptr<Fs> current_path(std::filesystem::path const& p, std::error_code& ec) noexcept = 0;

/**
* @brief Checks whether the path refers to an existing file system object
Expand Down Expand Up @@ -1154,4 +1171,6 @@ std::shared_ptr<Fs> make_mem_fs(std::filesystem::path const& temp_dir = "/tmp");

std::shared_ptr<Fs> make_union_fs(Fs& upper, Fs const& lower);

std::shared_ptr<Fs> make_read_only_fs(Fs const& fs);

} // namespace vfs
30 changes: 10 additions & 20 deletions internal/vfs/impl/fs.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#pragma once

#include <concepts>
#include <filesystem>
#include <memory>
#include <stdexcept>
#include <type_traits>

#include "vfs/impl/file.hpp"

Expand Down Expand Up @@ -34,32 +36,20 @@ std::logic_error err_unknown_fs_() {

} // namespace

inline FsBase& fs_base(Fs& fs) {
auto* b = dynamic_cast<FsBase*>(&fs);
template<typename T>
requires std::same_as<std::remove_const_t<T>, Fs>
inline auto& fs_base(T& fs) {
auto* b = dynamic_cast<std::conditional_t<std::is_const_v<T>, FsBase const, FsBase>*>(&fs);
if(b == nullptr) {
throw err_unknown_fs_();
}
return *b;
}

inline FsBase const& fs_base(Fs const& fs) {
auto const* b = dynamic_cast<FsBase const*>(&fs);
if(b == nullptr) {
throw err_unknown_fs_();
}
return *b;
}

inline std::shared_ptr<FsBase> fs_base(std::shared_ptr<Fs>&& fs) {
auto b = std::dynamic_pointer_cast<FsBase>(std::move(fs));
if(!b) {
throw err_unknown_fs_();
}
return b;
}

inline std::shared_ptr<FsBase> fs_base(std::shared_ptr<Fs> const& fs) {
auto b = std::dynamic_pointer_cast<FsBase>(fs);
template<typename T>
requires std::same_as<std::remove_const_t<T>, Fs>
inline auto fs_base(std::shared_ptr<T> fs) {
auto b = std::dynamic_pointer_cast<std::conditional_t<std::is_const_v<T>, FsBase const, FsBase>>(std::move(fs));
if(!b) {
throw err_unknown_fs_();
}
Expand Down
Loading

0 comments on commit bb72baf

Please sign in to comment.