41 #ifndef UTIL_ACCESS_CASTED_H 42 #define UTIL_ACCESS_CASTED_H 46 #include <type_traits> 57 using std::remove_pointer;
58 using std::remove_reference;
62 using PlainType =
typename remove_pointer<
63 typename remove_reference<T>::type>::type;
68 static constexpr
bool value = std::is_polymorphic<PlainType<T>>::value;
71 template <
typename SRC,
typename TAR>
74 static constexpr
bool value = std::is_base_of<PlainType<SRC>, PlainType<TAR>>::value
75 && ( ( std::is_pointer<typename remove_reference<SRC>::type>::value
76 && std::is_pointer<typename remove_reference<TAR>::type>::value
78 ||( !std::is_pointer<
typename remove_reference<SRC>::type>::value
79 && !std::is_pointer<
typename remove_reference<TAR>::type>::value
83 template <
typename SRC,
typename TAR>
86 static constexpr
bool value = !std::is_convertible<SRC,TAR>::value
92 template <
typename SRC,
typename TAR>
94 : std::is_convertible<SRC,TAR>
97 template <
typename SRC,
typename TAR>
100 static constexpr
bool value = !std::is_rvalue_reference<SRC>::value
101 && !std::is_pointer<typename remove_reference<SRC>::type>::value
102 && std::is_pointer<typename remove_reference<TAR>::type>::value;
105 template <
typename SRC,
typename TAR>
108 static constexpr
bool value = !std::is_pointer<typename remove_reference<TAR>::type>::value
109 && std::is_pointer<typename remove_reference<SRC>::type>::value;
113 template <
typename SRC,
typename TAR>
115 : std::enable_if< can_use_dynamic_downcast<SRC,TAR>::value, TAR>
118 template <
typename SRC,
typename TAR>
120 : std::enable_if< can_use_conversion<SRC,TAR>::value, TAR>
123 template <
typename SRC,
typename TAR>
125 : std::enable_if< can_take_address<SRC,TAR>::value, TAR>
128 template <
typename SRC,
typename TAR>
130 : std::enable_if< can_dereference<SRC,TAR>::value, TAR>
142 template<
typename TAR>
148 template<
typename SRC>
149 static typename if_can_use_dynamic_downcast<SRC&&,TAR>::type
154 return dynamic_cast<TAR
> (std::forward<SRC>(elem));
156 catch (std::bad_cast& castError)
159 ,
"AccessCasted: not the expected runtime type; downcast failed" 160 ,error::LUMIERA_ERROR_WRONG_TYPE);
165 template<
typename SRC>
166 static typename if_can_use_conversion<SRC&&,TAR>::type
169 return std::forward<SRC> (elem);
173 template<
typename SRC>
174 static typename if_can_take_address<SRC&&,TAR>::type
181 template<
typename SRC>
182 static typename if_can_dereference<SRC&&,TAR>::type
186 throw error::Invalid(
"AccessCasted: attempt to build a value or reference from a NULL pointer" 187 ,error::LUMIERA_ERROR_BOTTOM_VALUE);
200 static_assert (!
sizeof(TAR),
"AccessCasted: No valid conversion or cast supported for these types.");
201 throw error::Invalid(
"impossible or unsafe type conversion requested");
Helper template to access a given value, possibly converted or casted in a safe way.
static TAR access(...)
catch-all to signal failure of conversion
Derived specific exceptions within Lumiera's exception hierarchy.
Lumiera error handling (C++ interface).