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