ComPWA
Common Partial-Wave-Analysis Framework
Utils.hpp
Go to the documentation of this file.
1 #ifndef CORE_UTILS_HPP_
2 #define CORE_UTILS_HPP_
3 
4 #include "Core/Logging.hpp"
5 
6 #include <boost/algorithm/string/predicate.hpp>
7 #include <boost/algorithm/string/trim.hpp>
8 #include <boost/optional.hpp>
9 #include <boost/property_tree/ptree.hpp>
10 
11 #include <cmath>
12 #include <limits>
13 #include <sstream>
14 #include <vector>
15 
16 namespace ComPWA {
17 namespace Utils {
18 
21 inline bool equal(double x, double y, int nEpsilon) {
22  return std::abs(x - y) < std::numeric_limits<double>::epsilon() *
23  std::abs(x + y) * nEpsilon ||
24  std::abs(x - y) < std::numeric_limits<double>::min();
25 }
26 
27 inline double shiftAngle(double value) {
28  double originalVal = value;
29  while (value > M_PI)
30  value -= 2 * M_PI;
31  while (value < -1.0 * M_PI)
32  value += 2 * M_PI;
33  if (value != originalVal)
34  LOG(DEBUG) << "shiftAngle() | Shifting parameter from " << originalVal
35  << " to " << value << "!";
36  return value;
37 }
38 
41 inline std::vector<std::string> splitString(const std::string &str,
42  char separator = ' ') {
43  std::vector<std::string> result;
44  std::istringstream ss(str);
45  std::string token;
46 
47  while (std::getline(ss, token, separator)) {
48  result.push_back(token);
49  }
50  return result;
51 }
52 
53 } // namespace Utils
54 } // namespace ComPWA
55 
57  typedef std::string internal_type;
58  typedef bool external_type;
59 
60  // Converts a string to bool
61  boost::optional<external_type> get_value(const internal_type &str) {
62  if (!str.empty()) {
63  // first remove leading and trailing whitespace
64  auto strcopy(str);
65  using boost::algorithm::iequals;
66  using boost::algorithm::trim;
67  trim(strcopy);
68  if (iequals(strcopy, "true") || iequals(strcopy, "yes") || strcopy == "1")
69  return boost::optional<external_type>(true);
70  else
71  return boost::optional<external_type>(false);
72  } else
73  return boost::optional<external_type>(boost::none);
74  }
75 
76  // Converts a bool to string
77  boost::optional<internal_type> put_value(const external_type &b) {
78  return boost::optional<internal_type>(b ? "true" : "false");
79  }
80 };
81 
82 namespace boost {
83 namespace property_tree {
84 
85 template <typename Ch, typename Traits, typename Alloc>
86 struct translator_between<std::basic_string<Ch, Traits, Alloc>, bool> {
87  typedef BoolTranslator type;
88 };
89 
90 } // namespace property_tree
91 } // namespace boost
92 
93 #endif
bool equal(double x, double y, int nEpsilon)
Check of numbers x and are equal within nEpsion times the numerical limit.
Definition: Utils.hpp:21
Support for serialization of std::shared_ptr (and other types) is added in boost 1.56 .
boost::optional< external_type > get_value(const internal_type &str)
Definition: Utils.hpp:61
double shiftAngle(double value)
Definition: Utils.hpp:27
std::vector< std::string > splitString(const std::string &str, char separator=' ')
split the string into pieces, which are separated by the separator character (default separator: spac...
Definition: Utils.hpp:41
bool external_type
Definition: Utils.hpp:58
boost::optional< internal_type > put_value(const external_type &b)
Definition: Utils.hpp:77
std::string internal_type
Definition: Utils.hpp:57