FS: Make LowPathType a strongly typed enum.

This commit is contained in:
Subv 2017-12-12 17:41:36 -05:00
parent 6afd091c37
commit 90fdc8dcbf
3 changed files with 28 additions and 28 deletions

View file

@ -14,19 +14,19 @@ namespace FileSys {
Path::Path(LowPathType type, u32 size, u32 pointer) : type(type) {
switch (type) {
case Binary: {
case LowPathType::Binary: {
binary.resize(size);
Memory::ReadBlock(pointer, binary.data(), binary.size());
break;
}
case Char: {
case LowPathType::Char: {
string.resize(size - 1); // Data is always null-terminated.
Memory::ReadBlock(pointer, &string[0], string.size());
break;
}
case Wchar: {
case LowPathType::Wchar: {
u16str.resize(size / 2 - 1); // Data is always null-terminated.
Memory::ReadBlock(pointer, &u16str[0], u16str.size() * sizeof(char16_t));
break;
@ -39,12 +39,12 @@ Path::Path(LowPathType type, u32 size, u32 pointer) : type(type) {
std::string Path::DebugStr() const {
switch (GetType()) {
case Invalid:
case LowPathType::Invalid:
default:
return "[Invalid]";
case Empty:
case LowPathType::Empty:
return "[Empty]";
case Binary: {
case LowPathType::Binary: {
std::stringstream res;
res << "[Binary: ";
for (unsigned byte : binary)
@ -52,23 +52,23 @@ std::string Path::DebugStr() const {
res << ']';
return res.str();
}
case Char:
case LowPathType::Char:
return "[Char: " + AsString() + ']';
case Wchar:
case LowPathType::Wchar:
return "[Wchar: " + AsString() + ']';
}
}
std::string Path::AsString() const {
switch (GetType()) {
case Char:
case LowPathType::Char:
return string;
case Wchar:
case LowPathType::Wchar:
return Common::UTF16ToUTF8(u16str);
case Empty:
case LowPathType::Empty:
return {};
case Invalid:
case Binary:
case LowPathType::Invalid:
case LowPathType::Binary:
default:
// TODO(yuriks): Add assert
LOG_ERROR(Service_FS, "LowPathType cannot be converted to string!");
@ -78,14 +78,14 @@ std::string Path::AsString() const {
std::u16string Path::AsU16Str() const {
switch (GetType()) {
case Char:
case LowPathType::Char:
return Common::UTF8ToUTF16(string);
case Wchar:
case LowPathType::Wchar:
return u16str;
case Empty:
case LowPathType::Empty:
return {};
case Invalid:
case Binary:
case LowPathType::Invalid:
case LowPathType::Binary:
// TODO(yuriks): Add assert
LOG_ERROR(Service_FS, "LowPathType cannot be converted to u16string!");
return {};
@ -96,11 +96,11 @@ std::u16string Path::AsU16Str() const {
std::vector<u8> Path::AsBinary() const {
switch (GetType()) {
case Binary:
case LowPathType::Binary:
return binary;
case Char:
case LowPathType::Char:
return std::vector<u8>(string.begin(), string.end());
case Wchar: {
case LowPathType::Wchar: {
// use two u8 for each character of u16str
std::vector<u8> to_return(u16str.size() * 2);
for (size_t i = 0; i < u16str.size(); ++i) {
@ -110,9 +110,9 @@ std::vector<u8> Path::AsBinary() const {
}
return to_return;
}
case Empty:
case LowPathType::Empty:
return {};
case Invalid:
case LowPathType::Invalid:
default:
// TODO(yuriks): Add assert
LOG_ERROR(Service_FS, "LowPathType cannot be converted to binary!");

View file

@ -19,7 +19,7 @@ class FileBackend;
class DirectoryBackend;
// Path string type
enum LowPathType : u32 {
enum class LowPathType : u32 {
Invalid = 0,
Empty = 1,
Binary = 2,
@ -36,9 +36,9 @@ union Mode {
class Path {
public:
Path() : type(Invalid) {}
Path(const char* path) : type(Char), string(path) {}
Path(std::vector<u8> binary_data) : type(Binary), binary(std::move(binary_data)) {}
Path() : type(LowPathType::Invalid) {}
Path(const char* path) : type(LowPathType::Char), string(path) {}
Path(std::vector<u8> binary_data) : type(LowPathType::Binary), binary(std::move(binary_data)) {}
Path(LowPathType type, u32 size, u32 pointer);
LowPathType GetType() const {

View file

@ -22,7 +22,7 @@ namespace {
template <typename T>
ResultVal<std::tuple<MediaType, u64>> ParsePath(const Path& path, T program_id_reader) {
if (path.GetType() != Binary) {
if (path.GetType() != LowPathType::Binary) {
LOG_ERROR(Service_FS, "Wrong path type %d", static_cast<int>(path.GetType()));
return ERROR_INVALID_PATH;
}