From 57d3b99ecd87503883dd041be06dd12c6bea5aa6 Mon Sep 17 00:00:00 2001 From: Ilari Liusvaara Date: Sun, 18 May 2014 03:28:48 +0300 Subject: [PATCH] Introduce run_show_error() and use it to clean up some code --- include/platform/wxwidgets/platform.hpp | 2 + src/platform/wxwidgets/branchesmenu.cpp | 61 +++++----------------- src/platform/wxwidgets/editor-movie.cpp | 75 +++++++--------------------- src/platform/wxwidgets/main.cpp | 16 ++++++ src/platform/wxwidgets/settings-advanced.cpp | 13 ++--- src/platform/wxwidgets/tracelogger.cpp | 29 ++++------- 6 files changed, 60 insertions(+), 136 deletions(-) diff --git a/include/platform/wxwidgets/platform.hpp b/include/platform/wxwidgets/platform.hpp index 57655e03..f257d90e 100644 --- a/include/platform/wxwidgets/platform.hpp +++ b/include/platform/wxwidgets/platform.hpp @@ -103,6 +103,8 @@ std::string pick_text(wxWindow* parent, const std::string& title, const std::str //Show message box with OK button. void show_message_ok(wxWindow* parent, const std::string& title, const std::string& text, int icon); +//Run function and show errors. Returns true on error. +bool run_show_error(wxWindow* parent, const std::string& title, const std::string& text, std::function fn); //Some important windows (if open). extern wxwin_messages* msg_window; diff --git a/src/platform/wxwidgets/branchesmenu.cpp b/src/platform/wxwidgets/branchesmenu.cpp index e9bb206c..7f20420b 100644 --- a/src/platform/wxwidgets/branchesmenu.cpp +++ b/src/platform/wxwidgets/branchesmenu.cpp @@ -235,16 +235,10 @@ namespace return; } lsnes_instance.run([this, id, newname]() { - try { + run_show_error(this, "Error creating branch", "Can't create branch", [id, newname]() { auto p = project_get(); if(p) p->create_branch(id, newname); - } catch(std::exception& e) { - std::string err = e.what(); - runuifun([this, err]() { - show_message_ok(this, "Error creating branch", - "Can't create branch: " + err, wxICON_EXCLAMATION); - }); - } + }); }); branches->call_project_flush(); } @@ -253,16 +247,10 @@ namespace uint64_t id = get_selected_id(); if(id == 0xFFFFFFFFFFFFFFFFULL) return; lsnes_instance.run([this, id]() { - try { + run_show_error(this, "Error setting branch", "Can't set branch", [id]() { auto p = project_get(); if(p) p->set_current_branch(id); - } catch(std::exception& e) { - std::string err = e.what(); - runuifun([this, err]() { - show_message_ok(this, "Error setting branch", - "Can't set branch: " + err, wxICON_EXCLAMATION); - }); - } + }); }); branches->call_project_flush(); update_movie_state(); @@ -279,16 +267,10 @@ namespace return; } lsnes_instance.run([this, id, newname]() { - try { + run_show_error(this, "Error renaming branch", "Can't rename branch", [id, newname]() { auto p = project_get(); if(p) p->set_branch_name(id, newname); - } catch(std::exception& e) { - std::string err = e.what(); - runuifun([this, err]() { - show_message_ok(this, "Error renaming branch", - "Can't rename branch: " + err, wxICON_EXCLAMATION); - }); - } + }); }); branches->call_project_flush(); update_movie_state(); @@ -308,16 +290,11 @@ namespace if(pid == 0xFFFFFFFFFFFFFFFFULL) return; bsel->Destroy(); lsnes_instance.run([this, id, pid]() { - try { + run_show_error(this, "Error reparenting branch", "Can't reparent branch", + [id, pid]() { auto p = project_get(); if(p) p->set_parent_branch(id, pid); - } catch(std::exception& e) { - std::string err = e.what(); - runuifun([this, err]() { - show_message_ok(this, "Error reparenting branch", - "Can't reparent branch: " + err, wxICON_EXCLAMATION); - }); - } + }); }); branches->call_project_flush(); update_movie_state(); @@ -327,16 +304,10 @@ namespace uint64_t id = get_selected_id(); if(id == 0xFFFFFFFFFFFFFFFFULL) return; lsnes_instance.run([this, id]() { - try { + run_show_error(this, "Error deleting branch", "Can't delete branch", [id]() { auto p = project_get(); if(p) p->delete_branch(id); - } catch(std::exception& e) { - std::string err = e.what(); - runuifun([this, err]() { - show_message_ok(this, "Error deleting branch", - "Can't delete branch: " + err, wxICON_EXCLAMATION); - }); - } + }); }); branches->call_project_flush(); } @@ -435,15 +406,9 @@ void branches_menu::on_select(wxCommandEvent& e) std::string err; lsnes_instance.run_async([this, bid]() { auto p = project_get(); - try { + run_show_error(this->pwin, "Error changing branch", "Can't change branch", [p, bid]() { if(p) p->set_current_branch(bid); - } catch(std::exception& e) { - std::string err = e.what(); - runuifun([this, err]() { - show_message_ok(this->pwin, "Error changing branch", "Can't change branch: " + - err, wxICON_EXCLAMATION); - }); - } + }); if(p) p->flush(); update_movie_state(); }); diff --git a/src/platform/wxwidgets/editor-movie.cpp b/src/platform/wxwidgets/editor-movie.cpp index f3c4e722..25eac371 100644 --- a/src/platform/wxwidgets/editor-movie.cpp +++ b/src/platform/wxwidgets/editor-movie.cpp @@ -1657,15 +1657,10 @@ void wxeditor_movie::_moviepanel::on_popup_menu(wxCommandEvent& e) newname = pick_text(this, "Enter new branch name", "Enter name for a new branch (to fork " "from " + lsnes_instance.mbranch.name(oldname) + "):", "", false); lsnes_instance.run_async([this, oldname, newname] { - try { + run_show_error(this, "Error creating branch", "Can't create branch", + [newname, oldname]() { lsnes_instance.mbranch._new(newname, oldname); - } catch(std::exception& e) { - std::string error = e.what(); - runuifun([this, error]() { - show_message_ok(this, "Can't create branch", - "Can't create branch: " + error, wxICON_EXCLAMATION); - }); - } + }); }); } catch(canceled_exception& e) { } @@ -1684,17 +1679,10 @@ void wxeditor_movie::_moviepanel::on_popup_menu(wxCommandEvent& e) std::set brlist; bool failed = false; lsnes_instance.run([this, filename, &brlist, &failed]() { - try { + failed = run_show_error(this, "Can't get branches in movie", "", + [filename, &brlist]() { brlist = lsnes_instance.mbranch._movie_branches(filename); - } catch(std::exception& e) { - std::string error = e.what(); - failed = true; - runuifun([this, error]() { - show_message_ok(this, "Can't get branches in movie", - error, wxICON_EXCLAMATION); - - }); - } + }); }); if(failed) return; @@ -1714,15 +1702,9 @@ void wxeditor_movie::_moviepanel::on_popup_menu(wxCommandEvent& e) dbranch = pick_text(this, "Enter new branch name", "Enter name for an imported branch:", branch, false); lsnes_instance.run_async([this, filename, branch, dbranch, mode]() { - try { + run_show_error(this, "Can't import branch", "", [filename, branch, dbranch, mode]() { lsnes_instance.mbranch.import(filename, branch, dbranch, mode); - } catch(std::exception& e) { - std::string error = e.what(); - runuifun([this, error]() { - show_message_ok(this, "Can't import branch", - error, wxICON_EXCLAMATION); - }); - } + }); }); } catch(canceled_exception& e) { } @@ -1736,16 +1718,10 @@ void wxeditor_movie::_moviepanel::on_popup_menu(wxCommandEvent& e) file = g.first; mode = g.second; lsnes_instance.run_async([this, file, mode]() { - try { + run_show_error(this, "Can't export branch", "", [file, mode]() { std::string bname = lsnes_instance.mbranch.get(); lsnes_instance.mbranch._export(file, bname, mode == MBRANCH_IMPORT_BINARY); - } catch(std::exception& e) { - std::string error = e.what(); - runuifun([this, error]() { - show_message_ok(this, "Can't export branch", - error, wxICON_EXCLAMATION); - }); - } + }); }); } catch(canceled_exception& e) { } @@ -1762,15 +1738,10 @@ void wxeditor_movie::_moviepanel::on_popup_menu(wxCommandEvent& e) newname = pick_text(this, "Enter new branch name", "Enter name for a new branch (to rename " "'" + lsnes_instance.mbranch.name(oldname) + "'):", oldname, false); lsnes_instance.run_async([this, oldname, newname] { - try { + run_show_error(this, "Error renaming branch", "Can't rename branch", + [oldname, newname]() { lsnes_instance.mbranch.rename(oldname, newname); - } catch(std::exception& e) { - std::string error = e.what(); - runuifun([this, error]() { - show_message_ok(this, "Can't rename branch", - "Can't rename branch: " + error, wxICON_EXCLAMATION); - }); - } + }); }); } catch(canceled_exception& e) { } @@ -1784,15 +1755,9 @@ void wxeditor_movie::_moviepanel::on_popup_menu(wxCommandEvent& e) oldname = pick_among(this, "Select branch to delete", "Select branch to delete", choices, 0); lsnes_instance.run_async([this, oldname] { - try { + run_show_error(this, "Error deleting branch", "Can't delete branch", [oldname]() { lsnes_instance.mbranch._delete(oldname); - } catch(std::exception& e) { - std::string error = e.what(); - runuifun([this, error]() { - show_message_ok(this, "Can't delete branch", - "Can't delete branch: " + error, wxICON_EXCLAMATION); - }); - } + }); }); } catch(canceled_exception& e) { } @@ -1802,15 +1767,9 @@ void wxeditor_movie::_moviepanel::on_popup_menu(wxCommandEvent& e) if(!branch_names.count(id)) return; std::string name = branch_names[id]; lsnes_instance.run_async([this, name]() { - try { + run_show_error(this, "Error changing branch", "Can't change branch", [name]() { lsnes_instance.mbranch.set(name); - } catch(std::exception& e) { - std::string err = e.what(); - runuifun([this, err]() { - show_message_ok(this, "Error changing branch", - "Can't change branch: " + err, wxICON_EXCLAMATION); - }); - } + }); }); } } diff --git a/src/platform/wxwidgets/main.cpp b/src/platform/wxwidgets/main.cpp index bd95e6ab..1955f88c 100644 --- a/src/platform/wxwidgets/main.cpp +++ b/src/platform/wxwidgets/main.cpp @@ -739,3 +739,19 @@ void show_message_ok(wxWindow* parent, const std::string& title, const std::stri d3->ShowModal(); d3->Destroy(); } + +bool run_show_error(wxWindow* parent, const std::string& title, const std::string& text, std::function fn) +{ + try { + fn(); + return false; + } catch(std::exception& e) { + std::string err = e.what(); + std::string _title = title; + std::string _text = (text == "") ? err : (text + ": " + err); + runuifun([parent, _title, _text]() { + show_message_ok(parent, _title, _text, wxICON_EXCLAMATION); + }); + return true; + } +} diff --git a/src/platform/wxwidgets/settings-advanced.cpp b/src/platform/wxwidgets/settings-advanced.cpp index 900791c6..45371f96 100644 --- a/src/platform/wxwidgets/settings-advanced.cpp +++ b/src/platform/wxwidgets/settings-advanced.cpp @@ -284,18 +284,11 @@ namespace } catch(...) { return; } - bool error = false; - std::string errorstr; - lsnes_instance.run([&error, &errorstr, name, value]() { - try { + lsnes_instance.run([this, name, value]() { + run_show_error(this, "Error setting value", "", [name, value]() { lsnes_instance.setcache.set(name, value); - } catch(std::exception& e) { - error = true; - errorstr = e.what(); - } + }); }); - if(error) - wxMessageBox(towxstring(errorstr), wxT("Error setting value"), wxICON_EXCLAMATION | wxOK); } void wxeditor_esettings_advanced::on_selchange(wxCommandEvent& e) diff --git a/src/platform/wxwidgets/tracelogger.cpp b/src/platform/wxwidgets/tracelogger.cpp index 45023a23..19d42f28 100644 --- a/src/platform/wxwidgets/tracelogger.cpp +++ b/src/platform/wxwidgets/tracelogger.cpp @@ -1342,16 +1342,10 @@ back: return; } } - try { - addr = hex::from(offset); - } catch(std::exception& e) { - runuifun([this] { - show_message_ok(this, "Error in address", - "Expected or +", - wxICON_EXCLAMATION); - }); + if(run_show_error(this, "Error in address", "Expected or " + " +", [&addr, offset]() { + addr = hex::from(offset); })) return; - } addr += base; runuifun([this, addr]() { uint64_t nrow = 0; @@ -1651,29 +1645,24 @@ back: { std::map rowdata; if(regex_match("\\$data:.*", disasm)) { - try { + if(run_show_error(this, "Error in disassember", "Error in disassember", + [disasm, &rowdata, &addrbase, count]() { for(uint64_t i = 0; i < count; i++) { uint64_t base = addrbase; disasm_row r = disassemble_data_item(addrbase, disasm); rowdata[base] = r; } - } catch(std::exception& e) { - std::string err = e.what(); - runuifun([this, err]() { show_message_ok(this, "Error in disassembler", - "Error in disassember: " + err, wxICON_EXCLAMATION); }); + })) return; - } add_rows_main(rowdata); return; } disassembler* d; - try { + if(run_show_error(this, "Error in disassember", "No disassembler '" + disasm + "' found", + [&d, disasm]() { d = &disassembler::byname(disasm); - } catch(std::exception& e) { - runuifun([this, disasm]() { show_message_ok(this, "Error in disassembler", - "No disassembler '" + disasm + "' found", wxICON_EXCLAMATION); }); + })) return; - } for(uint64_t i = 0; i < count; i++) { uint64_t base = addrbase; disasm_row r; -- 2.11.4.GIT