Audacity  3.0.3
Validators.h
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity - A Digital Audio Editor
4  Copyright 1999-2009 Audacity Team
5  License: wxWidgets
6 
7  Dan Horgan
8 
9 ******************************************************************//*******************************************************************/
42 
43 #ifndef __VALIDATORS__
44 #define __VALIDATORS__
45 
46 class wxArrayString;
47 
48 #include "MemoryX.h"
49 
50 #include <wx/variant.h> // member variable
51 
52 class Validator /* not final */
53 {
54 private:
55  wxVariant mConverted;
56 
57 public:
58  Validator() {};
59  virtual ~Validator() {};
60  void SetConverted (const wxVariant &v)
61  {
62  mConverted = v;
63  }
64  const wxVariant &GetConverted()
65  {
66  return mConverted;
67  }
68 
70  virtual bool Validate(const wxVariant &v)
71  {
72  SetConverted(v);
73  return true;
74  }
75 
78  virtual wxString GetDescription() const
79  {
80  return wxT("any value");
81  }
82 
84  using Holder = std::unique_ptr<Validator>;
85  virtual Holder GetClone() const = 0;
86 };
87 
88 class DefaultValidator final : public Validator
89 {
90 public:
91  virtual Holder GetClone() const
92  {
93  return std::make_unique<DefaultValidator>(*this);
94  }
95 };
96 
97 class OptionValidator final : public Validator
98 {
99 private:
100  wxArrayString mOptions;
101 
102 public:
103  void AddOption(const wxString &option)
104  {
105  mOptions.push_back(option);
106  }
107  void AddOptions(const wxArrayString &options)
108  {
109  mOptions.insert(mOptions.begin(), options.begin(), options.end());
110  }
111  bool Validate(const wxVariant &v) override
112  {
113  SetConverted(v);
114  return make_iterator_range( mOptions ).contains( v.GetString() );
115  }
116  wxString GetDescription() const override
117  {
118  wxString desc = wxT("one of: ");
119  int optionCount = mOptions.size();
120  int i = 0;
121  for (i = 0; i+1 < optionCount; ++i)
122  {
123  desc += mOptions[i] + wxT(", ");
124  }
125  desc += mOptions[optionCount-1];
126  return desc;
127  }
128  Holder GetClone() const override
129  {
130  auto v = std::make_unique<OptionValidator>();
131  v->mOptions = mOptions;
132  // This std::move is needed to "upcast" the pointer type
133  return std::move(v);
134  }
135 };
136 
137 class BoolValidator final : public Validator
138 {
139 public:
140  bool Validate(const wxVariant &v) override
141  {
142  bool val;
143  if (!v.Convert(&val)) return false;
144  SetConverted(val);
145  return GetConverted().IsType(wxT("bool"));
146  }
147  wxString GetDescription() const override
148  {
149  return wxT("true/false or 1/0 or yes/no");
150  }
151  Holder GetClone() const override
152  {
153  return std::make_unique<BoolValidator>();
154  }
155 };
156 
157 class BoolArrayValidator final : public Validator
158 {
159 public:
160  virtual bool Validate(const wxVariant &v) override
161  {
162  wxString val; // Validate a string of chars containing only 0, 1 and x.
163  if (!v.Convert(&val))
164  return false;
165  SetConverted(val);
166  for(size_t i=0; i != val.length(); i++)
167  if( val[i] != '0' && val[i] != '1' && val[i] != 'x' && val[i] != 'X')
168  return false;
169  return true;
170  }
171  wxString GetDescription() const override
172  {
173  return wxT("0X101XX101...etc. where 0=false, 1=true, and X=don't care. Numbering starts at leftmost = track 0");
174  }
175  Holder GetClone() const override
176  {
177  return std::make_unique<BoolArrayValidator>();
178  }
179 };
180 
181 class DoubleValidator final : public Validator
182 {
183 public:
184  bool Validate(const wxVariant &v) override
185  {
186  double val;
187  if (!v.Convert(&val)) return false;
188  SetConverted(val);
189  return GetConverted().IsType(wxT("double"));
190  }
191  wxString GetDescription() const override
192  {
193  return wxT("a floating-point number");
194  }
195  Holder GetClone() const override
196  {
197  return std::make_unique<DoubleValidator>();
198  }
199 };
200 
201 class RangeValidator final : public Validator
202 {
203 private:
204  double mLower, mUpper;
205 public:
206  RangeValidator(double l, double u)
207  : mLower(l), mUpper(u)
208  { }
209  bool Validate(const wxVariant &v) override
210  {
211  double val;
212  if (!v.Convert(&val)) return false;
213  SetConverted(val);
214  return ((mLower < val) && (val < mUpper));
215  }
216  wxString GetDescription() const override
217  {
218  return wxString::Format(wxT("between %f and %f"), mLower, mUpper);
219  }
220  Holder GetClone() const override
221  {
222  return std::make_unique<RangeValidator>(mLower, mUpper);
223  }
224 };
225 
226 class IntValidator final : public Validator
227 {
228 public:
229  bool Validate(const wxVariant &v) override
230  {
231  double val;
232  if (!v.Convert(&val)) return false;
233  SetConverted(val);
234  if (!GetConverted().IsType(wxT("double"))) return false;
235  return ((long)val == val);
236  }
237  wxString GetDescription() const override
238  {
239  return wxT("an integer");
240  }
241  Holder GetClone() const override
242  {
243  return std::make_unique<IntValidator>();
244  }
245 };
246 
247 /*
248 class AndValidator final : public Validator
249 {
250 private:
251  Validator::Holder v1, v2;
252 public:
253  AndValidator(Validator::Holder &&u1, Validator::Holder &&u2)
254  : v1(std::move(u1)), v2(std::move(u2))
255  { }
256  bool Validate(const wxVariant &v) override
257  {
258  return v1->Validate(v) && v2->Validate(v);
259  }
260  wxString GetDescription() const override
261  {
262  return v1->GetDescription() + wxT(" and ") + v2->GetDescription();
263  }
264  Validator *GetClone() const override
265  {
266  return std::make_unique<AndValidator>(v1->GetClone(), v2->GetClone());
267  }
268 };*/
269 
270 #endif /* End of include guard: __VALIDATORS__ */
RangeValidator::GetClone
Holder GetClone() const override
Definition: Validators.h:220
DoubleValidator
Parameter must be a floating-point number.
Definition: Validators.h:182
OptionValidator::GetClone
Holder GetClone() const override
Definition: Validators.h:128
make_iterator_range
IteratorRange< Iterator > make_iterator_range(const Iterator &i1, const Iterator &i2)
Definition: MemoryX.h:549
Validator::GetDescription
virtual wxString GetDescription() const
Definition: Validators.h:78
BoolValidator::GetDescription
wxString GetDescription() const override
Definition: Validators.h:147
Validator::mConverted
wxVariant mConverted
Definition: Validators.h:55
OptionValidator::Validate
bool Validate(const wxVariant &v) override
Judge whether the passed value satisfies the Validator.
Definition: Validators.h:111
Validator
A Validator is an object which checks whether a wxVariant satisfies a certain criterion....
Definition: Validators.h:53
OptionValidator::mOptions
wxArrayString mOptions
Definition: Validators.h:100
RangeValidator::GetDescription
wxString GetDescription() const override
Definition: Validators.h:216
BoolValidator::GetClone
Holder GetClone() const override
Definition: Validators.h:151
RangeValidator::RangeValidator
RangeValidator(double l, double u)
Definition: Validators.h:206
IntValidator::GetClone
Holder GetClone() const override
Definition: Validators.h:241
DoubleValidator::Validate
bool Validate(const wxVariant &v) override
Judge whether the passed value satisfies the Validator.
Definition: Validators.h:184
Validator::GetConverted
const wxVariant & GetConverted()
Definition: Validators.h:64
desc
const TranslatableString desc
Definition: ExportPCM.cpp:58
DefaultValidator
Definition: Validators.h:89
BoolArrayValidator::GetClone
Holder GetClone() const override
Definition: Validators.h:175
IntValidator::GetDescription
wxString GetDescription() const override
Definition: Validators.h:237
DoubleValidator::GetDescription
wxString GetDescription() const override
Definition: Validators.h:191
BoolArrayValidator::Validate
virtual bool Validate(const wxVariant &v) override
Judge whether the passed value satisfies the Validator.
Definition: Validators.h:160
OptionValidator::AddOptions
void AddOptions(const wxArrayString &options)
Definition: Validators.h:107
IntValidator
Parameter must be integral.
Definition: Validators.h:227
BoolArrayValidator
Parameter must be char array of booleans, e.g. "011010001".
Definition: Validators.h:158
DefaultValidator::GetClone
virtual Holder GetClone() const
Definition: Validators.h:91
RangeValidator
Parameter must lie between the two given numbers.
Definition: Validators.h:202
OptionValidator
Parameter must be one of the defined options.
Definition: Validators.h:98
IntValidator::Validate
bool Validate(const wxVariant &v) override
Judge whether the passed value satisfies the Validator.
Definition: Validators.h:229
Validator::Validate
virtual bool Validate(const wxVariant &v)
Judge whether the passed value satisfies the Validator.
Definition: Validators.h:70
Validator::~Validator
virtual ~Validator()
Definition: Validators.h:59
BoolArrayValidator::GetDescription
wxString GetDescription() const override
Definition: Validators.h:171
RangeValidator::mLower
double mLower
Definition: Validators.h:204
BoolValidator::Validate
bool Validate(const wxVariant &v) override
Judge whether the passed value satisfies the Validator.
Definition: Validators.h:140
OptionValidator::AddOption
void AddOption(const wxString &option)
Definition: Validators.h:103
RangeValidator::Validate
bool Validate(const wxVariant &v) override
Judge whether the passed value satisfies the Validator.
Definition: Validators.h:209
OptionValidator::GetDescription
wxString GetDescription() const override
Definition: Validators.h:116
Validator::GetClone
virtual Holder GetClone() const =0
MemoryX.h
Validator::SetConverted
void SetConverted(const wxVariant &v)
Definition: Validators.h:60
BoolValidator
Parameter must be a boolean.
Definition: Validators.h:138
Validator::Validator
Validator()
Definition: Validators.h:58
RangeValidator::mUpper
double mUpper
Definition: Validators.h:204
DoubleValidator::GetClone
Holder GetClone() const override
Definition: Validators.h:195
Validator::Holder
std::unique_ptr< Validator > Holder
This MUST be overridden, to avoid slicing!
Definition: Validators.h:84