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 Shuttle.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 "Shuttle.h"
17
18class Effect;
20
23class AUDACITY_DLL_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 AUDACITY_DLL_API ShuttleGetAutomation final : public ConstSettingsVisitor
245{
246public:
247 ConstSettingsVisitor & Optional(const bool & var) override;
248 void Define(bool var, const wxChar * key, bool vdefault,
249 bool vmin, bool vmax, bool vscl) override;
250 void Define(int var, const wxChar * key, int vdefault,
251 int vmin, int vmax, int vscl) override;
252 void Define(size_t var, const wxChar * key, int vdefault,
253 int vmin, int vmax, int vscl) override;
254 void Define(float var, const wxChar * key, float vdefault,
255 float vmin, float vmax, float vscl) override;
256 void Define(double var, const wxChar * key, float vdefault,
257 float vmin, float vmax, float vscl) override;
258 void Define(double var, const wxChar * key, double vdefault,
259 double vmin, double vmax, double vscl) override;
260 void Define(const wxString &var, const wxChar * key, wxString vdefault,
261 wxString vmin, wxString vmax, wxString vscl) override;
262 void DefineEnum(int var, const wxChar * key, int vdefault,
263 const EnumValueSymbol strings[], size_t nStrings) override;
264};
265
266/**************************************************************************/
269class AUDACITY_DLL_API ShuttleSetAutomation final : public SettingsVisitor
270{
271public:
273 bool bOK{ false };
274 bool bWrite{ false };
275
276 bool CouldGet(const wxString &key);
278 mpEap = pEap;
279 bOK = true;
280 bWrite = false;
281 }
283 mpEap = pEap;
284 bOK = true;
285 bWrite = true;
286 }
287
288 SettingsVisitor & Optional( bool & var ) override;
289 void Define( bool & var, const wxChar * key, bool vdefault,
290 bool vmin, bool vmax, bool vscl ) override;
291 void Define( int & var, const wxChar * key, int vdefault,
292 int vmin, int vmax, int vscl ) override;
293 void Define( size_t & var, const wxChar * key, int vdefault,
294 int vmin, int vmax, int vscl ) override;
295 void Define( float & var, const wxChar * key, float vdefault,
296 float vmin, float vmax, float vscl ) override;
297 void Define( double & var, const wxChar * key, float vdefault,
298 float vmin, float vmax, float vscl ) override;
299 void Define( double & var, const wxChar * key, double vdefault,
300 double vmin, double vmax, double vscl ) override;
301 void Define( wxString &var, const wxChar * key, wxString vdefault,
302 wxString vmin, wxString vmax, wxString vscl ) override;
303 void DefineEnum( int &var, const wxChar * key, int vdefault,
304 const EnumValueSymbol strings[], size_t nStrings ) override;
305};
306
307/**************************************************************************/
311{
312public:
313 wxString Result;
314
315 SettingsVisitor & Optional( bool & var ) override;
316 SettingsVisitor & OptionalY( bool & var ) override;
317 SettingsVisitor & OptionalN( bool & var ) override;
318 void Define( bool & var, const wxChar * key, bool vdefault,
319 bool vmin, bool vmax, bool vscl ) override;
320 void Define( int & var, const wxChar * key, int vdefault,
321 int vmin, int vmax, int vscl ) override;
322 void Define( size_t & var, const wxChar * key, int vdefault,
323 int vmin, int vmax, int vscl ) override;
324 void Define( float & var, const wxChar * key, float vdefault,
325 float vmin, float vmax, float vscl ) override;
326 void Define( double & var, const wxChar * key, float vdefault,
327 float vmin, float vmax, float vscl ) override;
328 void Define( double & var, const wxChar * key, double vdefault,
329 double vmin, double vmax, double vscl ) override;
330 void Define( wxString &var, const wxChar * key, wxString vdefault,
331 wxString vmin, wxString vmax, wxString vscl ) override;
332 void DefineEnum( int &var, const wxChar * key, int vdefault,
333 const EnumValueSymbol strings[], size_t nStrings ) override;
334};
335
336#endif
static const AudacityProject::AttachedObjects::RegisteredFactory key
EffectType
static Settings & settings()
Definition: TrackInfo.cpp:87
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:148
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 ...
Definition: Shuttle.h:115
virtual void Define(Arg< bool > var, const wxChar *key, bool vdefault, bool vmin=false, bool vmax=false, bool vscl=false)
Definition: Shuttle.cpp:359
virtual void DefineEnum(Arg< int > var, const wxChar *key, int vdefault, const EnumValueSymbol strings[], size_t nStrings)
Definition: Shuttle.cpp:394
SettingsVisitor that sets parameters to their default values.
void Define(bool &var, const wxChar *key, bool vdefault, bool vmin, bool vmax, bool vscl) override
SettingsVisitor & Optional(bool &var) override
void DefineEnum(int &var, const wxChar *key, int vdefault, const EnumValueSymbol strings[], size_t nStrings) override
SettingsVisitor & OptionalY(bool &var) override
SettingsVisitor & OptionalN(bool &var) override
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.
Definition: Shuttle.h:28
const Type scale
Scaling factor, for slider control.
Definition: Shuttle.h:32
Member Structure::*const mem
Member holding the parameter.
Definition: Shuttle.h:27
const Type def
Default value.
Definition: Shuttle.h:29
const Type min
Minimum value.
Definition: Shuttle.h:30
const Type max
Maximum value.
Definition: Shuttle.h:31
Externalized state of a plug-in.
const size_t nSymbols
Definition: Shuttle.h:62
const EnumValueSymbol *const symbols
Definition: Shuttle.h:61