Photon 1.0.0
Loading...
Searching...
No Matches
std.h
Go to the documentation of this file.
1// Formatting library for C++ - formatters for standard library types
2//
3// Copyright (c) 2012 - present, Victor Zverovich
4// All rights reserved.
5//
6// For the license information refer to format.h.
7
8#ifndef FMT_STD_H_
9#define FMT_STD_H_
10
11#include <thread>
12#include <type_traits>
13#include <utility>
14
15#include "ostream.h"
16
17#if FMT_HAS_INCLUDE(<version>)
18#include <version>
19#endif
20// Checking FMT_CPLUSPLUS for warning suppression in MSVC.
21#if FMT_CPLUSPLUS >= 201703L
22#if FMT_HAS_INCLUDE(<filesystem>)
23#include <filesystem>
24#endif
25#if FMT_HAS_INCLUDE(<variant>)
26#include <variant>
27#endif
28#endif
29
30#ifdef __cpp_lib_filesystem
32
33namespace detail
34{
35
36 template <typename Char>
37 void write_escaped_path(basic_memory_buffer<Char>& quoted,
38 const std::filesystem::path& p)
39 {
40 write_escaped_string<Char>(std::back_inserter(quoted), p.string<Char>());
41 }
42#ifdef _WIN32
43 template <>
44 inline void write_escaped_path<char>(basic_memory_buffer<char>& quoted,
45 const std::filesystem::path& p)
46 {
47 auto s = p.u8string();
48 write_escaped_string<char>(
49 std::back_inserter(quoted),
50 string_view(reinterpret_cast<const char*>(s.c_str()), s.size()));
51 }
52#endif
53 template <>
54 inline void write_escaped_path<std::filesystem::path::value_type>(
56 const std::filesystem::path& p)
57 {
58 write_escaped_string<std::filesystem::path::value_type>(
59 std::back_inserter(quoted), p.native());
60 }
61
62} // namespace detail
63
64template <typename Char>
65struct formatter<std::filesystem::path, Char>
66 : formatter<basic_string_view<Char>>
67{
68 template <typename FormatContext>
69 auto format(const std::filesystem::path& p, FormatContext& ctx) const ->
70 typename FormatContext::iterator
71 {
73 detail::write_escaped_path(quoted, p);
75 basic_string_view<Char>(quoted.data(), quoted.size()), ctx);
76 }
77};
79#endif
80
82template <typename Char>
83struct formatter<std::thread::id, Char> : basic_ostream_formatter<Char>
84{
85};
87
88#ifdef __cpp_lib_variant
90template <typename Char>
91struct formatter<std::monostate, Char>
92{
93 template <typename ParseContext>
94 FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin())
95 {
96 return ctx.begin();
97 }
98
99 template <typename FormatContext>
100 auto format(const std::monostate&, FormatContext& ctx) const
101 -> decltype(ctx.out())
102 {
103 auto out = ctx.out();
104 out = detail::write<Char>(out, "monostate");
105 return out;
106 }
107};
108
109namespace detail
110{
111
112 template <typename T>
113 using variant_index_sequence =
114 std::make_index_sequence<std::variant_size<T>::value>;
115
116 // variant_size and variant_alternative check.
117 template <typename T, typename U = void>
118 struct is_variant_like_ : std::false_type
119 {
120 };
121 template <typename T>
122 struct is_variant_like_<T, std::void_t<decltype(std::variant_size<T>::value)>>
123 : std::true_type
124 {
125 };
126
127 // formattable element check
128 template <typename T, typename C>
129 class is_variant_formattable_
130 {
131 template <std::size_t... I>
132 static std::conjunction<
134 check(std::index_sequence<I...>);
135
136 public:
137 static constexpr const bool value =
138 decltype(check(variant_index_sequence<T>{}))::value;
139 };
140
141 template <typename Char, typename OutputIt, typename T>
142 auto write_variant_alternative(OutputIt out, const T& v) -> OutputIt
143 {
144 if constexpr (is_string<T>::value)
145 return write_escaped_string<Char>(out, detail::to_string_view(v));
146 else if constexpr (std::is_same_v<T, Char>)
147 return write_escaped_char(out, v);
148 else
149 return write<Char>(out, v);
150 }
151
152} // namespace detail
153
154template <typename T>
155struct is_variant_like
156{
157 static constexpr const bool value = detail::is_variant_like_<T>::value;
158};
159
160template <typename T, typename C>
161struct is_variant_formattable
162{
163 static constexpr const bool value =
164 detail::is_variant_formattable_<T, C>::value;
165};
166
167template <typename Variant, typename Char>
168struct formatter<
169 Variant,
170 Char,
171 std::enable_if_t<std::conjunction_v<
172 is_variant_like<Variant>,
173 is_variant_formattable<Variant, Char>>>>
174{
175 template <typename ParseContext>
176 FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin())
177 {
178 return ctx.begin();
179 }
180
181 template <typename FormatContext>
182 auto format(const Variant& value, FormatContext& ctx) const
183 -> decltype(ctx.out())
184 {
185 auto out = ctx.out();
186
187 out = detail::write<Char>(out, "variant(");
188 std::visit(
189 [&](const auto& v) {
190 out = detail::write_variant_alternative<Char>(out, v);
191 },
192 value);
193 *out++ = ')';
194 return out;
195 }
196};
198#endif
199
200#endif // FMT_STD_H_
Definition format.h:955
Definition core.h:483
Definition core.h:1598
typename std::enable_if< B, T >::type enable_if_t
Definition core.h:302
FMT_NODISCARD FMT_INLINE auto format(format_string< T... > fmt, T &&... args) -> std::string
Definition core.h:4090
basic_string_view< char > string_view
Definition core.h:604
#define FMT_CONSTEXPR
Definition core.h:106
bool_constant< !std::is_base_of< detail::unformattable, decltype(detail::arg_mapper< buffer_context< Char > >().map(std::declval< T >()))>::value &&!detail::has_fallback_formatter< T, Char >::value > is_formattable
Definition core.h:2427
#define FMT_BEGIN_NAMESPACE
Definition core.h:214
void void_t
Definition core.h:2201
#define FMT_END_NAMESPACE
Definition core.h:219
#define out
Definition encodings.cpp:5
auto write_escaped_char(OutputIt out, Char v) -> OutputIt
Definition format.h:2242
Definition args.h:20
Definition uuid.h:926
Definition ostream.h:166
Definition core.h:944
Definition core.h:666
Definition core.h:352
s
Definition tag_strings.h:47
p
Definition tag_strings.h:29