diff --git a/src/Dither.cpp b/src/Dither.cpp index 3e1eb1c37..246c74861 100644 --- a/src/Dither.cpp +++ b/src/Dither.cpp @@ -403,14 +403,14 @@ static const std::initializer_list choicesDither{ { XO("Triangle") }, { XO("Shaped") }, }; -static const std::initializer_list 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(); } diff --git a/src/Dither.h b/src/Dither.h index 4e349c424..69e61f4f4 100644 --- a/src/Dither.h +++ b/src/Dither.h @@ -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(); diff --git a/src/Prefs.cpp b/src/Prefs.cpp index 40edb3b40..8a7b327c6 100755 --- a/src/Prefs.cpp +++ b/src/Prefs.cpp @@ -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() ) diff --git a/src/Prefs.h b/src/Prefs.h index e160bf693..1154128cc 100644 --- a/src/Prefs.h +++ b/src/Prefs.h @@ -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( 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(defaultValue); + return static_cast( ReadIntWithDefault( integer ) ); + } + + bool WriteEnum( Enum value ) + { return WriteInt( static_cast( value ) ); } + +}; + // An event emitted by the application when the Preference dialog commits // changes wxDECLARE_EVENT(EVT_PREFS_UPDATE, wxCommandEvent); diff --git a/src/Resample.cpp b/src/Resample.cpp index 28b4f7c0c..ece7db087 100644 --- a/src/Resample.cpp +++ b/src/Resample.cpp @@ -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 void Resample::SetMethod(const bool useBestMethod) { if (useBestMethod) - mMethod = BestMethodSetting.ReadInt(); + mMethod = BestMethodSetting.ReadEnum(); else - mMethod = FastMethodSetting.ReadInt(); + mMethod = FastMethodSetting.ReadEnum(); } diff --git a/src/Resample.h b/src/Resample.h index 0a9bf246e..e3ea3225c 100644 --- a/src/Resample.h +++ b/src/Resample.h @@ -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. diff --git a/src/prefs/QualityPrefs.cpp b/src/prefs/QualityPrefs.cpp index 856ae459c..9fa77112e 100644 --- a/src/prefs/QualityPrefs.cpp +++ b/src/prefs/QualityPrefs.cpp @@ -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(); } diff --git a/src/prefs/TracksPrefs.cpp b/src/prefs/TracksPrefs.cpp index b06ff91cf..7897d4ac9 100644 --- a/src/prefs/TracksPrefs.cpp +++ b/src/prefs/TracksPrefs.cpp @@ -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 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 choicesZoom{ { wxT("FourPixelsPerSample"), XO("4 Pixels per Sample") }, { wxT("MaxZoom"), XO("Max Zoom") }, }; -static const std::initializer_list intChoicesZoom{ +static auto enumChoicesZoom = { WaveTrackViewConstants::kZoomToFit, WaveTrackViewConstants::kZoomToSelection, WaveTrackViewConstants::kZoomDefault, @@ -181,34 +183,34 @@ static const std::initializer_list 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(); } //////////