3 #include "fly/concepts/concepts.hpp"
4 #include "fly/types/string/concepts.hpp"
5 #include "fly/types/string/detail/classifier.hpp"
6 #include "fly/types/string/detail/format_parse_context.hpp"
7 #include "fly/types/string/detail/format_specifier.hpp"
8 #include "fly/types/string/formatters.hpp"
12 #include <string_view>
13 #include <type_traits>
15 namespace fly::detail {
27 template <
typename FormatContext>
35 FormatContext &context,
43 template <
typename FormatContext>
52 FormatContext &context,
59 template <
typename FormatContext>
64 const void *m_pointer;
65 std::int64_t m_signed_int;
66 std::uint64_t m_unsigned_int;
69 long double m_long_double;
75 FormatContext &context,
90 template <
typename FormatContext,
typename T>
91 void format_user_defined_value(
94 FormatContext &context,
108 template <
typename FormatContext,
typename T>
109 void format_string_value(
112 FormatContext &context,
125 template <
typename FormatContext,
typename T>
126 void format_standard_value(
128 FormatContext &context,
137 template <
typename FormatContext>
140 using char_type =
typename FormatContext::char_type;
155 template <fly::FormattableUserDefined T>
166 template <fly::FormattableString T>
176 template <fly::FormattablePo
inter T>
186 template <fly::FormattableIntegral T>
196 template <fly::FormattableFloatingPo
int T>
206 template <fly::FormattableBoolean T>
218 FormatContext &context,
228 template <
typename Visitor>
229 constexpr
auto visit(Visitor &&visitor)
const;
234 explicit operator bool() const noexcept;
237 enum class Type : std::uint8_t
269 template <
typename FormatContext,
typename... ParameterTypes>
283 friend FormatContext;
285 const std::array<
FormatParameter,
sizeof...(ParameterTypes)> m_parameters;
296 template <
typename FormatContext, fly::Formattable<FormatContext>... ParameterTypes>
297 constexpr
auto make_format_parameters(ParameterTypes &&...parameters)
300 std::forward<ParameterTypes>(parameters)...};
304 template <
typename FormatContext,
typename T>
305 inline void format_user_defined_value(
307 BasicFormatParseContext<typename FormatContext::char_type> &parse_context,
308 FormatContext &context,
309 BasicFormatSpecifier<typename FormatContext::char_type> &&specifier)
311 using Formatter =
typename FormatContext::template formatter_type<T>;
314 if constexpr (fly::FormattableWithParsing<decltype(parse_context), Formatter>)
316 bool formatter_requires_parsing =
true;
318 if constexpr (fly::DerivedFrom<Formatter, decltype(specifier)>)
320 formatter_requires_parsing = !specifier.m_was_parsed_as_standard_formatter;
321 specifier.copy_formatting_options_into(formatter);
324 if (formatter_requires_parsing)
326 parse_context.lexer().set_position(specifier.m_parse_index);
327 formatter.parse(parse_context);
331 formatter.format(*
static_cast<const T *
>(value), context);
335 template <
typename FormatContext,
typename T>
336 inline void format_string_value(
339 FormatContext &context,
340 BasicFormatSpecifier<typename FormatContext::char_type> &&specifier)
342 using view_type = std::basic_string_view<T>;
344 typename FormatContext::template formatter_type<view_type> formatter(std::move(specifier));
346 view_type view(
static_cast<const T *
>(value), size);
347 formatter.format(view, context);
351 template <
typename FormatContext,
typename T>
352 inline void format_standard_value(
353 StandardValue<FormatContext> value,
354 FormatContext &context,
355 BasicFormatSpecifier<typename FormatContext::char_type> &&specifier)
357 typename FormatContext::template formatter_type<T> formatter(std::move(specifier));
359 if constexpr (fly::FormattablePointer<T>)
361 if constexpr (std::is_null_pointer_v<T>)
363 formatter.format(
nullptr, context);
365 else if constexpr (std::is_const_v<T>)
367 formatter.format(
static_cast<T
>(value.m_pointer), context);
371 formatter.format(
static_cast<T
>(
const_cast<void *
>(value.m_pointer)), context);
374 else if constexpr (fly::SameAs<T, float>)
376 formatter.format(value.m_float, context);
378 else if constexpr (fly::SameAs<T, double>)
380 formatter.format(value.m_double, context);
382 else if constexpr (fly::SameAs<T, long double>)
384 formatter.format(value.m_long_double, context);
386 else if constexpr (fly::SameAs<T, bool>)
388 formatter.format(value.m_bool, context);
390 else if constexpr (std::is_signed_v<T>)
392 formatter.format(
static_cast<T
>(value.m_signed_int), context);
396 formatter.format(
static_cast<T
>(value.m_unsigned_int), context);
401 template <
typename FormatContext>
403 m_type(Type::Invalid),
404 m_value {.m_monostate {}}
409 template <
typename FormatContext>
410 template <fly::FormattableUserDefined T>
412 m_type(Type::UserDefined),
413 m_value {.m_user_defined {&value, format_user_defined_value<FormatContext, T>}}
418 template <
typename FormatContext>
419 template <fly::FormattableString T>
423 using U = std::remove_cvref_t<T>;
425 using standard_character_type = fly::StandardCharacterType<T>;
426 using standard_view_type = std::basic_string_view<standard_character_type>;
428 standard_view_type view;
430 if constexpr (std::is_array_v<U> || std::is_pointer_v<U>)
436 view = standard_view_type(value);
440 static_cast<const void *
>(view.data()),
442 format_string_value<FormatContext, standard_character_type>};
446 template <
typename FormatContext>
447 template <fly::FormattablePo
inter T>
449 m_type(Type::Pointer),
450 m_value {.m_standard {.m_pointer = value, .m_format {format_standard_value<FormatContext, T>}}}
455 template <
typename FormatContext>
456 template <fly::FormattableIntegral T>
459 m_value.m_standard.m_format = format_standard_value<FormatContext, T>;
461 if constexpr (std::is_signed_v<T>)
463 m_type = Type::SignedInt;
464 m_value.m_standard.m_signed_int = value;
468 m_type = Type::UnsignedInt;
469 m_value.m_standard.m_unsigned_int = value;
474 template <
typename FormatContext>
475 template <fly::FormattableFloatingPo
int T>
478 m_value.m_standard.m_format = format_standard_value<FormatContext, T>;
480 if constexpr (std::is_same_v<T, float>)
482 m_type = Type::Float;
483 m_value.m_standard.m_float = value;
485 else if constexpr (std::is_same_v<T, double>)
487 m_type = Type::Double;
488 m_value.m_standard.m_double = value;
490 else if constexpr (std::is_same_v<T, long double>)
492 m_type = Type::LongDouble;
493 m_value.m_standard.m_long_double = value;
498 template <
typename FormatContext>
499 template <fly::FormattableBoolean T>
502 m_value {.m_standard {.m_bool = value, .m_format {format_standard_value<FormatContext, T>}}}
507 template <
typename FormatContext>
510 FormatContext &context,
515 case Type::UserDefined:
516 m_value.m_user_defined.m_format(
517 m_value.m_user_defined.m_value,
520 std::move(specifier));
523 m_value.m_string.m_format(
524 m_value.m_string.m_value,
525 m_value.m_string.m_size,
527 std::move(specifier));
530 case Type::SignedInt:
531 case Type::UnsignedInt:
534 case Type::LongDouble:
536 m_value.m_standard.m_format(m_value.m_standard, context, std::move(specifier));
544 template <
typename FormatContext>
545 template <
typename Visitor>
550 case Type::UserDefined:
551 return visitor(m_value.m_user_defined);
553 return visitor(m_value.m_string);
555 return visitor(m_value.m_standard.m_pointer);
556 case Type::SignedInt:
557 return visitor(m_value.m_standard.m_signed_int);
558 case Type::UnsignedInt:
559 return visitor(m_value.m_standard.m_unsigned_int);
561 return visitor(m_value.m_standard.m_float);
563 return visitor(m_value.m_standard.m_double);
564 case Type::LongDouble:
565 return visitor(m_value.m_standard.m_long_double);
567 return visitor(m_value.m_standard.m_bool);
569 return visitor(m_value.m_monostate);
574 template <
typename FormatContext>
577 return m_type != Type::Invalid;
581 template <
typename FormatContext,
typename... ParameterTypes>
583 ParameterTypes &&...parameters) noexcept :
584 m_parameters {
FormatParameter {std::forward<ParameterTypes>(parameters)}...}
Definition: string.hpp:51
static constexpr size_type size(T &&value)
Definition: format_parse_context.hpp:22
Definition: format_parameters.hpp:21
Definition: format_parameters.hpp:61
Definition: format_parameters.hpp:45
Definition: format_parameters.hpp:29