Migrate to std::shared_ptr<GameBase>

This commit is contained in:
Loic Guegan 2022-02-28 20:30:57 +01:00
parent 44ea0a50a3
commit bd71371bed
10 changed files with 22 additions and 26 deletions

View file

@ -2,7 +2,7 @@
#include "MainWindow.hpp"
#include "ochess.hpp"
AppendGameDialog::AppendGameDialog(wxWindow *parent, GameBase *base)
AppendGameDialog::AppendGameDialog(wxWindow *parent, std::shared_ptr<GameBase> base)
: DialogAppendGame(parent), base(base) {
for (TabInfos *i : MAINWIN->ListTabInfos()) {
@ -26,7 +26,7 @@ void AppendGameDialog::OnCancel(wxCommandEvent &event) { this->Close(); }
void AppendGameDialog::OnImport(wxCommandEvent &event) {
std::vector<std::uint32_t> to_ignore;
std::vector<GameBase *> new_games_bases;
std::vector<std::shared_ptr<GameBase>> new_games_bases;
std::vector<std::shared_ptr<Game>> new_games;
wxArrayInt selections;
@ -35,7 +35,7 @@ void AppendGameDialog::OnImport(wxCommandEvent &event) {
for (int &i : selections) {
TabInfos *tinfo = tinfos[i];
if (tinfo->type == TabInfos::BASE) {
new_games_bases.push_back(static_cast<GameBase *>(tinfo->GetBase()));
new_games_bases.push_back(tinfo->GetBase());
} else if (tinfo->type == TabInfos::GAME) {
new_games.push_back(tinfo->GetGame());
}

View file

@ -1,11 +1,11 @@
#include "gamebase/GameBase.hpp"
class AppendGameDialog : public DialogAppendGame {
GameBase *base; // Should not be destroy
std::shared_ptr<GameBase> base; // Should not be destroy
std::vector<TabInfos *> tinfos;
public:
AppendGameDialog(wxWindow *parent, GameBase *base);
AppendGameDialog(wxWindow *parent, std::shared_ptr<GameBase> base);
void OnCancel(wxCommandEvent &event);
void OnImport(wxCommandEvent &event);
void OnClose(wxCloseEvent &e);

View file

@ -24,10 +24,6 @@ BaseTab::BaseTab(wxFrame *parent, std::string base_file)
LoadFile();
}
BaseTab::~BaseTab() {
delete base;
}
void BaseTab::OnImport(wxCommandEvent &event) {
AppendGameDialog *dia = new AppendGameDialog(this, base);
dia->ShowModal();
@ -53,7 +49,7 @@ void BaseTab::OnDelete(wxCommandEvent &event) {
}
void BaseTab::OnSave(wxCommandEvent &event) {
std::vector<GameBase *> new_games_bases;
std::vector<std::shared_ptr<GameBase>> new_games_bases;
std::vector<std::shared_ptr<Game>> new_games;
new_games.insert(
new_games.end(), edited.begin(),
@ -67,7 +63,7 @@ void BaseTab::OnSave(wxCommandEvent &event) {
void BaseTab::OnOpenGame(wxListEvent &event) {
wxLogDebug("Open!");
long id = std::stoi(event.GetItem().GetText().ToStdString());
std::shared_ptr<Game> *g =new std::shared_ptr<Game>(base->GetGame(id));
std::shared_ptr<Game> *g = new std::shared_ptr<Game>(base->GetGame(id));
if (g != NULL) {
edited.push_back(*g);
deleted.push_back(id);
@ -102,7 +98,7 @@ void BaseTab::LoadFile() {
wxFileName file(base_file);
wxString ext = file.GetExt().Lower();
if (ext == "pgn") {
base = new PGNGameBase(base_file);
base = std::shared_ptr<GameBase>(new PGNGameBase(base_file));
SetLabel(file.GetName() + "(PGN)");
}

View file

@ -8,14 +8,14 @@ wxDECLARE_EVENT(NEW_GAME_EVENT, wxCommandEvent);
wxDECLARE_EVENT(REFRESH_TAB_TITLE, wxCommandEvent);
class BaseTab : public TabBase, public TabInfos {
GameBase *base;
std::shared_ptr<GameBase> base;
std::vector<std::uint32_t> deleted;
std::vector<std::shared_ptr<Game>> edited;
std::string base_file;
public:
BaseTab(wxFrame *parent, std::string base_file);
~BaseTab();
void ApplyPreferences();
void LoadFile();
void OnDelete(wxCommandEvent &event);
@ -24,5 +24,5 @@ public:
void OnOpenGame(wxListEvent &event);
void OnImport(wxCommandEvent &event);
std::shared_ptr<Game> GetGame() { return (std::shared_ptr<Game>(NULL)); }
void *GetBase() { return (base); };
std::shared_ptr<GameBase> GetBase() { return (std::shared_ptr<GameBase>(base)); };
};

View file

@ -8,7 +8,7 @@ class GameBase {
public:
virtual std::shared_ptr<Game> GetGame(std::uint32_t id) = 0;
virtual void Save(std::vector<std::uint32_t> to_ignore,
std::vector<GameBase *> new_games_bases,
std::vector<std::shared_ptr<GameBase>> new_games_bases,
std::vector<std::shared_ptr<Game>> new_games) = 0;
virtual std::shared_ptr<Game> GetCurrentGame() = 0;
virtual bool NextGame() = 0;
@ -19,5 +19,5 @@ public:
*
* @param base
*/
virtual void Export(GameBase *base) = 0;
virtual void Export(std::shared_ptr<GameBase> base) = 0;
};

View file

@ -64,7 +64,7 @@ std::shared_ptr<Game> PGNGameBase::GetGame(std::uint32_t id) {
}
void PGNGameBase::Save(std::vector<std::uint32_t> to_ignore,
std::vector<GameBase *> new_games_bases,
std::vector<std::shared_ptr<GameBase>> new_games_bases,
std::vector<std::shared_ptr<Game>> new_games) {
wxStandardPaths stdPaths = wxStandardPaths::Get();
wxString tmp = stdPaths.GetTempDir() + "/save_pgn_tmp.pgn";
@ -87,7 +87,7 @@ void PGNGameBase::Save(std::vector<std::uint32_t> to_ignore,
}
// Now add new games
for (GameBase *current : new_games_bases) {
for (std::shared_ptr<GameBase> current : new_games_bases) {
current->Reset();
while (current->NextGame()) {
if (several) {
@ -114,7 +114,7 @@ void PGNGameBase::Save(std::vector<std::uint32_t> to_ignore,
wxRemoveFile(tmp);
}
void PGNGameBase::Export(GameBase *base) {
void PGNGameBase::Export(std::shared_ptr<GameBase> base) {
wxFile new_pgn(file, wxFile::write);
base->Reset();

View file

@ -14,8 +14,8 @@ public:
std::shared_ptr<Game> GetCurrentGame();
std::string GetTag(std::string tag);
void Save(std::vector<std::uint32_t> to_ignore,
std::vector<GameBase *> new_games_bases,
std::vector<std::shared_ptr<GameBase>> new_games_bases,
std::vector<std::shared_ptr<Game>> new_games);
void Reset();
void Export(GameBase *base);
void Export(std::shared_ptr<GameBase> base);
};

View file

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

View file

@ -26,5 +26,5 @@ public:
GameTab(wxFrame *parent, std::shared_ptr<Game> game);
void ApplyPreferences();
std::shared_ptr<Game> GetGame() { return (std::shared_ptr<Game>(game)); }
void *GetBase() { return (NULL); };
std::shared_ptr<GameBase> GetBase() { return (std::shared_ptr<GameBase>(NULL)); };
};

View file

@ -47,7 +47,7 @@ wxDECLARE_APP(MyApp);
void Abort(std::string msg);
class Game;
class GameBase;
/**
* @brief Attach informations to the application tabs
*
@ -59,5 +59,5 @@ public:
TabInfos(Type type_) : type(type_) {}
virtual void ApplyPreferences() = 0;
virtual std::shared_ptr<Game> GetGame() = 0;
virtual void *GetBase() = 0;
virtual std::shared_ptr<GameBase> GetBase() = 0;
};