mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-02-08 23:34:15 +00:00
Codechange: Remove std::function from Pool iteration wrapper
Add a separate template wrapper for filtered iteration
This commit is contained in:
parent
4cc1420beb
commit
150dfba95b
@ -12,7 +12,6 @@
|
|||||||
|
|
||||||
#include "smallvec_type.hpp"
|
#include "smallvec_type.hpp"
|
||||||
#include "enum_type.hpp"
|
#include "enum_type.hpp"
|
||||||
#include <functional>
|
|
||||||
|
|
||||||
/** Various types of a pool. */
|
/** Various types of a pool. */
|
||||||
enum PoolType {
|
enum PoolType {
|
||||||
@ -149,9 +148,8 @@ struct Pool : PoolBase {
|
|||||||
typedef size_t difference_type;
|
typedef size_t difference_type;
|
||||||
typedef std::forward_iterator_tag iterator_category;
|
typedef std::forward_iterator_tag iterator_category;
|
||||||
|
|
||||||
explicit PoolIterator(size_t index, std::function<bool(size_t)> filter = nullptr) : index(index), filter(filter)
|
explicit PoolIterator(size_t index) : index(index)
|
||||||
{
|
{
|
||||||
if (this->filter == nullptr) this->filter = [](size_t) { return true; };
|
|
||||||
this->ValidateIndex();
|
this->ValidateIndex();
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -162,8 +160,7 @@ struct Pool : PoolBase {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
size_t index;
|
size_t index;
|
||||||
std::function<bool(size_t)> filter;
|
void ValidateIndex() { while (this->index < T::GetPoolSize() && !(T::IsValidID(this->index))) this->index++; }
|
||||||
void ValidateIndex() { while (this->index < T::GetPoolSize() && !(T::IsValidID(this->index) && this->filter(this->index))) this->index++; }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -173,13 +170,54 @@ struct Pool : PoolBase {
|
|||||||
template <class T>
|
template <class T>
|
||||||
struct IterateWrapper {
|
struct IterateWrapper {
|
||||||
size_t from;
|
size_t from;
|
||||||
std::function<bool(size_t)> filter;
|
IterateWrapper(size_t from = 0) : from(from) {}
|
||||||
IterateWrapper(size_t from = 0, std::function<bool(size_t)> filter = nullptr) : from(from), filter(filter) {}
|
PoolIterator<T> begin() { return PoolIterator<T>(this->from); }
|
||||||
PoolIterator<T> begin() { return PoolIterator<T>(this->from, this->filter); }
|
|
||||||
PoolIterator<T> end() { return PoolIterator<T>(T::GetPoolSize()); }
|
PoolIterator<T> end() { return PoolIterator<T>(T::GetPoolSize()); }
|
||||||
bool empty() { return this->begin() == this->end(); }
|
bool empty() { return this->begin() == this->end(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Iterator to iterate all valid T of a pool
|
||||||
|
* @tparam T Type of the class/struct that is going to be iterated
|
||||||
|
*/
|
||||||
|
template <class T, class F>
|
||||||
|
struct PoolIteratorFiltered {
|
||||||
|
typedef T value_type;
|
||||||
|
typedef T* pointer;
|
||||||
|
typedef T& reference;
|
||||||
|
typedef size_t difference_type;
|
||||||
|
typedef std::forward_iterator_tag iterator_category;
|
||||||
|
|
||||||
|
explicit PoolIteratorFiltered(size_t index, F filter) : index(index), filter(filter)
|
||||||
|
{
|
||||||
|
this->ValidateIndex();
|
||||||
|
};
|
||||||
|
|
||||||
|
bool operator==(const PoolIteratorFiltered &other) const { return this->index == other.index; }
|
||||||
|
bool operator!=(const PoolIteratorFiltered &other) const { return !(*this == other); }
|
||||||
|
T * operator*() const { return T::Get(this->index); }
|
||||||
|
PoolIteratorFiltered & operator++() { this->index++; this->ValidateIndex(); return *this; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
size_t index;
|
||||||
|
F filter;
|
||||||
|
void ValidateIndex() { while (this->index < T::GetPoolSize() && !(T::IsValidID(this->index) && this->filter(this->index))) this->index++; }
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Iterable ensemble of all valid T
|
||||||
|
* @tparam T Type of the class/struct that is going to be iterated
|
||||||
|
*/
|
||||||
|
template <class T, class F>
|
||||||
|
struct IterateWrapperFiltered {
|
||||||
|
size_t from;
|
||||||
|
F filter;
|
||||||
|
IterateWrapperFiltered(size_t from, F filter) : from(from), filter(filter) {}
|
||||||
|
PoolIteratorFiltered<T, F> begin() { return PoolIteratorFiltered<T, F>(this->from, this->filter); }
|
||||||
|
PoolIteratorFiltered<T, F> end() { return PoolIteratorFiltered<T, F>(T::GetPoolSize(), this->filter); }
|
||||||
|
bool empty() { return this->begin() == this->end(); }
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class for all PoolItems
|
* Base class for all PoolItems
|
||||||
* @tparam Tpool The pool this item is going to be part of
|
* @tparam Tpool The pool this item is going to be part of
|
||||||
|
@ -142,15 +142,21 @@ struct Engine : EnginePool::PoolItem<&_engine_pool> {
|
|||||||
|
|
||||||
uint32 GetGRFID() const;
|
uint32 GetGRFID() const;
|
||||||
|
|
||||||
|
struct EngineTypeFilter {
|
||||||
|
VehicleType vt;
|
||||||
|
|
||||||
|
bool operator() (size_t index) { return Engine::Get(index)->type == this->vt; }
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an iterable ensemble of all valid engines of the given type
|
* Returns an iterable ensemble of all valid engines of the given type
|
||||||
* @param vt the VehicleType for engines to be valid
|
* @param vt the VehicleType for engines to be valid
|
||||||
* @param from index of the first engine to consider
|
* @param from index of the first engine to consider
|
||||||
* @return an iterable ensemble of all valid engines of the given type
|
* @return an iterable ensemble of all valid engines of the given type
|
||||||
*/
|
*/
|
||||||
static Pool::IterateWrapper<Engine> IterateType(VehicleType vt, size_t from = 0)
|
static Pool::IterateWrapperFiltered<Engine, EngineTypeFilter> IterateType(VehicleType vt, size_t from = 0)
|
||||||
{
|
{
|
||||||
return Pool::IterateWrapper<Engine>(from, [vt](size_t index) { return Engine::Get(index)->type == vt; });
|
return Pool::IterateWrapperFiltered<Engine, EngineTypeFilter>(from, EngineTypeFilter{ vt });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -83,15 +83,18 @@ public:
|
|||||||
return "admin";
|
return "admin";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct ServerNetworkAdminSocketHandlerFilter {
|
||||||
|
bool operator() (size_t index) { return ServerNetworkAdminSocketHandler::Get(index)->GetAdminStatus() == ADMIN_STATUS_ACTIVE; }
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an iterable ensemble of all active admin sockets
|
* Returns an iterable ensemble of all active admin sockets
|
||||||
* @param from index of the first socket to consider
|
* @param from index of the first socket to consider
|
||||||
* @return an iterable ensemble of all active admin sockets
|
* @return an iterable ensemble of all active admin sockets
|
||||||
*/
|
*/
|
||||||
static Pool::IterateWrapper<ServerNetworkAdminSocketHandler> IterateActive(size_t from = 0)
|
static Pool::IterateWrapperFiltered<ServerNetworkAdminSocketHandler, ServerNetworkAdminSocketHandlerFilter> IterateActive(size_t from = 0)
|
||||||
{
|
{
|
||||||
return Pool::IterateWrapper<ServerNetworkAdminSocketHandler>(from,
|
return Pool::IterateWrapperFiltered<ServerNetworkAdminSocketHandler, ServerNetworkAdminSocketHandlerFilter>(from, ServerNetworkAdminSocketHandlerFilter{});
|
||||||
[](size_t index) { return ServerNetworkAdminSocketHandler::Get(index)->GetAdminStatus() == ADMIN_STATUS_ACTIVE; });
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user