]> git.tdb.fi Git - libs/gltk.git/commitdiff
Rework exceptions and use maputils
authorMikko Rasa <tdb@tdb.fi>
Wed, 21 Nov 2012 15:30:10 +0000 (17:30 +0200)
committerMikko Rasa <tdb@tdb.fi>
Wed, 21 Nov 2012 15:30:10 +0000 (17:30 +0200)
14 files changed:
source/connector.cpp
source/container.cpp
source/container.h
source/layout.cpp
source/list.cpp
source/logic.cpp
source/logic.h
source/panel.cpp
source/part.cpp
source/resources.cpp
source/table.cpp
source/text.cpp
source/userinterface.h
source/widget.cpp

index 2b64d0019af1abcb3a6c50be76e0005cc8ff54e9..9915ecc19d8df6291f87c0d84350990d8a2847e8 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/core/maputils.h>
 #include "connector.h"
 #include "logic.h"
 
@@ -18,11 +19,8 @@ void Connector::connect(const Logic &logic)
 
        for(list<Logic::WidgetBinding>::const_iterator i=logic_binds.begin(); i!=logic_binds.end(); ++i)
        {
-               map<string, ConnAction *>::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);
        }
 }
 
index 483608a40764a3e3612be9460f5d92a100bd7ab7..bec206602a95248c72f53ad4de28f41444b71659 100644 (file)
@@ -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)
index 66884652fc4da3bb80097d90c0f167d05d06ef95..d1120cd8c36bb1e25fb845f6a20831cc264c057c 100644 (file)
@@ -2,12 +2,21 @@
 #define MSP_GLTK_CONTAINER_H_
 
 #include <list>
+#include <stdexcept>
 #include <sigc++/trackable.h>
 #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:
index 6ed46e4d44cecc7e9922ed4920566ada5f609422..ed7b93617dcd485c54759b7c00df05f90827f626 100644 (file)
@@ -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<Constraint>::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)
index d81c48646016ab03e5704747b235be8be07df03b..a1803694a23633cd0a3911bafeb007e2eef1f141 100644 (file)
@@ -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<int>(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];
 }
index 3ed745c09c9e083233efada18fb76d6d94c707e3..db382fe6074f14aebea7293d61808f1ff0e5c100 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/core/maputils.h>
 #include "logic.h"
 
 using namespace std;
@@ -14,13 +15,10 @@ Logic::Loader::Loader(Logic &l, const map<string, Widget *> &w):
 
 void Logic::Loader::bind(const string &wdg, const string &data)
 {
-       map<string, Widget *>::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);
index e6c5243b809acbb2b2cd24220bd879a55a20c2a7..cd105782d03cdf8c40947b79d96591faa6fe6462 100644 (file)
@@ -5,7 +5,6 @@
 #include <map>
 #include <string>
 #include <sigc++/slot.h>
-#include <msp/core/except.h>
 #include <msp/datafile/loader.h>
 
 namespace Msp {
index b44004ff129c63b90cfb2b4aa5b8b2039e2aabe1..4b44370343c47bcbe8fae960ad0d6827881bb234 100644 (file)
@@ -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
index 7eb19c7755f3a006bc7a81bc31d5c6841ce66361..0ba03a3d3e1d027e2e50021212db4b1cfabd8862 100644 (file)
@@ -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];
 }
index 11fc1fabe8a0a467ba82019337cc2d2851674e42..869885fecd97e77ae3719399c8fd53d6690baef7 100644 (file)
@@ -1,4 +1,3 @@
-#include <msp/core/except.h>
 #include <msp/fs/utils.h>
 #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;
 }
index 4a74e17ba5edec6d2b4e5ac42e1e9d56d0948c7b..936be5cc61b032bb4c94ca7622a54e138fd5dbbd 100644 (file)
@@ -1,3 +1,4 @@
+#include <stdexcept>
 #include <msp/gl/matrix.h>
 #include <msp/gl/transform.h>
 #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;
 }
index b0d57f7c80afdc5baae8ba0be0144a05f9144a2e..c6fe129fc405ab4c61aa34b3c088a784c9bf3f03 100644 (file)
@@ -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;
 }
 
index 58eb5186698db462a7747d17c27ba275deea06ea..04a9ccfd10b6cd815f1618f9a217fd3db59f39ff 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef MSP_GLTK_USERINTERFACE_H_
 #define MSP_GLTK_USERINTERFACE_H_
 
+#include <msp/core/maputils.h>
 #include "logic.h"
 #include "root.h"
 
@@ -41,15 +42,7 @@ public:
        template<typename W>
        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<W *>(i->second);
-               if(!w)
-                       throw Exception("Widget type mismatch");
-
-               return *w;
+               return dynamic_cast<W &>(*get_item(widgets, n));
        }
 
        template<typename W>
index 3f11af6d32e08bb707fc5b78bedd9b8aab964cf1..1cb42e5cae15598cdd82ea25809913e59a8359e7 100644 (file)
@@ -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);