audacia/src/commands/Validators.h

267 lines
6.3 KiB
C
Raw Normal View History

/**********************************************************************
Audacity - A Digital Audio Editor
Copyright 1999-2009 Audacity Team
License: wxWidgets
Dan Horgan
******************************************************************//**
\file Validators.h
\brief Contains declarations and definitions for Validator, OptionValidator,
BoolValidator, DoubleValidator, RangeValidator, IntValidator and AndValidator
classes.
\class Validator
\brief A Validator is an object which checks whether a wxVariant satisfies
a certain criterion. This is a base validator which allows anything.
\class OptionValidator
\brief Parameter must be one of the defined options
\class BoolValidator
\brief Parameter must be a boolean
\class BoolArrayValidator
\brief Parameter must be char array of booleans, e.g. "011010001"
\class DoubleValidator
\brief Parameter must be a floating-point number
\class RangeValidator
\brief Parameter must lie between the two given numbers
\class IntValidator
\brief Parameter must be integral
\class AndValidator
\brief Parameter must pass both of the supplied validators
*//*******************************************************************/
#ifndef __VALIDATORS__
#define __VALIDATORS__
#include "../MemoryX.h"
class Validator /* not final */
{
private:
wxVariant mConverted;
public:
Validator() {};
virtual ~Validator() {};
void SetConverted (const wxVariant &v)
{
mConverted = v;
}
const wxVariant &GetConverted()
{
return mConverted;
}
/// Judge whether the passed value satisfies the Validator
virtual bool Validate(const wxVariant &v)
{
SetConverted(v);
return true;
}
/// Return a description (for error messages)
/// should be of the form 'v must be $description'
virtual wxString GetDescription() const
{
return wxT("any value");
}
/// This MUST be overridden, to avoid slicing!
using Holder = std::unique_ptr<Validator>;
virtual Holder GetClone() const = 0;
};
class DefaultValidator final : public Validator
{
public:
virtual Holder GetClone() const
{
return std::make_unique<DefaultValidator>(*this);
}
};
class OptionValidator final : public Validator
{
private:
wxArrayString mOptions;
public:
void AddOption(const wxString &option)
{
mOptions.push_back(option);
}
void AddOptions(const wxArrayString &options)
{
mOptions.insert(mOptions.begin(), options.begin(), options.end());
}
bool Validate(const wxVariant &v) override
{
SetConverted(v);
return (mOptions.Index(v.GetString()) != wxNOT_FOUND);
}
wxString GetDescription() const override
{
wxString desc = wxT("one of: ");
int optionCount = mOptions.size();
int i = 0;
for (i = 0; i+1 < optionCount; ++i)
{
desc += mOptions[i] + wxT(", ");
}
desc += mOptions[optionCount-1];
return desc;
}
Holder GetClone() const override
{
auto v = std::make_unique<OptionValidator>();
v->mOptions = mOptions;
// This std::move is needed to "upcast" the pointer type
return std::move(v);
}
};
class BoolValidator final : public Validator
{
public:
bool Validate(const wxVariant &v) override
{
bool val;
if (!v.Convert(&val)) return false;
SetConverted(val);
return GetConverted().IsType(wxT("bool"));
}
wxString GetDescription() const override
{
return wxT("true/false or 1/0 or yes/no");
}
Holder GetClone() const override
{
return std::make_unique<BoolValidator>();
}
};
class BoolArrayValidator final : public Validator
{
public:
virtual bool Validate(const wxVariant &v) override
{
wxString val; // Validate a string of chars containing only 0, 1 and x.
2014-06-03 20:30:19 +00:00
if (!v.Convert(&val))
return false;
2014-06-03 20:30:19 +00:00
SetConverted(val);
for(size_t i=0; i != val.Len(); i++)
if( val[i] != '0' && val[i] != '1' && val[i] != 'x' && val[i] != 'X')
return false;
return true;
}
wxString GetDescription() const override
{
2018-10-15 16:54:44 +00:00
return wxT("0X101XX101...etc. where 0=false, 1=true, and X=don't care. Numbering starts at leftmost = track 0");
}
Holder GetClone() const override
{
return std::make_unique<BoolArrayValidator>();
}
};
class DoubleValidator final : public Validator
{
public:
bool Validate(const wxVariant &v) override
{
double val;
if (!v.Convert(&val)) return false;
SetConverted(val);
return GetConverted().IsType(wxT("double"));
}
wxString GetDescription() const override
{
return wxT("a floating-point number");
}
Holder GetClone() const override
{
return std::make_unique<DoubleValidator>();
}
};
class RangeValidator final : public Validator
{
private:
double mLower, mUpper;
public:
RangeValidator(double l, double u)
: mLower(l), mUpper(u)
{ }
bool Validate(const wxVariant &v) override
{
double val;
if (!v.Convert(&val)) return false;
SetConverted(val);
return ((mLower < val) && (val < mUpper));
}
wxString GetDescription() const override
{
From: martin@steghoefer.eu [PATCHES 02-15 of 15] Fix runtime problem with wxWidgets 3.0: Correct string formatting for: 2/15 %d + enum => %d + int 3/15 %lld + int64_t => %lld + long long 4/15 %d + int64_t => %lld + long long 5/15 %d + double => %f + double 6/15 %d + int32_t => %d + int 7/15 %d + intptr_t => %p + void* 8/15 gint, guint 9/15 %d + long => %ld + long 10/15 %n + int => %d + int 11/15 %x + int => %x + unsigned int 12/15 %f + int => %d + int 13/15 %S + wxChar* => %s + wxChar* 14/15 %d + size_t => %d + int 15/15 %d + size_t => %lld + long long "The functions wxString::Format, wxString::Printf (and others indirectly) have become stricter about parameter types that don't match (format specifier vs. function parameters). So the bugs (that were already present in audacity before) become visible in wx3.0 as error message dialogs. I've checked all occurrences of Printf, wxPrintf, PrintfV, Format, FormatV, wxLogDebug and wxLogError systematically and made the type match." Note (9/15): In TrackPanel.cpp, ExportMP2.cpp and CompareAudioCommand.cpp this patch supersedes related change done in r13466 because the new solution requires fewer casts and therefore simplifies the code. Note: Many .po files are affected, and we need to be very careful about this. Incorrect "%d" and similar in translation files may lead to crashes in those languages (only). This is something we should actually have been more careful about in the past. We need to write a script to check that the "%d" and similar format specifiers match between English and translation.
2014-11-08 16:42:34 +00:00
return wxString::Format(wxT("between %f and %f"), mLower, mUpper);
}
Holder GetClone() const override
{
return std::make_unique<RangeValidator>(mLower, mUpper);
}
};
class IntValidator final : public Validator
{
public:
bool Validate(const wxVariant &v) override
{
double val;
if (!v.Convert(&val)) return false;
SetConverted(val);
if (!GetConverted().IsType(wxT("double"))) return false;
return ((long)val == val);
}
wxString GetDescription() const override
{
return wxT("an integer");
}
Holder GetClone() const override
{
return std::make_unique<IntValidator>();
}
};
/*
class AndValidator final : public Validator
{
private:
Validator::Holder v1, v2;
public:
AndValidator(Validator::Holder &&u1, Validator::Holder &&u2)
: v1(std::move(u1)), v2(std::move(u2))
{ }
bool Validate(const wxVariant &v) override
{
return v1->Validate(v) && v2->Validate(v);
}
wxString GetDescription() const override
{
return v1->GetDescription() + wxT(" and ") + v2->GetDescription();
}
Validator *GetClone() const override
{
return std::make_unique<AndValidator>(v1->GetClone(), v2->GetClone());
}
};*/
#endif /* End of include guard: __VALIDATORS__ */