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