Audacity 3.2.0
Shuttle.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Shuttle.cpp
4
5 James Crook
6 (C) Audacity Developers, 2007
7
8 wxWidgets license. See Licensing.txt
9
10*******************************************************************//****************************************************************//****************************************************************//****************************************************************//*******************************************************************/
52
53
54#include "Shuttle.h"
55
56#include <wx/defs.h>
57#include <wx/checkbox.h>
58#include <wx/choice.h>
59#include <wx/statbox.h>
60#include <wx/stattext.h>
61#include <wx/textctrl.h>
62#include <wx/listctrl.h>
63
64#include "WrappedType.h"
65//#include "effects/Effect.h"
66
67
69{
70}
71
72bool Shuttle::TransferBool( const wxString & Name, bool & bValue, const bool & bDefault )
73{
74 if( mbStoreInClient )
75 {
76 bValue = bDefault;
77 if( ExchangeWithMaster( Name ))
78 {
79 if( !mValueString.empty() )
80 bValue = mValueString.GetChar(0) == wxT('y');
81 }
82 }
83 else
84 {
85 mValueString = (bValue==0) ? wxT("no"):wxT("yes");
86 return ExchangeWithMaster( Name );
87 }
88 return true;
89}
90
91bool Shuttle::TransferFloat( const wxString & Name, float & fValue, const float &fDefault )
92{
93 if( mbStoreInClient )
94 {
95 fValue = fDefault;
96 if( ExchangeWithMaster( Name ))
97 {
98 if( !mValueString.empty() )
99 fValue = wxAtof( mValueString );
100 }
101 }
102 else
103 {
104 mValueString = wxString::Format(wxT("%f"),fValue);
105 return ExchangeWithMaster( Name );
106 }
107 return true;
108}
109
110bool Shuttle::TransferDouble( const wxString & Name, double & dValue, const double &dDefault )
111{
112 if( mbStoreInClient )
113 {
114 dValue = dDefault;
115 if( ExchangeWithMaster( Name ))
116 {
117 if( !mValueString.empty() )
118 dValue = wxAtof( mValueString );
119 }
120 }
121 else
122 {
123 //%f is format string for double
124 mValueString = wxString::Format(wxT("%f"),dValue);
125 return ExchangeWithMaster( Name );
126 }
127 return true;
128}
129
130bool Shuttle::TransferInt( const wxString & Name, int & iValue, const int & iDefault )
131{
132 if( mbStoreInClient )
133 {
134 iValue = iDefault;
135 if( ExchangeWithMaster( Name ))
136 {
137 iValue = wxAtoi( mValueString );
138 }
139 }
140 else
141 {
142 mValueString = wxString::Format(wxT("%i"),iValue);
143 return ExchangeWithMaster( Name );
144 }
145 return true;
146}
147
148
149bool Shuttle::TransferInt( const wxString & Name, wxLongLong_t & iValue, const wxLongLong_t & iDefault )
150{
151 return TransferLongLong(Name, iValue, iDefault);
152}
153
154bool Shuttle::TransferLongLong( const wxString & Name, wxLongLong_t & iValue, const wxLongLong_t & iDefault )
155{
156 if( mbStoreInClient )
157 {
158 iValue = iDefault;
159 if( ExchangeWithMaster( Name ))
160 {
161 iValue = wxAtoi( mValueString );
162 }
163 }
164 else
165 {
167 mValueString = wxString::Format(wxT("%lld"), (long long) iValue);
168 return ExchangeWithMaster( Name );
169 }
170 return true;
171}
172
173
174bool Shuttle::TransferEnum( const wxString & Name, int & iValue,
175 const int nChoices, const wxString * pFirstStr)
176{
177 if( mbStoreInClient )
178 {
179 iValue = 0;// default index if none other selected.
180 if( ExchangeWithMaster( Name ))
181 {
182 wxString str = mValueString;
183 if( str.Left( 1 ) == wxT('"') && str.Right( 1 ) == wxT('"') )
184 {
185 str = str.Mid( 2, str.length() - 2 );
186 }
187
188 for( int i = 0; i < nChoices; i++ )
189 {
190 if( str == pFirstStr[i] )
191 {
192 iValue = i;
193 break;
194 }
195 }
196 }
197 }
198 else
199 {
200 //TIDY-ME: Out of range configuration values are silently discarded...
201 if( iValue > nChoices )
202 iValue = 0;
203 if( iValue < 0 )
204 iValue = 0;
205 mValueString = pFirstStr[iValue];
206 if( mValueString.Find( wxT(' ') ) != wxNOT_FOUND )
207 {
208 mValueString = wxT('"') + pFirstStr[iValue] + wxT('"'); //strings have quotes around them
209 }
210 return ExchangeWithMaster( Name );
211 }
212 return true;
213}
214
215bool Shuttle::TransferString( const wxString & Name, wxString & strValue, const wxString & WXUNUSED(strDefault) )
216{
217 if( mbStoreInClient )
218 {
219 if( ExchangeWithMaster( Name ))
220 {
221 strValue = mValueString;
222 }
223 else
224 return false;
225 }
226 else
227 {
228 mValueString = wxT('"') + strValue + wxT('"'); //strings have quotes around them
229 return ExchangeWithMaster( Name );
230 }
231 return true;
232}
233
234bool Shuttle::TransferWrappedType( const wxString & Name, WrappedType & W )
235{
236 if( mbStoreInClient )
237 {
238 if( ExchangeWithMaster( Name ))
239 {
240 W.WriteToAsString( mValueString );
241 }
242 }
243 else
244 {
245 mValueString = W.ReadAsString();
246 return ExchangeWithMaster( Name );
247 }
248 return true;
249}
250
251
252bool Shuttle::ExchangeWithMaster(const wxString & WXUNUSED(Name))
253{
254 // ExchangeWithMaster() will usually be over-ridden
255 // in derived classes. We could have made it an
256 // abstract function.
257 wxASSERT( false );
258 return true;
259}
260
261// This variant uses values of the form
262// param1=value1 param2=value2
263bool ShuttleCli::ExchangeWithMaster(const wxString & Name)
264{
265 if( !mbStoreInClient )
266 {
267 mParams += wxT(" ");
268 mParams +=Name;
269 mParams += wxT("=");
271 }
272 else
273 {
274 int i;
275 mParams = wxT(" ")+mParams;
276 i=mParams.Find( wxT(" ")+Name+wxT("=") );
277 if( i>=0 )
278 {
279 int j=i+2+Name.length();
280 wxString terminator = wxT(' ');
281 if(mParams.GetChar(j) == wxT('"')) //Strings are surrounded by quotes
282 {
283 terminator = wxT('"');
284 j++;
285 }
286 else if(mParams.GetChar(j) == wxT('\'')) // or by single quotes.
287 {
288 terminator = wxT('\'');
289 j++;
290 }
291 i=j;
292 while( j<(int)mParams.length() && mParams.GetChar(j) != terminator )
293 j++;
294 mValueString = mParams.Mid(i,j-i);
295 return true;
296 }
297 return false;
298 }
299 return true;
300}
301
302
303#ifdef _MSC_VER
304// If this is compiled with MSVC (Visual Studio)
305#pragma warning( push )
306#pragma warning( disable: 4100 ) // unused parameters.
307#endif //_MSC_VER
308
309
310// The ShouldSet and CouldGet functions have an important side effect
311// on the pOptionalFlag. They 'use it up' and clear it down for the next parameter.
312
313
314template<bool Const>
316
317template<bool Const>
320{
321 pOptionalFlag = nullptr;
322 return *this;
323}
324
325template<bool Const>
328{
329 return Optional( var );
330}
331
332template<bool Const>
335{
336 return Optional( var );
337}
338
339// Tests for parameter being optional.
340// Prepares for next parameter by clearing the pointer.
341// Reports on whether the parameter should be set, i.e. should set
342// if it was chosen to be set, or was not optional.
343template<bool Const>
345{
346 if( !pOptionalFlag )
347 return true;
348 bool result = *pOptionalFlag;
349 pOptionalFlag = NULL;
350 return result;
351}
352
353// These are functions to override. They do nothing.
354template<bool Const>
356 bool, bool, bool, bool)
357{}
358
359template<bool Const>
361 int, int, int, int)
362{}
363
364template<bool Const>
366 int, int, int, int)
367{}
368
369template<bool Const>
371 Arg<float>, const wxChar *, float, float, float, float)
372{}
373
374template<bool Const>
376 Arg<double>, const wxChar *, float, float, float, float )
377{}
378
379template<bool Const>
381 Arg<double>, const wxChar *, double, double, double, double)
382{}
383
384template<bool Const>
386 Ref<wxString>, const wxChar *, wxString, wxString, wxString, wxString)
387{}
388
389template<bool Const>
391 Arg<int>, const wxChar *, int, const EnumValueSymbol [], size_t)
392{}
393
394// Explicit instantiations
395template class SettingsVisitorBase<false>;
396template class SettingsVisitorBase<true>;
397
398#ifdef _MSC_VER
399// If this is compiled with MSVC (Visual Studio)
400#pragma warning( pop )
401#endif //_MSC_VER
wxT("CloseDown"))
#define str(a)
#define W(N, I)
Definition: ToChars.cpp:60
ComponentInterfaceSymbol pairs a persistent string identifier used internally with an optional,...
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:355
virtual SettingsVisitorBase & OptionalY(Ref< bool > var)
Definition: Shuttle.cpp:326
std::conditional_t< Const, T, T & > Arg
Definition: Shuttle.h:120
virtual ~SettingsVisitorBase()
virtual SettingsVisitorBase & Optional(Ref< bool > var)
Definition: Shuttle.cpp:318
virtual void DefineEnum(Arg< int > var, const wxChar *key, int vdefault, const EnumValueSymbol strings[], size_t nStrings)
Definition: Shuttle.cpp:390
std::conditional_t< Const, const T &, T & > Ref
Definition: Shuttle.h:118
virtual SettingsVisitorBase & OptionalN(Ref< bool > var)
Definition: Shuttle.cpp:333
wxString mParams
Definition: Shuttle.h:99
bool ExchangeWithMaster(const wxString &Name) override
Definition: Shuttle.cpp:263
virtual bool TransferBool(const wxString &Name, bool &bValue, const bool &bDefault)
Definition: Shuttle.cpp:72
virtual bool TransferString(const wxString &Name, wxString &strValue, const wxString &strDefault)
Definition: Shuttle.cpp:215
Shuttle()
Definition: Shuttle.cpp:68
virtual bool TransferDouble(const wxString &Name, double &dValue, const double &dDefault)
Definition: Shuttle.cpp:110
virtual bool TransferWrappedType(const wxString &Name, WrappedType &W)
Definition: Shuttle.cpp:234
bool mbStoreInClient
Definition: Shuttle.h:77
virtual bool ExchangeWithMaster(const wxString &Name)
Definition: Shuttle.cpp:252
virtual bool TransferFloat(const wxString &Name, float &fValue, const float &fDefault)
Definition: Shuttle.cpp:91
virtual bool TransferLongLong(const wxString &Name, wxLongLong_t &iValue, const wxLongLong_t &iDefault)
Definition: Shuttle.cpp:154
virtual bool TransferInt(const wxString &Name, int &iValue, const int &iDefault)
Definition: Shuttle.cpp:130
virtual bool TransferEnum(const wxString &Name, int &iValue, const int nChoices, const wxString *pFirstStr)
Definition: Shuttle.cpp:174
wxString mValueString
Definition: Shuttle.h:78
Used in type conversions, this wrapper for ints, strings, doubles and enums provides conversions betw...
Definition: WrappedType.h:31