]> git.tdb.fi Git - libs/gltk.git/commitdiff
Convert loops and iterators to use C++11 features
authorMikko Rasa <tdb@tdb.fi>
Sun, 20 Aug 2023 07:48:39 +0000 (10:48 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 20 Aug 2023 07:48:39 +0000 (10:48 +0300)
Build
source/arrangement.cpp
source/container.cpp
source/layout.cpp
source/list.cpp
source/panel.cpp
source/style.cpp
source/text.cpp
source/toggle.cpp
source/widget.cpp

diff --git a/Build b/Build
index f7dc8e2f6b744d3649955fefb612f1da6fbfdcb0..0de849752693355fbe28b4d897256b78bbbaf548 100644 (file)
--- 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";
index 5eeebbb99abe8df310fca5720df8f1ff7d3233d3..838a488ea9fdfccebef388b22241bb5771542dbc 100644 (file)
@@ -31,8 +31,8 @@ void Arrangement::arrange(Arrangement &arr)
        {
                Side side = static_cast<Side>(i);
                const Edge &edge = arr.get_edge(side);
-               for(list<Widget *>::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<Widget *>::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<Widget *>::iterator second = widgets.begin();
+       auto second = widgets.begin();
        ++second;
 
        widgets.erase(second, widgets.end());
index 6e1a3562f5280a853d1996f6b99e973f0a0d25eb..d5fa6d59e3d0882fc593611c640af316d6124947 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/core/algorithm.h>
 #include "container.h"
 #include "part.h"
 
@@ -41,19 +42,16 @@ void Container::add(Widget &wdg)
 
 void Container::remove(Widget &wdg)
 {
-       for(list<Child *>::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<Widget *> Container::get_children() const
 {
        list<Widget *> result;
-       for(list<Child *>::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<Child *>::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<Child *>::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<Child *>::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<shortest))
                        shortest = child_iv;
        }
@@ -195,8 +190,8 @@ void Container::rebuild_hierarchy()
        if(children_rebuild_needed)
        {
                children_rebuild_needed = false;
-               for(list<Child *>::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<Child *>::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<Child *>::iterator i=children.begin(); i!=children.end(); ++i)
+       for(const Child *c: children)
        {
-               if(Container *c = dynamic_cast<Container *>((*i)->widget))
-                       c->on_reparent();
-               (*i)->widget->update_style();
+               if(Container *o = dynamic_cast<Container *>(c->widget))
+                       o->on_reparent();
+               c->widget->update_style();
        }
 }
 
index 1cf35c722a3a95476532ec1d35898aa7a0e5b903..200801f36285b7c3d9ed55402fd764a27f0a8813 100644 (file)
@@ -1,5 +1,5 @@
-#include <algorithm>
 #include <limits>
+#include <msp/core/algorithm.h>
 #include <msp/core/maputils.h>
 #include <msp/strings/format.h>
 #include "arrangement.h"
@@ -98,8 +98,8 @@ Layout::Layout():
 
 Layout::~Layout()
 {
-       for(list<Slot *>::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<Arrangement *>::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<Slot *>::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<Slot *>::iterator j=slots.begin(); j!=slots.end(); ++j)
-                               if(j!=i)
-                               {
-                                       for(list<Constraint>::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<Slot *>::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<Slot *>::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<Constraint>::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<Slot *>::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<Constraint>::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<Slot *>::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<Slot *>::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<Constraint>::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<Slot *>::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<Slot *>::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<float> obj_coeff(n_rows, 0.0f);
        vector<float> row_coeff(n_rows, 1.0f);
        const vector<float> &constants = columns.back().values;
-       for(vector<Column>::iterator i=columns.begin(); i!=columns.end(); ++i)
-       {
-               if(i->values.size()>=2 && i->values.back()!=0.0f && (constants.size()<i->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()<c.values.size() || c.values.back()*constants[c.values.size()-1]>=0.0f) && obj_coeff[c.values.size()-1]==0.0f)
                {
                        bool basic = true;
-                       for(unsigned j=1; (basic && j+1<i->values.size()); ++j)
-                               basic = (i->values[j]==0.0f);
+                       for(unsigned j=1; (basic && j+1<c.values.size()); ++j)
+                               basic = (c.values[j]==0.0f);
                        if(basic)
                        {
-                               i->basic = 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<Column>::iterator i=columns.begin(); i!=columns.end(); ++i)
-               if(!i->values.empty())
+       for(Column &c: columns)
+               if(!c.values.empty())
                {
-                       for(unsigned j=0; j<i->values.size(); ++j)
+                       for(unsigned j=0; j<c.values.size(); ++j)
                        {
-                               i->values[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<artificial_rows.size(); ++i)
                artificial_rows[i] = i+1;
 
-       for(vector<Column>::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<Column>::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<unsigned>::iterator j=artificial_rows.begin(); j!=artificial_rows.end(); ++j)
-                                       if(*j<i->values.size())
-                                               i->values.front() += i->values[*j];
+                               objective = c.values.front();
+                               c.values.front() = 0.0f;
+                               for(unsigned r: artificial_rows)
+                                       if(r<c.values.size())
+                                               c.values.front() += c.values[r];
                        }
-                       i->values.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<Column>::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();
                }
 }
 
index a7185acab422eee81b86a2021bbf018d78c85073..5dd6d3ead5619927d7f31ed1747c2c72d221c885 100644 (file)
@@ -119,8 +119,8 @@ void List::set_data(ListData &d)
        own_data = false;
        observer = new DataObserver(*this);
 
-       for(vector<Item *>::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<n_items; ++i)
@@ -577,8 +577,8 @@ void List::DataObserver::cleared()
 {
        list.sel_index = -1;
        list.focus_index = -1;
-       for(vector<Item *>::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<Child *>::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<Child *>::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<unsigned> &widths) const
                widths.resize(children.size(), 0);
 
        unsigned n = 0;
-       for(list<Child *>::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<unsigned> &widths)
        const Sides &margin = part->get_margin();
        int x = margin.left;
        unsigned n = 0;
-       for(list<Child *>::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<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
-               (*i)->widget->autosize();
+       for(const Child *c: children)
+               c->widget->autosize();
 
        vector<unsigned> widths;
        List *list = static_cast<List *>(parent);
-       for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
-               if(*i!=this)
-                       if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
+       for(Item *i: list->items)
+               if(i!=this)
+                       if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(i))
                                mci->check_widths(widths);
 
        vector<unsigned> self_widths(widths);
@@ -702,8 +703,8 @@ void List::MultiColumnItem::on_style_change()
 
        if(update_all)
        {
-               for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
-                       if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
+               for(Item *i: list->items)
+                       if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(i))
                                mci->set_widths(self_widths);
        }
 
index 957385e0dbb82ae14c586e045a9c5ac1e0aa7cad..fa16ec7de2826733d04a876f7ec445953bc76a8d 100644 (file)
@@ -56,9 +56,9 @@ void Panel::render_special(const Part &part, GL::Renderer &renderer) const
 {
        if(part.get_name()=="children")
        {
-               for(list<Container::Child *>::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<Widget *>::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<Child *>::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 || score<best_score))
                {
-                       sibling = (*i)->widget;
+                       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<Widget *>::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());
 
index eff93fe3c354255b9f53048b54d2022a472f68b1..acbdff54febbda18dce5df7a7cc265fb16e2a647 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/core/algorithm.h>
 #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;
index bd358032c65ff22d6f1cee3d83af64f5570e6909..bffb6e6a86cc0b33f6b35285d6a22338cfb7c8ae 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/core/algorithm.h>
 #include <msp/gl/matrix.h>
 #include <msp/gl/meshbuilder.h>
 #include <msp/gl/texture2d.h>
@@ -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<Line>::iterator i=lines.begin(); i!=lines.end(); ++i)
-                       i->width = static_cast<unsigned>(font.get_string_width(text.substr(i->start, i->bytes))*font_size);
+               for(Line &l: lines)
+                       l.width = static_cast<unsigned>(font.get_string_width(text.substr(l.start, l.bytes))*font_size);
        }
 }
 
 unsigned Text::get_width() const
 {
        unsigned width = 0;
-       for(vector<Line>::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<Line>::iterator i;
-       for(i=lines.begin(); (i!=lines.end() && i->start+i->bytes<pos); ++i) ;
+       auto i = find_if(lines, [pos](const Line &l){ return l.start+l.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<Line>::iterator i;
-               for(i=lines.begin(); (i!=lines.end() && i->start+i->bytes<pos); ++i) ;
+               auto i = find_if(lines, [pos](const Line &l){ return l.start+l.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<end; dec.decode_char(text, i))
                ++count;
@@ -302,7 +301,7 @@ unsigned Text::count_characters(unsigned start, unsigned bytes) const
 void Text::check_alignment(unsigned offs) const
 {
        StringCodec::Utf8::Decoder dec;
-       string::const_iterator i = text.begin()+offs;
+       auto i = text.begin()+offs;
        dec.decode_char(text, i);
 }
 
@@ -350,8 +349,8 @@ void Text::coords_to_geom_line(unsigned i, const Geometry &rgeom, CoordsToGeomDa
 {
        if(i==data.row)
        {
-               string::const_iterator begin = text.begin()+lines[i].start;
-               string::const_iterator j = begin;
+               auto begin = text.begin()+lines[i].start;
+               auto j = begin;
                if(lines[i].length==lines[i].bytes)
                        j += data.col;
                else
index daacd989af6999c9efb65385df569e2d063b2da3..bc40276baf6f1b35c5e6d769b1730eed93f3d4e0 100644 (file)
@@ -40,9 +40,8 @@ void Toggle::set_exclusive(bool e)
 
 void Toggle::exclude_siblings()
 {
-       const list<Widget *> &siblings = parent->get_children();
-       for(list<Widget *>::const_iterator i=siblings.begin(); i!=siblings.end(); ++i)
-               if(Toggle *tgl = dynamic_cast<Toggle *>(*i))
+       for(Widget *w: parent->get_children())
+               if(Toggle *tgl = dynamic_cast<Toggle *>(w))
                        if(tgl!=this && tgl->get_exclusive() && tgl->get_value())
                                tgl->set_value(false);
 }
index 4ef1b46358fcd18fde457b5051ea89131e02331e..6ac4d2aa7d7679bc39512614ff32705f835891b6 100644 (file)
@@ -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);
        }
 }