epoc32/include/stdapis/boost/any.hpp
author William Roberts <williamr@symbian.org>
Tue, 16 Mar 2010 16:12:26 +0000
branchSymbian2
changeset 2 2fe1408b6811
permissions -rw-r--r--
Final list of Symbian^2 public API header files
williamr@2
     1
// See http://www.boost.org/libs/any for Documentation.
williamr@2
     2
williamr@2
     3
#ifndef BOOST_ANY_INCLUDED
williamr@2
     4
#define BOOST_ANY_INCLUDED
williamr@2
     5
williamr@2
     6
// what:  variant type boost::any
williamr@2
     7
// who:   contributed by Kevlin Henney,
williamr@2
     8
//        with features contributed and bugs found by
williamr@2
     9
//        Ed Brey, Mark Rodgers, Peter Dimov, and James Curran
williamr@2
    10
// when:  July 2001
williamr@2
    11
// where: tested with BCC 5.5, MSVC 6.0, and g++ 2.95
williamr@2
    12
williamr@2
    13
#include <algorithm>
williamr@2
    14
#include <typeinfo>
williamr@2
    15
williamr@2
    16
#include "boost/config.hpp"
williamr@2
    17
#include <boost/type_traits/remove_reference.hpp>
williamr@2
    18
#include <boost/type_traits/is_reference.hpp>
williamr@2
    19
#include <boost/throw_exception.hpp>
williamr@2
    20
#include <boost/static_assert.hpp>
williamr@2
    21
williamr@2
    22
