forked from stepmania/stepmania
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreference.h
196 lines (171 loc) · 5.1 KB
/
Preference.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* Preference - Holds user-chosen preferences that are saved between sessions. */
#ifndef PREFERENCE_H
#define PREFERENCE_H
#include "EnumHelper.h"
#include "LuaManager.h"
#include "RageUtil.h"
#include "RageUtil.hpp"
class XNode;
struct lua_State;
class IPreference
{
public:
IPreference( std::string const & sName );
virtual ~IPreference();
void ReadFrom( const XNode* pNode, bool bIsStatic );
void WriteTo( XNode* pNode ) const;
void ReadDefaultFrom( const XNode* pNode );
virtual void LoadDefault() = 0;
virtual void SetDefaultFromString( std::string const &s ) = 0;
virtual std::string ToString() const = 0;
virtual void FromString( std::string const &s ) = 0;
virtual void SetFromStack( lua_State *L );
virtual void PushValue( lua_State *L ) const;
std::string const &GetName() const { return m_sName; }
static IPreference *GetPreferenceByName( std::string const &sName );
static void LoadAllDefaults();
static void ReadAllPrefsFromNode( const XNode* pNode, bool bIsStatic );
static void SavePrefsToNode( XNode* pNode );
static void ReadAllDefaultsFromNode( const XNode* pNode );
std::string GetName() { return m_sName; }
void SetStatic( bool b ) { m_bIsStatic = b; }
private:
std::string m_sName;
bool m_bIsStatic; // loaded from Static.ini? If so, don't write to Preferences.ini
};
void BroadcastPreferenceChanged( const std::string& sPreferenceName );
template <class T>
class Preference : public IPreference
{
public:
Preference( std::string const & sName, const T& defaultValue, void (pfnValidate)(T& val) = nullptr ):
IPreference( sName ),
m_currentValue( defaultValue ),
m_defaultValue( defaultValue ),
m_pfnValidate( pfnValidate )
{
LoadDefault();
}
std::string ToString() const { return StringConversion::ToString<T>( m_currentValue ); }
void FromString( std::string const &s )
{
if( !StringConversion::FromString<T>(s, m_currentValue) )
{
m_currentValue = m_defaultValue;
}
if( m_pfnValidate )
{
m_pfnValidate( m_currentValue );
}
}
void SetFromStack( lua_State *L )
{
LuaHelpers::Pop<T>( L, m_currentValue );
if( m_pfnValidate )
{
m_pfnValidate( m_currentValue );
}
}
void PushValue( lua_State *L ) const
{
LuaHelpers::Push<T>( L, m_currentValue );
}
void LoadDefault()
{
m_currentValue = m_defaultValue;
}
void SetDefaultFromString( std::string const &s )
{
T def = m_defaultValue;
if( !StringConversion::FromString<T>(s, m_defaultValue) )
{
m_defaultValue = def;
}
}
const T &Get() const
{
return m_currentValue;
}
const T &GetDefault() const
{
return m_defaultValue;
}
operator const T () const
{
return Get();
}
void Set( const T& other )
{
m_currentValue = other;
BroadcastPreferenceChanged( GetName() );
}
static Preference<T> *GetPreferenceByName( std::string const &sName )
{
IPreference *pPreference = IPreference::GetPreferenceByName( sName );
Preference<T> *pRet = dynamic_cast<Preference<T> *>(pPreference);
return pRet;
}
private:
T m_currentValue;
T m_defaultValue;
void (*m_pfnValidate)(T& val);
};
/** @brief Utilities for working with Lua. */
namespace LuaHelpers { template<typename T> void Push( lua_State *L, const Preference<T> &Object ) { LuaHelpers::Push<T>( L, Object.Get() ); } }
template <class T>
class Preference1D
{
public:
typedef Preference<T> PreferenceT;
std::vector<PreferenceT*> m_v;
Preference1D( void pfn(size_t i, std::string &sNameOut, T &defaultValueOut ), size_t N )
{
for( size_t i=0; i<N; ++i )
{
std::string sName;
T defaultValue;
pfn( i, sName, defaultValue );
m_v.push_back( new Preference<T>(sName, defaultValue) );
}
}
~Preference1D()
{
for (auto *item: m_v)
{
Rage::safe_delete(item);
}
}
const Preference<T>& operator[]( size_t i ) const
{
return *m_v[i];
}
Preference<T>& operator[]( size_t i )
{
return *m_v[i];
}
};
#endif
/*
* (c) 2001-2004 Chris Danford, Chris Gomez
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/