Skip to content

Commit 1a6432e

Browse files
FlamefireFlow86
authored andcommitted
Use C++17 variable templates for traits
1 parent 46ad2b1 commit 1a6432e

31 files changed

Lines changed: 145 additions & 132 deletions

libs/common/include/Point.h

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2005 - 2021 Settlers Freaks (sf-team at siedler25.org)
1+
// Copyright (C) 2005 - 2025 Settlers Freaks (sf-team at siedler25.org)
22
//
33
// SPDX-License-Identifier: GPL-2.0-or-later
44

@@ -20,7 +20,7 @@ template<typename T>
2020
struct Point //-V690
2121
{
2222
using ElementType = T;
23-
static_assert(std::is_arithmetic<ElementType>::value, "Requires an arithmetic type");
23+
static_assert(std::is_arithmetic_v<ElementType>, "Requires an arithmetic type");
2424

2525
static constexpr auto MinElementValue = std::numeric_limits<T>::min();
2626
static constexpr auto MaxElementValue = std::numeric_limits<T>::max();
@@ -35,15 +35,15 @@ struct Point //-V690
3535
T x, y;
3636
constexpr Point() noexcept : x(getInvalidValue()), y(getInvalidValue()) {}
3737
constexpr Point(const T x, const T y) noexcept : x(x), y(y) {}
38-
template<typename U, std::enable_if_t<!(std::is_integral<T>::value && std::is_floating_point<U>::value), int> = 0>
38+
template<typename U, std::enable_if_t<!(std::is_integral_v<T> && std::is_floating_point_v<U>), int> = 0>
3939
constexpr explicit Point(const Point<U>& pt) noexcept : x(static_cast<T>(pt.x)), y(static_cast<T>(pt.y))
4040
{}
4141
/// Convert floating-point to integer by truncating
42-
template<typename U, std::enable_if_t<std::is_integral<T>::value && std::is_floating_point<U>::value, int> = 0>
42+
template<typename U, std::enable_if_t<std::is_integral_v<T> && std::is_floating_point_v<U>, int> = 0>
4343
constexpr explicit Point(Truncate_t, const Point<U>& pt) noexcept : x(static_cast<T>(pt.x)), y(static_cast<T>(pt.y))
4444
{}
4545
/// Convert floating-point to integer with rounding (default behavior)
46-
template<typename U, std::enable_if_t<std::is_integral<T>::value && std::is_floating_point<U>::value, int> = 0>
46+
template<typename U, std::enable_if_t<std::is_integral_v<T> && std::is_floating_point_v<U>, int> = 0>
4747
constexpr explicit Point(const Point<U>& pt) noexcept : x(helpers::iround<T>(pt.x)), y(helpers::iround<T>(pt.y))
4848
{}
4949
constexpr Point(const Point&) = default;
@@ -99,7 +99,7 @@ struct type_identity
9999
/// Convert the type T to a signed type if the condition is true (safe for float types)
100100
template<bool cond, typename T>
101101
using make_signed_if_t =
102-
typename std::conditional_t<cond && !std::is_signed<T>::value, std::make_signed<T>, type_identity<T>>::type;
102+
typename std::conditional_t<cond && !std::is_signed_v<T>, std::make_signed<T>, type_identity<T>>::type;
103103

104104
// clang-format off
105105

@@ -109,26 +109,25 @@ using make_signed_if_t =
109109
template<typename T, typename U>
110110
using mixed_type_t =
111111
make_signed_if_t<
112-
std::is_signed<T>::value || std::is_signed<U>::value,
112+
std::is_signed_v<T> || std::is_signed_v<U>,
113113
typename std::conditional_t<
114-
std::is_floating_point<T>::value == std::is_floating_point<U>::value, // both are FP or not FP?
114+
std::is_floating_point_v<T> == std::is_floating_point_v<U>, // both are FP or not FP?
115115
std::conditional<(sizeof(T) > sizeof(U)), T, U>, // Take the larger type
116-
std::conditional<std::is_floating_point<T>::value, T, U> // Take the floating point type
116+
std::conditional<std::is_floating_point_v<T>, T, U> // Take the floating point type
117117
>::type
118118
>;
119119

120120
template<typename T, typename U>
121-
using IsNonLossyOp = std::integral_constant<bool,
121+
constexpr bool is_lossles_op_v =
122122
// We can do T = T <op> U (except overflow) if:
123-
std::is_floating_point<T>::value || std::is_signed<T>::value || std::is_unsigned<U>::value
124-
>;
123+
std::is_floating_point_v<T> || std::is_signed_v<T> || std::is_unsigned_v<U>;
125124

126125
// clang-format on
127126

128127
template<typename T, typename U>
129-
using require_nonLossyOp = std::enable_if_t<IsNonLossyOp<T, U>::value>;
128+
using require_lossless_op = std::enable_if_t<is_lossles_op_v<T, U>>;
130129
template<typename T>
131-
using require_arithmetic = std::enable_if_t<std::is_arithmetic<T>::value>;
130+
using require_arithmetic = std::enable_if_t<std::is_arithmetic_v<T>>;
132131

133132
} // namespace detail
134133

