Skip to content

Commit

Permalink
Added std::source_location overload
Browse files Browse the repository at this point in the history
  • Loading branch information
realchonk committed Jul 17, 2022
1 parent 9ec08f1 commit c6fad5f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
17 changes: 16 additions & 1 deletion safmat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <variant>
#include <utility>
#include <cstring>
#include <version>
#include <string>
#include <memory>
#include <limits>
Expand All @@ -34,6 +35,10 @@
#include <array>
#include <span>

#if __cpp_lib_source_location >= 201907L
# include <source_location>
#endif

// Enable support for std::ostream Output (default=disabled).
#ifndef SAFMAT_OUT_OSTREAM
# define SAFMAT_OUT_OSTREAM 0
Expand Down Expand Up @@ -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<T>::write(out, s);
Expand Down Expand Up @@ -778,6 +783,16 @@ namespace safmat {
out.write(']');
}
};

#if __cpp_lib_source_location >= 201907L
template<>
struct Formatter<std::source_location> : 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
3 changes: 3 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit c6fad5f

Please sign in to comment.