move more common stuff out of the template class, so g_pvpSubscribers is

more useful
This commit is contained in:
Glenn Maynard
2004-12-07 01:44:17 +00:00
parent c37a82ed40
commit ba929b8e71
2 changed files with 51 additions and 31 deletions
+32 -9
View File
@@ -10,31 +10,54 @@ static const CString PrefsGroupNames[NUM_PREFS_GROUPS] = {
}; };
XToString( PrefsGroup ); 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<CString>::FromString( const CString &s )
{
m_currentValue = s;
}
CString Preference<CString>::ToString() const
{
return m_currentValue;
} }
#define READFROM_AND_WRITETO( type ) \ #define READFROM_AND_WRITETO( type ) \
void Preference<type>::ReadFrom( const IniFile &ini ) \ void Preference<type>::FromString( const CString &s ) \
{ \ { \
ini.GetValue( PrefsGroupToString(m_PrefsGroup), m_sName, m_currentValue ); \ ::FromString( s, m_currentValue ); \
} \ } \
void Preference<type>::WriteTo( IniFile &ini ) const \ CString Preference<type>::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( int )
READFROM_AND_WRITETO( float ) READFROM_AND_WRITETO( float )
READFROM_AND_WRITETO( bool ) 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 * (c) 2001-2004 Chris Danford, Chris Gomez
* All rights reserved. * All rights reserved.
+19 -22
View File
@@ -19,14 +19,23 @@ const CString& PrefsGroupToString( PrefsGroup pg );
class IPreference class IPreference
{ {
public: public:
virtual ~IPreference() { } IPreference( PrefsGroup PrefsGroup, const CString& sName );
virtual void LoadDefault() = 0; virtual ~IPreference();
virtual void ReadFrom( const IniFile &ini ) = 0;
virtual void WriteTo( IniFile &ini ) const = 0;
};
void SubscribePreference( IPreference *p ); virtual void LoadDefault() = 0;
void UnsubscribePreference( IPreference *p ); 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 T> template <class T>
class Preference : public IPreference class Preference : public IPreference
@@ -35,37 +44,25 @@ private:
// Make currentValue first in the list so that we can pass this object // Make currentValue first in the list so that we can pass this object
// as an argument in a var_arg function as in printf. // as an argument in a var_arg function as in printf.
T m_currentValue; T m_currentValue;
PrefsGroup m_PrefsGroup;
CString m_sName;
T m_defaultValue; T m_defaultValue;
CString ToString();
void FromString( const CString &s );
public: public:
Preference( PrefsGroup PrefsGroup, const CString& sName, const T& defaultValue ): Preference( PrefsGroup PrefsGroup, const CString& sName, const T& defaultValue ):
IPreference( PrefsGroup, sName ),
m_currentValue( defaultValue ), m_currentValue( defaultValue ),
m_PrefsGroup( PrefsGroup ),
m_sName( sName ),
m_defaultValue( defaultValue ) m_defaultValue( defaultValue )
{ {
SubscribePreference( this );
LoadDefault(); LoadDefault();
} }
~Preference() CString ToString() const;
{ void FromString( const CString &s );
UnsubscribePreference( this );
}
void LoadDefault() void LoadDefault()
{ {
m_currentValue = m_defaultValue; m_currentValue = m_defaultValue;
} }
void ReadFrom( const IniFile &ini );
void WriteTo( IniFile &ini ) const;
operator T () const operator T () const
{ {
return m_currentValue; return m_currentValue;