mirror of
https://gitlab.com/manzerbredes/ochess.git
synced 2025-04-07 02:26:29 +02:00
Migrate to std::shared_ptr<GameBase>
This commit is contained in:
parent
44ea0a50a3
commit
bd71371bed
10 changed files with 22 additions and 26 deletions
|
@ -2,7 +2,7 @@
|
||||||
#include "MainWindow.hpp"
|
#include "MainWindow.hpp"
|
||||||
#include "ochess.hpp"
|
#include "ochess.hpp"
|
||||||
|
|
||||||
AppendGameDialog::AppendGameDialog(wxWindow *parent, GameBase *base)
|
AppendGameDialog::AppendGameDialog(wxWindow *parent, std::shared_ptr<GameBase> base)
|
||||||
: DialogAppendGame(parent), base(base) {
|
: DialogAppendGame(parent), base(base) {
|
||||||
|
|
||||||
for (TabInfos *i : MAINWIN->ListTabInfos()) {
|
for (TabInfos *i : MAINWIN->ListTabInfos()) {
|
||||||
|
@ -26,7 +26,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<std::shared_ptr<GameBase>> new_games_bases;
|
||||||
std::vector<std::shared_ptr<Game>> new_games;
|
std::vector<std::shared_ptr<Game>> new_games;
|
||||||
|
|
||||||
wxArrayInt selections;
|
wxArrayInt selections;
|
||||||
|
@ -35,7 +35,7 @@ void AppendGameDialog::OnImport(wxCommandEvent &event) {
|
||||||
for (int &i : selections) {
|
for (int &i : selections) {
|
||||||
TabInfos *tinfo = tinfos[i];
|
TabInfos *tinfo = tinfos[i];
|
||||||
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(tinfo->GetBase());
|
||||||
} else if (tinfo->type == TabInfos::GAME) {
|
} else if (tinfo->type == TabInfos::GAME) {
|
||||||
new_games.push_back(tinfo->GetGame());
|
new_games.push_back(tinfo->GetGame());
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
#include "gamebase/GameBase.hpp"
|
#include "gamebase/GameBase.hpp"
|
||||||
|
|
||||||
class AppendGameDialog : public DialogAppendGame {
|
class AppendGameDialog : public DialogAppendGame {
|
||||||
GameBase *base; // Should not be destroy
|
std::shared_ptr<GameBase> base; // Should not be destroy
|
||||||
std::vector<TabInfos *> tinfos;
|
std::vector<TabInfos *> tinfos;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
AppendGameDialog(wxWindow *parent, GameBase *base);
|
AppendGameDialog(wxWindow *parent, std::shared_ptr<GameBase> base);
|
||||||
void OnCancel(wxCommandEvent &event);
|
void OnCancel(wxCommandEvent &event);
|
||||||
void OnImport(wxCommandEvent &event);
|
void OnImport(wxCommandEvent &event);
|
||||||
void OnClose(wxCloseEvent &e);
|
void OnClose(wxCloseEvent &e);
|
||||||
|
|
|
@ -24,10 +24,6 @@ BaseTab::BaseTab(wxFrame *parent, std::string base_file)
|
||||||
LoadFile();
|
LoadFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
BaseTab::~BaseTab() {
|
|
||||||
delete base;
|
|
||||||
}
|
|
||||||
|
|
||||||
void BaseTab::OnImport(wxCommandEvent &event) {
|
void BaseTab::OnImport(wxCommandEvent &event) {
|
||||||
AppendGameDialog *dia = new AppendGameDialog(this, base);
|
AppendGameDialog *dia = new AppendGameDialog(this, base);
|
||||||
dia->ShowModal();
|
dia->ShowModal();
|
||||||
|
@ -53,7 +49,7 @@ void BaseTab::OnDelete(wxCommandEvent &event) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void BaseTab::OnSave(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;
|
std::vector<std::shared_ptr<Game>> new_games;
|
||||||
new_games.insert(
|
new_games.insert(
|
||||||
new_games.end(), edited.begin(),
|
new_games.end(), edited.begin(),
|
||||||
|
@ -67,7 +63,7 @@ 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());
|
||||||
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) {
|
if (g != NULL) {
|
||||||
edited.push_back(*g);
|
edited.push_back(*g);
|
||||||
deleted.push_back(id);
|
deleted.push_back(id);
|
||||||
|
@ -102,7 +98,7 @@ void BaseTab::LoadFile() {
|
||||||
wxFileName file(base_file);
|
wxFileName file(base_file);
|
||||||
wxString ext = file.GetExt().Lower();
|
wxString ext = file.GetExt().Lower();
|
||||||
if (ext == "pgn") {
|
if (ext == "pgn") {
|
||||||
base = new PGNGameBase(base_file);
|
base = std::shared_ptr<GameBase>(new PGNGameBase(base_file));
|
||||||
SetLabel(file.GetName() + "(PGN)");
|
SetLabel(file.GetName() + "(PGN)");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,14 +8,14 @@ wxDECLARE_EVENT(NEW_GAME_EVENT, wxCommandEvent);
|
||||||
wxDECLARE_EVENT(REFRESH_TAB_TITLE, wxCommandEvent);
|
wxDECLARE_EVENT(REFRESH_TAB_TITLE, wxCommandEvent);
|
||||||
|
|
||||||
class BaseTab : public TabBase, public TabInfos {
|
class BaseTab : public TabBase, public TabInfos {
|
||||||
GameBase *base;
|
std::shared_ptr<GameBase> base;
|
||||||
std::vector<std::uint32_t> deleted;
|
std::vector<std::uint32_t> deleted;
|
||||||
std::vector<std::shared_ptr<Game>> edited;
|
std::vector<std::shared_ptr<Game>> edited;
|
||||||
std::string base_file;
|
std::string base_file;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
BaseTab(wxFrame *parent, std::string base_file);
|
BaseTab(wxFrame *parent, std::string base_file);
|
||||||
~BaseTab();
|
|
||||||
void ApplyPreferences();
|
void ApplyPreferences();
|
||||||
void LoadFile();
|
void LoadFile();
|
||||||
void OnDelete(wxCommandEvent &event);
|
void OnDelete(wxCommandEvent &event);
|
||||||
|
@ -24,5 +24,5 @@ public:
|
||||||
void OnOpenGame(wxListEvent &event);
|
void OnOpenGame(wxListEvent &event);
|
||||||
void OnImport(wxCommandEvent &event);
|
void OnImport(wxCommandEvent &event);
|
||||||
std::shared_ptr<Game> GetGame() { return (std::shared_ptr<Game>(NULL)); }
|
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)); };
|
||||||
};
|
};
|
|
@ -8,7 +8,7 @@ class GameBase {
|
||||||
public:
|
public:
|
||||||
virtual std::shared_ptr<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<std::shared_ptr<GameBase>> new_games_bases,
|
||||||
std::vector<std::shared_ptr<Game>> new_games) = 0;
|
std::vector<std::shared_ptr<Game>> new_games) = 0;
|
||||||
virtual std::shared_ptr<Game> GetCurrentGame() = 0;
|
virtual std::shared_ptr<Game> GetCurrentGame() = 0;
|
||||||
virtual bool NextGame() = 0;
|
virtual bool NextGame() = 0;
|
||||||
|
@ -19,5 +19,5 @@ public:
|
||||||
*
|
*
|
||||||
* @param base
|
* @param base
|
||||||
*/
|
*/
|
||||||
virtual void Export(GameBase *base) = 0;
|
virtual void Export(std::shared_ptr<GameBase> base) = 0;
|
||||||
};
|
};
|
|
@ -64,7 +64,7 @@ std::shared_ptr<Game> PGNGameBase::GetGame(std::uint32_t id) {
|
||||||
}
|
}
|
||||||
|
|
||||||
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<std::shared_ptr<GameBase>> new_games_bases,
|
||||||
std::vector<std::shared_ptr<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";
|
||||||
|
@ -87,7 +87,7 @@ void PGNGameBase::Save(std::vector<std::uint32_t> to_ignore,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now add new games
|
// Now add new games
|
||||||
for (GameBase *current : new_games_bases) {
|
for (std::shared_ptr<GameBase> current : new_games_bases) {
|
||||||
current->Reset();
|
current->Reset();
|
||||||
while (current->NextGame()) {
|
while (current->NextGame()) {
|
||||||
if (several) {
|
if (several) {
|
||||||
|
@ -114,7 +114,7 @@ void PGNGameBase::Save(std::vector<std::uint32_t> to_ignore,
|
||||||
wxRemoveFile(tmp);
|
wxRemoveFile(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PGNGameBase::Export(GameBase *base) {
|
void PGNGameBase::Export(std::shared_ptr<GameBase> base) {
|
||||||
wxFile new_pgn(file, wxFile::write);
|
wxFile new_pgn(file, wxFile::write);
|
||||||
|
|
||||||
base->Reset();
|
base->Reset();
|
||||||
|
|
|
@ -14,8 +14,8 @@ public:
|
||||||
std::shared_ptr<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<std::shared_ptr<GameBase>> new_games_bases,
|
||||||
std::vector<std::shared_ptr<Game>> new_games);
|
std::vector<std::shared_ptr<Game>> new_games);
|
||||||
void Reset();
|
void Reset();
|
||||||
void Export(GameBase *base);
|
void Export(std::shared_ptr<GameBase> base);
|
||||||
};
|
};
|
|
@ -19,7 +19,7 @@ public:
|
||||||
~EngineTab();
|
~EngineTab();
|
||||||
void ApplyPreferences() {}
|
void ApplyPreferences() {}
|
||||||
std::shared_ptr<Game> GetGame() { return (std::shared_ptr<Game>(NULL)); }
|
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 OnSave(wxCommandEvent &event);
|
||||||
void OnDelete(wxCommandEvent &event);
|
void OnDelete(wxCommandEvent &event);
|
||||||
};
|
};
|
|
@ -26,5 +26,5 @@ public:
|
||||||
GameTab(wxFrame *parent, std::shared_ptr<Game> game);
|
GameTab(wxFrame *parent, std::shared_ptr<Game> game);
|
||||||
void ApplyPreferences();
|
void ApplyPreferences();
|
||||||
std::shared_ptr<Game> GetGame() { return (std::shared_ptr<Game>(game)); }
|
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)); };
|
||||||
};
|
};
|
||||||
|
|
|
@ -47,7 +47,7 @@ wxDECLARE_APP(MyApp);
|
||||||
void Abort(std::string msg);
|
void Abort(std::string msg);
|
||||||
|
|
||||||
class Game;
|
class Game;
|
||||||
|
class GameBase;
|
||||||
/**
|
/**
|
||||||
* @brief Attach informations to the application tabs
|
* @brief Attach informations to the application tabs
|
||||||
*
|
*
|
||||||
|
@ -59,5 +59,5 @@ public:
|
||||||
TabInfos(Type type_) : type(type_) {}
|
TabInfos(Type type_) : type(type_) {}
|
||||||
virtual void ApplyPreferences() = 0;
|
virtual void ApplyPreferences() = 0;
|
||||||
virtual std::shared_ptr<Game> GetGame() = 0;
|
virtual std::shared_ptr<Game> GetGame() = 0;
|
||||||
virtual void *GetBase() = 0;
|
virtual std::shared_ptr<GameBase> GetBase() = 0;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue