Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
formatter_macros.h File Reference

Provides macros for generating std::formatter specializations. More...

#include "formatter.h"
#include "convert_string.h"
#include <format>
Include dependency graph for formatter_macros.h:

Go to the source code of this file.

Macros

#define DECLARE_FORMATTER(CLASS_NAME)
 Generates std::formatter specializations for narrow and wide characters.
 

Detailed Description

Provides macros for generating std::formatter specializations.

This file contains macros that generate std::formatter specializations for custom types to work with C++20 std::format. The macros eliminate code duplication by generating the necessary boilerplate code for formatter specializations.

Usage example:

// In your header file after class definition:
DECLARE_FORMATTER(my_namespace::my_class)
#define DECLARE_FORMATTER(CLASS_NAME)
Generates std::formatter specializations for narrow and wide characters.
Note
This project requires C++20 std::format support. The fmt library fallback has been removed as of issue #219.

Definition in file formatter_macros.h.

Macro Definition Documentation

◆ DECLARE_FORMATTER

#define DECLARE_FORMATTER ( CLASS_NAME)
Value:
template <> struct std::formatter<CLASS_NAME> : std::formatter<std::string_view> \
{ \
template <typename FormatContext> \
auto format(const CLASS_NAME& item, FormatContext& ctx) const \
{ \
return std::formatter<std::string_view>::format(item.to_string(), ctx); \
} \
}; \
\
template <> \
struct std::formatter<CLASS_NAME, wchar_t> : std::formatter<std::wstring_view, wchar_t> \
{ \
template <typename FormatContext> \
auto format(const CLASS_NAME& item, FormatContext& ctx) const \
{ \
auto str = item.to_string(); \
return std::formatter<std::wstring_view, wchar_t>::format(wstr, ctx); \
} \
};
static auto to_wstring(const std::string &value) -> std::tuple< std::optional< std::wstring >, std::optional< std::string > >
Converts a std::string (system-encoded) to a std::wstring.

Generates std::formatter specializations for narrow and wide characters.

This macro creates two formatter specializations:

  1. std::formatter<CLASS_NAME> for narrow character formatting
  2. std::formatter<CLASS_NAME, wchar_t> for wide character formatting

Requirements:

  • The class must have a to_string() method that returns std::string
  • The convert_string::to_wstring function must be available
Parameters
CLASS_NAMEThe fully qualified class name (including namespace if any)

Definition at line 43 of file formatter_macros.h.

43#define DECLARE_FORMATTER(CLASS_NAME) \
44template <> struct std::formatter<CLASS_NAME> : std::formatter<std::string_view> \
45{ \
46 template <typename FormatContext> \
47 auto format(const CLASS_NAME& item, FormatContext& ctx) const \
48 { \
49 return std::formatter<std::string_view>::format(item.to_string(), ctx); \
50 } \
51}; \
52 \
53template <> \
54struct std::formatter<CLASS_NAME, wchar_t> : std::formatter<std::wstring_view, wchar_t> \
55{ \
56 template <typename FormatContext> \
57 auto format(const CLASS_NAME& item, FormatContext& ctx) const \
58 { \
59 auto str = item.to_string(); \
60 auto wstr = utility_module::convert_string::to_wstring(str); \
61 return std::formatter<std::wstring_view, wchar_t>::format(wstr, ctx); \
62 } \
63};