xtd 1.0.0
Loading...
Searching...
No Matches
generic_stream_output.hpp
Go to the documentation of this file.
1
4#pragma once
6#include "any.hpp"
7#include "optional.hpp"
8#include <array>
9#include <deque>
10#include <stdexcept>
11#include <functional>
12#include <forward_list>
13#include <iostream>
14#include <iomanip>
15#include <list>
16#include <map>
17#include <set>
18#include <system_error>
19#include <tuple>
20#include <typeindex>
21#include <type_traits>
22#include <unordered_map>
23#include <unordered_set>
24#include <utility>
25#include <valarray>
26#include <variant>
27#include <vector>
28
31extern std::unordered_map<std::type_index, std::function<std::string(xtd::any const&)>> __any_stringer__;
32
33inline std::ostream& operator <<(std::ostream& os, const std::exception& value) {
34 return os << "exception: " << value.what();
35}
36
37template<typename value_t>
39inline std::ostream& operator <<(std::ostream& os, const std::optional<value_t>& value) {
40 if (!value.has_value()) return os << "(null)";
41 return os << "(" << value.value() << ")";
42}
43
44template<typename value_t>
46inline std::ostream& operator <<(std::ostream& os, const std::optional<value_t>&) {
47 return os << "(unregistered)";
48}
49
50template<typename type1_t, typename type2_t>
52inline std::ostream& operator <<(std::ostream& os, const std::pair<type1_t, type2_t>& value) {
53 return os << "(" << value.first << ", " << value.second << ")";
54}
55
56template<typename type1_t, typename type2_t>
58inline std::ostream& operator <<(std::ostream& os, const std::pair<type1_t, type2_t>& value) {
59 return os << "( , )";
60}
61
62template<typename type_t, unsigned n_t, unsigned last_t>
63requires xtd::stream_insertable<decltype(std::get<n_t>(std::declval<type_t>()))>
64struct __xtd_console_tuple_printer {
65 static void print(std::ostream& os, const type_t& value) {
66 os << std::get<n_t>(value);
67 if constexpr (n_t < last_t) os << ", ";
68 if constexpr (n_t < last_t)
69 __xtd_console_tuple_printer<type_t, n_t + 1, last_t>::print(os, value);
70 }
71};
72
73template<typename ...types_t>
74inline std::ostream& operator <<(std::ostream& os, const std::tuple<types_t ... >& value) {
75 os << "(";
76 __xtd_console_tuple_printer<std::tuple<types_t...>, 0, sizeof...(types_t) - 1>::print(os, value);
77 return os << ")";
78}
79
80template<typename iterator_t>
82inline void __xtd_console_print_container(std::ostream& os, const iterator_t& begin, const iterator_t& end) {
83 for (auto it = begin; it != end; ++it) {
84 os << (it != begin ? ", " : "") << *it;
85 }
86}
87
88template<typename iterator_t>
90inline void __xtd_console_print_container(std::ostream& os, const iterator_t& begin, const iterator_t& end) {
91 for (auto it = begin; it != end; ++it) {
92 os << (it != begin ? ", " : "") << " ";
93 }
94}
95
96template<typename iterator_t>
97inline std::ostream& __xtd_console_print_sequence_container(std::ostream& os, const iterator_t& begin, const iterator_t& end) {
98 os << "[";
99 __xtd_console_print_container(os, begin, end);
100 return os << "]";
101}
102
103template<typename type_t, size_t size_t>
104inline std::ostream& operator <<(std::ostream& os, const std::array<type_t, size_t>& values) {
105 return __xtd_console_print_sequence_container(os, values.begin(), values.end());
106}
107
108template<typename type_t, typename allocator_t = std::allocator < type_t>>
109inline std::ostream& operator <<(std::ostream& os, const std::deque<type_t, allocator_t>& values) {
110 return __xtd_console_print_sequence_container(os, values.begin(), values.end());
111}
112
113template<typename type_t, typename allocator_t = std::allocator<type_t>>
114inline std::ostream& operator <<(std::ostream& os, const std::forward_list<type_t, allocator_t>& values) {
115 return __xtd_console_print_sequence_container(os, values.begin(), values.end());
116}
117
118template<typename type_t>
119inline std::ostream& operator <<(std::ostream& os, const std::initializer_list<type_t>& values) {
120 return __xtd_console_print_sequence_container(os, values.begin(), values.end());
121}
122
123template<typename type_t, typename allocator_t = std::allocator<type_t>>
124inline std::ostream& operator <<(std::ostream& os, const std::list<type_t, allocator_t>& values) {
125 return __xtd_console_print_sequence_container(os, values.begin(), values.end());
126}
127
128template<typename type_t>
129inline std::ostream& operator <<(std::ostream& os, const std::valarray<type_t>& values) {
130 return __xtd_console_print_sequence_container(os, std::begin(values), std::end(values));
131}
132
133template<typename type_t, typename allocator_t = std::allocator<type_t>>
134inline std::ostream& operator <<(std::ostream& os, const std::vector<type_t, allocator_t>& values) {
135 return __xtd_console_print_sequence_container(os, values.begin(), values.end());
136}
137
138template<typename iterator_t>
139inline std::ostream& __xtd_console_print_associative_container(std::ostream& os, const iterator_t& begin, const iterator_t& end) {
140 os << "{";
141 __xtd_console_print_container(os, begin, end);
142 return os << "}";
143}
144
145template<typename key_t, typename value_t, typename compare_t = std::less <key_t>, typename allocator_t = std::allocator<std::pair<const key_t, value_t>>>
146inline std::ostream& operator <<(std::ostream& os, const std::map < key_t, value_t, compare_t, allocator_t >& values) {
147 return __xtd_console_print_associative_container(os, values.begin(), values.end());
148}
149
150template<typename key_t, typename value_t, typename compare_t = std::less <key_t>, typename allocator_t = std::allocator<std::pair<const key_t, value_t>>>
151inline std::ostream& operator <<(std::ostream& os, const std::multimap < key_t, value_t, compare_t, allocator_t >& values) {
152 return __xtd_console_print_associative_container(os, values.begin(), values.end());
153}
154
155template<typename key_t, typename compare_t = std::less <key_t>, typename allocator_t = std::allocator<key_t>>
156inline std::ostream& operator <<(std::ostream& os, const std::multiset < key_t, compare_t, allocator_t >& values) {
157 return __xtd_console_print_associative_container(os, values.begin(), values.end());
158}
159
160template < class key_t, typename compare_t = std::less < key_t>, typename allocator_t = std::allocator<key_t >>
161inline std::ostream& operator <<(std::ostream& os, const std::set < key_t, compare_t, allocator_t >& values) {
162 return __xtd_console_print_associative_container(os, values.begin(), values.end());
163}
164
165template < class key_t, typename value_t, typename pred_t = std::equal_to < key_t>, typename allocator_t = std::allocator<std::pair<const key_t, value_t >>>
166inline std::ostream& operator <<(std::ostream& os, const std::unordered_map < key_t, value_t, pred_t, allocator_t >& values) {
167 return __xtd_console_print_associative_container(os, values.begin(), values.end());
168}
169
170template < class key_t, typename value_t, typename pred_t = std::equal_to < key_t>, typename allocator_t = std::allocator<std::pair<const key_t, value_t >>>
171inline std::ostream& operator <<(std::ostream& os, const std::unordered_multimap < key_t, value_t, pred_t, allocator_t >& values) {
172 return __xtd_console_print_associative_container(os, values.begin(), values.end());
173}
174
175template < class key_t, typename pred_t = std::equal_to < key_t>, typename allocator_t = std::allocator<key_t >>
176std::ostream& operator <<(std::ostream& os, const std::unordered_multiset < key_t, pred_t, allocator_t >& values) {
177 return __xtd_console_print_associative_container(os, values.begin(), values.end());
178}
179
180template < class key_t, typename pred_t = std::equal_to < key_t>, typename allocator_t = std::allocator<key_t >>
181inline std::ostream& operator <<(std::ostream& os, const std::unordered_set < key_t, pred_t, allocator_t >& values) {
182 return __xtd_console_print_associative_container(os, values.begin(), values.end());
183}
184
185inline std::ostream& operator <<(std::ostream& os, const std::error_category& value) {
186 return os << "(" << value.name() << ")";
187}
188
189inline std::ostream& operator <<(std::ostream& os, const std::error_code& value) {
190 return os << "(value = " << value.value() << "category= " << value.category().name() << ")";
191}
192
193namespace xtd {
194 class iformatable;
195 template<typename type_t>
196 class istringable;
197}
198auto operator << (std::ostream& os, const xtd::iformatable& value) -> std::ostream&;
199template<typename type_t>
200auto operator << (std::ostream& os, const xtd::istringable<type_t>& value) noexcept -> std::ostream& {
201 return os << value.to_string();
202}
Contains xtd::any type and std::bad_any_cast exception.
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
Definition stream_insertable.hpp:13
@ value
Represnets the constant operator precedence (42).
Definition operator_precedence.hpp:30
std::any any
Represents the any alias on std::any.
Definition any.hpp:24
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
const_iterator begin() const
Returns an iterator to the beginning.
Definition read_only_span.hpp:183
const_iterator end() const
Returns an iterator to the end.
Definition read_only_span.hpp:213
void print(FILE *file, arg_t &&value)
Writes the text representation of the specified value to the file output stream.
Definition print.hpp:19
Contains xtd::optional type.
Contains xtd::stream_insertable concept.