Console Library 4.7.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
repr.h
Go to the documentation of this file.
1
10
11/*
12Copyright (c) 2026 MrXie1109
13
14Permission is hereby granted, free of charge, to any person obtaining a copy
15of this software and associated documentation files (the "Software"), to deal
16in the Software without restriction, including without limitation the rights
17to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18copies of the Software, and to permit persons to whom the Software is
19furnished to do so, subject to the following conditions:
20
21The above copyright notice and this permission notice shall be included in all
22copies or substantial portions of the Software.
23
24THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30SOFTWARE.
31*/
32
33#pragma once
34#include <iostream>
35#include <typeinfo>
36#include <memory>
37#include <string>
38#include "sfinae.h"
39#ifdef __GNUG__
40#include <cxxabi.h>
41#endif
42
43namespace console
44{
50
56 std::string tiname(const std::type_info &ti)
57 {
58#ifdef _MSC_VER
59 return ti.name();
60#elif defined(__GNUG__)
61 int status = 0;
62 std::unique_ptr<char, void (*)(void *)> result(
63 abi::__cxa_demangle(ti.name(), nullptr, nullptr, &status),
64 std::free);
65 return (status == 0) ? result.get() : ti.name();
66#else
67 return ti.name();
68#endif
69 }
70
71#ifndef CONSOLE_PLAIN_REPR
78 template <class T>
79 enable_if_string<T> repr(T &&value, std::ostream &os = std::cout)
80 {
81 os << '"' << value << '"';
82 }
83
90 template <class T>
91 enable_if_char<T> repr(T &&value, std::ostream &os = std::cout)
92 {
93 os << "'" << value << "'";
94 }
95
102 template <class T>
103 typename std::enable_if<
104 std::is_same<typename std::decay<T>::type, bool>::value>::type
105 repr(T &&value, std::ostream &os = std::cout)
106 {
107 os << (value ? "true" : "false");
108 }
109
116 template <class T>
117 typename std::enable_if<
118 std::is_same<typename std::decay<T>::type, std::nullptr_t>::value>::type
119 repr(T &&, std::ostream &os = std::cout)
120 {
121 os << "nullptr";
122 }
123
131 template <class Ret, class... Args>
132 void repr(Ret (*f)(Args...), std::ostream &os = std::cout)
133 {
134 if (f)
135 os << "<function at " << (void *)f << '>';
136 else
137 os << "nullptr";
138 }
139
146 template <class T>
147 typename std::enable_if<
148 !std::is_same<typename std::decay<T>::type, bool>::value &&
149 !is_string<typename std::decay<T>::type>::value &&
150 !is_char<typename std::decay<T>::type>::value &&
151 !std::is_function<typename std::decay<T>::type>::value &&
152 is_printable<typename std::decay<T>::type>::value>::type
153 repr(T &&value, std::ostream &os = std::cout)
154 {
155 os << value;
156 }
157
164 template <class T>
165 typename std::enable_if<
166 !std::is_same<typename std::decay<T>::type, std::nullptr_t>::value &&
167 !is_string<typename std::decay<T>::type>::value &&
168 !is_char<typename std::decay<T>::type>::value &&
169 !is_printable<typename std::decay<T>::type>::value>::type
170 repr(T &&value, std::ostream &os = std::cout)
171 {
172 os << '<'
173 << tiname(typeid(typename std::decay<T>::type))
174 << " object at "
175 << &value
176 << '>';
177 }
178#else
179 template <class T>
180 void repr(T &&value, std::ostream &os = std::cout)
181 {
182 os << std::forward<T>(value);
183 }
184#endif
185 // end of repr group
187}
enable_if_string< T > repr(T &&value, std::ostream &os=std::cout)
输出字符串类型(std::string, const char* 等)的表示,带双引号。
Definition repr.h:79
std::string tiname(const std::type_info &ti)
获取类型信息的可读名称(跨平台 demangle)。
Definition repr.h:56
typename std::enable_if< is_string< typename std::decay< T >::type >::value >::type enable_if_string
启用若 T(decay 后)是字符串类型。
Definition sfinae.h:337
typename std::enable_if< is_char< typename std::decay< T >::type >::value >::type enable_if_char
启用若 T(decay 后)是字符类型。
Definition sfinae.h:367
本库所有组件所在的顶层命名空间。
@ T
Definition kb.h:72
提供编译期类型特征检测(SFINAE 工具),用于判断容器、可调用对象、迭代器、下标访问、字符串、可打印类型、字符类型等。