cppx-core
downcasted_to_.hpp
Go to the documentation of this file.
1 #pragma once // Source encoding: UTF-8 with BOM (π is a lowercase Greek "pi").
3 #include <cppx-core/language/tmp/basic-Enable_if_.hpp> // cppx::Enable_if_
4 #include <cppx-core/language/tmp/basic-type-traits.hpp> // cppx::is_base_and_derived_
5 
6 namespace cppx
7 {
9  namespace impl
10  {
11  template< class Derived, class Type >
12  inline auto downcasted_to_( P_<Type> p )
13  -> P_<Derived>
14  {
15  const auto p_derived = dynamic_cast<P_<Derived>>( p );
16  return static_cast<P_<Type>>( p_derived ) == p? p_derived : nullptr;
17  }
18  }
20 
21  template<
22  class Derived,
23  class Type,
24  class = Enable_if_<is_base_and_derived_<Type, Derived>>
25  >
26  inline auto downcasted_to_( P_<Type> p )
27  -> P_<Derived>
28  { return impl::downcasted_to_<Derived>( p ); }
29 
30  // const
31  template<
32  class Derived,
33  class Type,
34  class = Enable_if_<is_base_and_derived_<Type, Derived>>
35  >
36  inline auto downcasted_to_( P_<const Type> p )
37  -> P_<const Derived>
38  { return impl::downcasted_to_<const Derived>( p ); }
39 } // namespace cppx
Some_type * P_
Creates a raw pointer type.
is_base_and_derived_, is_same_type_, is_a_, is_const_, is_class_, is_fixed_point_,...
Enable_if_ is just more readable than enable_if_t.
Simple type builders Type_, P_, R_, Raw_array_ and Raw_array_of_.
auto downcasted_to_(P_< Type > p) -> P_< Derived >