refactor: phases 5 & 6 — TransferService, Result<T>, RAII
Phase 5 — TransferService extraction: - New nxst::TransferService owns all sender/receiver state, threads, and atomics (previously 11 file-statics across transfer_sender.cpp + transfer_receiver.cpp) - startReceive() calls io::restore internally; UI never touches infra/fs or infra/net - TitlesLayout routes all transfer ops through mainApp->transfer - g_currentUId removed from global scope; TitlesLayout::InitTitles(AccountUid) - MainApplication gains transfer member; layout refs renamed to snake_case - userAppExit() updated to cancel via service Phase 6 — Result<T> + RAII: - include/nxst/domain/result.hpp: 85-line tagged-union Result<T,E> + Result<void,E> - include/nxst/infra/fs/handles.hpp: FsFileSystemHandle (auto fsFsClose), FileHandle (auto fclose) — eliminates manual close on every error path - io::backup / io::restore return nxst::Result<std::string> (was tuple<bool,Result,string>) - new u8[] + malloc in copyFile/restore replaced with std::vector<u8> - NACP save-creation extracted to createSaveIfNeeded() helper in io.cpp Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
namespace nxst {
|
||||
|
||||
template <class T, class E = std::string>
|
||||
class Result {
|
||||
bool ok;
|
||||
alignas(T) alignas(E) unsigned char storage[sizeof(T) > sizeof(E) ? sizeof(T) : sizeof(E)];
|
||||
|
||||
Result() = default;
|
||||
|
||||
public:
|
||||
static Result success(T val) {
|
||||
Result res;
|
||||
res.ok = true;
|
||||
new (res.storage) T(std::move(val));
|
||||
return res;
|
||||
}
|
||||
|
||||
static Result failure(E err) {
|
||||
Result res;
|
||||
res.ok = false;
|
||||
new (res.storage) E(std::move(err));
|
||||
return res;
|
||||
}
|
||||
|
||||
~Result() {
|
||||
if (ok) reinterpret_cast<T*>(storage)->~T();
|
||||
else reinterpret_cast<E*>(storage)->~E();
|
||||
}
|
||||
|
||||
Result(const Result& other) : ok(other.ok) {
|
||||
if (ok) new (storage) T(*reinterpret_cast<const T*>(other.storage));
|
||||
else new (storage) E(*reinterpret_cast<const E*>(other.storage));
|
||||
}
|
||||
|
||||
Result(Result&& other) : ok(other.ok) {
|
||||
if (ok) new (storage) T(std::move(*reinterpret_cast<T*>(other.storage)));
|
||||
else new (storage) E(std::move(*reinterpret_cast<E*>(other.storage)));
|
||||
}
|
||||
|
||||
Result& operator=(const Result&) = delete;
|
||||
|
||||
bool isOk() const noexcept { return ok; }
|
||||
const T& value() const { return *reinterpret_cast<const T*>(storage); }
|
||||
const E& error() const { return *reinterpret_cast<const E*>(storage); }
|
||||
};
|
||||
|
||||
// Specialisation for Result<void>
|
||||
template <class E>
|
||||
class Result<void, E> {
|
||||
bool ok;
|
||||
alignas(E) unsigned char storage[sizeof(E)];
|
||||
|
||||
Result() = default;
|
||||
|
||||
public:
|
||||
static Result success() { Result res; res.ok = true; return res; }
|
||||
|
||||
static Result failure(E err) {
|
||||
Result res;
|
||||
res.ok = false;
|
||||
new (res.storage) E(std::move(err));
|
||||
return res;
|
||||
}
|
||||
|
||||
~Result() { if (!ok) reinterpret_cast<E*>(storage)->~E(); }
|
||||
|
||||
Result(const Result& other) : ok(other.ok) {
|
||||
if (!ok) new (storage) E(*reinterpret_cast<const E*>(other.storage));
|
||||
}
|
||||
|
||||
Result(Result&& other) : ok(other.ok) {
|
||||
if (!ok) new (storage) E(std::move(*reinterpret_cast<E*>(other.storage)));
|
||||
}
|
||||
|
||||
Result& operator=(const Result&) = delete;
|
||||
|
||||
bool isOk() const noexcept { return ok; }
|
||||
const E& error() const { return *reinterpret_cast<const E*>(storage); }
|
||||
};
|
||||
|
||||
} // namespace nxst
|
||||
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
#include <cstdio>
|
||||
#include <switch.h>
|
||||
|
||||
namespace nxst {
|
||||
|
||||
// RAII wrapper for FsFileSystem — auto-closes on destruction.
|
||||
struct FsFileSystemHandle {
|
||||
FsFileSystem fs{};
|
||||
bool valid{false};
|
||||
|
||||
FsFileSystemHandle() = default;
|
||||
~FsFileSystemHandle() { if (valid) fsFsClose(&fs); } // NOLINT(modernize-use-equals-default)
|
||||
|
||||
FsFileSystemHandle(const FsFileSystemHandle&) = delete;
|
||||
FsFileSystemHandle& operator=(const FsFileSystemHandle&) = delete;
|
||||
|
||||
FsFileSystem* get() { return &fs; }
|
||||
|
||||
void release() { valid = false; } // transfer ownership to devfs
|
||||
};
|
||||
|
||||
// RAII wrapper for FILE* — auto-fclose on destruction.
|
||||
struct FileHandle {
|
||||
FILE* ptr{nullptr};
|
||||
|
||||
explicit FileHandle(FILE* file) : ptr(file) {}
|
||||
~FileHandle() { if (ptr != nullptr) fclose(ptr); } // NOLINT(modernize-use-equals-default)
|
||||
|
||||
FileHandle(const FileHandle&) = delete;
|
||||
FileHandle& operator=(const FileHandle&) = delete;
|
||||
|
||||
explicit operator bool() const { return ptr != nullptr; }
|
||||
FILE* get() const { return ptr; }
|
||||
};
|
||||
|
||||
} // namespace nxst
|
||||
@@ -26,27 +26,26 @@
|
||||
|
||||
#pragma once
|
||||
#include <nxst/domain/account.hpp>
|
||||
#include <nxst/domain/result.hpp>
|
||||
#include <nxst/infra/fs/directory.hpp>
|
||||
#include <nxst/domain/title.hpp>
|
||||
#include <nxst/domain/util.hpp>
|
||||
#include <dirent.h>
|
||||
#include <switch.h>
|
||||
#include <sys/stat.h>
|
||||
#include <tuple>
|
||||
#include <unistd.h>
|
||||
#include <utility>
|
||||
|
||||
#define BUFFER_SIZE 0x80000
|
||||
|
||||
namespace io {
|
||||
std::tuple<bool, Result, std::string> backup(size_t index, AccountUid uid);
|
||||
std::tuple<bool, Result, std::string> restore(size_t index, AccountUid uid, size_t cellIndex, const std::string& nameFromCell);
|
||||
nxst::Result<std::string> backup(size_t index, AccountUid uid);
|
||||
nxst::Result<std::string> restore(size_t index, AccountUid uid, size_t cellIndex, const std::string& nameFromCell);
|
||||
|
||||
Result copyDirectory(const std::string& srcPath, const std::string& dstPath);
|
||||
void copyFile(const std::string& srcPath, const std::string& dstPath);
|
||||
void copyFile(const std::string& srcPath, const std::string& dstPath);
|
||||
Result createDirectory(const std::string& path);
|
||||
Result deleteFolderRecursively(const std::string& path);
|
||||
bool directoryExists(const std::string& path);
|
||||
bool fileExists(const std::string& path);
|
||||
bool directoryExists(const std::string& path);
|
||||
bool fileExists(const std::string& path);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user