add Env::GetFileOrThrow()
This commit is contained in:
parent
9b16a36135
commit
744863f466
@ -28,18 +28,28 @@ class FileRef {
|
||||
FileRef& operator=(const FileRef&) = delete;
|
||||
FileRef& operator=(FileRef&&) = delete;
|
||||
|
||||
File& operator*() const
|
||||
File& operator*()
|
||||
try {
|
||||
return owner_->env().GetFile(id_);
|
||||
return owner_->env().GetFileOrThrow(id_);
|
||||
} catch (ExpiredException&) {
|
||||
auto& ret = owner_->ResolveOrThrow(path_);
|
||||
const_cast<File::Id&>(id_) = ret.id();
|
||||
id_ = ret.id();
|
||||
return ret;
|
||||
}
|
||||
|
||||
FileRef& operator=(const File::Path& path) noexcept {
|
||||
return operator=(File::Path{path});
|
||||
}
|
||||
FileRef& operator=(File::Path&& path) noexcept {
|
||||
if (path_ != path) {
|
||||
path_ = std::move(path);
|
||||
id_ = 0;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
const File::Path& path() const noexcept { return path_; }
|
||||
File::Path& path() noexcept { id_ = 0; return path_; }
|
||||
File::Id id() const { **this; return id_; }
|
||||
File::Id id() { **this; return id_; }
|
||||
|
||||
private:
|
||||
File* owner_;
|
||||
@ -63,13 +73,15 @@ struct serializer<
|
||||
nf7::FileRef> {
|
||||
public:
|
||||
template <typename Archive>
|
||||
static Archive& save(Archive& ar, const nf7::FileRef& p) {
|
||||
ar(p.path());
|
||||
static Archive& save(Archive& ar, const nf7::FileRef& ref) {
|
||||
ar(ref.path());
|
||||
return ar;
|
||||
}
|
||||
template <typename Archive>
|
||||
static Archive& load(Archive& ar, nf7::FileRef& p) {
|
||||
ar(p.path());
|
||||
static Archive& load(Archive& ar, nf7::FileRef& ref) {
|
||||
nf7::File::Path path;
|
||||
ar(path);
|
||||
ref = path;
|
||||
return ar;
|
||||
}
|
||||
};
|
||||
|
@ -20,7 +20,7 @@ class LoggerRef final {
|
||||
LoggerRef& operator=(const LoggerRef&) = default;
|
||||
LoggerRef& operator=(LoggerRef&&) = default;
|
||||
|
||||
void SetUp(nf7::File& f, std::string_view name = "_logger") {
|
||||
void SetUp(nf7::File& f, std::string_view name = "_logger") noexcept {
|
||||
try {
|
||||
id_ = f.id();
|
||||
logger_ = f.ResolveUpwardOrThrow(name).
|
||||
@ -29,7 +29,7 @@ class LoggerRef final {
|
||||
id_ = 0;
|
||||
}
|
||||
}
|
||||
void TearDown() {
|
||||
void TearDown() noexcept {
|
||||
id_ = 0;
|
||||
logger_ = nullptr;
|
||||
}
|
||||
|
@ -306,7 +306,7 @@ void Obj::Update() noexcept {
|
||||
if (path != src_.path()) {
|
||||
auto ctx = std::make_shared<nf7::GenericContext>(env(), id());
|
||||
auto task = [this, p = std::move(path)]() mutable {
|
||||
src_.path() = std::move(p);
|
||||
src_ = std::move(p);
|
||||
Reset();
|
||||
};
|
||||
ctx->description() = "changing source path";
|
||||
|
@ -106,7 +106,7 @@ class Logger final : public nf7::File,
|
||||
|
||||
std::string GetPathString(File::Id id) const noexcept
|
||||
try {
|
||||
return env().GetFile(id).abspath().Stringify();
|
||||
return env().GetFileOrThrow(id).abspath().Stringify();
|
||||
} catch (ExpiredException&) {
|
||||
return "[EXPIRED]";
|
||||
}
|
||||
|
9
main.cc
9
main.cc
@ -89,7 +89,7 @@ class Env final : public nf7::Env {
|
||||
void Handle(const File::Event& e) noexcept override
|
||||
try {
|
||||
// trigger File::Handle()
|
||||
GetFile(e.id).Handle(e);
|
||||
GetFileOrThrow(e.id).Handle(e);
|
||||
|
||||
// trigger file watcher
|
||||
auto itr = watchers_map_.find(e.id);
|
||||
@ -128,12 +128,9 @@ class Env final : public nf7::Env {
|
||||
}
|
||||
|
||||
protected:
|
||||
File& GetFile(File::Id id) const override {
|
||||
File* GetFile(File::Id id) const noexcept override {
|
||||
auto itr = files_.find(id);
|
||||
if (itr == files_.end()) {
|
||||
throw ExpiredException("file ("+std::to_string(id)+") is expired");
|
||||
}
|
||||
return *itr->second;
|
||||
return itr != files_.end()? itr->second: nullptr;
|
||||
}
|
||||
File::Id AddFile(File& f) noexcept override {
|
||||
auto [itr, ok] = files_.emplace(file_next_++, &f);
|
||||
|
5
nf7.cc
5
nf7.cc
@ -229,6 +229,11 @@ void Env::Pop() noexcept {
|
||||
env_stack_.pop_back();
|
||||
}
|
||||
|
||||
File& Env::GetFileOrThrow(File::Id id) const {
|
||||
if (auto ret = GetFile(id)) return *ret;
|
||||
throw ExpiredException("file ("+std::to_string(id)+") is expired");
|
||||
}
|
||||
|
||||
Env::Watcher::Watcher(Env& env) noexcept : env_(&env) {
|
||||
}
|
||||
Env::Watcher::~Watcher() noexcept {
|
||||
|
3
nf7.hh
3
nf7.hh
@ -253,7 +253,8 @@ class Env {
|
||||
Env& operator=(const Env&) = delete;
|
||||
Env& operator=(Env&&) = delete;
|
||||
|
||||
virtual File& GetFile(File::Id) const = 0;
|
||||
virtual File* GetFile(File::Id) const noexcept = 0;
|
||||
File& GetFileOrThrow(File::Id) const;
|
||||
|
||||
// all Exec*() methods are thread-safe
|
||||
using Task = std::function<void()>;
|
||||
|
Loading…
x
Reference in New Issue
Block a user