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