-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathustd.hpp
132 lines (99 loc) · 2.16 KB
/
ustd.hpp
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
// === (C) 2020/2021 === parallel_f / ustd (tasks, queues, lists in parallel threads)
// Written by Denis Oliver Kropp <[email protected]>
#pragma once
#include <any>
#include <string>
// parallel_f :: ustd == implementation
namespace ustd {
class string
{
private:
std::string str;
public:
template <typename... Args>
string(Args... args)
{
(str.append(args), ...);
}
std::string to_string() const
{
return std::string("'") + str + std::string("'");
}
void assign(const std::string& s)
{
str.assign(s);
}
operator std::string() const
{
return str;
}
string operator +(const string& other) const
{
return string(str, other.str);
}
};
template <typename __Type>
class to_string : public string
{
public:
to_string(const __Type& v)
{
std::string name(typeid(v).name());
size_t off = 0;
if (name.find(" ") != std::string::npos)
off = name.find(" ") + 1;
std::stringstream ss;
ss << name.substr(off) << std::string("(") << v << std::string(")");
assign(ss.str());
}
};
template <>
inline to_string<ustd::string>::to_string(const ustd::string& v)
{
assign(ustd::string("'") + v + ustd::string("'"));
}
template <>
inline to_string<std::any>::to_string(const std::any& v)
{
std::string name(v.type().name());
size_t off = 0;
if (name.find(" ") != std::string::npos)
off = name.find(" ") + 1;
assign(name.substr(off) + std::string("(") + std::string(to_string(v.has_value())) + std::string(")"));
}
template <>
inline to_string<std::string>::to_string(const std::string& v)
{
assign(std::string("'") + v + std::string("'"));
}
template <>
inline to_string<float>::to_string(const float& v)
{
assign(std::to_string(v));
}
template <>
inline to_string<double>::to_string(const double& v)
{
assign(std::to_string(v));
}
template <>
inline to_string<int>::to_string(const int& v)
{
assign(std::to_string(v));
}
template <>
inline to_string<long int>::to_string(const long int& v)
{
assign(std::to_string(v));
}
template <>
inline to_string<long long int>::to_string(const long long int& v)
{
assign(std::to_string(v));
}
template <>
inline to_string<bool>::to_string(const bool& v)
{
assign(v ? "true" : "false");
}
}