28#include <unordered_map>
29#include <unordered_set>
37 { t.operator->() } -> std::same_as<typename T::element_type*>;
38 { *t } -> std::convertible_to<typename T::element_type&>;
39 {
static_cast<bool>(t) } -> std::same_as<bool>;
40 typename T::element_type;
45std::string FormatItem(
const T& item) {
48 return std::format(
"{}", *item);
53 return std::format(
"{}", item);
59template <std::ranges::input_range Range>
60std::string FormatRange(
const Range& range, std::string_view separator,
61 std::string_view prefix, std::string_view suffix) {
62 if (std::ranges::empty(range)) {
63 return std::format(
"{}{}", prefix, suffix);
70 for (
const auto& element : range) {
74 ss << std::format(
"{}", element);
83template <
typename MapType>
84std::string FormatMap(
const MapType& map) {
86 std::ranges::transform_view formatted_range =
87 map | std::views::transform([](
const auto& pair) -> std::string {
88 const auto& [key, value] = pair;
89 return std::format(
"{}: {}", FormatItem(key), FormatItem(value));
91 return FormatRange(formatted_range,
", ",
"{",
"}");
95template <
typename K,
typename V>
96struct std::formatter<
std::map<K, V>> : std::formatter<std::string_view> {
97 template <
class FormatContext>
98 auto format(
const std::map<K, V>& map, FormatContext& ctx)
const {
99 return std::formatter<std::string_view>::format(FormatMap(map), ctx);
104template <
typename K,
typename V>
105struct std::formatter<
std::unordered_map<K, V>> : std::formatter<std::string_view> {
106 template <
class FormatContext>
107 auto format(
const std::unordered_map<K, V>& map, FormatContext& ctx)
const {
108 return std::formatter<std::string_view>::format(FormatMap(map), ctx);
114struct std::formatter<
std::vector<T>> : std::formatter<std::string_view> {
115 template <
class FormatContext>
116 auto format(
const std::vector<T>& vec, FormatContext& ctx)
const {
117 auto formatted_range =
118 vec | std::views::transform([](
const auto& item) {
return FormatItem(item); });
119 return std::formatter<std::string_view>::format(
120 FormatRange(formatted_range,
", ",
"[",
"]"), ctx);
125template <
typename T,
size_t Extent>
126struct std::formatter<
std::span<T, Extent>> : std::formatter<std::string_view> {
127 template <
class FormatContext>
128 auto format(
const std::span<T, Extent>& span, FormatContext& ctx)
const {
129 auto formatted_range =
130 span | std::views::transform([](
const auto& item) {
return FormatItem(item); });
131 return std::formatter<std::string_view>::format(
132 FormatRange(formatted_range,
", ",
"[",
"]"), ctx);
137template <
typename T,
typename Hash,
typename KeyEqual,
typename Allocator>
138struct std::formatter<
std::unordered_set<T, Hash, KeyEqual, Allocator>>
139 : std::formatter<std::string_view> {
140 template <
class FormatContext>
141 auto format(
const std::unordered_set<T, Hash, KeyEqual, Allocator>& set,
142 FormatContext& ctx)
const {
143 auto formatted_range =
144 set | std::views::transform([](
const auto& item) {
return FormatItem(item); });
145 return std::formatter<std::string_view>::format(
146 FormatRange(formatted_range,
", ",
"[",
"]"), ctx);
Concept for smart pointer types.
Definition formatter_internal.h:36
std::shared_ptr< MapType > map(SchemaField key, SchemaField value)
Create a MapType with the given key and value fields.
Definition type.cc:388