cast.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. # include <boost/python/detail/prefix.hpp>
  8. # include <boost/type_traits/same_traits.hpp>
  9. # include <boost/type_traits/cv_traits.hpp>
  10. # include <boost/type.hpp>
  11. # include <boost/python/base_type_traits.hpp>
  12. # include <boost/python/detail/convertible.hpp>
  13. namespace boost { namespace python {
  14. namespace detail
  15. {
  16. template <class Source, class Target> inline Target* upcast_impl(Source*, Target*);
  17. template <class Source, class Target>
  18. inline Target* upcast(Source* p, yes_convertible, no_convertible, Target*)
  19. {
  20. return p;
  21. }
  22. template <class Source, class Target>
  23. inline Target* upcast(Source* p, no_convertible, no_convertible, Target*)
  24. {
  25. typedef typename base_type_traits<Source>::type base;
  26. return detail::upcast_impl((base*)p, (Target*)0);
  27. }
  28. template <bool is_same = true>
  29. struct upcaster
  30. {
  31. template <class T>
  32. static inline T* execute(T* x, T*) { return x; }
  33. };
  34. template <>
  35. struct upcaster<false>
  36. {
  37. template <class Source, class Target>
  38. static inline Target* execute(Source* x, Target*)
  39. {
  40. return detail::upcast(
  41. x, detail::convertible<Target*>::check(x)
  42. , detail::convertible<Source*>::check((Target*)0)
  43. , (Target*)0);
  44. }
  45. };
  46. template <class Target, class Source>
  47. inline Target* downcast(Source* p, yes_convertible)
  48. {
  49. return static_cast<Target*>(p);
  50. }
  51. template <class Target, class Source>
  52. inline Target* downcast(Source* p, no_convertible, boost::type<Target>* = 0)
  53. {
  54. typedef typename base_type_traits<Source>::type base;
  55. return (Target*)detail::downcast<base>(p, convertible<Source*>::check((base*)0));
  56. }
  57. template <class T>
  58. inline void assert_castable(boost::type<T>* = 0)
  59. {
  60. typedef char must_be_a_complete_type[sizeof(T)];
  61. }
  62. template <class Source, class Target>
  63. inline Target* upcast_impl(Source* x, Target*)
  64. {
  65. typedef typename add_cv<Source>::type src_t;
  66. typedef typename add_cv<Target>::type target_t;
  67. bool const same = is_same<src_t,target_t>::value;
  68. return detail::upcaster<same>::execute(x, (Target*)0);
  69. }
  70. }
  71. template <class Target, class Source>
  72. inline Target* upcast(Source* x, Target* = 0)
  73. {
  74. detail::assert_castable<Source>();
  75. detail::assert_castable<Target>();
  76. return detail::upcast_impl(x, (Target*)0);
  77. }
  78. template <class Target, class Source>
  79. inline Target* downcast(Source* x, Target* = 0)
  80. {
  81. detail::assert_castable<Source>();
  82. detail::assert_castable<Target>();
  83. return detail::downcast<Target>(x, detail::convertible<Source*>::check((Target*)0));
  84. }
  85. }} // namespace boost::python
  86. #endif // CAST_DWA200269_HPP