Add fmt::formatter specialisations for fmtlib-10

This commit is contained in:
Danny Robson 2023-07-21 14:20:49 +10:00
parent 343b3d36ce
commit 1fa4eba5d3
13 changed files with 179 additions and 30 deletions

View File

@ -10,6 +10,7 @@
#include <vector>
#include <iosfwd>
#include <fmt/core.h>
///////////////////////////////////////////////////////////////////////////////
@ -27,3 +28,15 @@ namespace cruft {
std::ostream&
operator<< (std::ostream&, backtrace const&);
}
template <>
struct fmt::formatter<cruft::backtrace> {
constexpr format_parse_context::iterator
parse (format_parse_context &ctx)
{
return ctx.begin ();
}
format_context::iterator
format (cruft::backtrace const&, format_context &ctx);
};

View File

@ -11,6 +11,8 @@
#include "cast.hpp"
#include "iterator/zip.hpp"
#include <fmt/ostream.h>
#include <ostream>
#include <memory>
@ -44,9 +46,20 @@ backtrace::backtrace (void)
///////////////////////////////////////////////////////////////////////////////
std::ostream&
cruft::operator <<(std::ostream &os, backtrace const &rhs) {
os << "[ ";
fmt::print (os, "{}", rhs);
return os;
}
auto const &frames = rhs.frames ();
///////////////////////////////////////////////////////////////////////////////
fmt::format_context::iterator
fmt::formatter<cruft::backtrace>::format (
cruft::backtrace const &obj,
fmt::format_context &ctx
) {
fmt::format_to (ctx.out (), "[ ");
auto const &frames = obj.frames ();
using strings_t = std::unique_ptr<char *, decltype(&std::free)>;
strings_t const names (
@ -58,11 +71,14 @@ cruft::operator <<(std::ostream &os, backtrace const &rhs) {
);
for (auto const [idx,addr]: cruft::iterator::izip (frames)) {
os << "{ addr: " << frames[idx]
<< ", name: '" << names.get()[idx] << '\''
<< " }, ";
fmt::format_to (
ctx.out (),
"{{ addr: {}, name: '{}', }}, ",
frames[idx],
names.get()[idx]
);
}
os << " ]";
return os;
}
return fmt::format_to (ctx.out (), " ]");
}

View File

@ -11,6 +11,8 @@
#include "./traits.hpp"
#include "../iterator/infix.hpp"
#include <fmt/format.h>
#include <cstddef>
#include <ostream>
#include <algorithm>
@ -33,3 +35,26 @@ namespace cruft {
return os;
}
}
template <typename T>
struct fmt::formatter<
T,
std::enable_if_t<cruft::is_coord_v<T>, char>
> {
constexpr format_parse_context::iterator
parse (format_parse_context &ctx)
{
return ctx.begin ();
}
format_context::iterator
format (T const &val, format_context &ctx)
{
return fmt::format_to (
ctx.out (),
"[{}]",
fmt::join (std::begin (val), std::end (val), ", ")
);
}
};

View File

@ -8,6 +8,8 @@
#include "except.hpp"
#include <sstream>
///////////////////////////////////////////////////////////////////////////////
std::ostream&
@ -15,3 +17,16 @@ cruft::operator<< (std::ostream &os, const cruft::error &obj)
{
return obj.describe (os);
}
///////////////////////////////////////////////////////////////////////////////
fmt::format_context::iterator
fmt::formatter<cruft::error>::format (
cruft::error const &obj,
fmt::format_context &ctx
) {
std::ostringstream os;
obj.describe (os);
return fmt::format_to (ctx.out (), "{}", os.str ());
}

View File

@ -8,6 +8,8 @@
#pragma once
#include <fmt/core.h>
#include <utility>
#include <iosfwd>
#include <string>
@ -62,4 +64,17 @@ namespace cruft {
/// std::ostream.
std::ostream&
operator<< (std::ostream &os, error const&);
}
}
template <>
struct fmt::formatter<cruft::error> {
constexpr format_parse_context::iterator
parse (format_parse_context &ctx)
{
return ctx.begin ();
}
format_context::iterator
format (cruft::error const&, format_context &ctx);
};

1
io.cpp
View File

@ -13,6 +13,7 @@
#include "posix/except.hpp"
#include <fmt/ostream.h>
#include <fmt/std.h>
#include <cstdio>
#include <fcntl.h>

View File

