Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/options/Option.cpp
169678 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2001-2025 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
const std::string&
235
Option::getListSeparator() const {
236
return myListSeparator;
237
}
238
239
void
240
Option::setListSeparator(const std::string& listSep) {
241
myListSeparator = listSep;
242
}
243
244
const std::string&
245
Option::getSubTopic() const {
246
return mySubTopic;
247
}
248
249
250
void
251
Option::setSubtopic(const std::string& subtopic) {
252
mySubTopic = subtopic;
253
}
254
255
256
const std::string&
257
Option::getTypeName() const {
258
return myTypeName;
259
}
260
261
// -------------------------------------------------------------------------
262
// Option_Integer - methods
263
// -------------------------------------------------------------------------
264
265
Option_Integer::Option_Integer(int value) :
266
Option(true),
267
myValue(value) {
268
myTypeName = "INT";
269
myValueString = toString(value);
270
}
271
272
273
int
274
Option_Integer::getInt() const {
275
return myValue;
276
}
277
278
279
bool
280
Option_Integer::set(const std::string& v, const std::string& orig, const bool /* append */) {
281
try {
282
myValue = StringUtils::toInt(v);
283
return markSet(orig);
284
} catch (...) {
285
std::string s = "'" + v + "' is not a valid integer.";
286
throw ProcessError(s);
287
}
288
}
289
290
291
bool
292
Option_Integer::isInteger() const {
293
return true;
294
}
295
296
// -------------------------------------------------------------------------
297
// Option_String - methods
298
// -------------------------------------------------------------------------
299
300
Option_String::Option_String() :
301
Option() {
302
myTypeName = "STR";
303
}
304
305
306
Option_String::Option_String(const std::string& value, std::string typeName) :
307
Option(true),
308
myValue(value) {
309
myTypeName = typeName;
310
myValueString = value;
311
}
312
313
314
std::string
315
Option_String::getString() const {
316
return myValue;
317
}
318
319
320
bool
321
Option_String::set(const std::string& v, const std::string& orig, const bool /* append */) {
322
myValue = v;
323
return markSet(orig);
324
}
325
326
// -------------------------------------------------------------------------
327
// Option_Float - methods
328
// -------------------------------------------------------------------------
329
330
Option_Float::Option_Float(double value) :
331
Option(true),
332
myValue(value) {
333
myTypeName = "FLOAT";
334
std::ostringstream oss;
335
oss << value;
336
myValueString = oss.str();
337
}
338
339
340
double
341
Option_Float::getFloat() const {
342
return myValue;
343
}
344
345
346
bool
347
Option_Float::set(const std::string& v, const std::string& orig, const bool /* append */) {
348
try {
349
myValue = StringUtils::toDouble(v);
350
return markSet(orig);
351
} catch (...) {
352
throw ProcessError(TLF("'%' is not a valid float.", v));
353
}
354
}
355
356
357
bool
358
Option_Float::isFloat() const {
359
return true;
360
}
361
362
// -------------------------------------------------------------------------
363
// Option_Bool - methods
364
// -------------------------------------------------------------------------
365
366
Option_Bool::Option_Bool(bool value) :
367
Option(true),
368
myValue(value) {
369
myTypeName = "BOOL";
370
myValueString = value ? "true" : "false";
371
}
372
373
374
bool
375
Option_Bool::getBool() const {
376
return myValue;
377
}
378
379
380
bool
381
Option_Bool::set(const std::string& v, const std::string& orig, const bool /* append */) {
382
try {
383
myValue = StringUtils::toBool(v);
384
return markSet(orig);
385
} catch (...) {
386
throw ProcessError(TLF("'%' is not a valid bool.", v));
387
}
388
}
389
390
391
bool
392
Option_Bool::isBool() const {
393
return true;
394
}
395
396
// -------------------------------------------------------------------------
397
// Option_BoolExtended - methods
398
// -------------------------------------------------------------------------
399
400
Option_BoolExtended::Option_BoolExtended(bool value) :
401
Option_Bool(value) {
402
}
403
404
405
bool
406
Option_BoolExtended::set(const std::string& v, const std::string& orig, const bool /* append */) {
407
try {
408
myValue = StringUtils::toBool(v);
409
return markSet("");
410
} catch (...) {
411
myValue = true;
412
}
413
return markSet(orig);
414
}
415
416
// -------------------------------------------------------------------------
417
// Option_IntVector - methods
418
// -------------------------------------------------------------------------
419
420
Option_IntVector::Option_IntVector() :
421
Option() {
422
myTypeName = "INT[]";
423
}
424
425
426
Option_IntVector::Option_IntVector(const IntVector& value)
427
: Option(true), myValue(value) {
428
myTypeName = "INT[]";
429
myValueString = joinToString(value, ",");
430
}
431
432
433
const IntVector&
434
Option_IntVector::getIntVector() const {
435
return myValue;
436
}
437
438
439
bool
440
Option_IntVector::set(const std::string& v, const std::string& orig, const bool append) {
441
if (!append) {
442
myValue.clear();
443
}
444
try {
445
if (v.find(';') != std::string::npos) {
446
WRITE_WARNING(TL("Please note that using ';' as list separator is deprecated and not accepted anymore."));
447
}
448
StringTokenizer st(v, ",", true);
449
while (st.hasNext()) {
450
myValue.push_back(StringUtils::toInt(st.next()));
451
}
452
return markSet(orig);
453
} catch (EmptyData&) {
454
throw ProcessError("Empty element occurred in " + v);
455
} catch (...) {
456
throw ProcessError(TLF("'%' is not a valid integer vector.", v));
457
}
458
}
459
460
// -------------------------------------------------------------------------
461
// Option_StringVector - methods
462
// -------------------------------------------------------------------------
463
464
Option_StringVector::Option_StringVector() :
465
Option() {
466
myTypeName = "STR[]";
467
}
468
469
470
Option_StringVector::Option_StringVector(const StringVector& value) :
471
Option(true), myValue(value) {
472
myTypeName = "STR[]";
473
myValueString = joinToString(value, ",");
474
}
475
476
477
const StringVector&
478
Option_StringVector::getStringVector() const {
479
return myValue;
480
}
481
482
483
bool
484
Option_StringVector::set(const std::string& v, const std::string& orig, const bool append) {
485
if (!append) {
486
myValue.clear();
487
}
488
StringTokenizer st(v, ",");
489
while (st.hasNext()) {
490
myValue.push_back(StringUtils::prune(st.next()));
491
}
492
return markSet(append && getValueString() != "" ? getValueString() + "," + orig : orig);
493
}
494
495
// -------------------------------------------------------------------------
496
// Option_FileName - methods
497
// -------------------------------------------------------------------------
498
499
Option_FileName::Option_FileName() :
500
Option_StringVector() {
501
myTypeName = "FILE";
502
}
503
504
505
Option_FileName::Option_FileName(const StringVector& value) :
506
Option_StringVector(value) {
507
myTypeName = "FILE";
508
}
509
510
511
bool
512
Option_FileName::isFileName() const {
513
return true;
514
}
515
516
517
std::string
518
Option_FileName::getString() const {
519
return joinToString(getStringVector(), ",");
520
}
521
522
// -------------------------------------------------------------------------
523
// Option_Network - methods
524
// -------------------------------------------------------------------------
525
526
Option_Network::Option_Network(const std::string& value) :
527
Option_String(value, "NETWORK") {
528
}
529
530
531
bool Option_Network::isNetwork() const {
532
return true;
533
}
534
535
// -------------------------------------------------------------------------
536
// Option_Additional - methods
537
// -------------------------------------------------------------------------
538
539
Option_Additional::Option_Additional(const std::string& value) :
540
Option_String(value, "ADDITIONAL") {
541
}
542
543
544
bool
545
Option_Additional::isAdditional() const {
546
return true;
547
}
548
549
// -------------------------------------------------------------------------
550
// Option_Route - methods
551
// -------------------------------------------------------------------------
552
553
Option_Route::Option_Route(const std::string& value) :
554
Option_String(value, "ROUTE") {
555
}
556
557
558
bool
559
Option_Route::isRoute() const {
560
return true;
561
}
562
563
// -------------------------------------------------------------------------
564
// Option_Data - methods
565
// -------------------------------------------------------------------------
566
567
Option_Data::Option_Data(const std::string& value) :
568
Option_String(value, "DATA") {
569
}
570
571
572
bool
573
Option_Data::isData() const {
574
return true;
575
}
576
577
// -------------------------------------------------------------------------
578
// Option_Data - methods
579
// -------------------------------------------------------------------------
580
581
Option_SumoConfig::Option_SumoConfig(const std::string& value) :
582
Option_String(value, "SUMOCONFIG") {
583
}
584
585
586
bool
587
Option_SumoConfig::isSumoConfig() const {
588
return true;
589
}
590
591
// -------------------------------------------------------------------------
592
// Option_Data - methods
593
// -------------------------------------------------------------------------
594
595
Option_Edge::Option_Edge(const std::string& value) :
596
Option_String(value, "EDGE") {
597
}
598
599
600
bool
601
Option_Edge::isEdge() const {
602
return true;
603
}
604
605
// -------------------------------------------------------------------------
606
// Option_Data - methods
607
// -------------------------------------------------------------------------
608
609
Option_EdgeVector::Option_EdgeVector(const std::string& value) :
610
Option_String(value, "EDGE[]") {
611
}
612
613
614
bool
615
Option_EdgeVector::isEdgeVector() const {
616
return true;
617
}
618
619
/****************************************************************************/
620
621