Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
utility_module::enum_formatter< T, Converter > Class Template Reference

A generic formatter for enum types, using a user-provided converter functor. More...

#include <formatter.h>

Collaboration diagram for utility_module::enum_formatter< T, Converter >:
Collaboration graph

Public Member Functions

template<typename ParseContext >
constexpr auto parse (ParseContext &context)
 A no-op parse function required by the formatting library.
 
template<typename FormatContext >
auto format (const T &value, FormatContext &context) const
 Formats the enum value into a provided format context.
 

Static Private Member Functions

template<typename CharT , typename OutputIt >
static auto do_format (OutputIt &out, const T &value)
 Internal helper that dispatches to std::format based on character type.
 

Detailed Description

template<typename T, typename Converter>
class utility_module::enum_formatter< T, Converter >

A generic formatter for enum types, using a user-provided converter functor.

The enum_formatter template allows formatting an enum value by converting it to a string (via a custom Converter functor) and then passing that string to C++20's std::format.

Template Parameters
TThe enum type to be formatted.
ConverterA callable object (e.g., a functor or lambda) that accepts an enum value of type T and returns its string representation.

Example

// 1) Define an enum and a corresponding converter functor:
enum class color { Red, Green, Blue };
struct color_converter {
std::string operator()(color c) const {
switch (c) {
case color::Red: return "Red";
case color::Green: return "Green";
case color::Blue: return "Blue";
}
return "Unknown";
}
};
// 2) Use enum_formatter<Color, color_converter> to format:
// e.g. std::format("Color: {}", color::Green) // -> "Color: Green"

Definition at line 46 of file formatter.h.

Member Function Documentation

◆ do_format()

template<typename T , typename Converter >
template<typename CharT , typename OutputIt >
static auto utility_module::enum_formatter< T, Converter >::do_format ( OutputIt & out,
const T & value )
inlinestaticprivate

Internal helper that dispatches to std::format based on character type.

Template Parameters
CharTThe character type (char or wchar_t).
OutputItThe output iterator type.
Parameters
outAn output iterator to which formatted text is appended.
valueThe enum value to be converted to string and then formatted.
Returns
An iterator pointing to the end of the inserted sequence.

Definition at line 57 of file formatter.h.

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 }

◆ format()

template<typename T , typename Converter >
template<typename FormatContext >
auto utility_module::enum_formatter< T, Converter >::format ( const T & value,
FormatContext & context ) const
inline

Formats the enum value into a provided format context.

Template Parameters
FormatContextThe type of the format context (provided by std::format).
Parameters
valueThe enum value to format.
contextThe format context, which holds the output iterator and additional state.
Returns
An iterator pointing to the end of the formatted output.

Internally calls the helper do_format() to convert the enum to a string using Converter and then writes it.

Definition at line 92 of file formatter.h.

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 }
static auto do_format(auto &out, const T &value)
Internal helper that dispatches to std::format based on character type.
Definition formatter.h:62

References kcenon::thread::utils::enum_formatter< T, Converter >::do_format().

Here is the call graph for this function:

◆ parse()

template<typename T , typename Converter >
template<typename ParseContext >
auto utility_module::enum_formatter< T, Converter >::parse ( ParseContext & context)
inlineconstexpr

A no-op parse function required by the formatting library.

Template Parameters
ParseContextThe parse context type.
Parameters
contextThe format parse context.
Returns
An iterator pointing to the end of context.

The formatter does not accept any custom formatting specifiers, so it simply returns context.begin().

Definition at line 80 of file formatter.h.

80{ return context.begin(); }

The documentation for this class was generated from the following file: