Hooks: C++11

This commit is contained in:
Paul Beckingham 2016-02-03 21:06:49 -05:00
parent c25f312477
commit cdd1c4681d
2 changed files with 25 additions and 38 deletions

View file

@ -45,13 +45,6 @@
extern Context context; extern Context context;
////////////////////////////////////////////////////////////////////////////////
Hooks::Hooks ()
: _enabled (true)
, _debug (0)
{
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void Hooks::initialize () void Hooks::initialize ()
{ {
@ -111,7 +104,7 @@ bool Hooks::enable (bool value)
// - all emitted non-JSON lines are considered feedback or error messages // - all emitted non-JSON lines are considered feedback or error messages
// depending on the status code. // depending on the status code.
// //
void Hooks::onLaunch () void Hooks::onLaunch () const
{ {
if (! _enabled) if (! _enabled)
return; return;
@ -164,7 +157,7 @@ void Hooks::onLaunch ()
// - all emitted non-JSON lines are considered feedback or error messages // - all emitted non-JSON lines are considered feedback or error messages
// depending on the status code. // depending on the status code.
// //
void Hooks::onExit () void Hooks::onExit () const
{ {
if (! _enabled) if (! _enabled)
return; return;
@ -226,7 +219,7 @@ void Hooks::onExit ()
// - all emitted non-JSON lines are considered feedback or error messages // - all emitted non-JSON lines are considered feedback or error messages
// depending on the status code. // depending on the status code.
// //
void Hooks::onAdd (Task& task) void Hooks::onAdd (Task& task) const
{ {
if (! _enabled) if (! _enabled)
return; return;
@ -292,7 +285,7 @@ void Hooks::onAdd (Task& task)
// - all emitted non-JSON lines are considered feedback or error messages // - all emitted non-JSON lines are considered feedback or error messages
// depending on the status code. // depending on the status code.
// //
void Hooks::onModify (const Task& before, Task& after) void Hooks::onModify (const Task& before, Task& after) const
{ {
if (! _enabled) if (! _enabled)
return; return;
@ -346,16 +339,16 @@ void Hooks::onModify (const Task& before, Task& after)
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
std::vector <std::string> Hooks::list () std::vector <std::string> Hooks::list () const
{ {
return _scripts; return _scripts;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
std::vector <std::string> Hooks::scripts (const std::string& event) std::vector <std::string> Hooks::scripts (const std::string& event) const
{ {
std::vector <std::string> matching; std::vector <std::string> matching;
for (auto& i : _scripts) for (const auto& i : _scripts)
{ {
if (i.find ("/" + event) != std::string::npos) if (i.find ("/" + event) != std::string::npos)
{ {
@ -500,7 +493,7 @@ void Hooks::assertFeedback (const std::vector <std::string>& input) const
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
std::vector <std::string>& Hooks::buildHookScriptArgs (std::vector <std::string>& args) std::vector <std::string>& Hooks::buildHookScriptArgs (std::vector <std::string>& args) const
{ {
Variant v; Variant v;
@ -530,7 +523,7 @@ std::vector <std::string>& Hooks::buildHookScriptArgs (std::vector <std::string>
int Hooks::callHookScript ( int Hooks::callHookScript (
const std::string& script, const std::string& script,
const std::vector <std::string>& input, const std::vector <std::string>& input,
std::vector <std::string>& output) std::vector <std::string>& output) const
{ {
if (_debug >= 1) if (_debug >= 1)
context.debug ("Hook: Calling " + script); context.debug ("Hook: Calling " + script);
@ -538,12 +531,12 @@ int Hooks::callHookScript (
if (_debug >= 2) if (_debug >= 2)
{ {
context.debug ("Hook: input"); context.debug ("Hook: input");
for (auto& i : input) for (const auto& i : input)
context.debug (" " + i); context.debug (" " + i);
} }
std::string inputStr; std::string inputStr;
for (auto& i : input) for (const auto& i : input)
inputStr += i + "\n"; inputStr += i + "\n";
std::vector <std::string> args; std::vector <std::string> args;
@ -551,7 +544,7 @@ int Hooks::callHookScript (
if (_debug >= 2) if (_debug >= 2)
{ {
context.debug ("Hooks: args"); context.debug ("Hooks: args");
for (auto& arg: args) for (const auto& arg: args)
context.debug (" " + arg); context.debug (" " + arg);
} }
@ -573,7 +566,7 @@ int Hooks::callHookScript (
if (_debug >= 2) if (_debug >= 2)
{ {
context.debug ("Hook: output"); context.debug ("Hook: output");
for (auto& i : output) for (const auto& i : output)
if (i != "") if (i != "")
context.debug (" " + i); context.debug (" " + i);

View file

@ -34,36 +34,30 @@
class Hooks class Hooks
{ {
public: public:
Hooks (); Hooks () = default;
Hooks (const Hooks&) = delete;
Hooks& operator= (const Hooks&) = delete;
void initialize (); void initialize ();
bool enable (bool); bool enable (bool);
void onLaunch () const;
void onLaunch (); void onExit () const;
void onExit (); void onAdd (Task&) const;
void onAdd (Task&); void onModify (const Task&, Task&) const;
void onModify (const Task&, Task&); std::vector <std::string> list () const;
std::vector <std::string> list ();
private: private:
std::vector <std::string> scripts (const std::string&); std::vector <std::string> scripts (const std::string&) const;
void separateOutput (const std::vector <std::string>&, std::vector <std::string>&, std::vector <std::string>&) const; void separateOutput (const std::vector <std::string>&, std::vector <std::string>&, std::vector <std::string>&) const;
bool isJSON (const std::string&) const; bool isJSON (const std::string&) const;
void assertValidJSON (const std::vector <std::string>&) const; void assertValidJSON (const std::vector <std::string>&) const;
void assertNTasks (const std::vector <std::string>&, unsigned int) const; void assertNTasks (const std::vector <std::string>&, unsigned int) const;
void assertSameTask (const std::vector <std::string>&, const Task&) const; void assertSameTask (const std::vector <std::string>&, const Task&) const;
void assertFeedback (const std::vector <std::string>&) const; void assertFeedback (const std::vector <std::string>&) const;
std::vector <std::string>& buildHookScriptArgs (std::vector <std::string>&); std::vector <std::string>& buildHookScriptArgs (std::vector <std::string>&) const;
int callHookScript (const std::string&, const std::vector <std::string>&, std::vector <std::string>&); int callHookScript (const std::string&, const std::vector <std::string>&, std::vector <std::string>&) const;
private: private:
bool _enabled; bool _enabled {true};
int _debug; int _debug {0};
std::vector <std::string> _scripts; std::vector <std::string> _scripts {};
}; };
#endif #endif
////////////////////////////////////////////////////////////////////////////////