-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnyType.h
More file actions
248 lines (202 loc) · 6.01 KB
/
Copy pathAnyType.h
File metadata and controls
248 lines (202 loc) · 6.01 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
#ifndef BaseLib_Templates_AnyType_h_Included
#define BaseLib_Templates_AnyType_h_Included
#include"BaseLib/CommonDefines.h"
#include <algorithm>
#include <typeinfo>
#include <cstring>
namespace BaseLib { namespace Templates
{
/**
* @brief The Any class
*/
class Any
{
public:
Any() : content_(new Holder<bool>(false))
{ }
template<typename ValueType>
Any(const ValueType & value)
: content_(new Holder<ValueType>(value))
{ }
Any(const Any & other)
: content_(other.content_ ? other.content_->Clone() : 0)
{ }
virtual ~Any()
{
delete content_;
}
public:
Any & swap(Any & rhs)
{
std::swap(content_, rhs.content_);
return *this;
}
template<typename ValueType>
Any & operator=(const ValueType & rhs)
{
Any(rhs).swap(*this);
return *this;
}
Any & operator=(Any rhs)
{
rhs.swap(*this);
return *this;
}
static Any Empty()
{
static Any empty;
return empty;
}
public:
bool empty() const
{
return !content_;
}
const std::type_info & type() const
{
return content_ ? content_->Type() : typeid(void);
}
// ------------------------------------------------
// operator comparisons
// ------------------------------------------------
bool operator==(const Any &rvalue) const
{
return *(this->content_) == *(rvalue.content_);
}
bool operator!=(const Any &rvalue) const
{
return *(this->content_) != *(rvalue.content_);
}
private: // types
class PlaceHolder
{
public:
virtual ~PlaceHolder()
{ }
public:
virtual const std::type_info & Type() const = 0;
virtual PlaceHolder * Clone() const = 0;
virtual bool operator==(const PlaceHolder &) const = 0;
virtual bool operator!=(const PlaceHolder &) const = 0;
};
template<typename ValueType>
class Holder : public PlaceHolder
{
public:
Holder(const ValueType & value)
: held(value)
{ }
public:
virtual const std::type_info & Type() const
{
return typeid(ValueType);
}
virtual PlaceHolder * Clone() const
{
return new Holder(held);
}
virtual bool operator==(const PlaceHolder &other) const
{
const Any::Holder<ValueType> *holderOther = Holder::HolderCast(&other);
//ASSERT(holderOther);
if(!holderOther) return false;
return holderOther->held == this->held;
}
virtual bool operator!=(const PlaceHolder &other) const
{
const Any::Holder<ValueType> *holderOther = Holder::HolderCast(&other);
//ASSERT(holderOther);
if(!holderOther) return false;
return holderOther->held != this->held;
}
static const Any::Holder<ValueType>* HolderCast(const PlaceHolder *const operand)
{
return operand &&
operand->Type() == typeid(ValueType)
? static_cast< const Any::Holder<ValueType> *const >(operand)
: 0;
}
public:
ValueType held;
private:
// intentionally left unimplemented
Holder & operator=(const Holder &);
};
private:
template<typename ValueType>
friend ValueType * AnyCast(Any *);
template<typename ValueType>
friend ValueType * UnsafeAnyCast(Any *);
PlaceHolder * content_;
};
class bad_any_cast : public std::bad_cast
{
public:
virtual const char * what() const throw()
{
return "boost::bad_any_cast: "
"failed conversion using boost::any_cast";
}
};
template<typename ValueType>
ValueType * AnyCast(Any * operand)
{
return operand &&
#ifdef BOOST_AUX_ANY_TYPE_ID_NAME
std::strcmp(operand->type().name(), typeid(ValueType).name()) == 0
#else
operand->type() == typeid(ValueType)
#endif
? &static_cast<Any::Holder<ValueType> *>(operand->content_)->held
: 0;
}
template<typename ValueType>
inline const ValueType * AnyCast(const Any * operand)
{
return AnyCast<ValueType>(const_cast<Any *>(operand));
}
//template<typename ValueType>
//ValueType AnyCast(Any & operand)
//{
// typedef BOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref;
//#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
// // If 'nonref' is still reference type, it means the user has not
// // specialized 'remove_reference'.
// // Please use BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION macro
// // to generate specialization of remove_reference for your class
// // See type traits library documentation for details
// BOOST_STATIC_ASSERT(!is_reference<nonref>::value);
//#endif
// nonref * result = AnyCast<nonref>(&operand);
// if(!result)
// boost::throw_exception(bad_any_cast());
// return *result;
//}
//template<typename ValueType>
//inline ValueType AnyCast(const Any & operand)
//{
// typedef BOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref;
//#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
// // The comment in the above version of 'any_cast' explains when this
// // assert is fired and what to do.
// BOOST_STATIC_ASSERT(!is_reference<nonref>::value);
//#endif
// return AnyCast<const nonref &>(const_cast<Any &>(operand));
//}
// Note: The "unsafe" versions of any_cast are not part of the
// public interface and may be removed at any time. They are
// required where we know what type is stored in the any and can't
// use typeid() comparison, e.g., when our types may travel across
// different shared libraries.
template<typename ValueType>
inline ValueType * UnsafeAnyCast(Any * operand)
{
return &static_cast<Any::Holder<ValueType> *>(operand->content_)->held;
}
template<typename ValueType>
inline const ValueType * UnsafeAnyCast(const Any * operand)
{
return UnsafeAnyCast<ValueType>(const_cast<Any *>(operand));
}
}}
#endif