C++11: Cleaned up column code wiht range-based for

This commit is contained in:
Paul Beckingham 2015-05-11 17:33:20 -04:00
parent 5b675ea834
commit bd3d58484a
7 changed files with 48 additions and 69 deletions

View file

@ -99,18 +99,17 @@ void ColumnDepends::measure (Task& task, unsigned int& minimum, unsigned int& ma
if (task.has ("depends"))
{
std::vector <int> blocking_ids;
std::vector <Task>::iterator i;
for (i = blocking.begin (); i != blocking.end (); ++i)
blocking_ids.push_back (i->id);
for (auto& i : blocking)
blocking_ids.push_back (i.id);
std::string all;
join (all, " ", blocking_ids);
maximum = all.length ();
unsigned int length;
for (i = blocking.begin (); i != blocking.end (); ++i)
for (auto& i : blocking)
{
length = format (i->id).length ();
length = format (i.id).length ();
if (length > minimum)
minimum = length;
}
@ -150,9 +149,8 @@ void ColumnDepends::render (
_style == "list")
{
std::vector <int> blocking_ids;
std::vector <Task>::iterator t;
for (t = blocking.begin (); t != blocking.end (); ++t)
blocking_ids.push_back (t->id);
for (auto& t : blocking)
blocking_ids.push_back (t.id);
std::string combined;
join (combined, " ", blocking_ids);
@ -160,9 +158,8 @@ void ColumnDepends::render (
std::vector <std::string> all;
wrapText (all, combined, width, _hyphenate);
std::vector <std::string>::iterator i;
for (i = all.begin (); i != all.end (); ++i)
lines.push_back (color.colorize (leftJustify (*i, width)));
for (auto& i : all)
lines.push_back (color.colorize (leftJustify (i, width)));
}
}
}

View file

@ -116,10 +116,9 @@ void ColumnDescription::measure (Task& task, unsigned int& minimum, unsigned int
std::map <std::string, std::string> annos;
task.getAnnotations (annos);
std::map <std::string, std::string>::iterator i;
for (i = annos.begin (); i != annos.end (); i++)
for (auto& i : annos)
{
unsigned int len = min_anno + 1 + utf8_width (i->second);
unsigned int len = min_anno + 1 + utf8_width (i.second);
if (len > maximum)
maximum = len;
}
@ -144,9 +143,8 @@ void ColumnDescription::measure (Task& task, unsigned int& minimum, unsigned int
unsigned int min_anno = Date::length (_dateformat);
std::map <std::string, std::string> annos;
task.getAnnotations (annos);
std::map <std::string, std::string>::iterator i;
for (i = annos.begin (); i != annos.end (); i++)
maximum += min_anno + 1 + utf8_width (i->second);
for (auto& i : annos)
maximum += min_anno + 1 + utf8_width (i.second);
}
}
@ -195,20 +193,18 @@ void ColumnDescription::render (
task.getAnnotations (annos);
if (annos.size ())
{
std::map <std::string, std::string>::iterator i;
for (i = annos.begin (); i != annos.end (); i++)
for (auto& i : annos)
{
Date dt (strtol (i->first.substr (11).c_str (), NULL, 10));
description += "\n" + std::string (_indent, ' ') + dt.toString (_dateformat) + " " + i->second;
Date dt (strtol (i.first.substr (11).c_str (), NULL, 10));
description += "\n" + std::string (_indent, ' ') + dt.toString (_dateformat) + " " + i.second;
}
}
std::vector <std::string> raw;
wrapText (raw, description, width, _hyphenate);
std::vector <std::string>::iterator i;
for (i = raw.begin (); i != raw.end (); ++i)
lines.push_back (color.colorize (leftJustify (*i, width)));
for (auto& i : raw)
lines.push_back (color.colorize (leftJustify (i, width)));
}
// This is a description
@ -217,9 +213,8 @@ void ColumnDescription::render (
std::vector <std::string> raw;
wrapText (raw, description, width, _hyphenate);
std::vector <std::string>::iterator i;
for (i = raw.begin (); i != raw.end (); ++i)
lines.push_back (color.colorize (leftJustify (*i, width)));
for (auto& i : raw)
lines.push_back (color.colorize (leftJustify (i, width)));
}
// This is a description <date> <anno> ...
@ -229,20 +224,18 @@ void ColumnDescription::render (
task.getAnnotations (annos);
if (annos.size ())
{
std::map <std::string, std::string>::iterator i;
for (i = annos.begin (); i != annos.end (); i++)
for (auto& i : annos)
{
Date dt (atoi (i->first.substr (11).c_str ()));
description += " " + dt.toString (_dateformat) + " " + i->second;
Date dt (atoi (i.first.substr (11).c_str ()));
description += " " + dt.toString (_dateformat) + " " + i.second;
}
}
std::vector <std::string> raw;
wrapText (raw, description, width, _hyphenate);
std::vector <std::string>::iterator i;
for (i = raw.begin (); i != raw.end (); ++i)
lines.push_back (color.colorize (leftJustify (*i, width)));
for (auto& i : raw)
lines.push_back (color.colorize (leftJustify (i, width)));
}
// This is a des...
@ -267,9 +260,8 @@ void ColumnDescription::render (
std::vector <std::string> raw;
wrapText (raw, description, width, _hyphenate);
std::vector <std::string>::iterator i;
for (i = raw.begin (); i != raw.end (); ++i)
lines.push_back (color.colorize (leftJustify (*i, width)));
for (auto& i : raw)
lines.push_back (color.colorize (leftJustify (i, width)));
}
// This is a des... [2]
@ -292,7 +284,6 @@ void ColumnDescription::render (
lines.push_back (color.colorize (description.substr (0, width - len_annos - 3) + "..." + annos_count));
else
lines.push_back (color.colorize (leftJustify (description + annos_count, width)));
}
}

