# HG changeset patch # User Chris Cannam # Date 1585912322 -3600 # Node ID 7c92c644db2093ae82547ad2daf221ce0753055c # Parent 498ed1e86f9228a09e2057598378dfd43c792b7d# Parent 5f8fbbde08ff503357144e68d925597fa0ead9b4 Merge from branch audio-source-refactor. Various changes to memory management for plugins, ring buffers etc, for safer code further up the stack diff -r 5f8fbbde08ff -r 7c92c644db20 base/Command.h --- a/base/Command.h Fri Mar 20 16:30:33 2020 +0000 +++ b/base/Command.h Fri Apr 03 12:12:02 2020 +0100 @@ -19,6 +19,7 @@ #include #include #include +#include #include "Debug.h" @@ -32,6 +33,56 @@ virtual QString getName() const = 0; }; +/** + * GenericCommand is a Command that can be constructed directly using + * lambdas, without having to create a subclass. Best for commands + * invoked only in a single place, and where little state is involved. + */ +class GenericCommand : public Command +{ +public: + GenericCommand(QString name, + std::function execute, + std::function unexecute) : + m_name(name), + m_execute(execute), + m_unexecute(unexecute), + m_onDelete([] {}) { + } + + GenericCommand(QString name, + std::function execute, + std::function unexecute, + std::function onDelete) : + m_name(name), + m_execute(execute), + m_unexecute(unexecute), + m_onDelete(onDelete) { + } + + virtual ~GenericCommand() { + m_onDelete(); + } + + QString getName() const override { + return m_name; + } + + void execute() override { + m_execute(); + } + + void unexecute() override { + m_unexecute(); + } + +private: + QString m_name; + std::function m_execute; + std::function m_unexecute; + std::function m_onDelete; +}; + class MacroCommand : public Command { public: