mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-03-06 22:28:56 +00:00
Codechange: Use EnumBitSet for SortListFlags. (#13506)
This commit is contained in:
parent
5dc02a686f
commit
2824e790ec
@ -11,20 +11,16 @@
|
||||
#define SORTLIST_TYPE_H
|
||||
|
||||
#include "core/enum_type.hpp"
|
||||
#include "core/bitmath_func.hpp"
|
||||
#include "core/mem_func.hpp"
|
||||
#include "timer/timer_game_tick.h"
|
||||
|
||||
/** Flags of the sort list. */
|
||||
enum SortListFlags : uint8_t {
|
||||
VL_NONE = 0, ///< no sort
|
||||
VL_DESC = 1 << 0, ///< sort descending or ascending
|
||||
VL_RESORT = 1 << 1, ///< instruct the code to resort the list in the next loop
|
||||
VL_REBUILD = 1 << 2, ///< rebuild the sort list
|
||||
VL_FILTER = 1 << 3, ///< filter disabled/enabled
|
||||
VL_END = 1 << 4,
|
||||
enum class SortListFlag : uint8_t {
|
||||
Desc, ///< sort descending or ascending
|
||||
Resort, ///< instruct the code to resort the list in the next loop
|
||||
Rebuild, ///< rebuild the sort list
|
||||
Filter, ///< filter disabled/enabled
|
||||
};
|
||||
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). */
|
||||
struct Listing {
|
||||
@ -87,7 +83,7 @@ public:
|
||||
GUIList() :
|
||||
sort_func_list({}),
|
||||
filter_func_list({}),
|
||||
flags(VL_NONE),
|
||||
flags({}),
|
||||
sort_type(0),
|
||||
filter_type(0),
|
||||
resort_timer(1),
|
||||
@ -99,7 +95,7 @@ public:
|
||||
GUIList(const P ¶ms) :
|
||||
sort_func_list({}),
|
||||
filter_func_list({}),
|
||||
flags(VL_NONE),
|
||||
flags({}),
|
||||
sort_type(0),
|
||||
filter_type(0),
|
||||
resort_timer(1),
|
||||
@ -125,7 +121,7 @@ public:
|
||||
{
|
||||
assert(n_type < std::size(this->sort_func_list));
|
||||
if (this->sort_type != n_type) {
|
||||
SETBITS(this->flags, VL_RESORT);
|
||||
this->flags.Set(SortListFlag::Resort);
|
||||
this->sort_type = n_type;
|
||||
}
|
||||
}
|
||||
@ -138,7 +134,7 @@ public:
|
||||
Listing GetListing() const
|
||||
{
|
||||
Listing l;
|
||||
l.order = (this->flags & VL_DESC) != 0;
|
||||
l.order = this->flags.Test(SortListFlag::Desc);
|
||||
l.criteria = this->sort_type;
|
||||
|
||||
return l;
|
||||
@ -152,9 +148,9 @@ public:
|
||||
void SetListing(Listing l)
|
||||
{
|
||||
if (l.order) {
|
||||
SETBITS(this->flags, VL_DESC);
|
||||
this->flags.Set(SortListFlag::Desc);
|
||||
} else {
|
||||
CLRBITS(this->flags, VL_DESC);
|
||||
this->flags.Reset(SortListFlag::Desc);
|
||||
}
|
||||
this->sort_type = l.criteria;
|
||||
}
|
||||
@ -190,7 +186,7 @@ public:
|
||||
Filtering GetFiltering() const
|
||||
{
|
||||
Filtering f;
|
||||
f.state = (this->flags & VL_FILTER) != 0;
|
||||
f.state = this->flags.Test(SortListFlag::Filter);
|
||||
f.criteria = this->filter_type;
|
||||
|
||||
return f;
|
||||
@ -204,9 +200,9 @@ public:
|
||||
void SetFiltering(Filtering f)
|
||||
{
|
||||
if (f.state) {
|
||||
SETBITS(this->flags, VL_FILTER);
|
||||
this->flags.Set(SortListFlag::Filter);
|
||||
} else {
|
||||
CLRBITS(this->flags, VL_FILTER);
|
||||
this->flags.Reset(SortListFlag::Filter);
|
||||
}
|
||||
this->filter_type = f.criteria;
|
||||
}
|
||||
@ -222,7 +218,7 @@ public:
|
||||
bool NeedResort()
|
||||
{
|
||||
if (--this->resort_timer == 0) {
|
||||
SETBITS(this->flags, VL_RESORT);
|
||||
this->flags.Reset(SortListFlag::Resort);
|
||||
this->ResetResortTimer();
|
||||
return true;
|
||||
}
|
||||
@ -235,7 +231,7 @@ public:
|
||||
*/
|
||||
void ForceResort()
|
||||
{
|
||||
SETBITS(this->flags, VL_RESORT);
|
||||
this->flags.Set(SortListFlag::Resort);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -245,7 +241,7 @@ public:
|
||||
*/
|
||||
bool IsDescSortOrder() const
|
||||
{
|
||||
return (this->flags & VL_DESC) != 0;
|
||||
return this->flags.Test(SortListFlag::Desc);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -255,7 +251,7 @@ public:
|
||||
*/
|
||||
void ToggleSortOrder()
|
||||
{
|
||||
this->flags ^= VL_DESC;
|
||||
this->flags.Flip(SortListFlag::Desc);
|
||||
|
||||
if (this->IsSortable()) std::reverse(std::vector<T>::begin(), std::vector<T>::end());
|
||||
}
|
||||
@ -270,16 +266,16 @@ public:
|
||||
bool Sort(Comp compare)
|
||||
{
|
||||
/* 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();
|
||||
|
||||
/* Do not sort when the list is not sortable */
|
||||
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>) {
|
||||
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
|
||||
{
|
||||
return (this->flags & VL_FILTER) != 0;
|
||||
return this->flags.Test(SortListFlag::Filter);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -330,9 +326,9 @@ public:
|
||||
void SetFilterState(bool state)
|
||||
{
|
||||
if (state) {
|
||||
SETBITS(this->flags, VL_FILTER);
|
||||
this->flags.Set(SortListFlag::Filter);
|
||||
} else {
|
||||
CLRBITS(this->flags, VL_FILTER);
|
||||
this->flags.Reset(SortListFlag::Filter);
|
||||
}
|
||||
}
|
||||
|
||||
@ -346,7 +342,7 @@ public:
|
||||
bool Filter(FilterFunction *decide, F filter_data)
|
||||
{
|
||||
/* 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;
|
||||
for (auto it = std::vector<T>::begin(); it != std::vector<T>::end(); /* Nothing */) {
|
||||
@ -390,7 +386,7 @@ public:
|
||||
*/
|
||||
bool NeedRebuild() const
|
||||
{
|
||||
return (this->flags & VL_REBUILD) != 0;
|
||||
return this->flags.Test(SortListFlag::Rebuild);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -398,7 +394,7 @@ public:
|
||||
*/
|
||||
void ForceRebuild()
|
||||
{
|
||||
SETBITS(this->flags, VL_REBUILD);
|
||||
this->flags.Set(SortListFlag::Rebuild);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -408,8 +404,8 @@ public:
|
||||
*/
|
||||
void RebuildDone()
|
||||
{
|
||||
CLRBITS(this->flags, VL_REBUILD);
|
||||
SETBITS(this->flags, VL_RESORT);
|
||||
this->flags.Reset(SortListFlag::Rebuild);
|
||||
this->flags.Set(SortListFlag::Resort);
|
||||
}
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user