-
Notifications
You must be signed in to change notification settings - Fork 27
/
param.h
334 lines (280 loc) · 7.06 KB
/
param.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
* Copyright 1993-2012 NVIDIA Corporation. All rights reserved.
*
* Please refer to the NVIDIA end user license agreement (EULA) associated
* with this source code for terms and conditions that govern your use of
* this software. Any use, reproduction, disclosure, or distribution of
* this software and related documentation outside the terms of the EULA
* is strictly prohibited.
*
*/
/*
Simple parameter system
[email protected] 4/2001
*/
#ifndef PARAM_H
#define PARAM_H
#include <string>
#include <vector>
#include <map>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string.h>
#ifndef WIN32
#include <stdlib.h>
//Default strdup under Linux gives deprecated warnings
//to keep it warning free, give own implementation
inline char* _strdup(const char* str)
{
char *p = (char*) malloc(strlen(str) + 1);
if(p) { strcpy(p, str); }
return p;
}
#endif
// base class for named parameter
class ParamBase
{
public:
ParamBase(const char *name) : m_name(name) { }
virtual ~ParamBase() { }
std::string &GetName()
{
return m_name;
}
virtual float GetFloatValue() = 0;
virtual int GetIntValue() = 0;
virtual std::string GetValueString() = 0;
virtual void Reset() = 0;
virtual void Increment() = 0;
virtual void Decrement() = 0;
virtual float GetPercentage() = 0;
virtual void SetPercentage(float p) = 0;
virtual void Write(std::ostream &stream) = 0;
virtual void Read(std::istream &stream) = 0;
virtual bool IsList() = 0;
protected:
std::string m_name;
};
// derived class for single-valued parameter
template<class T> class Param : public ParamBase
{
public:
Param(const char *name, T value = 0, T min = 0, T max = 10000, T step = 1, T *ptr = 0) :
ParamBase(name),
m_default(value),
m_min(min),
m_max(max),
m_step(step),
m_precision(3)
{
if (ptr)
{
m_ptr = ptr;
}
else
{
m_ptr = &m_value;
}
*m_ptr = value;
}
~Param() { }
T GetValue() const
{
return *m_ptr;
}
T SetValue(const T value)
{
*m_ptr = value;
}
float GetFloatValue()
{
return (float) *m_ptr;
}
int GetIntValue()
{
return (int) *m_ptr;
}
std::string GetValueString()
{
std::ostringstream ost;
ost<<std::setprecision(m_precision)<<std::fixed;
ost<<*m_ptr;
return ost.str();
}
void SetPrecision(int x)
{
m_precision = x;
}
float GetPercentage()
{
return (*m_ptr - m_min) / (float)(m_max - m_min);
}
void SetPercentage(float p)
{
*m_ptr = (T)(m_min + p * (m_max - m_min));
}
void Reset()
{
*m_ptr = m_default;
}
void Increment()
{
*m_ptr += m_step;
if (*m_ptr > m_max)
{
*m_ptr = m_max;
}
}
void Decrement()
{
*m_ptr -= m_step;
if (*m_ptr < m_min)
{
*m_ptr = m_min;
}
}
void Write(std::ostream &stream)
{
char *str = _strdup(m_name.c_str());
// replace spaces with _
char *ch;
while(ch = strchr(str, ' ')) {
*ch = '_';
}
//stream << m_name << " " << *m_ptr << '\n';
stream << str << " " << *m_ptr << '\n';
free(str);
}
void Read(std::istream &stream)
{
std::string name;
stream >> name >> *m_ptr;
}
bool IsList()
{
return false;
}
private:
T m_value;
T *m_ptr; // pointer to value declared elsewhere
T m_default, m_min, m_max, m_step;
int m_precision; // number of digits after decimal point in string output
};
extern const Param<int> dummy;
// list of parameters
class ParamList : public ParamBase
{
public:
ParamList(const char *name = "") :
ParamBase(name)
{
active = true;
}
~ParamList() { }
float GetFloatValue()
{
return 0.0f;
}
int GetIntValue()
{
return 0;
}
void AddParam(ParamBase *param)
{
m_params.push_back(param);
m_map[param->GetName()] = param;
m_current = m_params.begin();
}
// look-up parameter based on name
ParamBase *GetParam(char *name)
{
ParamBase *p = m_map[name];
if (p)
{
return p;
}
else
{
return (ParamBase *) &dummy;
}
}
ParamBase *GetParam(int i)
{
return m_params[i];
}
ParamBase *GetCurrent()
{
return *m_current;
}
int GetSize()
{
return (int)m_params.size();
}
std::string GetValueString()
{
return m_name;
}
// functions to traverse list
void Reset()
{
m_current = m_params.begin();
}
void Increment()
{
m_current++;
if (m_current == m_params.end())
{
m_current = m_params.begin();
}
}
void Decrement()
{
if (m_current == m_params.begin())
{
m_current = m_params.end()-1;
}
else
{
m_current--;
}
}
float GetPercentage()
{
return 0.0f;
}
void SetPercentage(float /*p*/) {}
void Write(std::ostream &stream)
{
stream << m_name << '\n';
for (std::vector<ParamBase *>::const_iterator p = m_params.begin(); p != m_params.end(); ++p)
{
(*p)->Write(stream);
}
}
void Read(std::istream &stream)
{
stream >> m_name;
for (std::vector<ParamBase *>::const_iterator p = m_params.begin(); p != m_params.end(); ++p)
{
(*p)->Read(stream);
}
}
bool IsList()
{
return true;
}
void ResetAll()
{
for (std::vector<ParamBase *>::const_iterator p = m_params.begin(); p != m_params.end(); ++p)
{
(*p)->Reset();
}
}
protected:
bool active;
std::vector<ParamBase *> m_params;
std::map<std::string, ParamBase *> m_map;
std::vector<ParamBase *>::const_iterator m_current;
};
#endif