Repository navigation
Expand file tree
/
Copy pathAny.cpp
More file actions
251 lines (209 loc) · 7.12 KB
/
Copy pathAny.cpp
File metadata and controls
251 lines (209 loc) · 7.12 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include <iostream>
#include <vector>
// Undefine this to prevent tAny from throwing an exception if comparison operators are not defined.
#define tAnyThrowException
//********************************************
// tAny Implementation
#include <type_traits>
#include <typeinfo>
#include <utility>
#include <stdexcept>
#include <memory>
// SFINAE helpers
template<typename T, typename = void>
struct has_equal_operator : std::false_type {};
template<typename T>
struct has_equal_operator<T, typename std::enable_if<
std::is_convertible<decltype(std::declval<T>() == std::declval<T>()), bool>::value
>::type> : std::true_type {};
template<typename T, typename = void>
struct has_less_operator : std::false_type {};
template<typename T>
struct has_less_operator<T, typename std::enable_if<
std::is_convertible<decltype(std::declval<T>() < std::declval<T>()), bool>::value
>::type> : std::true_type {};
class tAny {
private:
struct IBase {
virtual ~IBase() {}
virtual const std::type_info& type() const = 0;
virtual std::unique_ptr<IBase> clone() const = 0;
virtual bool equals(const IBase*) const = 0;
virtual bool less_than(const IBase*) const = 0;
};
template<typename T>
struct Derived : IBase {
T value;
template<typename U>
Derived(U&& v) : value(std::forward<U>(v)) {}
const std::type_info& type() const override { return typeid(T); }
std::unique_ptr<IBase> clone() const override {
return std::unique_ptr<IBase>(new Derived<T>(value));
}
bool equals(const IBase* other) const override {
if (type() != other->type()) return false;
return equal_to(static_cast<const Derived<T>*>(other)->value);
}
bool less_than(const IBase* other) const override {
if (type() != other->type()) return type().before(other->type());
return less(static_cast<const Derived<T>*>(other)->value);
}
private:
template<typename U = T>
typename std::enable_if<has_equal_operator<U>::value, bool>::type
equal_to(const T& other) const {
return value == other;
}
template<typename U = T>
typename std::enable_if<!has_equal_operator<U>::value, bool>::type
equal_to(const T&) const {
#ifdef tAnyThrowException
throw std::runtime_error("Type '" + std::string(typeid(T).name()) + "' does not support equality comparison");
#else
return false;
#endif
}
template<typename U = T>
typename std::enable_if<has_less_operator<U>::value, bool>::type
less(const T& other) const {
return value < other;
}
template<typename U = T>
typename std::enable_if<!has_less_operator<U>::value, bool>::type
less(const T&) const {
#ifdef tAnyThrowException
throw std::runtime_error("Type '" + std::string(typeid(T).name()) + "' does not support less than comparison");
#else
return false;
#endif
}
};
std::unique_ptr<IBase> ptr;
public:
tAny() : ptr(nullptr) {}
template<typename T, typename = typename std::enable_if<!std::is_same<typename std::decay<T>::type, tAny>::value>::type>
tAny(T&& value) : ptr(new Derived<typename std::decay<T>::type>(std::forward<T>(value))) {}
tAny(const tAny& other) : ptr(other.ptr ? other.ptr->clone() : nullptr) {}
tAny(tAny&& other) noexcept = default;
tAny& operator=(const tAny& other) {
tAny(other).swap(*this);
return *this;
}
tAny& operator=(tAny&& other) noexcept = default;
void swap(tAny& other) noexcept {
ptr.swap(other.ptr);
}
template<typename T>
T& as() {
if (!is<T>()) {
throw std::bad_cast();
}
return static_cast<Derived<T>*>(ptr.get())->value;
}
template<typename T>
const T& as() const {
if (!is<T>()) {
throw std::bad_cast();
}
return static_cast<const Derived<T>*>(ptr.get())->value;
}
template<typename T>
bool is() const {
return ptr && typeid(T) == ptr->type();
}
bool empty() const {
return ptr == nullptr;
}
const std::type_info& type() const {
return ptr ? ptr->type() : typeid(void);
}
friend bool operator==(const tAny& lhs, const tAny& rhs) {
if (lhs.empty() && rhs.empty()) return true;
if (lhs.empty() || rhs.empty()) return false;
return lhs.ptr->equals(rhs.ptr.get());
}
friend bool operator!=(const tAny& lhs, const tAny& rhs) {
return !(lhs == rhs);
}
friend bool operator<(const tAny& lhs, const tAny& rhs) {
if (lhs.empty()) return !rhs.empty();
if (rhs.empty()) return false;
return lhs.ptr->less_than(rhs.ptr.get());
}
friend bool operator>(const tAny& lhs, const tAny& rhs) {
return rhs < lhs;
}
friend bool operator<=(const tAny& lhs, const tAny& rhs) {
return !(rhs < lhs);
}
friend bool operator>=(const tAny& lhs, const tAny& rhs) {
return !(lhs < rhs);
}
};
//********************************************
//********************************************
// Test Code
struct NoCompare {
NoCompare(int v = 0) : value(v){}
int value;
};
std::ostream& operator<<(std::ostream& ostream, const tAny& any)
{
if(any.is<int>())
{
ostream << any.as<int>();
}
else if(any.is<std::string>())
{
ostream << any.as<std::string>();
}
else if(any.is<float>())
{
ostream << any.as<float>();
}
else if(any.is<NoCompare>())
{
ostream << "NoCompare::value:" << any.as<NoCompare>().value;
}
return ostream;
}
int main()
{
std::vector<tAny> values = { 10, std::string("Hello world"), 5.4f, NoCompare(-1)};
for( const auto& v : values)
{
if(v.is<int>())
{
std::cout << "Integer: " << v << std::endl;
}
else if(v.is<std::string>())
{
std::cout << "String: " << v << std::endl;
}
else if(v.is<float>())
{
std::cout << "Float: " << v << std::endl;
}
else if(v.is<NoCompare>())
{
std::cout << "NoCompare Class: "<< v << std::endl;;
}
}
// Use try/catch in case tAnyThrowException is defined
try
{
for( int i =0; i< values.size(); i++)
{
for( int j = 0; j< values.size(); j++)
{
std::cout << "values[" << i << "] == values[" <<j<< "] -> " << values[i] << " == " << values[j] << " : " << (values[i] == values[j]) << std::endl;
std::cout << "values[" << i << "] < values[" <<j<< "] -> " << values[i] << " < " << values[j] << " : " << (values[i] < values[j]) << std::endl;
std::cout << "values[" << i << "] > values[" <<j<< "] -> " << values[i] << " > " << values[j] << " : " << (values[i] > values[j]) << std::endl;
}
}
}
catch(std::exception& e)
{
std::cout << e.what() <<std::endl;
}
}