50 #ifndef UTIL_ACCESS_CASTED_H 51 #define UTIL_ACCESS_CASTED_H 55 #include <type_traits> 66 using std::remove_pointer;
67 using std::remove_reference;
71 using PlainType =
typename remove_pointer<
72 typename remove_reference<T>::type>::type;
77 static constexpr
bool value = std::is_polymorphic<PlainType<T>>::value;
80 template <
typename SRC,
typename TAR>
83 static constexpr
bool value = std::is_base_of<PlainType<SRC>, PlainType<TAR>>::value
84 && ( ( std::is_pointer<typename remove_reference<SRC>::type>::value
85 && std::is_pointer<typename remove_reference<TAR>::type>::value
87 ||( !std::is_pointer<
typename remove_reference<SRC>::type>::value
88 && !std::is_pointer<
typename remove_reference<TAR>::type>::value
92 template <
typename SRC,
typename TAR>
95 static constexpr
bool value = !std::is_convertible<SRC,TAR>::value
101 template <
typename SRC,
typename TAR>
103 : std::is_convertible<SRC,TAR>
106 template <
typename SRC,
typename TAR>
109 static constexpr
bool value = !std::is_rvalue_reference<SRC>::value
110 && !std::is_pointer<typename remove_reference<SRC>::type>::value
111 && std::is_pointer<typename remove_reference<TAR>::type>::value;
114 template <
typename SRC,
typename TAR>
117 static constexpr
bool value = !std::is_pointer<typename remove_reference<TAR>::type>::value
118 && std::is_pointer<typename remove_reference<SRC>::type>::value;
122 template <
typename SRC,
typename TAR>
124 : std::enable_if< can_use_dynamic_downcast<SRC,TAR>::value, TAR>
127 template <
typename SRC,
typename TAR>
129 : std::enable_if< can_use_conversion<SRC,TAR>::value, TAR>
132 template <
typename SRC,
typename TAR>
134 : std::enable_if< can_take_address<SRC,TAR>::value, TAR>
137 template <
typename SRC,
typename TAR>
139 : std::enable_if< can_dereference<SRC,TAR>::value, TAR>
151 template<
typename TAR>
157 template<
typename SRC>
158 static typename if_can_use_dynamic_downcast<SRC&&,TAR>::type
163 return dynamic_cast<TAR
> (std::forward<SRC>(elem));
165 catch (std::bad_cast& castError)
168 ,
"AccessCasted: not the expected runtime type; downcast failed" 169 ,error::LUMIERA_ERROR_WRONG_TYPE);
174 template<
typename SRC>
175 static typename if_can_use_conversion<SRC&&,TAR>::type
178 return std::forward<SRC> (elem);
182 template<
typename SRC>
183 static typename if_can_take_address<SRC&&,TAR>::type
190 template<
typename SRC>
191 static typename if_can_dereference<SRC&&,TAR>::type
195 throw error::Invalid(
"AccessCasted: attempt to build a value or reference from a NULL pointer" 196 ,error::LUMIERA_ERROR_BOTTOM_VALUE);
209 static_assert (!
sizeof(TAR),
"AccessCasted: No valid conversion or cast supported for these types.");
210 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).