Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/options/Option.cpp
193693 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2001-2026 German Aerospace Center (DLR) and others.
4
// This program and the accompanying materials are made available under the
5
// terms of the Eclipse Public License 2.0 which is available at
6
// https://www.eclipse.org/legal/epl-2.0/
7
// This Source Code may also be made available under the following Secondary
8
// Licenses when the conditions for such availability set forth in the Eclipse
9
// Public License 2.0 are satisfied: GNU General Public License, version 2
10
// or later which is available at
11
// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
/****************************************************************************/
14
/// @file Option.cpp
15
/// @author Daniel Krajzewicz
16
/// @author Jakob Erdmann
17
/// @author Michael Behrisch
18
/// @date Mon, 17 Dec 2001
19
///
20
// A class representing a single program option
21
/****************************************************************************/
22
#include <config.h>
23
24
#include <string>
25
#include <exception>
26
#include <sstream>
27
#include "Option.h"
28
#include <utils/common/StringUtils.h>
29
#include <utils/common/UtilExceptions.h>
30
#include <utils/common/StringTokenizer.h>
31
#include <utils/common/StringUtils.h>
32
#include <utils/common/MsgHandler.h>
33
#include <utils/common/ToString.h>
34
35
36
// ===========================================================================
37
// method definitions
38
// ===========================================================================
39
40
// -------------------------------------------------------------------------
41
// Option - methods
42
// -------------------------------------------------------------------------
43
44
Option::Option(bool set) :
45
myAmSet(set) {
46
}
47
48
49
Option::~Option() {}
50
51
52
bool
53
Option::isSet() const {
54
return myAmSet;
55
}
56
57
58
double
59
Option::getFloat() const {
60
throw InvalidArgument("This is not a double-option");
61
}
62
63
64
int
65
Option::getInt() const {
66
throw InvalidArgument("This is not an int-option");
67
}
68
69
70
std::string
71
Option::getString() const {
72
throw InvalidArgument("This is not a string-option");
73
}
74
75
76
bool
77
Option::getBool() const {
78
throw InvalidArgument("This is not a bool-option");
79
}
80
81
82
const IntVector&
83
Option::getIntVector() const {
84
throw InvalidArgument("This is not an int vector-option");
85
}
86
87
88
const StringVector&
89
Option::getStringVector() const {
90
throw InvalidArgument("This is not a string vector-option");
91
}
92
93
94
bool
95
Option::markSet(const std::string& orig) {
96
bool ret = myAmWritable;
97
myHaveTheDefaultValue = false;
98
myAmSet = true;
99
myAmWritable = false;
100
myValueString = orig;
101
return ret;
102
}
103
104
105
const std::string&
106
Option::getValueString() const {
107
return myValueString;
108
}
109
110
111
bool
112
Option::isDefault() const {
113
return myHaveTheDefaultValue;
114
}
115
116
117
bool
118
Option::isInteger() const {
119
return false;
120
}
121
122
123
bool
124
Option::isFloat() const {
125
return false;
126
}
127
128
129
bool
130
Option::isBool() const {
131
return false;
132
}
133
134
135
bool
136
Option::isFileName() const {
137
return false;
138
}
139
140
141
bool
142
Option::isNetwork() const {
143
return false;
144
}
145
146
147
bool
148
Option::isAdditional() const {
149
return false;
150
}
151
152
153
bool
154
Option::isRoute() const {
155
return false;
156
}
157
158
159
bool
160
Option::isData() const {
161
return false;
162
}
163
164
165
bool
166
Option::isSumoConfig() const {
167
return false;
168
}
169
170
171
bool
172
Option::isEdge() const {
173
return false;
174
}
175
176
177
bool
178
Option::isEdgeVector() const {
179
return false;
180
}
181
182
183
bool
184
Option::isWriteable() const {
185
return myAmWritable;
186
}
187
188
189
void
190
Option::resetWritable() {
191
myAmWritable = true;
192
}
193
194
195
void
196
Option::resetDefault() {
197
myHaveTheDefaultValue = true;
198
}
199
200
201
const std::string&
202
Option::getDescription() const {
203
return myDescription;
204
}
205
206
207
void
208
Option::setDescription(const std::string& desc) {
209
myDescription = desc;
210
}
211
212
213
bool
214
Option::isRequired() const {
215
return myRequired;
216
}
217
218
219
void
220
Option::setRequired() {
221
myRequired = true;
222
}
223
224
bool
225
Option::isPositional() const {
226
return myPositional;
227
}
228
229
void
230
Option::setPositional() {
231
myPositional = true;
232
}
233
234
235
bool
236
Option::isEditable() const {
237
return myEditable;
238
}
239
240
241
void Option::setEditable(const bool value) {
242
myEditable = value;
243
}
244
245
246
const std::string&
247
Option::getListSeparator() const {
248
return myListSeparator;
249
}
250
251
void
252
Option::setListSeparator(const std::string& listSep) {
253
myListSeparator = listSep;
254
}
255
256
const std::string&
257
Option::getSubTopic() const {
258
return mySubTopic;
259
}
260
261
262
void
263
Option::setSubtopic(const std::string& subtopic) {
264
mySubTopic = subtopic;
265
}
266
267
268
const std::string&
269
Option::getTypeName() const {
270
return myTypeName;
271
}
272
273
// -------------------------------------------------------------------------
274
// Option_Integer - methods
275
// -------------------------------------------------------------------------
276
277
Option_Integer::Option_Integer(int value) :
278
Option(true),
279
myValue(value) {
280
myTypeName = "INT";
281
myValueString = toString(value);
282
}
283
284
285
int
286
Option_Integer::getInt() const {
287
return myValue;
288
}
289
290
291
bool
292
Option_Integer::set(const std::string& v, const std::string& orig, const bool /* append */) {
293
try {
294
myValue = StringUtils::toInt(v);
295
return markSet(orig);
296
} catch (...) {
297
std::string s = "'" + v + "' is not a valid integer.";
298
throw ProcessError(s);
299
}
300
}
301
302
303
bool
304
Option_Integer::isInteger() const {
305
return true;
306
}
307
308
// -------------------------------------------------------------------------
309
// Option_String - methods
310
// -------------------------------------------------------------------------
311
312
Option_String::Option_String() :
313
Option() {
314
myTypeName = "STR";
315
}
316
317
318
Option_String::Option_String(const std::string& value, std::string typeName) :
319
Option(true),
320
myValue(value) {
321
myTypeName = typeName;
322
myValueString = value;
323
}
324
325
326
std::string
327
Option_String::getString() const {
328
return myValue;
329
}
330
331
332
bool
333
Option_String::set(const std::string& v, const std::string& orig, const bool /* append */) {
334
myValue = v;
335
return markSet(orig);
336
}
337
338
// -------------------------------------------------------------------------
339
// Option_Float - methods
340
// -------------------------------------------------------------------------
341
342
Option_Float::Option_Float(double value) :
343
Option(true),
344
myValue(value) {
345
myTypeName = "FLOAT";
346
std::ostringstream oss;
347
oss << value;
348
myValueString = oss.str();
349
}
350
351
352
double
353
Option_Float::getFloat() const {
354
return myValue;
355
}
356
357
358
bool
359
Option_Float::set(const std::string& v, const std::string& orig, const bool /* append */) {
360
try {
361
myValue = StringUtils::toDouble(v);
362
return markSet(orig);
363
} catch (...) {
364
throw ProcessError(TLF("'%' is not a valid float.", v));
365
}
366
}
367
368
369
bool
370
Option_Float::isFloat() const {
371
return true;
372
}
373
374
// -------------------------------------------------------------------------
375
// Option_Bool - methods
376
// -------------------------------------------------------------------------
377
378
Option_Bool::Option_Bool(bool value) :
379
Option(true),
380
myValue(value) {
381
myTypeName = "BOOL";
382
myValueString = value ? "true" : "false";
383
}
384
385
386
bool
387
Option_Bool::getBool() const {
388
return myValue;
389
}
390
391
392
bool
393
Option_Bool::set(const std::string& v, const std::string& orig, const bool /* append */) {
394
try {
395
myValue = StringUtils::toBool(v);
396
return markSet(orig);
397
} catch (...) {
398
throw ProcessError(TLF("'%' is not a valid bool.", v));
399
}
400
}
401
402
403
bool
404
Option_Bool::isBool() const {
405
return true;
406
}
407
408
// -------------------------------------------------------------------------
409
// Option_BoolExtended - methods
410
// -------------------------------------------------------------------------
411
412
Option_BoolExtended::Option_BoolExtended(bool value) :
413
Option_Bool(value) {
414
}
415
416
417
bool
418
Option_BoolExtended::set(const std::string& v, const std::string& orig, const bool /* append */) {
419
try {
420
myValue = StringUtils::toBool(v);
421
return markSet("");
422
} catch (...) {
423
myValue = true;
424
}
425
return markSet(orig);
426
}
427
428
// -------------------------------------------------------------------------
429
// Option_IntVector - methods
430
// -------------------------------------------------------------------------
431
432
Option_IntVector::Option_IntVector() :
433
Option() {
434
myTypeName = "INT[]";
435
}
436
437
438
Option_IntVector::Option_IntVector(const IntVector& value)
439
: Option(true), myValue(value) {
440
myTypeName = "INT[]";
441
myValueString = joinToString(value, ",");
442
}
443
444
445
const IntVector&
446
Option_IntVector::getIntVector() const {
447
return myValue;
448
}
449
450
451
bool
452
Option_IntVector::set(const std::string& v, const std::string& orig, const bool append) {
453
if (!append) {
454
myValue.clear();
455
}
456
try {
457
if (v.find(';') != std::string::npos) {
458
WRITE_WARNING(TL("Please note that using ';' as list separator is deprecated and not accepted anymore."));
459
}
460
StringTokenizer st(v, ",", true);
461
while (st.hasNext()) {
462
myValue.push_back(StringUtils::toInt(st.next()));
463
}
464
return markSet(orig);
465
} catch (EmptyData&) {
466
throw ProcessError("Empty element occurred in " + v);
467
} catch (...) {
468
throw ProcessError(TLF("'%' is not a valid integer vector.", v));
469
}
470
}
471
472
// -------------------------------------------------------------------------
473
// Option_StringVector - methods
474
// -------------------------------------------------------------------------
475
476
Option_StringVector::Option_StringVector() :
477
Option() {
478
myTypeName = "STR[]";
479
}
480
481
482
Option_StringVector::Option_StringVector(const StringVector& value) :
483
Option(true), myValue(value) {
484
myTypeName = "STR[]";
485
myValueString = joinToString(value, ",");
486
}
487
488
489
const StringVector&
490
Option_StringVector::getStringVector() const {
491
return myValue;
492
}
493
494
495
bool
496
Option_StringVector::set(const std::string& v, const std::string& orig, const bool append) {
497
if (!append) {
498
myValue.clear();
499
}
500
StringTokenizer st(v, ",");
501
while (st.hasNext()) {
502
myValue.push_back(StringUtils::prune(st.next()));
503
}
504
return markSet(append && getValueString() != "" ? getValueString() + "," + orig : orig);
505
}
506
507
// -------------------------------------------------------------------------
508
// Option_FileName - methods
509
// -------------------------------------------------------------------------
510
511
Option_FileName::Option_FileName() :
512
Option_StringVector() {
513
myTypeName = "FILE";
514
}
515
516
517
Option_FileName::Option_FileName(const StringVector& value) :
518
Option_StringVector(value) {
519
myTypeName = "FILE";
520
}
521
522
523
bool
524
Option_FileName::isFileName() const {
525
return true;
526
}
527
528
529
std::string
530
Option_FileName::getString() const {
531
return joinToString(getStringVector(), ",");
532
}
533
534
// -------------------------------------------------------------------------
535
// Option_Network - methods
536
// -------------------------------------------------------------------------
537
538
Option_Network::Option_Network(const std::string& value) :
539
Option_String(value, "NETWORK") {
540
}
541
542
543
bool Option_Network::isNetwork() const {
544
return true;
545
}
546
547
// -------------------------------------------------------------------------
548
// Option_Additional - methods
549
// -------------------------------------------------------------------------
550
551
Option_Additional::Option_Additional(const std::string& value) :
552
Option_String(value, "ADDITIONAL") {
553
}
554
555
556
bool
557
Option_Additional::isAdditional() const {
558
return true;
559
}
560
561
// -------------------------------------------------------------------------
562
// Option_Route - methods
563
// -------------------------------------------------------------------------
564
565
Option_Route::Option_Route(const std::string& value) :
566
Option_String(value, "ROUTE") {
567
}
568
569
570
bool
571
Option_Route::isRoute() const {
572
return true;
573
}
574
575
// -------------------------------------------------------------------------
576
// Option_Data - methods
577
// -------------------------------------------------------------------------
578
579
Option_Data::Option_Data(const std::string& value) :
580
Option_String(value, "DATA") {
581
}
582
583
584
bool
585
Option_Data::isData() const {
586
return true;
587
}
588
589
// -------------------------------------------------------------------------
590
// Option_Data - methods
591
// -------------------------------------------------------------------------
592
593
Option_SumoConfig::Option_SumoConfig(const std::string& value) :
594
Option_String(value, "SUMOCONFIG") {
595
}
596
597
598
bool
599
Option_SumoConfig::isSumoConfig() const {
600
return true;
601
}
602
603
// -------------------------------------------------------------------------
604
// Option_Data - methods
605
// -------------------------------------------------------------------------
606
607
Option_Edge::Option_Edge(const std::string& value) :
608
Option_String(value, "EDGE") {
609
}
610
611
612
bool
613
Option_Edge::isEdge() const {
614
return true;
615
}
616
617
// -------------------------------------------------------------------------
618
// Option_Data - methods
619
// -------------------------------------------------------------------------
620
621
Option_EdgeVector::Option_EdgeVector(const std::string& value) :
622
Option_String(value, "EDGE[]") {
623
}
624
625
626
bool
627
Option_EdgeVector::isEdgeVector() const {
628
return true;
629
}
630
631
/****************************************************************************/
632
633