Update contrib.
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>
10 # include <boost/type_traits/same_traits.hpp>
11 # include <boost/type_traits/cv_traits.hpp>
12 # include <boost/type.hpp>
13 # include <boost/python/base_type_traits.hpp>
14 # include <boost/python/detail/convertible.hpp>
16 namespace boost { namespace python {
20 template <class Source, class Target> inline Target* upcast_impl(Source*, Target*);
22 template <class Source, class Target>
23 inline Target* upcast(Source* p, yes_convertible, no_convertible, Target*)
28 template <class Source, class Target>
29 inline Target* upcast(Source* p, no_convertible, no_convertible, Target*)
31 typedef typename base_type_traits<Source>::type base;
33 return detail::upcast_impl((base*)p, (Target*)0);
36 template <bool is_same = true>
40 static inline T* execute(T* x, T*) { return x; }
44 struct upcaster<false>
46 template <class Source, class Target>
47 static inline Target* execute(Source* x, Target*)
49 return detail::upcast(
50 x, detail::convertible<Target*>::check(x)
51 , detail::convertible<Source*>::check((Target*)0)
57 template <class Target, class Source>
58 inline Target* downcast(Source* p, yes_convertible)
60 return static_cast<Target*>(p);
63 template <class Target, class Source>
64 inline Target* downcast(Source* p, no_convertible, boost::type<Target>* = 0)
66 typedef typename base_type_traits<Source>::type base;
67 return (Target*)detail::downcast<base>(p, convertible<Source*>::check((base*)0));
71 inline void assert_castable(boost::type<T>* = 0)
73 typedef char must_be_a_complete_type[sizeof(T)];
76 template <class Source, class Target>
77 inline Target* upcast_impl(Source* x, Target*)
79 typedef typename add_cv<Source>::type src_t;
80 typedef typename add_cv<Target>::type target_t;
81 bool const same = is_same<src_t,target_t>::value;
83 return detail::upcaster<same>::execute(x, (Target*)0);
87 template <class Target, class Source>
88 inline Target* upcast(Source* x, Target* = 0)
90 detail::assert_castable<Source>();
91 detail::assert_castable<Target>();
92 return detail::upcast_impl(x, (Target*)0);
96 template <class Target, class Source>
97 inline Target* downcast(Source* x, Target* = 0)
99 detail::assert_castable<Source>();
100 detail::assert_castable<Target>();
101 return detail::downcast<Target>(x, detail::convertible<Source*>::check((Target*)0));
104 }} // namespace boost::python
106 #endif // CAST_DWA200269_HPP