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
5#pragma once
6
7#include <format>
8#include <string>
9#include <string_view>
10#include <type_traits>
11
12namespace utility_module
13{
46 template <typename T, typename Converter> class enum_formatter
47 {
48 private:
57 template <typename CharT, typename OutputIt> static auto do_format(OutputIt& out, const T& value)
58 {
59 if constexpr (std::is_same_v<CharT, wchar_t>)
60 {
61 return std::format_to(out, L"{}", Converter{}(value));
62 }
63 else
64 {
65 return std::format_to(out, "{}", Converter{}(value));
66 }
67 }
68
69 public:
79 template <typename ParseContext>
80 constexpr auto parse(ParseContext& context) { return context.begin(); }
81
92 template <typename FormatContext> auto format(const T& value, FormatContext& context) const
93 {
94 using char_type =
95 typename std::iterator_traits<typename FormatContext::iterator>::value_type;
96 return do_format<char_type>(context.out(), value);
97 }
98 };
99
119 {
120 public:
128 template <typename... FormatArgs>
129 static auto format(const char* formats, const FormatArgs&... args) -> std::string
130 {
131 return std::vformat(formats, std::make_format_args(args...));
132 }
133
141 template <typename... FormatArgs>
142 static auto format(const wchar_t* formats, const FormatArgs&... args) -> std::wstring
143 {
144 return std::vformat(formats, std::make_wformat_args(args...));
145 }
146
158 template <typename OutputIt, typename... FormatArgs>
159 static auto format_to(OutputIt out, const char* formats, const FormatArgs&... args)
160 -> OutputIt
161 {
162 return std::vformat_to(out, formats, std::make_format_args(args...));
163 }
164
174 template <typename OutputIt, typename... FormatArgs>
175 static auto format_to(OutputIt out, const wchar_t* formats, const FormatArgs&... args)
176 -> OutputIt
177 {
178 return std::vformat_to(out, formats, std::make_wformat_args(args...));
179 }
180 };
181} // namespace utility_module
static auto do_format(auto &out, const T &value)
Internal helper that dispatches to std::format based on character type.
Definition formatter.h:62
A generic formatter for enum types, using a user-provided converter functor.
Definition formatter.h:47
static auto do_format(OutputIt &out, const T &value)
Internal helper that dispatches to std::format based on character type.
Definition formatter.h:57
constexpr auto parse(ParseContext &context)
A no-op parse function required by the formatting library.
Definition formatter.h:80
auto format(const T &value, FormatContext &context) const
Formats the enum value into a provided format context.
Definition formatter.h:92
Provides convenience methods for string formatting using C++20 <format>.
Definition formatter.h:119
static auto format(const wchar_t *formats, const FormatArgs &... args) -> std::wstring
Formats a wide-character string with the given arguments.
Definition formatter.h:142
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:159
static auto format(const char *formats, const FormatArgs &... args) -> std::string
Formats a narrow-character string with the given arguments.
Definition formatter.h:129
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:175