ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/utils/any.hpp
Revision: 1964
Committed: Wed Feb 2 23:12:01 2005 UTC (19 years, 5 months ago) by tim
File size: 5555 byte(s)
Log Message:
adding boost::any

File Contents

# User Rev Content
1 tim 1964 /*
2     * 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    
11     * 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    
18     * 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    
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     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     }
57    
58     template<typename ValueType>
59     any(const ValueType & value)
60     : content(new holder<ValueType>(value))
61     {
62     }
63    
64     any(const any & other)
65     : content(other.content ? other.content->clone() : 0)
66     {
67     }
68    
69     ~any()
70     {
71     delete content;
72     }
73    
74     public: // modifiers
75    
76     any & swap(any & rhs)
77     {
78     std::swap(content, rhs.content);
79     return *this;
80     }
81    
82     template<typename ValueType>
83     any & operator=(const ValueType & rhs)
84     {
85     any(rhs).swap(*this);
86     return *this;
87     }
88    
89     any & operator=(const any & rhs)
90     {
91     any(rhs).swap(*this);
92     return *this;
93     }
94    
95     public: // queries
96    
97     bool empty() const
98     {
99     return !content;
100     }
101    
102     const std::type_info & type() const
103     {
104     return content ? content->type() : typeid(void);
105     }
106    
107     //#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    
113     class placeholder
114     {
115     public: // structors
116    
117     virtual ~placeholder()
118     {
119     }
120    
121     public: // queries
122    
123     virtual const std::type_info & type() const = 0;
124    
125     virtual placeholder * clone() const = 0;
126    
127     };
128    
129     template<typename ValueType>
130     class holder : public placeholder
131     {
132     public: // structors
133    
134     holder(const ValueType & value)
135     : held(value)
136     {
137     }
138    
139     public: // queries
140    
141     virtual const std::type_info & type() const
142     {
143     return typeid(ValueType);
144     }
145    
146     virtual placeholder * clone() const
147     {
148     return new holder(held);
149     }
150    
151     public: // representation
152    
153     ValueType held;
154    
155     };
156    
157     //#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    
170     placeholder * content;
171    
172     };
173    
174     class bad_any_cast : public std::bad_cast
175     {
176     public:
177     virtual const char * what() const throw()
178     {
179     return "boost::bad_any_cast: "
180     "failed conversion using boost::any_cast";
181     }
182     };
183    
184     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    
192     template<typename ValueType>
193     const ValueType * any_cast(const any * operand)
194     {
195     return any_cast<ValueType>(const_cast<any *>(operand));
196     }
197    
198     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    
207    
208     template<typename T>
209     bool equal_any_type(const any& operand) {
210    
211     }
212    
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

Properties

Name Value
svn:executable *