Mantid
Loading...
Searching...
No Matches
OptionalBool.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8
9#include <json/value.h>
10
11#include <algorithm>
12#include <ostream>
13#include <utility>
14
15namespace Mantid::Kernel {
16
17const std::string OptionalBool::StrFalse = "False";
18const std::string OptionalBool::StrTrue = "True";
19const std::string OptionalBool::StrUnset = "Unset";
20
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}
35
36OptionalBool::OptionalBool() : m_arg(Unset) {}
39OptionalBool::OptionalBool(std::string arg) : m_arg(Validate(arg)) {}
40OptionalBool::OptionalBool(char const *arg) : m_arg(Validate(std::string(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")) {}
46OptionalBool &OptionalBool::operator=(std::string const &arg) {
48 return *this;
49}
51 m_arg = OptionalBool::Validate(std::string(arg));
52 return *this;
53}
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 "\nAccepted values are 0 or 1");
59 return *this;
60}
61
62bool OptionalBool::operator==(const OptionalBool &other) const { return m_arg == other.getValue(); }
63
64bool OptionalBool::operator!=(const OptionalBool &other) const { return m_arg != other.getValue(); }
65
67
68std::ostream &operator<<(std::ostream &os, OptionalBool const &object) {
69 os << OptionalBool::enumToStrMap()[object.getValue()];
70 return os;
71}
72
73std::istream &operator>>(std::istream &istream, OptionalBool &object) {
74 std::string result;
75 istream >> result;
76 object.m_arg = OptionalBool::strToEnumMap()[result];
77 return istream;
78}
79
80std::map<std::string, OptionalBool::Value> OptionalBool::strToEnumMap() {
82}
83
84std::map<OptionalBool::Value, std::string> OptionalBool::enumToStrMap() {
85 std::map<OptionalBool::Value, std::string> map;
86 auto opposite = strToEnumMap();
87 for (auto &oppositePair : opposite) {
88 map.emplace(oppositePair.second, oppositePair.first);
89 }
90 return map;
91}
92
98Json::Value encodeAsJson(const OptionalBool &value) {
99 const auto enumValue = value.getValue();
100 if (enumValue == OptionalBool::True)
101 return Json::Value(true);
102 else if (enumValue == OptionalBool::False)
103 return Json::Value(false);
104 else
105 return Json::Value();
106}
107
108// Specialization of ToCpp for OptionalBool
109namespace pwvjdetail {
110
111bool ToCpp<OptionalBool>::operator()(const Json::Value &value) { return value.asBool(); }
112
113} // namespace pwvjdetail
114
115} // namespace Mantid::Kernel
double value
The value of the point.
Definition FitMW.cpp:51
OptionalBool : Tri-state bool.
static std::map< Value, std::string > enumToStrMap()
Value Validate(const std::string &arg)
bool operator==(const OptionalBool &other) const
bool operator!=(const OptionalBool &other) const
static const std::string StrUnset
static std::map< std::string, Value > strToEnumMap()
static const std::string StrFalse
static const std::string StrTrue
OptionalBool & operator=(const OptionalBool &other)=default
MANTID_KERNEL_DLL std::ostream & operator<<(std::ostream &, CPUTimer &)
Convenience function to provide for easier debug printing.
Definition CPUTimer.cpp:86
MANTID_KERNEL_DLL std::istream & operator>>(std::istream &, Interpolation &)
Reads in parameter value.
MANTID_KERNEL_DLL::Json::Value encodeAsJson(const OptionalBool &)
Encode an OptionalBool as a Json::Value.
STL namespace.
std::string to_string(const wide_integer< Bits, Signed > &n)