From dadf4ce2aba713522d1949a16ffbf0fa5d6e6298 Mon Sep 17 00:00:00 2001 From: Dario Izzo Date: Thu, 20 Oct 2022 11:52:59 +0200 Subject: [PATCH] stream formatters added to monomial types --- include/obake/detail/fmt_compat.hpp | 64 +++++++++++++++++++ .../obake/polynomials/d_packed_monomial.hpp | 15 ++++- include/obake/polynomials/packed_monomial.hpp | 15 ++++- src/polynomials/packed_monomial.cpp | 4 +- 4 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 include/obake/detail/fmt_compat.hpp diff --git a/include/obake/detail/fmt_compat.hpp b/include/obake/detail/fmt_compat.hpp new file mode 100644 index 00000000..d42779e3 --- /dev/null +++ b/include/obake/detail/fmt_compat.hpp @@ -0,0 +1,64 @@ +// Copyright 2020, 2021, 2022 Francesco Biscani (bluescarni@gmail.com), Dario Izzo (dario.izzo@gmail.com) +// +// This file is part of the heyoka library. +// +// This Source Code Form is subject to the terms of the Mozilla +// Public License v. 2.0. If a copy of the MPL was not distributed +// with this file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#ifndef OBAKE_DETAIL_FMT_COMPAT_HPP +#define OBAKE_DETAIL_FMT_COMPAT_HPP + +#include + +#include + +#if FMT_VERSION >= 90000L + +#include + +#else + +#include + +#include + +#endif + +namespace obake::detail +{ + +#if FMT_VERSION >= 90000L + +using ostream_formatter = fmt::ostream_formatter; + +#else + +struct ostream_formatter { + template + constexpr auto parse(ParseContext &ctx) + { + if (ctx.begin() != ctx.end()) { + // LCOV_EXCL_START + throw std::invalid_argument("The ostream formatter does not accept any format string"); + // LCOV_EXCL_STOP + } + + return ctx.begin(); + } + + template + auto format(const T &x, FormatContext &ctx) + { + std::ostringstream oss; + oss << x; + + return fmt::format_to(ctx.out(), "{}", oss.str()); + } +}; + +#endif + +} // namespace obake::detail + +#endif \ No newline at end of file diff --git a/include/obake/polynomials/d_packed_monomial.hpp b/include/obake/polynomials/d_packed_monomial.hpp index 5752e665..0b0f9621 100644 --- a/include/obake/polynomials/d_packed_monomial.hpp +++ b/include/obake/polynomials/d_packed_monomial.hpp @@ -27,9 +27,6 @@ #include #include -#include -#include - #include #include #include @@ -37,6 +34,7 @@ #include #include +#include #include #include #include @@ -1424,6 +1422,17 @@ inline constexpr bool monomial_hash_is_homomorphic> } // namespace obake +// fmt formatter for d_packed_monomial, implemented +// on top of the streaming operator. +namespace fmt +{ + +template +struct formatter> : obake::detail::ostream_formatter { +}; + +} // namespace fmt + namespace boost::serialization { diff --git a/include/obake/polynomials/packed_monomial.hpp b/include/obake/polynomials/packed_monomial.hpp index a40c0475..671c2545 100644 --- a/include/obake/polynomials/packed_monomial.hpp +++ b/include/obake/polynomials/packed_monomial.hpp @@ -24,9 +24,6 @@ #include -#include -#include - #include #include #include @@ -34,6 +31,7 @@ #include #include +#include #include #include #include @@ -799,6 +797,17 @@ inline constexpr bool monomial_hash_is_homomorphic> = true; } // namespace obake +// fmt formatter for packed_monomial, implemented +// on top of the streaming operator. +namespace fmt +{ + +template +struct formatter> : obake::detail::ostream_formatter { +}; + +} // namespace fmt + namespace boost::serialization { diff --git a/src/polynomials/packed_monomial.cpp b/src/polynomials/packed_monomial.cpp index f0927831..df81789f 100644 --- a/src/polynomials/packed_monomial.cpp +++ b/src/polynomials/packed_monomial.cpp @@ -14,12 +14,10 @@ #include #include -#include -#include - #include #include +#include #include #include #include