From: Mikko Rasa Date: Wed, 21 Nov 2012 15:30:10 +0000 (+0200) Subject: Rework exceptions and use maputils X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=c1faa54a3218b53757b8b55de0ff8aa64412253b;p=libs%2Fgltk.git Rework exceptions and use maputils --- diff --git a/source/connector.cpp b/source/connector.cpp index 2b64d00..9915ecc 100644 --- a/source/connector.cpp +++ b/source/connector.cpp @@ -1,3 +1,4 @@ +#include #include "connector.h" #include "logic.h" @@ -18,11 +19,8 @@ void Connector::connect(const Logic &logic) for(list::const_iterator i=logic_binds.begin(); i!=logic_binds.end(); ++i) { - map::const_iterator j = actions.find(i->type); - if(j!=actions.end()) - j->second->connect(*this, *i->wdg, i->data); - else - throw KeyError("Unknown binding type", i->type); + ConnAction *action = get_item(actions, i->type); + action->connect(*this, *i->wdg, i->data); } } diff --git a/source/container.cpp b/source/container.cpp index 483608a..bec2066 100644 --- a/source/container.cpp +++ b/source/container.cpp @@ -5,6 +5,11 @@ using namespace std; namespace Msp { namespace GLtk { +hierarchy_error::hierarchy_error(const string &w): + logic_error(w) +{ } + + Container::Container(): click_focus(0), click_button(0) @@ -35,7 +40,7 @@ void Container::remove(Widget &wdg) return; } - throw InvalidState("That Widget is not in this Container"); + throw hierarchy_error("widget not in container"); } Container::Child *Container::create_child(Widget *wdg) diff --git a/source/container.h b/source/container.h index 6688465..d1120cd 100644 --- a/source/container.h +++ b/source/container.h @@ -2,12 +2,21 @@ #define MSP_GLTK_CONTAINER_H_ #include +#include #include #include "widget.h" namespace Msp { namespace GLtk { +class hierarchy_error: public std::logic_error +{ +public: + hierarchy_error(const std::string &); + virtual ~hierarchy_error() throw() { } +}; + + class Container: virtual public Widget { protected: diff --git a/source/layout.cpp b/source/layout.cpp index 6ed46e4..ed7b936 100644 --- a/source/layout.cpp +++ b/source/layout.cpp @@ -94,7 +94,7 @@ Layout::~Layout() void Layout::set_container(Container &c) { if(container) - throw InvalidState("This layout is already assigned to a Container"); + throw logic_error("container!=0"); container = &c; } @@ -109,7 +109,7 @@ void Layout::set_margin(const Sides &m) void Layout::add_widget(Widget &wdg) { if(!container) - throw InvalidState("Can't add Widgets without a Container"); + throw logic_error("!container"); Slot *slot = create_slot(wdg); for(list::iterator i=slot->constraints.begin(); i!=slot->constraints.end(); ++i) @@ -160,7 +160,7 @@ Layout::Slot &Layout::get_slot_for_widget(Widget &wdg) if(&(*i)->widget==&wdg) return **i; - throw InvalidParameterValue("Widget is not in the Layout"); + throw hierarchy_error("widget not in layout"); } Layout::ConstraintType Layout::complement(ConstraintType type) @@ -180,7 +180,7 @@ Layout::ConstraintType Layout::complement(ConstraintType type) void Layout::add_constraint(Widget &src, ConstraintType type, Widget &tgt) { if(&src==&tgt) - throw InvalidParameterValue("Can't add a self-referencing constraint"); + throw invalid_argument("&src==&tgt"); Slot &src_slot = get_slot_for_widget(src); Slot &tgt_slot = get_slot_for_widget(tgt); @@ -337,7 +337,7 @@ Layout::LinearProgram::Row Layout::LinearProgram::add_row() Layout::LinearProgram::Row Layout::LinearProgram::operator[](unsigned r) { if(r>=n_rows) - throw InvalidParameterValue("Row index out of range"); + throw out_of_range("LinearProgram::operator[]"); return Row(*this, r); } @@ -350,9 +350,9 @@ Layout::LinearProgram::Row Layout::LinearProgram::get_object_row() float Layout::LinearProgram::get_variable(unsigned i) { if(!solved || infeasible) - throw InvalidState("Not solved"); + throw logic_error("not solved"); if(i+1>=n_columns) - throw InvalidParameterValue("Variable index out of range"); + throw out_of_range("LinearProgram::get_variable"); unsigned r = columns[i].basic; return columns.back().values[r]; @@ -478,7 +478,7 @@ Layout::LinearProgram::Row::Row(LinearProgram &lp, unsigned i): float &Layout::LinearProgram::Row::operator[](unsigned c) { if(c>=linprog.n_columns) - throw InvalidParameterValue("Column index out of range"); + throw out_of_range("Row::operator[]"); Column &column = linprog.columns[c]; if(column.values.size()<=index) diff --git a/source/list.cpp b/source/list.cpp index d81c486..a180369 100644 --- a/source/list.cpp +++ b/source/list.cpp @@ -90,7 +90,7 @@ void List::append(const string &v) void List::insert(unsigned i, const string &v) { if(i>items.size()) - throw InvalidParameterValue("Index out of range"); + throw out_of_range("List::insert"); items.insert(items.begin()+i, v); check_view_range(); @@ -100,7 +100,7 @@ void List::insert(unsigned i, const string &v) void List::remove(unsigned i) { if(i>items.size()) - throw InvalidParameterValue("Index out of range"); + throw out_of_range("List::remove"); items.erase(items.begin()+i); if(sel_index>static_cast(i)) @@ -131,13 +131,13 @@ void List::set_selected_index(int i) signal_item_selected.emit(sel_index, items[sel_index]); } else - throw InvalidParameterValue("Index out of range"); + throw out_of_range("List::set_selected_index"); } const string &List::get_selected() const { if(sel_index<0) - throw InvalidState("No selection"); + throw logic_error("sel_index<0"); return items[sel_index]; } diff --git a/source/logic.cpp b/source/logic.cpp index 3ed745c..db382fe 100644 --- a/source/logic.cpp +++ b/source/logic.cpp @@ -1,3 +1,4 @@ +#include #include "logic.h" using namespace std; @@ -14,13 +15,10 @@ Logic::Loader::Loader(Logic &l, const map &w): void Logic::Loader::bind(const string &wdg, const string &data) { - map::const_iterator i = widgets.find(wdg); - if(i==widgets.end()) - throw KeyError("Unknown widget", wdg); + WidgetBinding act; + act.wdg = get_item(widgets, wdg); string::size_type colon = data.find(':'); - WidgetBinding act; - act.wdg = i->second; act.type = data.substr(0, colon); if(colon!=string::npos) act.data = data.substr(colon+1); diff --git a/source/logic.h b/source/logic.h index e6c5243..cd10578 100644 --- a/source/logic.h +++ b/source/logic.h @@ -5,7 +5,6 @@ #include #include #include -#include #include namespace Msp { diff --git a/source/panel.cpp b/source/panel.cpp index b44004f..4b44370 100644 --- a/source/panel.cpp +++ b/source/panel.cpp @@ -53,7 +53,7 @@ void Panel::raise(Widget &wdg) return; } - throw InvalidState("That Widget is not in this Panel"); + throw hierarchy_error("widget not in panel"); } Widget *Panel::get_final_input_focus() const diff --git a/source/part.cpp b/source/part.cpp index 7eb19c7..0ba03a3 100644 --- a/source/part.cpp +++ b/source/part.cpp @@ -18,7 +18,7 @@ Part::Part(const string &n): const Graphic *Part::get_graphic(State state) const { if(state>N_STATES_) - throw InvalidParameterValue("Invalid state"); + throw invalid_argument("Part::get_graphic"); return graphic[state]; } diff --git a/source/resources.cpp b/source/resources.cpp index 11fc1fa..869885f 100644 --- a/source/resources.cpp +++ b/source/resources.cpp @@ -1,4 +1,3 @@ -#include #include #include "resources.h" @@ -42,7 +41,7 @@ void Resources::set_path(const FS::Path &p) const GL::Font &Resources::get_default_font() const { if(!default_font) - throw InvalidState("No default font"); + throw logic_error("!default_font"); return *default_font; } diff --git a/source/table.cpp b/source/table.cpp index 4a74e17..936be5c 100644 --- a/source/table.cpp +++ b/source/table.cpp @@ -1,3 +1,4 @@ +#include #include #include #include "part.h" @@ -39,14 +40,14 @@ void Table::set_columns(unsigned c) void Table::set_column_width(unsigned c, unsigned w) { if(c>=columns) - throw InvalidParameterValue("Column index out of bounds"); + throw invalid_argument("Table::set_column_width"); col_w[c] = w; } void Table::set_cell_text(unsigned r, unsigned c, const string &t) { if(r>=rows || c>=columns) - throw InvalidParameterValue("Cell coordinates out of bounds"); + throw out_of_range("Table::set_cell_text"); data[r*columns+c] = t; } diff --git a/source/text.cpp b/source/text.cpp index b0d57f7..c6fe129 100644 --- a/source/text.cpp +++ b/source/text.cpp @@ -108,7 +108,7 @@ void Text::insert(unsigned pos, const string &s) unsigned Text::get_line_length(unsigned i) const { if(i>=lines.size()) - throw InvalidParameterValue("Invalid line number"); + throw out_of_range("Text::get_line_length"); return lines[i].length; } diff --git a/source/userinterface.h b/source/userinterface.h index 58eb518..04a9ccf 100644 --- a/source/userinterface.h +++ b/source/userinterface.h @@ -1,6 +1,7 @@ #ifndef MSP_GLTK_USERINTERFACE_H_ #define MSP_GLTK_USERINTERFACE_H_ +#include #include "logic.h" #include "root.h" @@ -41,15 +42,7 @@ public: template W &get_widget(const std::string &n) const { - WidgetMap::const_iterator i = widgets.find(n); - if(i==widgets.end()) - throw KeyError("Unknown widget", n); - - W *w = dynamic_cast(i->second); - if(!w) - throw Exception("Widget type mismatch"); - - return *w; + return dynamic_cast(*get_item(widgets, n)); } template diff --git a/source/widget.cpp b/source/widget.cpp index 3f11af6..1cb42e5 100644 --- a/source/widget.cpp +++ b/source/widget.cpp @@ -66,7 +66,7 @@ void Widget::set_geometry(const Geometry &g) void Widget::set_parent(Container *p) { if(parent && p) - throw InvalidState("Widget is already in a Container"); + throw hierarchy_error("widget already parented"); else if(p==parent) return; parent = p; @@ -127,9 +127,9 @@ void Widget::set_focusable(bool f) void Widget::set_focus() { if(!parent) - throw InvalidState("No parent"); + throw hierarchy_error("no parent"); if(!visible) - throw InvalidState("Can't set focus on invisible widget"); + throw logic_error("!visible"); signal_request_focus.emit(); } @@ -137,7 +137,7 @@ void Widget::set_focus() void Widget::render() const { if(!style) - throw InvalidState(format("Attempt to render a widget with null style (class=\"%s\", style_name=\"%s\")", get_class(), style_name)); + throw logic_error(format("Attempt to render a widget with null style (class=\"%s\", style_name=\"%s\")", get_class(), style_name)); GL::push_matrix(); GL::translate(geom.x, geom.y, 0);