Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/boost/type_erasure/any_cast.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // Boost.TypeErasure library
0002 //
0003 // Copyright 2011 Steven Watanabe
0004 //
0005 // Distributed under the Boost Software License Version 1.0. (See
0006 // accompanying file LICENSE_1_0.txt or copy at
0007 // http://www.boost.org/LICENSE_1_0.txt)
0008 //
0009 // $Id$
0010 
0011 #ifndef BOOST_TYPE_ERASURE_ANY_CAST_HPP_INCLUDED
0012 #define BOOST_TYPE_ERASURE_ANY_CAST_HPP_INCLUDED
0013 
0014 #include <stdexcept>
0015 #include <boost/throw_exception.hpp>
0016 #include <boost/type_traits/add_const.hpp>
0017 #include <boost/type_traits/is_pointer.hpp>
0018 #include <boost/type_traits/remove_cv.hpp>
0019 #include <boost/type_traits/remove_reference.hpp>
0020 #include <boost/type_traits/remove_pointer.hpp>
0021 #include <boost/type_traits/is_void.hpp>
0022 #include <boost/mpl/assert.hpp>
0023 #include <boost/mpl/bool.hpp>
0024 #include <boost/type_erasure/any.hpp>
0025 #include <boost/type_erasure/builtin.hpp>
0026 #include <boost/type_erasure/exception.hpp>
0027 #include <boost/type_erasure/detail/access.hpp>
0028 
0029 namespace boost {
0030 namespace type_erasure {
0031 
0032 namespace detail {
0033 
0034 template<class Concept, class T>
0035 void* get_pointer(::boost::type_erasure::any<Concept, T>& arg)
0036 {
0037     return ::boost::type_erasure::detail::access::data(arg).data;
0038 }
0039 
0040 template<class Concept, class T>
0041 const void* get_pointer(const ::boost::type_erasure::any<Concept, T>& arg)
0042 {
0043     return ::boost::type_erasure::detail::access::data(arg).data;
0044 }
0045 
0046 template<class Concept, class T>
0047 void* get_pointer(::boost::type_erasure::any<Concept, T&>& arg)
0048 {
0049     return ::boost::type_erasure::detail::access::data(arg).data;
0050 }
0051 
0052 template<class Concept, class T>
0053 void* get_pointer(const ::boost::type_erasure::any<Concept, T&>& arg)
0054 {
0055     return ::boost::type_erasure::detail::access::data(arg).data;
0056 }
0057 
0058 template<class Concept, class T>
0059 const void* get_pointer(::boost::type_erasure::any<Concept, const T&>& arg)
0060 {
0061     return ::boost::type_erasure::detail::access::data(arg).data;
0062 }
0063 
0064 template<class Concept, class T>
0065 const void* get_pointer(const ::boost::type_erasure::any<Concept, const T&>& arg)
0066 {
0067     return ::boost::type_erasure::detail::access::data(arg).data;
0068 }
0069 
0070 template<class T, class Concept, class Tag>
0071 bool check_any_cast(const any<Concept, Tag>&, ::boost::mpl::true_)
0072 {
0073     return true;
0074 }
0075 
0076 template<class T, class Concept, class Tag>
0077 bool check_any_cast(const any<Concept, Tag>& arg, ::boost::mpl::false_)
0078 {
0079     typedef typename ::boost::remove_cv<
0080         typename ::boost::remove_reference<Tag>::type
0081     >::type tag_type;
0082     return ::boost::type_erasure::detail::access::table(arg)
0083         .template find<typeid_<tag_type> >()() == typeid(T);
0084 }
0085 
0086 template<class T, class Concept, class Tag>
0087 bool check_any_cast(const any<Concept, Tag>& arg)
0088 {
0089     return ::boost::type_erasure::detail::check_any_cast<T>(
0090         arg, ::boost::is_void<typename ::boost::remove_reference<T>::type>());
0091 }
0092 
0093 }
0094 
0095 /**
0096  * Attempts to extract the object that @c arg holds.
0097  * If casting to a pointer fails, \any_cast returns
0098  * a null pointer.  Casting to @c void* always succeeds
0099  * and returns the address of stored object.
0100  *
0101  * \code
0102  * any<mpl::vector<typeid_<>, copy_constructible<> > > x(1);
0103  * any_cast<int>(x);      // returns 1
0104  * any_cast<int&>(x);     // returns a reference to the contents of x
0105  * any_cast<double>(x);   // throws bad_any_cast
0106  * any_cast<int*>(&x);    // returns a pointer to the contents of x
0107  * any_cast<void*>(&x);   // returns a pointer to the contents of x
0108  * any_cast<double*>(&x); // returns NULL
0109  * \endcode
0110  *
0111  * \pre if @c arg is a pointer, @c T must be a pointer type.
0112  * \pre @c Concept must contain @ref typeid_ "typeid_<Tag>".
0113  *
0114  * \throws bad_any_cast if @c arg doesn't contain
0115  *         an object of type @c T and we're casting
0116  *         to a value or reference.
0117  */
0118 template<class T, class Concept, class Tag>
0119 T any_cast(any<Concept, Tag>& arg)
0120 {
0121     if(::boost::type_erasure::detail::check_any_cast<T>(arg)) {
0122         return *static_cast<
0123             typename ::boost::remove_reference<
0124                 typename ::boost::add_const<T>::type
0125             >::type*
0126         >(::boost::type_erasure::detail::get_pointer(arg));
0127     } else {
0128         BOOST_THROW_EXCEPTION(::boost::type_erasure::bad_any_cast());
0129     }
0130 }
0131 
0132 /** \overload */
0133 template<class T, class Concept, class Tag>
0134 T any_cast(const any<Concept, Tag>& arg)
0135 {
0136     if(::boost::type_erasure::detail::check_any_cast<T>(arg)) {
0137         return *static_cast<
0138             typename ::boost::remove_reference<
0139                 typename ::boost::add_const<T>::type
0140             >::type*
0141         >(::boost::type_erasure::detail::get_pointer(arg));
0142     } else {
0143         BOOST_THROW_EXCEPTION(::boost::type_erasure::bad_any_cast());
0144     }
0145 }
0146 
0147 /** \overload */
0148 template<class T, class Concept, class Tag>
0149 T any_cast(any<Concept, Tag>* arg)
0150 {
0151     BOOST_MPL_ASSERT((::boost::is_pointer<T>));
0152     if(::boost::type_erasure::detail::check_any_cast<
0153         typename ::boost::remove_pointer<T>::type>(*arg)) {
0154         return static_cast<
0155             typename ::boost::remove_pointer<T>::type*>(
0156                 ::boost::type_erasure::detail::get_pointer(*arg));
0157     } else {
0158         return 0;
0159     }
0160 }
0161 
0162 /** \overload */
0163 template<class T, class Concept, class Tag>
0164 T any_cast(const any<Concept, Tag>* arg)
0165 {
0166     BOOST_MPL_ASSERT((::boost::is_pointer<T>));
0167     if(::boost::type_erasure::detail::check_any_cast<
0168         typename ::boost::remove_pointer<T>::type>(*arg)) {
0169         return static_cast<
0170             typename ::boost::remove_pointer<T>::type*>(
0171                 ::boost::type_erasure::detail::get_pointer(*arg));
0172     } else {
0173         return 0;
0174     }
0175 }
0176 
0177 }
0178 }
0179 
0180 #endif