1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005, 2006 Christian Schoenebeck * |
7 |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
9 |
* it under the terms of the GNU General Public License as published by * |
10 |
* the Free Software Foundation; either version 2 of the License, or * |
11 |
* (at your option) any later version. * |
12 |
* * |
13 |
* This program is distributed in the hope that it will be useful, * |
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
16 |
* GNU General Public License for more details. * |
17 |
* * |
18 |
* You should have received a copy of the GNU General Public License * |
19 |
* along with this program; if not, write to the Free Software * |
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
21 |
* MA 02111-1307 USA * |
22 |
***************************************************************************/ |
23 |
|
24 |
#include <strings.h> |
25 |
#include <stdlib.h> |
26 |
|
27 |
#include "DeviceParameter.h" |
28 |
|
29 |
namespace LinuxSampler { |
30 |
|
31 |
// if string is encapsulated into apostrophes or quotation marks, then remove those apostrophes / quotation marks |
32 |
static void __eliminate_quotation(String& s) { |
33 |
if (s.size()) { |
34 |
const char cBegin = s[0]; |
35 |
const char cEnd = s[s.size() - 1]; |
36 |
if ( (cBegin == '\'' && cEnd == '\'') || (cBegin == '\"' && cEnd == '\"') ) { |
37 |
s = s.substr(1, s.size() - 2); |
38 |
} |
39 |
} |
40 |
} |
41 |
|
42 |
static bool __parse_bool(String val) throw (Exception) { |
43 |
__eliminate_quotation(val); |
44 |
int b; |
45 |
if (val == "1" || !strcasecmp(val.c_str(),"true")) b = true; |
46 |
else if (val == "0" || !strcasecmp(val.c_str(),"false")) b = false; |
47 |
else throw Exception("Invalid value for boolean Device parameter"); |
48 |
return b; |
49 |
} |
50 |
|
51 |
static int __parse_int(String val) throw (Exception) { |
52 |
__eliminate_quotation(val); |
53 |
return atoi(val.c_str()); // TODO: format check is missing |
54 |
} |
55 |
|
56 |
static float __parse_float(String val) throw (Exception) { |
57 |
__eliminate_quotation(val); |
58 |
return atof(val.c_str()); // TODO: format check is missing |
59 |
} |
60 |
|
61 |
static String __parse_string(String val) { |
62 |
__eliminate_quotation(val); |
63 |
return val; |
64 |
} |
65 |
|
66 |
static std::vector<String> __parse_strings(String val) throw (Exception) { |
67 |
std::vector<String> vS; |
68 |
|
69 |
// checking for empty list |
70 |
if (val.length() == 0) return vS; |
71 |
|
72 |
// if there's only a single value, then we also allow to give it without being encapsulated into apostrophes |
73 |
if (val.find("\'") == String::npos && val.find("\"") == String::npos) { |
74 |
vS.push_back(val); |
75 |
} |
76 |
else { // if multiple strings or a string encapsulated into apostrophes |
77 |
char* pStart = (char*) val.c_str(); |
78 |
char* pC = pStart; |
79 |
|
80 |
while (true) { |
81 |
if (*pC != '\'' && *pC != '\"') throw Exception("Invalid form, all individual strings should be encapsulated into apostrophes, separated by commas"); |
82 |
|
83 |
// search for token end |
84 |
char* pTokenStart = pC + 1; |
85 |
do { |
86 |
pC++; |
87 |
if (*pC == '\0') throw Exception("Invalid form, all individual strings should be encapsulated into apostrophes, separated by commas"); |
88 |
} |
89 |
while (*pC != '\'' && *pC != '\"'); |
90 |
String token = val.substr((int)(pTokenStart - pStart), (int)(pC - pTokenStart)); |
91 |
vS.push_back(token); // we found the token's end |
92 |
|
93 |
// now there should be either a comma or the end of the total string |
94 |
if (*(++pC) == '\0') break; |
95 |
if (*pC != ',') throw Exception("Invalid form, all individual strings should be encapsulated into apostrophes, separated by commas"); |
96 |
pC++; |
97 |
} |
98 |
} |
99 |
|
100 |
return vS; |
101 |
} |
102 |
|
103 |
|
104 |
|
105 |
// *************** DeviceRuntimeParameterBool *************** |
106 |
// * |
107 |
|
108 |
DeviceRuntimeParameterBool::DeviceRuntimeParameterBool(bool bVal) { |
109 |
this->bVal = bVal; |
110 |
} |
111 |
|
112 |
String DeviceRuntimeParameterBool::Type() { |
113 |
return "BOOL"; |
114 |
} |
115 |
|
116 |
bool DeviceRuntimeParameterBool::Multiplicity() { |
117 |
return false; |
118 |
} |
119 |
|
120 |
optional<String> DeviceRuntimeParameterBool::RangeMin() { |
121 |
return optional<String>::nothing; |
122 |
} |
123 |
|
124 |
optional<String> DeviceRuntimeParameterBool::RangeMax() { |
125 |
return optional<String>::nothing; |
126 |
} |
127 |
|
128 |
optional<String> DeviceRuntimeParameterBool::Possibilities() { |
129 |
return optional<String>::nothing; |
130 |
} |
131 |
|
132 |
String DeviceRuntimeParameterBool::Value() { |
133 |
return (ValueAsBool()) ? "true" : "false"; |
134 |
} |
135 |
|
136 |
void DeviceRuntimeParameterBool::SetValue(String val) throw (Exception) { |
137 |
if (Fix()) throw Exception("Device parameter is read only"); |
138 |
int b = __parse_bool(val); |
139 |
SetValue(b); |
140 |
} |
141 |
|
142 |
bool DeviceRuntimeParameterBool::ValueAsBool() { |
143 |
return bVal; |
144 |
} |
145 |
|
146 |
void DeviceRuntimeParameterBool::SetValue(bool b) throw (Exception) { |
147 |
if (Fix()) throw Exception("Device parameter is read only"); |
148 |
OnSetValue(b); |
149 |
bVal = b; |
150 |
} |
151 |
|
152 |
|
153 |
|
154 |
// *************** DeviceRuntimeParameterInt *************** |
155 |
// * |
156 |
|
157 |
DeviceRuntimeParameterInt::DeviceRuntimeParameterInt(int iVal) { |
158 |
this->iVal = iVal; |
159 |
} |
160 |
|
161 |
String DeviceRuntimeParameterInt::Type() { |
162 |
return "INT"; |
163 |
} |
164 |
|
165 |
bool DeviceRuntimeParameterInt::Multiplicity() { |
166 |
return false; |
167 |
} |
168 |
|
169 |
optional<String> DeviceRuntimeParameterInt::RangeMin() { |
170 |
optional<int> rangemin = RangeMinAsInt(); |
171 |
if (!rangemin) return optional<String>::nothing; |
172 |
return ToString(*rangemin); |
173 |
} |
174 |
|
175 |
optional<String> DeviceRuntimeParameterInt::RangeMax() { |
176 |
optional<int> rangemax = RangeMaxAsInt(); |
177 |
if (!rangemax) return optional<String>::nothing; |
178 |
return ToString(*rangemax); |
179 |
} |
180 |
|
181 |
optional<String> DeviceRuntimeParameterInt::Possibilities() { |
182 |
std::vector<int> possibilities = PossibilitiesAsInt(); |
183 |
if (possibilities.empty()) return optional<String>::nothing; |
184 |
|
185 |
std::stringstream ss; |
186 |
std::vector<int>::iterator iter = possibilities.begin(); |
187 |
while (iter != possibilities.end()) { |
188 |
if (ss.str() != "") ss << ","; |
189 |
ss << *iter; |
190 |
iter++; |
191 |
} |
192 |
return ss.str(); |
193 |
} |
194 |
|
195 |
String DeviceRuntimeParameterInt::Value() { |
196 |
return ToString(ValueAsInt()); |
197 |
} |
198 |
|
199 |
void DeviceRuntimeParameterInt::SetValue(String val) throw (Exception) { |
200 |
if (Fix()) throw Exception("Device parameter is read only"); |
201 |
int i = __parse_int(val); |
202 |
if (RangeMinAsInt() && i < *RangeMinAsInt()) throw Exception("Invalid device parameter value: too small"); |
203 |
if (RangeMaxAsInt() && i > *RangeMaxAsInt()) throw Exception("Invalid device parameter value: too big"); |
204 |
|
205 |
std::vector<int> possibilities = PossibilitiesAsInt(); |
206 |
if (possibilities.size()) { |
207 |
bool valid = false; |
208 |
std::vector<int>::iterator iter = possibilities.begin(); |
209 |
while (iter != possibilities.end()) { |
210 |
if (i == *iter) { |
211 |
valid = true; |
212 |
break; |
213 |
} |
214 |
iter++; |
215 |
} |
216 |
if (!valid) throw Exception("Invalid device parameter value: not in set of possible values"); |
217 |
} |
218 |
SetValue(i); |
219 |
} |
220 |
|
221 |
int DeviceRuntimeParameterInt::ValueAsInt() { |
222 |
return iVal; |
223 |
} |
224 |
|
225 |
void DeviceRuntimeParameterInt::SetValue(int i) throw (Exception) { |
226 |
if (Fix()) throw Exception("Device parameter is read only"); |
227 |
OnSetValue(i); |
228 |
iVal = i; |
229 |
} |
230 |
|
231 |
|
232 |
|
233 |
// *************** DeviceRuntimeParameterFloat *************** |
234 |
// * |
235 |
|
236 |
DeviceRuntimeParameterFloat::DeviceRuntimeParameterFloat(float fVal) { |
237 |
this->fVal = fVal; |
238 |
} |
239 |
|
240 |
String DeviceRuntimeParameterFloat::Type() { |
241 |
return "FLOAT"; |
242 |
} |
243 |
|
244 |
bool DeviceRuntimeParameterFloat::Multiplicity() { |
245 |
return false; |
246 |
} |
247 |
|
248 |
optional<String> DeviceRuntimeParameterFloat::RangeMin() { |
249 |
optional<float> rangemin = RangeMinAsFloat(); |
250 |
if (!rangemin) return optional<String>::nothing; |
251 |
return ToString(*rangemin); |
252 |
} |
253 |
|
254 |
optional<String> DeviceRuntimeParameterFloat::RangeMax() { |
255 |
optional<float> rangemax = RangeMaxAsFloat(); |
256 |
if (!rangemax) return optional<String>::nothing; |
257 |
return ToString(*rangemax); |
258 |
} |
259 |
|
260 |
optional<String> DeviceRuntimeParameterFloat::Possibilities() { |
261 |
std::vector<float> possibilities = PossibilitiesAsFloat(); |
262 |
if (possibilities.empty()) return optional<String>::nothing; |
263 |
|
264 |
std::stringstream ss; |
265 |
std::vector<float>::iterator iter = possibilities.begin(); |
266 |
while (iter != possibilities.end()) { |
267 |
if (ss.str() != "") ss << ","; |
268 |
ss << *iter; |
269 |
iter++; |
270 |
} |
271 |
return ss.str(); |
272 |
} |
273 |
|
274 |
String DeviceRuntimeParameterFloat::Value() { |
275 |
return ToString(ValueAsFloat()); |
276 |
} |
277 |
|
278 |
void DeviceRuntimeParameterFloat::SetValue(String val) throw (Exception) { |
279 |
if (Fix()) throw Exception("Device parameter is read only"); |
280 |
float f = __parse_float(val); |
281 |
if (RangeMinAsFloat() && f < *RangeMinAsFloat()) throw Exception("Invalid device parameter value: too small"); |
282 |
if (RangeMaxAsFloat() && f > *RangeMaxAsFloat()) throw Exception("Invalid device parameter value: too big"); |
283 |
|
284 |
std::vector<float> possibilities = PossibilitiesAsFloat(); |
285 |
if (possibilities.size()) { |
286 |
bool valid = false; |
287 |
std::vector<float>::iterator iter = possibilities.begin(); |
288 |
while (iter != possibilities.end()) { |
289 |
if (f == *iter) { |
290 |
valid = true; |
291 |
break; |
292 |
} |
293 |
iter++; |
294 |
} |
295 |
if (!valid) throw Exception("Invalid device parameter value: not in set of possible values"); |
296 |
} |
297 |
SetValue(f); |
298 |
} |
299 |
|
300 |
float DeviceRuntimeParameterFloat::ValueAsFloat() { |
301 |
return fVal; |
302 |
} |
303 |
|
304 |
void DeviceRuntimeParameterFloat::SetValue(float f) throw (Exception) { |
305 |
if (Fix()) throw Exception("Device parameter is read only"); |
306 |
OnSetValue(f); |
307 |
fVal = f; |
308 |
} |
309 |
|
310 |
|
311 |
|
312 |
// *************** DeviceRuntimeParameterString *************** |
313 |
// * |
314 |
|
315 |
DeviceRuntimeParameterString::DeviceRuntimeParameterString(String sVal) { |
316 |
this->sVal = sVal; |
317 |
} |
318 |
|
319 |
String DeviceRuntimeParameterString::Type() { |
320 |
return "STRING"; |
321 |
} |
322 |
|
323 |
bool DeviceRuntimeParameterString::Multiplicity() { |
324 |
return false; |
325 |
} |
326 |
|
327 |
optional<String> DeviceRuntimeParameterString::RangeMin() { |
328 |
return optional<String>::nothing; |
329 |
} |
330 |
|
331 |
optional<String> DeviceRuntimeParameterString::RangeMax() { |
332 |
return optional<String>::nothing; |
333 |
} |
334 |
|
335 |
optional<String> DeviceRuntimeParameterString::Possibilities() { |
336 |
std::vector<String> possibilities = PossibilitiesAsString(); |
337 |
if (possibilities.empty()) return optional<String>::nothing; |
338 |
|
339 |
std::stringstream ss; |
340 |
std::vector<String>::iterator iter = possibilities.begin(); |
341 |
while (iter != possibilities.end()) { |
342 |
if (ss.str() != "") ss << ","; |
343 |
ss << "'" << *iter << "'"; |
344 |
iter++; |
345 |
} |
346 |
return ss.str(); |
347 |
} |
348 |
|
349 |
String DeviceRuntimeParameterString::Value() { |
350 |
return "\'" + ValueAsString() + "\'"; |
351 |
} |
352 |
|
353 |
void DeviceRuntimeParameterString::SetValue(String val) throw (Exception) { |
354 |
if (Fix()) throw Exception("Device parameter is read only"); |
355 |
SetValueAsString(__parse_string(val)); |
356 |
} |
357 |
|
358 |
String DeviceRuntimeParameterString::ValueAsString() { |
359 |
return sVal; |
360 |
} |
361 |
|
362 |
void DeviceRuntimeParameterString::SetValueAsString(String val) throw (Exception) { |
363 |
if (Fix()) throw Exception("Device parameter is read only"); |
364 |
if (val.find("\'") != String::npos) throw Exception("Character -> \' <- not allowed"); |
365 |
if (val.find("\"") != String::npos) throw Exception("Character -> \" <- not allowed"); |
366 |
OnSetValue(val); |
367 |
sVal = val; |
368 |
} |
369 |
|
370 |
|
371 |
|
372 |
// *************** DeviceRuntimeParameterStrings *************** |
373 |
// * |
374 |
|
375 |
DeviceRuntimeParameterStrings::DeviceRuntimeParameterStrings(std::vector<String> vS) { |
376 |
this->sVals = vS; |
377 |
} |
378 |
|
379 |
String DeviceRuntimeParameterStrings::Type() { |
380 |
return "STRING"; |
381 |
} |
382 |
|
383 |
bool DeviceRuntimeParameterStrings::Multiplicity() { |
384 |
return true; |
385 |
} |
386 |
|
387 |
optional<String> DeviceRuntimeParameterStrings::RangeMin() { |
388 |
return optional<String>::nothing; |
389 |
} |
390 |
|
391 |
optional<String> DeviceRuntimeParameterStrings::RangeMax() { |
392 |
return optional<String>::nothing; |
393 |
} |
394 |
|
395 |
optional<String> DeviceRuntimeParameterStrings::Possibilities() { |
396 |
std::vector<String> possibilities = PossibilitiesAsString(); |
397 |
if (possibilities.empty()) return optional<String>::nothing; |
398 |
|
399 |
std::stringstream ss; |
400 |
std::vector<String>::iterator iter = possibilities.begin(); |
401 |
while (iter != possibilities.end()) { |
402 |
if (ss.str() != "") ss << ","; |
403 |
ss << "'" << *iter << "'"; |
404 |
iter++; |
405 |
} |
406 |
return ss.str(); |
407 |
} |
408 |
|
409 |
String DeviceRuntimeParameterStrings::Value() { |
410 |
String result; |
411 |
std::vector<String>::iterator iter = this->sVals.begin(); |
412 |
while (iter != this->sVals.end()) { |
413 |
if (result != "") result += ","; |
414 |
result += "'" + *iter + "'"; |
415 |
iter++; |
416 |
} |
417 |
return result; |
418 |
} |
419 |
|
420 |
void DeviceRuntimeParameterStrings::SetValue(String val) throw (Exception) { |
421 |
if (Fix()) throw Exception("Device parameter is read only"); |
422 |
std::vector<String> vS = __parse_strings(val); |
423 |
SetValue(vS); |
424 |
} |
425 |
|
426 |
std::vector<String> DeviceRuntimeParameterStrings::ValueAsStrings() { |
427 |
return sVals; |
428 |
} |
429 |
|
430 |
void DeviceRuntimeParameterStrings::SetValue(std::vector<String> vS) throw (Exception) { |
431 |
if (Fix()) throw Exception("Device parameter is read only"); |
432 |
OnSetValue(vS); |
433 |
sVals = vS; |
434 |
} |
435 |
|
436 |
|
437 |
|
438 |
|
439 |
|
440 |
|
441 |
// *************** DeviceCreationParameter *************** |
442 |
// * |
443 |
|
444 |
optional<String> DeviceCreationParameter::Depends() { |
445 |
std::map<String,DeviceCreationParameter*> dependencies = DependsAsParameters(); |
446 |
if (!dependencies.size()) return optional<String>::nothing; |
447 |
|
448 |
std::map<String,DeviceCreationParameter*>::iterator iter = dependencies.begin(); |
449 |
String s; |
450 |
for (; iter != dependencies.end(); iter++) { |
451 |
if (s != "") s += ","; |
452 |
s += iter->first; |
453 |
} |
454 |
return s; |
455 |
} |
456 |
|
457 |
optional<String> DeviceCreationParameter::Default() { |
458 |
std::map<String,String> Parameters; // empty parameters vector |
459 |
return Default(Parameters); |
460 |
} |
461 |
|
462 |
optional<String> DeviceCreationParameter::RangeMin() { |
463 |
std::map<String,String> Parameters; // empty parameters vector |
464 |
return RangeMin(Parameters); |
465 |
} |
466 |
|
467 |
optional<String> DeviceCreationParameter::RangeMax() { |
468 |
std::map<String,String> Parameters; // empty parameters vector |
469 |
return RangeMax(Parameters); |
470 |
} |
471 |
|
472 |
optional<String> DeviceCreationParameter::Possibilities() { |
473 |
std::map<String,String> Parameters; // empty parameters vector |
474 |
return Possibilities(Parameters); |
475 |
} |
476 |
|
477 |
|
478 |
|
479 |
// *************** DeviceCreationParameterBool *************** |
480 |
// * |
481 |
|
482 |
DeviceCreationParameterBool::DeviceCreationParameterBool(bool bVal) : DeviceCreationParameter() { |
483 |
this->bVal = bVal; |
484 |
} |
485 |
|
486 |
DeviceCreationParameterBool::DeviceCreationParameterBool(String val) throw (Exception) { |
487 |
this->bVal = __parse_bool(val); |
488 |
} |
489 |
|
490 |
void DeviceCreationParameterBool::InitWithDefault() { |
491 |
std::map<String,String> Parameters; // empty parameters vector |
492 |
optional<bool> defaultval = DefaultAsBool(Parameters); |
493 |
this->bVal = (defaultval) ? *defaultval : false; |
494 |
} |
495 |
|
496 |
String DeviceCreationParameterBool::Type() { |
497 |
return "BOOL"; |
498 |
} |
499 |
|
500 |
bool DeviceCreationParameterBool::Multiplicity() { |
501 |
return false; |
502 |
} |
503 |
|
504 |
optional<String> DeviceCreationParameterBool::Default(std::map<String,String> Parameters) { |
505 |
optional<bool> defaultval = DefaultAsBool(Parameters); |
506 |
if (!defaultval) return optional<String>::nothing; |
507 |
return (*defaultval) ? "true" : "false"; |
508 |
} |
509 |
|
510 |
optional<String> DeviceCreationParameterBool::RangeMin(std::map<String,String> Parameters) { |
511 |
return optional<String>::nothing; |
512 |
} |
513 |
|
514 |
optional<String> DeviceCreationParameterBool::RangeMax(std::map<String,String> Parameters) { |
515 |
return optional<String>::nothing; |
516 |
} |
517 |
|
518 |
optional<String> DeviceCreationParameterBool::Possibilities(std::map<String,String> Parameters) { |
519 |
return optional<String>::nothing; |
520 |
} |
521 |
|
522 |
String DeviceCreationParameterBool::Value() { |
523 |
return (ValueAsBool()) ? "true" : "false"; |
524 |
} |
525 |
|
526 |
void DeviceCreationParameterBool::SetValue(String val) throw (Exception) { |
527 |
if (Fix()) throw Exception("Device parameter is read only"); |
528 |
int b = __parse_bool(val); |
529 |
SetValue(b); |
530 |
} |
531 |
|
532 |
bool DeviceCreationParameterBool::ValueAsBool() { |
533 |
return bVal; |
534 |
} |
535 |
|
536 |
void DeviceCreationParameterBool::SetValue(bool b) throw (Exception) { |
537 |
if (Fix()) throw Exception("Device parameter is read only"); |
538 |
OnSetValue(b); |
539 |
bVal = b; |
540 |
} |
541 |
|
542 |
|
543 |
|
544 |
// *************** DeviceCreationParameterInt *************** |
545 |
// * |
546 |
|
547 |
DeviceCreationParameterInt::DeviceCreationParameterInt(int iVal) : DeviceCreationParameter() { |
548 |
this->iVal = iVal; |
549 |
} |
550 |
|
551 |
DeviceCreationParameterInt::DeviceCreationParameterInt(String val) throw (Exception) { |
552 |
this->iVal = __parse_int(val); |
553 |
} |
554 |
|
555 |
void DeviceCreationParameterInt::InitWithDefault() { |
556 |
std::map<String,String> Parameters; // empty parameters vector |
557 |
optional<int> i = DefaultAsInt(Parameters); |
558 |
this->iVal = (i) ? *i : 0; |
559 |
} |
560 |
|
561 |
String DeviceCreationParameterInt::Type() { |
562 |
return "INT"; |
563 |
} |
564 |
|
565 |
bool DeviceCreationParameterInt::Multiplicity() { |
566 |
return false; |
567 |
} |
568 |
|
569 |
optional<String> DeviceCreationParameterInt::Default(std::map<String,String> Parameters) { |
570 |
optional<int> defaultval = DefaultAsInt(Parameters); |
571 |
if (!defaultval) return optional<String>::nothing; |
572 |
return ToString(*defaultval); |
573 |
} |
574 |
|
575 |
optional<String> DeviceCreationParameterInt::RangeMin(std::map<String,String> Parameters) { |
576 |
optional<int> rangemin = RangeMinAsInt(Parameters); |
577 |
if (!rangemin) return optional<String>::nothing; |
578 |
return ToString(*rangemin); |
579 |
} |
580 |
|
581 |
optional<String> DeviceCreationParameterInt::RangeMax(std::map<String,String> Parameters) { |
582 |
optional<int> rangemax = RangeMaxAsInt(Parameters); |
583 |
if (!rangemax) return optional<String>::nothing; |
584 |
return ToString(*rangemax); |
585 |
} |
586 |
|
587 |
optional<String> DeviceCreationParameterInt::Possibilities(std::map<String,String> Parameters) { |
588 |
std::vector<int> possibilities = PossibilitiesAsInt(Parameters); |
589 |
if (possibilities.empty()) return optional<String>::nothing; |
590 |
|
591 |
std::vector<int>::iterator iter = possibilities.begin(); |
592 |
std::stringstream ss; |
593 |
while (iter != possibilities.end()) { |
594 |
if (ss.str() != "") ss << ","; |
595 |
ss << *iter; |
596 |
iter++; |
597 |
} |
598 |
return ss.str(); |
599 |
} |
600 |
|
601 |
String DeviceCreationParameterInt::Value() { |
602 |
return ToString(ValueAsInt()); |
603 |
} |
604 |
|
605 |
void DeviceCreationParameterInt::SetValue(String val) throw (Exception) { |
606 |
if (Fix()) throw Exception("Device parameter is read only"); |
607 |
int i = __parse_int(val); |
608 |
|
609 |
std::map<String,String> emptyMap; |
610 |
if (RangeMinAsInt(emptyMap) && i < *RangeMinAsInt(emptyMap)) { |
611 |
throw Exception("Invalid device parameter value: too small"); |
612 |
} |
613 |
if (RangeMaxAsInt(emptyMap) && i > *RangeMaxAsInt(emptyMap)) { |
614 |
throw Exception("Invalid device parameter value: too big"); |
615 |
} |
616 |
|
617 |
if (PossibilitiesAsInt(emptyMap).size()) { |
618 |
bool valid = false; |
619 |
std::vector<int> possibilities = PossibilitiesAsInt(emptyMap); |
620 |
std::vector<int>::iterator iter = possibilities.begin(); |
621 |
while (iter != possibilities.end()) { |
622 |
if (i == *iter) { |
623 |
valid = true; |
624 |
break; |
625 |
} |
626 |
iter++; |
627 |
} |
628 |
if (!valid) throw Exception("Invalid Device parameter value: not in set of possible values"); |
629 |
} |
630 |
SetValue(i); |
631 |
} |
632 |
|
633 |
int DeviceCreationParameterInt::ValueAsInt() { |
634 |
return iVal; |
635 |
} |
636 |
|
637 |
void DeviceCreationParameterInt::SetValue(int i) throw (Exception) { |
638 |
if (Fix()) throw Exception("Device parameter is read only"); |
639 |
OnSetValue(i); |
640 |
iVal = i; |
641 |
} |
642 |
|
643 |
|
644 |
|
645 |
// *************** DeviceCreationParameterFloat *************** |
646 |
// * |
647 |
|
648 |
DeviceCreationParameterFloat::DeviceCreationParameterFloat(float fVal) : DeviceCreationParameter() { |
649 |
this->fVal = fVal; |
650 |
} |
651 |
|
652 |
DeviceCreationParameterFloat::DeviceCreationParameterFloat(String val) throw (Exception) { |
653 |
this->fVal = __parse_float(val); |
654 |
} |
655 |
|
656 |
void DeviceCreationParameterFloat::InitWithDefault() { |
657 |
std::map<String,String> Parameters; // empty parameters vector |
658 |
optional<float> f = DefaultAsFloat(Parameters); |
659 |
this->fVal = (f) ? *f : 0.0f; |
660 |
} |
661 |
|
662 |
String DeviceCreationParameterFloat::Type() { |
663 |
return "FLOAT"; |
664 |
} |
665 |
|
666 |
bool DeviceCreationParameterFloat::Multiplicity() { |
667 |
return false; |
668 |
} |
669 |
|
670 |
optional<String> DeviceCreationParameterFloat::Default(std::map<String,String> Parameters) { |
671 |
optional<float> defaultval = DefaultAsFloat(Parameters); |
672 |
if (!defaultval) return optional<String>::nothing; |
673 |
return ToString(*defaultval); |
674 |
} |
675 |
|
676 |
optional<String> DeviceCreationParameterFloat::RangeMin(std::map<String,String> Parameters) { |
677 |
optional<float> rangemin = RangeMinAsFloat(Parameters); |
678 |
if (!rangemin) return optional<String>::nothing; |
679 |
return ToString(*rangemin); |
680 |
} |
681 |
|
682 |
optional<String> DeviceCreationParameterFloat::RangeMax(std::map<String,String> Parameters) { |
683 |
optional<float> rangemax = RangeMaxAsFloat(Parameters); |
684 |
if (!rangemax) return optional<String>::nothing; |
685 |
return ToString(*rangemax); |
686 |
} |
687 |
|
688 |
optional<String> DeviceCreationParameterFloat::Possibilities(std::map<String,String> Parameters) { |
689 |
std::vector<float> possibilities = PossibilitiesAsFloat(Parameters); |
690 |
if (possibilities.empty()) return optional<String>::nothing; |
691 |
|
692 |
std::vector<float>::iterator iter = possibilities.begin(); |
693 |
std::stringstream ss; |
694 |
while (iter != possibilities.end()) { |
695 |
if (ss.str() != "") ss << ","; |
696 |
ss << *iter; |
697 |
iter++; |
698 |
} |
699 |
return ss.str(); |
700 |
} |
701 |
|
702 |
String DeviceCreationParameterFloat::Value() { |
703 |
return ToString(ValueAsFloat()); |
704 |
} |
705 |
|
706 |
void DeviceCreationParameterFloat::SetValue(String val) throw (Exception) { |
707 |
if (Fix()) throw Exception("Device parameter is read only"); |
708 |
float f = __parse_float(val); |
709 |
//if (RangeMinAsFloat() && i < *RangeMinAsFloat()) throw Exception("Invalid device parameter value: too small"); |
710 |
//if (RangeMaxAsFloat() && i > *RangeMaxAsFloat()) throw Exception("Invalid device parameter value: too big"); |
711 |
/*if (PossibilitiesAsFloat()) { |
712 |
bool valid = false; |
713 |
std::vector<float>* pPossibilities = PossibilitiesAsFloat(); |
714 |
std::vector<float>::iterator iter = pPossibilities->begin(); |
715 |
while (iter != pPossibilities->end()) { |
716 |
if (f == *iter) { |
717 |
valid = true; |
718 |
break; |
719 |
} |
720 |
iter++; |
721 |
} |
722 |
if (!valid) throw Exception("Invalid Device parameter value: not in set of possible values"); |
723 |
}*/ |
724 |
SetValue(f); |
725 |
} |
726 |
|
727 |
float DeviceCreationParameterFloat::ValueAsFloat() { |
728 |
return fVal; |
729 |
} |
730 |
|
731 |
void DeviceCreationParameterFloat::SetValue(float f) throw (Exception) { |
732 |
if (Fix()) throw Exception("Device parameter is read only"); |
733 |
OnSetValue(f); |
734 |
fVal = f; |
735 |
} |
736 |
|
737 |
|
738 |
|
739 |
// *************** DeviceCreationParameterString *************** |
740 |
// * |
741 |
|
742 |
DeviceCreationParameterString::DeviceCreationParameterString(String sVal) : DeviceCreationParameter() { |
743 |
this->sVal = __parse_string(sVal); |
744 |
} |
745 |
|
746 |
void DeviceCreationParameterString::InitWithDefault() { |
747 |
std::map<String,String> Parameters; // empty parameters vector |
748 |
optional<String> defaulval = DefaultAsString(Parameters); |
749 |
if (defaulval) this->sVal = *defaulval; |
750 |
else this->sVal = ""; |
751 |
} |
752 |
|
753 |
String DeviceCreationParameterString::Type() { |
754 |
return "STRING"; |
755 |
} |
756 |
|
757 |
bool DeviceCreationParameterString::Multiplicity() { |
758 |
return false; |
759 |
} |
760 |
|
761 |
optional<String> DeviceCreationParameterString::Default(std::map<String,String> Parameters) { |
762 |
optional<String> defaultval = DefaultAsString(Parameters); |
763 |
if (!defaultval) return optional<String>::nothing; |
764 |
return "'" + *defaultval + "'"; |
765 |
} |
766 |
|
767 |
optional<String> DeviceCreationParameterString::RangeMin(std::map<String,String> Parameters) { |
768 |
return optional<String>::nothing; |
769 |
} |
770 |
|
771 |
optional<String> DeviceCreationParameterString::RangeMax(std::map<String,String> Parameters) { |
772 |
return optional<String>::nothing; |
773 |
} |
774 |
|
775 |
optional<String> DeviceCreationParameterString::Possibilities(std::map<String,String> Parameters) { |
776 |
std::vector<String> possibilities = PossibilitiesAsString(Parameters); |
777 |
if (possibilities.empty()) return optional<String>::nothing; |
778 |
|
779 |
std::stringstream ss; |
780 |
std::vector<String>::iterator iter = possibilities.begin(); |
781 |
while (iter != possibilities.end()) { |
782 |
if (ss.str() != "") ss << ","; |
783 |
ss << "'" << *iter << "'"; |
784 |
iter++; |
785 |
} |
786 |
return ss.str(); |
787 |
} |
788 |
|
789 |
String DeviceCreationParameterString::Value() { |
790 |
return "\'" + ValueAsString() + "\'"; |
791 |
} |
792 |
|
793 |
void DeviceCreationParameterString::SetValue(String val) throw (Exception) { |
794 |
if (Fix()) throw Exception("Device parameter is read only"); |
795 |
SetValueAsString(__parse_string(val)); |
796 |
} |
797 |
|
798 |
String DeviceCreationParameterString::ValueAsString() { |
799 |
return sVal; |
800 |
} |
801 |
|
802 |
void DeviceCreationParameterString::SetValueAsString(String val) throw (Exception) { |
803 |
if (val.find("\'") != String::npos) throw Exception("Character -> \' <- not allowed"); |
804 |
if (val.find("\"") != String::npos) throw Exception("Character -> \" <- not allowed"); |
805 |
OnSetValue(val); |
806 |
sVal = val; |
807 |
} |
808 |
|
809 |
|
810 |
|
811 |
// *************** DeviceCreationParameterStrings *************** |
812 |
// * |
813 |
|
814 |
DeviceCreationParameterStrings::DeviceCreationParameterStrings(std::vector<String> sVals) : DeviceCreationParameter() { |
815 |
this->sVals = sVals; |
816 |
} |
817 |
|
818 |
DeviceCreationParameterStrings::DeviceCreationParameterStrings(String val) throw (Exception) { |
819 |
this->sVals = __parse_strings(val); |
820 |
} |
821 |
|
822 |
void DeviceCreationParameterStrings::InitWithDefault() { |
823 |
std::map<String,String> Parameters; // empty parameters vector |
824 |
optional<std::vector<String> > defaultval = DefaultAsStrings(Parameters); |
825 |
this->sVals = (defaultval) ? *defaultval : std::vector<String>(); |
826 |
} |
827 |
|
828 |
String DeviceCreationParameterStrings::Type() { |
829 |
return "STRING"; |
830 |
} |
831 |
|
832 |
bool DeviceCreationParameterStrings::Multiplicity() { |
833 |
return true; |
834 |
} |
835 |
|
836 |
optional<String> DeviceCreationParameterStrings::Default(std::map<String,String> Parameters) { |
837 |
std::vector<String> defaultval = DefaultAsStrings(Parameters); |
838 |
if (defaultval.empty()) return optional<String>::nothing; |
839 |
String result; |
840 |
std::vector<String>::iterator iter = defaultval.begin(); |
841 |
for (; iter != defaultval.end(); iter++) { |
842 |
if (result != "") result += ","; |
843 |
result += ("'" + *iter + "'"); |
844 |
} |
845 |
return result; |
846 |
} |
847 |
|
848 |
optional<String> DeviceCreationParameterStrings::RangeMin(std::map<String,String> Parameters) { |
849 |
return optional<String>::nothing; |
850 |
} |
851 |
|
852 |
optional<String> DeviceCreationParameterStrings::RangeMax(std::map<String,String> Parameters) { |
853 |
return optional<String>::nothing; |
854 |
} |
855 |
|
856 |
optional<String> DeviceCreationParameterStrings::Possibilities(std::map<String,String> Parameters) { |
857 |
std::vector<String> possibilities = PossibilitiesAsString(Parameters); |
858 |
if (possibilities.empty()) return optional<String>::nothing; |
859 |
|
860 |
std::stringstream ss; |
861 |
std::vector<String>::iterator iter = possibilities.begin(); |
862 |
while (iter != possibilities.end()) { |
863 |
if (ss.str() != "") ss << ","; |
864 |
ss << "'" << *iter << "'"; |
865 |
iter++; |
866 |
} |
867 |
return ss.str(); |
868 |
} |
869 |
|
870 |
String DeviceCreationParameterStrings::Value() { |
871 |
String result; |
872 |
std::vector<String>::iterator iter = this->sVals.begin(); |
873 |
while (iter != this->sVals.end()) { |
874 |
if (result != "") result += ","; |
875 |
result += "'" + *iter + "'"; |
876 |
iter++; |
877 |
} |
878 |
return result; |
879 |
} |
880 |
|
881 |
void DeviceCreationParameterStrings::SetValue(String val) throw (Exception) { |
882 |
if (Fix()) throw Exception("Device parameter is read only"); |
883 |
std::vector<String> vS = __parse_strings(val); |
884 |
SetValue(vS); |
885 |
} |
886 |
|
887 |
std::vector<String> DeviceCreationParameterStrings::ValueAsStrings() { |
888 |
return sVals; |
889 |
} |
890 |
|
891 |
void DeviceCreationParameterStrings::SetValue(std::vector<String> vS) throw (Exception) { |
892 |
if (Fix()) throw Exception("Device parameter is read only"); |
893 |
OnSetValue(vS); |
894 |
sVals = vS; |
895 |
} |
896 |
|
897 |
} // namespace LinuxSampler |