1 |
tim |
280 |
/* |
2 |
gezelter |
507 |
* Boost Software License - Version 1.0 - August 17th, 2003 |
3 |
|
|
* |
4 |
|
|
* Permission is hereby granted, free of charge, to any person or organization |
5 |
|
|
* obtaining a copy of the software and accompanying documentation covered by |
6 |
|
|
* this license (the "Software") to use, reproduce, display, distribute, |
7 |
|
|
* execute, and transmit the Software, and to prepare derivative works of the |
8 |
|
|
* Software, and to permit third-parties to whom the Software is furnished to |
9 |
|
|
* do so, all subject to the following: |
10 |
tim |
280 |
|
11 |
gezelter |
507 |
* The copyright notices in the Software and this entire statement, including |
12 |
|
|
* the above license grant, this restriction and the following disclaimer, |
13 |
|
|
* must be included in all copies of the Software, in whole or in part, and |
14 |
|
|
* all derivative works of the Software, unless such copies or derivative |
15 |
|
|
* works are solely in the form of machine-executable object code generated by |
16 |
|
|
* a source language processor. |
17 |
tim |
280 |
|
18 |
gezelter |
507 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19 |
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20 |
|
|
* FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
21 |
|
|
* SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
22 |
|
|
* FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
23 |
|
|
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
24 |
|
|
* DEALINGS IN THE SOFTWARE. |
25 |
|
|
*/ |
26 |
tim |
280 |
|
27 |
|
|
// See http://www.boost.org/libs/any for Documentation. |
28 |
|
|
|
29 |
|
|
#ifndef BOOST_ANY_INCLUDED |
30 |
|
|
#define BOOST_ANY_INCLUDED |
31 |
|
|
|
32 |
|
|
// what: variant type boost::any |
33 |
|
|
// who: contributed by Kevlin Henney, |
34 |
|
|
// with features contributed and bugs found by |
35 |
|
|
// Ed Brey, Mark Rodgers, Peter Dimov, and James Curran |
36 |
|
|
// when: July 2001 |
37 |
|
|
// where: tested with BCC 5.5, MSVC 6.0, and g++ 2.95 |
38 |
|
|
|
39 |
|
|
#include <algorithm> |
40 |
|
|
#include <typeinfo> |
41 |
|
|
|
42 |
|
|
namespace boost |
43 |
|
|
{ |
44 |
|
|
|
45 |
gezelter |
507 |
template<class E> inline void throw_exception(E const & e) { |
46 |
|
|
throw e; |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
class any |
50 |
|
|
{ |
51 |
|
|
public: // structors |
52 |
|
|
|
53 |
|
|
any() |
54 |
|
|
: content(0) |
55 |
|
|
{ |
56 |
tim |
280 |
} |
57 |
|
|
|
58 |
gezelter |
507 |
template<typename ValueType> |
59 |
|
|
any(const ValueType & value) |
60 |
|
|
: content(new holder<ValueType>(value)) |
61 |
tim |
280 |
{ |
62 |
gezelter |
507 |
} |
63 |
tim |
280 |
|
64 |
gezelter |
507 |
any(const any & other) |
65 |
|
|
: content(other.content ? other.content->clone() : 0) |
66 |
|
|
{ |
67 |
|
|
} |
68 |
tim |
280 |
|
69 |
gezelter |
507 |
~any() |
70 |
|
|
{ |
71 |
|
|
delete content; |
72 |
|
|
} |
73 |
tim |
280 |
|
74 |
gezelter |
507 |
public: // modifiers |
75 |
tim |
280 |
|
76 |
gezelter |
507 |
any & swap(any & rhs) |
77 |
|
|
{ |
78 |
|
|
std::swap(content, rhs.content); |
79 |
|
|
return *this; |
80 |
|
|
} |
81 |
tim |
280 |
|
82 |
gezelter |
507 |
template<typename ValueType> |
83 |
|
|
any & operator=(const ValueType & rhs) |
84 |
|
|
{ |
85 |
|
|
any(rhs).swap(*this); |
86 |
|
|
return *this; |
87 |
|
|
} |
88 |
tim |
280 |
|
89 |
gezelter |
507 |
any & operator=(const any & rhs) |
90 |
|
|
{ |
91 |
|
|
any(rhs).swap(*this); |
92 |
|
|
return *this; |
93 |
|
|
} |
94 |
tim |
280 |
|
95 |
gezelter |
507 |
public: // queries |
96 |
tim |
280 |
|
97 |
gezelter |
507 |
bool empty() const |
98 |
|
|
{ |
99 |
|
|
return !content; |
100 |
|
|
} |
101 |
tim |
280 |
|
102 |
gezelter |
507 |
const std::type_info & type() const |
103 |
|
|
{ |
104 |
|
|
return content ? content->type() : typeid(void); |
105 |
|
|
} |
106 |
tim |
280 |
|
107 |
gezelter |
507 |
//#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS |
108 |
|
|
// private: // types |
109 |
|
|
//#else |
110 |
|
|
public: // types (public so any_cast can be non-friend) |
111 |
|
|
//#endif |
112 |
tim |
280 |
|
113 |
gezelter |
507 |
class placeholder |
114 |
|
|
{ |
115 |
|
|
public: // structors |
116 |
tim |
280 |
|
117 |
gezelter |
507 |
virtual ~placeholder() |
118 |
|
|
{ |
119 |
|
|
} |
120 |
tim |
280 |
|
121 |
gezelter |
507 |
public: // queries |
122 |
tim |
280 |
|
123 |
gezelter |
507 |
virtual const std::type_info & type() const = 0; |
124 |
tim |
280 |
|
125 |
gezelter |
507 |
virtual placeholder * clone() const = 0; |
126 |
tim |
280 |
|
127 |
gezelter |
507 |
}; |
128 |
tim |
280 |
|
129 |
gezelter |
507 |
template<typename ValueType> |
130 |
|
|
class holder : public placeholder |
131 |
|
|
{ |
132 |
|
|
public: // structors |
133 |
tim |
280 |
|
134 |
gezelter |
507 |
holder(const ValueType & value) |
135 |
|
|
: held(value) |
136 |
|
|
{ |
137 |
|
|
} |
138 |
tim |
280 |
|
139 |
gezelter |
507 |
public: // queries |
140 |
tim |
280 |
|
141 |
gezelter |
507 |
virtual const std::type_info & type() const |
142 |
|
|
{ |
143 |
|
|
return typeid(ValueType); |
144 |
|
|
} |
145 |
tim |
280 |
|
146 |
gezelter |
507 |
virtual placeholder * clone() const |
147 |
|
|
{ |
148 |
|
|
return new holder(held); |
149 |
|
|
} |
150 |
tim |
280 |
|
151 |
gezelter |
507 |
public: // representation |
152 |
tim |
280 |
|
153 |
gezelter |
507 |
ValueType held; |
154 |
tim |
280 |
|
155 |
gezelter |
507 |
}; |
156 |
tim |
280 |
|
157 |
gezelter |
507 |
//#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS |
158 |
|
|
// |
159 |
|
|
// private: // representation |
160 |
|
|
// |
161 |
|
|
// template<typename ValueType> |
162 |
|
|
// friend ValueType * any_cast(any *); |
163 |
|
|
// |
164 |
|
|
//#else |
165 |
|
|
// |
166 |
|
|
public: // representation (public so any_cast can be non-friend) |
167 |
|
|
// |
168 |
|
|
//#endif |
169 |
tim |
280 |
|
170 |
gezelter |
507 |
placeholder * content; |
171 |
tim |
280 |
|
172 |
gezelter |
507 |
}; |
173 |
tim |
280 |
|
174 |
gezelter |
507 |
class bad_any_cast : public std::bad_cast |
175 |
|
|
{ |
176 |
|
|
public: |
177 |
|
|
virtual const char * what() const throw() |
178 |
tim |
280 |
{ |
179 |
gezelter |
507 |
return "boost::bad_any_cast: " |
180 |
|
|
"failed conversion using boost::any_cast"; |
181 |
tim |
280 |
} |
182 |
gezelter |
507 |
}; |
183 |
tim |
280 |
|
184 |
gezelter |
507 |
template<typename ValueType> |
185 |
|
|
ValueType * any_cast(any * operand) |
186 |
|
|
{ |
187 |
|
|
return operand && operand->type() == typeid(ValueType) |
188 |
|
|
? &static_cast<any::holder<ValueType> *>(operand->content)->held |
189 |
|
|
: 0; |
190 |
|
|
} |
191 |
tim |
280 |
|
192 |
gezelter |
507 |
template<typename ValueType> |
193 |
|
|
const ValueType * any_cast(const any * operand) |
194 |
|
|
{ |
195 |
|
|
return any_cast<ValueType>(const_cast<any *>(operand)); |
196 |
|
|
} |
197 |
tim |
280 |
|
198 |
gezelter |
507 |
template<typename ValueType> |
199 |
|
|
ValueType any_cast(const any & operand) |
200 |
|
|
{ |
201 |
|
|
const ValueType * result = any_cast<ValueType>(&operand); |
202 |
|
|
if(!result) |
203 |
|
|
boost::throw_exception(bad_any_cast()); |
204 |
|
|
return *result; |
205 |
|
|
} |
206 |
tim |
280 |
|
207 |
|
|
|
208 |
gezelter |
877 |
// template<typename T> |
209 |
|
|
// bool equal_any_type(const any& operand) { |
210 |
|
|
// |
211 |
|
|
// } |
212 |
tim |
280 |
|
213 |
|
|
} |
214 |
|
|
|
215 |
|
|
// Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved. |
216 |
|
|
// |
217 |
|
|
// Distributed under the Boost Software License, Version 1.0. (See |
218 |
|
|
// accompanying file LICENSE_1_0.txt or copy at |
219 |
|
|
// http://www.boost.org/LICENSE_1_0.txt) |
220 |
|
|
|
221 |
|
|
#endif |