Audacity 3.2.0
ShuttleAutomation.h
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 @file ShuttleAutomation.h
6
7 Paul Licameli split from SettingsVisitor.h
8
9**********************************************************************/
10
11#ifndef __AUDACITY_SHUTTLE_AUTOMATION__
12#define __AUDACITY_SHUTTLE_AUTOMATION__
13
14#include <type_traits>
15#include "EffectInterface.h"
16#include "SettingsVisitor.h"
17
18class Effect;
20
23class COMMAND_PARAMETERS_API EffectParameterMethods {
24public:
26 virtual void Reset(Effect &effect) const = 0;
27 virtual void Visit(Effect &effect,
28 SettingsVisitor &visitor, EffectSettings &settings) const = 0;
29 virtual void Visit(const Effect &effect,
30 ConstSettingsVisitor &visitor, const EffectSettings &settings) const = 0;
31 virtual void Get(const Effect &effect, const EffectSettings &settings,
32 CommandParameters & parms) const = 0;
33 virtual bool Set(Effect &effect,
34 const CommandParameters & parms, EffectSettings &settings) const = 0;
35};
36
38
59template <typename EffectType, const auto &...Parameters>
61public:
62 using Params = std::remove_pointer_t< decltype(
63 // When all effects become stateless, EffectType argument won't be needed
64 EffectType::FetchParameters(
65 std::declval<EffectType&>(), std::declval<EffectSettings &>())
66 ) >;
67
68 virtual ~CapturedParameters() = default;
69
70 // Another helper type
71 // boolean argument is true if updating (not resetting defaults)
72 // Returns true if successful, but that is ignored when resetting
73 // When all effects become stateless, EffectType argument won't be needed
75 std::function< bool(EffectType&, EffectSettings &, Params &, bool) >;
76
77 // Constructors
78
80
81 template< typename Fn,
82 // Require that the argument be callable with appropriate arguments
83 typename = decltype( std::declval<Fn>()(
84 std::declval<EffectType &>(), std::declval<EffectSettings &>(),
85 std::declval<Params &>(), true) )
86 >
87 // Like the previous, but with an argument,
88 // which is called at the end of Reset or Set. Its return value is
89 // ignored in Reset() and passed as the result of Set.
90 explicit CapturedParameters(Fn &&PostSet)
91 : PostSetFn{ std::forward<Fn>(PostSet) }
92 {}
93
94 void Reset(Effect &effect) const override {
95 EffectSettings dummy;
96 if (auto pStruct = EffectType::FetchParameters(
97 static_cast<EffectType&>(effect), dummy))
98 DoReset(effect, dummy, *pStruct, *this);
99 }
100 void Visit(Effect &effect,
102 const override {
103 if (auto pStruct = EffectType::FetchParameters(
104 static_cast<EffectType&>(effect), settings))
105 DoVisit<false>(*pStruct, visitor);
106 }
107 void Visit(const Effect &effect,
109 const override {
110 // const_cast the effect...
111 auto &nonconstEffect = const_cast<Effect&>(effect);
112 // ... and the settings ...
113 auto &nonconstSettings = const_cast<EffectSettings&>(settings);
114 // ... but only to fetch the structure and pass it as const &
115 if (auto pStruct = EffectType::FetchParameters(
116 static_cast<EffectType&>(nonconstEffect), nonconstSettings))
117 DoVisit(*pStruct, visitor);
118 }
119 void Get(const Effect &effect, const EffectSettings &settings,
120 CommandParameters & parms) const override {
121 // const_cast the effect...
122 auto &nonconstEffect = const_cast<Effect &>(effect);
123 // ... and the settings ...
124 auto &nonconstSettings = const_cast<EffectSettings&>(settings);
125 // ... but only to fetch the structure and pass it as const &
126 if (auto pStruct = EffectType::FetchParameters(
127 static_cast<EffectType&>(nonconstEffect), nonconstSettings))
128 DoGet(*pStruct, parms);
129 }
130 bool Set(Effect &effect, const CommandParameters & parms,
131 EffectSettings &settings) const override {
132 if (auto pStruct = EffectType::FetchParameters(
133 static_cast<EffectType&>(effect), settings))
134 return DoSet(effect, settings, *pStruct, *this, parms);
135 else
136 return false;
137 }
138
139private:
141
142 // Function templates. There are functions to treat individual parameters,
143 // sometimes with two overloads, and variadic functions that use
144 // fold expressions that apply to the sequence of parameters.
145
146 template< typename Member, typename Type, typename Value >
147 static void ResetOne(Params &structure,
149 // Do one assignment of the default value
150 structure.*(param.mem) = param.def;
151 }
152 static void DoReset(Effect &effect, EffectSettings settings,
153 Params &structure,
154 const CapturedParameters &This) {
155 (ResetOne(structure, Parameters), ...);
156 // Call the post-set function after all other assignments
157 if (This.PostSetFn)
158 This.PostSetFn(
159 static_cast<EffectType&>(effect), settings, structure, false);
160 }
161
162 template< bool Const, typename Member, typename Type, typename Value >
163 static void VisitOne(Params &structure, SettingsVisitorBase<Const> &visitor,
165 // Visit one variable
166 visitor.Define( structure.*(param.mem), param.key,
167 static_cast<Member>(param.def),
168 static_cast<Member>(param.min),
169 static_cast<Member>(param.max),
170 static_cast<Member>(param.scale) );
171 }
172 // More specific overload for enumeration parameters
173 template< bool Const, typename Member >
174 static void VisitOne(Params &structure, SettingsVisitorBase<Const> &visitor,
175 const EnumParameter<Params, Member> &param) {
176 // Visit one enumeration variable, passing the table of names
177 visitor.DefineEnum( structure.*(param.mem),
178 param.key, param.def, param.symbols, param.nSymbols );
179 }
180 template<bool Const>
181 static void DoVisit(Params &structure, SettingsVisitorBase<Const> &visitor) {
182 (VisitOne<Const>(structure, visitor, Parameters), ...);
183 }
184
185 template< typename Member, typename Type, typename Value >
186 static void GetOne(const Params &structure, CommandParameters & parms,
188 // Serialize one variable
189 parms.Write( param.key, static_cast<Value>(structure.*(param.mem)) );
190 }
191 // More specific overload for enumeration parameters
192 template< typename Member >
193 static void GetOne(const Params &structure, CommandParameters & parms,
194 const EnumParameter<Params, Member> &param) {
195 // Serialize one enumeration variable as a string identifier, not a number
196 parms.Write(
197 param.key, param.symbols[ structure.*(param.mem) ].Internal() );
198 }
199 static void DoGet(const Params &structure, CommandParameters &parms) {
200 (GetOne(structure, parms, Parameters), ...);
201 }
202
203 template< typename Member, typename Type, typename Value >
204 static bool SetOne(Params &structure,
205 const CommandParameters &parms,
207 // Deserialize and assign one variable (or fail)
208 Value temp;
209 if (!parms.ReadAndVerify(param.key, &temp, param.def,
210 param.min, param.max))
211 return false;
212 structure.*(param.mem) = temp;
213 return true;
214 }
215 // More specific overload for enumeration parameters
216 template< typename Member >
217 static bool SetOne(Params &structure,
218 const CommandParameters &parms,
219 const EnumParameter<Params, Member> &param) {
220 // Deserialize and assign one enumeration variable (or fail)
221 int temp;
222 if (!parms.ReadAndVerify(param.key, &temp, param.def,
223 param.symbols, param.nSymbols))
224 return false;
225 structure.*(param.mem) = temp;
226 return true;
227 }
228 static bool DoSet(Effect &effect,
229 EffectSettings &settings, Params &structure,
230 const CapturedParameters &This, const CommandParameters &parms) {
231 if (!(SetOne(structure, parms, Parameters) && ...))
232 return false;
233 // Call the post-set function after all other assignments, or return
234 // true if no function was given
235 return !This.PostSetFn ||
236 This.PostSetFn(
237 static_cast<EffectType&>(effect), settings, structure, true);
238 }
239};
240
241/**************************************************************************/
244class COMMAND_PARAMETERS_API ShuttleGetAutomation final
245 : public ConstSettingsVisitor
246{
247public:
248 ConstSettingsVisitor & Optional(const bool & var) override;
249 void Define(bool var, const wxChar * key, bool vdefault,
250 bool vmin, bool vmax, bool vscl) override;
251 void Define(int var, const wxChar * key, int vdefault,
252 int vmin, int vmax, int vscl) override;
253 void Define(size_t var, const wxChar * key, int vdefault,
254 int vmin, int vmax, int vscl) override;
255 void Define(float var, const wxChar * key, float vdefault,
256 float vmin, float vmax, float vscl) override;
257 void Define(double var, const wxChar * key, float vdefault,
258 float vmin, float vmax, float vscl) override;
259 void Define(double var, const wxChar * key, double vdefault,
260 double vmin, double vmax, double vscl) override;
261 void Define(const wxString &var, const wxChar * key, wxString vdefault,
262 wxString vmin, wxString vmax, wxString vscl) override;
263 void DefineEnum(int var, const wxChar * key, int vdefault,
264 const EnumValueSymbol strings[], size_t nStrings) override;
265};
266
267/**************************************************************************/
270class COMMAND_PARAMETERS_API ShuttleSetAutomation final : public SettingsVisitor
271{
272public:
274 bool bOK{ false };
275 bool bWrite{ false };
276
277 bool CouldGet(const wxString &key);
279 mpEap = pEap;
280 bOK = true;
281 bWrite = false;
282 }
284 mpEap = pEap;
285 bOK = true;
286 bWrite = true;
287 }
288
289 SettingsVisitor & Optional( bool & var ) override;
290 void Define( bool & var, const wxChar * key, bool vdefault,
291 bool vmin, bool vmax, bool vscl ) override;
292 void Define( int & var, const wxChar * key, int vdefault,
293 int vmin, int vmax, int vscl ) override;
294 void Define( size_t & var, const wxChar * key, int vdefault,
295 int vmin, int vmax, int vscl ) override;
296 void Define( float & var, const wxChar * key, float vdefault,
297 float vmin, float vmax, float vscl ) override;
298 void Define( double & var, const wxChar * key, float vdefault,
299 float vmin, float vmax, float vscl ) override;
300 void Define( double & var, const wxChar * key, double vdefault,
301 double vmin, double vmax, double vscl ) override;
302 void Define( wxString &var, const wxChar * key, wxString vdefault,
303 wxString vmin, wxString vmax, wxString vscl ) override;
304 void DefineEnum( int &var, const wxChar * key, int vdefault,
305 const EnumValueSymbol strings[], size_t nStrings ) override;
306};
307
308/**************************************************************************/
311class COMMAND_PARAMETERS_API ShuttleDefaults final : public SettingsVisitor
312{
313public:
314 wxString Result;
315
316 SettingsVisitor & Optional( bool & var ) override;
317 SettingsVisitor & OptionalY( bool & var ) override;
318 SettingsVisitor & OptionalN( bool & var ) override;
319 void Define( bool & var, const wxChar * key, bool vdefault,
320 bool vmin, bool vmax, bool vscl ) override;
321 void Define( int & var, const wxChar * key, int vdefault,
322 int vmin, int vmax, int vscl ) override;
323 void Define( size_t & var, const wxChar * key, int vdefault,
324 int vmin, int vmax, int vscl ) override;
325 void Define( float & var, const wxChar * key, float vdefault,
326 float vmin, float vmax, float vscl ) override;
327 void Define( double & var, const wxChar * key, float vdefault,
328 float vmin, float vmax, float vscl ) override;
329 void Define( double & var, const wxChar * key, double vdefault,
330 double vmin, double vmax, double vscl ) override;
331 void Define( wxString &var, const wxChar * key, wxString vdefault,
332 wxString vmin, wxString vmax, wxString vscl ) override;
333 void DefineEnum( int &var, const wxChar * key, int vdefault,
334 const EnumValueSymbol strings[], size_t nStrings ) override;
335};
336
337#endif
EffectType
static const AudacityProject::AttachedObjects::RegisteredFactory key
static Settings & settings()
Definition: TrackInfo.cpp:51
Generates EffectParameterMethods overrides from variadic template arguments.
static bool DoSet(Effect &effect, EffectSettings &settings, Params &structure, const CapturedParameters &This, const CommandParameters &parms)
static bool SetOne(Params &structure, const CommandParameters &parms, const EffectParameter< Params, Member, Type, Value > &param)
void Visit(Effect &effect, SettingsVisitor &visitor, EffectSettings &settings) const override
bool Set(Effect &effect, const CommandParameters &parms, EffectSettings &settings) const override
static void DoReset(Effect &effect, EffectSettings settings, Params &structure, const CapturedParameters &This)
CapturedParameters(Fn &&PostSet)
std::function< bool(EffectType &, EffectSettings &, Params &, bool) > PostSetFunction
static void DoGet(const Params &structure, CommandParameters &parms)
static void GetOne(const Params &structure, CommandParameters &parms, const EnumParameter< Params, Member > &param)
virtual ~CapturedParameters()=default
PostSetFunction PostSetFn
static void VisitOne(Params &structure, SettingsVisitorBase< Const > &visitor, const EffectParameter< Params, Member, Type, Value > &param)
static void GetOne(const Params &structure, CommandParameters &parms, const EffectParameter< Params, Member, Type, Value > &param)
static bool SetOne(Params &structure, const CommandParameters &parms, const EnumParameter< Params, Member > &param)
void Visit(const Effect &effect, ConstSettingsVisitor &visitor, const EffectSettings &settings) const override
static void VisitOne(Params &structure, SettingsVisitorBase< Const > &visitor, const EnumParameter< Params, Member > &param)
void Get(const Effect &effect, const EffectSettings &settings, CommandParameters &parms) const override
static void ResetOne(Params &structure, const EffectParameter< Params, Member, Type, Value > &param)
std::remove_pointer_t< decltype(EffectType::FetchParameters(std::declval< EffectType & >(), std::declval< EffectSettings & >())) > Params
void Reset(Effect &effect) const override
static void DoVisit(Params &structure, SettingsVisitorBase< Const > &visitor)
CommandParameters, derived from wxFileConfig, is essentially doing the same things as the SettingsVis...
bool ReadAndVerify(const wxString &key, float *val, float defVal, float min, float max) const
ComponentInterfaceSymbol pairs a persistent string identifier used internally with an optional,...
const wxString & Internal() const
Base class for many of the effects in Audacity.
Definition: Effect.h:26
Interface for manipulations of an Effect's settings.
virtual bool Set(Effect &effect, const CommandParameters &parms, EffectSettings &settings) const =0
virtual void Get(const Effect &effect, const EffectSettings &settings, CommandParameters &parms) const =0
virtual void Visit(Effect &effect, SettingsVisitor &visitor, EffectSettings &settings) const =0
virtual void Visit(const Effect &effect, ConstSettingsVisitor &visitor, const EffectSettings &settings) const =0
virtual void Reset(Effect &effect) const =0
virtual ~EffectParameterMethods()
Visitor of effect or command parameters. This is a base class with lots of virtual functions that do ...
virtual void Define(Arg< bool > var, const wxChar *key, bool vdefault, bool vmin=false, bool vmax=false, bool vscl=false)
virtual void DefineEnum(Arg< int > var, const wxChar *key, int vdefault, const EnumValueSymbol strings[], size_t nStrings)
SettingsVisitor that sets parameters to their default values.
SettingsVisitor that gets parameter values into a string.
SettingsVisitor that sets parameters to a value (from a string)
void SetForValidating(CommandParameters *pEap)
void SetForWriting(CommandParameters *pEap)
STL namespace.
const wxChar *const key
Identifier in configuration file.
const Type scale
Scaling factor, for slider control.
Member Structure::*const mem
Member holding the parameter.
const Type def
Default value.
const Type min
Minimum value.
const Type max
Maximum value.
Externalized state of a plug-in.
const size_t nSymbols
const EnumValueSymbol *const symbols