From 84a87d87bfd569192ff1564e3d8b2e68d5fff4bc Mon Sep 17 00:00:00 2001 From: Shauren Date: Thu, 20 Aug 2020 15:46:31 +0200 Subject: Core/Util: Added another template utility - find_type_if * Trinity::find_type_if - Find a type matching predicate in a given template parameter pack --- src/common/Utilities/Types.h | 68 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/common/Utilities/Types.h (limited to 'src/common') 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 . + */ + +#ifndef Types_h__ +#define Types_h__ + +#include "advstd.h" + +namespace Trinity +{ + // end "iterator" tag for find_type_if + struct find_type_end; + + template typename Check, typename... Ts> + struct find_type_if; + + template typename Check> + struct find_type_if + { + using type = find_type_end; + }; + + template typename Check, typename T1, typename... Ts> + struct find_type_if : std::conditional_t::value, advstd::type_identity, find_type_if> + { + }; + + /* + 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 + struct Example + { + using TupleArg = Trinity::find_type_if_t; + + bool HasTuple() + { + return !std::is_same_v; + } + }; + + Example, char> example; + example.HasTuple() == true; // TupleArg is std::tuple + + Example example2; + example2.HasTuple() == false; // TupleArg is Trinity::find_type_end + */ + template typename Check, typename... Ts> + using find_type_if_t = typename find_type_if::type; +} + +#endif // Types_h__ -- cgit v1.2.3