| 270 | }; |
| 271 | |
| 272 | struct DiffFlags : DiffBase<uint32_t> { |
| 273 | DiffFlags(const std::string_view textHeaderIn, const std::string_view jsonPathIn, |
| 274 | const std::optional<uint32_t>& value1, const std::optional<uint32_t>& value2, |
| 275 | const char*(*bitToString)(uint32_t, bool)) |
| 276 | : DiffBase<uint32_t>(textHeaderIn, jsonPathIn, value1, value2), toStringFn(bitToString) {} |
| 277 | |
| 278 | DiffFlags(const std::string_view textHeaderIn, const std::string_view jsonPathIn, const uint32_t& value1, const uint32_t& value2, |
| 279 | const char*(*bitToString)(uint32_t, bool)) |
| 280 | : DiffBase<uint32_t>(textHeaderIn, jsonPathIn, value1, value2), toStringFn(bitToString) {} |
| 281 | |
| 282 | virtual std::string value(std::size_t index, OutputFormat format) const override { |
| 283 | const auto space = format != OutputFormat::json_mini ? " " : ""; |
| 284 | const auto quote = format == OutputFormat::text ? "" : "\""; |
| 285 | |
| 286 | std::stringstream formattedValue; |
| 287 | bool first = true; |
| 288 | std::string comma = ""; |
| 289 | for (uint32_t bitIndex = 0; bitIndex < 32; ++bitIndex) { |
| 290 | uint32_t bitMask = 1u << bitIndex; |
| 291 | bool bitValue = (bitMask & DiffBase<uint32_t>::rawValue(index)) != 0; |
| 292 | |
| 293 | const char* bitStr = toStringFn(bitIndex, bitValue); |
| 294 | if (bitStr) { |
| 295 | fmt::print(formattedValue, "{}{}{}{}", comma, quote, bitStr, quote); |
| 296 | } else if (bitValue) { |
| 297 | fmt::print(formattedValue, "{}{}", comma, bitMask); |
| 298 | } |
| 299 | |
| 300 | if (bitValue && first) { |
| 301 | first = false; |
| 302 | comma = fmt::format(",{}", space); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | if (format == OutputFormat::text) { |
| 307 | return fmt::format("0x{:x} ({})", DiffBase<uint32_t>::rawValue(index), formattedValue.str()); |
| 308 | } else { |
| 309 | if (formattedValue.str().empty()) { |
| 310 | return "[]"; |
| 311 | } else { |
| 312 | return fmt::format("[{}{}{}]", space, formattedValue.str(), space); |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | private: |
| 318 | const char*(*toStringFn)(uint32_t, bool); |
| 319 | }; |
| 320 | |
| 321 | template <typename T, std::size_t N> |
| 322 | struct DiffArray : public DiffBase<std::array<T, N>> { |
no outgoing calls
no test coverage detected