From 8a14abe007fb7b265e875c99aef7f53d5d185f39 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Sat, 31 Dec 2022 20:45:03 +0100 Subject: [PATCH] Cleaning pointers related code --- src/MainWindow.cpp | 6 +-- src/base_tab/BaseGameTab.cpp | 2 +- src/base_tab/BaseGameTab.hpp | 2 +- src/base_tab/gamebase/PGNGameBase.cpp | 6 +-- src/engine_tab/EngineTab.cpp | 4 +- src/engine_tab/EngineTab.hpp | 4 +- src/game_tab/Game.cpp | 38 +++++++++---------- src/game_tab/GameTab.hpp | 2 +- src/game_tab/HalfMove.cpp | 28 +++++++------- src/game_tab/HalfMove.hpp | 4 +- src/game_tab/left_panel/GameTabLeftPanel.cpp | 4 +- src/game_tab/left_panel/board/BoardCanvas.cpp | 4 +- .../right_panel/GameTabRightPanel.cpp | 14 +++---- src/game_tab/right_panel/LiveEngineDialog.cpp | 8 ++-- 14 files changed, 63 insertions(+), 63 deletions(-) diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 83fda6d..5305a28 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -17,7 +17,7 @@ wxDEFINE_EVENT(CLOSE_LINKED_TAB, wxCommandEvent); MainWindow::MainWindow() : MainFrame(NULL, wxID_ANY, "OChess: The Open Chess software", wxDefaultPosition, wxSize(1500, 1000)), - prefsEditor(NULL) { + prefsEditor(nullptr) { SetStatusText("OChess"); /// File menu @@ -196,7 +196,7 @@ void MainWindow::NewEngine() { } void MainWindow::OpenSettings() { - if (prefsEditor != NULL) { + if (prefsEditor != nullptr) { delete prefsEditor; } prefsEditor = new wxPreferencesEditor("Preferences"); @@ -213,7 +213,7 @@ void MainWindow::ApplyPreferences() { } void MainWindow::OnClose(wxCloseEvent &e) { - if (prefsEditor != NULL) { + if (prefsEditor != nullptr) { prefsEditor->Dismiss(); } e.Skip(); diff --git a/src/base_tab/BaseGameTab.cpp b/src/base_tab/BaseGameTab.cpp index e7e3ab6..dfa5a0a 100644 --- a/src/base_tab/BaseGameTab.cpp +++ b/src/base_tab/BaseGameTab.cpp @@ -82,7 +82,7 @@ void BaseGameTab::Reset(std::shared_ptr base){ // Load all games (for now :) SHOW_DIALOG_BUSY("Loading database..."); this->base=base; - if (base != NULL) { + if (base != nullptr) { while (base->NextGame()) { glm->AddGame( base->GetTag("White"), diff --git a/src/base_tab/BaseGameTab.hpp b/src/base_tab/BaseGameTab.hpp index c91e05d..2e8cf84 100644 --- a/src/base_tab/BaseGameTab.hpp +++ b/src/base_tab/BaseGameTab.hpp @@ -25,6 +25,6 @@ public: std::vector GetDeletedGameIds() {return(deleted);}; std::shared_ptr OpenGame(long gameid, long item); - std::shared_ptr GetGame() { return (std::shared_ptr(NULL)); } + std::shared_ptr GetGame() { return nullptr; } std::shared_ptr GetBase() { return (std::shared_ptr(base)); }; }; \ No newline at end of file diff --git a/src/base_tab/gamebase/PGNGameBase.cpp b/src/base_tab/gamebase/PGNGameBase.cpp index 5bcbfec..5d0bf57 100644 --- a/src/base_tab/gamebase/PGNGameBase.cpp +++ b/src/base_tab/gamebase/PGNGameBase.cpp @@ -75,7 +75,7 @@ std::shared_ptr PGNGameBase::GetGame(std::uint32_t id) { } curid++; } - return (std::shared_ptr(NULL)); + return nullptr; } void PGNGameBase::Save(std::vector to_delete, @@ -155,7 +155,7 @@ std::string PGNGameBase::GetPGN(std::shared_ptr g) { pgn += '[' + element + " \"" + g->GetTag(element) + "\"]\n"; } - if(m !=NULL){ + if(m !=nullptr){ pgn += GetMovesPGN(m,m->IsABlackMove()); pgn += " "; } @@ -192,7 +192,7 @@ std::string PGNGameBase::GetMovesPGN(HalfMove *m, bool needDots) { } } - if (m->GetMainline() != NULL) { + if (m->GetMainline() != nullptr) { part += " " + GetMovesPGN(m->GetMainline(), !m->IsABlackMove() && newNeedDots); } diff --git a/src/engine_tab/EngineTab.cpp b/src/engine_tab/EngineTab.cpp index 67ddc22..1433bac 100644 --- a/src/engine_tab/EngineTab.cpp +++ b/src/engine_tab/EngineTab.cpp @@ -30,7 +30,7 @@ EngineTab::EngineTab(wxWindow *parent, uciadapter::UCI *engine, } EngineTab::EngineTab(wxWindow *parent, std::string name) - : TabEngine(parent), TabInfos(TabInfos::ENGINE), engine(NULL) { + : TabEngine(parent), TabInfos(TabInfos::ENGINE), engine(nullptr) { SetLabel(name); engineName = name; confGroup = "engines/" + engineName; @@ -45,7 +45,7 @@ EngineTab::EngineTab(wxWindow *parent, std::string name) } EngineTab::~EngineTab() { - if (engine != NULL) { + if (engine != nullptr) { wxLogDebug("EngineTab destructor: destroying engine!"); engine->quit(); delete engine; diff --git a/src/engine_tab/EngineTab.hpp b/src/engine_tab/EngineTab.hpp index 8a4c488..0daa404 100644 --- a/src/engine_tab/EngineTab.hpp +++ b/src/engine_tab/EngineTab.hpp @@ -18,8 +18,8 @@ public: EngineTab(wxWindow *parent, std::string name); ~EngineTab(); void ApplyPreferences() {} - std::shared_ptr GetGame() { return (std::shared_ptr(NULL)); } - std::shared_ptr GetBase() { return (std::shared_ptr(NULL)); } + std::shared_ptr GetGame() { return nullptr; } + std::shared_ptr GetBase() { return nullptr; } void OnSave(wxCommandEvent &event); void OnDelete(wxCommandEvent &event); }; \ No newline at end of file diff --git a/src/game_tab/Game.cpp b/src/game_tab/Game.cpp index b831f0f..fdc754a 100644 --- a/src/game_tab/Game.cpp +++ b/src/game_tab/Game.cpp @@ -1,13 +1,13 @@ #include "Game.hpp" -Game::Game() : current(NULL), moves(NULL), result("*") { +Game::Game() : current(nullptr), moves(nullptr), result("*") { tags["White"] = ""; tags["Black"] = ""; initial_fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; board = "rnbqkbnrpppppppp PPPPPPPPRNBQKBNR"; } -Game::Game(std::string fen) : current(NULL), moves(NULL), result("*") { +Game::Game(std::string fen) : current(nullptr), moves(nullptr), result("*") { tags["White"] = ""; tags["Black"] = ""; tags["FEN"] = fen; @@ -30,13 +30,13 @@ Game::Game(const Game* g){ tags=g->tags; current=nullptr; moves=nullptr; - if(g->moves != NULL){ + if(g->moves != nullptr){ moves=new HalfMove(g->moves); } } Game::~Game() { - if (moves != NULL) { + if (moves != nullptr) { delete moves; } } @@ -50,7 +50,7 @@ void Game::SetTag(std::string tagname, std::string value) { } bool Game::IsBlackToPlay() { - if (current == NULL) { + if (current == nullptr) { return (false); } return (!current->IsBlack); @@ -60,13 +60,13 @@ void Game::DeleteTag(std::string tagname) { tags.erase(tagname); } void Game::DeleteMove(HalfMove *m) { if (moves == m) { - current = NULL; - moves = NULL; + current = nullptr; + moves = nullptr; delete m; } else { - if (m != NULL) { + if (m != nullptr) { current = m->GetParent(); - if (current != NULL) { + if (current != nullptr) { current->RemoveChild(m); } delete m; @@ -79,14 +79,14 @@ HalfMove *Game::GetCurrentMove() { return (current); } HalfMove *Game::GetMoves() { return (moves); } void Game::PromoteMove(HalfMove *m) { - if (m != NULL) { + if (m != nullptr) { current = m; m->Promote(); } } void Game::SetMoveAsMainline(HalfMove *m) { - if (m != NULL) { + if (m != nullptr) { current = m; m->SetAsMainline(); } @@ -103,13 +103,13 @@ bool Game::Play(std::string move) { wxLogDebug("%c", capture); m->SetCapture(capture); } - if (current != NULL) { + if (current != nullptr) { current->AddMove(m); - } else if (moves != NULL) { + } else if (moves != nullptr) { moves->AddVariation(m); } current = m; - if (moves == NULL) { + if (moves == nullptr) { moves = m; } return (true); @@ -118,7 +118,7 @@ bool Game::Play(std::string move) { } void Game::Previous() { - if (current != NULL) { + if (current != nullptr) { current = current->GetParent(); } } @@ -132,9 +132,9 @@ std::vector Game::ListTags() { } void Game::Next() { - if (current != NULL) { + if (current != nullptr) { HalfMove *m = current->GetMainline(); - if (m != NULL) { + if (m != nullptr) { current = m; } } else { @@ -145,7 +145,7 @@ void Game::Next() { void Game::SetCurrent(HalfMove *m) { current = m; } std::string Game::GetFen() { - if (current == NULL) { + if (current == nullptr) { return (initial_fen); } return (current->GetFen()); @@ -156,7 +156,7 @@ std::string Game::GetResult() { return (result); } void Game::SetResult(std::string result) { this->result = result; } void Game::BuildAndVerify() { - if (moves != NULL) { + if (moves != nullptr) { moves->BuildAndVerify(GetFen()); } } diff --git a/src/game_tab/GameTab.hpp b/src/game_tab/GameTab.hpp index f951ae7..e5cc9f9 100644 --- a/src/game_tab/GameTab.hpp +++ b/src/game_tab/GameTab.hpp @@ -27,7 +27,7 @@ public: GameTab(wxFrame *parent, std::shared_ptr game); void ApplyPreferences(); std::shared_ptr GetGame() { return (std::shared_ptr(game)); } - std::shared_ptr GetBase() { return (std::shared_ptr(NULL)); }; + std::shared_ptr GetBase() { return nullptr; }; void OnToolClick(wxCommandEvent &event); void OnLink(){board_panel->DisableSaveTool();}; }; diff --git a/src/game_tab/HalfMove.cpp b/src/game_tab/HalfMove.cpp index 735f17c..7f19480 100644 --- a/src/game_tab/HalfMove.cpp +++ b/src/game_tab/HalfMove.cpp @@ -12,7 +12,7 @@ HalfMove::HalfMove(std::string move_absolute, std::string move_san, std::string } HalfMove::~HalfMove() { - if (mainline != NULL) { + if (mainline != nullptr) { delete mainline; } for (HalfMove *m : variations) { @@ -29,7 +29,7 @@ HalfMove::HalfMove(HalfMove *m){ Number = m->Number; nag = m->nag; comment=m->comment; - if(m->mainline != NULL){ + if(m->mainline != nullptr){ SetMainline(new HalfMove(m->mainline)); } for (int i=0; i < m->variations.size(); i++) { @@ -56,17 +56,17 @@ std::map HalfMove::GetLineCaptures() { captures[c] = 1; } m = m->parent; - } while (m != NULL); + } while (m != nullptr); return (captures); } void HalfMove::SetCapture(char c) { capture = c; } void HalfMove::AddMove(HalfMove *m) { - if (this->mainline == NULL) { + if (this->mainline == nullptr) { SetMainline(m); } else { - if (mainline != NULL) { + if (mainline != nullptr) { mainline->AddVariation(m); } } @@ -82,7 +82,7 @@ void HalfMove::SetMainline(HalfMove *m) { } HalfMove::mainline = m; cgeditor::CGEHalfMove::MainLine = m; - if (m != NULL) { + if (m != nullptr) { m->SetParent(this); } } @@ -105,7 +105,7 @@ void HalfMove::RemoveChild(HalfMove *m) { HalfMove::variations.erase(HalfMove::variations.begin() + i); } if (HalfMove::mainline == m) { - HalfMove::mainline = NULL; + HalfMove::mainline = nullptr; } cgeditor::CGEHalfMove::RemoveChild((CGEHalfMove *)m); } @@ -115,7 +115,7 @@ HalfMove *HalfMove::GetParent() { return (parent); } HalfMove *HalfMove::GetRoot() { HalfMove *m = this; HalfMove *p = HalfMove::parent; - while (p != NULL) { + while (p != nullptr) { if (p->mainline != m) { return (m); } @@ -143,7 +143,7 @@ HalfMove::HalfMove(pgnp::HalfMove *m) : capture(' ') { this->IsBlack = m->isBlack; this->comment=m->comment; this->Number = m->count; - if (m->MainLine != NULL) { + if (m->MainLine != nullptr) { this->SetMainline(new HalfMove(m->MainLine)); } for (pgnp::HalfMove *v : m->variations) { @@ -155,10 +155,10 @@ void HalfMove::SetFen(std::string fen) { this->fen = fen; } void HalfMove::Promote() { HalfMove *root = GetRoot(); - if (root->parent != NULL) { + if (root->parent != nullptr) { HalfMove *p = root->parent; if (p->HalfMove::mainline != root) { - if (root->parent->HalfMove::parent != NULL) { + if (root->parent->HalfMove::parent != nullptr) { HalfMove *pp = root->parent->HalfMove::parent; if (pp->HalfMove::mainline == p) { pp->HalfMove::SetMainline(root); @@ -168,7 +168,7 @@ void HalfMove::Promote() { } } if (p->HalfMove::mainline == root) { - p->HalfMove::SetMainline(NULL); + p->HalfMove::SetMainline(nullptr); } else { p->HalfMove::RemoveChild(root); } @@ -180,7 +180,7 @@ void HalfMove::Promote() { bool HalfMove::IsVariation() { HalfMove *m = this; HalfMove *p = HalfMove::parent; - while (p != NULL) { + while (p != nullptr) { if (p->mainline != m) { return (true); } @@ -208,7 +208,7 @@ void HalfMove::BuildAndVerify(HalfMove *m, std::string fen) { m->capture = capture; } m->fen = arbiter.GetFEN(); - if (m->mainline != NULL) { + if (m->mainline != nullptr) { BuildAndVerify(m->mainline, arbiter.GetFEN()); } for (HalfMove *v : m->variations) { diff --git a/src/game_tab/HalfMove.hpp b/src/game_tab/HalfMove.hpp index da3c919..3d5177e 100644 --- a/src/game_tab/HalfMove.hpp +++ b/src/game_tab/HalfMove.hpp @@ -14,8 +14,8 @@ * */ class HalfMove : public cgeditor::CGEHalfMove { - HalfMove *parent = NULL; - HalfMove *mainline = NULL; + HalfMove *parent = nullptr; + HalfMove *mainline = nullptr; chessarbiter::ChessArbiter arbiter; std::vector variations; std::string fen; diff --git a/src/game_tab/left_panel/GameTabLeftPanel.cpp b/src/game_tab/left_panel/GameTabLeftPanel.cpp index 71d44bf..70a09cd 100644 --- a/src/game_tab/left_panel/GameTabLeftPanel.cpp +++ b/src/game_tab/left_panel/GameTabLeftPanel.cpp @@ -80,7 +80,7 @@ void GameTabLeftPanel::OnPlay(wxCommandEvent &event) { std::string fen = game->GetFen(); std::map captures; HalfMove *m = game->GetCurrentMove(); - if (m != NULL) { + if (m != nullptr) { captures = m->GetLineCaptures(); } } @@ -89,7 +89,7 @@ void GameTabLeftPanel::Notify(bool animate, bool backward) { std::string fen = game->GetFen(); std::map captures; HalfMove *m = game->GetCurrentMove(); - if (m != NULL) { + if (m != nullptr) { captures = m->GetLineCaptures(); } diff --git a/src/game_tab/left_panel/board/BoardCanvas.cpp b/src/game_tab/left_panel/board/BoardCanvas.cpp index 3e1744c..55e16f0 100644 --- a/src/game_tab/left_panel/board/BoardCanvas.cpp +++ b/src/game_tab/left_panel/board/BoardCanvas.cpp @@ -81,9 +81,9 @@ void BoardCanvas::OnPaint(wxPaintEvent &event) { } void BoardCanvas::ApplyPreferences() { - if (t != NULL) + if (t != nullptr) delete t; - if (t_captures != NULL) + if (t_captures != nullptr) delete t_captures; t = new Theme(); t_captures = new Theme(); diff --git a/src/game_tab/right_panel/GameTabRightPanel.cpp b/src/game_tab/right_panel/GameTabRightPanel.cpp index 9bdc29c..1b53c83 100644 --- a/src/game_tab/right_panel/GameTabRightPanel.cpp +++ b/src/game_tab/right_panel/GameTabRightPanel.cpp @@ -9,7 +9,7 @@ wxDEFINE_EVENT(NEXT_MOVE_EVENT, wxCommandEvent); GameTabRightPanel::GameTabRightPanel(wxFrame *parent, std::shared_ptr game) : TabGameRightPanel(parent), game(game), selected_item(-1), - live_engine(NULL) { + live_engine(nullptr) { editor_canvas = new EditorCanvas((wxFrame *)editor_page); editor_canvas_sizer->Add(editor_canvas, 1, wxEXPAND); tags_list->InsertColumn(0, L"Name", wxLIST_FORMAT_LEFT, 200); @@ -42,7 +42,7 @@ GameTabRightPanel::GameTabRightPanel(wxFrame *parent, std::shared_ptr game LIVE_ANALYSIS_GAME_BUTTON); nag_panel->Bind(wxEVT_BUTTON, [p=this](wxCommandEvent &e){ HalfMove *m = p->game->GetCurrentMove(); - if (m != NULL) { + if (m != nullptr) { m->nag=p->GetNagFromStr(((wxButton*)e.GetEventObject())->GetLabel().ToStdString()); p->editor_canvas->Refresh(); } @@ -60,7 +60,7 @@ GameTabRightPanel::GameTabRightPanel(wxFrame *parent, std::shared_ptr game } void GameTabRightPanel::OnLiveAnalysis(wxCommandEvent &event) { - if (live_engine == NULL) { + if (live_engine == nullptr) { int selection = engine_list->GetSelection(); if (selection != wxNOT_FOUND) { live_engine = new LiveEngineDialog( @@ -97,14 +97,14 @@ void GameTabRightPanel::NotifyBoard() { } void GameTabRightPanel::OnLiveEngineClose(wxCloseEvent &e) { - live_engine = NULL; + live_engine = nullptr; e.Skip(); } void GameTabRightPanel::OnCommentChange(wxCommandEvent &event) { wxLogDebug("GameTabRightPanel: comment input change"); HalfMove *m = game->GetCurrentMove(); - if (m != NULL) { + if (m != nullptr) { m->comment=event.GetString().ToStdString(); } editor_canvas->Refresh(); @@ -172,13 +172,13 @@ void GameTabRightPanel::OnMoveSetAsMainline(wxCommandEvent &event) { void GameTabRightPanel::Notify() { HalfMove *m = game->GetCurrentMove(); - if (m != NULL) { + if (m != nullptr) { comment_input->ChangeValue( m->comment); // ChangeValue do not raise events } editor_canvas->SetMoves(game->GetMoves(), m); // Put it here for now: - if (live_engine != NULL) { + if (live_engine != nullptr) { live_engine->SetFEN(game->GetFen()); } } diff --git a/src/game_tab/right_panel/LiveEngineDialog.cpp b/src/game_tab/right_panel/LiveEngineDialog.cpp index dd24096..9184216 100644 --- a/src/game_tab/right_panel/LiveEngineDialog.cpp +++ b/src/game_tab/right_panel/LiveEngineDialog.cpp @@ -2,7 +2,7 @@ LiveEngineDialog::LiveEngineDialog(wxWindow *parent, std::string engine_name) : DialogLiveEngine(parent), engine_name(engine_name), interval(1000), - engine(NULL) { + engine(nullptr) { lines_list->InsertColumn(0, "#", wxLIST_FORMAT_LEFT, 50); lines_list->InsertColumn(1, "CP", wxLIST_FORMAT_LEFT, 70); lines_list->InsertColumn(2, "Line", wxLIST_FORMAT_LEFT, 300); @@ -14,14 +14,14 @@ LiveEngineDialog::LiveEngineDialog(wxWindow *parent, std::string engine_name) } LiveEngineDialog::~LiveEngineDialog() { - if (engine != NULL) { + if (engine != nullptr) { wxLogDebug("LiveEngineDialog destructor: delete engine"); delete engine; } } void LiveEngineDialog::InitEngine() { - if (engine == NULL) { + if (engine == nullptr) { wxLogDebug("Start engine: %s", engine_name); CONFIG_OPEN(conf); engine = new uciadapter::UCI( @@ -53,7 +53,7 @@ void LiveEngineDialog::InitEngine() { } void LiveEngineDialog::OnClose(wxCloseEvent &e) { - if (engine != NULL) { + if (engine != nullptr) { wxLogDebug("Close live engine!!"); timer.Stop(); engine->stop();