12#include "detray/definitions/detail/qualifiers.hpp"
13#include "detray/definitions/indexing.hpp"
14#include "detray/utils/concepts.hpp"
15#include "detray/utils/invalid_values.hpp"
16#include "detray/utils/logging.hpp"
17#include "detray/utils/type_list.hpp"
32template <concepts::type_id ID,
typename... registered_types>
42 n_types =
sizeof...(registered_types),
43 e_any =
sizeof...(registered_types),
44 e_unknown =
sizeof...(registered_types) + 1,
52 DETRAY_HOST_DEVICE
static constexpr bool is_valid(
53 const std::size_t type_idx) {
54 return type_idx < n_types;
60 template <
typename id_t =
id>
61 requires(!std::convertible_to<id_t, std::size_t>)
62 DETRAY_HOST_DEVICE
static constexpr bool is_valid(
const id_t type_id) {
63 return static_cast<std::size_t
>(type_id) < n_types;
88template <
typename orig_types_t,
class type_selector_t>
89DETRAY_DEVICE_CONSTANT
constexpr auto index_map_v =
90 detray::types::filtered_indices<orig_types_t, type_selector_t>(
101template <concepts::type_registry registry_t,
class type_selector_t>
105 using id =
typename registry_t::id;
110 decltype(detray::types::filtered_list<orig_types, type_selector_t>());
114 n_types = detray::types::size<type_list>,
115 e_any = detray::types::size<type_list>,
116 e_unknown = detray::types::size<type_list> + 1u,
126 const std::size_t filtered_type_idx) {
127 return filtered_type_idx < n_types;
133 template <
typename id_t =
id>
134 requires(!std::convertible_to<id_t, std::size_t>)
135 DETRAY_HOST_DEVICE
static constexpr bool is_valid(
const id_t orig_id) {
136 if (
static_cast<std::size_t
>(orig_id) >=
index_map().size()) {
147 return detray::types::filtered_indices<orig_types, type_selector_t>(
156 static constexpr std::size_t
mapped_index(
const std::size_t orig_idx) {
157 return index_map_v<orig_types, type_selector_t>[orig_idx];
165 return index_map_v<orig_types, type_selector_t>[
static_cast<std::size_t
>(
174 for (std::size_t i : index_map_v<orig_types, type_selector_t>) {
176 assert(registry_t::is_valid(j));
181 return detray::detail::invalid_value<std::size_t>();
213 : std::integral_constant<std::size_t, sizeof...(Ts)> {};
215template <
typename R,
class S>
217 : std::integral_constant<std::size_t, mapped_registry<R, S>::n_types> {};
225 :
public contains_impl<T, I, typename registry<ID, Ts...>::type_list> {};
229template <
typename T, std::
size_t I,
typename R,
class S>
245 static constexpr bool value = base_t::value;
250 std::same_as<type_list_t, orig_list_t>
257 static constexpr std::size_t
pos =
258 !base_t::value ? std::numeric_limits<std::size_t>::max() :
mapped_idx;
269 "Index out of range in type registry 'at'");
276template <std::
size_t N,
typename R,
class S>
279 "Index out of range in mapped type registry 'at'");
291template <
typename =
void,
typename =
void>
299 static_cast<ID
>(position<
typename registry<ID, Ts...>::type_list, T>);
301 static_assert(contains<
typename registry<ID, Ts...>::type_list, T>,
302 "Unknown type in 'get id' for type registry");
304 "Unmatched type ID in type registry 'id'");
309template <
typename T,
typename R,
class S>
323 static_assert(std::same_as<type_list_t, orig_list_t> ||
324 (std::same_as<type_list_t, filt_list_t> &&
325 contains<filt_list_t, T>),
326 "Unknown type in 'get id' for mapped type registry");
332 contains<orig_list_t, T>
333 ?
static_cast<id_t>(position<orig_list_t, T>)
337template <
typename R,
typename T>
343template <std::
size_t I,
typename =
void>
347template <std::size_t I,
typename ID,
typename... Ts>
350 "Unmatched type ID in type registry 'get'");
357template <std::
size_t I,
typename R,
class S>
361 "Unmatched type ID in mapped type registry 'get'");
368template <
typename R, auto I>
374template <std::
size_t I,
typename =
void>
378template <std::size_t I,
typename ID,
typename... Ts>
381 "Cannot cast to ID: Invalid index for type registry");
384 static constexpr ID
value =
static_cast<ID
>(I);
388template <std::
size_t I,
typename R,
class S>
392 "Cannot cast to ID: Invalid index for ,apped type registry");
400template <std::
size_t I,
typename =
void>
404template <std::size_t I,
typename ID,
typename... Ts>
407 "Cannot cast to index: Invalid index for type registry");
410 static constexpr std::size_t
value = I;
414template <std::
size_t I,
typename R,
class S>
418 "Cannot cast to index: Invalid index for ,apped type registry");
424template <
typename R, std::
size_t I>
427template <
typename R, auto I>
428inline constexpr std::size_t index_cast =
449 typename... Args, std::size_t current_idx = 0u,
450 std::size_t... remaining_idcs>
451 requires(std::invocable<functor_t,
452 const types::front<typename registry_t::type_list>&,
454DETRAY_HOST_DEVICE
constexpr decltype(
auto) visit_helper(
455 const std::
size_t idx,
456 std::index_sequence<current_idx, remaining_idcs...> ,
460 if (registry_t::mapped_index(idx) == current_idx) {
461 return functor_t{}(types::at<registry_t, current_idx>{},
462 std::forward<Args>(args)...);
465 if (idx == current_idx) {
466 return functor_t{}(types::at<registry_t, current_idx>{},
467 std::forward<Args>(args)...);
472 using return_t = std::invoke_result_t<
473 functor_t,
const types::front<typename registry_t::type_list>&, Args...>;
475 if constexpr (
sizeof...(remaining_idcs) >= 1u) {
476 using next_elem_t = types::at<registry_t, current_idx + 1u>;
477 using next_return_t =
478 std::invoke_result_t<functor_t,
const next_elem_t&, Args...>;
480 std::same_as<return_t, next_return_t>,
481 "Functor return type must be the same for all elements of the "
484 return visit_helper<registry_t, functor_t>(
485 idx, std::index_sequence<remaining_idcs...>{},
486 std::forward<Args>(args)...);
487 }
else if constexpr (std::is_same_v<return_t, void>) {
501template <concepts::type_registry registry_t,
typename functor_t,
503DETRAY_HOST_DEVICE
constexpr decltype(
auto) visit(
const std::size_t idx,
505 return visit_helper<registry_t, functor_t>(
506 idx, std::make_index_sequence<registry_t::n_types>{},
507 std::forward<Args>(args)...);
517template <concepts::type_registry registry_t,
typename functor_t,
519DETRAY_HOST_DEVICE
constexpr decltype(
auto) visit(
520 const typename registry_t::id type_id, Args&&... args) {
521 assert(registry_t::is_valid(type_id));
523 return visit<registry_t, functor_t>(
static_cast<std::size_t
>(type_id),
524 std::forward<Args>(args)...);
Select a number of types from another registry and map their IDs to a continuous range of new type in...
Definition type_registry.hpp:102
static constexpr std::size_t mapped_index(const std::size_t orig_idx)
Return the filtered position corresponding to an original position.
Definition type_registry.hpp:156
static consteval auto index_map()
Definition type_registry.hpp:146
static constexpr bool is_valid(const std::size_t filtered_type_idx)
Definition type_registry.hpp:125
typename registry_t::id id
Make the original type ids accessible.
Definition type_registry.hpp:105
decltype(detray::types::filtered_list< orig_types, type_selector_t >()) type_list
Make the filtered types accessible.
Definition type_registry.hpp:109
static constexpr std::size_t mapped_index(const id orig_id)
Return the filtered position corresponding to an original ID.
Definition type_registry.hpp:164
static constexpr bool is_valid(const id_t orig_id)
Check whether an original ID selects a type retained by the mapping.
Definition type_registry.hpp:135
static constexpr id original_id(const std::size_t pos)
Definition type_registry.hpp:187
static constexpr std::size_t original_index(const std::size_t pos)
Definition type_registry.hpp:172
typename registry_t::type_list orig_types
Make the original types accessible.
Definition type_registry.hpp:107
match types with indices/identifiers and vice versa.
Definition type_registry.hpp:33
ID id
Definition type_registry.hpp:36
static constexpr bool is_valid(const std::size_t type_idx)
Definition type_registry.hpp:52
static constexpr bool is_valid(const id_t type_id)
Check whether an identifier corresponds to a registered type.
Definition type_registry.hpp:62
detray::types::list< registered_types... > type_list
Definition type_registry.hpp:38
Mapped type registry concepts.
Definition type_registry.hpp:199
Type identifier concept.
Definition concepts.hpp:36
Definition type_list.hpp:363
Type registry concepts.
Definition type_registry.hpp:75
Detray include(s).
Definition array_operators.hpp:18
typename mapped_registry< R, S >::id ID
Identifier type of the original registry.
Definition type_registry.hpp:395
static constexpr ID value
Identifier corresponding to the compile-time mapped index.
Definition type_registry.hpp:397
static constexpr ID value
Identifier corresponding to the compile-time index.
Definition type_registry.hpp:384
Get the type corresponding to an ID.
Definition type_registry.hpp:375
static constexpr std::size_t value
Filtered index corresponding to the original index.
Definition type_registry.hpp:421
static constexpr std::size_t value
Registry index corresponding to the compile-time index.
Definition type_registry.hpp:410
Definition type_registry.hpp:401
contains_impl< T, I, type_list_t > base_t
Lookup result in the chosen list.
Definition type_registry.hpp:242
std::conditional_t< contains< orig_list_t, T >, orig_list_t, filt_list_t > type_list_t
List in which T is looked up.
Definition type_registry.hpp:238
static constexpr std::size_t pos
Position reported by this trait, or std::size_t's maximum when absent.
Definition type_registry.hpp:257
typename mapped_registry< R, S >::type_list filt_list_t
Filtered registry's ordered type list.
Definition type_registry.hpp:236
typename mapped_registry< R, S >::orig_types orig_list_t
Original registry's ordered type list.
Definition type_registry.hpp:234
static constexpr std::size_t mapped_idx
Position of T in the filtered list when it came from the original list.
Definition type_registry.hpp:249
static constexpr bool value
Whether T occurs in the selected list.
Definition type_registry.hpp:245
Does the list contain a particular type?
Definition type_list.hpp:48
typename get_at< N, typename mapped_registry< R, S >::type_list >::type type
Type at filtered position N in the mapped registry.
Definition type_registry.hpp:281
typename get_at< N, typename registry< ID, Ts... >::type_list >::type type
Type at position N in the registry.
Definition type_registry.hpp:271
Access the N-th type.
Definition type_list.hpp:117
typename mapped_reg_t::type_list filt_list_t
Type list after applying the selector.
Definition type_registry.hpp:318
typename mapped_reg_t::id id_t
Identifier type shared with the original registry.
Definition type_registry.hpp:329
mapped_registry< R, S > mapped_reg_t
Mapped registry being queried.
Definition type_registry.hpp:314
typename mapped_reg_t::orig_types orig_list_t
Type list before applying the selector.
Definition type_registry.hpp:316
std::conditional_t< contains< orig_list_t, T >, orig_list_t, filt_list_t > type_list_t
List used to resolve T, chosen from the original or filtered list.
Definition type_registry.hpp:320
static constexpr id_t value
Identifier of T in the original registry.
Definition type_registry.hpp:331
static constexpr ID value
Identifier value assigned to T by its position in the registry.
Definition type_registry.hpp:298
Traits for the type registry.
Definition type_registry.hpp:292
Number of types in the list.
Definition type_list.hpp:35
at< mapped_registry< R, S >, mapped_registry< R, S >::mapped_index(I)> type
Type selected by original identifier value I.
Definition type_registry.hpp:364
at< registry< ID, Ts... >, I > type
Type registered under identifier value I.
Definition type_registry.hpp:353
Get the type corresponding to an ID.
Definition type_registry.hpp:344
type list implementation
Definition type_list.hpp:30