1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-08-07 07:39:29 +02:00

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

View File

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

View File

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

View File

@ -171,10 +171,10 @@ protected:
/// (generally not equal to their table positions), /// (generally not equal to their table positions),
/// and optionally an old preference key path that stored integer codes, to be /// and optionally an old preference key path that stored integer codes, to be
/// migrated into one that stores internal string values instead /// migrated into one that stores internal string values instead
class EnumSetting : public ChoiceSetting class EnumSettingBase : public ChoiceSetting
{ {
public: public:
EnumSetting( EnumSettingBase(
const wxString &key, const wxString &key,
EnumValueSymbols symbols, EnumValueSymbols symbols,
long defaultSymbol, long defaultSymbol,
@ -183,6 +183,8 @@ public:
const wxString &oldKey const wxString &oldKey
); );
protected:
// Read and write the encoded values // Read and write the encoded values
int ReadInt() const; int ReadInt() const;
@ -193,7 +195,6 @@ public:
bool WriteInt( int code ); // you flush gPrefs afterward bool WriteInt( int code ); // you flush gPrefs afterward
protected:
size_t FindInt( int code ) const; size_t FindInt( int code ) const;
void Migrate( wxString& ) override; void Migrate( wxString& ) override;
@ -202,6 +203,45 @@ private:
const wxString mOldKey; 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 // An event emitted by the application when the Preference dialog commits
// changes // changes
wxDECLARE_EVENT(EVT_PREFS_UPDATE, wxCommandEvent); wxDECLARE_EVENT(EVT_PREFS_UPDATE, wxCommandEvent);

View File

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

View File

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

View File

@ -32,7 +32,7 @@
////////// //////////
static EnumSetting formatSetting{ static EnumSetting< sampleFormat > formatSetting{
wxT("/SamplingRate/DefaultProjectSampleFormatChoice"), wxT("/SamplingRate/DefaultProjectSampleFormatChoice"),
{ {
{ wxT("Format16Bit"), XO("16-bit") }, { wxT("Format16Bit"), XO("16-bit") },
@ -241,6 +241,6 @@ QualityPrefsFactory = [](wxWindow *parent, wxWindowID winid)
sampleFormat QualityPrefs::SampleFormatChoice() 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: public:
TracksViewModeEnumSetting( TracksViewModeEnumSetting(
const wxString &key, const wxString &key,
EnumValueSymbols symbols, EnumValueSymbols symbols,
long defaultSymbol, long defaultSymbol,
std::vector<int> intValues, std::initializer_list< WaveTrackViewConstants::Display > intValues,
const wxString &oldKey const wxString &oldKey
) )
: EnumSetting{ : EnumSetting{
@ -120,11 +121,12 @@ static TracksViewModeEnumSetting viewModeSetting{
WaveTrackViewConstants::Display TracksPrefs::ViewModeChoice() WaveTrackViewConstants::Display TracksPrefs::ViewModeChoice()
{ {
return (WaveTrackViewConstants::Display) viewModeSetting.ReadInt(); return viewModeSetting.ReadEnum();
} }
////////// //////////
static EnumSetting sampleDisplaySetting{ static EnumSetting< WaveTrackViewConstants::SampleDisplay >
sampleDisplaySetting{
wxT("/GUI/SampleViewChoice"), wxT("/GUI/SampleViewChoice"),
{ {
{ wxT("ConnectDots"), XO("Connect dots") }, { wxT("ConnectDots"), XO("Connect dots") },
@ -142,7 +144,7 @@ static EnumSetting sampleDisplaySetting{
WaveTrackViewConstants::SampleDisplay TracksPrefs::SampleViewChoice() 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("FourPixelsPerSample"), XO("4 Pixels per Sample") },
{ wxT("MaxZoom"), XO("Max Zoom") }, { wxT("MaxZoom"), XO("Max Zoom") },
}; };
static const std::initializer_list<int> intChoicesZoom{ static auto enumChoicesZoom = {
WaveTrackViewConstants::kZoomToFit, WaveTrackViewConstants::kZoomToFit,
WaveTrackViewConstants::kZoomToSelection, WaveTrackViewConstants::kZoomToSelection,
WaveTrackViewConstants::kZoomDefault, WaveTrackViewConstants::kZoomDefault,
@ -181,34 +183,34 @@ static const std::initializer_list<int> intChoicesZoom{
WaveTrackViewConstants::kMaxZoom, WaveTrackViewConstants::kMaxZoom,
}; };
static EnumSetting zoom1Setting{ static EnumSetting< WaveTrackViewConstants::ZoomPresets > zoom1Setting{
wxT("/GUI/ZoomPreset1Choice"), wxT("/GUI/ZoomPreset1Choice"),
choicesZoom, choicesZoom,
2, // kZoomDefault 2, // kZoomDefault
// for migrating old preferences: // for migrating old preferences:
intChoicesZoom, enumChoicesZoom,
wxT("/GUI/ZoomPreset1") wxT("/GUI/ZoomPreset1")
}; };
static EnumSetting zoom2Setting{ static EnumSetting< WaveTrackViewConstants::ZoomPresets > zoom2Setting{
wxT("/GUI/ZoomPreset2Choice"), wxT("/GUI/ZoomPreset2Choice"),
choicesZoom, choicesZoom,
13, // kZoom4To1 13, // kZoom4To1
// for migrating old preferences: // for migrating old preferences:
intChoicesZoom, enumChoicesZoom,
wxT("/GUI/ZoomPreset2") wxT("/GUI/ZoomPreset2")
}; };
WaveTrackViewConstants::ZoomPresets TracksPrefs::Zoom1Choice() WaveTrackViewConstants::ZoomPresets TracksPrefs::Zoom1Choice()
{ {
return (WaveTrackViewConstants::ZoomPresets) zoom1Setting.ReadInt(); return zoom1Setting.ReadEnum();
} }
WaveTrackViewConstants::ZoomPresets TracksPrefs::Zoom2Choice() WaveTrackViewConstants::ZoomPresets TracksPrefs::Zoom2Choice()
{ {
return (WaveTrackViewConstants::ZoomPresets) zoom2Setting.ReadInt(); return zoom2Setting.ReadEnum();
} }
////////// //////////