diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2024-02-26 18:46:21 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2024-02-26 20:06:25 +0100 |
commit | bde5939b2f1eb5c04c1ab429a79fa799c3bfee28 (patch) | |
tree | b1c49902c88746d9b2facaf20f4f52d9197c6dd9 /compat/debug.cpp | |
parent | 8a380af2b8f28400f29a5468a3a1a5629a665867 (diff) |
implement printing fractions for corrade's Debug{}
Diffstat (limited to 'compat/debug.cpp')
-rw-r--r-- | compat/debug.cpp | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/compat/debug.cpp b/compat/debug.cpp index 0a77d298..0c7a6fd1 100644 --- a/compat/debug.cpp +++ b/compat/debug.cpp @@ -1,11 +1,16 @@ #include "debug.hpp" #include "compat/strerror.hpp" #include <cerrno> +#include <cstdio> #include <Corrade/Containers/StringView.h> // Error{} << "error" << colon() << "can't open file" << colon() << quoted("foo") << error_string(EINVAL); // ===> "error: can't open file 'foo': Invalid argument" +#ifdef __clang__ +#pragma clang diagnostic ignored "-Wformat-nonliteral" +#endif + namespace floormat::detail::corrade_debug { Debug& operator<<(Debug& dbg, Colon box) @@ -46,6 +51,15 @@ Debug& quoted_end(Debug& dbg, Debug::Flags flags, char c) template struct Quoted<StringView>; +Debug& operator<<(Debug& dbg, Fraction f) +{ + char fmt[8], buf[56]; + std::snprintf(fmt, sizeof fmt, "%%.%hhuf", f.decimal_points); + std::snprintf(buf, sizeof buf, fmt, (double)f.value); + dbg << buf; + return dbg; +} + } // namespace floormat::detail::corrade_debug namespace floormat { @@ -56,4 +70,6 @@ Colon colon(char c) { return Colon{c}; } ErrorString error_string(int error) { return { error }; } ErrorString error_string() { return { errno }; } +Fraction fraction(float value, uint8_t decimal_points) { return Fraction { value, decimal_points }; } + } // namespace floormat |