Codechange: Use EnumBitSet for SortListFlags. (#13506)

This commit is contained in:
Peter Nelson 2025-02-09 14:38:35 +00:00 committed by GitHub
parent 5dc02a686f
commit 2824e790ec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -11,20 +11,16 @@
#define SORTLIST_TYPE_H #define SORTLIST_TYPE_H
#include "core/enum_type.hpp" #include "core/enum_type.hpp"
#include "core/bitmath_func.hpp"
#include "core/mem_func.hpp"
#include "timer/timer_game_tick.h" #include "timer/timer_game_tick.h"
/** Flags of the sort list. */ /** Flags of the sort list. */
enum SortListFlags : uint8_t { enum class SortListFlag : uint8_t {
VL_NONE = 0, ///< no sort Desc, ///< sort descending or ascending
VL_DESC = 1 << 0, ///< sort descending or ascending Resort, ///< instruct the code to resort the list in the next loop
VL_RESORT = 1 << 1, ///< instruct the code to resort the list in the next loop Rebuild, ///< rebuild the sort list
VL_REBUILD = 1 << 2, ///< rebuild the sort list Filter, ///< filter disabled/enabled
VL_FILTER = 1 << 3, ///< filter disabled/enabled
VL_END = 1 << 4,
}; };
DECLARE_ENUM_AS_BIT_SET(SortListFlags) using SortListFlags = EnumBitSet<SortListFlag, uint8_t>;
/** Data structure describing how to show the list (what sort direction and criteria). */ /** Data structure describing how to show the list (what sort direction and criteria). */
struct Listing { struct Listing {
@ -87,7 +83,7 @@ public:
GUIList() : GUIList() :
sort_func_list({}), sort_func_list({}),
filter_func_list({}), filter_func_list({}),
flags(VL_NONE), flags({}),
sort_type(0), sort_type(0),
filter_type(0), filter_type(0),
resort_timer(1), resort_timer(1),
@ -99,7 +95,7 @@ public:
GUIList(const P &params) : GUIList(const P &params) :
sort_func_list({}), sort_func_list({}),
filter_func_list({}), filter_func_list({}),
flags(VL_NONE), flags({}),
sort_type(0), sort_type(0),
filter_type(0), filter_type(0),
resort_timer(1), resort_timer(1),
@ -125,7 +121,7 @@ public:
{ {
assert(n_type < std::size(this->sort_func_list)); assert(n_type < std::size(this->sort_func_list));
if (this->sort_type != n_type) { if (this->sort_type != n_type) {
SETBITS(this->flags, VL_RESORT); this->flags.Set(SortListFlag::Resort);
this->sort_type = n_type; this->sort_type = n_type;
} }
} }
@ -138,7 +134,7 @@ public:
Listing GetListing() const Listing GetListing() const
{ {
Listing l; Listing l;
l.order = (this->flags & VL_DESC) != 0; l.order = this->flags.Test(SortListFlag::Desc);
l.criteria = this->sort_type; l.criteria = this->sort_type;
return l; return l;
@ -152,9 +148,9 @@ public:
void SetListing(Listing l) void SetListing(Listing l)
{ {
if (l.order) { if (l.order) {
SETBITS(this->flags, VL_DESC); this->flags.Set(SortListFlag::Desc);
} else { } else {
CLRBITS(this->flags, VL_DESC); this->flags.Reset(SortListFlag::Desc);
} }
this->sort_type = l.criteria; this->sort_type = l.criteria;
} }
@ -190,7 +186,7 @@ public:
Filtering GetFiltering() const Filtering GetFiltering() const
{ {
Filtering f; Filtering f;
f.state = (this->flags & VL_FILTER) != 0; f.state = this->flags.Test(SortListFlag::Filter);
f.criteria = this->filter_type; f.criteria = this->filter_type;
return f; return f;
@ -204,9 +200,9 @@ public:
void SetFiltering(Filtering f) void SetFiltering(Filtering f)
{ {
if (f.state) { if (f.state) {
SETBITS(this->flags, VL_FILTER); this->flags.Set(SortListFlag::Filter);
} else { } else {
CLRBITS(this->flags, VL_FILTER); this->flags.Reset(SortListFlag::Filter);
} }
this->filter_type = f.criteria; this->filter_type = f.criteria;
} }
@ -222,7 +218,7 @@ public:
bool NeedResort() bool NeedResort()
{ {
if (--this->resort_timer == 0) { if (--this->resort_timer == 0) {
SETBITS(this->flags, VL_RESORT); this->flags.Reset(SortListFlag::Resort);
this->ResetResortTimer(); this->ResetResortTimer();
return true; return true;
} }
@ -235,7 +231,7 @@ public:
*/ */
void ForceResort() void ForceResort()
{ {
SETBITS(this->flags, VL_RESORT); this->flags.Set(SortListFlag::Resort);
} }
/** /**
@ -245,7 +241,7 @@ public:
*/ */
bool IsDescSortOrder() const bool IsDescSortOrder() const
{ {
return (this->flags & VL_DESC) != 0; return this->flags.Test(SortListFlag::Desc);
} }
/** /**
@ -255,7 +251,7 @@ public:
*/ */
void ToggleSortOrder() void ToggleSortOrder()
{ {
this->flags ^= VL_DESC; this->flags.Flip(SortListFlag::Desc);
if (this->IsSortable()) std::reverse(std::vector<T>::begin(), std::vector<T>::end()); if (this->IsSortable()) std::reverse(std::vector<T>::begin(), std::vector<T>::end());
} }
@ -270,16 +266,16 @@ public:
bool Sort(Comp compare) bool Sort(Comp compare)
{ {
/* Do not sort if the resort bit is not set */ /* Do not sort if the resort bit is not set */
if (!(this->flags & VL_RESORT)) return false; if (!this->flags.Test(SortListFlag::Resort)) return false;
CLRBITS(this->flags, VL_RESORT); this->flags.Reset(SortListFlag::Resort);
this->ResetResortTimer(); this->ResetResortTimer();
/* Do not sort when the list is not sortable */ /* Do not sort when the list is not sortable */
if (!this->IsSortable()) return false; if (!this->IsSortable()) return false;
const bool desc = (this->flags & VL_DESC) != 0; const bool desc = this->flags.Test(SortListFlag::Desc);
if constexpr (std::is_same_v<P, std::nullptr_t>) { if constexpr (std::is_same_v<P, std::nullptr_t>) {
std::sort(std::vector<T>::begin(), std::vector<T>::end(), [&](const T &a, const T &b) { return desc ? compare(b, a) : compare(a, b); }); std::sort(std::vector<T>::begin(), std::vector<T>::end(), [&](const T &a, const T &b) { return desc ? compare(b, a) : compare(a, b); });
@ -319,7 +315,7 @@ public:
*/ */
bool IsFilterEnabled() const bool IsFilterEnabled() const
{ {
return (this->flags & VL_FILTER) != 0; return this->flags.Test(SortListFlag::Filter);
} }
/** /**
@ -330,9 +326,9 @@ public:
void SetFilterState(bool state) void SetFilterState(bool state)
{ {
if (state) { if (state) {
SETBITS(this->flags, VL_FILTER); this->flags.Set(SortListFlag::Filter);
} else { } else {
CLRBITS(this->flags, VL_FILTER); this->flags.Reset(SortListFlag::Filter);
} }
} }
@ -346,7 +342,7 @@ public:
bool Filter(FilterFunction *decide, F filter_data) bool Filter(FilterFunction *decide, F filter_data)
{ {
/* Do not filter if the filter bit is not set */ /* Do not filter if the filter bit is not set */
if (!(this->flags & VL_FILTER)) return false; if (!this->flags.Test(SortListFlag::Filter)) return false;
bool changed = false; bool changed = false;
for (auto it = std::vector<T>::begin(); it != std::vector<T>::end(); /* Nothing */) { for (auto it = std::vector<T>::begin(); it != std::vector<T>::end(); /* Nothing */) {
@ -390,7 +386,7 @@ public:
*/ */
bool NeedRebuild() const bool NeedRebuild() const
{ {
return (this->flags & VL_REBUILD) != 0; return this->flags.Test(SortListFlag::Rebuild);
} }
/** /**
@ -398,7 +394,7 @@ public:
*/ */
void ForceRebuild() void ForceRebuild()
{ {
SETBITS(this->flags, VL_REBUILD); this->flags.Set(SortListFlag::Rebuild);
} }
/** /**
@ -408,8 +404,8 @@ public:
*/ */
void RebuildDone() void RebuildDone()
{ {
CLRBITS(this->flags, VL_REBUILD); this->flags.Reset(SortListFlag::Rebuild);
SETBITS(this->flags, VL_RESORT); this->flags.Set(SortListFlag::Resort);
} }
}; };