ChimeraTK-DeviceAccess 03.26.00
Loading...
Searching...
No Matches
Boolean.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2// SPDX-License-Identifier: LGPL-3.0-or-later
3#pragma once
4
5#include <algorithm>
6#include <istream>
7#include <limits>
8
9namespace ChimeraTK {
10
11 /********************************************************************************************************************/
12
16 class Boolean {
17 public:
18 constexpr Boolean() : _value() {}
19 // We want implicit construction and conversion. Turn off the linter warnings.
20 // NOLINTBEGIN(hicpp-explicit-conversions, google-explicit-constructor)
21 constexpr Boolean(bool value) : _value(value) {}
22
23 constexpr operator const bool&() const { return _value; }
24 constexpr operator bool&() { return _value; }
25 // NOLINTEND(hicpp-explicit-conversions, google-explicit-constructor)
26 // TODO: test user types to numeric etc
27
28 private:
29 bool _value;
30 };
31
32 /********************************************************************************************************************/
33
34 inline std::istream& operator>>(std::istream& is, Boolean& value) {
35 std::string data;
36 is >> data;
37
38 std::transform(data.begin(), data.end(), data.begin(), [](unsigned char c) { return std::tolower(c); });
39
40 if(data == "false" || data == "0" || data.empty()) {
41 value = false;
42 }
43 else {
44 value = true;
45 }
46 return is;
47 }
48
49 /********************************************************************************************************************/
50
51 template<typename T>
52 constexpr bool isBoolean = std::is_same_v<T, bool> || std::is_same_v<T, Boolean>;
53
54 /********************************************************************************************************************/
55
56 // Define ChimeraTK::to_string to convert Boolean into string. We cannot define std::to_string(Boolean), as it would
57 // violate the C++ standard. The right definition can be autoselected with
58 // "using std::to_string; using ChimeraTK::to_string;".
59 // NOLINTNEXTLINE(readability-identifier-naming) - name is fixed by C++ standard
60 inline std::string to_string(Boolean& value) {
61 if(value) {
62 return "true";
63 }
64 return "false";
65 }
66
67 /********************************************************************************************************************/
68
69} // namespace ChimeraTK
70
71/********************************************************************************************************************/
72
73namespace std {
74 template<>
75 class numeric_limits<ChimeraTK::Boolean> : public numeric_limits<bool> {};
76} // namespace std
77
78/********************************************************************************************************************/
Wrapper Class to avoid vector<bool> problems.
Definition Boolean.h:16
constexpr Boolean(bool value)
Definition Boolean.h:21
constexpr Boolean()
Definition Boolean.h:18
std::string to_string(Boolean &value)
Definition Boolean.h:60
constexpr bool isBoolean
Definition Boolean.h:52
std::istream & operator>>(std::istream &is, Boolean &value)
Definition Boolean.h:34
STL namespace.