Migrate to std::shared_ptr<Game>

This commit is contained in:
Loic Guegan 2022-02-28 20:16:57 +01:00
parent 4c959fe12e
commit 44ea0a50a3
16 changed files with 44 additions and 49 deletions

View file

@ -52,7 +52,7 @@ MainWindow::MainWindow()
Bind(REFRESH_ENGINE_LIST, &MainWindow::OnRefreshEngineList, this, wxID_ANY); Bind(REFRESH_ENGINE_LIST, &MainWindow::OnRefreshEngineList, this, wxID_ANY);
// Add new game tab by default // Add new game tab by default
NewGame(new Game()); NewGame(std::shared_ptr<Game>(new Game()));
} }
void MainWindow::OnCloseTabEvent(wxCommandEvent &event) { void MainWindow::OnCloseTabEvent(wxCommandEvent &event) {
@ -191,8 +191,9 @@ void MainWindow::NewGame(bool useFen) {
} }
void MainWindow::OnNewGame(wxCommandEvent &event) { void MainWindow::OnNewGame(wxCommandEvent &event) {
Game *g = (Game *)event.GetClientData(); std::shared_ptr<Game> *g = (std::shared_ptr<Game>*)event.GetClientData();
NewGame(g); NewGame(*g);
delete g;
} }
void MainWindow::OnPageChange(wxAuiNotebookEvent &event) { void MainWindow::OnPageChange(wxAuiNotebookEvent &event) {
@ -214,7 +215,7 @@ void MainWindow::OnRefreshTabTitle(wxCommandEvent &event) {
} }
} }
void MainWindow::NewGame(Game *game) { void MainWindow::NewGame(std::shared_ptr<Game> game) {
GameTab *gt = new GameTab((wxFrame *)notebook, game); GameTab *gt = new GameTab((wxFrame *)notebook, game);
notebook->AddPage(gt, gt->GetLabel()); notebook->AddPage(gt, gt->GetLabel());
notebook->SetSelection(notebook->GetPageIndex(gt)); notebook->SetSelection(notebook->GetPageIndex(gt));

View file

@ -23,7 +23,7 @@ class MainWindow : public MainFrame {
void OpenFile(); void OpenFile();
void OnPageChange(wxAuiNotebookEvent &event); void OnPageChange(wxAuiNotebookEvent &event);
void OnRefreshTabTitle(wxCommandEvent &event); void OnRefreshTabTitle(wxCommandEvent &event);
void NewGame(Game *game); void NewGame(std::shared_ptr<Game> game);
void OpenSettings(); void OpenSettings();
void NewEngine(); void NewEngine();
void OnCloseTabEvent(wxCommandEvent &event); void OnCloseTabEvent(wxCommandEvent &event);

View file

@ -27,7 +27,7 @@ void AppendGameDialog::OnCancel(wxCommandEvent &event) { this->Close(); }
void AppendGameDialog::OnImport(wxCommandEvent &event) { void AppendGameDialog::OnImport(wxCommandEvent &event) {
std::vector<std::uint32_t> to_ignore; std::vector<std::uint32_t> to_ignore;
std::vector<GameBase *> new_games_bases; std::vector<GameBase *> new_games_bases;
std::vector<Game *> new_games; std::vector<std::shared_ptr<Game>> new_games;
wxArrayInt selections; wxArrayInt selections;
game_list->GetSelections(selections); game_list->GetSelections(selections);
@ -37,7 +37,7 @@ void AppendGameDialog::OnImport(wxCommandEvent &event) {
if (tinfo->type == TabInfos::BASE) { if (tinfo->type == TabInfos::BASE) {
new_games_bases.push_back(static_cast<GameBase *>(tinfo->GetBase())); new_games_bases.push_back(static_cast<GameBase *>(tinfo->GetBase()));
} else if (tinfo->type == TabInfos::GAME) { } else if (tinfo->type == TabInfos::GAME) {
new_games.push_back(static_cast<Game *>(tinfo->GetGame())); new_games.push_back(tinfo->GetGame());
} }
} }

View file

@ -54,7 +54,7 @@ void BaseTab::OnDelete(wxCommandEvent &event) {
void BaseTab::OnSave(wxCommandEvent &event) { void BaseTab::OnSave(wxCommandEvent &event) {
std::vector<GameBase *> new_games_bases; std::vector<GameBase *> new_games_bases;
std::vector<Game *> new_games; std::vector<std::shared_ptr<Game>> new_games;
new_games.insert( new_games.insert(
new_games.end(), edited.begin(), new_games.end(), edited.begin(),
edited.end()); // Add edited game (since they are also deleted) edited.end()); // Add edited game (since they are also deleted)
@ -67,9 +67,9 @@ void BaseTab::OnSave(wxCommandEvent &event) {
void BaseTab::OnOpenGame(wxListEvent &event) { void BaseTab::OnOpenGame(wxListEvent &event) {
wxLogDebug("Open!"); wxLogDebug("Open!");
long id = std::stoi(event.GetItem().GetText().ToStdString()); long id = std::stoi(event.GetItem().GetText().ToStdString());
Game *g = base->GetGame(id); std::shared_ptr<Game> *g =new std::shared_ptr<Game>(base->GetGame(id));
if (g != NULL) { if (g != NULL) {
edited.push_back(g); edited.push_back(*g);
deleted.push_back(id); deleted.push_back(id);
game_list->SetItemBackgroundColour(event.GetIndex(), *wxGREEN); game_list->SetItemBackgroundColour(event.GetIndex(), *wxGREEN);
wxCommandEvent newGameEvent(NEW_GAME_EVENT, GetId()); wxCommandEvent newGameEvent(NEW_GAME_EVENT, GetId());

View file

@ -10,7 +10,7 @@ wxDECLARE_EVENT(REFRESH_TAB_TITLE, wxCommandEvent);
class BaseTab : public TabBase, public TabInfos { class BaseTab : public TabBase, public TabInfos {
GameBase *base; GameBase *base;
std::vector<std::uint32_t> deleted; std::vector<std::uint32_t> deleted;
std::vector<Game *> edited; std::vector<std::shared_ptr<Game>> edited;
std::string base_file; std::string base_file;
public: public:
@ -23,6 +23,6 @@ public:
void OnExport(wxCommandEvent &event); void OnExport(wxCommandEvent &event);
void OnOpenGame(wxListEvent &event); void OnOpenGame(wxListEvent &event);
void OnImport(wxCommandEvent &event); void OnImport(wxCommandEvent &event);
void *GetGame() { return (NULL); } std::shared_ptr<Game> GetGame() { return (std::shared_ptr<Game>(NULL)); }
void *GetBase() { return (base); }; void *GetBase() { return (base); };
}; };

View file

@ -6,11 +6,11 @@
class GameBase { class GameBase {
public: public:
virtual Game *GetGame(std::uint32_t id) = 0; virtual std::shared_ptr<Game> GetGame(std::uint32_t id) = 0;
virtual void Save(std::vector<std::uint32_t> to_ignore, virtual void Save(std::vector<std::uint32_t> to_ignore,
std::vector<GameBase *> new_games_bases, std::vector<GameBase *> new_games_bases,
std::vector<Game *> new_games) = 0; std::vector<std::shared_ptr<Game>> new_games) = 0;
virtual Game *GetCurrentGame() = 0; virtual std::shared_ptr<Game> GetCurrentGame() = 0;
virtual bool NextGame() = 0; virtual bool NextGame() = 0;
virtual std::string GetTag(std::string tag) = 0; virtual std::string GetTag(std::string tag) = 0;
virtual void Reset() = 0; virtual void Reset() = 0;

View file

@ -28,7 +28,7 @@ std::string PGNGameBase::GetTag(std::string tag) {
return (""); return ("");
} }
Game *PGNGameBase::GetCurrentGame() { std::shared_ptr<Game> PGNGameBase::GetCurrentGame() {
pgnp::HalfMove *pgnp_moves = new pgnp::HalfMove(); pgnp::HalfMove *pgnp_moves = new pgnp::HalfMove();
pgn->GetMoves(pgnp_moves); pgn->GetMoves(pgnp_moves);
std::string fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; std::string fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
@ -42,7 +42,7 @@ Game *PGNGameBase::GetCurrentGame() {
g->SetTag(s, pgn->GetTagValue(s)); g->SetTag(s, pgn->GetTagValue(s));
} }
g->SetResult(pgn->GetResult()); g->SetResult(pgn->GetResult());
return (g); return (std::shared_ptr<Game>(g));
} }
void PGNGameBase::Reset() { void PGNGameBase::Reset() {
@ -51,7 +51,7 @@ void PGNGameBase::Reset() {
pgn->FromFile(file); pgn->FromFile(file);
} }
Game *PGNGameBase::GetGame(std::uint32_t id) { std::shared_ptr<Game> PGNGameBase::GetGame(std::uint32_t id) {
Reset(); Reset();
std::uint32_t curid = 0; std::uint32_t curid = 0;
while (NextGame()) { while (NextGame()) {
@ -60,12 +60,12 @@ Game *PGNGameBase::GetGame(std::uint32_t id) {
} }
curid++; curid++;
} }
return (NULL); return (std::shared_ptr<Game>(NULL));
} }
void PGNGameBase::Save(std::vector<std::uint32_t> to_ignore, void PGNGameBase::Save(std::vector<std::uint32_t> to_ignore,
std::vector<GameBase *> new_games_bases, std::vector<GameBase *> new_games_bases,
std::vector<Game *> new_games) { std::vector<std::shared_ptr<Game>> new_games) {
wxStandardPaths stdPaths = wxStandardPaths::Get(); wxStandardPaths stdPaths = wxStandardPaths::Get();
wxString tmp = stdPaths.GetTempDir() + "/save_pgn_tmp.pgn"; wxString tmp = stdPaths.GetTempDir() + "/save_pgn_tmp.pgn";
wxFile new_pgn(tmp, wxFile::write); wxFile new_pgn(tmp, wxFile::write);
@ -80,9 +80,8 @@ void PGNGameBase::Save(std::vector<std::uint32_t> to_ignore,
} else { } else {
several = true; several = true;
} }
Game *g = GetCurrentGame(); std::shared_ptr<Game> g = GetCurrentGame();
new_pgn.Write(g->GetPGN()); new_pgn.Write(g->GetPGN());
delete g;
} }
id++; id++;
} }
@ -96,13 +95,12 @@ void PGNGameBase::Save(std::vector<std::uint32_t> to_ignore,
} else { } else {
several = true; several = true;
} }
Game *g = current->GetCurrentGame(); std::shared_ptr<Game> g = current->GetCurrentGame();
new_pgn.Write(g->GetPGN()); new_pgn.Write(g->GetPGN());
delete g;
} }
} }
for (Game *g : new_games) { for (std::shared_ptr<Game> g : new_games) {
if (several) { if (several) {
new_pgn.Write("\n\n"); new_pgn.Write("\n\n");
} else { } else {
@ -127,9 +125,8 @@ void PGNGameBase::Export(GameBase *base) {
} else { } else {
several = true; several = true;
} }
Game *g = base->GetCurrentGame(); std::shared_ptr<Game> g = base->GetCurrentGame();
new_pgn.Write(g->GetPGN()); new_pgn.Write(g->GetPGN());
delete g;
} }
new_pgn.Close(); new_pgn.Close();

View file

@ -9,13 +9,13 @@ class PGNGameBase : public GameBase {
public: public:
PGNGameBase(std::string pgn_file); PGNGameBase(std::string pgn_file);
~PGNGameBase(); ~PGNGameBase();
Game *GetGame(std::uint32_t id); std::shared_ptr<Game> GetGame(std::uint32_t id);
bool NextGame(); bool NextGame();
Game *GetCurrentGame(); std::shared_ptr<Game> GetCurrentGame();
std::string GetTag(std::string tag); std::string GetTag(std::string tag);
void Save(std::vector<std::uint32_t> to_ignore, void Save(std::vector<std::uint32_t> to_ignore,
std::vector<GameBase *> new_games_bases, std::vector<GameBase *> new_games_bases,
std::vector<Game *> new_games); std::vector<std::shared_ptr<Game>> new_games);
void Reset(); void Reset();
void Export(GameBase *base); void Export(GameBase *base);
}; };

View file

@ -18,7 +18,7 @@ public:
EngineTab(wxWindow *parent, std::string name); EngineTab(wxWindow *parent, std::string name);
~EngineTab(); ~EngineTab();
void ApplyPreferences() {} void ApplyPreferences() {}
void *GetGame() { return (NULL); } std::shared_ptr<Game> GetGame() { return (std::shared_ptr<Game>(NULL)); }
void *GetBase() { return (NULL); } void *GetBase() { return (NULL); }
void OnSave(wxCommandEvent &event); void OnSave(wxCommandEvent &event);
void OnDelete(wxCommandEvent &event); void OnDelete(wxCommandEvent &event);

View file

@ -3,7 +3,7 @@
wxDEFINE_EVENT(GAME_CHANGE, wxCommandEvent); wxDEFINE_EVENT(GAME_CHANGE, wxCommandEvent);
GameTab::GameTab(wxFrame *parent, Game *game) GameTab::GameTab(wxFrame *parent, std::shared_ptr<Game> game)
: wxPanel(parent), game(game), TabInfos(TabInfos::GAME) { : wxPanel(parent), game(game), TabInfos(TabInfos::GAME) {
// Splitter // Splitter
wxSplitterWindow *splitter = new wxSplitterWindow(this, wxID_ANY); wxSplitterWindow *splitter = new wxSplitterWindow(this, wxID_ANY);
@ -30,10 +30,6 @@ GameTab::GameTab(wxFrame *parent, Game *game)
Bind(GAME_CHANGE, &GameTab::OnGameChange, this, wxID_ANY); Bind(GAME_CHANGE, &GameTab::OnGameChange, this, wxID_ANY);
} }
GameTab::~GameTab() {
delete game;
}
void GameTab::OnGameChange(wxCommandEvent &event) { void GameTab::OnGameChange(wxCommandEvent &event) {
board_panel->Notify(); board_panel->Notify();
editor_panel->Notify(); editor_panel->Notify();

View file

@ -16,16 +16,15 @@ wxDECLARE_EVENT(GAME_CHANGE, wxCommandEvent);
class GameTab : public wxPanel, public TabInfos { class GameTab : public wxPanel, public TabInfos {
GameTabRightPanel *editor_panel; GameTabRightPanel *editor_panel;
GameTabLeftPanel *board_panel; GameTabLeftPanel *board_panel;
Game *game; std::shared_ptr<Game> game;
void RefreshLabel(); void RefreshLabel();
void OnRefreshTabTitle(wxCommandEvent &event); void OnRefreshTabTitle(wxCommandEvent &event);
void OnGameChange(wxCommandEvent &event); void OnGameChange(wxCommandEvent &event);
public: public:
GameTab(wxFrame *parent, Game *game); GameTab(wxFrame *parent, std::shared_ptr<Game> game);
~GameTab();
void ApplyPreferences(); void ApplyPreferences();
void *GetGame() { return (game); } std::shared_ptr<Game> GetGame() { return (std::shared_ptr<Game>(game)); }
void *GetBase() { return (NULL); }; void *GetBase() { return (NULL); };
}; };

View file

@ -1,7 +1,7 @@
#include "GameTabLeftPanel.hpp" #include "GameTabLeftPanel.hpp"
#include <wx/clipbrd.h> #include <wx/clipbrd.h>
GameTabLeftPanel::GameTabLeftPanel(wxFrame *parent, Game *game) GameTabLeftPanel::GameTabLeftPanel(wxFrame *parent, std::shared_ptr<Game> game)
: TabGameLeftPanel(parent), game(game) { : TabGameLeftPanel(parent), game(game) {
// Add board // Add board

View file

@ -9,12 +9,12 @@ wxDECLARE_EVENT(GAME_CHANGE, wxCommandEvent);
class GameTabLeftPanel : public TabGameLeftPanel { class GameTabLeftPanel : public TabGameLeftPanel {
Game *game; std::shared_ptr<Game> game;
BoardCanvas *board_canvas; BoardCanvas *board_canvas;
void NotifyEditor(); void NotifyEditor();
public: public:
GameTabLeftPanel(wxFrame *parent, Game *game); GameTabLeftPanel(wxFrame *parent, std::shared_ptr<Game> game);
void Notify(); void Notify();
void OnPlay(wxCommandEvent &event); void OnPlay(wxCommandEvent &event);
void OnGotoMove(wxCommandEvent &event); void OnGotoMove(wxCommandEvent &event);

View file

@ -7,7 +7,7 @@ wxDEFINE_EVENT(SET_AS_MAINLINE_EVENT, wxCommandEvent);
wxDEFINE_EVENT(PREVIOUS_MOVE_EVENT, wxCommandEvent); wxDEFINE_EVENT(PREVIOUS_MOVE_EVENT, wxCommandEvent);
wxDEFINE_EVENT(NEXT_MOVE_EVENT, wxCommandEvent); wxDEFINE_EVENT(NEXT_MOVE_EVENT, wxCommandEvent);
GameTabRightPanel::GameTabRightPanel(wxFrame *parent, Game *game) GameTabRightPanel::GameTabRightPanel(wxFrame *parent, std::shared_ptr<Game> game)
: TabGameRightPanel(parent), game(game), selected_item(-1), : TabGameRightPanel(parent), game(game), selected_item(-1),
live_engine(NULL) { live_engine(NULL) {
editor_canvas = new EditorCanvas((wxFrame *)editor_page); editor_canvas = new EditorCanvas((wxFrame *)editor_page);

View file

@ -16,13 +16,13 @@ wxDECLARE_EVENT(REFRESH_TAB_TITLE, wxCommandEvent);
wxDECLARE_EVENT(GAME_CHANGE, wxCommandEvent); wxDECLARE_EVENT(GAME_CHANGE, wxCommandEvent);
class GameTabRightPanel : public TabGameRightPanel { class GameTabRightPanel : public TabGameRightPanel {
Game *game; std::shared_ptr<Game> game;
EditorCanvas *editor_canvas; EditorCanvas *editor_canvas;
long selected_item; long selected_item;
LiveEngineDialog *live_engine; LiveEngineDialog *live_engine;
public: public:
GameTabRightPanel(wxFrame *parent, Game *game); GameTabRightPanel(wxFrame *parent, std::shared_ptr<Game> game);
void NotifyBoard(); void NotifyBoard();
void Notify(); void Notify();
void OnCommentChange(wxCommandEvent &event); void OnCommentChange(wxCommandEvent &event);

View file

@ -6,12 +6,12 @@
#endif #endif
#include "binres/binres.hpp" #include "binres/binres.hpp"
#include "gui.h"
#include <memory>
#include <wx/app.h> #include <wx/app.h>
#include <wx/config.h> #include <wx/config.h>
#include <wx/filefn.h> // Check file exists etc #include <wx/filefn.h> // Check file exists etc
#include <wx/log.h> #include <wx/log.h>
#include "gui.h"
#include <memory>
#define MAINWIN ((MainWindow *)wxGetApp().GetTopWindow()) #define MAINWIN ((MainWindow *)wxGetApp().GetTopWindow())
@ -46,6 +46,8 @@ wxDECLARE_APP(MyApp);
///@brief Abort ochess with a message ///@brief Abort ochess with a message
void Abort(std::string msg); void Abort(std::string msg);
class Game;
/** /**
* @brief Attach informations to the application tabs * @brief Attach informations to the application tabs
* *
@ -56,6 +58,6 @@ public:
Type type; Type type;
TabInfos(Type type_) : type(type_) {} TabInfos(Type type_) : type(type_) {}
virtual void ApplyPreferences() = 0; virtual void ApplyPreferences() = 0;
virtual void *GetGame() = 0; virtual std::shared_ptr<Game> GetGame() = 0;
virtual void *GetBase() = 0; virtual void *GetBase() = 0;
}; };