ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/utils/any.hpp
Revision: 2577
Committed: Wed Feb 1 17:26:43 2006 UTC (18 years, 5 months ago) by gezelter
File size: 5126 byte(s)
Log Message:
Fixing some compile-time warnings and bugs for icc 9.1 on Mac OS X

File Contents

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