00001 /* Preference - Holds user-chosen preferences that are saved between sessions. */ 00002 00003 #ifndef PREFERENCE_H 00004 #define PREFERENCE_H 00005 00006 #include "EnumHelper.h" 00007 class IniFile; 00008 00009 enum PrefsGroup 00010 { 00011 Debug, 00012 Editor, 00013 Options, 00014 NUM_PREFS_GROUPS 00015 }; 00016 const CString& PrefsGroupToString( PrefsGroup pg ); 00017 00018 00019 class IPreference 00020 { 00021 public: 00022 IPreference( PrefsGroup PrefsGroup, const CString& sName ); 00023 virtual ~IPreference(); 00024 00025 virtual void LoadDefault() = 0; 00026 virtual void ReadFrom( const IniFile &ini ); 00027 virtual void WriteTo( IniFile &ini ) const; 00028 00029 virtual CString ToString() const = 0; 00030 virtual void FromString( const CString &s ) = 0; 00031 00032 PrefsGroup GetPrefsGroup() const { return m_PrefsGroup; } 00033 const CString &GetName() const { return m_sName; } 00034 00035 protected: 00036 PrefsGroup m_PrefsGroup; 00037 CString m_sName; 00038 }; 00039 00040 template <class T> 00041 class Preference : public IPreference 00042 { 00043 private: 00044 // Make currentValue first in the list so that we can pass this object 00045 // as an argument in a var_arg function as in printf. 00046 T m_currentValue; 00047 T m_defaultValue; 00048 00049 public: 00050 Preference( PrefsGroup PrefsGroup, const CString& sName, const T& defaultValue ): 00051 IPreference( PrefsGroup, sName ), 00052 m_currentValue( defaultValue ), 00053 m_defaultValue( defaultValue ) 00054 { 00055 LoadDefault(); 00056 } 00057 00058 CString ToString() const; 00059 void FromString( const CString &s ); 00060 00061 void LoadDefault() 00062 { 00063 m_currentValue = m_defaultValue; 00064 } 00065 00066 T &Value() 00067 { 00068 return m_currentValue; 00069 } 00070 00071 operator const T () const 00072 { 00073 return m_currentValue; 00074 } 00075 00076 void operator=( const T& other ) 00077 { 00078 m_currentValue = other; 00079 } 00080 }; 00081 00082 #endif 00083 00084 /* 00085 * (c) 2001-2004 Chris Danford, Chris Gomez 00086 * All rights reserved. 00087 * 00088 * Permission is hereby granted, free of charge, to any person obtaining a 00089 * copy of this software and associated documentation files (the 00090 * "Software"), to deal in the Software without restriction, including 00091 * without limitation the rights to use, copy, modify, merge, publish, 00092 * distribute, and/or sell copies of the Software, and to permit persons to 00093 * whom the Software is furnished to do so, provided that the above 00094 * copyright notice(s) and this permission notice appear in all copies of 00095 * the Software and that both the above copyright notice(s) and this 00096 * permission notice appear in supporting documentation. 00097 * 00098 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00099 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00100 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF 00101 * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS 00102 * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT 00103 * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS 00104 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR 00105 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 00106 * PERFORMANCE OF THIS SOFTWARE. 00107 */