Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
formatter.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
11#pragma once
12
13#include <format>
14#include <string>
15#include <string_view>
16#include <type_traits>
17
19{
52 template <typename T, typename Converter> class enum_formatter
53 {
54 private:
62 template <typename CharT> static auto do_format(auto& out, const T& value)
63 {
64 if constexpr (std::is_same_v<CharT, wchar_t>)
65 {
66 return std::format_to(out, L"{}", Converter{}(value));
67 }
68 else
69 {
70 return std::format_to(out, "{}", Converter{}(value));
71 }
72 }
73
74 public:
83 constexpr auto parse(auto& context) { return context.begin(); }
84
95 template <typename FormatContext> auto format(const T& value, FormatContext& context) const
96 {
97 using char_type =
98 typename std::iterator_traits<typename FormatContext::iterator>::value_type;
99 return do_format<char_type>(context.out(), value);
100 }
101 };
102
122 {
123 public:
131 template <typename... FormatArgs>
132 static auto format(const char* formats, const FormatArgs&... args) -> std::string
133 {
134 return std::vformat(formats, std::make_format_args(args...));
135 }
136
144 template <typename... FormatArgs>
145 static auto format(const wchar_t* formats, const FormatArgs&... args) -> std::wstring
146 {
147 return std::vformat(formats, std::make_wformat_args(args...));
148 }
149
161 template <typename OutputIt, typename... FormatArgs>
162 static auto format_to(OutputIt out, const char* formats, const FormatArgs&... args)
163 -> OutputIt
164 {
165 return std::vformat_to(out, formats, std::make_format_args(args...));
166 }
167
177 template <typename OutputIt, typename... FormatArgs>
178 static auto format_to(OutputIt out, const wchar_t* formats, const FormatArgs&... args)
179 -> OutputIt
180 {
181 return std::vformat_to(out, formats, std::make_wformat_args(args...));
182 }
183 };
184} // namespace kcenon::thread::utils
185
186// Backward compatibility: provide aliases in utility_module namespace
187namespace utility_module
188{
190 template <typename T, typename Converter>
192} // namespace utility_module
A generic formatter for enum types, using a user-provided converter functor.
Definition formatter.h:53
constexpr auto parse(auto &context)
A no-op parse function required by the formatting library.
Definition formatter.h:83
static auto do_format(auto &out, const T &value)
Internal helper that dispatches to std::format based on character type.
Definition formatter.h:62
auto format(const T &value, FormatContext &context) const
Formats the enum value into a provided format context.
Definition formatter.h:95
Provides convenience methods for string formatting using C++20 <format>.
Definition formatter.h:122
static auto format(const wchar_t *formats, const FormatArgs &... args) -> std::wstring
Formats a wide-character string with the given arguments.
Definition formatter.h:145
static auto format_to(OutputIt out, const wchar_t *formats, const FormatArgs &... args) -> OutputIt
Formats a wide-character string directly to an output iterator.
Definition formatter.h:178
static auto format_to(OutputIt out, const char *formats, const FormatArgs &... args) -> OutputIt
Formats a narrow-character string directly to an output iterator.
Definition formatter.h:162
static auto format(const char *formats, const FormatArgs &... args) -> std::string
Formats a narrow-character string with the given arguments.
Definition formatter.h:132
kcenon::thread::utils::formatter formatter
Definition formatter.h:189