EnumSetting has a template parameter

This commit is contained in:
Paul Licameli 2019-04-03 09:17:16 -04:00
parent 85b06fe6d0
commit 0d910bbe02
8 changed files with 79 additions and 37 deletions

View File

@ -403,14 +403,14 @@ static const std::initializer_list<EnumValueSymbol> choicesDither{
{ XO("Triangle") },
{ XO("Shaped") },
};
static const std::initializer_list<int> intChoicesDither{
static auto intChoicesDither = {
DitherType::none,
DitherType::rectangle,
DitherType::triangle,
DitherType::shaped,
};
EnumSetting Dither::FastSetting{
EnumSetting< DitherType > Dither::FastSetting{
wxT("Quality/DitherAlgorithmChoice"),
choicesDither,
0, // none
@ -420,7 +420,7 @@ EnumSetting Dither::FastSetting{
wxT("Quality/DitherAlgorithm")
};
EnumSetting Dither::BestSetting{
EnumSetting< DitherType > Dither::BestSetting{
wxT("Quality/HQDitherAlgorithmChoice"),
choicesDither,
3, // shaped
@ -432,10 +432,10 @@ EnumSetting Dither::BestSetting{
DitherType Dither::FastDitherChoice()
{
return (DitherType) FastSetting.ReadInt();
return (DitherType) FastSetting.ReadEnum();
}
DitherType Dither::BestDitherChoice()
{
return (DitherType) BestSetting.ReadInt();
return (DitherType) BestSetting.ReadEnum();
}

View File

@ -12,7 +12,7 @@
#include "audacity/Types.h" // for samplePtr
class EnumSetting;
template< typename Enum > class EnumSetting;
/// These ditherers are currently available:
@ -25,7 +25,7 @@ public:
static DitherType FastDitherChoice();
static DitherType BestDitherChoice();
static EnumSetting FastSetting, BestSetting;
static EnumSetting< DitherType > FastSetting, BestSetting;
/// Default constructor
Dither();

View File

@ -331,7 +331,7 @@ bool ChoiceSetting::Write( const wxString &value )
return result;
}
EnumSetting::EnumSetting(
EnumSettingBase::EnumSettingBase(
const wxString &key,
EnumValueSymbols symbols,
long defaultSymbol,
@ -358,7 +358,7 @@ void ChoiceSetting::SetDefault( long value )
wxASSERT( false );
}
int EnumSetting::ReadInt() const
int EnumSettingBase::ReadInt() const
{
auto index = Find( Read() );
@ -366,7 +366,7 @@ int EnumSetting::ReadInt() const
return mIntValues[ index ];
}
int EnumSetting::ReadIntWithDefault( int defaultValue ) const
int EnumSettingBase::ReadIntWithDefault( int defaultValue ) const
{
wxString defaultString;
auto index0 = FindInt( defaultValue );
@ -381,7 +381,7 @@ int EnumSetting::ReadIntWithDefault( int defaultValue ) const
return mIntValues[ index ];
}
size_t EnumSetting::FindInt( int code ) const
size_t EnumSettingBase::FindInt( int code ) const
{
const auto start = mIntValues.begin();
return size_t(
@ -389,7 +389,7 @@ size_t EnumSetting::FindInt( int code ) const
- start );
}
void EnumSetting::Migrate( wxString &value )
void EnumSettingBase::Migrate( wxString &value )
{
int intValue = 0;
if ( !mOldKey.empty() &&
@ -409,7 +409,7 @@ void EnumSetting::Migrate( wxString &value )
}
}
bool EnumSetting::WriteInt( int code ) // you flush gPrefs afterward
bool EnumSettingBase::WriteInt( int code ) // you flush gPrefs afterward
{
auto index = FindInt( code );
if ( index >= mSymbols.size() )

View File

@ -171,10 +171,10 @@ protected:
/// (generally not equal to their table positions),
/// and optionally an old preference key path that stored integer codes, to be
/// migrated into one that stores internal string values instead
class EnumSetting : public ChoiceSetting
class EnumSettingBase : public ChoiceSetting
{
public:
EnumSetting(
EnumSettingBase(
const wxString &key,
EnumValueSymbols symbols,
long defaultSymbol,
@ -183,6 +183,8 @@ public:
const wxString &oldKey
);
protected:
// Read and write the encoded values
int ReadInt() const;
@ -193,7 +195,6 @@ public:
bool WriteInt( int code ); // you flush gPrefs afterward
protected:
size_t FindInt( int code ) const;
void Migrate( wxString& ) override;
@ -202,6 +203,45 @@ private:
const wxString mOldKey;
};
/// Adapts EnumSettingBase to a particular enumeration type
template< typename Enum >
class EnumSetting : public EnumSettingBase
{
public:
EnumSetting(
const wxString &key,
EnumValueSymbols symbols,
long defaultSymbol,
std::initializer_list< Enum > values, // must have same size as symbols
const wxString &oldKey
)
: EnumSettingBase{
key, symbols, defaultSymbol,
{ values.begin(), values.end() },
oldKey
}
{}
// Wrap ReadInt() and ReadIntWithDefault() and WriteInt()
Enum ReadEnum() const
{ return static_cast<Enum>( ReadInt() ); }
// new direct use is discouraged but it may be needed in legacy code:
// use a default in case the preference is not defined, which may not be
// the default-default stored in this object.
Enum ReadEnumWithDefault( Enum defaultValue ) const
{
auto integer = static_cast<int>(defaultValue);
return static_cast<Enum>( ReadIntWithDefault( integer ) );
}
bool WriteEnum( Enum value )
{ return WriteInt( static_cast<int>( value ) ); }
};
// An event emitted by the application when the Preference dialog commits
// changes
wxDECLARE_EVENT(EVT_PREFS_UPDATE, wxCommandEvent);

View File

@ -63,7 +63,7 @@ static auto intChoicesMethod = {
0, 1, 2, 3
};
EnumSetting Resample::FastMethodSetting{
EnumSetting< int > Resample::FastMethodSetting{
wxT("/Quality/LibsoxrSampleRateConverterChoice"),
methodNames,
1, // Medium Quality
@ -73,7 +73,7 @@ EnumSetting Resample::FastMethodSetting{
wxT("/Quality/LibsoxrSampleRateConverter")
};
EnumSetting Resample::BestMethodSetting
EnumSetting< int > Resample::BestMethodSetting
{
wxT("/Quality/LibsoxrHQSampleRateConverterChoice"),
methodNames,
@ -115,7 +115,7 @@ std::pair<size_t, size_t>
void Resample::SetMethod(const bool useBestMethod)
{
if (useBestMethod)
mMethod = BestMethodSetting.ReadInt();
mMethod = BestMethodSetting.ReadEnum();
else
mMethod = FastMethodSetting.ReadInt();
mMethod = FastMethodSetting.ReadEnum();
}

View File

@ -16,7 +16,7 @@
#include "SampleFormat.h"
class EnumSetting;
template< typename Enum > class EnumSetting;
struct soxr;
extern "C" void soxr_delete(soxr*);
@ -41,8 +41,8 @@ class Resample final
Resample(const bool useBestMethod, const double dMinFactor, const double dMaxFactor);
~Resample();
static EnumSetting FastMethodSetting;
static EnumSetting BestMethodSetting;
static EnumSetting< int > FastMethodSetting;
static EnumSetting< int > BestMethodSetting;
/** @brief Main processing function. Resamples from the input buffer to the
* output buffer.

View File

@ -32,7 +32,7 @@
//////////
static EnumSetting formatSetting{
static EnumSetting< sampleFormat > formatSetting{
wxT("/SamplingRate/DefaultProjectSampleFormatChoice"),
{
{ wxT("Format16Bit"), XO("16-bit") },
@ -241,6 +241,6 @@ QualityPrefsFactory = [](wxWindow *parent, wxWindowID winid)
sampleFormat QualityPrefs::SampleFormatChoice()
{
return (sampleFormat)formatSetting.ReadInt();
return formatSetting.ReadEnum();
}

View File

@ -54,14 +54,15 @@ namespace {
//////////
class TracksViewModeEnumSetting : public EnumSetting {
class TracksViewModeEnumSetting
: public EnumSetting< WaveTrackViewConstants::Display > {
public:
TracksViewModeEnumSetting(
const wxString &key,
EnumValueSymbols symbols,
long defaultSymbol,
std::vector<int> intValues,
std::initializer_list< WaveTrackViewConstants::Display > intValues,
const wxString &oldKey
)
: EnumSetting{
@ -120,11 +121,12 @@ static TracksViewModeEnumSetting viewModeSetting{
WaveTrackViewConstants::Display TracksPrefs::ViewModeChoice()
{
return (WaveTrackViewConstants::Display) viewModeSetting.ReadInt();
return viewModeSetting.ReadEnum();
}
//////////
static EnumSetting sampleDisplaySetting{
static EnumSetting< WaveTrackViewConstants::SampleDisplay >
sampleDisplaySetting{
wxT("/GUI/SampleViewChoice"),
{
{ wxT("ConnectDots"), XO("Connect dots") },
@ -142,7 +144,7 @@ static EnumSetting sampleDisplaySetting{
WaveTrackViewConstants::SampleDisplay TracksPrefs::SampleViewChoice()
{
return (WaveTrackViewConstants::SampleDisplay) sampleDisplaySetting.ReadInt();
return sampleDisplaySetting.ReadEnum();
}
//////////
@ -163,7 +165,7 @@ static const std::initializer_list<EnumValueSymbol> choicesZoom{
{ wxT("FourPixelsPerSample"), XO("4 Pixels per Sample") },
{ wxT("MaxZoom"), XO("Max Zoom") },
};
static const std::initializer_list<int> intChoicesZoom{
static auto enumChoicesZoom = {
WaveTrackViewConstants::kZoomToFit,
WaveTrackViewConstants::kZoomToSelection,
WaveTrackViewConstants::kZoomDefault,
@ -181,34 +183,34 @@ static const std::initializer_list<int> intChoicesZoom{
WaveTrackViewConstants::kMaxZoom,
};
static EnumSetting zoom1Setting{
static EnumSetting< WaveTrackViewConstants::ZoomPresets > zoom1Setting{
wxT("/GUI/ZoomPreset1Choice"),
choicesZoom,
2, // kZoomDefault
// for migrating old preferences:
intChoicesZoom,
enumChoicesZoom,
wxT("/GUI/ZoomPreset1")
};
static EnumSetting zoom2Setting{
static EnumSetting< WaveTrackViewConstants::ZoomPresets > zoom2Setting{
wxT("/GUI/ZoomPreset2Choice"),
choicesZoom,
13, // kZoom4To1
// for migrating old preferences:
intChoicesZoom,
enumChoicesZoom,
wxT("/GUI/ZoomPreset2")
};
WaveTrackViewConstants::ZoomPresets TracksPrefs::Zoom1Choice()
{
return (WaveTrackViewConstants::ZoomPresets) zoom1Setting.ReadInt();
return zoom1Setting.ReadEnum();
}
WaveTrackViewConstants::ZoomPresets TracksPrefs::Zoom2Choice()
{
return (WaveTrackViewConstants::ZoomPresets) zoom2Setting.ReadInt();
return zoom2Setting.ReadEnum();
}
//////////