-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrint.inl
150 lines (129 loc) · 3.65 KB
/
Print.inl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* \file Print.inl
* \brief std::ostream printers
*/
#if defined(__GNUC__) || \
defined(__MINGW32__) || defined(__MINGW64__) || \
defined(__clang__)
#include <cxxabi.h>
#endif
namespace stdstream
{
//-------------------------------------------------------------------------------------------------
inline
Print::Print(
const std::string &a_contName, ///< contatiner name
const std::string &a_delimiter, ///< items delimiter
std::ostream &out_os ///< [in,out] std::stream
) :
_contName {a_contName},
_delimiter{a_delimiter},
_os {out_os}
{
}
//-------------------------------------------------------------------------------------------------
template<typename IteratorT>
inline void
Print::range(
IteratorT a_first, ///< first iterator
IteratorT a_last ///< last iterator
)
{
// title
_title(a_first, a_last);
// body
if (a_first == a_last) {
_os << "{}";
return;
}
_os << "{";
_os << *a_first;
for (++ a_first; a_first != a_last; ++ a_first) {
_os << _delimiter;
_os << *a_first;
}
_os << "}";
}
//-------------------------------------------------------------------------------------------------
template<typename ContT>
inline void
Print::container(
const ContT &a_cont ///< container
)
{
range(a_cont.cbegin(), a_cont.cend());
}
//-------------------------------------------------------------------------------------------------
template<typename T1, typename T2>
inline void
Print::container(
const std::pair<T1, T2> &a_cont ///< container
)
{
// title
_os << _contName << ": ";
// body
_os << "{" << a_cont.first << _delimiter << a_cont.second << "}";
}
//-------------------------------------------------------------------------------------------------
template<typename IteratorT>
inline void
Print::_title(
IteratorT a_first, ///< first iterator
IteratorT a_last ///< last iterator
)
{
const auto valueSize = std::distance(a_first, a_last);
_os << _contName << " (size=" << valueSize << "): ";
}
//-------------------------------------------------------------------------------------------------
template<typename T>
inline std::string
Print::typeNameDemangle(
const T &a_cont ///< container
)
{
const char * const typeName = typeid( decltype(a_cont) ).name();
std::string demangledName;
{
#if defined(__GNUC__) || \
defined(__MINGW32__) || defined(__MINGW64__) || \
defined(__clang__)
char *buff {};
std::size_t *length {};
int status {- 1};
char *pszRv = abi::__cxa_demangle(typeName, buff, length, &status);
demangledName = (pszRv == nullptr) ? "<unknown>" : pszRv;
std::free(pszRv);
pszRv = nullptr;
#elif defined(_MSC_VER)
demangledName = (typeName == nullptr) ? "<unknown>" : typeName;
#else
demangledName = (typeName == nullptr) ? "<unknown>" : typeName;
#endif
}
std::string demangledNameCustom;
// Template's huge info - remove (pair<int, int> -> pair)
{
const auto posBegin = demangledName.find("<");
const auto posEnd = demangledName.rfind(">");
// std::cout << STD_TRACE_VAR2(posBegin, posEnd) << std::endl;
if (posBegin != std::string::npos &&
posEnd != std::string::npos)
{
demangledNameCustom += demangledName.substr(0, posBegin);
demangledNameCustom += demangledName.substr(posEnd + 1);
}
}
// "__cxx11" - remove (std::__cxx11::list -> std::list)
{
const std::string cxxStr = "__cxx11::";
const auto pos = demangledNameCustom.find(cxxStr);
if (pos != std::string::npos) {
demangledNameCustom.erase(pos, cxxStr.size());
}
}
return demangledNameCustom;
}
//-------------------------------------------------------------------------------------------------
} // ns stdstream