• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
7 
8 # include <boost/python/detail/prefix.hpp>
9 # include <boost/python/detail/type_traits.hpp>
10 
11 # include <boost/type.hpp>
12 # include <boost/python/base_type_traits.hpp>
13 # include <boost/python/detail/convertible.hpp>
14 
15 namespace boost { namespace python {
16 
17 namespace detail
18 {
19   template <class Source, class Target> inline Target* upcast_impl(Source*, Target*);
20 
21   template <class Source, class Target>
upcast(Source * p,yes_convertible,no_convertible,Target *)22   inline Target* upcast(Source* p, yes_convertible, no_convertible, Target*)
23   {
24       return p;
25   }
26 
27   template <class Source, class Target>
upcast(Source * p,no_convertible,no_convertible,Target *)28   inline Target* upcast(Source* p, no_convertible, no_convertible, Target*)
29   {
30       typedef typename base_type_traits<Source>::type base;
31 
32       return detail::upcast_impl((base*)p, (Target*)0);
33   }
34 
35   template <bool is_same = true>
36   struct upcaster
37   {
38       template <class T>
executeboost::python::detail::upcaster39       static inline T* execute(T* x, T*) { return x; }
40   };
41 
42   template <>
43   struct upcaster<false>
44   {
45       template <class Source, class Target>
executeboost::python::detail::upcaster46       static inline Target* execute(Source* x, Target*)
47       {
48           return detail::upcast(
49               x, detail::convertible<Target*>::check(x)
50               , detail::convertible<Source*>::check((Target*)0)
51               , (Target*)0);
52       }
53   };
54 
55 
56   template <class Target, class Source>
downcast(Source * p,yes_convertible)57   inline Target* downcast(Source* p, yes_convertible)
58   {
59       return static_cast<Target*>(p);
60   }
61 
62   template <class Target, class Source>
downcast(Source * p,no_convertible,boost::type<Target> * =0)63   inline Target* downcast(Source* p, no_convertible, boost::type<Target>* = 0)
64   {
65       typedef typename base_type_traits<Source>::type base;
66       return (Target*)detail::downcast<base>(p, convertible<Source*>::check((base*)0));
67   }
68 
69   template <class T>
assert_castable(boost::type<T> * =0)70   inline void assert_castable(boost::type<T>* = 0)
71   {
72       typedef char must_be_a_complete_type[sizeof(T)] BOOST_ATTRIBUTE_UNUSED;
73   }
74 
75   template <class Source, class Target>
upcast_impl(Source * x,Target *)76   inline Target* upcast_impl(Source* x, Target*)
77   {
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;
81 
82       return detail::upcaster<same>::execute(x, (Target*)0);
83   }
84 }
85 
86 template <class Target, class Source>
upcast(Source * x,Target * =0)87 inline Target* upcast(Source* x, Target* = 0)
88 {
89     detail::assert_castable<Source>();
90     detail::assert_castable<Target>();
91     return detail::upcast_impl(x, (Target*)0);
92 
93 }
94 
95 template <class Target, class Source>
downcast(Source * x,Target * =0)96 inline Target* downcast(Source* x, Target* = 0)
97 {
98     detail::assert_castable<Source>();
99     detail::assert_castable<Target>();
100     return detail::downcast<Target>(x, detail::convertible<Source*>::check((Target*)0));
101 }
102 
103 }} // namespace boost::python
104 
105 #endif // CAST_DWA200269_HPP
106