Skip to content

Commit

Permalink
Add RTTR_NODISCARD macro
Browse files Browse the repository at this point in the history
  • Loading branch information
falbrechtskirchinger committed Jul 24, 2023
1 parent f4c43aa commit 934a5e6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
20 changes: 4 additions & 16 deletions libs/common/include/s25util/enumUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,9 @@

#pragma once

#include "macros.h"
#include <type_traits>

// 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<typename Enum>
struct IsBitset : std::false_type
{};
Expand Down Expand Up @@ -69,21 +59,21 @@ constexpr auto operator^=(Enum& lhs, Enum rhs) noexcept

namespace bitset {
template<typename Enum, std::enable_if_t<IsValidBitset<Enum>::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<Enum>;
return val & Enum(~static_cast<Int>(flag));
}

template<typename Enum, std::enable_if_t<IsValidBitset<Enum>::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<Enum>;
return state ? (val | flag) : (val & Enum(~static_cast<Int>(flag)));
}

template<typename Enum, std::enable_if_t<IsValidBitset<Enum>::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;
}
Expand All @@ -103,5 +93,3 @@ constexpr bool isSet(Enum val, Enum flag)
/* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \
static_assert(std::is_unsigned<std::underlying_type_t<Type>>::value, \
#Type " must use unsigned type as the underlying type")

#undef ENUM_UTILS_NODISCARD
15 changes: 15 additions & 0 deletions libs/common/include/s25util/macros.h
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 934a5e6

Please sign in to comment.