namespace boost
williamr@2
    23
{
williamr@2
    24
    class any
williamr@2
    25
    {
williamr@2
    26
    public: // structors
williamr@2
    27
williamr@2
    28
        any()
williamr@2
    29
          : content(0)
williamr@2
    30
        {
williamr@2
    31
        }
williamr@2
    32
williamr@2
    33
        template<typename ValueType>
williamr@2
    34
        any(const ValueType & value)
williamr@2
    35
          : content(new holder<ValueType>(value))
williamr@2
    36
        {
williamr@2
    37
        }
williamr@2
    38
williamr@2
    39
        any(const any & other)
williamr@2
    40
          : content(other.content ? other.content->clone() : 0)
williamr@2
    41
        {
williamr@2
    42
        }
williamr@2
    43
williamr@2
    44
        ~any()
williamr@2
    45
        {
williamr@2
    46
            delete content;
williamr@2
    47
        }
williamr@2
    48
williamr@2
    49
    public: // modifiers
williamr@2
    50
williamr@2
    51
        any & swap(any & rhs)
williamr@2
    52
        {
williamr@2
    53
            std::swap(content, rhs.content);
williamr@2
    54
            return *this;
williamr@2
    55
        }
williamr@2
    56
williamr@2
    57
        template<typename ValueType>
williamr@2
    58
        any & operator=(const ValueType & rhs)
williamr@2
    59
        {
williamr@2
    60
            any(rhs).swap(*this);
williamr@2
    61
            return *this;
williamr@2
    62
        }
williamr@2
    63
williamr@2
    64
        any & operator=(const any & rhs)
williamr@2
    65
        {
williamr@2
    66
            any(rhs).swap(*this);
williamr@2
    67
            return *this;
williamr@2
    68
        }
williamr@2
    69
williamr@2
    70
    public: // queries
williamr@2
    71
williamr@2
    72
        bool empty() const
williamr@2
    73
        {
williamr@2
    74
            return !content;
williamr@2
    75
        }
williamr@2
    76
williamr@2
    77
        const std::type_info & type() const
williamr@2
    78
        {
williamr@2
    79
            return content ? content->type() : typeid(void);
williamr@2
    80
        }
williamr@2
    81
williamr@2
    82
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
williamr@2
    83
    private: // types
williamr@2
    84
#else
williamr@2
    85
    public: // types (public so any_cast can be non-friend)
williamr@2
    86
#endif
williamr@2
    87
williamr@2
    88
        class placeholder
williamr@2
    89
        {
williamr@2
    90
        public: // structors
williamr@2
    91
williamr@2
    92
            virtual ~placeholder()
williamr@2
    93
            {
williamr@2
    94
            }
williamr@2
    95
williamr@2
    96
        public: // queries
williamr@2
    97
williamr@2
    98
            virtual const std::type_info & type() const = 0;
williamr@2
    99
williamr@2
   100
            virtual placeholder * clone() const = 0;
williamr@2
   101
williamr@2
   102
        };
williamr@2
   103
williamr@2
   104
        template<typename ValueType>
williamr@2
   105
        class holder : public placeholder
williamr@2
   106
        {
williamr@2
   107
        public: // structors
williamr@2
   108
williamr@2
   109
            holder(const ValueType & value)
williamr@2
   110
              : held(value)
williamr@2
   111
            {
williamr@2
   112
            }
williamr@2
   113
williamr@2
   114
        public: // queries
williamr@2
   115
williamr@2
   116
            virtual const std::type_info & type() const
williamr@2
   117
            {
williamr@2
   118
                return typeid(ValueType);
williamr@2
   119
            }
williamr@2
   120
williamr@2
   121
            virtual placeholder * clone() const
williamr@2
   122
            {
williamr@2
   123
                return new holder(held);
williamr@2
   124
            }
williamr@2
   125
williamr@2
   126
        public: // representation
williamr@2
   127
williamr@2
   128
            ValueType held;
williamr@2
   129
williamr@2
   130
        };
williamr@2
   131
williamr@2
   132
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
williamr@2
   133
williamr@2
   134
    private: // representation
williamr@2
   135
williamr@2
   136
        template<typename ValueType>
williamr@2
   137
        friend ValueType * any_cast(any *);
williamr@2
   138
williamr@2
   139
        template<typename ValueType>
williamr@2
   140
        friend ValueType * unsafe_any_cast(any *);
williamr@2
   141
williamr@2
   142
#else
williamr@2
   143
williamr@2
   144
    public: // representation (public so any_cast can be non-friend)
williamr@2
   145
williamr@2
   146
#endif
williamr@2
   147
williamr@2
   148
        placeholder * content;
williamr@2
   149
williamr@2
   150
    };
williamr@2
   151
williamr@2
   152
    class bad_any_cast : public std::bad_cast
williamr@2
   153
    {
williamr@2
   154
    public:
williamr@2
   155
        virtual const char * what() const throw()
williamr@2
   156
        {
williamr@2
   157
            return "boost::bad_any_cast: "
williamr@2
   158
                   "failed conversion using boost::any_cast";
williamr@2
   159
        }
williamr@2
   160
    };
williamr@2
   161
williamr@2
   162
    template<typename ValueType>
williamr@2
   163
    ValueType * any_cast(any * operand)
williamr@2
   164
    {
williamr@2
   165
        return operand && operand->type() == typeid(ValueType)
williamr@2
   166
                    ? &static_cast<any::holder<ValueType> *>(operand->content)->held
williamr@2
   167
                    : 0;
williamr@2
   168
    }
williamr@2
   169
williamr@2
   170
    template<typename ValueType>
williamr@2
   171
    const ValueType * any_cast(const any * operand)
williamr@2
   172
    {
williamr@2
   173
        return any_cast<ValueType>(const_cast<any *>(operand));
williamr@2
   174
    }
williamr@2
   175
williamr@2
   176
    template<typename ValueType>
williamr@2
   177
    ValueType any_cast(const any & operand)
williamr@2
   178
    {
williamr@2
   179
        typedef BOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref;
williamr@2
   180
williamr@2
   181
#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
williamr@2
   182
        // If 'nonref' is still reference type, it means the user has not
williamr@2
   183
        // specialized 'remove_reference'.
williamr@2
   184
williamr@2
   185
        // Please use BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION macro
williamr@2
   186
        // to generate specialization of remove_reference for your class
williamr@2
   187
        // See type traits library documentation for details
williamr@2
   188
        BOOST_STATIC_ASSERT(!is_reference<nonref>::value);
williamr@2
   189
#endif
williamr@2
   190
williamr@2
   191
        const nonref * result = any_cast<nonref>(&operand);
williamr@2
   192
        if(!result)
williamr@2
   193
            boost::throw_exception(bad_any_cast());
williamr@2
   194
        return *result;
williamr@2
   195
    }
williamr@2
   196
williamr@2
   197
    template<typename ValueType>
williamr@2
   198
    ValueType any_cast(any & operand)
williamr@2
   199
    {
williamr@2
   200
        typedef BOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref;
williamr@2
   201
williamr@2
   202
#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
williamr@2
   203
        // The comment in the above version of 'any_cast' explains when this
williamr@2
   204
        // assert is fired and what to do.
williamr@2
   205
        BOOST_STATIC_ASSERT(!is_reference<nonref>::value);
williamr@2
   206
#endif
williamr@2
   207
williamr@2
   208
        nonref * result = any_cast<nonref>(&operand);
williamr@2
   209
        if(!result)
williamr@2
   210
            boost::throw_exception(bad_any_cast());
williamr@2
   211
        return *result;
williamr@2
   212
    }
williamr@2
   213
williamr@2
   214
    // Note: The "unsafe" versions of any_cast are not part of the
williamr@2
   215
    // public interface and may be removed at any time. They are
williamr@2
   216
    // required where we know what type is stored in the any and can't
williamr@2
   217
    // use typeid() comparison, e.g., when our types may travel across
williamr@2
   218
    // different shared libraries.
williamr@2
   219
    template<typename ValueType>
williamr@2
   220
    inline ValueType * unsafe_any_cast(any * operand)
williamr@2
   221
    {
williamr@2
   222
        return &static_cast<any::holder<ValueType> *>(operand->content)->held;
williamr@2
   223
    }
williamr@2
   224
williamr@2
   225
    template<typename ValueType>
williamr@2
   226
    inline const ValueType * unsafe_any_cast(const any * operand)
williamr@2
   227
    {
williamr@2
   228
        return unsafe_any_cast<ValueType>(const_cast<any *>(operand));
williamr@2
   229
    }
williamr@2
   230
}
williamr@2
   231
williamr@2
   232
// Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
williamr@2
   233
//
williamr@2
   234
// Distributed under the Boost Software License, Version 1.0. (See
williamr@2
   235
// accompanying file LICENSE_1_0.txt or copy at
williamr@2
   236
// http://www.boost.org/LICENSE_1_0.txt)
williamr@2
   237
williamr@2
   238
#endif