xtd 1.0.0
Loading...
Searching...
No Matches
__xtd_std_format.hpp
Go to the documentation of this file.
1
4#pragma once
6#if !defined(__XTD_CORE_INTERNAL__)
7#error "Do not include this file: Internal use only"
8#endif
10
11# include "../raw_type.hpp"
12# include "../string.hpp"
13# include "../textual.hpp"
14
16#include <version>
17#if (defined(__cpp_lib_format) && defined(_MSC_VER) && _MSC_VER >= 1932) || (defined(__apple_build_version__) && __apple_build_version__ >= 15000300) || (!defined(__APPLE__) && defined(__GNUC__) && __GNUC__ >= 13) || (!defined(__APPLE__) && defined(__clang_major__) && __clang_major__ >= 15)
18# include <format>
19template <class type_t>
20requires (std::derived_from<xtd::raw_type<type_t>, xtd::object> ||
21 std::derived_from<xtd::raw_type<type_t>, xtd::istringable<xtd::raw_type<type_t>>> ||
22 std::derived_from<xtd::raw_type<type_t>, xtd::iformatable> ||
23 requires (const xtd::raw_type<type_t>& value) {{value.to_string()} -> xtd::textual;} ||
24 requires (const xtd::raw_type<type_t>& value, const xtd::string& fmt) {{value.to_string(fmt)} -> xtd::textual;} ||
25 std::derived_from<xtd::raw_type<type_t>, std::exception> ||
26 std::is_enum_v<xtd::raw_type<type_t>>
27 )
28struct std::formatter<type_t> : std::formatter<std::string> {
29 template<typename format_context_t>
30 constexpr auto parse(format_context_t& ctx) {
31 auto iterator = ctx.begin();
32 for (; iterator != ctx.end() && *iterator != '}'; ++iterator)
33 format_value += *iterator;
34 return iterator;
35 }
36
37 template<typename object_t, typename format_context_t>
38 requires (requires (const xtd::raw_type<object_t>& value, const xtd::string& fmt) {{value.to_string(fmt)} -> xtd::textual;})
39 constexpr auto format(const object_t& obj, format_context_t& ctx) const {
40 return std::format_to(ctx.out(), "{}", obj.to_string(format_value).c_str());
41 }
42
43 template<typename object_t, typename format_context_t>
44 requires (!requires (const xtd::raw_type<object_t>& value, const xtd::string& fmt) {{value.to_string(fmt)} -> xtd::textual;} && requires (const xtd::raw_type<object_t>& value) {{value.to_string()} -> xtd::textual;})
45 constexpr auto format(const object_t& obj, format_context_t& ctx) const {
46 return std::format_to(ctx.out(), "{}", obj.to_string().c_str());
47 }
48
49private:
50 std::string format_value;
51};
52#endif
Provides functionality to format the value of an object into a string representation.
Definition iformatable.hpp:42
Provides a way to represent the current object as a string.
Definition istringable.hpp:26
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:45
Definition textual.hpp:16
@ value
Represnets the constant operator precedence (42).
Definition operator_precedence.hpp:30
auto format(const xtd::string &fmt, args_t &&... args) -> xtd::string
Writes the text representation of the specified arguments list, to string using the specified format ...
Definition format.hpp:21
xtd::basic_string< char > string
Represents text as a sequence of UTF-8 code units.
Definition __string_definitions.hpp:43
std::remove_cvref_t< value_t > raw_type
Represents a raw type alias equivalent to std::remove_cvref_t<value_t>.
Definition raw_type.hpp:25
auto parse(const std::string &str) -> value_t
Convert a string into a type.
Definition parse.hpp:34
const xtd::collections::generic::helpers::wrap_pointer_iterator< pointer > iterator
Represents the iterator of read_only_span value type.
Definition read_only_span.hpp:70
Contains xtd::raw_type alias.
Contains xtd::string alias.
Contains xtd::textual concept.