Command.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2006 Chris Cannam.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #ifndef SV_COMMAND_H
17 #define SV_COMMAND_H
18 
19 #include <QObject>
20 #include <QString>
21 #include <vector>
22 #include <functional>
23 
24 #include "Debug.h"
25 
26 class Command
27 {
28 public:
29  virtual ~Command() { }
30 
31  virtual void execute() = 0;
32  virtual void unexecute() = 0;
33  virtual QString getName() const = 0;
34 };
35 
41 class GenericCommand : public Command
42 {
43 public:
44  GenericCommand(QString name,
45  std::function<void()> execute,
46  std::function<void()> unexecute) :
47  m_name(name),
48  m_execute(execute),
49  m_unexecute(unexecute),
50  m_onDelete([] {}) {
51  }
52 
53  GenericCommand(QString name,
54  std::function<void()> execute,
55  std::function<void()> unexecute,
56  std::function<void()> onDelete) :
57  m_name(name),
58  m_execute(execute),
59  m_unexecute(unexecute),
60  m_onDelete(onDelete) {
61  }
62 
63  virtual ~GenericCommand() {
64  m_onDelete();
65  }
66 
67  QString getName() const override {
68  return m_name;
69  }
70 
71  void execute() override {
72  m_execute();
73  }
74 
75  void unexecute() override {
76  m_unexecute();
77  }
78 
79 private:
80  QString m_name;
81  std::function<void()> m_execute;
82  std::function<void()> m_unexecute;
83  std::function<void()> m_onDelete;
84 };
85 
86 class MacroCommand : public Command
87 {
88 public:
89  MacroCommand(QString name);
90  virtual ~MacroCommand();
91 
92  virtual void addCommand(Command *command);
93  virtual void deleteCommand(Command *command);
94  virtual bool haveCommands() const;
95 
96  void execute() override;
97  void unexecute() override;
98 
99  QString getName() const override;
100  virtual void setName(QString name);
101 
102 protected:
103  QString m_name;
104  std::vector<Command *> m_commands;
105 };
106 
112 class BundleCommand : public QObject, public MacroCommand
113 {
114  Q_OBJECT
115 
116 public:
117  BundleCommand(QString name);
118  virtual ~BundleCommand();
119 
120  QString getName() const override;
121 };
122 
123 #endif
124 
void unexecute() override
Definition: Command.h:75
std::vector< Command * > m_commands
Definition: Command.h:104
virtual ~Command()
Definition: Command.h:29
std::function< void()> m_onDelete
Definition: Command.h:83
virtual void unexecute()=0
BundleCommand is a MacroCommand whose name includes a note of how many commands it contains...
Definition: Command.h:112
virtual QString getName() const =0
GenericCommand(QString name, std::function< void()> execute, std::function< void()> unexecute, std::function< void()> onDelete)
Definition: Command.h:53
std::function< void()> m_execute
Definition: Command.h:81
QString m_name
Definition: Command.h:103
GenericCommand is a Command that can be constructed directly using lambdas, without having to create ...
Definition: Command.h:41
QString m_name
Definition: Command.h:80
QString getName() const override
Definition: Command.h:67
virtual void execute()=0
GenericCommand(QString name, std::function< void()> execute, std::function< void()> unexecute)
Definition: Command.h:44
virtual ~GenericCommand()
Definition: Command.h:63
void execute() override
Definition: Command.h:71
std::function< void()> m_unexecute
Definition: Command.h:82