diff options
author | Shauren <shauren.trinity@gmail.com> | 2020-08-20 15:46:31 +0200 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2020-08-20 15:46:31 +0200 |
commit | 84a87d87bfd569192ff1564e3d8b2e68d5fff4bc (patch) | |
tree | dfae95c608e91527d9ef4e834ae56ce2b469da29 | |
parent | 7c08fc863ac45f2d41b18103b0132004a4c0c524 (diff) |
Core/Util: Added another template utility - find_type_if
* Trinity::find_type_if - Find a type matching predicate in a given template parameter pack
-rw-r--r-- | src/common/Utilities/Types.h | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/common/Utilities/Types.h b/src/common/Utilities/Types.h new file mode 100644 index 00000000000..ab78fb871a9 --- /dev/null +++ b/src/common/Utilities/Types.h @@ -0,0 +1,68 @@ +/* + * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef Types_h__ +#define Types_h__ + +#include "advstd.h" + +namespace Trinity +{ + // end "iterator" tag for find_type_if + struct find_type_end; + + template<template<typename...> typename Check, typename... Ts> + struct find_type_if; + + template<template<typename...> typename Check> + struct find_type_if<Check> + { + using type = find_type_end; + }; + + template<template<typename...> typename Check, typename T1, typename... Ts> + struct find_type_if<Check, T1, Ts...> : std::conditional_t<Check<T1>::value, advstd::type_identity<T1>, find_type_if<Check, Ts...>> + { + }; + + /* + Utility to find a type matching predicate (Check) in a given type list (Ts) + Evaluates to first type matching predicate or find_type_end + Check must be a type that contains static bool ::value, _v aliases don't work + + template<typename... Ts> + struct Example + { + using TupleArg = Trinity::find_type_if_t<Trinity::is_tuple, Ts...>; + + bool HasTuple() + { + return !std::is_same_v<TupleArg, Trinity::find_type_end>; + } + }; + + Example<int, std::string, std::tuple<int, int, int>, char> example; + example.HasTuple() == true; // TupleArg is std::tuple<int, int, int> + + Example<int, std::string, char> example2; + example2.HasTuple() == false; // TupleArg is Trinity::find_type_end + */ + template<template<typename...> typename Check, typename... Ts> + using find_type_if_t = typename find_type_if<Check, Ts...>::type; +} + +#endif // Types_h__ |