From 934a5e64f722719f70caeaf404993c2ed843e788 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Mon, 24 Jul 2023 11:21:03 +0200 Subject: [PATCH] Add RTTR_NODISCARD macro --- libs/common/include/s25util/enumUtils.h | 20 ++++---------------- libs/common/include/s25util/macros.h | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 16 deletions(-) create mode 100644 libs/common/include/s25util/macros.h diff --git a/libs/common/include/s25util/enumUtils.h b/libs/common/include/s25util/enumUtils.h index 3eaee9b..cd82838 100644 --- a/libs/common/include/s25util/enumUtils.h +++ b/libs/common/include/s25util/enumUtils.h @@ -4,19 +4,9 @@ #pragma once +#include "macros.h" #include -// TODO Find a better home; could be useful elsewhere -#if __cplusplus >= 201703L -# define ENUM_UTILS_NODISCARD [[nodiscard]] -#elif defined(__clang__) || defined(__GNUC__) -# define ENUM_UTILS_NODISCARD __attribute__((warn_unused_result)) -#elif defined(_MSC_VER) -# define ENUM_UTILS_NODISCARD _Check_return_ -#else -# define ENUM_UTILS_NODISCARD -#endif - template struct IsBitset : std::false_type {}; @@ -69,21 +59,21 @@ constexpr auto operator^=(Enum& lhs, Enum rhs) noexcept namespace bitset { template::value, int> = 0> -ENUM_UTILS_NODISCARD constexpr Enum clear(Enum val, Enum flag) +RTTR_NODISCARD constexpr Enum clear(Enum val, Enum flag) { using Int = std::underlying_type_t; return val & Enum(~static_cast(flag)); } template::value, int> = 0> -ENUM_UTILS_NODISCARD constexpr Enum set(Enum val, Enum flag, bool state = true) +RTTR_NODISCARD constexpr Enum set(Enum val, Enum flag, bool state = true) { using Int = std::underlying_type_t; return state ? (val | flag) : (val & Enum(~static_cast(flag))); } template::value, int> = 0> -ENUM_UTILS_NODISCARD constexpr Enum toggle(Enum val, Enum flag) +RTTR_NODISCARD constexpr Enum toggle(Enum val, Enum flag) { return val ^ flag; } @@ -103,5 +93,3 @@ constexpr bool isSet(Enum val, Enum flag) /* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \ static_assert(std::is_unsigned>::value, \ #Type " must use unsigned type as the underlying type") - -#undef ENUM_UTILS_NODISCARD diff --git a/libs/common/include/s25util/macros.h b/libs/common/include/s25util/macros.h new file mode 100644 index 0000000..87a498b --- /dev/null +++ b/libs/common/include/s25util/macros.h @@ -0,0 +1,15 @@ +// Copyright (C) 2005 - 2023 Settlers Freaks (sf-team at siedler25.org) +// +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#if __cplusplus >= 201703L +# define RTTR_NODISCARD [[nodiscard]] +#elif defined(__clang__) || defined(__GNUC__) +# define RTTR_NODISCARD __attribute__((warn_unused_result)) +#elif defined(_Check_return_) // MSVC requires /analyze switch +# define RTTR_NODISCARD _Check_return_ +#else +# define RTTR_NODISCARD +#endif