From: Mikko Rasa Date: Sun, 20 Aug 2023 07:48:39 +0000 (+0300) Subject: Convert loops and iterators to use C++11 features X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=b4a3d651f57c46507aaa99a71a14fea15f0e430d;p=libs%2Fgltk.git Convert loops and iterators to use C++11 features --- diff --git a/Build b/Build index f7dc8e2..0de8497 100644 --- a/Build +++ b/Build @@ -9,6 +9,11 @@ package "mspgltk" require "mspdatafile"; require "sigc++-2.0"; + build_info + { + standard CXX "c++11"; + }; + library "mspgltk" { source "source"; diff --git a/source/arrangement.cpp b/source/arrangement.cpp index 5eeebbb..838a488 100644 --- a/source/arrangement.cpp +++ b/source/arrangement.cpp @@ -31,8 +31,8 @@ void Arrangement::arrange(Arrangement &arr) { Side side = static_cast(i); const Edge &edge = arr.get_edge(side); - for(list::const_iterator j=edge.widgets.begin(); j!=edge.widgets.end(); ++j) - process_widget(**j, side, edge.aligned); + for(Widget *w: edge.widgets) + process_widget(*w, side, edge.aligned); } finish_slot(); } @@ -44,13 +44,13 @@ void Arrangement::add_constraint(Widget &wdg, Layout::ConstraintType type, Side void Arrangement::add_constraint(Widget &wdg, Layout::ConstraintType type, const Edge &edge, int spacing) { - for(list::const_iterator i=edge.widgets.begin(); i!=edge.widgets.end(); ++i) - if(*i!=&wdg) + for(Widget *w: edge.widgets) + if(w!=&wdg) { if(spacing<0) - layout.add_constraint(wdg, type, **i); + layout.add_constraint(wdg, type, *w); else - layout.add_constraint(wdg, type, **i, spacing); + layout.add_constraint(wdg, type, *w, spacing); } } @@ -106,7 +106,7 @@ void Arrangement::Edge::align() if(aligned || widgets.empty()) return; - list::iterator second = widgets.begin(); + auto second = widgets.begin(); ++second; widgets.erase(second, widgets.end()); diff --git a/source/container.cpp b/source/container.cpp index 6e1a356..d5fa6d5 100644 --- a/source/container.cpp +++ b/source/container.cpp @@ -1,3 +1,4 @@ +#include #include "container.h" #include "part.h" @@ -41,19 +42,16 @@ void Container::add(Widget &wdg) void Container::remove(Widget &wdg) { - for(list::iterator i=children.begin(); i!=children.end(); ++i) - if((*i)->widget==&wdg) - { - wdg.set_parent(0); - delete *i; - children.erase(i); - if(wdg.get_animation_interval()) - check_animation_interval(); - on_child_removed(wdg); - return; - } + auto i = find_if(children, [&wdg](const Child *c){ return c->widget==&wdg; }); + if(i==children.end()) + throw hierarchy_error("widget not in container"); - throw hierarchy_error("widget not in container"); + wdg.set_parent(0); + delete *i; + children.erase(i); + if(wdg.get_animation_interval()) + check_animation_interval(); + on_child_removed(wdg); } Container::Child *Container::create_child(Widget *wdg) @@ -94,14 +92,14 @@ void Container::reposition_child(Widget &child, const Part &part) const list Container::get_children() const { list result; - for(list::const_iterator i=children.begin(); i!=children.end(); ++i) - result.push_back((*i)->widget); + for(const Child *c: children) + result.push_back(c->widget); return result; } Widget *Container::get_child_at(int x, int y) const { - for(list::const_iterator i=children.end(); i!=children.begin();) + for(auto i=children.end(); i!=children.begin();) if((*--i)->widget->is_visible() && (*i)->widget->get_geometry().is_inside(x, y)) return (*i)->widget; @@ -123,14 +121,11 @@ Widget *Container::get_descendant_at(int x, int y) const void Container::raise(Widget &wdg) { - for(list::iterator i=children.begin(); i!=children.end(); ++i) - if((*i)->widget==&wdg) - { - children.splice(children.end(), children, i); - return; - } + auto i = find_if(children, [&wdg](const Child *c){ return c->widget==&wdg; }); + if(i==children.end()) + throw hierarchy_error("widget not in container"); - throw hierarchy_error("widget not in container"); + children.splice(children.end(), children, i); } void Container::set_pointer_focus(Widget *wdg, bool grab) @@ -177,9 +172,9 @@ Widget *Container::get_final_input_focus() const void Container::check_animation_interval() { Time::TimeDelta shortest; - for(list::const_iterator i=children.begin(); i!=children.end(); ++i) + for(const Child *c: children) { - const Time::TimeDelta &child_iv = (*i)->widget->get_animation_interval(); + const Time::TimeDelta &child_iv = c->widget->get_animation_interval(); if(child_iv && (!shortest || child_iv::iterator i=children.begin(); i!=children.end(); ++i) - (*i)->widget->rebuild_hierarchy(); + for(Child *c: children) + c->widget->rebuild_hierarchy(); } } @@ -355,29 +350,29 @@ bool Container::navigate(Navigation nav) void Container::animate(const Time::TimeDelta &dt) { - for(list::iterator i=children.begin(); i!=children.end(); ++i) + for(Child *c: children) { - const Time::TimeDelta &child_iv = (*i)->widget->get_animation_interval(); + const Time::TimeDelta &child_iv = c->widget->get_animation_interval(); if(!child_iv) continue; - (*i)->time_since_animate += dt; - if((*i)->time_since_animate>=child_iv) + c->time_since_animate += dt; + if(c->time_since_animate>=child_iv) { - Time::TimeDelta child_dt = (*i)->time_since_animate; - (*i)->time_since_animate = min((*i)->time_since_animate-child_iv, child_iv); - (*i)->widget->animate(child_dt); + Time::TimeDelta child_dt = c->time_since_animate; + c->time_since_animate = min(c->time_since_animate-child_iv, child_iv); + c->widget->animate(child_dt); } } } void Container::on_reparent() { - for(list::iterator i=children.begin(); i!=children.end(); ++i) + for(const Child *c: children) { - if(Container *c = dynamic_cast((*i)->widget)) - c->on_reparent(); - (*i)->widget->update_style(); + if(Container *o = dynamic_cast(c->widget)) + o->on_reparent(); + c->widget->update_style(); } } diff --git a/source/layout.cpp b/source/layout.cpp index 1cf35c7..200801f 100644 --- a/source/layout.cpp +++ b/source/layout.cpp @@ -1,5 +1,5 @@ -#include #include +#include #include #include #include "arrangement.h" @@ -98,8 +98,8 @@ Layout::Layout(): Layout::~Layout() { - for(list::iterator i=slots.begin(); i!=slots.end(); ++i) - delete *i; + for(Slot *s: slots) + delete s; } void Layout::set_container(Container &c) @@ -154,7 +154,7 @@ Arrangement *Layout::get_arrangement() const void Layout::pop_arrangement(Arrangement &arr) { - list::iterator begin = find(arrangement_stack.begin(), arrangement_stack.end(), &arr); + auto begin = find(arrangement_stack, &arr); if(begin==arrangement_stack.end()) return; @@ -184,71 +184,70 @@ void Layout::add_widget(Widget &wdg) void Layout::remove_widget(Widget &wdg) { - for(list::iterator i=slots.begin(); i!=slots.end(); ++i) - if(&(*i)->widget==&wdg) + auto i = find_if(slots, [&wdg](Slot *s){ return &s->widget==&wdg; }); + if(i==slots.end()) + return; + + for(Slot *s: slots) + if(s!=*i) { - for(list::iterator j=slots.begin(); j!=slots.end(); ++j) - if(j!=i) - { - for(list::iterator k=(*j)->constraints.begin(); k!=(*j)->constraints.end(); ) - { - if(&k->target==*i) - (*j)->constraints.erase(k++); - else - ++k; - } - } + for(auto k=s->constraints.begin(); k!=s->constraints.end(); ) + { + if(&k->target==*i) + s->constraints.erase(k++); + else + ++k; + } + } - delete *i; - slots.erase(i); + delete *i; + slots.erase(i); - update_slot_indices(); - update(); - return; - } + update_slot_indices(); + update(); } void Layout::update_slot_indices() { n_active_slots = 0; unsigned n_floating = 0; - for(list::iterator i=slots.begin(); i!=slots.end(); ++i) + for(Slot *s: slots) { - if((*i)->widget.is_visible() || (*i)->ghost) + if(s->widget.is_visible() || s->ghost) { - (*i)->index = n_active_slots++; - if((*i)->floating) + s->index = n_active_slots++; + if(s->floating) ++n_floating; } else - (*i)->index = -1; + s->index = -1; } n_slack_vars[0] = n_floating*2; n_slack_vars[1] = n_floating*2; - for(list::iterator i=slots.begin(); i!=slots.end(); ++i) - if((*i)->index>=0) + for(const Slot *s: slots) + if(s->index>=0) { - if(!(*i)->floating) + if(!s->floating) { for(unsigned j=0; j<2; ++j) - if(((*i)->*(pointers[j].packing)).gravity==0) + if((s->*(pointers[j].packing)).gravity==0) n_slack_vars[j] += 2; } - for(list::iterator j=(*i)->constraints.begin(); j!=(*i)->constraints.end(); ++j) - if(j->target.index>(*i)->index && (j->type&SLACK)) - ++n_slack_vars[j->type&1]; + for(const Constraint &c: s->constraints) + if(c.target.index>s->index && (c.type&SLACK)) + ++n_slack_vars[c.type&1]; } } Layout::Slot &Layout::get_slot_for_widget(Widget &wdg) { - for(list::iterator i=slots.begin(); i!=slots.end(); ++i) - if(&(*i)->widget==&wdg) - return **i; + auto i = find_if(slots, [&wdg](const Slot *s){ return &s->widget==&wdg; }); + if(i==slots.end()) + throw hierarchy_error("widget not in layout"); - throw hierarchy_error("widget not in layout"); + return **i; } Layout::ConstraintType Layout::complement(ConstraintType type) @@ -264,8 +263,8 @@ void Layout::create_constraint(Widget &src, ConstraintType type, Widget &tgt, in Slot &src_slot = get_slot_for_widget(src); Slot &tgt_slot = get_slot_for_widget(tgt); - for(list::iterator i=src_slot.constraints.begin(); i!=src_slot.constraints.end(); ++i) - if(i->type==type && &i->target==&tgt_slot) + for(const Constraint &c: src_slot.constraints) + if(c.type==type && &c.target==&tgt_slot) return; src_slot.constraints.push_back(Constraint(type, tgt_slot)); @@ -336,8 +335,8 @@ void Layout::update() solve_constraints(HORIZONTAL, UPDATE); solve_constraints(VERTICAL, UPDATE); - for(list::iterator i=slots.begin(); i!=slots.end(); ++i) - (*i)->widget.set_geometry((*i)->geom); + for(const Slot *s: slots) + s->widget.set_geometry(s->geom); } void Layout::autosize(Geometry &geom) @@ -364,29 +363,29 @@ void Layout::solve_constraints(int dir, SolveMode mode) LinearProgram linprog(n_active_slots*5+n_slack_vars[dir]+1); float weight = slots.size()+1; unsigned k = n_active_slots*5; - for(list::iterator i=slots.begin(); i!=slots.end(); ++i) + for(const Slot *s: slots) { - if((*i)->index<0) + if(s->index<0) continue; LinearProgram::Row objective = linprog.get_objective_row(); if(mode==AUTOSIZE) { - objective[(*i)->index*5] = -1; - objective[(*i)->index*5+1] = -1; + objective[s->index*5] = -1; + objective[s->index*5+1] = -1; } else { - if(!(*i)->floating) - objective[(*i)->index*5] = ((*i)->*(ptrs.packing)).gravity/weight; - objective[(*i)->index*5+1] = (((*i)->*(ptrs.packing)).expand ? weight : -1); + if(!s->floating) + objective[s->index*5] = (s->*(ptrs.packing)).gravity/weight; + objective[s->index*5+1] = ((s->*(ptrs.packing)).expand ? weight : -1); } { // Prevent the widget from going past the container's low edge. LinearProgram::Row row = linprog.add_row(); - row[(*i)->index*5] = 1; - row[(*i)->index*5+2] = -1; + row[s->index*5] = 1; + row[s->index*5+2] = -1; row.back() = margin.*(ptrs.low_margin); } @@ -394,26 +393,26 @@ void Layout::solve_constraints(int dir, SolveMode mode) { // Prevent the widget from going past the container's high edge. LinearProgram::Row row = linprog.add_row(); - row[(*i)->index*5] = 1; - row[(*i)->index*5+1] = 1; - row[(*i)->index*5+3] = 1; + row[s->index*5] = 1; + row[s->index*5+1] = 1; + row[s->index*5+3] = 1; row.back() = geom.*(ptrs.dim)-margin.*(ptrs.high_margin); } - if((*i)->floating || ((*i)->*(ptrs.packing)).gravity==0) + if(s->floating || (s->*(ptrs.packing)).gravity==0) { /* Try to keep the widget as close to a target position as possible. Since linear programs can't express absolute values directly, use two opposing slack variables that are optimized for a low value. */ - float a = ((*i)->*(ptrs.packing)).gravity*0.5+0.5; + float a = (s->*(ptrs.packing)).gravity*0.5+0.5; LinearProgram::Row row = linprog.add_row(); - row[(*i)->index*5] = 1; - row[(*i)->index*5+1] = a; + row[s->index*5] = 1; + row[s->index*5+1] = a; row[k] = 1; row[k+1] = -1; - if((*i)->floating) + if(s->floating) { - const Geometry &cgeom = (*i)->widget.get_geometry(); + const Geometry &cgeom = s->widget.get_geometry(); row.back() = cgeom.*(ptrs.pos)+cgeom.*(ptrs.dim)*a; } else @@ -427,30 +426,30 @@ void Layout::solve_constraints(int dir, SolveMode mode) /* Don't allow the widget's dimension to get below that determined by autosizing. */ LinearProgram::Row row = linprog.add_row(); - row[(*i)->index*5+1] = 1; - row[(*i)->index*5+4] = -1; - row.back() = (*i)->autosize_geom.*(ptrs.dim); + row[s->index*5+1] = 1; + row[s->index*5+4] = -1; + row.back() = s->autosize_geom.*(ptrs.dim); } /* Add rows for user-defined constraints. Constraints are always added in pairs, so it's only necessary to create a row for one half. */ - for(list::iterator j=(*i)->constraints.begin(); j!=(*i)->constraints.end(); ++j) - if(j->target.index>(*i)->index && (j->type&1)==dir) + for(const Constraint &c: s->constraints) + if(c.target.index>s->index && (c.type&1)==dir) { LinearProgram::Row row = linprog.add_row(); - float polarity = ((j->type&SELF_DIM) ? -1 : 1); - float dim_weight = ((j->type&HALF_DIM) ? 0.5f : 1); - if(j->type&SELF_POS) - row[(*i)->index*5] = polarity; - if(j->type&SELF_DIM) - row[(*i)->index*5+1] = polarity*dim_weight; - if(j->type&TARGET_POS) - row[j->target.index*5] = -polarity; - if(j->type&TARGET_DIM) - row[j->target.index*5+1] = -polarity*dim_weight; - if(j->type&SPACING) - row.back() = (j->spacing>=0 ? j->spacing : this->*(ptrs.spacing)); - if(j->type&SLACK) + float polarity = ((c.type&SELF_DIM) ? -1 : 1); + float dim_weight = ((c.type&HALF_DIM) ? 0.5f : 1); + if(c.type&SELF_POS) + row[s->index*5] = polarity; + if(c.type&SELF_DIM) + row[s->index*5+1] = polarity*dim_weight; + if(c.type&TARGET_POS) + row[c.target.index*5] = -polarity; + if(c.type&TARGET_DIM) + row[c.target.index*5+1] = -polarity*dim_weight; + if(c.type&SPACING) + row.back() = (c.spacing>=0 ? c.spacing : this->*(ptrs.spacing)); + if(c.type&SLACK) row[k++] = -1; } } @@ -461,20 +460,20 @@ void Layout::solve_constraints(int dir, SolveMode mode) if(mode==AUTOSIZE) { autosize_geom.*(ptrs.dim) = 0; - for(list::iterator i=slots.begin(); i!=slots.end(); ++i) - if((*i)->index>=0) + for(const Slot *s: slots) + if(s->index>=0) { - int high_edge = linprog.get_variable((*i)->index*5)+linprog.get_variable((*i)->index*5+1); + int high_edge = linprog.get_variable(s->index*5)+linprog.get_variable(s->index*5+1); autosize_geom.*(ptrs.dim) = max(autosize_geom.*(ptrs.dim), high_edge+margin.*(ptrs.high_margin)); } } else { - for(list::iterator i=slots.begin(); i!=slots.end(); ++i) - if((*i)->index>=0) + for(Slot *s: slots) + if(s->index>=0) { - (*i)->geom.*(ptrs.pos) = linprog.get_variable((*i)->index*5); - (*i)->geom.*(ptrs.dim) = linprog.get_variable((*i)->index*5+1); + s->geom.*(ptrs.pos) = linprog.get_variable(s->index*5); + s->geom.*(ptrs.dim) = linprog.get_variable(s->index*5+1); } } } @@ -728,31 +727,29 @@ void Layout::LinearProgram::prepare_columns() vector obj_coeff(n_rows, 0.0f); vector row_coeff(n_rows, 1.0f); const vector &constants = columns.back().values; - for(vector::iterator i=columns.begin(); i!=columns.end(); ++i) - { - if(i->values.size()>=2 && i->values.back()!=0.0f && (constants.size()values.size() || i->values.back()*constants[i->values.size()-1]>=0.0f) && obj_coeff[i->values.size()-1]==0.0f) + for(Column &c: columns) + if(c.values.size()>=2 && c.values.back()!=0.0f && (constants.size()=0.0f) && obj_coeff[c.values.size()-1]==0.0f) { bool basic = true; - for(unsigned j=1; (basic && j+1values.size()); ++j) - basic = (i->values[j]==0.0f); + for(unsigned j=1; (basic && j+1basic = i->values.size()-1; - row_coeff[i->basic] = 1.0f/i->values.back(); - obj_coeff[i->basic] = -i->values.front(); - i->values.clear(); + c.basic = c.values.size()-1; + row_coeff[c.basic] = 1.0f/c.values.back(); + obj_coeff[c.basic] = -c.values.front(); + c.values.clear(); } } - } // Price out the newly-created basic variables. - for(vector::iterator i=columns.begin(); i!=columns.end(); ++i) - if(!i->values.empty()) + for(Column &c: columns) + if(!c.values.empty()) { - for(unsigned j=0; jvalues.size(); ++j) + for(unsigned j=0; jvalues[j] *= row_coeff[j]; - i->values.front() += obj_coeff[j]*i->values[j]; + c.values[j] *= row_coeff[j]; + c.values.front() += obj_coeff[j]*c.values[j]; } } } @@ -763,28 +760,28 @@ void Layout::LinearProgram::add_artificial_variables() for(unsigned i=0; i::iterator i=columns.begin(); i!=columns.end(); ++i) - if(i->basic) - artificial_rows[i->basic-1] = 0; + for(const Column &c: columns) + if(c.basic) + artificial_rows[c.basic-1] = 0; artificial_rows.erase(std::remove(artificial_rows.begin(), artificial_rows.end(), 0), artificial_rows.end()); /* Force all non-basic columns fully into existence and relocate objective row to bottom in preparation of phase 1. A new objective row is calculated by pricing out the constraint rows. */ - for(vector::iterator i=columns.begin(); i!=columns.end(); ++i) - if(!i->basic) + for(Column &c: columns) + if(!c.basic) { float objective = 0.0f; - if(!i->values.empty()) + if(!c.values.empty()) { - objective = i->values.front(); - i->values.front() = 0.0f; - for(vector::iterator j=artificial_rows.begin(); j!=artificial_rows.end(); ++j) - if(*jvalues.size()) - i->values.front() += i->values[*j]; + objective = c.values.front(); + c.values.front() = 0.0f; + for(unsigned r: artificial_rows) + if(rvalues.resize(n_rows+1, 0.0f); - i->values.back() = objective; + c.values.resize(n_rows+1, 0.0f); + c.values.back() = objective; } if(artificial_rows.empty()) @@ -821,11 +818,11 @@ void Layout::LinearProgram::remove_artificial_variables() /* Get rid of the artificial variables and restore the original objective row to form the phase 2 problem. */ columns.erase(columns.begin()+(n_columns-1), columns.end()-1); - for(vector::iterator i=columns.begin(); i!=columns.end(); ++i) - if(!i->basic) + for(Column &c: columns) + if(!c.basic) { - i->values.front() = i->values.back(); - i->values.pop_back(); + c.values.front() = c.values.back(); + c.values.pop_back(); } } diff --git a/source/list.cpp b/source/list.cpp index a7185ac..5dd6d3e 100644 --- a/source/list.cpp +++ b/source/list.cpp @@ -119,8 +119,8 @@ void List::set_data(ListData &d) own_data = false; observer = new DataObserver(*this); - for(vector::iterator i=items.begin(); i!=items.end(); ++i) - delete *i; + for(Item *i: items) + delete i; items.clear(); unsigned n_items = data->size(); for(unsigned i=0; i::iterator i=list.items.begin(); i!=list.items.end(); ++i) - delete *i; + for(Item *i: list.items) + delete i; list.items.clear(); list.items_changed(); @@ -605,10 +605,10 @@ void List::Item::autosize_special(const Part &part, Geometry &ageom) const if(part.get_name()=="children") { const Sides &margin = part.get_margin(); - for(list::const_iterator i=children.begin(); i!=children.end(); ++i) + for(const Child *c: children) { Geometry cgeom; - (*i)->widget->autosize(cgeom); + c->widget->autosize(cgeom); ageom.w = max(ageom.w, cgeom.x+cgeom.w+margin.right); ageom.h = max(ageom.h, cgeom.y+cgeom.h+margin.top); } @@ -624,8 +624,8 @@ void List::Item::render_special(const Part &part, GL::Renderer &renderer) const { if(part.get_name()=="children") { - for(list::const_iterator i=children.begin(); i!=children.end(); ++i) - (*i)->widget->render(renderer); + for(const Child *c: children) + c->widget->render(renderer); } } @@ -651,12 +651,13 @@ void List::MultiColumnItem::check_widths(vector &widths) const widths.resize(children.size(), 0); unsigned n = 0; - for(list::const_iterator i=children.begin(); i!=children.end(); ++i, ++n) + for(const Child *c: children) { Geometry cgeom; - (*i)->widget->autosize(cgeom); + c->widget->autosize(cgeom); // TODO invent a better way to specify spacings widths[n] = max(widths[n], cgeom.w+8); + ++n; } } @@ -672,10 +673,10 @@ void List::MultiColumnItem::set_widths(const vector &widths) const Sides &margin = part->get_margin(); int x = margin.left; unsigned n = 0; - for(list::const_iterator i=children.begin(); i!=children.end(); ++i, ++n) + for(const Child *c: children) { - (*i)->widget->set_position(x, margin.bottom); - x += widths[n]; + c->widget->set_position(x, margin.bottom); + x += widths[n++]; } } @@ -684,14 +685,14 @@ void List::MultiColumnItem::on_style_change() if(!style) return; - for(std::list::const_iterator i=children.begin(); i!=children.end(); ++i) - (*i)->widget->autosize(); + for(const Child *c: children) + c->widget->autosize(); vector widths; List *list = static_cast(parent); - for(vector::const_iterator i=list->items.begin(); i!=list->items.end(); ++i) - if(*i!=this) - if(MultiColumnItem *mci = dynamic_cast(*i)) + for(Item *i: list->items) + if(i!=this) + if(MultiColumnItem *mci = dynamic_cast(i)) mci->check_widths(widths); vector self_widths(widths); @@ -702,8 +703,8 @@ void List::MultiColumnItem::on_style_change() if(update_all) { - for(vector::const_iterator i=list->items.begin(); i!=list->items.end(); ++i) - if(MultiColumnItem *mci = dynamic_cast(*i)) + for(Item *i: list->items) + if(MultiColumnItem *mci = dynamic_cast(i)) mci->set_widths(self_widths); } diff --git a/source/panel.cpp b/source/panel.cpp index 957385e..fa16ec7 100644 --- a/source/panel.cpp +++ b/source/panel.cpp @@ -56,9 +56,9 @@ void Panel::render_special(const Part &part, GL::Renderer &renderer) const { if(part.get_name()=="children") { - for(list::const_iterator i=children.begin(); i!=children.end(); ++i) - if((*i)->widget->is_visible()) - (*i)->widget->render(renderer); + for(const Container::Child *c: children) + if(c->widget->is_visible()) + c->widget->render(renderer); } } @@ -109,7 +109,7 @@ bool Panel::navigate(Navigation nav) } else if(nav==NAV_NEXT || nav==NAV_PREVIOUS) { - vector::iterator i = find(nav_order, input_focus); + auto i = find(nav_order, input_focus); if(nav==NAV_NEXT) { @@ -137,12 +137,12 @@ Widget *Panel::find_next_child(int origin_x, int origin_y, int origin_dim, int n { Widget *sibling = 0; int best_score = 0; - for(list::const_iterator i=children.begin(); i!=children.end(); ++i) + for(const Child *c: children) { - if((*i)->widget==input_focus || !(*i)->widget->is_focusable()) + if(c->widget==input_focus || !c->widget->is_focusable()) continue; - const Geometry &cgeom = (*i)->widget->get_geometry(); + const Geometry &cgeom = c->widget->get_geometry(); int dx = compute_delta(cgeom.x, cgeom.w, origin_x, origin_dim, nav_x); int dy = compute_delta(cgeom.y, cgeom.h, origin_y, origin_dim, nav_y); @@ -154,7 +154,7 @@ Widget *Panel::find_next_child(int origin_x, int origin_y, int origin_dim, int n if(score>=0 && (!sibling || scorewidget; + sibling = c->widget; best_score = score; } } @@ -196,7 +196,7 @@ void Panel::on_child_added(Widget &wdg) void Panel::on_child_removed(Widget &wdg) { - vector::iterator i = std::remove(nav_order.begin(), nav_order.end(), &wdg); + auto i = std::remove(nav_order.begin(), nav_order.end(), &wdg); if(i!=nav_order.end()) nav_order.erase(i, nav_order.end()); diff --git a/source/style.cpp b/source/style.cpp index eff93fe..acbdff5 100644 --- a/source/style.cpp +++ b/source/style.cpp @@ -1,3 +1,4 @@ +#include #include "resources.h" #include "style.h" @@ -36,11 +37,8 @@ const GL::Sampler &Style::get_sampler() const const Part *Style::get_part(const string &name) const { - for(PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i) - if(i->get_name()==name) - return &*i; - - return 0; + auto i = find_if(parts, [&name](const Part &p){ return p.get_name()==name; }); + return (i!=parts.end() ? &*i : 0); } bool Style::compare_states(State s1, State s2) const @@ -53,8 +51,8 @@ bool Style::compare_states(State s1, State s2) const if(c1.r!=c2.r || c1.g!=c2.g || c1.b!=c2.b) return true; - for(PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i) - if(i->get_graphic(s1)!=i->get_graphic(s2)) + for(const Part &p: parts) + if(p.get_graphic(s1)!=p.get_graphic(s2)) return true; return false; diff --git a/source/text.cpp b/source/text.cpp index bd35803..bffb6e6 100644 --- a/source/text.cpp +++ b/source/text.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -48,16 +49,16 @@ void Text::set_style(const Style *s) { const GL::Font &font = style->get_font(); float font_size = style->get_font_size(); - for(vector::iterator i=lines.begin(); i!=lines.end(); ++i) - i->width = static_cast(font.get_string_width(text.substr(i->start, i->bytes))*font_size); + for(Line &l: lines) + l.width = static_cast(font.get_string_width(text.substr(l.start, l.bytes))*font_size); } } unsigned Text::get_width() const { unsigned width = 0; - for(vector::const_iterator i=lines.begin(); i!=lines.end(); ++i) - width = max(width, i->width); + for(const Line &l: lines) + width = max(width, l.width); return width; } @@ -92,8 +93,7 @@ void Text::erase(unsigned pos, unsigned len) check_alignment(pos+len); text.erase(pos, len); - vector::iterator i; - for(i=lines.begin(); (i!=lines.end() && i->start+i->bytes=pos; }); if(pos+len>i->start+i->bytes) find_lines(); @@ -116,8 +116,7 @@ void Text::insert(unsigned pos, const string &s) find_lines(); else { - vector::iterator i; - for(i=lines.begin(); (i!=lines.end() && i->start+i->bytes=pos; }); i->bytes += s.size(); i->length = count_characters(i->start, i->bytes); @@ -158,7 +157,7 @@ unsigned Text::move_offset(unsigned offs, int change) const return offs; StringCodec::Utf8::Decoder dec(StringCodec::IGNORE_ERRORS); - string::const_iterator i = text.begin()+offs; + auto i = text.begin()+offs; if(change>0) { for(; change>0; --change) @@ -169,7 +168,7 @@ unsigned Text::move_offset(unsigned offs, int change) const while(change<0 && i!=text.begin()) { --i; - string::const_iterator j = i; + auto j = i; if(dec.decode_char(text, j)!=-1) ++change; } @@ -211,7 +210,7 @@ unsigned Text::coords_to_offset(unsigned row, unsigned col) const else { StringCodec::Utf8::Decoder dec; - string::const_iterator i = text.begin()+line.start; + auto i = text.begin()+line.start; for(col=min(col, line.length); col; --col) dec.decode_char(text, i); return i-text.begin(); @@ -291,8 +290,8 @@ void Text::find_lines() unsigned Text::count_characters(unsigned start, unsigned bytes) const { StringCodec::Utf8::Decoder dec; - string::const_iterator i = text.begin()+start; - string::const_iterator end = i+bytes; + auto i = text.begin()+start; + auto end = i+bytes; unsigned count = 0; for(; i &siblings = parent->get_children(); - for(list::const_iterator i=siblings.begin(); i!=siblings.end(); ++i) - if(Toggle *tgl = dynamic_cast(*i)) + for(Widget *w: parent->get_children()) + if(Toggle *tgl = dynamic_cast(w)) if(tgl!=this && tgl->get_exclusive() && tgl->get_value()) tgl->set_value(false); } diff --git a/source/widget.cpp b/source/widget.cpp index 4ef1b46..6ac4d2a 100644 --- a/source/widget.cpp +++ b/source/widget.cpp @@ -52,18 +52,17 @@ void Widget::autosize(Geometry &ageom) const return; ageom = Geometry(geom.x, geom.y, 0, 0); - const Style::PartSeq &parts = style->get_parts(); - for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i) + for(const Part &p: style->get_parts()) { - if(i->get_name().empty()) + if(p.get_name().empty()) { - const Geometry &pgeom = i->get_geometry(); - const Sides &pmargin = i->get_margin(); + const Geometry &pgeom = p.get_geometry(); + const Sides &pmargin = p.get_margin(); ageom.w = max(ageom.w, pgeom.w+pmargin.left+pmargin.right); ageom.h = max(ageom.h, pgeom.h+pmargin.top+pmargin.bottom); } else - autosize_special(*i, ageom); + autosize_special(p, ageom); } } @@ -231,13 +230,12 @@ void Widget::rebuild() return; PartCache::Rebuild rebuild_cache(part_cache); - const Style::PartSeq &parts = style->get_parts(); - for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i) + for(const Part &p: style->get_parts()) { - if(i->get_name().empty()) - i->build(geom, state, part_cache); + if(p.get_name().empty()) + p.build(geom, state, part_cache); else - rebuild_special(*i); + rebuild_special(p); } } @@ -258,16 +256,15 @@ void Widget::render(GL::Renderer &renderer) const int y = 0; map_coords_to_root(x, y); renderer.set_matrix(GL::Matrix::translation(x, y, 0)); - const PartCache::PartList &parts = part_cache.get_parts(); - for(PartCache::PartList::const_iterator i=parts.begin(); i!=parts.end(); ++i) + for(const CachedPart &p: part_cache.get_parts()) { - if(i->mesh && i->texture) + if(p.mesh && p.texture) { - renderer.set_texture(texture_tag, i->texture, &style->get_sampler()); - i->mesh->draw(renderer); + renderer.set_texture(texture_tag, p.texture, &style->get_sampler()); + p.mesh->draw(renderer); } - else if(i->part) - render_special(*i->part, renderer); + else if(p.part) + render_special(*p.part, renderer); } }