View file

@ -118,9 +118,8 @@ void ColumnProject::render (
std::vector <std::string> raw;
wrapText (raw, project, width, _hyphenate);
std::vector <std::string>::iterator i;
for (i = raw.begin (); i != raw.end (); ++i)
lines.push_back (color.colorize (leftJustify (*i, width)));
for (auto& i : raw)
lines.push_back (color.colorize (leftJustify (i, width)));
}
}

View file

@ -100,18 +100,16 @@ void ColumnString::render (
std::vector <std::string> raw;
wrapText (raw, value, width, _hyphenate);
std::vector <std::string>::iterator i;
for (i = raw.begin (); i != raw.end (); ++i)
lines.push_back (color.colorize (leftJustify (*i, width)));
for (auto& i : raw)
lines.push_back (color.colorize (leftJustify (i, width)));
}
else if (_style == "right")
{
std::vector <std::string> raw;
wrapText (raw, value, width, _hyphenate);
std::vector <std::string>::iterator i;
for (i = raw.begin (); i != raw.end (); ++i)
lines.push_back (color.colorize (rightJustify (*i, width)));
for (auto& i : raw)
lines.push_back (color.colorize (rightJustify (i, width)));
}
else if (_style == "left_fixed")
{

View file

@ -107,10 +107,9 @@ void ColumnTags::measure (Task& task, unsigned int& minimum, unsigned int& maxim
{
std::vector <std::string> all;
split (all, tags, ',');
std::vector <std::string>::iterator i;
for (i = all.begin (); i != all.end (); ++i)
for (auto& i : all)
{
unsigned int length = utf8_width (*i);
unsigned int length = utf8_width (i);
if (length > minimum)
minimum = length;
}
@ -145,9 +144,8 @@ void ColumnTags::render (
std::vector <std::string> all;
wrapText (all, tags, width, _hyphenate);
std::vector <std::string>::iterator i;
for (i = all.begin (); i != all.end (); ++i)
lines.push_back (color.colorize (leftJustify (*i, width)));
for (auto& i : all)
lines.push_back (color.colorize (leftJustify (i, width)));
}
else if (_style == "indicator")
{

View file

@ -64,9 +64,8 @@ bool ColumnUDA::validate (std::string& value)
return true;
// Look for exact match value.
std::vector <std::string>::iterator i;
for (i = _values.begin (); i != _values.end (); ++i)
if (*i == value)
for (auto& i : _values)
if (i == value)
return true;
// Fail if not found.
@ -171,9 +170,8 @@ void ColumnUDA::render (
std::vector <std::string> raw;
wrapText (raw, value, width, _hyphenate);
std::vector <std::string>::iterator i;
for (i = raw.begin (); i != raw.end (); ++i)
lines.push_back (color.colorize (leftJustify (*i, width)));
for (auto& i : raw)
lines.push_back (color.colorize (leftJustify (i, width)));
}
else if (_type == "numeric")
{

View file

@ -149,24 +149,22 @@ void Column::uda (std::map <std::string, Column*>& all)
// For each UDA, instantiate and initialize ColumnUDA().
std::map <std::string, int> udas;
Config::const_iterator i;
for (i = context.config.begin (); i != context.config.end (); ++i)
for (auto& i : context.config)
{
if (i->first.substr (0, 4) == "uda.")
if (i.first.substr (0, 4) == "uda.")
{
std::string::size_type period = 4;
if ((period = i->first.find ('.', period)) != std::string::npos)
udas[i->first.substr (4, period - 4)] = 0;
if ((period = i.first.find ('.', period)) != std::string::npos)
udas[i.first.substr (4, period - 4)] = 0;
}
}
std::map <std::string, int>::iterator uda;
for (uda = udas.begin (); uda != udas.end (); ++uda)
for (auto& uda : udas)
{
if (all.find (uda->first) != all.end ())
throw format (STRING_UDA_COLLISION, uda->first);
if (all.find (uda.first) != all.end ())
throw format (STRING_UDA_COLLISION, uda.first);
Column* c = Column::uda (uda->first);
Column* c = Column::uda (uda.first);
all[c->_name] = c;
}
}