9#include <framework/gearbox/Interface.h>
10#include <framework/gearbox/GearDir.h>
11#include <framework/gearbox/Unit.h>
12#include <framework/logging/Logger.h>
14#include <boost/lexical_cast.hpp>
15#include <boost/tokenizer.hpp>
16#include <boost/algorithm/string.hpp>
17#include <boost/format.hpp>
19namespace Belle2::gearbox {
24 std::vector<GearDir> result;
26 for (
int i = 1; i <= size; ++i) {
27 result.emplace_back(*
this, path, i);
36 }
catch (PathEmptyError&) {
43 std::string value = getString(path);
45 return boost::lexical_cast<double>(value);
46 }
catch (boost::bad_lexical_cast&) {
47 throw ConversionError() << path << value;
54 return getDouble(path);
55 }
catch (PathEmptyError&) {
62 std::string value = getString(path);
65 return boost::lexical_cast<int>(value);
66 }
catch (boost::bad_lexical_cast&) {
67 throw ConversionError() << path << value;
75 }
catch (PathEmptyError&) {
82 std::string value = getString(path);
83 boost::to_lower(value);
84 if (value.empty() || value ==
"false" || value ==
"off" || value ==
"0")
return false;
92 }
catch (PathEmptyError&) {
99 std::pair<std::string, std::string> value = getStringWithUnit(path);
102 numValue = boost::lexical_cast<double>(value.first);
103 }
catch (boost::bad_lexical_cast&) {
104 throw ConversionError() << path << value.first;
106 if (value.second.empty()) {
107 B2WARNING(
"Requested unit conversion for parameter '" << path <<
"' but no unit found");
117 return getWithUnit(path);
118 }
catch (PathEmptyError&) {
125 using tokenizer = boost::tokenizer<boost::char_separator<char> >;
126 boost::char_separator<char> sep(
",; \t\n\r");
128 std::pair<std::string, std::string> value = getStringWithUnit(path);
129 tokenizer tokens(value.first, sep);
130 std::vector<double> result;
132 for (
const std::string& tok : tokens) {
134 numValue = boost::lexical_cast<double>(tok);
135 }
catch (boost::bad_lexical_cast&) {
136 throw (ConversionError() << path << value.first);
138 if (!value.second.empty()) {
141 result.push_back(numValue);
146 std::vector<double>
Interface::getArray(
const std::string& path,
const std::vector<double>& defaultValue)
const noexcept(
false)
149 return getArray(path);
150 }
catch (PathEmptyError&) {
158 return boost::trim_right_copy_if(path, boost::is_any_of(
"/"));
168 static boost::format index_fmt(
"%1%[%2%]");
169 return (index_fmt %
ensureNode(path) % index).str();
174 return ensurePath(path) + boost::trim_left_copy_if(subpath, boost::is_any_of(
"/"));
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.
double getDouble(const std::string &path="") const noexcept(false)
Get the parameter path as a double.
std::string joinPath(const std::string &path, const std::string &subpath) const
joind to paths, inserting a slash if neccessary
std::string addIndex(const std::string &path, int index) const
add [index] to the path (after stripping trailing slashes)
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.
std::string ensureNode(const std::string &path) const
make sure the path really corresponds to an XPath node expression by removing trailing slashes
std::string ensurePath(const std::string &path) const
make sure the path really corresponds to a path by appending a trailing slash if neccessary
double getWithUnit(const std::string &path) const noexcept(false)
Get the parameter path as a double converted to the standard unit.
bool getBool(const std::string &path="") const noexcept(false)
Get the parameter path as a bool.
int getInt(const std::string &path="") const noexcept(false)
Get the parameter path as a int.
static double convertValue(double value, const std::string &unitString)
Converts a floating point value to the standard framework unit.