Audacity 3.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
46class wxArrayString;
47
48#include "MemoryX.h"
49
50#include <wx/variant.h> // member variable
51
52class Validator /* not final */
53{
54private:
55 wxVariant mConverted;
56
57public:
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
88class DefaultValidator final : public Validator
89{
90public:
91 virtual Holder GetClone() const
92 {
93 return std::make_unique<DefaultValidator>(*this);
94 }
95};
96
97class OptionValidator final : public Validator
98{
99private:
100 wxArrayString mOptions;
101
102public:
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
137class BoolValidator final : public Validator
138{
139public:
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
157class BoolArrayValidator final : public Validator
158{
159public:
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
181class DoubleValidator final : public Validator
182{
183public:
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
201class RangeValidator final : public Validator
202{
203private:
204 double mLower, mUpper;
205public:
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
226class IntValidator final : public Validator
227{
228public:
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/*
248class AndValidator final : public Validator
249{
250private:
251 Validator::Holder v1, v2;
252public:
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__ */
wxT("CloseDown"))
const TranslatableString desc
Definition: ExportPCM.cpp:58
IteratorRange< Iterator > make_iterator_range(const Iterator &i1, const Iterator &i2)
Definition: MemoryX.h:431
Parameter must be char array of booleans, e.g. "011010001".
Definition: Validators.h:158
wxString GetDescription() const override
Definition: Validators.h:171
virtual bool Validate(const wxVariant &v) override
Judge whether the passed value satisfies the Validator.
Definition: Validators.h:160
Holder GetClone() const override
Definition: Validators.h:175
Parameter must be a boolean.
Definition: Validators.h:138
bool Validate(const wxVariant &v) override
Judge whether the passed value satisfies the Validator.
Definition: Validators.h:140
Holder GetClone() const override
Definition: Validators.h:151
wxString GetDescription() const override
Definition: Validators.h:147
virtual Holder GetClone() const
Definition: Validators.h:91
Parameter must be a floating-point number.
Definition: Validators.h:182
bool Validate(const wxVariant &v) override
Judge whether the passed value satisfies the Validator.
Definition: Validators.h:184
Holder GetClone() const override
Definition: Validators.h:195
wxString GetDescription() const override
Definition: Validators.h:191
Parameter must be integral.
Definition: Validators.h:227
bool Validate(const wxVariant &v) override
Judge whether the passed value satisfies the Validator.
Definition: Validators.h:229
Holder GetClone() const override
Definition: Validators.h:241
wxString GetDescription() const override
Definition: Validators.h:237
Parameter must be one of the defined options.
Definition: Validators.h:98
bool Validate(const wxVariant &v) override
Judge whether the passed value satisfies the Validator.
Definition: Validators.h:111
wxArrayString mOptions
Definition: Validators.h:100
void AddOption(const wxString &option)
Definition: Validators.h:103
Holder GetClone() const override
Definition: Validators.h:128
wxString GetDescription() const override
Definition: Validators.h:116
void AddOptions(const wxArrayString &options)
Definition: Validators.h:107
Parameter must lie between the two given numbers.
Definition: Validators.h:202
RangeValidator(double l, double u)
Definition: Validators.h:206
Holder GetClone() const override
Definition: Validators.h:220
bool Validate(const wxVariant &v) override
Judge whether the passed value satisfies the Validator.
Definition: Validators.h:209
wxString GetDescription() const override
Definition: Validators.h:216
A Validator is an object which checks whether a wxVariant satisfies a certain criterion....
Definition: Validators.h:53
virtual Holder GetClone() const =0
virtual wxString GetDescription() const
Definition: Validators.h:78
const wxVariant & GetConverted()
Definition: Validators.h:64
virtual ~Validator()
Definition: Validators.h:59
void SetConverted(const wxVariant &v)
Definition: Validators.h:60
std::unique_ptr< Validator > Holder
This MUST be overridden, to avoid slicing!
Definition: Validators.h:84
wxVariant mConverted
Definition: Validators.h:55
virtual bool Validate(const wxVariant &v)
Judge whether the passed value satisfies the Validator.
Definition: Validators.h:70