Belle II Software light-2406-ragdoll
Interface.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <framework/gearbox/Interface.h>
10#include <framework/gearbox/GearDir.h>
11#include <framework/gearbox/Unit.h>
12#include <framework/logging/Logger.h>
13
14#include <boost/lexical_cast.hpp>
15#include <boost/tokenizer.hpp>
16#include <boost/algorithm/string.hpp>
17#include <boost/format.hpp>
18
19namespace Belle2::gearbox {
20
21 std::vector<GearDir> Interface::getNodes(const std::string& path) const
22 {
23 int size = getNumberNodes(path);
24 std::vector<GearDir> result;
25 result.reserve(size);
26 for (int i = 1; i <= size; ++i) {
27 result.emplace_back(*this, path, i);
28 }
29 return result;
30 }
31
32 std::string Interface::getString(const std::string& path, const std::string& defaultValue) const
33 {
34 try {
35 return getString(path);
36 } catch (PathEmptyError&) {
37 return defaultValue;
38 }
39 }
40
41 double Interface::getDouble(const std::string& path) const noexcept(false)
42 {
43 std::string value = getString(path);
44 try {
45 return boost::lexical_cast<double>(value);
46 } catch (boost::bad_lexical_cast&) {
47 throw ConversionError() << path << value;
48 }
49 }
50
51 double Interface::getDouble(const std::string& path, double defaultValue) const noexcept(false)
52 {
53 try {
54 return getDouble(path);
55 } catch (PathEmptyError&) {
56 return defaultValue;
57 }
58 }
59
60 int Interface::getInt(const std::string& path) const noexcept(false)
61 {
62 std::string value = getString(path);
63 try {
64 //hide spurious warning in boost
65 return boost::lexical_cast<int>(value);
66 } catch (boost::bad_lexical_cast&) {
67 throw ConversionError() << path << value;
68 }
69 }
70
71 int Interface::getInt(const std::string& path, int defaultValue) const noexcept(false)
72 {
73 try {
74 return getInt(path);
75 } catch (PathEmptyError&) {
76 return defaultValue;
77 }
78 }
79
80 bool Interface::getBool(const std::string& path) const noexcept(false)
81 {
82 std::string value = getString(path);
83 boost::to_lower(value);
84 if (value.empty() || value == "false" || value == "off" || value == "0") return false;
85 return true;
86 }
87
88 bool Interface::getBool(const std::string& path, bool defaultValue) const
89 {
90 try {
91 return getBool(path);
92 } catch (PathEmptyError&) {
93 return defaultValue;
94 }
95 }
96
97 double Interface::getWithUnit(const std::string& path) const noexcept(false)
98 {
99 std::pair<std::string, std::string> value = getStringWithUnit(path);
100 double numValue(0);
101 try {
102 numValue = boost::lexical_cast<double>(value.first);
103 } catch (boost::bad_lexical_cast&) {
104 throw ConversionError() << path << value.first;
105 }
106 if (value.second.empty()) {
107 B2WARNING("Requested unit conversion for parameter '" << path << "' but no unit found");
108 } else {
109 numValue = Unit::convertValue(numValue, value.second);
110 }
111 return numValue;
112 }
113
114 double Interface::getWithUnit(const std::string& path, double defaultValue) const noexcept(false)
115 {
116 try {
117 return getWithUnit(path);
118 } catch (PathEmptyError&) {
119 return defaultValue;
120 }
121 }
122
123 std::vector<double> Interface::getArray(const std::string& path) const noexcept(false)
124 {
125 using tokenizer = boost::tokenizer<boost::char_separator<char> >;
126 boost::char_separator<char> sep(",; \t\n\r");
127
128 std::pair<std::string, std::string> value = getStringWithUnit(path);
129 tokenizer tokens(value.first, sep);
130 std::vector<double> result;
131 double numValue(0);
132 for (const std::string& tok : tokens) {
133 try {
134 numValue = boost::lexical_cast<double>(tok);
135 } catch (boost::bad_lexical_cast&) {
136 throw (ConversionError() << path << value.first);
137 }
138 if (!value.second.empty()) {
139 numValue = Unit::convertValue(numValue, value.second);
140 }
141 result.push_back(numValue);
142 }
143 return result;
144 }
145
146 std::vector<double> Interface::getArray(const std::string& path, const std::vector<double>& defaultValue) const noexcept(false)
147 {
148 try {
149 return getArray(path);
150 } catch (PathEmptyError&) {
151 return defaultValue;
152 }
153 }
154
155
156 std::string Interface::ensureNode(const std::string& path) const
157 {
158 return boost::trim_right_copy_if(path, boost::is_any_of("/"));
159 }
160
161 std::string Interface::ensurePath(const std::string& path) const
162 {
163 return ensureNode(path) + '/';
164 }
165
166 std::string Interface::addIndex(const std::string& path, int index) const
167 {
168 static boost::format index_fmt("%1%[%2%]");
169 return (index_fmt % ensureNode(path) % index).str();
170 }
171
172 std::string Interface::joinPath(const std::string& path, const std::string& subpath) const
173 {
174 return ensurePath(path) + boost::trim_left_copy_if(subpath, boost::is_any_of("/"));
175 }
176
177}
virtual std::string getString(const std::string &path="") const noexcept(false)=0
Get the parameter path as a string.
std::vector< double > getArray(const std::string &path) const noexcept(false)
Get the parameter path as a list of double values converted to the standard unit.
Definition: Interface.cc:123
double getDouble(const std::string &path="") const noexcept(false)
Get the parameter path as a double.
Definition: Interface.cc:41
std::string joinPath(const std::string &path, const std::string &subpath) const
joind to paths, inserting a slash if neccessary
Definition: Interface.cc:172
std::string addIndex(const std::string &path, int index) const
add [index] to the path (after stripping trailing slashes)
Definition: Interface.cc:166
virtual int getNumberNodes(const std::string &path="") const =0
Return the number of nodes a given path will expand to.
std::vector< GearDir > getNodes(const std::string &path="") const
Get vector of GearDirs which point to all the nodes the given path evaluates to.
Definition: Interface.cc:21
std::string ensureNode(const std::string &path) const
make sure the path really corresponds to an XPath node expression by removing trailing slashes
Definition: Interface.cc:156
std::string ensurePath(const std::string &path) const
make sure the path really corresponds to a path by appending a trailing slash if neccessary
Definition: Interface.cc:161
double getWithUnit(const std::string &path) const noexcept(false)
Get the parameter path as a double converted to the standard unit.
Definition: Interface.cc:97
bool getBool(const std::string &path="") const noexcept(false)
Get the parameter path as a bool.
Definition: Interface.cc:80
int getInt(const std::string &path="") const noexcept(false)
Get the parameter path as a int.
Definition: Interface.cc:60
static double convertValue(double value, const std::string &unitString)
Converts a floating point value to the standard framework unit.
Definition: UnitConst.cc:129