-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcvar.hpp
executable file
·170 lines (145 loc) · 3.74 KB
/
cvar.hpp
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
#ifndef CVAR_HPP
#define CVAR_HPP
#include "msvcfix.hpp"
#include <string>
#include <map>
#include <sstream>
#include <iterator>
class BaseConsoleVar;
extern bool loadingMap;
class VarPool
{
protected:
typedef std::map<std::string, BaseConsoleVar*> varmap;
typedef varmap::iterator varmap_iterator;
varmap vars;
class VarIterator : public varmap::iterator
{
public:
VarIterator(varmap::iterator i)
: varmap_iterator(i) { }
BaseConsoleVar *operator*(void) {
return (this->varmap_iterator::operator*()).second;
}
};
public:
typedef VarIterator iterator;
void registerVar(BaseConsoleVar *var);
BaseConsoleVar *lookup(const char *name);
void restoreVars(void);
iterator begin(void);
iterator end(void);
};
extern VarPool *clientCvars, *serverCvars;
class BaseConsoleVar
{
public:
BaseConsoleVar(std::string name, VarPool *&pool)
{
this->name = name;
if(pool==NULL) pool = new VarPool;
pool->registerVar(this);
}
virtual ~BaseConsoleVar() { }
const char *getName(void) {
return name.c_str();
}
virtual std::string defaultValueString(void) = 0;
virtual std::string currentValueString(void) = 0;
virtual std::string valueString(void) = 0;
virtual void valueFromString(const char *str) = 0;
virtual void restoreValue(void) = 0;
protected:
std::string name;
};
template<class T>
class ConsoleVar : BaseConsoleVar
{
public:
ConsoleVar(const char *name, T default_value, VarPool *&pool)
: BaseConsoleVar(name, pool)
{
this->default_value = default_value;
this->change_handler = NULL;
value = previous_value = default_value;
}
ConsoleVar(const char *name, T default_value, VarPool *&pool, void (*change_handler)(T new_value))
: BaseConsoleVar(name, pool)
{
this->default_value = default_value;
this->change_handler = change_handler;
value = previous_value = default_value;
}
void setValue(const T& rhs) {
bool isChange;
isChange = (rhs != value);
value = rhs;
if(!loadingMap) previous_value = rhs;
if(change_handler != NULL && isChange)
change_handler(rhs);
}
void restoreValue(void) {
setValue(previous_value);
}
T& getValue(void) {
return value;
}
const T& getDefault(void) {
return default_value;
}
std::string defaultValueString(void) {
std::stringstream str;
str << default_value;
return str.str();
}
std::string currentValueString(void) {
std::stringstream str;
str << value;
return str.str();
}
std::string valueString(void) {
return name+" is "+currentValueString()+
" (default "+defaultValueString()+")";
}
void valueFromString(const char *c_str);
protected:
void (*change_handler)(T new_value);
T default_value;
T value;
T previous_value;
};
template<class T>
class ClientConsoleVar
: public ConsoleVar<T>
{
public:
ClientConsoleVar(const char *name, T default_value)
: ConsoleVar<T>(name, default_value, clientCvars) { }
ClientConsoleVar(const char *name, T default_value, void (*change_handler)(T new_value))
: ConsoleVar<T>(name, default_value, clientCvars, change_handler) { }
inline operator T(void) {
return this->value;
}
inline const T& operator= (const T& rhs) {
setValue(rhs);
return rhs;
}
};
template<class T>
class ServerConsoleVar
: public ConsoleVar<T>
{
public:
ServerConsoleVar(const char *name, T default_value)
: ConsoleVar<T>(name, default_value, serverCvars) { }
ServerConsoleVar(const char *name, T default_value, void (*change_handler)(T new_value))
: ConsoleVar<T>(name, default_value, serverCvars, change_handler) { }
inline operator T(void) {
return this->value;
}
inline const T& operator= (const T& rhs) {
setValue(rhs);
return rhs;
}
};
#endif