Console Library 8.0.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
repr.h
Go to the documentation of this file.
1
11
12/*
13Copyright (c) 2026 MrXie1109
14
15Permission is hereby granted, free of charge, to any person obtaining a copy
16of this software and associated documentation files (the "Software"), to deal
17in the Software without restriction, including without limitation the rights
18to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19copies of the Software, and to permit persons to whom the Software is
20furnished to do so, subject to the following conditions:
21
22The above copyright notice and this permission notice shall be included in all
23copies or substantial portions of the Software.
24
25THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31SOFTWARE.
32*/
33
34#pragma once
35#include <iostream>
36#include <memory>
37#include <string>
38#include <typeinfo>
39
40#include "../core/sfinae.h"
41#include "./strpp.h"
42
43#ifdef __GNUG__
44#include <cxxabi.h>
45#endif
46
47namespace console {
53
59 inline std::string tiname_impl(const std::type_info &ti) {
60#ifdef _MSC_VER
61 return ti.name();
62#elif defined(__GNUG__)
63 int status = 0;
64 std::unique_ptr<char, void (*)(void *)> result(
65 abi::__cxa_demangle(ti.name(), nullptr, nullptr, &status),
66 std::free);
67 return (status == 0) ? result.get() : ti.name();
68#else
69 return ti.name();
70#endif
71 }
72
74 inline std::string tiname(const std::type_info &ti) {
75 return tiname_impl(ti);
76 }
77
78 inline std::wstring wtiname(const std::type_info &ti) {
79 auto s = tiname_impl(ti);
80 return std::wstring(s.begin(), s.end());
81 }
82
83#ifndef CONSOLE_PLAIN_REPR
92 template <class T,
93 class CharT = char,
94 class Traits = std::char_traits<CharT>>
95 typename std::enable_if<
96 is_string<typename std::decay<T>::type>::value>::type
97 repr(T &&value, std::basic_ostream<CharT, Traits> &os = std::cout) {
98 os << "\"";
99 to_stream(value, os);
100 os << "\"";
101 }
102
111 template <class T,
112 class CharT = char,
113 class Traits = std::char_traits<CharT>>
114 typename std::enable_if<is_char<typename std::decay<T>::type>::value>::type
115 repr(T &&value, std::basic_ostream<CharT, Traits> &os = std::cout) {
116 os << "'";
117 to_stream(value, os);
118 os << "'";
119 }
120
129 template <class T,
130 class CharT = char,
131 class Traits = std::char_traits<CharT>>
132 typename std::enable_if<
133 std::is_same<typename std::decay<T>::type, bool>::value>::type
134 repr(T &&value, std::basic_ostream<CharT, Traits> &os = std::cout) {
135 os << (value ? "true" : "false");
136 }
137
146 template <class T,
147 class CharT = char,
148 class Traits = std::char_traits<CharT>>
149 typename std::enable_if<
150 std::is_same<typename std::decay<T>::type, std::nullptr_t>::value>::type
151 repr(T &&, std::basic_ostream<CharT, Traits> &os = std::cout) {
152 os << "<nullptr>";
153 }
154
164 template <class CharT, class Traits, class Ret, class... Args>
165 void
166 repr(Ret (*f)(Args...), std::basic_ostream<CharT, Traits> &os = std::cout) {
167 if (f)
168 os << "<function at " << (void *)f << '>';
169 else
170 os << "<nullptr>";
171 }
172
182 template <class T,
183 class CharT = char,
184 class Traits = std::char_traits<CharT>>
185 typename std::enable_if<
186 !std::is_same<typename std::decay<T>::type, bool>::value
187 && !std::is_same<typename std::decay<T>::type, std::nullptr_t>::value
188 && !is_string<typename std::decay<T>::type>::value
189 && !is_char<typename std::decay<T>::type>::value
190 && !std::is_function<typename std::remove_pointer<T>::type>::value
191 && is_basic_printable<CharT, Traits, typename std::decay<T>::type>::
192 value>::type
193 repr(T &&value, std::basic_ostream<CharT, Traits> &os = std::cout) {
194 os << value;
195 }
196
206 template <class T,
207 class CharT = char,
208 class Traits = std::char_traits<CharT>>
209 typename std::enable_if<
210 !std::is_same<typename std::decay<T>::type, std::nullptr_t>::value
211 && !is_string<typename std::decay<T>::type>::value
212 && !is_char<typename std::decay<T>::type>::value
213 && !is_basic_printable<CharT, Traits, typename std::decay<T>::type>::
214 value>::type
215 repr(T &&value, std::basic_ostream<CharT, Traits> &os = std::cout) {
216 os << "<'" << tiname_impl(typeid(typename std::decay<T>::type))
217 << "' object at " << &value << '>';
218 }
219
220 template <class T,
221 class CharT = char,
222 class Traits = std::char_traits<CharT>>
223 void repr(std::reference_wrapper<T> v,
224 std::basic_ostream<CharT, Traits> &os = std::cout) {
225 repr(v.get(), os);
226 }
227
228#else
229 template <class T,
230 class CharT = char,
231 class Traits = std::char_traits<CharT>>
232 void repr(T &&value, std::basic_ostream<CharT, Traits> &os = std::cout) {
233 os << std::forward<T>(value);
234 }
235#endif
236 // end of repr group
238}
std::enable_if< is_string< typenamestd::decay< T >::type >::value >::type repr(T &&value, std::basic_ostream< CharT, Traits > &os=std::cout)
输出字符串类型(std::string, const char* 等)的表示,带双引号。
Definition repr.h:97
std::string tiname_impl(const std::type_info &ti)
获取类型信息的可读名称(跨平台 demangle),返回 std::string。
Definition repr.h:59
std::wstring wtiname(const std::type_info &ti)
tiname_impl 的 wchar_t 版。
Definition repr.h:78
std::string tiname(const std::type_info &ti)
tiname_impl 的 char 版别名。
Definition repr.h:74
void to_stream(char ch, std::basic_ostream< CharT, Traits > &os)
将 ch 输出到流。
Definition strpp.h:574
本库所有组件所在的顶层命名空间。
提供编译期类型特征检测(SFINAE 工具), 用于判断容器、可调用对象、迭代器、下标访问、字符串、可打印类型、字符类型等。
提供字符串处理工具函数和格式化类。