]> git.lyx.org Git - lyx.git/blob - boost/boost/any.hpp
complie fix
[lyx.git] / boost / boost / any.hpp
1 #ifndef BOOST_ANY_INCLUDED
2 #define BOOST_ANY_INCLUDED
3
4 // what:  variant type boost::any
5 // who:   contributed by Kevlin Henney,
6 //        with features contributed and bugs found by
7 //        Ed Brey, Mark Rodgers, Peter Dimov, and James Curran
8 // when:  July 2001
9 // where: tested with BCC 5.5, MSVC 6.0, and g++ 2.95
10
11 #include <algorithm>
12 #include <typeinfo>
13
14 #include "boost/config.hpp"
15
16 namespace boost
17 {
18     class any
19     {
20     public: // structors
21
22         any()
23           : content(0)
24         {
25         }
26
27         template<typename ValueType>
28         any(const ValueType & value)
29           : content(new holder<ValueType>(value))
30         {
31         }
32
33         any(const any & other)
34           : content(other.content ? other.content->clone() : 0)
35         {
36         }
37
38         ~any()
39         {
40             delete content;
41         }
42
43     public: // modifiers
44
45         any & swap(any & rhs)
46         {
47             std::swap(content, rhs.content);
48             return *this;
49         }
50
51         template<typename ValueType>
52         any & operator=(const ValueType & rhs)
53         {
54             any(rhs).swap(*this);
55             return *this;
56         }
57
58         any & operator=(const any & rhs)
59         {
60             any(rhs).swap(*this);
61             return *this;
62         }
63
64     public: // queries
65
66         bool empty() const
67         {
68             return !content;
69         }
70
71         const std::type_info & type() const
72         {
73             return content ? content->type() : typeid(void);
74         }
75
76 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
77     private: // types
78 #else
79     public: // types (public so any_cast can be non-friend)
80 #endif
81
82         class placeholder
83         {
84         public: // structors
85     
86             virtual ~placeholder()
87             {
88             }
89
90         public: // queries
91
92             virtual const std::type_info & type() const = 0;
93
94             virtual placeholder * clone() const = 0;
95     
96         };
97
98         template<typename ValueType>
99         class holder : public placeholder
100         {
101         public: // structors
102
103             holder(const ValueType & value)
104               : held(value)
105             {
106             }
107
108         public: // queries
109
110             virtual const std::type_info & type() const
111             {
112                 return typeid(ValueType);
113             }
114
115             virtual placeholder * clone() const
116             {
117                 return new holder(held);
118             }
119
120         public: // representation
121
122             ValueType held;
123
124         };
125
126 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
127
128     private: // representation
129
130         template<typename ValueType>
131         friend ValueType * any_cast(any *);
132
133 #else
134
135     public: // representation (public so any_cast can be non-friend)
136
137 #endif
138
139         placeholder * content;
140
141     };
142
143     class bad_any_cast : public std::bad_cast
144     {
145     public:
146         virtual const char * what() const throw()
147         {
148             return "boost::bad_any_cast: "
149                    "failed conversion using boost::any_cast";
150         }
151     };
152
153     template<typename ValueType>
154     ValueType * any_cast(any * operand)
155     {
156         return operand && operand->type() == typeid(ValueType)
157                     ? &static_cast<any::holder<ValueType> *>(operand->content)->held
158                     : 0;
159     }
160
161     template<typename ValueType>
162     const ValueType * any_cast(const any * operand)
163     {
164         return any_cast<ValueType>(const_cast<any *>(operand));
165     }
166
167     template<typename ValueType>
168     ValueType any_cast(const any & operand)
169     {
170         const ValueType * result = any_cast<ValueType>(&operand);
171         if(!result)
172             throw bad_any_cast();
173         return *result;
174     }
175
176 }
177
178 // Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
179 //
180 // Permission to use, copy, modify, and distribute this software for any
181 // purpose is hereby granted without fee, provided that this copyright and
182 // permissions notice appear in all copies and derivatives.
183 //
184 // This software is provided "as is" without express or implied warranty.
185
186 #endif