EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Options.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Options.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2020 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
10 
11 #include <cstdlib>
12 #include <istream>
13 #include <ostream>
14 
15 namespace {
16 static constexpr char s_rangeSeparator = ':';
17 }
18 
20  std::istream& is, ActsExamples::Options::Interval& interval) {
21  std::string buf;
22  is >> buf;
23 
24  // default to an unbounded interval
25  interval.lower.reset();
26  interval.upper.reset();
27 
28  // find the limit separator
29  auto pos = buf.find_first_of(s_rangeSeparator);
30  // no separator -> invalid input -> unbounded interval
31  if (pos == std::string::npos) {
32  return is;
33  }
34 
35  // if it exists, parse limit before separator
36  if (0 < pos) {
37  auto lowerStr = buf.substr(0, pos);
38  interval.lower = std::atof(lowerStr.c_str());
39  }
40  // if it exists, parse limit after separator
41  if ((pos + 1) < buf.size()) {
42  auto upperStr = buf.substr(pos + 1);
43  interval.upper = std::atof(upperStr.c_str());
44  }
45 
46  return is;
47 }
48 
50  std::ostream& os, const ActsExamples::Options::Interval& interval) {
51  if (not interval.lower.has_value() and not interval.upper.has_value()) {
52  os << "unbounded";
53  } else {
54  if (interval.lower.has_value()) {
55  os << interval.lower.value();
56  }
57  os << s_rangeSeparator;
58  if (interval.upper.has_value()) {
59  os << interval.upper.value();
60  }
61  }
62  return os;
63 }
64 
66  std::istream& is, std::vector<ActsExamples::Options::Interval>& intervals) {
67  for (auto& interval : intervals) {
68  is >> interval;
69  }
70  return is;
71 }
72 
74  std::ostream& os,
75  const std::vector<ActsExamples::Options::Interval>& intervals) {
76  for (auto& interval : intervals) {
77  os << interval;
78  }
79  return os;
80 }
81 
82 namespace {
84 template <typename Iterator>
85 inline std::ostream& printContainer(std::ostream& os, Iterator begin,
86  Iterator end, const char* separator) {
87  for (auto it = begin; it != end; ++it) {
88  if (it != begin) {
89  os << separator;
90  }
91  os << *it;
92  }
93  return os;
94 }
95 } // namespace
96 
97 std::ostream& std::operator<<(std::ostream& os, const read_series& vec) {
98  return printContainer(os, vec.begin(), vec.end(), " ");
99 }
100 
101 std::ostream& std::operator<<(std::ostream& os, const read_range& vec) {
102  return printContainer(os, vec.begin(), vec.end(), " ");
103 }
104 
105 std::ostream& std::operator<<(std::ostream& os, const read_strings& vec) {
106  return printContainer(os, vec.begin(), vec.end(), " ");
107 }