@@ -153,7 +152,7 @@ constexpr auto prodOfComponents(const Point<T>& pt) noexcept
153152
{
154153
// Let the compiler handle conversion to at least 32 bits keeping float types
155154
using op_type = decltype(T{} * uint32_t{});
156-
using ResultType = ::detail::make_signed_if_t<std::is_signed<T>::value, op_type>;
155+
using ResultType = ::detail::make_signed_if_t<std::is_signed_v<T>, op_type>;
157156
return static_cast<ResultType>(pt.x * pt.y);
158157
}
159158

@@ -198,7 +197,7 @@ RTTR_GEN_ARITH(/)
198197

199198
// Scaling operators
200199

201-
template<typename T, typename U, class = detail::require_nonLossyOp<T, U>>
200+
template<typename T, typename U, class = detail::require_lossless_op<T, U>>
202201
constexpr Point<T>& operator*=(Point<T>& lhs, U factor) noexcept
203202
{
204203
return lhs *= Point<T>::all(factor);
@@ -216,7 +215,7 @@ constexpr auto operator*(const T left, const Point<U>& factor) noexcept
216215
return factor * left;
217216
}
218217

219-
template<typename T, typename U, class = detail::require_nonLossyOp<T, U>>
218+
template<typename T, typename U, class = detail::require_lossless_op<T, U>>
220219
constexpr Point<T>& operator/=(Point<T>& lhs, U div) noexcept
221220
{
222221
return lhs /= Point<T>::all(div);

libs/common/include/Rect.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2005 - 2021 Settlers Freaks (sf-team at siedler25.org)
1+
// Copyright (C) 2005 - 2025 Settlers Freaks (sf-team at siedler25.org)
22
//
33
// SPDX-License-Identifier: GPL-2.0-or-later
44

@@ -21,7 +21,7 @@ struct RectBase
2121
{
2222
using position_type = Point<T>;
2323
using extent_elem_type =
24-
typename std::conditional_t<std::is_integral<T>::value, std::make_unsigned<T>, std::common_type<T>>::type;
24+
typename std::conditional_t<std::is_integral_v<T>, std::make_unsigned<T>, std::common_type<T>>::type;
2525
using extent_type = Point<extent_elem_type>;
2626
T left, top, right, bottom;
2727
constexpr RectBase() : RectBase(position_type::all(0), extent_type::all(0)) {}

libs/common/include/helpers/EnumRange.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2005 - 2021 Settlers Freaks (sf-team at siedler25.org)
1+
// Copyright (C) 2005 - 2025 Settlers Freaks (sf-team at siedler25.org)
22
//
33
// SPDX-License-Identifier: GPL-2.0-or-later
44

@@ -29,7 +29,7 @@ struct EnumIterTraits
2929
template<class T>
3030
struct EnumRange
3131
{
32-
static_assert(std::is_enum<T>::value, "Must be an enum!");
32+
static_assert(std::is_enum_v<T>, "Must be an enum!");
3333
class iterator : public EnumIterTraits<T>
3434
{
3535
unsigned value;
@@ -51,7 +51,7 @@ struct EnumRange
5151
template<class T>
5252
struct EnumRangeWithOffset
5353
{
54-
static_assert(std::is_enum<T>::value, "Must be an enum!");
54+
static_assert(std::is_enum_v<T>, "Must be an enum!");
5555
class iterator : public EnumIterTraits<T>
5656
{
5757
unsigned value;

libs/common/include/helpers/MaxEnumValue.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2005 - 2021 Settlers Freaks (sf-team at siedler25.org)
1+
// Copyright (C) 2005 - 2025 Settlers Freaks (sf-team at siedler25.org)
22
//
33
// SPDX-License-Identifier: GPL-2.0-or-later
44

@@ -14,13 +14,17 @@ namespace helpers {
1414
template<class T>
1515
struct MaxEnumValue
1616
{
17-
static_assert(std::is_enum<T>::value, "T must be an enum");
17+
static_assert(std::is_enum_v<T>, "T must be an enum");
1818
static constexpr T value = maxEnumValue(T{});
1919
};
2020

21-
/// Return the maximum value of an Enum
21+
/// Return the highest enumerator of an enum type
2222
template<class T_Enum>
23-
inline constexpr unsigned MaxEnumValue_v = static_cast<std::underlying_type_t<T_Enum>>(MaxEnumValue<T_Enum>::value);
23+
inline constexpr T_Enum MaxEnumerator_v = MaxEnumValue<T_Enum>::value;
24+
25+
/// Return the maximum numeric value of an Enum
26+
template<class T_Enum>
27+
inline constexpr unsigned MaxEnumValue_v = static_cast<std::underlying_type_t<T_Enum>>(MaxEnumerator_v<T_Enum>);
2428

2529
/// Return the number of enumerators for an enum type
2630
template<class T_Enum>

libs/common/include/helpers/OptionalEnum.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2005 - 2021 Settlers Freaks (sf-team at siedler25.org)
1+
// Copyright (C) 2005 - 2025 Settlers Freaks (sf-team at siedler25.org)
22
//
33
// SPDX-License-Identifier: GPL-2.0-or-later
44

@@ -16,7 +16,7 @@ namespace helpers {
1616
template<class T>
1717
class OptionalEnum
1818
{
19-
static_assert(std::is_enum<T>::value, "Only works for enums");
19+
static_assert(std::is_enum_v<T>, "Only works for enums");
2020
using underlying_type = std::underlying_type_t<T>;
2121

2222
public:

libs/common/include/helpers/Range.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2005 - 2024 Settlers Freaks (sf-team at siedler25.org)
1+
// Copyright (C) 2005 - 2025 Settlers Freaks (sf-team at siedler25.org)
22
//
33
// SPDX-License-Identifier: GPL-2.0-or-later
44

@@ -16,7 +16,7 @@ namespace helpers {
1616
template<typename T>
1717
class range
1818
{
19-
static_assert(std::is_integral<T>::value, "Must be an integral!");
19+
static_assert(std::is_integral_v<T>, "Must be an integral!");
2020
const T startValue_;
2121
const T endValue_;
2222

libs/common/include/helpers/containerUtils.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2005 - 2024 Settlers Freaks (sf-team at siedler25.org)
1+
// Copyright (C) 2005 - 2025 Settlers Freaks (sf-team at siedler25.org)
22
//
33
// SPDX-License-Identifier: GPL-2.0-or-later
44

@@ -21,6 +21,8 @@ namespace detail {
2121
template<class T>
2222
struct has_pop_front<T, std::void_t<decltype(T::pop_front())>> : std::true_type
2323
{};
24+
template<class T>
25+
constexpr bool has_pop_front_v = has_pop_front<T>::value;
2426

2527
template<class T, class U, typename = void>
2628
struct has_find : std::false_type
@@ -29,6 +31,8 @@ namespace detail {
2931
template<class T, class U>
3032
struct has_find<T, U, std::void_t<decltype(std::declval<T>().find(std::declval<U>()))>> : std::true_type
3133
{};
34+
template<class T, class U>
35+
constexpr bool has_find_v = has_find<T, U>::value;
3236
} // namespace detail
3337

3438
/// Removes an element from a container by its reverse iterator and returns an iterator to the next element
@@ -62,7 +66,7 @@ template<typename T>
6266
void pop_front(T& container)
6367
{
6468
RTTR_Assert(!container.empty());
65-
if constexpr(detail::has_pop_front<T>::value)
69+
if constexpr(detail::has_pop_front_v<T>)
6670
container.pop_front();
6771
else
6872
container.erase(container.begin());
@@ -72,7 +76,7 @@ void pop_front(T& container)
7276
template<typename T, typename U>
7377
auto find(T& container, const U& value)
7478
{
75-
if constexpr(detail::has_find<T, U>::value)
79+
if constexpr(detail::has_find_v<T, U>)
7680
return container.find(value);
7781
else
7882
{

0 commit comments

Comments
 (0)