sl@0: // Copyright David Abrahams 2002. sl@0: // Distributed under the Boost Software License, Version 1.0. (See sl@0: // accompanying file LICENSE_1_0.txt or copy at sl@0: // http://www.boost.org/LICENSE_1_0.txt) sl@0: #ifndef CAST_DWA200269_HPP sl@0: # define CAST_DWA200269_HPP sl@0: sl@0: # include sl@0: sl@0: # include sl@0: # include sl@0: # include sl@0: # include sl@0: # include sl@0: sl@0: namespace boost { namespace python { sl@0: sl@0: namespace detail sl@0: { sl@0: template inline Target* upcast_impl(Source*, Target*); sl@0: sl@0: template sl@0: inline Target* upcast(Source* p, yes_convertible, no_convertible, Target*) sl@0: { sl@0: return p; sl@0: } sl@0: sl@0: template sl@0: inline Target* upcast(Source* p, no_convertible, no_convertible, Target*) sl@0: { sl@0: typedef typename base_type_traits::type base; sl@0: sl@0: return detail::upcast_impl((base*)p, (Target*)0); sl@0: } sl@0: sl@0: template sl@0: struct upcaster sl@0: { sl@0: template sl@0: static inline T* execute(T* x, T*) { return x; } sl@0: }; sl@0: sl@0: template <> sl@0: struct upcaster sl@0: { sl@0: template sl@0: static inline Target* execute(Source* x, Target*) sl@0: { sl@0: return detail::upcast( sl@0: x, detail::convertible::check(x) sl@0: , detail::convertible::check((Target*)0) sl@0: , (Target*)0); sl@0: } sl@0: }; sl@0: sl@0: sl@0: template sl@0: inline Target* downcast(Source* p, yes_convertible) sl@0: { sl@0: return static_cast(p); sl@0: } sl@0: sl@0: template sl@0: inline Target* downcast(Source* p, no_convertible, boost::type* = 0) sl@0: { sl@0: typedef typename base_type_traits::type base; sl@0: return (Target*)detail::downcast(p, convertible::check((base*)0)); sl@0: } sl@0: sl@0: template sl@0: inline void assert_castable(boost::type* = 0) sl@0: { sl@0: typedef char must_be_a_complete_type[sizeof(T)]; sl@0: } sl@0: sl@0: template sl@0: inline Target* upcast_impl(Source* x, Target*) sl@0: { sl@0: typedef typename add_cv::type src_t; sl@0: typedef typename add_cv::type target_t; sl@0: bool const same = is_same::value; sl@0: sl@0: return detail::upcaster::execute(x, (Target*)0); sl@0: } sl@0: } sl@0: sl@0: template sl@0: inline Target* upcast(Source* x, Target* = 0) sl@0: { sl@0: detail::assert_castable(); sl@0: detail::assert_castable(); sl@0: return detail::upcast_impl(x, (Target*)0); sl@0: sl@0: } sl@0: sl@0: template sl@0: inline Target* downcast(Source* x, Target* = 0) sl@0: { sl@0: detail::assert_castable(); sl@0: detail::assert_castable(); sl@0: return detail::downcast(x, detail::convertible::check((Target*)0)); sl@0: } sl@0: sl@0: }} // namespace boost::python sl@0: sl@0: #endif // CAST_DWA200269_HPP