os/ossrv/ossrv_pub/boost_apis/boost/python/cast.hpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright David Abrahams 2002.
sl@0
     2
// Distributed under the Boost Software License, Version 1.0. (See
sl@0
     3
// accompanying file LICENSE_1_0.txt or copy at
sl@0
     4
// http://www.boost.org/LICENSE_1_0.txt)
sl@0
     5
#ifndef CAST_DWA200269_HPP
sl@0
     6
# define CAST_DWA200269_HPP
sl@0
     7
sl@0
     8
# include <boost/python/detail/prefix.hpp>
sl@0
     9
sl@0
    10
# include <boost/type_traits/same_traits.hpp>
sl@0
    11
# include <boost/type_traits/cv_traits.hpp>
sl@0
    12
# include <boost/type.hpp>
sl@0
    13
# include <boost/python/base_type_traits.hpp>
sl@0
    14
# include <boost/python/detail/convertible.hpp>
sl@0
    15
sl@0
    16
namespace boost { namespace python { 
sl@0
    17
sl@0
    18
namespace detail
sl@0
    19
{
sl@0
    20
  template <class Source, class Target> inline Target* upcast_impl(Source*, Target*);
sl@0
    21
  
sl@0
    22
  template <class Source, class Target>
sl@0
    23
  inline Target* upcast(Source* p, yes_convertible, no_convertible, Target*)
sl@0
    24
  {
sl@0
    25
      return p;
sl@0
    26
  }
sl@0
    27
sl@0
    28
  template <class Source, class Target>
sl@0
    29
  inline Target* upcast(Source* p, no_convertible, no_convertible, Target*)
sl@0
    30
  {
sl@0
    31
      typedef typename base_type_traits<Source>::type base;
sl@0
    32
      
sl@0
    33
      return detail::upcast_impl((base*)p, (Target*)0);
sl@0
    34
  }
sl@0
    35
sl@0
    36
  template <bool is_same = true>
sl@0
    37
  struct upcaster
sl@0
    38
  {
sl@0
    39
      template <class T>
sl@0
    40
      static inline T* execute(T* x, T*) { return x; }
sl@0
    41
  };
sl@0
    42
  
sl@0
    43
  template <>
sl@0
    44
  struct upcaster<false>
sl@0
    45
  {
sl@0
    46
      template <class Source, class Target>
sl@0
    47
      static inline Target* execute(Source* x, Target*)
sl@0
    48
      {
sl@0
    49
          return detail::upcast(
sl@0
    50
              x, detail::convertible<Target*>::check(x)
sl@0
    51
              , detail::convertible<Source*>::check((Target*)0)
sl@0
    52
              , (Target*)0);
sl@0
    53
      }
sl@0
    54
  };
sl@0
    55
sl@0
    56
sl@0
    57
  template <class Target, class Source>
sl@0
    58
  inline Target* downcast(Source* p, yes_convertible)
sl@0
    59
  {
sl@0
    60
      return static_cast<Target*>(p);
sl@0
    61
  }
sl@0
    62
sl@0
    63
  template <class Target, class Source>
sl@0
    64
  inline Target* downcast(Source* p, no_convertible, boost::type<Target>* = 0)
sl@0
    65
  {
sl@0
    66
      typedef typename base_type_traits<Source>::type base;
sl@0
    67
      return (Target*)detail::downcast<base>(p, convertible<Source*>::check((base*)0));
sl@0
    68
  }
sl@0
    69
sl@0
    70
  template <class T>
sl@0
    71
  inline void assert_castable(boost::type<T>* = 0)
sl@0
    72
  {
sl@0
    73
      typedef char must_be_a_complete_type[sizeof(T)];
sl@0
    74
  }
sl@0
    75
sl@0
    76
  template <class Source, class Target>
sl@0
    77
  inline Target* upcast_impl(Source* x, Target*)
sl@0
    78
  {
sl@0
    79
      typedef typename add_cv<Source>::type src_t;
sl@0
    80
      typedef typename add_cv<Target>::type target_t;
sl@0
    81
      bool const same = is_same<src_t,target_t>::value;
sl@0
    82
      
sl@0
    83
      return detail::upcaster<same>::execute(x, (Target*)0);
sl@0
    84
  }
sl@0
    85
}
sl@0
    86
sl@0
    87
template <class Target, class Source>
sl@0
    88
inline Target* upcast(Source* x, Target* = 0)
sl@0
    89
{
sl@0
    90
    detail::assert_castable<Source>();
sl@0
    91
    detail::assert_castable<Target>();
sl@0
    92
    return detail::upcast_impl(x, (Target*)0);
sl@0
    93
    
sl@0
    94
}
sl@0
    95
sl@0
    96
template <class Target, class Source>
sl@0
    97
inline Target* downcast(Source* x, Target* = 0)
sl@0
    98
{
sl@0
    99
    detail::assert_castable<Source>();
sl@0
   100
    detail::assert_castable<Target>();
sl@0
   101
    return detail::downcast<Target>(x, detail::convertible<Source*>::check((Target*)0));
sl@0
   102
}
sl@0
   103
sl@0
   104
}} // namespace boost::python
sl@0
   105
sl@0
   106
#endif // CAST_DWA200269_HPP