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