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