@ -9,6 +9,7 @@
#pragma once
#include <iosfwd>
#include <fmt/core.h>
namespace cruft {
@ -35,6 +36,20 @@ namespace cruft {
};
template <typename T>
std::ostream& operator<< (std::ostream&, const accumulator<T>&);
std::ostream&
operator<< (std::ostream&, const accumulator<T>&);
}
}
}
template <typename T>
struct fmt::formatter<cruft::stats::accumulator<T>> {
constexpr format_parse_context::iterator
parse (format_parse_context &ctx)
{
return ctx.begin ();
}
format_context::iterator
format (cruft::stats::accumulator<T> const&, format_context &ctx);
};

View File

@ -50,11 +50,11 @@ namespace cruft::TAP {
//---------------------------------------------------------------------
template <typename ...Args, size_t N>
bool
expect (const bool test, const char (&fmt)[N], Args&&... args)
expect (const bool test, const char (&format)[N], Args&&... args)
{
m_output << (test ? "ok " : "not ok ") << ++m_size
<< " - ";
fmt::print (m_output, fmt, std::forward<Args> (args)...);
fmt::vprint (m_output, format, fmt::make_format_args (std::forward<Args> (args)...));
m_output << std::endl;
if (!test)

View File

@ -2,6 +2,8 @@
#include "../exe.hpp"
#include <fmt/std.h>
#include <filesystem>

View File

@ -3,6 +3,7 @@
#include "tap.hpp"
#include <fmt/ostream.h>
#include <fmt/std.h>
#include <ostream>

View File

@ -156,7 +156,7 @@ cruft::polled_duration::stop (void) {
m_series.add (dt / MILLISECOND);
if (m_next < now) {
LOG_DEBUG ("timing: '{:s}'. {:s}", m_name, m_series);
LOG_DEBUG ("timing: '{:s}'. {}", m_name, m_series);
m_series.reset ();
m_next = now + m_interval;
}

62
uri.cpp
View File

@ -7,7 +7,7 @@
#include "./debug/assert.hpp"
#include "./debug/warn.hpp"
#include <ostream>
#include <fmt/ostream.h>
using cruft::uri;
@ -340,23 +340,33 @@ cruft::uri::percent_decode (view<const char*> s)
///////////////////////////////////////////////////////////////////////////////
static constexpr
char const*
to_string (cruft::uri::component val)
{
switch (val) {
case cruft::uri::SCHEME: return "SCHEME";
case cruft::uri::USER: return "USER";
case cruft::uri::HOST: return "HOST";
case cruft::uri::PORT: return "PORT";
case cruft::uri::PATH: return "PATH";
case cruft::uri::QUERY: return "QUERY";
case cruft::uri::FRAGMENT: return "FRAGMENT";
case cruft::uri::NUM_COMPONENTS:
break;
}
unhandled (val);
}
std::ostream&
cruft::operator<< (std::ostream &os, cruft::uri::component c)
{
switch (c) {
case cruft::uri::SCHEME: return os << "SCHEME";
case cruft::uri::USER: return os << "USER";
case cruft::uri::HOST: return os << "HOST";
case cruft::uri::PORT: return os << "PORT";
case cruft::uri::PATH: return os << "PATH";
case cruft::uri::QUERY: return os << "QUERY";
case cruft::uri::FRAGMENT: return os << "FRAGMENT";
case cruft::uri::NUM_COMPONENTS:
unreachable ();
}
unreachable ();
fmt::print (os, "{}", c);
return os;
}
@ -364,7 +374,27 @@ cruft::operator<< (std::ostream &os, cruft::uri::component c)
std::ostream&
cruft::operator<< (std::ostream &os, cruft::uri const &val)
{
return os << val.value ();
fmt::print (os, "{}", val);
return os;
}
//-----------------------------------------------------------------------------
fmt::format_context::iterator
fmt::formatter<cruft::uri::component>::format (
cruft::uri::component val,
fmt::format_context &ctx
) const {
return fmt::formatter<std::string>::format (::to_string (val), ctx);
}
//-----------------------------------------------------------------------------
fmt::format_context::iterator
fmt::formatter<cruft::uri>::format (
cruft::uri const &val,
fmt::format_context &ctx
) const {
return fmt::formatter<std::string>::format (val.value (), ctx);
}

16
uri.hpp
View File

@ -11,6 +11,8 @@
#include "debug/assert.hpp"
#include "view.hpp"
#include <fmt/core.h>
#include <array>
#include <iosfwd>
#include <map>
@ -156,3 +158,17 @@ namespace cruft {
std::ostream& operator<< (std::ostream&, uri const&);
std::ostream& operator<< (std::ostream&, uri::component);
}
template <>
struct fmt::formatter<cruft::uri> : public fmt::formatter<std::string> {
format_context::iterator
format (cruft::uri const&, format_context &ctx) const;
};
template <>
struct fmt::formatter<cruft::uri::component> : public fmt::formatter<std::string> {
format_context::iterator
format (cruft::uri::component, format_context &ctx) const;
};