xtd 0.2.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
30extern std::unordered_map<std::type_index, std::function<std::string(xtd::any const&)>> __any_stringer__;
31
32inline std::ostream& operator <<(std::ostream& os, const std::exception& value) {
33 return os << "exception: " << value.what();
34}
35
36template<class value_t>
38inline std::ostream& operator <<(std::ostream& os, const std::optional<value_t>& value) {
39 if (!value.has_value()) return os << "(null)";
40 return os << "(" << value.value() << ")";
41}
42
43template<class value_t>
45inline std::ostream& operator <<(std::ostream& os, const std::optional<value_t>&) {
46 return os << "(unregistered)";
47}
48
49template<class type1_t, class type2_t>
51inline std::ostream& operator <<(std::ostream& os, const std::pair<type1_t, type2_t>& value) {
52 return os << "(" << value.first << ", " << value.second << ")";
53}
54
55template<class type1_t, class type2_t>
57inline std::ostream& operator <<(std::ostream& os, const std::pair<type1_t, type2_t>& value) {
58 return os << "( , )";
59}
60
61template<class type_t, unsigned n_t, unsigned last_t>
62requires xtd::stream_insertable<decltype(std::get<n_t>(std::declval<type_t>()))>
63struct __xtd_console_tuple_printer {
64 static void print(std::ostream& os, const type_t& value) {
65 os << std::get<n_t>(value);
66 if constexpr (n_t < last_t) os << ", ";
67 if constexpr (n_t < last_t)
68 __xtd_console_tuple_printer<type_t, n_t + 1, last_t>::print(os, value);
69 }
70};
71
72template<class ...types_t>
73inline std::ostream& operator <<(std::ostream& os, const std::tuple<types_t ... >& value) {
74 os << "(";
75 __xtd_console_tuple_printer<std::tuple<types_t...>, 0, sizeof...(types_t) - 1>::print(os, value);
76 return os << ")";
77}
78
79/*
80template<class ...args_t>
81inline std::ostream& operator <<(std::ostream& os, const std::variant<args_t ...>& value) {
82 std::visit([&](auto && t){
83 os << t;
84 }, value);
85 return os;
86}*/
87
88template<class iterator_t>
89//requires xtd::stream_insertable<std::iter_value_t<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 ? ", " : "") << *it;
93 }
94}
95
96/*
97template<class iterator_t>
98requires (!xtd::stream_insertable<std::iter_value_t<iterator_t>>)
99inline void __xtd_console_print_container(std::ostream& os, const iterator_t& begin, const iterator_t& end) {
100 for (auto it = begin; it != end; ++it) {
101 os << (it != begin ? ", " : "") << " ";
102 }
103}
104 */
105
106template<class iterator_t>
107inline std::ostream& __xtd_console_print_sequence_container(std::ostream& os, const iterator_t& begin, const iterator_t& end) {
108 os << "[";
109 __xtd_console_print_container(os, begin, end);
110 return os << "]";
111}
112
113template<class type_t, size_t size_t>
114inline std::ostream& operator <<(std::ostream& os, const std::array<type_t, size_t>& values) {
115 return __xtd_console_print_sequence_container(os, values.begin(), values.end());
116}
117
118template<class type_t, class allocator_t = std::allocator < type_t>>
119inline std::ostream& operator <<(std::ostream& os, const std::deque<type_t, allocator_t>& values) {
120 return __xtd_console_print_sequence_container(os, values.begin(), values.end());
121}
122
123template<class type_t, class allocator_t = std::allocator<type_t>>
124inline std::ostream& operator <<(std::ostream& os, const std::forward_list<type_t, allocator_t>& values) {
125 return __xtd_console_print_sequence_container(os, values.begin(), values.end());
126}
127
128template<class type_t>
129inline std::ostream& operator <<(std::ostream& os, const std::initializer_list<type_t>& values) {
130 return __xtd_console_print_sequence_container(os, values.begin(), values.end());
131}
132
133template<class type_t, class allocator_t = std::allocator<type_t>>
134inline std::ostream& operator <<(std::ostream& os, const std::list<type_t, allocator_t>& values) {
135 return __xtd_console_print_sequence_container(os, values.begin(), values.end());
136}
137
138template<class type_t>
139inline std::ostream& operator <<(std::ostream& os, const std::valarray<type_t>& values) {
140 return __xtd_console_print_sequence_container(os, std::begin(values), std::end(values));
141}
142
143template<class type_t, class allocator_t = std::allocator<type_t>>
144inline std::ostream& operator <<(std::ostream& os, const std::vector<type_t, allocator_t>& values) {
145 return __xtd_console_print_sequence_container(os, values.begin(), values.end());
146}
147
148template<class iterator_t>
149inline std::ostream& __xtd_console_print_associative_container(std::ostream& os, const iterator_t& begin, const iterator_t& end) {
150 os << "{";
151 __xtd_console_print_container(os, begin, end);
152 return os << "}";
153}
154
155template<class key_t, class value_t, class compare_t = std::less <key_t>, class allocator_t = std::allocator<std::pair<const key_t, value_t>>>
156inline std::ostream& operator <<(std::ostream& os, const std::map < key_t, value_t, compare_t, allocator_t >& values) {
157 return __xtd_console_print_associative_container(os, values.begin(), values.end());
158}
159
160template<class key_t, class value_t, class compare_t = std::less <key_t>, class allocator_t = std::allocator<std::pair<const key_t, value_t>>>
161inline std::ostream& operator <<(std::ostream& os, const std::multimap < key_t, value_t, compare_t, allocator_t >& values) {
162 return __xtd_console_print_associative_container(os, values.begin(), values.end());
163}
164
165template<class key_t, class compare_t = std::less <key_t>, class allocator_t = std::allocator<key_t>>
166inline std::ostream& operator <<(std::ostream& os, const std::multiset < key_t, compare_t, allocator_t >& values) {
167 return __xtd_console_print_associative_container(os, values.begin(), values.end());
168}
169
170template < class key_t, class compare_t = std::less < key_t>, class allocator_t = std::allocator<key_t >>
171inline std::ostream& operator <<(std::ostream& os, const std::set < key_t, compare_t, allocator_t >& values) {
172 return __xtd_console_print_associative_container(os, values.begin(), values.end());
173}
174
175template < class key_t, class value_t, class pred_t = std::equal_to < key_t>, class allocator_t = std::allocator<std::pair<const key_t, value_t >>>
176inline std::ostream& operator <<(std::ostream& os, const std::unordered_map < key_t, value_t, pred_t, allocator_t >& values) {
177 return __xtd_console_print_associative_container(os, values.begin(), values.end());
178}
179
180template < class key_t, class value_t, class pred_t = std::equal_to < key_t>, class allocator_t = std::allocator<std::pair<const key_t, value_t >>>
181inline std::ostream& operator <<(std::ostream& os, const std::unordered_multimap < key_t, value_t, pred_t, allocator_t >& values) {
182 return __xtd_console_print_associative_container(os, values.begin(), values.end());
183}
184
185template < class key_t, class pred_t = std::equal_to < key_t>, class allocator_t = std::allocator<key_t >>
186std::ostream& operator <<(std::ostream& os, const std::unordered_multiset < key_t, pred_t, allocator_t >& values) {
187 return __xtd_console_print_associative_container(os, values.begin(), values.end());
188}
189
190template < class key_t, class pred_t = std::equal_to < key_t>, class allocator_t = std::allocator<key_t >>
191inline std::ostream& operator <<(std::ostream& os, const std::unordered_set < key_t, pred_t, allocator_t >& values) {
192 return __xtd_console_print_associative_container(os, values.begin(), values.end());
193}
194
195inline std::ostream& operator <<(std::ostream& os, const std::error_category& value) {
196 return os << "(" << value.name() << ")";
197}
198
199inline std::ostream& operator <<(std::ostream& os, const std::error_code& value) {
200 return os << "(value = " << value.value() << "category= " << value.category().name() << ")";
201}
202
203namespace xtd {
204 class iformatable;
205 template <class type_t>
206 class istringable;
207}
208auto operator << (std::ostream& os, const xtd::iformatable& value) -> std::ostream&;
209template<class type_t>
210auto operator << (std::ostream& os, const xtd::istringable<type_t>& value) noexcept -> std::ostream& {
211 return os << value.to_string();
212}
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: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.