diff --git a/stepmania/src/Preference.cpp b/stepmania/src/Preference.cpp index 395af56175..1f328e7866 100644 --- a/stepmania/src/Preference.cpp +++ b/stepmania/src/Preference.cpp @@ -10,31 +10,54 @@ static const CString PrefsGroupNames[NUM_PREFS_GROUPS] = { }; XToString( PrefsGroup ); -void SubscribePreference( IPreference *p ) +IPreference::IPreference( PrefsGroup PrefsGroup, const CString& sName ): + m_PrefsGroup( PrefsGroup ), + m_sName( sName ) { - PrefsManager::Subscribe( p ); + PrefsManager::Subscribe( this ); } -void UnsubscribePreference( IPreference *p ) +IPreference::~IPreference() { - PrefsManager::Unsubscribe( p ); + PrefsManager::Unsubscribe( this ); +} + +void Preference::FromString( const CString &s ) +{ + m_currentValue = s; +} + +CString Preference::ToString() const +{ + return m_currentValue; } #define READFROM_AND_WRITETO( type ) \ - void Preference::ReadFrom( const IniFile &ini ) \ + void Preference::FromString( const CString &s ) \ { \ - ini.GetValue( PrefsGroupToString(m_PrefsGroup), m_sName, m_currentValue ); \ + ::FromString( s, m_currentValue ); \ } \ - void Preference::WriteTo( IniFile &ini ) const \ + CString Preference::ToString() const \ { \ - ini.SetValue( PrefsGroupToString(m_PrefsGroup), m_sName, m_currentValue ); \ + return ::ToString( m_currentValue ); \ } \ -READFROM_AND_WRITETO( CString ) READFROM_AND_WRITETO( int ) READFROM_AND_WRITETO( float ) READFROM_AND_WRITETO( bool ) +void IPreference::ReadFrom( const IniFile &ini ) +{ + CString sVal; + if( ini.GetValue( PrefsGroupToString(m_PrefsGroup), m_sName, sVal ) ) + FromString( sVal ); +} + +void IPreference::WriteTo( IniFile &ini ) const +{ + ini.SetValue( PrefsGroupToString(m_PrefsGroup), m_sName, ToString() ); +} + /* * (c) 2001-2004 Chris Danford, Chris Gomez * All rights reserved. diff --git a/stepmania/src/Preference.h b/stepmania/src/Preference.h index 386234fac5..b716f87845 100644 --- a/stepmania/src/Preference.h +++ b/stepmania/src/Preference.h @@ -19,14 +19,23 @@ const CString& PrefsGroupToString( PrefsGroup pg ); class IPreference { public: - virtual ~IPreference() { } - virtual void LoadDefault() = 0; - virtual void ReadFrom( const IniFile &ini ) = 0; - virtual void WriteTo( IniFile &ini ) const = 0; -}; + IPreference( PrefsGroup PrefsGroup, const CString& sName ); + virtual ~IPreference(); -void SubscribePreference( IPreference *p ); -void UnsubscribePreference( IPreference *p ); + virtual void LoadDefault() = 0; + virtual void ReadFrom( const IniFile &ini ); + virtual void WriteTo( IniFile &ini ) const; + + virtual CString ToString() const = 0; + virtual void FromString( const CString &s ) = 0; + + PrefsGroup GetPrefsGroup() const { return m_PrefsGroup; } + const CString &GetName() const { return m_sName; } + +protected: + PrefsGroup m_PrefsGroup; + CString m_sName; +}; template class Preference : public IPreference @@ -35,37 +44,25 @@ private: // Make currentValue first in the list so that we can pass this object // as an argument in a var_arg function as in printf. T m_currentValue; - PrefsGroup m_PrefsGroup; - CString m_sName; T m_defaultValue; - CString ToString(); - void FromString( const CString &s ); - public: Preference( PrefsGroup PrefsGroup, const CString& sName, const T& defaultValue ): + IPreference( PrefsGroup, sName ), m_currentValue( defaultValue ), - m_PrefsGroup( PrefsGroup ), - m_sName( sName ), m_defaultValue( defaultValue ) { - SubscribePreference( this ); LoadDefault(); } - ~Preference() - { - UnsubscribePreference( this ); - } + CString ToString() const; + void FromString( const CString &s ); void LoadDefault() { m_currentValue = m_defaultValue; } - void ReadFrom( const IniFile &ini ); - void WriteTo( IniFile &ini ) const; - operator T () const { return m_currentValue;