ECCE @ 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 
19 std::istream& FW::Options::operator>>(std::istream& is,
20  FW::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 
49 std::ostream& FW::Options::operator<<(std::ostream& os,
50  const FW::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 
65 namespace {
67 template <typename Iterator>
68 inline std::ostream& printContainer(std::ostream& os, Iterator begin,
69  Iterator end, const char* separator) {
70  for (auto it = begin; it != end; ++it) {
71  if (it != begin) {
72  os << separator;
73  }
74  os << *it;
75  }
76  return os;
77 }
78 } // namespace
79 
80 std::ostream& std::operator<<(std::ostream& os, const read_series& vec) {
81  return printContainer(os, vec.begin(), vec.end(), " ");
82 }
83 
84 std::ostream& std::operator<<(std::ostream& os, const read_range& vec) {
85  return printContainer(os, vec.begin(), vec.end(), " ");
86 }
87 
88 std::ostream& std::operator<<(std::ostream& os, const read_strings& vec) {
89  return printContainer(os, vec.begin(), vec.end(), " ");
90 }