archive: Make use of std::pair for OpenFileFromArchive (#5399)

* archive: Make use of std::pair for OpenFileFromArchive

The tuple only makes use of two elements, so we can use a pair to
simplify the number of underlying template instantiations that need to
be done, while also simplifying the surrounding code.

* archive: Simplify MakeResult calls

MakeResult can deduce the contained result type based off the type of
the variable being passed to it, so explicitly specifying it is a little
verbose.
This commit is contained in:
LC 2020-06-19 10:02:00 -04:00 committed by GitHub
parent 1504018a56
commit f7aaa37bf2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 18 deletions

View File

@ -54,7 +54,7 @@ ResultVal<ArchiveHandle> ArchiveManager::OpenArchive(ArchiveIdCode id_code,
++next_handle;
}
handle_map.emplace(next_handle, std::move(res));
return MakeResult<ArchiveHandle>(next_handle++);
return MakeResult(next_handle++);
}
ResultCode ArchiveManager::CloseArchive(ArchiveHandle handle) {
@ -79,21 +79,22 @@ ResultCode ArchiveManager::RegisterArchiveType(std::unique_ptr<FileSys::ArchiveF
return RESULT_SUCCESS;
}
std::tuple<ResultVal<std::shared_ptr<File>>, std::chrono::nanoseconds>
std::pair<ResultVal<std::shared_ptr<File>>, std::chrono::nanoseconds>
ArchiveManager::OpenFileFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path,
const FileSys::Mode mode) {
ArchiveBackend* archive = GetArchive(archive_handle);
if (archive == nullptr)
return std::make_tuple(FileSys::ERR_INVALID_ARCHIVE_HANDLE,
static_cast<std::chrono::nanoseconds>(0));
if (archive == nullptr) {
return std::make_pair(FileSys::ERR_INVALID_ARCHIVE_HANDLE, std::chrono::nanoseconds{0});
}
std::chrono::nanoseconds open_timeout_ns{archive->GetOpenDelayNs()};
const std::chrono::nanoseconds open_timeout_ns{archive->GetOpenDelayNs()};
auto backend = archive->OpenFile(path, mode);
if (backend.Failed())
return std::make_tuple(backend.Code(), open_timeout_ns);
if (backend.Failed()) {
return std::make_pair(backend.Code(), open_timeout_ns);
}
auto file = std::make_shared<File>(system.Kernel(), std::move(backend).Unwrap(), path);
return std::make_tuple(MakeResult<std::shared_ptr<File>>(std::move(file)), open_timeout_ns);
return std::make_pair(MakeResult(std::move(file)), open_timeout_ns);
}
ResultCode ArchiveManager::DeleteFileFromArchive(ArchiveHandle archive_handle,
@ -178,22 +179,25 @@ ResultCode ArchiveManager::RenameDirectoryBetweenArchives(ArchiveHandle src_arch
ResultVal<std::shared_ptr<Directory>> ArchiveManager::OpenDirectoryFromArchive(
ArchiveHandle archive_handle, const FileSys::Path& path) {
ArchiveBackend* archive = GetArchive(archive_handle);
if (archive == nullptr)
if (archive == nullptr) {
return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
}
auto backend = archive->OpenDirectory(path);
if (backend.Failed())
if (backend.Failed()) {
return backend.Code();
}
auto directory = std::make_shared<Directory>(std::move(backend).Unwrap(), path);
return MakeResult<std::shared_ptr<Directory>>(std::move(directory));
return MakeResult(std::move(directory));
}
ResultVal<u64> ArchiveManager::GetFreeBytesInArchive(ArchiveHandle archive_handle) {
ArchiveBackend* archive = GetArchive(archive_handle);
if (archive == nullptr)
const ArchiveBackend* archive = GetArchive(archive_handle);
if (archive == nullptr) {
return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
return MakeResult<u64>(archive->GetFreeBytes());
}
return MakeResult(archive->GetFreeBytes());
}
ResultCode ArchiveManager::FormatArchive(ArchiveIdCode id_code,

View File

@ -86,10 +86,10 @@ public:
* @param archive_handle Handle to an open Archive object
* @param path Path to the File inside of the Archive
* @param mode Mode under which to open the File
* @return Tuple of the opened File object and the open delay
* @return Pair containing the opened File object and the open delay
*/
std::tuple<ResultVal<std::shared_ptr<File>>, std::chrono::nanoseconds> OpenFileFromArchive(
ArchiveHandle archive_handle, const FileSys::Path& path, const FileSys::Mode mode);
std::pair<ResultVal<std::shared_ptr<File>>, std::chrono::nanoseconds> OpenFileFromArchive(
ArchiveHandle archive_handle, const FileSys::Path& path, FileSys::Mode mode);
/**
* Delete a File from an Archive