Loaders: Refactor chaining to avoid dup code.

This commit is contained in:
Unknown W. Brackets 2018-12-27 10:32:47 -08:00
parent 359afb2d6b
commit 7f84c87931
9 changed files with 62 additions and 107 deletions

View file

@ -25,12 +25,12 @@
// Takes ownership of backend.
CachingFileLoader::CachingFileLoader(FileLoader *backend)
: backend_(backend) {
: ProxiedFileLoader(backend) {
}
void CachingFileLoader::Prepare() {
std::call_once(preparedFlag_, [this](){
filesize_ = backend_->FileSize();
filesize_ = ProxiedFileLoader::FileSize();
if (filesize_ > 0) {
InitCache();
}
@ -41,27 +41,25 @@ CachingFileLoader::~CachingFileLoader() {
if (filesize_ > 0) {
ShutdownCache();
}
// Takes ownership.
delete backend_;
}
bool CachingFileLoader::Exists() {
if (exists_ == -1) {
exists_ = backend_->Exists() ? 1 : 0;
exists_ = ProxiedFileLoader::Exists() ? 1 : 0;
}
return exists_ == 1;
}
bool CachingFileLoader::ExistsFast() {
if (exists_ == -1) {
return backend_->ExistsFast();
return ProxiedFileLoader::ExistsFast();
}
return exists_ == 1;
}
bool CachingFileLoader::IsDirectory() {
if (isDirectory_ == -1) {
isDirectory_ = backend_->IsDirectory() ? 1 : 0;
isDirectory_ = ProxiedFileLoader::IsDirectory() ? 1 : 0;
}
return isDirectory_ == 1;
}
@ -71,10 +69,6 @@ s64 CachingFileLoader::FileSize() {
return filesize_;
}
std::string CachingFileLoader::Path() const {
return backend_->Path();
}
size_t CachingFileLoader::ReadAt(s64 absolutePos, size_t bytes, void *data, Flags flags) {
Prepare();
if (absolutePos >= filesize_) {
@ -288,11 +282,3 @@ void CachingFileLoader::StartReadAhead(s64 pos) {
});
th.detach();
}
bool CachingFileLoader::IsRemote() {
return backend_->IsRemote();
}
void CachingFileLoader::Cancel() {
backend_->Cancel();
}