/[svn]/linuxsampler/trunk/src/drivers/DeviceParameter.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/drivers/DeviceParameter.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1495 - (show annotations) (download)
Mon Nov 19 22:10:18 2007 UTC (16 years, 5 months ago) by schoenebeck
File size: 31310 byte(s)
* LSCP: allow naughty liblscp to send non-string device parameters
  within apostrophes as well

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

  ViewVC Help
Powered by ViewVC