-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathc_variable.cpp
153 lines (130 loc) · 3.33 KB
/
c_variable.cpp
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
#include "c_console.h"
namespace c {
Reference::Reference(const char *name, const char *description, void *handle, int type)
: m_name(name)
, m_description(description)
, m_handle(handle)
, m_type(type)
, m_next(Console::m_references)
{
Console::m_references = this;
}
template <typename T>
Variable<T>::Variable(int flags,
const char *name,
const char *description,
const T &min,
const T &max)
: m_reference({ name, description, (void *)this, Trait<T>::value })
, m_min(min)
, m_max(max)
, m_default()
, m_current()
, m_flags(flags)
{
}
template <typename T>
Variable<T>::Variable(int flags,
const char *name,
const char *description,
const T &min,
const T &max,
const T &def)
: m_reference({ name, description, (void *)this, Trait<T>::value })
, m_min(min)
, m_max(max)
, m_default(def)
, m_current(def)
, m_flags(flags)
{
}
template <typename T>
Variable<T>::operator T&() {
return m_current;
}
template <typename T>
T &Variable<T>::get() {
return m_current;
}
template <typename T>
const T &Variable<T>::get() const {
return m_current;
}
template <typename T>
const T Variable<T>::min() const {
return m_min;
}
template <typename T>
const T Variable<T>::max() const {
return m_max;
}
template <typename T>
int Variable<T>::set(const T &value) {
if (m_flags & kReadOnly)
return Console::kVarReadOnlyError;
if (value < m_min)
return Console::kVarRangeError;
if (value > m_max)
return Console::kVarRangeError;
m_current = value;
return Console::kVarSuccess;
}
template <typename T>
void Variable<T>::setMin(const T &min) {
m_min = min;
if (m_current < m_min) m_current = m_min;
}
template <typename T>
void Variable<T>::setMax(const T &max) {
m_max = max;
if (m_current > m_max) m_current = m_max;
}
template <typename T>
int Variable<T>::flags() const {
return m_flags;
}
template <typename T>
void Variable<T>::toggle() {
m_current = !m_current;
}
Variable<u::string>::Variable(int flags,
const char *name,
const char *description,
const char *def)
: m_reference({ name, description, (void *)this, kVarString })
, m_default(def)
, m_current(nullptr)
, m_flags(flags)
{
}
Variable<u::string>::Variable(int flags,
const char *name,
const char *description)
: m_reference({ name, description, (void *)this, kVarString })
, m_default(nullptr)
, m_current(nullptr)
, m_flags(flags)
{
}
Variable<u::string>::operator u::string&() {
return *m_current;
}
u::string &Variable<u::string>::get() {
return *m_current;
}
const u::string &Variable<u::string>::get() const {
return *m_current;
}
int Variable<u::string>::set(const u::string &value) {
if (m_flags & kReadOnly)
return Console::kVarReadOnlyError;
*m_current = value;
return Console::kVarSuccess;
}
int Variable<u::string>::flags() const {
return m_flags;
}
template struct Variable<int>;
template struct Variable<float>;
template struct Variable<u::string>;
}