42#ifndef UTIL_ACCESS_CASTED_H
43#define UTIL_ACCESS_CASTED_H
58 using std::remove_pointer;
59 using std::remove_reference;
64 typename remove_reference<T>::type>::type;
69 static constexpr bool value = std::is_polymorphic<PlainType<T>>::value;
72 template <
typename SRC,
typename TAR>
75 static constexpr bool value = std::is_base_of<PlainType<SRC>,
PlainType<TAR>>::value
76 && ( ( std::is_pointer<typename remove_reference<SRC>::type>::value
77 && std::is_pointer<typename remove_reference<TAR>::type>::value
79 ||( !std::is_pointer<
typename remove_reference<SRC>::type>::value
80 && !std::is_pointer<
typename remove_reference<TAR>::type>::value
84 template <
typename SRC,
typename TAR>
87 static constexpr bool value = !std::is_convertible<SRC,TAR>::value
93 template <
typename SRC,
typename TAR>
95 : std::is_convertible<SRC,TAR>
98 template <
typename SRC,
typename TAR>
101 static constexpr bool value = !std::is_rvalue_reference<SRC>::value
102 && !std::is_pointer<typename remove_reference<SRC>::type>::value
103 && std::is_pointer<typename remove_reference<TAR>::type>::value;
106 template <
typename SRC,
typename TAR>
109 static constexpr bool value = !std::is_pointer<typename remove_reference<TAR>::type>::value
110 && std::is_pointer<typename remove_reference<SRC>::type>::value;
114 template <
typename SRC,
typename TAR>
116 : std::enable_if< can_use_dynamic_downcast<SRC,TAR>::value, TAR>
119 template <
typename SRC,
typename TAR>
121 : std::enable_if< can_use_conversion<SRC,TAR>::value, TAR>
124 template <
typename SRC,
typename TAR>
126 : std::enable_if< can_take_address<SRC,TAR>::value, TAR>
129 template <
typename SRC,
typename TAR>
131 : std::enable_if< can_dereference<SRC,TAR>::value, TAR>
143 template<
typename TAR>
149 template<
typename SRC>
150 static typename if_can_use_dynamic_downcast<SRC&&,TAR>::type
155 return dynamic_cast<TAR
> (std::forward<SRC>(elem));
157 catch (std::bad_cast& castError)
160 ,
"AccessCasted: not the expected runtime type; downcast failed"
161 ,error::LUMIERA_ERROR_WRONG_TYPE);
166 template<
typename SRC>
167 static typename if_can_use_conversion<SRC&&,TAR>::type
170 return std::forward<SRC> (elem);
174 template<
typename SRC>
175 static typename if_can_take_address<SRC&&,TAR>::type
182 template<
typename SRC>
183 static typename if_can_dereference<SRC&&,TAR>::type
187 throw error::Invalid(
"AccessCasted: attempt to build a value or reference from a NULL pointer"
188 ,error::LUMIERA_ERROR_BOTTOM_VALUE);
201 static_assert (!
sizeof(TAR),
"AccessCasted: No valid conversion or cast supported for these types.");
202 throw error::Invalid(
"impossible or unsafe type conversion requested");
Lumiera error handling (C++ interface).
LumieraError< LERR_(INVALID)> Invalid
typename remove_pointer< typename remove_reference< T >::type >::type PlainType
Helper template to access a given value, possibly converted or casted in a safe way.
static if_can_use_conversion< SRC &&, TAR >::type access(SRC &&elem)
static if_can_use_dynamic_downcast< SRC &&, TAR >::type access(SRC &&elem)
static if_can_dereference< SRC &&, TAR >::type access(SRC &&elem)
static TAR access(...)
catch-all to signal failure of conversion
static if_can_take_address< SRC &&, TAR >::type access(SRC &&elem)