From c6fad5f3b20aa132676df38a7bdeb2d2a2d8f46d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20St=C3=BCrz?= Date: Sun, 17 Jul 2022 20:24:03 +0200 Subject: [PATCH] Added std::source_location overload --- safmat.hpp | 17 ++++++++++++++++- test.cpp | 3 +++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/safmat.hpp b/safmat.hpp index 3477dfc..7c72fdd 100644 --- a/safmat.hpp +++ b/safmat.hpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -34,6 +35,10 @@ #include #include +#if __cpp_lib_source_location >= 201907L +# include +#endif + // Enable support for std::ostream Output (default=disabled). #ifndef SAFMAT_OUT_OSTREAM # define SAFMAT_OUT_OSTREAM 0 @@ -84,7 +89,7 @@ namespace safmat::io { struct OutputImpl : OutputBase { T *out; - OutputImpl(T *out) : out(out) {} + OutputImpl(T *out) noexcept : out(out) {} void write(std::string_view s) const override { OutputAdapter::write(out, s); @@ -778,6 +783,16 @@ namespace safmat { out.write(']'); } }; + +#if __cpp_lib_source_location >= 201907L + template<> + struct Formatter : internal::PaddedFormatter { + void format_to(FormatContext &ctx, const std::source_location &loc) { + auto &out = ctx.out; + safmat::format_to(out, "{}:{}:{}", loc.file_name(), loc.line(), loc.column()); + } + }; +#endif } #endif // FILE_SAFMAT_HPP diff --git a/test.cpp b/test.cpp index 0d4a193..0d5b79f 100644 --- a/test.cpp +++ b/test.cpp @@ -31,6 +31,9 @@ int main() { using namespace std::literals; using namespace safmat; + auto loc = std::source_location::current(); + println("loc = {}", loc); + try { println("'{:x^100}'", "Hello"); println("Hello {:0{}}", 42, 100);