Merge pull request #8290 from lioncash/const-fn

UICommon/NetPlayIndex: Minor interface cleanup
This commit is contained in:
Connor McLaughlin 2019-08-09 23:05:48 +10:00 committed by GitHub
commit f64f4a08c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 14 deletions

View File

@ -25,13 +25,14 @@ NetPlayIndex::~NetPlayIndex()
Remove(); Remove();
} }
static std::optional<picojson::value> ParseResponse(std::vector<u8> response) static std::optional<picojson::value> ParseResponse(const std::vector<u8>& response)
{ {
std::string response_string(reinterpret_cast<char*>(response.data()), response.size()); const std::string response_string(reinterpret_cast<const char*>(response.data()),
response.size());
picojson::value json; picojson::value json;
auto error = picojson::parse(json, response_string); const auto error = picojson::parse(json, response_string);
if (!error.empty()) if (!error.empty())
return {}; return {};
@ -86,8 +87,6 @@ NetPlayIndex::List(const std::map<std::string, std::string>& filters)
for (const auto& entry : entries.get<picojson::array>()) for (const auto& entry : entries.get<picojson::array>())
{ {
NetPlaySession session;
const auto& name = entry.get("name"); const auto& name = entry.get("name");
const auto& region = entry.get("region"); const auto& region = entry.get("region");
const auto& method = entry.get("method"); const auto& method = entry.get("method");
@ -107,6 +106,7 @@ NetPlayIndex::List(const std::map<std::string, std::string>& filters)
continue; continue;
} }
NetPlaySession session;
session.name = name.to_str(); session.name = name.to_str();
session.region = region.to_str(); session.region = region.to_str();
session.game_id = game_id.to_str(); session.game_id = game_id.to_str();
@ -160,7 +160,7 @@ void NetPlayIndex::NotificationLoop()
} }
} }
bool NetPlayIndex::Add(NetPlaySession session) bool NetPlayIndex::Add(const NetPlaySession& session)
{ {
Common::HttpRequest request; Common::HttpRequest request;
auto response = request.Get( auto response = request.Get(
@ -221,7 +221,7 @@ void NetPlayIndex::SetPlayerCount(int player_count)
m_player_count = player_count; m_player_count = player_count;
} }
void NetPlayIndex::SetGame(const std::string game) void NetPlayIndex::SetGame(std::string game)
{ {
m_game = std::move(game); m_game = std::move(game);
} }
@ -257,7 +257,7 @@ std::vector<std::pair<std::string, std::string>> NetPlayIndex::GetRegions()
// It isn't very secure but is preferable to adding another dependency on mbedtls // It isn't very secure but is preferable to adding another dependency on mbedtls
// The encrypted data is encoded as nibbles with the character 'A' as the base offset // The encrypted data is encoded as nibbles with the character 'A' as the base offset
bool NetPlaySession::EncryptID(const std::string& password) bool NetPlaySession::EncryptID(std::string_view password)
{ {
if (password.empty()) if (password.empty())
return false; return false;
@ -285,7 +285,7 @@ bool NetPlaySession::EncryptID(const std::string& password)
return true; return true;
} }
std::optional<std::string> NetPlaySession::DecryptID(const std::string& password) const std::optional<std::string> NetPlaySession::DecryptID(std::string_view password) const
{ {
if (password.empty()) if (password.empty())
return {}; return {};
@ -333,7 +333,7 @@ bool NetPlayIndex::HasActiveSession() const
return !m_secret.empty(); return !m_secret.empty();
} }
void NetPlayIndex::SetErrorCallback(std::function<void()> function) void NetPlayIndex::SetErrorCallback(std::function<void()> callback)
{ {
m_error_callback = function; m_error_callback = std::move(callback);
} }

View File

@ -8,6 +8,7 @@
#include <map> #include <map>
#include <optional> #include <optional>
#include <string> #include <string>
#include <string_view>
#include <thread> #include <thread>
#include <utility> #include <utility>
#include <vector> #include <vector>
@ -29,8 +30,8 @@ struct NetPlaySession
bool has_password; bool has_password;
bool in_game; bool in_game;
bool EncryptID(const std::string& password); bool EncryptID(std::string_view password);
std::optional<std::string> DecryptID(const std::string& password) const; std::optional<std::string> DecryptID(std::string_view password) const;
}; };
class NetPlayIndex class NetPlayIndex
@ -44,7 +45,7 @@ public:
static std::vector<std::pair<std::string, std::string>> GetRegions(); static std::vector<std::pair<std::string, std::string>> GetRegions();
bool Add(NetPlaySession session); bool Add(const NetPlaySession& session);
void Remove(); void Remove();
bool HasActiveSession() const; bool HasActiveSession() const;