From 0b6f8ba51e61792ea23a55394a963d0961a9906f Mon Sep 17 00:00:00 2001 From: David Marcec Date: Sun, 12 Aug 2018 01:34:22 +1000 Subject: Code cleanup for profile manager --- src/core/hle/service/acc/profile_manager.cpp | 60 ++++++++++++++-------------- 1 file changed, 31 insertions(+), 29 deletions(-) (limited to 'src/core/hle/service/acc/profile_manager.cpp') diff --git a/src/core/hle/service/acc/profile_manager.cpp b/src/core/hle/service/acc/profile_manager.cpp index e8f6884d1..ee13ae3cd 100644 --- a/src/core/hle/service/acc/profile_manager.cpp +++ b/src/core/hle/service/acc/profile_manager.cpp @@ -2,6 +2,7 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include #include "core/hle/service/acc/profile_manager.h" #include "core/settings.h" @@ -12,20 +13,21 @@ constexpr ResultCode ERROR_USER_ALREADY_EXISTS(ErrorModule::Account, -2); constexpr ResultCode ERROR_ARGUMENT_IS_NULL(ErrorModule::Account, 20); ProfileManager::ProfileManager() { + // TODO(ogniK): Create the default user we have for now until loading/saving users is added auto user_uuid = UUID{1, 0}; CreateNewUser(user_uuid, Settings::values.username); OpenUser(user_uuid); } -size_t ProfileManager::AddToProfiles(const ProfileInfo& user) { +boost::optional ProfileManager::AddToProfiles(const ProfileInfo& user) { if (user_count >= MAX_USERS) { - return std::numeric_limits::max(); + return boost::none; } profiles[user_count] = std::move(user); return user_count++; } -bool ProfileManager::RemoveProfileAtIdx(size_t index) { +bool ProfileManager::RemoveProfileAtIndex(size_t index) { if (index >= MAX_USERS || index >= user_count) { return false; } @@ -38,7 +40,7 @@ bool ProfileManager::RemoveProfileAtIdx(size_t index) { } ResultCode ProfileManager::AddUser(ProfileInfo user) { - if (AddToProfiles(user) == std::numeric_limits::max()) { + if (AddToProfiles(user) == boost::none) { return ERROR_TOO_MANY_USERS; } return RESULT_SUCCESS; @@ -58,13 +60,13 @@ ResultCode ProfileManager::CreateNewUser(UUID uuid, std::array& userna [&uuid](const ProfileInfo& profile) { return uuid == profile.user_uuid; })) { return ERROR_USER_ALREADY_EXISTS; } - ProfileInfo prof_inf; - prof_inf.user_uuid = std::move(uuid); - prof_inf.username = std::move(username); - prof_inf.data = std::array(); - prof_inf.creation_time = 0x0; - prof_inf.is_open = false; - return AddUser(prof_inf); + ProfileInfo profile; + profile.user_uuid = std::move(uuid); + profile.username = std::move(username); + profile.data = {}; + profile.creation_time = 0x0; + profile.is_open = false; + return AddUser(profile); } ResultCode ProfileManager::CreateNewUser(UUID uuid, const std::string& username) { @@ -77,28 +79,27 @@ ResultCode ProfileManager::CreateNewUser(UUID uuid, const std::string& username) return CreateNewUser(uuid, username_output); } -size_t ProfileManager::GetUserIndex(const UUID& uuid) const { +boost::optional ProfileManager::GetUserIndex(const UUID& uuid) const { if (!uuid) { - return std::numeric_limits::max(); + return boost::none; } auto iter = std::find_if(profiles.begin(), profiles.end(), [&uuid](const ProfileInfo& p) { return p.user_uuid == uuid; }); if (iter == profiles.end()) { - return std::numeric_limits::max(); + return boost::none; } return static_cast(std::distance(profiles.begin(), iter)); } -size_t ProfileManager::GetUserIndex(ProfileInfo user) const { +boost::optional ProfileManager::GetUserIndex(ProfileInfo user) const { return GetUserIndex(user.user_uuid); } -bool ProfileManager::GetProfileBase(size_t index, ProfileBase& profile) const { - if (index >= MAX_USERS) { - profile.Invalidate(); +bool ProfileManager::GetProfileBase(boost::optional index, ProfileBase& profile) const { + if (index == boost::none || index >= MAX_USERS) { return false; } - const auto& prof_info = profiles[index]; + const auto& prof_info = profiles[index.get()]; profile.user_uuid = prof_info.user_uuid; profile.username = prof_info.username; profile.timestamp = prof_info.creation_time; @@ -124,24 +125,24 @@ size_t ProfileManager::GetOpenUserCount() const { } bool ProfileManager::UserExists(UUID uuid) const { - return (GetUserIndex(uuid) != std::numeric_limits::max()); + return (GetUserIndex(uuid) != boost::none); } void ProfileManager::OpenUser(UUID uuid) { auto idx = GetUserIndex(uuid); - if (idx == std::numeric_limits::max()) { + if (idx == boost::none) { return; } - profiles[idx].is_open = true; + profiles[idx.get()].is_open = true; last_opened_user = uuid; } void ProfileManager::CloseUser(UUID uuid) { auto idx = GetUserIndex(uuid); - if (idx == std::numeric_limits::max()) { + if (idx == boost::none) { return; } - profiles[idx].is_open = false; + profiles[idx.get()].is_open = false; } std::array ProfileManager::GetAllUsers() const { @@ -166,22 +167,23 @@ UUID ProfileManager::GetLastOpenedUser() const { return last_opened_user; } -bool ProfileManager::GetProfileBaseAndData(size_t index, ProfileBase& profile, - std::array& data) { +bool ProfileManager::GetProfileBaseAndData(boost::optional index, ProfileBase& profile, + std::array& data) const { if (GetProfileBase(index, profile)) { - std::memcpy(data.data(), profiles[index].data.data(), MAX_DATA); + std::memcpy(data.data(), profiles[index.get()].data.data(), MAX_DATA); return true; } return false; } + bool ProfileManager::GetProfileBaseAndData(UUID uuid, ProfileBase& profile, - std::array& data) { + std::array& data) const { auto idx = GetUserIndex(uuid); return GetProfileBaseAndData(idx, profile, data); } bool ProfileManager::GetProfileBaseAndData(ProfileInfo user, ProfileBase& profile, - std::array& data) { + std::array& data) const { return GetProfileBaseAndData(user.user_uuid, profile, data); } -- cgit v1.2.3