This repository was archived by the owner on Sep 7, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathlibanytype.hpp
More file actions
193 lines (157 loc) · 5.19 KB
/
Copy pathlibanytype.hpp
File metadata and controls
193 lines (157 loc) · 5.19 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// version 1.3
#pragma once
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <map>
enum Types {
ANYNONE,
ANYSTRING,
ANYINTEGER,
ANYFLOAT,
ANYBOOLEAN,
ANYLIST,
ANYDICT
};
struct AnyType {
Types type = ANYNONE;
std::string str;
long long integer = 0;
long double lfloat = 0;
bool boolean = false;
std::vector<AnyType*> list;
std::map<AnyType*, AnyType*> dict;
AnyType() = default;
AnyType(std::string val) { str = val; type = ANYSTRING; }
AnyType(const char* val) { str = val; type = ANYSTRING; }
AnyType(char val) { integer = val; type = ANYINTEGER; }
AnyType(short val) { integer = val; type = ANYINTEGER; }
AnyType(int val) { integer = val; type = ANYINTEGER; }
AnyType(long val) { integer = val; type = ANYINTEGER; }
AnyType(long long val) { integer = val; type = ANYINTEGER; }
AnyType(float val) { lfloat = val; type = ANYFLOAT; }
AnyType(double val) { lfloat = val; type = ANYFLOAT; }
AnyType(long double val) { lfloat = val; type = ANYFLOAT; }
AnyType(bool val) { boolean = val; type = ANYBOOLEAN; }
AnyType(std::vector<AnyType*> val) { list = val; type = ANYLIST; }
AnyType(std::map<AnyType*, AnyType*> val) { dict = val; type = ANYDICT; }
AnyType(const AnyType& obj) {
clear();
copy(obj);
// std::cout << "Copy constructor" << std::endl;
}
AnyType(AnyType&& obj) {
clear();
swap(obj);
// std::cout << "Move constructor" << std::endl;
}
~AnyType() {
clear();
}
AnyType& operator=(const AnyType& obj) {
clear();
copy(obj);
// std::cout << "Copy operator" << std::endl;
return *this;
}
AnyType& operator=(AnyType&& obj) {
clear();
swap(obj);
// std::cout << "Move operator" << std::endl;
return *this;
}
void copy(const AnyType& obj) {
type = obj.type;
str = obj.str;
integer = obj.integer;
lfloat = obj.lfloat;
boolean = obj.boolean;
for (AnyType* i : obj.list) list.push_back(new AnyType(*i));
for (auto i : obj.dict) dict[new AnyType(*i.first)] = (new AnyType(*i.second));
}
void swap(AnyType& obj) {
std::swap(type, obj.type);
std::swap(str, obj.str);
std::swap(integer, obj.integer);
std::swap(lfloat, obj.lfloat);
std::swap(boolean, obj.boolean);
std::swap(list, obj.list);
std::swap(dict, obj.dict);
}
void clear() {
type = ANYNONE;
str.clear();
integer = 0;
lfloat = 0;
boolean = false;
if (list.size()) {
for (auto i : list) {
i->clear();
delete i;
}
}
if (dict.size()) {
for (auto i : dict) {
i.second->clear();
delete i.first;
delete i.second;
}
}
list.clear();
dict.clear();
}
bool operator==(const AnyType& arg) {
if (arg.type == type) {
if (type == ANYNONE) return true;
else if (type == ANYSTRING) return str == arg.str;
else if (type == ANYINTEGER) return integer == arg.integer;
else if (type == ANYFLOAT) return lfloat == arg.lfloat;
else if (type == ANYBOOLEAN) return boolean == arg.boolean;
else if (type == ANYLIST) {
for (size_t i = 0; i < list.size(); i++) {
if (*list[i] != *arg.list[i]) return false;
}
return true;
}
}
return false;
}
bool operator!=(const AnyType& arg) { return !(*this == arg); }
};
void anyPrint(AnyType obj, bool endline = true) {
if (obj.type == ANYSTRING) std::cout << std::quoted(obj.str);
else if (obj.type == ANYINTEGER) std::cout << obj.integer;
else if (obj.type == ANYFLOAT) std::cout << std::setprecision(15) << obj.lfloat;
else if (obj.type == ANYBOOLEAN) std::cout << std::boolalpha << obj.boolean;
else if (obj.type == ANYLIST) {
std::cout << "[";
anyPrint(obj.list[0], false);
for (size_t i = 1; i < obj.list.size(); i++) {
std::cout << ", ";
anyPrint(obj.list[i], false);
}
std::cout << "]";
}
else if (obj.type == ANYDICT) {
std::cout << "{";
for (auto i : obj.dict) {
anyPrint(*i.first, false);
std::cout << ": ";
anyPrint(*i.second, false);
std::cout << ", ";
}
std::cout << "}";
}
if (endline) std::cout << std::endl;
}
AnyType str2any(std::string string, Types type) {
if (type == ANYSTRING) return string;
else if (type == ANYINTEGER) return stoll(string);
else if (type == ANYFLOAT) return stold(string);
else if (type == ANYBOOLEAN) {
bool b = false;
std::istringstream(string) >> std::boolalpha >> b;
return b;
} else return { ANYNONE };
}