1 // Copyright David Abrahams 2002.
2 // Distributed under the Boost Software License, Version 1.0. (See
3 // accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5 #ifndef CAST_DWA200269_HPP
6 # define CAST_DWA200269_HPP
8 # include <boost/python/detail/prefix.hpp>
9 # include <boost/python/detail/type_traits.hpp>
11 # include <boost/type.hpp>
12 # include <boost/python/base_type_traits.hpp>
13 # include <boost/python/detail/convertible.hpp>
15 namespace boost { namespace python {
19 template <class Source, class Target> inline Target* upcast_impl(Source*, Target*);
21 template <class Source, class Target>
22 inline Target* upcast(Source* p, yes_convertible, no_convertible, Target*)
27 template <class Source, class Target>
28 inline Target* upcast(Source* p, no_convertible, no_convertible, Target*)
30 typedef typename base_type_traits<Source>::type base;
32 return detail::upcast_impl((base*)p, (Target*)0);
35 template <bool is_same = true>
39 static inline T* execute(T* x, T*) { return x; }
43 struct upcaster<false>
45 template <class Source, class Target>
46 static inline Target* execute(Source* x, Target*)
48 return detail::upcast(
49 x, detail::convertible<Target*>::check(x)
50 , detail::convertible<Source*>::check((Target*)0)
56 template <class Target, class Source>
57 inline Target* downcast(Source* p, yes_convertible)
59 return static_cast<Target*>(p);
62 template <class Target, class Source>
63 inline Target* downcast(Source* p, no_convertible, boost::type<Target>* = 0)
65 typedef typename base_type_traits<Source>::type base;
66 return (Target*)detail::downcast<base>(p, convertible<Source*>::check((base*)0));
70 inline void assert_castable(boost::type<T>* = 0)
72 typedef char must_be_a_complete_type[sizeof(T)] BOOST_ATTRIBUTE_UNUSED;
75 template <class Source, class Target>
76 inline Target* upcast_impl(Source* x, Target*)
78 typedef typename detail::add_cv<Source>::type src_t;
79 typedef typename detail::add_cv<Target>::type target_t;
80 bool const same = detail::is_same<src_t,target_t>::value;
82 return detail::upcaster<same>::execute(x, (Target*)0);
86 template <class Target, class Source>
87 inline Target* upcast(Source* x, Target* = 0)
89 detail::assert_castable<Source>();
90 detail::assert_castable<Target>();
91 return detail::upcast_impl(x, (Target*)0);
95 template <class Target, class Source>
96 inline Target* downcast(Source* x, Target* = 0)
98 detail::assert_castable<Source>();
99 detail::assert_castable<Target>();
100 return detail::downcast<Target>(x, detail::convertible<Source*>::check((Target*)0));
103 }} // namespace boost::python
105 #endif // CAST_DWA200269_HPP