Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
format.h
1// SPDX-FileCopyrightText: © 2024-2026 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include "utils/traits.h"
7
8#include <fmt/format.h>
9
14template <typename... Args>
15std::string
16format_str (std::string_view format, Args &&... args)
17{
18 return fmt::vformat (format, fmt::make_format_args (args...));
19}
20
21// Generic formatter for smart pointers (std::shared_ptr and std::unique_ptr)
22template <SmartPtr Ptr>
23struct fmt::formatter<Ptr> : fmt::formatter<std::string_view>
24{
25 template <typename FormatContext>
26 auto format (const Ptr &ptr, FormatContext &ctx) const
27 {
28 if (ptr)
29 {
30 return fmt::formatter<std::string_view>::format (
31 fmt::format ("{}", *ptr), ctx);
32 }
33
34 return fmt::formatter<std::string_view>::format ("(null)", ctx);
35 }
36};
37
38// Formatter for std::pair
39template <fmt::formattable T, fmt::formattable U>
40struct fmt::formatter<std::pair<T, U>> : fmt::formatter<std::string_view>
41{
42 auto format (const std::pair<T, U> &pair, format_context &ctx) const
43 -> format_context::iterator
44 {
45 return fmt::formatter<std::string_view>::format (
46 fmt::format ("({}, {})", pair.first, pair.second), ctx);
47 }
48};
49static_assert (fmt::formattable<std::pair<int, std::string>>);