Corium 1.1.0
High-Performance Zero-Heap C++20 MPSC Application Runtime
Loading...
Searching...
No Matches
VariantIndex.hpp
Go to the documentation of this file.
1
7#pragma once
8
9#include <type_traits>
10#include <variant>
11
12namespace corium {
13namespace internal {
14
15template <typename T, typename... Types>
16constexpr std::size_t get_variant_index_impl() {
17 constexpr bool matches[] = { std::is_same_v<T, Types>... };
18
19 for (std::size_t i = 0; i < sizeof...(Types); ++i) {
20 if (matches[i]) return i;
21 }
22
23 return static_cast<std::size_t>(-1);
24}
25
27template <typename T, typename Variant>
29
30template <typename T, typename... Types>
31struct variant_index<T, std::variant<Types...>> {
32 static constexpr std::size_t value = get_variant_index_impl<T, Types...>();
33
34 static_assert(value != static_cast<std::size_t>(-1),
35 "ERROR: The requested Event type is not part of the std::variant!");
36};
37
39template <typename T, typename Variant>
41
43template <typename T, typename Variant>
45
46template <typename T, typename... Types>
47struct has_variant_type<T, std::variant<Types...>> {
48 static constexpr bool value = (std::is_same_v<T, Types> || ...);
49};
50
52template <typename T, typename Variant>
54
55template <typename T>
56struct TypeId {
57 static inline const char id = 0;
58};
59
60using TypeIdPtr = const void*;
61
63template <typename T>
64inline TypeIdPtr getTypeId() noexcept {
65 return &TypeId<T>::id;
66}
67
68template <typename T, typename = void>
70 using type = T;
71};
72
73template <typename T>
74struct extract_event_variant<T, std::void_t<typename T::EventVariant>> {
75 using type = typename T::EventVariant;
76};
77
78template <typename T>
80
81} // namespace internal
82
86
87} // namespace corium
88
constexpr bool has_variant_type_v
Compile-time boolean indicating if type T exists in Variant.
Definition VariantIndex.hpp:53
typename extract_event_variant< T >::type extract_event_variant_t
Definition VariantIndex.hpp:79
constexpr std::size_t get_variant_index_impl()
Definition VariantIndex.hpp:16
TypeIdPtr getTypeId() noexcept
Get static unique address for type T without dynamic allocations or RTTI.
Definition VariantIndex.hpp:64
const void * TypeIdPtr
Definition VariantIndex.hpp:60
constexpr std::size_t variant_index_v
Compile-time constant of type T's index in Variant.
Definition VariantIndex.hpp:40
Definition Application.hpp:16
Definition VariantIndex.hpp:56
Definition VariantIndex.hpp:69
T type
Definition VariantIndex.hpp:70
Helper trait to check if type T exists inside std::variant<Types...> at compile time.
Definition VariantIndex.hpp:44
Helper trait to compute index of type T inside std::variant<Types...> at compile time.
Definition VariantIndex.hpp:28