|
| 1 | +/* |
| 2 | + * This file is part of the AzerothCore Project. See AUTHORS file for Copyright information |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; either version 2 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | + * more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License along |
| 15 | + * with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +#ifndef TypeList_h__ |
| 19 | +#define TypeList_h__ |
| 20 | + |
| 21 | +#include <cstddef> |
| 22 | +#include <utility> |
| 23 | + |
| 24 | +namespace Acore |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @brief Provide an empty tag type containing the specified @p Ts. |
| 28 | + * |
| 29 | + * @tparam Ts The types in this list. |
| 30 | + */ |
| 31 | + template <typename... Ts> |
| 32 | + struct type_list { }; |
| 33 | + |
| 34 | + namespace Impl |
| 35 | + { |
| 36 | + template <typename T> |
| 37 | + inline constexpr bool is_type_list = false; |
| 38 | + |
| 39 | + template <typename... Ts> |
| 40 | + inline constexpr bool is_type_list<type_list<Ts...>> = true; |
| 41 | + |
| 42 | + template <typename List> |
| 43 | + struct list_size; |
| 44 | + |
| 45 | + template <typename... Ts> |
| 46 | + struct list_size<type_list<Ts...>> |
| 47 | + { |
| 48 | + static constexpr std::size_t value = sizeof...(Ts); |
| 49 | + }; |
| 50 | + |
| 51 | + template <typename... Ts, typename Func> |
| 52 | + constexpr void for_each(type_list<Ts...>, Func&& f) |
| 53 | + { |
| 54 | + (f.template operator()<Ts>(), ...); |
| 55 | + } |
| 56 | + |
| 57 | + template <typename... Ts, typename Pred> |
| 58 | + constexpr std::size_t count_if(type_list<Ts...>, Pred pred) |
| 59 | + { |
| 60 | + return ((pred.template operator()<Ts>() ? std::size_t{1} : std::size_t{0}) + ... + std::size_t{0}); |
| 61 | + } |
| 62 | + |
| 63 | + template <typename... Ts, typename Pred> |
| 64 | + constexpr bool any_of(type_list<Ts...>, Pred pred) |
| 65 | + { |
| 66 | + return (false || ... || pred.template operator()<Ts>()); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @brief Satisfied only by Acore::type_list specializations. |
| 72 | + * |
| 73 | + * Constrains the public list operations so they are viable only for an |
| 74 | + * actual type_list. |
| 75 | + */ |
| 76 | + template <typename T> |
| 77 | + concept AnyTypeList = Impl::is_type_list<T>; |
| 78 | + |
| 79 | + /** |
| 80 | + * @brief The number of types in the specified @p List. |
| 81 | + * |
| 82 | + * @tparam List A type_list specialization. |
| 83 | + */ |
| 84 | + template <AnyTypeList List> |
| 85 | + inline constexpr std::size_t size_v = Impl::list_size<List>::value; |
| 86 | + |
| 87 | + /** |
| 88 | + * @brief Invoke the specified @p f once for each type in the specified |
| 89 | + * @p List, in declaration order. |
| 90 | + * |
| 91 | + * @tparam List A type_list specialization. |
| 92 | + * @tparam Func The type of the callable. |
| 93 | + * @param f The callable to invoke. |
| 94 | + */ |
| 95 | + template <AnyTypeList List, typename Func> |
| 96 | + constexpr void for_each(Func&& f) |
| 97 | + { |
| 98 | + Impl::for_each(List{}, std::forward<Func>(f)); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @brief Return the number of types in the specified @p List for which the |
| 103 | + * specified @p pred returns true. |
| 104 | + * |
| 105 | + * @tparam List A type_list specialization. |
| 106 | + * @tparam Pred The type of the predicate. |
| 107 | + * @param pred The predicate to evaluate. |
| 108 | + * @return The number of matching types. |
| 109 | + */ |
| 110 | + template <AnyTypeList List, typename Pred> |
| 111 | + constexpr std::size_t count_if(Pred pred) |
| 112 | + { |
| 113 | + return Impl::count_if(List{}, pred); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @brief Return true if the specified @p pred returns true for any type in |
| 118 | + * the specified @p List, and false otherwise. |
| 119 | + * |
| 120 | + * @tparam List A type_list specialization. |
| 121 | + * @tparam Pred The type of the predicate. |
| 122 | + * @param pred The predicate to evaluate. |
| 123 | + * @return True if any type matches, and false otherwise. |
| 124 | + */ |
| 125 | + template <AnyTypeList List, typename Pred> |
| 126 | + constexpr bool any_of(Pred pred) |
| 127 | + { |
| 128 | + return Impl::any_of(List{}, pred); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +#endif // TypeList_h__ |
0 commit comments