From 59197f23d05f5e220eecad4c194dba4f52372e47 Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Wed, 23 Nov 2016 20:58:00 +0100 Subject: [PATCH] actions: use unique_ptr for storing actions --- src/actions.cpp | 27 ++++++++++++++++----------- src/actions.h | 2 +- src/lyrics.h | 1 - 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/actions.cpp b/src/actions.cpp index 5ee283f7..f68b83df 100644 --- a/src/actions.cpp +++ b/src/actions.cpp @@ -76,9 +76,7 @@ namespace ph = std::placeholders; namespace { -boost::array< - Actions::BaseAction *, static_cast(Actions::Type::_numberOfActions) -> AvailableActions; +std::vector> AvailableActions; void populateActions(); @@ -281,9 +279,9 @@ bool isMPDMusicDirSet() BaseAction &get(Actions::Type at) { - if (AvailableActions[1] == nullptr) + if (AvailableActions.empty()) populateActions(); - BaseAction *action = AvailableActions[static_cast(at)]; + BaseAction *action = AvailableActions.at(static_cast(at)).get(); // action should be always present if action type in queried assert(action != nullptr); return *action; @@ -291,14 +289,14 @@ BaseAction &get(Actions::Type at) BaseAction *get(const std::string &name) { - BaseAction *result = 0; - if (AvailableActions[1] == nullptr) + BaseAction *result = nullptr; + if (AvailableActions.empty()) populateActions(); - for (auto it = AvailableActions.begin(); it != AvailableActions.end(); ++it) + for (const auto &action : AvailableActions) { - if (*it != nullptr && (*it)->name() == name) + if (action->name() == name) { - result = *it; + result = action.get(); break; } } @@ -2695,8 +2693,9 @@ namespace { void populateActions() { + AvailableActions.resize(static_cast(Actions::Type::_numberOfActions)); auto insert_action = [](Actions::BaseAction *a) { - AvailableActions[static_cast(a->type())] = a; + AvailableActions.at(static_cast(a->type())).reset(a); }; insert_action(new Actions::Dummy()); insert_action(new Actions::UpdateEnvironment()); @@ -2825,6 +2824,12 @@ void populateActions() insert_action(new Actions::ShowVisualizer()); insert_action(new Actions::ShowClock()); insert_action(new Actions::ShowServerInfo()); + for (size_t i = 0; i < AvailableActions.size(); ++i) + { + if (AvailableActions[i] == nullptr) + throw std::logic_error("undefined action at position " + + boost::lexical_cast(i)); + } } bool scrollTagCanBeRun(NC::List *&list, SongList *&songs) diff --git a/src/actions.h b/src/actions.h index bfe32934..bde3cefb 100644 --- a/src/actions.h +++ b/src/actions.h @@ -35,7 +35,7 @@ namespace Actions { enum class Type { - MacroUtility = 0, + MacroUtility = -1, Dummy, UpdateEnvironment, MouseEvent, diff --git a/src/lyrics.h b/src/lyrics.h index d891a4c6..c2267f21 100644 --- a/src/lyrics.h +++ b/src/lyrics.h @@ -21,7 +21,6 @@ #ifndef NCMPCPP_LYRICS_H #define NCMPCPP_LYRICS_H - #include #include #include