actions: use unique_ptr for storing actions

This commit is contained in:
Andrzej Rybczak
2016-11-23 20:58:00 +01:00
parent c422b993db
commit 59197f23d0
3 changed files with 17 additions and 13 deletions

View File

@@ -76,9 +76,7 @@ namespace ph = std::placeholders;
namespace { namespace {
boost::array< std::vector<std::unique_ptr<Actions::BaseAction>> AvailableActions;
Actions::BaseAction *, static_cast<size_t>(Actions::Type::_numberOfActions)
> AvailableActions;
void populateActions(); void populateActions();
@@ -281,9 +279,9 @@ bool isMPDMusicDirSet()
BaseAction &get(Actions::Type at) BaseAction &get(Actions::Type at)
{ {
if (AvailableActions[1] == nullptr) if (AvailableActions.empty())
populateActions(); populateActions();
BaseAction *action = AvailableActions[static_cast<size_t>(at)]; BaseAction *action = AvailableActions.at(static_cast<size_t>(at)).get();
// action should be always present if action type in queried // action should be always present if action type in queried
assert(action != nullptr); assert(action != nullptr);
return *action; return *action;
@@ -291,14 +289,14 @@ BaseAction &get(Actions::Type at)
BaseAction *get(const std::string &name) BaseAction *get(const std::string &name)
{ {
BaseAction *result = 0; BaseAction *result = nullptr;
if (AvailableActions[1] == nullptr) if (AvailableActions.empty())
populateActions(); 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; break;
} }
} }
@@ -2695,8 +2693,9 @@ namespace {
void populateActions() void populateActions()
{ {
AvailableActions.resize(static_cast<size_t>(Actions::Type::_numberOfActions));
auto insert_action = [](Actions::BaseAction *a) { auto insert_action = [](Actions::BaseAction *a) {
AvailableActions[static_cast<size_t>(a->type())] = a; AvailableActions.at(static_cast<size_t>(a->type())).reset(a);
}; };
insert_action(new Actions::Dummy()); insert_action(new Actions::Dummy());
insert_action(new Actions::UpdateEnvironment()); insert_action(new Actions::UpdateEnvironment());
@@ -2825,6 +2824,12 @@ void populateActions()
insert_action(new Actions::ShowVisualizer()); insert_action(new Actions::ShowVisualizer());
insert_action(new Actions::ShowClock()); insert_action(new Actions::ShowClock());
insert_action(new Actions::ShowServerInfo()); 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<std::string>(i));
}
} }
bool scrollTagCanBeRun(NC::List *&list, SongList *&songs) bool scrollTagCanBeRun(NC::List *&list, SongList *&songs)

View File

@@ -35,7 +35,7 @@ namespace Actions {
enum class Type enum class Type
{ {
MacroUtility = 0, MacroUtility = -1,
Dummy, Dummy,
UpdateEnvironment, UpdateEnvironment,
MouseEvent, MouseEvent,

View File

@@ -21,7 +21,6 @@
#ifndef NCMPCPP_LYRICS_H #ifndef NCMPCPP_LYRICS_H
#define NCMPCPP_LYRICS_H #define NCMPCPP_LYRICS_H
#include <boost/optional.hpp> #include <boost/optional.hpp>
#include <future> #include <future>
#include <memory> #include <memory>