ParameterSetID.cc
Go to the documentation of this file.
1 // ======================================================================
2 //
3 // ParameterSetID
4 //
5 // ======================================================================
6 
9 
10 #include <iomanip>
11 
12 using namespace boost;
13 using namespace cet;
14 using namespace fhicl;
15 using namespace std;
16 
17 // ======================================================================
18 
19 constexpr sha1::digest_t invalid_id{{}};
20 
21 // ----------------------------------------------------------------------
22 
23 ParameterSetID::ParameterSetID() noexcept : valid_{false}, id_{invalid_id} {}
24 
25 ParameterSetID::ParameterSetID(ParameterSet const& ps) : valid_{false}, id_{}
26 {
27  reset(ps);
28 }
29 
31  : valid_{id.size() == max_str_size()}, id_{}
32 {
33  if (valid_) {
34  for (size_t i = 0, e = id_.size(); i != e; ++i) {
35  id_[i] = std::stoi(id.substr(i * 2, 2), nullptr, 16);
36  }
37  if (id != to_string()) {
39  << "ParameterSetID construction failure: " << id
40  << " != " << to_string() << ".\n";
41  }
42  } else if (id.empty()) {
43  id_ = invalid_id;
44  } else {
46  << "Attempt to construct ParameterSetID from inappropriate input: " << id
47  << ".\n";
48  }
49 }
50 
51 // ----------------------------------------------------------------------
52 
53 bool
55 {
56  return valid_;
57 }
58 
59 string
61 {
62  std::ostringstream oss;
63  oss << std::hex << std::setfill('0');
64  // The range-for loop performs an implicit cast from 'unsigned char'
65  // to 'unsigned int'.
66  for (unsigned int const num : id_) {
67  oss << std::setw(2) << num;
68  }
69  return oss.str();
70 }
71 
72 // ----------------------------------------------------------------------
73 
74 void
76 {
77  valid_ = false;
78  id_ = invalid_id;
79 }
80 
81 void
83 {
84  auto const& hash = ps.to_string();
85  sha1 sha{hash.c_str()};
86 
87  id_ = sha.digest();
88  valid_ = true;
89 }
90 
91 void
93 {
94  id_.swap(other.id_);
95  std::swap(valid_, other.valid_);
96 }
97 
98 // ----------------------------------------------------------------------
99 
100 bool
102 {
103  return id_ == other.id_;
104 }
105 
106 bool
108 {
109  return id_ != other.id_;
110 }
111 
112 bool
114 {
115  return id_ < other.id_;
116 }
117 
118 bool
120 {
121  return id_ > other.id_;
122 }
123 
124 bool
126 {
127  return id_ <= other.id_;
128 }
129 
130 bool
132 {
133  return id_ >= other.id_;
134 }
135 
136 // ----------------------------------------------------------------------
137 
138 ostream&
139 fhicl::operator<<(ostream& os, ParameterSetID const& psid)
140 {
141  return os << psid.to_string();
142 }
143 
144 // ======================================================================
void swap(ParameterSetID &)
std::string to_string() const
bool operator>(ParameterSetID const &) const noexcept
bool operator>=(ParameterSetID const &) const noexcept
std::string string
Definition: nybbler.cc:12
Definition: sha1.h:26
STL namespace.
bool operator!=(ParameterSetID const &) const noexcept
QTextStream & hex(QTextStream &s)
cet::sha1::digest_t id_
std::array< uchar, digest_sz > digest_t
Definition: sha1.h:30
const double e
void swap(Handle< T > &a, Handle< T > &b)
static constexpr std::size_t max_str_size() noexcept
bool operator<(ParameterSetID const &) const noexcept
static constexpr double ps
Definition: Units.h:99
bool is_valid() const noexcept
bool operator<=(ParameterSetID const &) const noexcept
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
constexpr sha1::digest_t invalid_id
void reset(ParameterSet const &)
void invalidate() noexcept
std::ostream & operator<<(std::ostream &, ParameterSetID const &)
Q_EXPORT QTSManip setfill(int f)
Definition: qtextstream.h:337
bool operator==(ParameterSetID const &) const noexcept
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
decltype(auto) constexpr empty(T &&obj)
ADL-aware version of std::empty.
Definition: StdUtils.h:97
std::string to_string() const
Definition: ParameterSet.h:153