feat: improve EnumHelper

- allow multiple enum flags in EnumHelper::Has to check whether given flags contains specified flags.
- update version in CMake script.
This commit is contained in:
yyc12345 2024-11-03 18:06:36 +08:00
parent 7adac00035
commit 831fa130bc
2 changed files with 22 additions and 17 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.23) cmake_minimum_required(VERSION 3.23)
project(YYCC project(YYCC
VERSION 1.2.0 VERSION 1.3.0
LANGUAGES CXX LANGUAGES CXX
) )

View File

@ -88,8 +88,13 @@ namespace YYCC::EnumHelper {
/** /**
* @brief Merge given enum flags like performing <TT>e1 | e2 | ... | en</TT> * @brief Merge given enum flags like performing <TT>e1 | e2 | ... | en</TT>
* @tparam TEnum Enum type for processing. * @tparam TEnum Enum type for processing.
* @param[in] il The list of enum flags to be merged. * @param[in] val The first enum flag to be merged.
* @param[in] val_left Left enum flags to be merged.
* @return The merged enum flag. * @return The merged enum flag.
* @remarks
* This function use recursive expansion to get final merge result.
* So there is no difference of each arguments.
* We independ first argument just served for expansion.
*/ */
template<typename TEnum, typename... Ts, std::enable_if_t<all_enum_values_v<TEnum, Ts...>, int> = 0> template<typename TEnum, typename... Ts, std::enable_if_t<all_enum_values_v<TEnum, Ts...>, int> = 0>
constexpr TEnum Merge(TEnum val, Ts... val_left) { constexpr TEnum Merge(TEnum val, Ts... val_left) {
@ -114,7 +119,7 @@ namespace YYCC::EnumHelper {
} }
/** /**
* @brief Use specified enum flags to mask given enum flags like performing <TT>e1 &= e2</TT> * @brief Use specified enum flag to mask given enum flag like performing <TT>e1 &= e2</TT>
* @tparam TEnum Enum type for processing. * @tparam TEnum Enum type for processing.
* @param[in,out] e1 The enum flags to be masked. * @param[in,out] e1 The enum flags to be masked.
* @param[in] e2 The mask enum flag. * @param[in] e2 The mask enum flag.
@ -126,10 +131,10 @@ namespace YYCC::EnumHelper {
} }
/** /**
* @brief Add specified enum flags to given enum flags like performing <TT>e1 = e1 | e2 | ... | en</TT> * @brief Add multiple enum flags to given enum flag like performing <TT>e1 |= (e2 | e3 | ... | en)</TT>
* @tparam TEnum Enum type for processing. * @tparam TEnum Enum type for processing.
* @param[in,out] e1 The enum flags to be processed. * @param[in,out] e1 The enum flag which flags add on.
* @param[in] e2 The enum flag to be added. * @param[in] vals The enum flag to be added.
*/ */
template<typename TEnum, typename... Ts, std::enable_if_t<all_enum_values_v<TEnum, Ts...>, int> = 0> template<typename TEnum, typename... Ts, std::enable_if_t<all_enum_values_v<TEnum, Ts...>, int> = 0>
constexpr void Add(TEnum& e1, Ts... vals) { constexpr void Add(TEnum& e1, Ts... vals) {
@ -138,10 +143,10 @@ namespace YYCC::EnumHelper {
} }
/** /**
* @brief Remove specified enum flags from given enum flags like performing <TT>e1 &= ~(e2 | e3 | ... | en)</TT> * @brief Remove multiple enum flags from given enum flag like performing <TT>e1 &= ~(e2 | e3 | ... | en)</TT>
* @tparam TEnum Enum type for processing. * @tparam TEnum Enum type for processing.
* @param[in,out] e1 The enum flags to be processed. * @param[in,out] e1 The enum flag which flags removed from.
* @param[in] e2 The enum flag to be removed. * @param[in] vals The enum flag to be removed.
*/ */
template<typename TEnum, typename... Ts, std::enable_if_t<all_enum_values_v<TEnum>, int> = 0> template<typename TEnum, typename... Ts, std::enable_if_t<all_enum_values_v<TEnum>, int> = 0>
constexpr void Remove(TEnum& e1, Ts... vals) { constexpr void Remove(TEnum& e1, Ts... vals) {
@ -150,23 +155,23 @@ namespace YYCC::EnumHelper {
} }
/** /**
* @brief Check whether given enum flags has specified enum flag like performing <TT>bool(e & probe)</TT> * @brief Check whether given enum flag has any of specified multiple enum flags (OR) like performing <TT>bool(e1 & (e2 | e3 | ... | en))</TT>
* @tparam TEnum Enum type for processing. * @tparam TEnum Enum type for processing.
* @param[in] e1 The enum flags to be checked. * @param[in] e1 The enum flag where we check.
* @param[in] e2 The enum flag for checking. * @param[in] vals The enum flags for checking.
* @return True if it has, otherwise false. * @return True if it has any of given flags (OR), otherwise false.
*/ */
template<typename TEnum, std::enable_if_t<std::is_enum_v<TEnum>, int> = 0> template<typename TEnum, typename... Ts, std::enable_if_t<std::is_enum_v<TEnum>, int> = 0>
constexpr bool Has(TEnum e1, TEnum e2) { constexpr bool Has(TEnum e1, Ts... vals) {
using ut = std::underlying_type_t<TEnum>; using ut = std::underlying_type_t<TEnum>;
return static_cast<bool>(static_cast<ut>(e1) & static_cast<ut>(e2)); return static_cast<bool>(static_cast<ut>(e1) & static_cast<ut>(Merge(vals...)));
} }
/** /**
* @brief Cast given enum flags to its equvalent boolean value like performing <TT>bool(e)</TT> * @brief Cast given enum flags to its equvalent boolean value like performing <TT>bool(e)</TT>
* @tparam TEnum Enum type for processing. * @tparam TEnum Enum type for processing.
* @param e The enum flags to be cast. * @param e The enum flags to be cast.
* @return The cast enum flag. * @return The equvalent bool value of given enum flag.
*/ */
template<typename TEnum, std::enable_if_t<std::is_enum_v<TEnum>, int> = 0> template<typename TEnum, std::enable_if_t<std::is_enum_v<TEnum>, int> = 0>
constexpr bool Bool(TEnum e) { constexpr bool Bool(TEnum e) {