|
22 | 22 | #include <string> |
23 | 23 | #include <type_traits> |
24 | 24 | #include <array> |
| 25 | +#include <sstream> |
| 26 | +#include <fmt/format.h> |
25 | 27 | #endif |
26 | 28 |
|
27 | 29 | namespace o2 |
@@ -135,6 +137,11 @@ class Vertex : public VertexBase |
135 | 137 | { |
136 | 138 | } |
137 | 139 |
|
| 140 | +#if !defined(GPUCA_NO_FMT) && !defined(GPUCA_GPUCODE_DEVICE) |
| 141 | + void print() const; |
| 142 | + std::string asString() const; |
| 143 | +#endif |
| 144 | + |
138 | 145 | GPUd() ushort getNContributors() const { return mNContributors; } |
139 | 146 | GPUd() void setNContributors(ushort v) { mNContributors = v; } |
140 | 147 | GPUd() void addContributor() { mNContributors++; } |
@@ -162,6 +169,49 @@ class Vertex : public VertexBase |
162 | 169 |
|
163 | 170 | #if !defined(GPUCA_GPUCODE_DEVICE) && !defined(GPUCA_NO_FMT) |
164 | 171 | std::ostream& operator<<(std::ostream& os, const o2::dataformats::VertexBase& v); |
| 172 | + |
| 173 | +namespace detail |
| 174 | +{ |
| 175 | +template <typename T> |
| 176 | +concept Streamable = requires(std::ostream& os, const T& a) { |
| 177 | + { os << a } -> std::same_as<std::ostream&>; |
| 178 | +}; |
| 179 | + |
| 180 | +template <typename T> |
| 181 | +concept HasFormattableTimeStamp = requires(const T& t) { |
| 182 | + { fmt::format("{}", t.getTimeStamp()) } -> std::convertible_to<std::string>; |
| 183 | +}; |
| 184 | +} // namespace detail |
| 185 | + |
| 186 | +template <typename Stamp> |
| 187 | +inline std::string Vertex<Stamp>::asString() const |
| 188 | +{ |
| 189 | + const std::string stamp = [&]() -> std::string { |
| 190 | + if constexpr (detail::Streamable<Stamp>) { |
| 191 | + std::ostringstream oss; |
| 192 | + oss << mTimeStamp; |
| 193 | + return oss.str(); |
| 194 | + } else if constexpr (detail::HasFormattableTimeStamp<Stamp>) { |
| 195 | + return fmt::format("{}", mTimeStamp.getTimeStamp()); |
| 196 | + } else { |
| 197 | + return "X"; |
| 198 | + } |
| 199 | + }(); |
| 200 | + return fmt::format("{} NContrib:{} Chi2:{:.2f} Flags:{:b} Stamp:{}", VertexBase::asString(), mNContributors, mChi2, mBits, stamp); |
| 201 | +} |
| 202 | + |
| 203 | +template <typename Stamp> |
| 204 | +inline std::ostream& operator<<(std::ostream& os, const o2::dataformats::Vertex<Stamp>& v) |
| 205 | +{ |
| 206 | + os << v.asString(); |
| 207 | + return os; |
| 208 | +} |
| 209 | + |
| 210 | +template <typename Stamp> |
| 211 | +inline void Vertex<Stamp>::print() const |
| 212 | +{ |
| 213 | + std::cout << *this << '\n'; |
| 214 | +} |
165 | 215 | #endif |
166 | 216 |
|
167 | 217 | } // namespace dataformats |
|
0 commit comments