Skip to content

Commit 56cb940

Browse files
committed
actions: use shared_ptr to store actions
1 parent 59197f2 commit 56cb940

8 files changed

Lines changed: 47 additions & 35 deletions

File tree

src/actions.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ namespace ph = std::placeholders;
7676

7777
namespace {
7878

79-
std::vector<std::unique_ptr<Actions::BaseAction>> AvailableActions;
79+
std::vector<std::shared_ptr<Actions::BaseAction>> AvailableActions;
8080

8181
void populateActions();
8282

@@ -281,22 +281,26 @@ BaseAction &get(Actions::Type at)
281281
{
282282
if (AvailableActions.empty())
283283
populateActions();
284-
BaseAction *action = AvailableActions.at(static_cast<size_t>(at)).get();
285-
// action should be always present if action type in queried
286-
assert(action != nullptr);
287-
return *action;
284+
return *AvailableActions.at(static_cast<size_t>(at));
288285
}
289286

290-
BaseAction *get(const std::string &name)
287+
std::shared_ptr<BaseAction> get_(Actions::Type at)
291288
{
292-
BaseAction *result = nullptr;
289+
if (AvailableActions.empty())
290+
populateActions();
291+
return AvailableActions.at(static_cast<size_t>(at));
292+
}
293+
294+
std::shared_ptr<BaseAction> get_(const std::string &name)
295+
{
296+
std::shared_ptr<BaseAction> result;
293297
if (AvailableActions.empty())
294298
populateActions();
295299
for (const auto &action : AvailableActions)
296300
{
297301
if (action->name() == name)
298302
{
299-
result = action.get();
303+
result = action;
300304
break;
301305
}
302306
}

src/actions.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ struct BaseAction
216216
};
217217

218218
BaseAction &get(Type at);
219-
BaseAction *get(const std::string &name);
219+
220+
std::shared_ptr<BaseAction> get_(Type at);
221+
std::shared_ptr<BaseAction> get_(const std::string &name);
220222

221223
struct Dummy: BaseAction
222224
{

src/bindings.cpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,13 @@ NC::Key::Type stringToKey(const std::string &s)
134134
}
135135

136136
template <typename F>
137-
Actions::BaseAction *parseActionLine(const std::string &line, F error)
137+
std::shared_ptr<Actions::BaseAction> parseActionLine(const std::string &line, F error)
138138
{
139-
Actions::BaseAction *result = 0;
139+
std::shared_ptr<Actions::BaseAction> result;
140140
size_t i = 0;
141141
for (; i < line.size() && !isspace(line[i]); ++i) { }
142142
if (i == line.size()) // only action name
143-
result = Actions::get(line);
143+
result = Actions::get_(line);
144144
else // there is something else
145145
{
146146
std::string action_name = line.substr(0, i);
@@ -149,9 +149,11 @@ Actions::BaseAction *parseActionLine(const std::string &line, F error)
149149
// push single character into input queue
150150
std::string arg = getEnclosedString(line, '"', '"', 0);
151151
NC::Key::Type k = stringToSpecialKey(arg);
152-
auto queue = std::vector<NC::Key::Type>{ k };
153152
if (k != NC::Key::None)
154-
result = new Actions::PushCharacters(&Global::wFooter, std::move(queue));
153+
result = std::static_pointer_cast<Actions::BaseAction>(
154+
std::make_shared<Actions::PushCharacters>(
155+
&Global::wFooter,
156+
std::vector<NC::Key::Type>{k}));
155157
else
156158
error() << "invalid character passed to push_character: '" << arg << "'\n";
157159
}
@@ -161,11 +163,13 @@ Actions::BaseAction *parseActionLine(const std::string &line, F error)
161163
std::string arg = getEnclosedString(line, '"', '"', 0);
162164
if (!arg.empty())
163165
{
164-
std::vector<NC::Key::Type> queue(arg.begin(), arg.end());
165166
// if char is signed, erase 1s from char -> int conversion
166167
for (auto it = arg.begin(); it != arg.end(); ++it)
167168
*it &= 0xff;
168-
result = new Actions::PushCharacters(&Global::wFooter, std::move(queue));
169+
result = std::static_pointer_cast<Actions::BaseAction>(
170+
std::make_shared<Actions::PushCharacters>(
171+
&Global::wFooter,
172+
std::vector<NC::Key::Type>{arg.begin(), arg.end()}));
169173
}
170174
else
171175
error() << "empty argument passed to push_characters\n";
@@ -176,25 +180,28 @@ Actions::BaseAction *parseActionLine(const std::string &line, F error)
176180
std::string arg = getEnclosedString(line, '"', '"', 0);
177181
ScreenType screen_type = stringToScreenType(arg);
178182
if (screen_type != ScreenType::Unknown)
179-
result = new Actions::RequireScreen(screen_type);
183+
result = std::static_pointer_cast<Actions::BaseAction>(
184+
std::make_shared<Actions::RequireScreen>(screen_type));
180185
else
181186
error() << "unknown screen passed to require_screen: '" << arg << "'\n";
182187
}
183188
else if (action_name == "require_runnable")
184189
{
185190
// require that given action is runnable
186191
std::string arg = getEnclosedString(line, '"', '"', 0);
187-
auto action = Actions::get(arg);
192+
auto action = Actions::get_(arg);
188193
if (action)
189-
result = new Actions::RequireRunnable(action);
194+
result = std::static_pointer_cast<Actions::BaseAction>(
195+
std::make_shared<Actions::RequireRunnable>(action));
190196
else
191197
error() << "unknown action passed to require_runnable: '" << arg << "'\n";
192198
}
193199
else if (action_name == "run_external_command")
194200
{
195201
std::string command = getEnclosedString(line, '"', '"', 0);
196202
if (!command.empty())
197-
result = new Actions::RunExternalCommand(std::move(command));
203+
result = std::static_pointer_cast<Actions::BaseAction>(
204+
std::make_shared<Actions::RunExternalCommand>(std::move(command)));
198205
else
199206
error() << "empty command passed to run_external_command\n";
200207
}
@@ -472,11 +479,11 @@ void BindingsConfiguration::generateDefaults()
472479
if (notBound(k = stringToKey("up")))
473480
bind(k, Actions::Type::ScrollUp);
474481
if (notBound(k = stringToKey("shift-up")))
475-
bind(k, Binding::ActionChain({ &Actions::get(Actions::Type::SelectItem), &Actions::get(Actions::Type::ScrollUp) }));
482+
bind(k, Binding::ActionChain({Actions::get_(Actions::Type::SelectItem), Actions::get_(Actions::Type::ScrollUp)}));
476483
if (notBound(k = stringToKey("down")))
477484
bind(k, Actions::Type::ScrollDown);
478485
if (notBound(k = stringToKey("shift-down")))
479-
bind(k, Binding::ActionChain({ &Actions::get(Actions::Type::SelectItem), &Actions::get(Actions::Type::ScrollDown) }));
486+
bind(k, Binding::ActionChain({Actions::get_(Actions::Type::SelectItem), Actions::get_(Actions::Type::ScrollDown)}));
480487
if (notBound(k = stringToKey("[")))
481488
bind(k, Actions::Type::ScrollUpAlbum);
482489
if (notBound(k = stringToKey("]")))

src/bindings.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ std::wstring keyToWString(const NC::Key::Type key);
3333
/// Represents either single action or chain of actions bound to a certain key
3434
struct Binding
3535
{
36-
typedef std::vector<Actions::BaseAction *> ActionChain;
36+
typedef std::vector<std::shared_ptr<Actions::BaseAction>> ActionChain;
3737

3838
template <typename ArgT>
3939
Binding(ArgT &&actions_)
4040
: m_actions(std::forward<ArgT>(actions_)) {
4141
assert(!m_actions.empty());
4242
}
4343
Binding(Actions::Type at)
44-
: Binding(ActionChain({&Actions::get(at)})) { }
44+
: Binding(ActionChain({Actions::get_(at)})) { }
4545

4646
bool execute() const {
4747
return std::all_of(m_actions.begin(), m_actions.end(),
@@ -53,9 +53,10 @@ struct Binding
5353
return m_actions.size() == 1;
5454
}
5555

56-
Actions::BaseAction *action() const {
56+
Actions::BaseAction &action() const {
5757
assert(isSingle());
58-
return m_actions[0];
58+
assert(m_actions[0] != nullptr);
59+
return *m_actions[0];
5960
}
6061

6162
const ActionChain &actions() const {

src/help.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ std::string display_keys(const Actions::Type at)
6060
{
6161
for (auto j = it->second.begin(); j != it->second.end(); ++j)
6262
{
63-
if (j->isSingle() && j->action()->type() == at)
63+
if (j->isSingle() && j->action().type() == at)
6464
{
6565
skey = keyToWString(it->first);
6666
if (!skey.empty())

src/macro_utilities.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ void PushCharacters::run()
4646
(*m_window)->pushChar(*it);
4747
}
4848

49-
RequireRunnable::RequireRunnable(BaseAction *action)
49+
RequireRunnable::RequireRunnable(std::shared_ptr<BaseAction> action)
5050
: BaseAction(Type::MacroUtility, "require_runnable")
51-
, m_action(action)
51+
, m_action(std::move(action))
5252
{
5353
assert(m_action != nullptr);
5454
m_name += " \"";
@@ -75,7 +75,7 @@ bool RequireScreen::canBeRun()
7575
return Global::myScreen->type() == m_screen_type;
7676
}
7777

78-
RunExternalCommand::RunExternalCommand(std::string command)
78+
RunExternalCommand::RunExternalCommand(std::string &&command)
7979
: BaseAction(Type::MacroUtility, "run_external_command")
8080
, m_command(std::move(command))
8181
{

src/macro_utilities.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ struct PushCharacters: BaseAction
4040

4141
struct RequireRunnable: BaseAction
4242
{
43-
RequireRunnable(BaseAction *action);
43+
RequireRunnable(std::shared_ptr<BaseAction> action);
4444

4545
private:
4646
virtual bool canBeRun() override;
4747
virtual void run() override { }
4848

49-
BaseAction *m_action;
49+
std::shared_ptr<BaseAction> m_action;
5050
};
5151

5252
struct RequireScreen: BaseAction
@@ -62,7 +62,7 @@ struct RequireScreen: BaseAction
6262

6363
struct RunExternalCommand: BaseAction
6464
{
65-
RunExternalCommand(std::string command);
65+
RunExternalCommand(std::string &&command);
6666

6767
private:
6868
virtual void run() override;

src/ncmpcpp.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,6 @@ int main(int argc, char **argv)
207207
if (!key_pressed)
208208
continue;
209209

210-
//Statusbar::print(ToString(keyToWString(input)));
211-
212210
// The reason we want to update timer here is that if the timer is updated
213211
// in Status::trace, then Key::read usually blocks for 500ms and if key is
214212
// pressed 400ms after Key::read was called, we end up with Timer that is

0 commit comments

Comments
 (0)