Skip to content

Commit

Permalink
Game Initialization/Lobby Info: use unique_ptr for game_info map
Browse files Browse the repository at this point in the history
  • Loading branch information
Vultraz committed Aug 28, 2017
1 parent 520371b commit d3fe82b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 27 deletions.
30 changes: 8 additions & 22 deletions src/game_initialization/lobby_info.cpp
Expand Up @@ -55,18 +55,6 @@ lobby_info::lobby_info(const config& game_config, const std::vector<std::string>
{
}

lobby_info::~lobby_info()
{
delete_games();
}

void lobby_info::delete_games()
{
for(const auto & v : games_by_id_) {
delete v.second;
}
}

void do_notify(notify_mode mode, const std::string & sender, const std::string & message)
{
switch (mode) {
Expand Down Expand Up @@ -128,12 +116,12 @@ void lobby_info::process_gamelist(const config& data)
SCOPE_LB;
gamelist_ = data;
gamelist_initialized_ = true;
delete_games();

games_by_id_.clear();

for(const auto & c : gamelist_.child("gamelist").child_range("game")) {
game_info* game = new game_info(c, game_config_, installed_addons_);
games_by_id_[game->id] = game;
std::unique_ptr<game_info> game(new game_info(c, game_config_, installed_addons_));
games_by_id_.emplace(game->id, std::move(game));
}

DBG_LB << dump_games_map(games_by_id_);
Expand Down Expand Up @@ -189,9 +177,7 @@ bool lobby_info::process_gamelist_diff(const config& data)

if(current_i->second->display_status == game_info::NEW) {
// This means the game never made it through to the user interface,
// so just deleting it is fine
// TODO: use std::unique_ptr instead of deleting manually.
delete current_i->second;
// so just deleting it is fine.
games_by_id_.erase(current_i);
} else {
current_i->second->display_status = game_info::DELETED;
Expand Down Expand Up @@ -254,7 +240,7 @@ void lobby_info::sync_games_display_status()
game_info_map::iterator i = games_by_id_.begin();
while(i != games_by_id_.end()) {
if(i->second->display_status == game_info::DELETED) {
delete i->second;

i = games_by_id_.erase(i);
} else {
i->second->display_status = game_info::CLEAN;
Expand All @@ -269,13 +255,13 @@ void lobby_info::sync_games_display_status()
game_info* lobby_info::get_game_by_id(int id)
{
game_info_map::iterator i = games_by_id_.find(id);
return i == games_by_id_.end() ? nullptr : i->second;
return i == games_by_id_.end() ? nullptr : i->second.get();
}

const game_info* lobby_info::get_game_by_id(int id) const
{
game_info_map::const_iterator i = games_by_id_.find(id);
return i == games_by_id_.end() ? nullptr : i->second;
return i == games_by_id_.end() ? nullptr : i->second.get();
}

room_info* lobby_info::get_room(const std::string& name)
Expand Down Expand Up @@ -362,7 +348,7 @@ void lobby_info::make_games_vector()
games_.clear();

for(const auto & v : games_by_id_) {
games_.push_back(v.second);
games_.push_back(v.second.get());
}
}

Expand Down
7 changes: 2 additions & 5 deletions src/game_initialization/lobby_info.hpp
Expand Up @@ -17,6 +17,7 @@
#include "config.hpp"
#include "game_initialization/lobby_data.hpp"
#include <boost/dynamic_bitset.hpp>
#include <memory>

namespace mp {

Expand All @@ -29,11 +30,7 @@ class lobby_info
public:
explicit lobby_info(const config& game_config, const std::vector<std::string>& installed_addons);

~lobby_info();

void delete_games();

typedef std::map<int, game_info*> game_info_map;
typedef std::map<int, std::unique_ptr<game_info>> game_info_map;

using game_filter_func = std::function<bool(const game_info&)>;

Expand Down

0 comments on commit d3fe82b

Please sign in to comment.