XRTraits C++ OpenXR Utilities
APITraits.h
Go to the documentation of this file.
1 // Copyright 2018-2019, Collabora, Ltd.
2 // SPDX-License-Identifier: BSL-1.0
3 /*!
4  * @file
5  * @brief C++ type traits related to OpenXR, and some other compile-time
6  * functionality.
7  *
8  * Includes a generated header that defines most of these based on the registry.
9  *
10  * @author Ryan Pavlik <ryan.pavlik@collabora.com>
11  */
12 
13 #pragma once
14 
15 // Internal Includes
16 #ifdef __GNUC__
17 #pragma GCC diagnostic push
18 #pragma GCC diagnostic ignored "-Wpragmas"
19 #pragma GCC diagnostic ignored "-Wswitch-enum"
20 #endif
21 
22 #include "xrtraits/traits/APITraitsImpl.h" // Generated header
23 
24 #ifdef __GNUC__
25 #pragma GCC diagnostic pop
26 #endif
27 
28 #include "xrtraits/Common.h"
29 
30 // Library Includes
31 #include <openxr/openxr.h>
32 #include <openxr/openxr_reflection.h>
33 
34 // Standard Includes
35 #include <type_traits>
36 #include <utility>
37 
38 namespace xrtraits {
39 
40 namespace traits {
41 
42 #ifndef XRTRAITS_DOXYGEN
43  namespace detail {
44  using std::remove_const_t;
45  using std::remove_cv_t;
46  using std::remove_pointer_t;
47  using std::remove_reference_t;
48  /*! Removes reference and pointer qualifications (in order,
49  * outside-in).
50  */
51  template <typename T>
52  using remove_reference_pointer_t =
53  remove_pointer_t<remove_reference_t<T>>;
54  /*! Removes reference, pointer, and const/volatile
55  * qualifications (in order, outside-in).
56  */
57  template <typename T>
58  using remove_reference_pointer_cv_t =
59  remove_cv_t<remove_reference_pointer_t<T>>;
60 
61  template <typename T>
62  constexpr bool has_const_next_pointer_v =
63  std::is_const<remove_pointer_t<decltype(T::next)>>::value;
64 
65 #define XRTRAITS_MAKE_STRINGIFY_CASE(SYM, _) \
66  case SYM: \
67  return #SYM;
68  constexpr const char*
69  structureTypeEnumToString(XrStructureType tag)
70  {
71  // Awkward workaround: we shouldn't stringify the
72  // MAX_ENUM
73  if (tag == XR_STRUCTURE_TYPE_MAX_ENUM) {
74  return nullptr;
75  }
76  switch (tag) {
77 
78  XR_LIST_ENUM_XrStructureType(
79  XRTRAITS_MAKE_STRINGIFY_CASE);
80  default:
81  return nullptr;
82  }
83  }
84 #undef XRTRAITS_MAKE_STRINGIFY_CASE
85  } // namespace detail
86 
87 #endif // !XRTRAITS_DOXYGEN
88 
89  /*! true if the given type is an XR tagged type (starting with
90  * `XrStructureType type; const void* next;`).
91  *
92  * Note that this does *not* imply that there is a (known)
93  * XrStructureType enum value (for use in the `type` member) that
94  * corresponds to T. For that, @see has_xr_type_tag_v
95  *
96  * Removes pointer, reference, and const from type before looking it
97  * up.
98  */
99  template <typename T>
100  constexpr bool is_xr_tagged_type_v = detail::is_xr_tagged_type_impl_v<
101  detail::remove_reference_pointer_cv_t<T>>;
102 
103  /*! For types that are XR tagged types and have a defined enum value
104  * (type tag) for their type member, this is true.
105  *
106  * Note: Not all types with is_xr_tagged_type_v have their own type tag:
107  * some (that primarily serve as "abstract" types) do not but are
108  * intended primarily as ways to pass references to concrete types that
109  * do have a defined type tag.
110  *
111  * Removes pointer, reference, and const from type before looking it
112  * up.
113  */
114  template <typename T>
115  constexpr bool has_xr_type_tag_v = detail::has_xr_type_tag_impl_v<
116  detail::remove_reference_pointer_cv_t<T>>;
117 
118  /*! Access the defined enum value (type tag) for the type member of an
119  * XR tagged type with known tag.
120  *
121  * Removes pointer, reference, and const from type before looking it
122  * up.
123  */
124  template <typename T>
125  constexpr XrStructureType xr_type_tag_v = detail::xr_type_tag_impl_v<
126  detail::remove_reference_pointer_cv_t<T>>;
127 
128  /*! Identify if a tagged type has a const next pointer.
129  *
130  * Removes pointer, reference, and const from type before looking it
131  * up.
132  */
133  template <typename T>
134  constexpr bool has_const_next_pointer_v =
136  detail::remove_reference_pointer_cv_t<T>>;
137 
138  /*! Get a string corresponding to a type enum value, or nullptr if not
139  * recognized.
140  */
141  constexpr const char* to_string(XrStructureType t)
142  {
143  return detail::structureTypeEnumToString(t);
144  }
145 
146  /*! Get the type name string associated with a type enum value, or
147  * nullptr if not recognized.
148  */
149  constexpr const char* getTypeName(XrStructureType t)
150  {
151  return detail::structureTypeEnumToTypeNameString(t);
152  }
153 
154  //! Do SourceType and TargetType have the same const-qualifications?
155  template <typename SourceType, typename TargetType>
156  constexpr bool same_constness = (is_const_v<SourceType> ==
157  is_const_v<TargetType>);
158 
159 } // namespace traits
160 
161 } // namespace xrtraits
Main namespace for these C++ OpenXR utilities.
Definition: GetChained.h:26
constexpr XrStructureType xr_type_tag_v
Definition: APITraits.h:125
constexpr bool is_xr_tagged_type_v
Definition: APITraits.h:100
constexpr bool has_xr_type_tag_v
Definition: APITraits.h:115
constexpr const char * to_string(XrStructureType t)
Definition: APITraits.h:141
Header with common utilities used by multiple headers.
constexpr bool has_const_next_pointer_v
Definition: APITraits.h:134
constexpr const char * getTypeName(XrStructureType t)
Definition: APITraits.h:149
constexpr bool same_constness
Do SourceType and TargetType have the same const-qualifications?
Definition: APITraits.h:156