Audacity  2.2.0
CommandFunctors.h
Go to the documentation of this file.
1 //
2 // CommandFunctors.h
3 // Audacity
4 //
5 // Created by Paul Licameli on 4/22/16.
6 //
7 //
8 
9 #ifndef __AUDACITY_COMMAND_FUNCTORS__
10 #define __AUDACITY_COMMAND_FUNCTORS__
11 
12 #include <wx/string.h>
13 #include <wx/event.h>
14 #include "../MemoryX.h"
15 
16 class wxEvent;
17 typedef wxString PluginID;
18 
19 class AUDACITY_DLL_API CommandFunctor /* not final */
20 {
21 public:
23  virtual ~CommandFunctor(){};
24  virtual void operator()(int index, const wxEvent *e) = 0;
25 };
26 
27 using CommandFunctorPointer = std::shared_ptr <CommandFunctor>;
28 
29 
30 // Define functor subclasses that dispatch to the correct call sequence on
31 // member functions of AudacityProject (or other class!)
32 
33 template<typename OBJ>
34 using audCommandFunction = void (OBJ::*)();
35 
36 template<typename OBJ>
37 class VoidFunctor final : public CommandFunctor
38 {
39 public:
40  explicit VoidFunctor(OBJ *This, audCommandFunction<OBJ> pfn)
41  : mThis{ This }, mCommandFunction{ pfn } {}
42  void operator () (int, const wxEvent *) override
43  { (mThis->*mCommandFunction) (); }
44 private:
45  OBJ *const mThis;
46  const audCommandFunction<OBJ> mCommandFunction;
47 };
48 
49 template<typename OBJ>
50 using audCommandKeyFunction = void (OBJ::*)(const wxEvent *);
51 
52 template<typename OBJ>
53 class KeyFunctor final : public CommandFunctor
54 {
55 public:
56  explicit KeyFunctor(OBJ *This, audCommandKeyFunction<OBJ> pfn)
57  : mThis{ This }, mCommandKeyFunction{ pfn } {}
58  void operator () (int, const wxEvent *evt) override
59  { (mThis->*mCommandKeyFunction) (evt); }
60 private:
61  OBJ *const mThis;
63 };
64 
65 // This allows functions to be used either by command manager or by a wxMenu popup,
66 // but the functions MUST ignore the argument!
67 template<typename OBJ>
68 using audCommandPopupFunction = void (OBJ::*)(wxCommandEvent&);
69 
70 template<typename OBJ>
71 class PopupFunctor final : public CommandFunctor
72 {
73 public:
74  explicit PopupFunctor(OBJ *This, audCommandPopupFunction<OBJ> pfn)
75  : mThis{ This }, mCommandPopupFunction{ pfn } {}
76  void operator () (int, const wxEvent *) override
77  { wxCommandEvent dummy; (mThis->*mCommandPopupFunction) (dummy); }
78 private:
79  OBJ *const mThis;
81 };
82 
83 template<typename OBJ>
84 using audCommandListFunction = void (OBJ::*)(int);
85 
86 template<typename OBJ>
87 class ListFunctor final : public CommandFunctor
88 {
89 public:
90  explicit ListFunctor(OBJ *This, audCommandListFunction<OBJ> pfn)
91  : mThis{ This }, mCommandListFunction{ pfn } {}
92  void operator () (int index, const wxEvent *) override
93  { (mThis->*mCommandListFunction)(index); }
94 private:
95  OBJ *const mThis;
97 };
98 
99 template<typename OBJ>
100 using audCommandPluginFunction = bool (OBJ::*)(const PluginID &, int);
101 
102 template<typename OBJ>
103 class PluginFunctor final : public CommandFunctor
104 {
105 public:
106  explicit PluginFunctor(OBJ *This, const PluginID &id, audCommandPluginFunction<OBJ> pfn)
107  : mPluginID{ id }, mThis{ This }, mCommandPluginFunction{ pfn } {}
108  void operator () (int, const wxEvent *) override
110  (mPluginID,
111  0 // AudacityProject::OnEffectFlags::kNone
112  ); }
113 private:
115  OBJ *const mThis;
117 };
118 
119 // Now define an overloaded factory function
120 template<typename OBJ>
122  audCommandFunction<OBJ> pfn)
123 { return CommandFunctorPointer{ safenew VoidFunctor<OBJ>{ This, pfn } }; }
124 
125 template<typename OBJ>
128 { return CommandFunctorPointer{ safenew KeyFunctor<OBJ>{ This, pfn } }; }
129 
130 template<typename OBJ>
133 { return CommandFunctorPointer{ safenew PopupFunctor<OBJ>{ This, pfn } }; }
134 
135 template<typename OBJ>
138 { return CommandFunctorPointer{ safenew ListFunctor<OBJ>{ This, pfn } }; }
139 
140 template<typename OBJ>
141 inline CommandFunctorPointer MakeFunctor(OBJ *This, const PluginID &id,
143 { return CommandFunctorPointer{ safenew PluginFunctor<OBJ>{ This, id, pfn } }; }
144 
145 // Now define the macro abbreviations that call the factory
146 #define FNT(OBJ, This, X) (MakeFunctor<OBJ>(This, X ))
147 #define FNTS(OBJ, This, X, S) (MakeFunctor<OBJ>(This, (S), X ))
148 
149 #define FN(X) FNT(AudacityProject, this, & AudacityProject :: X)
150 #define FNS(X, S) FNTS(AudacityProject, this, & AudacityProject :: X, S)
151 
152 #endif
void operator()(int index, const wxEvent *) override
Definition: CommandFunctors.h:92
OBJ *const mThis
Definition: CommandFunctors.h:79
const audCommandFunction< OBJ > mCommandFunction
Definition: CommandFunctors.h:46
void(OBJ::*)( audCommandFunction)
Definition: CommandFunctors.h:34
wxString PluginID
Definition: CommandFunctors.h:16
PopupFunctor(OBJ *This, audCommandPopupFunction< OBJ > pfn)
Definition: CommandFunctors.h:74
void(OBJ::*)(const wxEvent *) audCommandKeyFunction
Definition: CommandFunctors.h:50
const audCommandPluginFunction< OBJ > mCommandPluginFunction
Definition: CommandFunctors.h:116
void operator()(int, const wxEvent *) override
Definition: CommandFunctors.h:42
bool(OBJ::*)(const PluginID &, int) audCommandPluginFunction
Definition: CommandFunctors.h:100
PluginFunctor(OBJ *This, const PluginID &id, audCommandPluginFunction< OBJ > pfn)
Definition: CommandFunctors.h:106
Definition: CommandFunctors.h:87
const audCommandPopupFunction< OBJ > mCommandPopupFunction
Definition: CommandFunctors.h:80
void operator()(int, const wxEvent *) override
Definition: CommandFunctors.h:76
#define safenew
Definition: Audacity.h:254
OBJ *const mThis
Definition: CommandFunctors.h:95
OBJ *const mThis
Definition: CommandFunctors.h:61
VoidFunctor(OBJ *This, audCommandFunction< OBJ > pfn)
Definition: CommandFunctors.h:40
virtual ~CommandFunctor()
Definition: CommandFunctors.h:23
CommandFunctor()
Definition: CommandFunctors.h:22
Definition: CommandFunctors.h:53
KeyFunctor(OBJ *This, audCommandKeyFunction< OBJ > pfn)
Definition: CommandFunctors.h:56
CommandFunctorPointer MakeFunctor(OBJ *This, audCommandFunction< OBJ > pfn)
Definition: CommandFunctors.h:121
std::shared_ptr< CommandFunctor > CommandFunctorPointer
Definition: CommandFunctors.h:27
Definition: CommandFunctors.h:37
ListFunctor(OBJ *This, audCommandListFunction< OBJ > pfn)
Definition: CommandFunctors.h:90
CommandFunctor is a very small class that works with CommandManager. It holds the callback for one co...
Definition: CommandFunctors.h:19
void(OBJ::*)(int) audCommandListFunction
Definition: CommandFunctors.h:84
void(OBJ::*)(wxCommandEvent &) audCommandPopupFunction
Definition: CommandFunctors.h:68
Definition: CommandFunctors.h:71
void operator()(int, const wxEvent *evt) override
Definition: CommandFunctors.h:58
const PluginID mPluginID
Definition: CommandFunctors.h:114
OBJ *const mThis
Definition: CommandFunctors.h:45
OBJ *const mThis
Definition: CommandFunctors.h:115
Definition: CommandFunctors.h:103
void operator()(int, const wxEvent *) override
Definition: CommandFunctors.h:108
const audCommandListFunction< OBJ > mCommandListFunction
Definition: CommandFunctors.h:96
const audCommandKeyFunction< OBJ > mCommandKeyFunction
Definition: CommandFunctors.h:62