]> AND Private Git Repository - canny.git/blob - stc/exp/ml_stc_linux_make_v1.0/include/boost/any.hpp
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
646cd295d133f8e5defe76b4bbac102e51ed0e29
[canny.git] / stc / exp / ml_stc_linux_make_v1.0 / include / boost / any.hpp
1 // See http://www.boost.org/libs/any for Documentation.\r
2 \r
3 #ifndef BOOST_ANY_INCLUDED\r
4 #define BOOST_ANY_INCLUDED\r
5 \r
6 // what:  variant type boost::any\r
7 // who:   contributed by Kevlin Henney,\r
8 //        with features contributed and bugs found by\r
9 //        Ed Brey, Mark Rodgers, Peter Dimov, and James Curran\r
10 // when:  July 2001\r
11 // where: tested with BCC 5.5, MSVC 6.0, and g++ 2.95\r
12 \r
13 #include <algorithm>\r
14 #include <typeinfo>\r
15 \r
16 #include "boost/config.hpp"\r
17 #include <boost/type_traits/remove_reference.hpp>\r
18 #include <boost/type_traits/is_reference.hpp>\r
19 #include <boost/throw_exception.hpp>\r
20 #include <boost/static_assert.hpp>\r
21 \r
22 // See boost/python/type_id.hpp\r
23 // TODO: add BOOST_TYPEID_COMPARE_BY_NAME to config.hpp\r
24 # if (defined(__GNUC__) && __GNUC__ >= 3) \\r
25  || defined(_AIX) \\r
26  || (   defined(__sgi) && defined(__host_mips)) \\r
27  || (defined(__hpux) && defined(__HP_aCC)) \\r
28  || (defined(linux) && defined(__INTEL_COMPILER) && defined(__ICC))\r
29 #  define BOOST_AUX_ANY_TYPE_ID_NAME\r
30 #include <cstring>\r
31 # endif \r
32 \r
33 namespace boost\r
34 {\r
35     class any\r
36     {\r
37     public: // structors\r
38 \r
39         any()\r
40           : content(0)\r
41         {\r
42         }\r
43 \r
44         template<typename ValueType>\r
45         any(const ValueType & value)\r
46           : content(new holder<ValueType>(value))\r
47         {\r
48         }\r
49 \r
50         any(const any & other)\r
51           : content(other.content ? other.content->clone() : 0)\r
52         {\r
53         }\r
54 \r
55         ~any()\r
56         {\r
57             delete content;\r
58         }\r
59 \r
60     public: // modifiers\r
61 \r
62         any & swap(any & rhs)\r
63         {\r
64             std::swap(content, rhs.content);\r
65             return *this;\r
66         }\r
67 \r
68         template<typename ValueType>\r
69         any & operator=(const ValueType & rhs)\r
70         {\r
71             any(rhs).swap(*this);\r
72             return *this;\r
73         }\r
74 \r
75         any & operator=(any rhs)\r
76         {\r
77             rhs.swap(*this);\r
78             return *this;\r
79         }\r
80 \r
81     public: // queries\r
82 \r
83         bool empty() const\r
84         {\r
85             return !content;\r
86         }\r
87 \r
88         const std::type_info & type() const\r
89         {\r
90             return content ? content->type() : typeid(void);\r
91         }\r
92 \r
93 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS\r
94     private: // types\r
95 #else\r
96     public: // types (public so any_cast can be non-friend)\r
97 #endif\r
98 \r
99         class placeholder\r
100         {\r
101         public: // structors\r
102 \r
103             virtual ~placeholder()\r
104             {\r
105             }\r
106 \r
107         public: // queries\r
108 \r
109             virtual const std::type_info & type() const = 0;\r
110 \r
111             virtual placeholder * clone() const = 0;\r
112 \r
113         };\r
114 \r
115         template<typename ValueType>\r
116         class holder : public placeholder\r
117         {\r
118         public: // structors\r
119 \r
120             holder(const ValueType & value)\r
121               : held(value)\r
122             {\r
123             }\r
124 \r
125         public: // queries\r
126 \r
127             virtual const std::type_info & type() const\r
128             {\r
129                 return typeid(ValueType);\r
130             }\r
131 \r
132             virtual placeholder * clone() const\r
133             {\r
134                 return new holder(held);\r
135             }\r
136 \r
137         public: // representation\r
138 \r
139             ValueType held;\r
140 \r
141         private: // intentionally left unimplemented\r
142             holder & operator=(const holder &);\r
143         };\r
144 \r
145 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS\r
146 \r
147     private: // representation\r
148 \r
149         template<typename ValueType>\r
150         friend ValueType * any_cast(any *);\r
151 \r
152         template<typename ValueType>\r
153         friend ValueType * unsafe_any_cast(any *);\r
154 \r
155 #else\r
156 \r
157     public: // representation (public so any_cast can be non-friend)\r
158 \r
159 #endif\r
160 \r
161         placeholder * content;\r
162 \r
163     };\r
164 \r
165     class bad_any_cast : public std::bad_cast\r
166     {\r
167     public:\r
168         virtual const char * what() const throw()\r
169         {\r
170             return "boost::bad_any_cast: "\r
171                    "failed conversion using boost::any_cast";\r
172         }\r
173     };\r
174 \r
175     template<typename ValueType>\r
176     ValueType * any_cast(any * operand)\r
177     {\r
178         return operand && \r
179 #ifdef BOOST_AUX_ANY_TYPE_ID_NAME\r
180             std::strcmp(operand->type().name(), typeid(ValueType).name()) == 0\r
181 #else\r
182             operand->type() == typeid(ValueType)\r
183 #endif\r
184             ? &static_cast<any::holder<ValueType> *>(operand->content)->held\r
185             : 0;\r
186     }\r
187 \r
188     template<typename ValueType>\r
189     inline const ValueType * any_cast(const any * operand)\r
190     {\r
191         return any_cast<ValueType>(const_cast<any *>(operand));\r
192     }\r
193 \r
194     template<typename ValueType>\r
195     ValueType any_cast(any & operand)\r
196     {\r
197         typedef BOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref;\r
198 \r
199 #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION\r
200         // If 'nonref' is still reference type, it means the user has not\r
201         // specialized 'remove_reference'.\r
202 \r
203         // Please use BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION macro\r
204         // to generate specialization of remove_reference for your class\r
205         // See type traits library documentation for details\r
206         BOOST_STATIC_ASSERT(!is_reference<nonref>::value);\r
207 #endif\r
208 \r
209         nonref * result = any_cast<nonref>(&operand);\r
210         if(!result)\r
211             boost::throw_exception(bad_any_cast());\r
212         return *result;\r
213     }\r
214 \r
215     template<typename ValueType>\r
216     inline ValueType any_cast(const any & operand)\r
217     {\r
218         typedef BOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref;\r
219 \r
220 #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION\r
221         // The comment in the above version of 'any_cast' explains when this\r
222         // assert is fired and what to do.\r
223         BOOST_STATIC_ASSERT(!is_reference<nonref>::value);\r
224 #endif\r
225 \r
226         return any_cast<const nonref &>(const_cast<any &>(operand));\r
227     }\r
228 \r
229     // Note: The "unsafe" versions of any_cast are not part of the\r
230     // public interface and may be removed at any time. They are\r
231     // required where we know what type is stored in the any and can't\r
232     // use typeid() comparison, e.g., when our types may travel across\r
233     // different shared libraries.\r
234     template<typename ValueType>\r
235     inline ValueType * unsafe_any_cast(any * operand)\r
236     {\r
237         return &static_cast<any::holder<ValueType> *>(operand->content)->held;\r
238     }\r
239 \r
240     template<typename ValueType>\r
241     inline const ValueType * unsafe_any_cast(const any * operand)\r
242     {\r
243         return unsafe_any_cast<ValueType>(const_cast<any *>(operand));\r
244     }\r
245 }\r
246 \r
247 // Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.\r
248 //\r
249 // Distributed under the Boost Software License, Version 1.0. (See\r
250 // accompanying file LICENSE_1_0.txt or copy at\r
251 // http://www.boost.org/LICENSE_1_0.txt)\r
252 \r
253 #endif\r