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