14 #include "../Common.h" 16 #ifdef XRTRAITS_HAVE_CONSTEXPR_IF 19 #include "../Attributes.h" 20 #include "../exceptions/CastExceptions.h" 32 #ifndef XRTRAITS_DOXYGEN 34 using ::std::conditional_t;
35 using ::std::enable_if_t;
36 using ::std::is_const;
37 using ::std::is_pointer;
38 using ::std::is_reference;
40 using ::std::remove_pointer_t;
45 template <
typename SourceType>
46 inline auto cast_to_base(SourceType source)
49 is_same_v<SourceType, const void*> ||
50 is_same_v<SourceType, void*>,
51 "cast_to_base is only for void* or const void*");
53 if constexpr (points_to_const_v<SourceType>) {
55 return reinterpret_cast< 56 XrBaseInStructure const*
>(source);
59 return reinterpret_cast<XrBaseOutStructure*
>(
67 template <
typename SourceType>
68 inline XrStructureType risky_get_type(SourceType source)
71 is_same_v<SourceType, const void*> ||
72 is_same_v<SourceType, void*>,
73 "risky_get_type is only for void* or const void*");
74 return cast_to_base(source)->type;
78 #endif // !XRTRAITS_DOXYGEN 109 template <
typename TargetType,
typename SourceType>
112 using Tgt = TargetType;
117 is_same_v<detail::remove_reference_pointer_cv_t<SourceType>,
119 "Can only use xr_tagged_risky_cast to cast from a " 120 "pointer to (possibly const) void. If you have something " 121 "with real type data, you are looking for " 122 "xr_tagged_dynamic_cast.");
125 traits::is_xr_tagged_type_v<Tgt>,
126 "Can only use xr_tagged_risky_cast to cast to a " 127 "pointer/reference to a (possibly const) XR tagged type.");
135 static_assert(traits::has_xr_type_tag_v<Tgt>,
136 "Can only use xr_tagged_risky_cast to cast to a " 137 "pointer/reference to a concrete XR tagged type " 138 "with a known, defined type tag value " 139 "(XrStructureType enum value).");
141 static_assert(detail::points_or_refers_to_const_v<SourceType>
142 ? detail::points_or_refers_to_const_v<Tgt>
144 "Cannot remove const qualification through " 145 "xr_tagged_risky_cast");
147 #ifndef XRTRAITS_USE_EXCEPTIONS 148 static_assert(is_pointer_v<Tgt>,
149 "Casts to references require the availability of " 150 "xrtraits exceptions.");
151 #endif // !XRTRAITS_USE_EXCEPTIONS 154 "xr_tagged_dynamic_cast";
155 constexpr
bool is_target_pointer = is_pointer_v<Tgt>;
158 if (source ==
nullptr) {
159 if constexpr (is_target_pointer) {
165 #ifdef XRTRAITS_USE_EXCEPTIONS 167 traits::xr_type_tag_v<Tgt>,
nullptr,
169 #endif // XRTRAITS_USE_EXCEPTIONS 173 if (traits::xr_type_tag_v<Tgt> ==
174 detail::risky_get_type(source)) {
177 return detail::perform_dereferencing_reinterpret_cast<
182 if constexpr (is_target_pointer) {
188 #ifdef XRTRAITS_USE_EXCEPTIONS 190 traits::xr_type_tag_v<Tgt>,
191 detail::risky_get_type(source), castType);
192 #endif // XRTRAITS_USE_EXCEPTIONS 200 #endif // XRTRAITS_HAVE_CONSTEXPR_IF Main namespace for these C++ OpenXR utilities.
Definition: GetChained.h:26
#define XRTRAITS_MAYBE_UNUSED
Compatibility wrapper for [[maybe_unused]]
Definition: Attributes.h:26
Definition: CastExceptions.h:38
constexpr bool is_same_v
Variable template wrapping std::is_same<T, U>::value.
Definition: Common.h:34
Header shared between the two type-safe cast headers.
TargetType xr_tagged_risky_cast(SourceType source)
Definition: TaggedRiskyCast.h:110