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<typename 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<typename value_t>
45inline std::ostream& operator <<(std::ostream& os, const std::optional<value_t>&) {
46 return os << "(unregistered)";
47}
48
49template<typename type1_t, typename 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<typename type1_t, typename type2_t>
57inline std::ostream& operator <<(std::ostream& os, const std::pair<type1_t, type2_t>& value) {
58 return os << "( , )";
59}
60
61template<typename 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<typename ...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<typename ...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<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 ? ", " : "") << *it;
93 }
94}
95
96template<typename iterator_t>
98inline void __xtd_console_print_container(std::ostream& os, const iterator_t& begin, const iterator_t& end) {
99 for (auto it = begin; it != end; ++it) {
100 os << (it != begin ? ", " : "") << " ";
101 }
102}
103
104template<typename iterator_t>
105inline std::ostream& __xtd_console_print_sequence_container(std::ostream& os, const iterator_t& begin, const iterator_t& end) {
106 os << "[";
107 __xtd_console_print_container(os, begin, end);
108 return os << "]";
109}
110
111template<typename type_t, size_t size_t>
112inline std::ostream& operator <<(std::ostream& os, const std::array<type_t, size_t>& values) {
113 return __xtd_console_print_sequence_container(os, values.begin(), values.end());
114}
115
116template<typename type_t, typename allocator_t = std::allocator < type_t>>
117inline std::ostream& operator <<(std::ostream& os, const std::deque<type_t, allocator_t>& values) {
118 return __xtd_console_print_sequence_container(os, values.begin(), values.end());
119}
120
121template<typename type_t, typename allocator_t = std::allocator<type_t>>
122inline std::ostream& operator <<(std::ostream& os, const std::forward_list<type_t, allocator_t>& values) {
123 return __xtd_console_print_sequence_container(os, values.begin(), values.end());
124}
125
126template<typename type_t>
127inline std::ostream& operator <<(std::ostream& os, const std::initializer_list<type_t>& values) {
128 return __xtd_console_print_sequence_container(os, values.begin(), values.end());
129}
130
131template<typename type_t, typename allocator_t = std::allocator<type_t>>
132inline std::ostream& operator <<(std::ostream& os, const std::list<type_t, allocator_t>& values) {
133 return __xtd_console_print_sequence_container(os, values.begin(), values.end());
134}
135
136template<typename type_t>
137inline std::ostream& operator <<(std::ostream& os, const std::valarray<type_t>& values) {
138 return __xtd_console_print_sequence_container(os, std::begin(values), std::end(values));
139}
140
141template<typename type_t, typename allocator_t = std::allocator<type_t>>
142inline std::ostream& operator <<(std::ostream& os, const std::vector<type_t, allocator_t>& values) {
143 return __xtd_console_print_sequence_container(os, values.begin(), values.end());
144}
145
146template<typename iterator_t>
147inline std::ostream& __xtd_console_print_associative_container(std::ostream& os, const iterator_t& begin, const iterator_t& end) {
148 os << "{";
149 __xtd_console_print_container(os, begin, end);
150 return os << "}";
151}
152
153template<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>>>
154inline std::ostream& operator <<(std::ostream& os, const std::map < key_t, value_t, compare_t, allocator_t >& values) {
155 return __xtd_console_print_associative_container(os, values.begin(), values.end());
156}
157
158template<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>>>
159inline std::ostream& operator <<(std::ostream& os, const std::multimap < key_t, value_t, compare_t, allocator_t >& values) {
160 return __xtd_console_print_associative_container(os, values.begin(), values.end());
161}
162
163template<typename key_t, typename compare_t = std::less <key_t>, typename allocator_t = std::allocator<key_t>>
164inline std::ostream& operator <<(std::ostream& os, const std::multiset < key_t, compare_t, allocator_t >& values) {
165 return __xtd_console_print_associative_container(os, values.begin(), values.end());
166}
167
168template < class key_t, typename compare_t = std::less < key_t>, typename allocator_t = std::allocator<key_t >>
169inline std::ostream& operator <<(std::ostream& os, const std::set < key_t, compare_t, allocator_t >& values) {
170 return __xtd_console_print_associative_container(os, values.begin(), values.end());
171}
172
173template < 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 >>>
174inline std::ostream& operator <<(std::ostream& os, const std::unordered_map < key_t, value_t, pred_t, allocator_t >& values) {
175 return __xtd_console_print_associative_container(os, values.begin(), values.end());
176}
177
178template < 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 >>>
179inline std::ostream& operator <<(std::ostream& os, const std::unordered_multimap < key_t, value_t, pred_t, allocator_t >& values) {
180 return __xtd_console_print_associative_container(os, values.begin(), values.end());
181}
182
183template < class key_t, typename pred_t = std::equal_to < key_t>, typename allocator_t = std::allocator<key_t >>
184std::ostream& operator <<(std::ostream& os, const std::unordered_multiset < key_t, pred_t, allocator_t >& values) {
185 return __xtd_console_print_associative_container(os, values.begin(), values.end());
186}
187
188template < class key_t, typename pred_t = std::equal_to < key_t>, typename allocator_t = std::allocator<key_t >>
189inline std::ostream& operator <<(std::ostream& os, const std::unordered_set < key_t, pred_t, allocator_t >& values) {
190 return __xtd_console_print_associative_container(os, values.begin(), values.end());
191}
192
193inline std::ostream& operator <<(std::ostream& os, const std::error_category& value) {
194 return os << "(" << value.name() << ")";
195}
196
197inline std::ostream& operator <<(std::ostream& os, const std::error_code& value) {
198 return os << "(value = " << value.value() << "category= " << value.category().name() << ")";
199}
200
201namespace xtd {
202 class iformatable;
203 template<typename type_t>
204 class istringable;
205}
206auto operator << (std::ostream& os, const xtd::iformatable& value) -> std::ostream&;
207template<typename type_t>
208auto operator << (std::ostream& os, const xtd::istringable<type_t>& value) noexcept -> std::ostream& {
209 return os << value.to_string();
210}
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
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.