|
| 1 | +#pragma once |
| 2 | +#include <type_traits> |
| 3 | +#include <utility> |
| 4 | + |
| 5 | +/** |
| 6 | + * @namespace core::meta::concepts |
| 7 | + * @brief Namespace for meta-programming concepts and utilities. |
| 8 | + */ |
| 9 | +namespace core::meta::concepts { |
| 10 | + |
| 11 | +/** |
| 12 | + * @struct convertible_to_anything |
| 13 | + * @brief Helper struct that can be implicitly converted to any type. |
| 14 | + * @tparam i Index parameter to differentiate between different instances. |
| 15 | + */ |
| 16 | +template <std::size_t i> |
| 17 | +struct convertible_to_anything { |
| 18 | + /** |
| 19 | + * @brief Implicit conversion operator to any type T. |
| 20 | + * @tparam T The target type to convert to. |
| 21 | + */ |
| 22 | + template <typename T> |
| 23 | + operator T(){}; |
| 24 | +}; |
| 25 | + |
| 26 | +/** |
| 27 | + * @concept is_constructable_with |
| 28 | + * @brief Concept to check if a type T can be constructed with I arguments. |
| 29 | + * @tparam T The type to check. |
| 30 | + * @tparam I The number of arguments to check for. |
| 31 | + */ |
| 32 | +template <typename T, std::size_t I> |
| 33 | +concept is_constructable_with = requires { |
| 34 | + []<std::size_t... is>(std::index_sequence<is...> i_s) |
| 35 | + -> decltype(T{convertible_to_anything<is>{}...}) { |
| 36 | + return {}; |
| 37 | + }(std::make_index_sequence<I>{}); |
| 38 | +}; |
| 39 | + |
| 40 | +/** |
| 41 | + * @concept aggregate_of |
| 42 | + * @brief Concept to check if a type T is an aggregate with exactly n members. |
| 43 | + * @tparam T The type to check. |
| 44 | + * @tparam n The number of members to check for. |
| 45 | + */ |
| 46 | +template <typename T, std::size_t n> |
| 47 | +concept aggregate_of = std::is_aggregate_v<T> && is_constructable_with<T, n> && |
| 48 | + !is_constructable_with<T, n + 1>; |
| 49 | + |
| 50 | +/** |
| 51 | + * @brief Maximum number of aggregate members to check. |
| 52 | + */ |
| 53 | +constexpr std::size_t maxAggregateMembers = 12; |
| 54 | + |
| 55 | +/** |
| 56 | + * @var number_of_aggregate_members |
| 57 | + * @brief Variable template to determine the number of aggregate members in type |
| 58 | + * T. |
| 59 | + * @tparam T The type to check. |
| 60 | + */ |
| 61 | +template <typename T> |
| 62 | +constexpr auto number_of_aggregate_members = |
| 63 | + []<std::size_t... indexes>(std::index_sequence<indexes...> i_s) { |
| 64 | + return ((aggregate_of<T, indexes> * indexes) + ... + 0); |
| 65 | + }(std::make_index_sequence<maxAggregateMembers>{}); |
| 66 | + |
| 67 | +} // namespace core::meta::concepts |
0 commit comments