8
8
9
9
#include < json/value.h>
10
10
11
+ #include < algorithm>
11
12
#include < ostream>
12
13
#include < utility>
13
14
14
15
namespace Mantid ::Kernel {
15
16
16
- const std::string OptionalBool::StrUnset = " Unset" ;
17
17
const std::string OptionalBool::StrFalse = " False" ;
18
18
const std::string OptionalBool::StrTrue = " True" ;
19
+ const std::string OptionalBool::StrUnset = " Unset" ;
19
20
20
- OptionalBool::OptionalBool () : m_arg(Unset) {}
21
-
22
- OptionalBool::OptionalBool (bool arg) { m_arg = arg ? True : False; }
21
+ OptionalBool::Value OptionalBool::Validate (const std::string &arg) {
22
+ auto l_toLower = [](char c) -> char { return (c >= ' A' && c <= ' Z' ? c + 0x20 : c); };
23
+ std::string argLower = arg;
24
+ std::transform (arg.begin (), arg.end (), argLower.begin (), l_toLower);
25
+ if (argLower == " false" ) {
26
+ return False;
27
+ } else if (argLower == " true" ) {
28
+ return True;
29
+ } else if (argLower == " unset" ) {
30
+ return Unset;
31
+ } else {
32
+ throw std::invalid_argument (" Invalid value for OptionalBool: " + arg);
33
+ }
34
+ }
23
35
24
- OptionalBool::OptionalBool (Value arg) : m_arg(arg) {}
36
+ OptionalBool::OptionalBool () : m_arg(Unset) {}
37
+ OptionalBool::OptionalBool (bool arg) : m_arg(OptionalBool::Value(arg)) {}
38
+ OptionalBool::OptionalBool (OptionalBool::Value arg) : m_arg(arg) {}
39
+ OptionalBool::OptionalBool (std::string arg) : m_arg(Validate(arg)) {}
40
+ OptionalBool::OptionalBool (char const *arg) : m_arg(Validate(std::string(arg))) {}
41
+ OptionalBool::OptionalBool (const int arg)
42
+ : m_arg(arg == 0 ? OptionalBool::False
43
+ : arg == 1 ? OptionalBool::True
44
+ : throw std::invalid_argument (" Invalid value for OptionalBool: " + std::to_string(arg) +
45
+ "\nAccepted values are 0 or 1")) {}
46
+ OptionalBool &OptionalBool::operator =(std::string const &arg) {
47
+ m_arg = OptionalBool::Validate (arg);
48
+ return *this ;
49
+ }
50
+ OptionalBool &OptionalBool::operator =(char const *arg) {
51
+ m_arg = OptionalBool::Validate (std::string (arg));
52
+ return *this ;
53
+ }
54
+ OptionalBool &OptionalBool::operator =(const int arg) {
55
+ m_arg = arg == 0 ? OptionalBool::False
56
+ : arg == 1 ? OptionalBool::True
57
+ : throw std::invalid_argument (" Invalid value for OptionalBool: " + std::to_string (arg) +
58
+ " \n Accepted values are 0 or 1" );
59
+ return *this ;
60
+ }
25
61
26
62
bool OptionalBool::operator ==(const OptionalBool &other) const { return m_arg == other.getValue (); }
27
63
@@ -37,17 +73,17 @@ std::ostream &operator<<(std::ostream &os, OptionalBool const &object) {
37
73
std::istream &operator >>(std::istream &istream, OptionalBool &object) {
38
74
std::string result;
39
75
istream >> result;
40
- object.m_arg = OptionalBool::strToEmumMap ()[result];
76
+ object.m_arg = OptionalBool::strToEnumMap ()[result];
41
77
return istream;
42
78
}
43
79
44
- std::map<std::string, OptionalBool::Value> OptionalBool::strToEmumMap () {
80
+ std::map<std::string, OptionalBool::Value> OptionalBool::strToEnumMap () {
45
81
return {{StrUnset, OptionalBool::Unset}, {StrFalse, OptionalBool::False}, {StrTrue, OptionalBool::True}};
46
82
}
47
83
48
84
std::map<OptionalBool::Value, std::string> OptionalBool::enumToStrMap () {
49
- std::map<Value, std::string> map;
50
- auto opposite = strToEmumMap ();
85
+ std::map<OptionalBool:: Value, std::string> map;
86
+ auto opposite = strToEnumMap ();
51
87
for (auto &oppositePair : opposite) {
52
88
map.emplace (oppositePair.second , oppositePair.first );
53
89
}
0 commit comments