C++11: Cleaned up program framework with range-based for

This commit is contained in:
Paul Beckingham 2015-05-11 17:50:53 -04:00
parent 5a57dfd70d
commit e74c6963a9
28 changed files with 937 additions and 1221 deletions

View file

@ -73,18 +73,17 @@ void Hooks::initialize ()
if (_debug >= 1)
{
std::vector <std::string>::iterator i;
for (i = _scripts.begin (); i != _scripts.end (); ++i)
for (auto& i : _scripts)
{
Path p (*i);
Path p (i);
std::string name = p.name ();
if (name.substr (0, 6) == "on-add" ||
name.substr (0, 9) == "on-modify" ||
name.substr (0, 9) == "on-launch" ||
name.substr (0, 7) == "on-exit")
context.debug ("Found hook script " + *i);
context.debug ("Found hook script " + i);
else
context.debug ("Found misnamed hook script " + *i);
context.debug ("Found misnamed hook script " + i);
}
}
}
@ -124,12 +123,11 @@ void Hooks::onLaunch ()
std::vector <std::string> matchingScripts = scripts ("on-launch");
if (matchingScripts.size ())
{
std::vector <std::string>::iterator script;
for (script = matchingScripts.begin (); script != matchingScripts.end (); ++script)
for (auto& script : matchingScripts)
{
std::vector <std::string> input;
std::vector <std::string> output;
int status = callHookScript (*script, input, output);
int status = callHookScript (script, input, output);
std::vector <std::string> outputJSON;
std::vector <std::string> outputFeedback;
@ -139,17 +137,14 @@ void Hooks::onLaunch ()
if (status == 0)
{
std::vector <std::string>::iterator message;
for (message = outputFeedback.begin (); message != outputFeedback.end (); ++message)
context.footnote (*message);
for (auto& message : outputFeedback)
context.footnote (message);
}
else
{
assertFeedback (outputFeedback);
std::vector <std::string>::iterator message;
for (message = outputFeedback.begin (); message != outputFeedback.end (); ++message)
context.error (*message);
for (auto& message : outputFeedback)
context.error (message);
throw 0; // This is how hooks silently terminate processing.
}
@ -187,16 +182,14 @@ void Hooks::onExit ()
// Convert to a vector of strings.
std::vector <std::string> input;
std::vector <Task>::const_iterator t;
for (t = tasks.begin (); t != tasks.end (); ++t)
input.push_back (t->composeJSON ());
for (auto& t : tasks)
input.push_back (t.composeJSON ());
// Call the hook scripts, with the invariant input.
std::vector <std::string>::iterator script;
for (script = matchingScripts.begin (); script != matchingScripts.end (); ++script)
for (auto& script : matchingScripts)
{
std::vector <std::string> output;
int status = callHookScript (*script, input, output);
int status = callHookScript (script, input, output);
std::vector <std::string> outputJSON;
std::vector <std::string> outputFeedback;
@ -206,17 +199,14 @@ void Hooks::onExit ()
if (status == 0)
{
std::vector <std::string>::iterator message;
for (message = outputFeedback.begin (); message != outputFeedback.end (); ++message)
context.footnote (*message);
for (auto& message : outputFeedback)
context.footnote (message);
}
else
{
assertFeedback (outputFeedback);
std::vector <std::string>::iterator message;
for (message = outputFeedback.begin (); message != outputFeedback.end (); ++message)
context.error (*message);
for (auto& message : outputFeedback)
context.error (message);
throw 0; // This is how hooks silently terminate processing.
}
@ -253,11 +243,10 @@ void Hooks::onAdd (Task& task)
input.push_back (task.composeJSON ());
// Call the hook scripts.
std::vector <std::string>::iterator script;
for (script = matchingScripts.begin (); script != matchingScripts.end (); ++script)
for (auto& script : matchingScripts)
{
std::vector <std::string> output;
int status = callHookScript (*script, input, output);
int status = callHookScript (script, input, output);
std::vector <std::string> outputJSON;
std::vector <std::string> outputFeedback;
@ -272,17 +261,14 @@ void Hooks::onAdd (Task& task)
// Propagate forward to the next script.
input[0] = outputJSON[0];
std::vector <std::string>::iterator message;
for (message = outputFeedback.begin (); message != outputFeedback.end (); ++message)
context.footnote (*message);
for (auto& message : outputFeedback)
context.footnote (message);
}
else
{
assertFeedback (outputFeedback);
std::vector <std::string>::iterator message;
for (message = outputFeedback.begin (); message != outputFeedback.end (); ++message)
context.error (*message);
for (auto& message : outputFeedback)
context.error (message);
throw 0; // This is how hooks silently terminate processing.
}
@ -324,11 +310,10 @@ void Hooks::onModify (const Task& before, Task& after)
input.push_back (after.composeJSON ()); // [line 1] modified
// Call the hook scripts.
std::vector <std::string>::iterator script;
for (script = matchingScripts.begin (); script != matchingScripts.end (); ++script)
for (auto& script : matchingScripts)
{
std::vector <std::string> output;
int status = callHookScript (*script, input, output);
int status = callHookScript (script, input, output);
std::vector <std::string> outputJSON;
std::vector <std::string> outputFeedback;
@ -343,17 +328,14 @@ void Hooks::onModify (const Task& before, Task& after)
// Propagate accepted changes forward to the next script.
input[1] = outputJSON[0];
std::vector <std::string>::iterator message;
for (message = outputFeedback.begin (); message != outputFeedback.end (); ++message)
context.footnote (*message);
for (auto& message : outputFeedback)
context.footnote (message);
}
else
{
assertFeedback (outputFeedback);
std::vector <std::string>::iterator message;
for (message = outputFeedback.begin (); message != outputFeedback.end (); ++message)
context.error (*message);
for (auto& message : outputFeedback)
context.error (message);
throw 0; // This is how hooks silently terminate processing.
}
@ -375,14 +357,13 @@ std::vector <std::string> Hooks::list ()
std::vector <std::string> Hooks::scripts (const std::string& event)
{
std::vector <std::string> matching;
std::vector <std::string>::iterator i;
for (i = _scripts.begin (); i != _scripts.end (); ++i)
for (auto& i : _scripts)
{
if (i->find ("/" + event) != std::string::npos)
if (i.find ("/" + event) != std::string::npos)
{
File script (*i);
File script (i);
if (script.executable ())
matching.push_back (*i);
matching.push_back (i);
}
}
@ -395,13 +376,12 @@ void Hooks::separateOutput (
std::vector <std::string>& json,
std::vector <std::string>& feedback) const
{
std::vector <std::string>::const_iterator i;
for (i = output.begin (); i != output.end (); ++i)
for (auto& i : output)
{
if (isJSON (*i))
json.push_back (*i);
if (isJSON (i))
json.push_back (i);
else
feedback.push_back (*i);
feedback.push_back (i);
}
}
@ -416,12 +396,11 @@ bool Hooks::isJSON (const std::string& input) const
////////////////////////////////////////////////////////////////////////////////
void Hooks::assertValidJSON (const std::vector <std::string>& input) const
{
std::vector <std::string>::const_iterator i;
for (i = input.begin (); i != input.end (); i++)
for (auto& i : input)
{
if (i->length () < 3 ||
(*i)[0] != '{' ||
(*i)[i->length () - 1] != '}')
if (i.length () < 3 ||
i[0] != '{' ||
i[i.length () - 1] != '}')
{
context.error (STRING_HOOK_ERROR_OBJECT);
throw 0;
@ -429,7 +408,7 @@ void Hooks::assertValidJSON (const std::vector <std::string>& input) const
try
{
json::value* root = json::parse (*i);
json::value* root = json::parse (i);
if (root->type () != json::j_object)
{
context.error (STRING_HOOK_ERROR_OBJECT);
@ -451,7 +430,7 @@ void Hooks::assertValidJSON (const std::vector <std::string>& input) const
catch (const std::string& e)
{
context.error (format (STRING_HOOK_ERROR_SYNTAX, *i));
context.error (format (STRING_HOOK_ERROR_SYNTAX, i));
if (_debug)
context.error (STRING_HOOK_ERROR_JSON + e);
throw 0;
@ -459,7 +438,7 @@ void Hooks::assertValidJSON (const std::vector <std::string>& input) const
catch (...)
{
context.error (STRING_HOOK_ERROR_NOPARSE + *i);
context.error (STRING_HOOK_ERROR_NOPARSE + i);
throw 0;
}
}
@ -480,13 +459,12 @@ void Hooks::assertSameTask (const std::vector <std::string>& input, const Task&
{
std::string uuid = task.get ("uuid");
std::vector <std::string>::const_iterator i;
for (i = input.begin (); i != input.end (); i++)
for (auto& i : input)
{
json::object* root_obj = (json::object*)json::parse (*i);
json::object* root_obj = (json::object*)json::parse (i);
// If there is no UUID at all.
json_object_iter u = root_obj->_data.find ("uuid");
auto u = root_obj->_data.find ("uuid");
if (u == root_obj->_data.end () ||
u->second->type () != json::j_string)
{
@ -508,9 +486,8 @@ void Hooks::assertSameTask (const std::vector <std::string>& input, const Task&
void Hooks::assertFeedback (const std::vector <std::string>& input) const
{
bool foundSomething = false;
std::vector <std::string>::const_iterator i;
for (i = input.begin (); i != input.end (); ++i)
if (nontrivial (*i))
for (auto& i : input)
if (nontrivial (i))
foundSomething = true;
if (! foundSomething)
@ -559,22 +536,20 @@ int Hooks::callHookScript (
if (_debug >= 2)
{
context.debug ("Hook: input");
std::vector <std::string>::const_iterator i;
for (i = input.begin (); i != input.end (); ++i)
context.debug (" " + *i);
for (auto& i : input)
context.debug (" " + i);
}
std::string inputStr;
std::vector <std::string>::const_iterator i;
for (i = input.begin (); i != input.end (); ++i)
inputStr += *i + "\n";
for (auto& i : input)
inputStr += i + "\n";
std::vector <std::string> args;
buildHookScriptArgs (args);
if (_debug >= 2)
{
context.debug ("Hooks: args");
for (auto arg: args)
for (auto& arg: args)
context.debug (" " + arg);
}
@ -596,10 +571,9 @@ int Hooks::callHookScript (
if (_debug >= 2)
{
context.debug ("Hook: output");
std::vector <std::string>::iterator i;
for (i = output.begin (); i != output.end (); ++i)
if (*i != "")
context.debug (" " + *i);
for (auto& i : output)
if (i != "")
context.debug (" " + i);
context.debug (format ("Hook: Completed with status {1}", status));
context.debug (" "); // Blank line