Skip to content

Commit

Permalink
Merge pull request #26 from contour-terminal/maintenance/stdlib
Browse files Browse the repository at this point in the history
Add support for C++20 format API
  • Loading branch information
christianparpart authored Sep 25, 2024
2 parents 17ac598 + a6d1684 commit 511607a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-22.04, ubuntu-20.04]
os: [ubuntu-24.04, ubuntu-22.04]
cxx: [20]
build_type: ["RelWithDebInfo"]
llvm_version:
Expand All @@ -56,8 +56,6 @@ jobs:
# max-size: 256M
- name: "update APT database"
run: sudo apt -q update


- name: Install clang
run: |
wget https://apt.llvm.org/llvm.sh
Expand All @@ -78,6 +76,5 @@ jobs:
-DCMAKE_BUILD_TYPE=${{matrix.build_type}}
- name: "build"
run: cmake --build build --parallel 3

- name: "run test"
run: ./build/test-boxed-cpp
21 changes: 16 additions & 5 deletions include/boxed-cpp/boxed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,19 +207,30 @@ struct hash<boxed::detail::boxed<T, U>>
} // namespace std
// {{{ fmtlib integration
// clang-format off
#if __has_include(<fmt/format.h>)
#if __has_include(<format>)
#include <format>
// clang-format on

template <typename Type, typename Tag>
struct std::formatter<boxed::detail::boxed<Type, Tag>>: std::formatter<Type>
{
auto format(boxed::detail::boxed<Type, Tag> const& val, auto& ctx) const
{
return std::formatter<Type>::format(val.value, ctx);
}
};
#elif __has_include(<fmt/format.h>)

// clang-format off
#include <fmt/format.h>
// clang-format on

template <typename Type, typename Tag>
struct fmt::formatter<boxed::detail::boxed<Type, Tag>>
struct fmt::formatter<boxed::detail::boxed<Type, Tag>>: fmt::formatter<Type>
{
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }

auto format(boxed::detail::boxed<Type, Tag> const& val, fmt::format_context& ctx) const
{
return fmt::format_to(ctx.out(), "{}", val.value);
return fmt::formatter<Type>::format(val.value, ctx);
}
};

Expand Down
23 changes: 22 additions & 1 deletion test-boxed-cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ struct Wrap
{
overload<Ts...> func_wrap;

Wrap(Ts... funcs): func_wrap(funcs...) {}
Wrap(Ts... funcs): func_wrap { funcs... } {}

template <typename... Args>
auto operator()(Args... args)
Expand All @@ -215,3 +215,24 @@ TEST_CASE("advanced")
REQUIRE(x_coord(rho, theta, phi) == x_coord(theta, phi, rho));
REQUIRE(x_coord(phi, theta, rho) == x_coord(phi, theta, rho));
}

#ifdef __has_include
#if __has_include(<version>)
#include <version>
#endif
#endif

#if defined(__cpp_lib_format) // && __cpp_lib_format >= 202207LL
TEST_CASE("formatter")
{
auto constexpr l = Length { 3 };
auto constexpr f = From { 2 };
auto constexpr t = To { 4 };
auto constexpr bd = BoxedDouble { 3.14 };

REQUIRE(std::format("{}", l) == "3");
REQUIRE(std::format("{}", f) == "2");
REQUIRE(std::format("{}", t) == "4");
REQUIRE(std::format("{}", bd) == "3.14");
}
#endif

0 comments on commit 511607a

Please sign in to comment.