1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ossrv_pub/boost_apis/boost/python/cast.hpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,106 @@
1.4 +// Copyright David Abrahams 2002.
1.5 +// Distributed under the Boost Software License, Version 1.0. (See
1.6 +// accompanying file LICENSE_1_0.txt or copy at
1.7 +// http://www.boost.org/LICENSE_1_0.txt)
1.8 +#ifndef CAST_DWA200269_HPP
1.9 +# define CAST_DWA200269_HPP
1.10 +
1.11 +# include <boost/python/detail/prefix.hpp>
1.12 +
1.13 +# include <boost/type_traits/same_traits.hpp>
1.14 +# include <boost/type_traits/cv_traits.hpp>
1.15 +# include <boost/type.hpp>
1.16 +# include <boost/python/base_type_traits.hpp>
1.17 +# include <boost/python/detail/convertible.hpp>
1.18 +
1.19 +namespace boost { namespace python {
1.20 +
1.21 +namespace detail
1.22 +{
1.23 + template <class Source, class Target> inline Target* upcast_impl(Source*, Target*);
1.24 +
1.25 + template <class Source, class Target>
1.26 + inline Target* upcast(Source* p, yes_convertible, no_convertible, Target*)
1.27 + {
1.28 + return p;
1.29 + }
1.30 +
1.31 + template <class Source, class Target>
1.32 + inline Target* upcast(Source* p, no_convertible, no_convertible, Target*)
1.33 + {
1.34 + typedef typename base_type_traits<Source>::type base;
1.35 +
1.36 + return detail::upcast_impl((base*)p, (Target*)0);
1.37 + }
1.38 +
1.39 + template <bool is_same = true>
1.40 + struct upcaster
1.41 + {
1.42 + template <class T>
1.43 + static inline T* execute(T* x, T*) { return x; }
1.44 + };
1.45 +
1.46 + template <>
1.47 + struct upcaster<false>
1.48 + {
1.49 + template <class Source, class Target>
1.50 + static inline Target* execute(Source* x, Target*)
1.51 + {
1.52 + return detail::upcast(
1.53 + x, detail::convertible<Target*>::check(x)
1.54 + , detail::convertible<Source*>::check((Target*)0)
1.55 + , (Target*)0);
1.56 + }
1.57 + };
1.58 +
1.59 +
1.60 + template <class Target, class Source>
1.61 + inline Target* downcast(Source* p, yes_convertible)
1.62 + {
1.63 + return static_cast<Target*>(p);
1.64 + }
1.65 +
1.66 + template <class Target, class Source>
1.67 + inline Target* downcast(Source* p, no_convertible, boost::type<Target>* = 0)
1.68 + {
1.69 + typedef typename base_type_traits<Source>::type base;
1.70 + return (Target*)detail::downcast<base>(p, convertible<Source*>::check((base*)0));
1.71 + }
1.72 +
1.73 + template <class T>
1.74 + inline void assert_castable(boost::type<T>* = 0)
1.75 + {
1.76 + typedef char must_be_a_complete_type[sizeof(T)];
1.77 + }
1.78 +
1.79 + template <class Source, class Target>
1.80 + inline Target* upcast_impl(Source* x, Target*)
1.81 + {
1.82 + typedef typename add_cv<Source>::type src_t;
1.83 + typedef typename add_cv<Target>::type target_t;
1.84 + bool const same = is_same<src_t,target_t>::value;
1.85 +
1.86 + return detail::upcaster<same>::execute(x, (Target*)0);
1.87 + }
1.88 +}
1.89 +
1.90 +template <class Target, class Source>
1.91 +inline Target* upcast(Source* x, Target* = 0)
1.92 +{
1.93 + detail::assert_castable<Source>();
1.94 + detail::assert_castable<Target>();
1.95 + return detail::upcast_impl(x, (Target*)0);
1.96 +
1.97 +}
1.98 +
1.99 +template <class Target, class Source>
1.100 +inline Target* downcast(Source* x, Target* = 0)
1.101 +{
1.102 + detail::assert_castable<Source>();
1.103 + detail::assert_castable<Target>();
1.104 + return detail::downcast<Target>(x, detail::convertible<Source*>::check((Target*)0));
1.105 +}
1.106 +
1.107 +}} // namespace boost::python
1.108 +
1.109 +#endif // CAST_DWA200269_HPP