userver: userver/compiler/demangle.hpp Source File
Loading...
Searching...
No Matches
demangle.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/compiler/demangle.hpp
4/// @brief @copybrief compiler::GetTypeName
5/// @ingroup userver_universal
6
7#include <chrono>
8#include <string>
9#include <typeindex>
10
11USERVER_NAMESPACE_BEGIN
12
13/// Compiler and C++ language related tweaks
14namespace compiler {
15
16/// Returns a human-readable representation of provided type name.
17std::string GetTypeName(std::type_index type);
18
19namespace detail {
20
21template <class T>
22struct TypeNameHelper {
23 static std::string Get() { return GetTypeName(typeid(T)); }
24};
25
26template <>
27struct TypeNameHelper<std::string> {
28 static std::string Get() { return "std::string"; }
29};
30
31template <>
32struct TypeNameHelper<std::chrono::nanoseconds> {
33 static std::string Get() { return "std::chrono::nanoseconds"; }
34};
35
36template <>
37struct TypeNameHelper<std::chrono::microseconds> {
38 static std::string Get() { return "std::chrono::microseconds"; }
39};
40
41template <>
42struct TypeNameHelper<std::chrono::milliseconds> {
43 static std::string Get() { return "std::chrono::milliseconds"; }
44};
45
46template <>
47struct TypeNameHelper<std::chrono::seconds> {
48 static std::string Get() { return "std::chrono::seconds"; }
49};
50
51template <>
52struct TypeNameHelper<std::chrono::minutes> {
53 static std::string Get() { return "std::chrono::minutes"; }
54};
55
56template <>
57struct TypeNameHelper<std::chrono::hours> {
58 static std::string Get() { return "std::chrono::hours"; }
59};
60
61template <>
62struct TypeNameHelper<std::chrono::steady_clock::time_point> {
63 static std::string Get() { return "std::chrono::steady_clock::time_point"; }
64};
65
66template <>
67struct TypeNameHelper<std::chrono::system_clock::time_point> {
68 static std::string Get() { return "std::chrono::system_clock::time_point"; }
69};
70
71} // namespace detail
72
73/// @brief Returns a human-readable representation of provided type name.
74///
75/// GetTypeName(typeidT)) outputs the type, not the alias. For std::chrono
76/// functions it gives unreadable results:
77/// std::chrono::duration<long, std::ratio<1l, 1l> > - it's `seconds`
78///
79/// The `GetTypeName<T>()` provides a more readable output.
80template <typename T>
81std::string GetTypeName() {
82 return detail::TypeNameHelper<T>::Get();
83}
84
85} // namespace compiler
86
87USERVER_NAMESPACE_END