Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for C++20 format API #26

Merged
merged 3 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading