~ubuntu-branches/ubuntu/breezy/aqsis/breezy

« back to all changes in this revision

Viewing changes to boost/boost/any.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Will Newton
  • Date: 2004-12-07 20:06:49 UTC
  • Revision ID: james.westby@ubuntu.com-20041207200649-fccswkrvp4oc8lmn
Tags: upstream-0.9.3
ImportĀ upstreamĀ versionĀ 0.9.3

Show diffs side-by-side

added added

removed removed

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