Merge branch 'master' into jgrpp
# Conflicts: # config.lib # projects/openttd_vs140.vcxproj # projects/openttd_vs140.vcxproj.filters # projects/openttd_vs141.vcxproj # projects/openttd_vs141.vcxproj.filters # projects/openttd_vs142.vcxproj # projects/openttd_vs142.vcxproj.filters # src/aircraft_cmd.cpp # src/base_station_base.h # src/core/pool_type.hpp # src/disaster_vehicle.cpp # src/economy.cpp # src/engine.cpp # src/group.h # src/group_cmd.cpp # src/group_gui.cpp # src/lang/english.txt # src/lang/german.txt # src/linkgraph/linkgraph_gui.cpp # src/network/network_command.cpp # src/network/network_server.cpp # src/openttd.cpp # src/order_cmd.cpp # src/road_cmd.cpp # src/saveload/afterload.cpp # src/saveload/cargopacket_sl.cpp # src/saveload/linkgraph_sl.cpp # src/saveload/order_sl.cpp # src/saveload/station_sl.cpp # src/saveload/town_sl.cpp # src/saveload/vehicle_sl.cpp # src/screenshot.cpp # src/screenshot.h # src/settings_gui.cpp # src/settings_type.h # src/smallmap_gui.cpp # src/station.cpp # src/station_cmd.cpp # src/table/settings.ini # src/toolbar_gui.cpp # src/town_cmd.cpp # src/train.h # src/train_cmd.cpp # src/train_gui.cpp # src/vehicle.cpp # src/vehicle_base.h # src/vehiclelist.cpp # src/window_type.h
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
|
||||
#include "smallvec_type.hpp"
|
||||
#include "enum_type.hpp"
|
||||
#include <functional>
|
||||
|
||||
/** Various types of a pool. */
|
||||
enum PoolType {
|
||||
@@ -137,6 +138,49 @@ struct Pool : PoolBase {
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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>
|
||||
struct PoolIterator {
|
||||
typedef T value_type;
|
||||
typedef T* pointer;
|
||||
typedef T& reference;
|
||||
typedef size_t difference_type;
|
||||
typedef std::forward_iterator_tag iterator_category;
|
||||
|
||||
explicit PoolIterator(size_t index, std::function<bool(size_t)> filter = nullptr) : index(index), filter(filter)
|
||||
{
|
||||
if (this->filter == nullptr) this->filter = [](size_t) { return true; };
|
||||
this->ValidateIndex();
|
||||
};
|
||||
|
||||
bool operator==(const PoolIterator &other) const { return this->index == other.index; }
|
||||
bool operator!=(const PoolIterator &other) const { return !(*this == other); }
|
||||
T * operator*() const { return T::Get(this->index); }
|
||||
PoolIterator & operator++() { this->index++; this->ValidateIndex(); return *this; }
|
||||
|
||||
private:
|
||||
size_t index;
|
||||
std::function<bool(size_t)> 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>
|
||||
struct IterateWrapper {
|
||||
size_t from;
|
||||
std::function<bool(size_t)> filter;
|
||||
IterateWrapper(size_t from = 0, std::function<bool(size_t)> filter = nullptr) : from(from), filter(filter) {}
|
||||
PoolIterator<T> begin() { return PoolIterator<T>(this->from, this->filter); }
|
||||
PoolIterator<T> end() { return PoolIterator<T>(T::GetPoolSize()); }
|
||||
bool empty() { return this->begin() == this->end(); }
|
||||
};
|
||||
|
||||
/**
|
||||
* Base class for all PoolItems
|
||||
* @tparam Tpool The pool this item is going to be part of
|
||||
@@ -145,6 +189,9 @@ struct Pool : PoolBase {
|
||||
struct PoolItem {
|
||||
Tindex index; ///< Index of this pool item
|
||||
|
||||
/** Type of the pool this item is going to be part of */
|
||||
typedef struct Pool<Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero> Pool;
|
||||
|
||||
/**
|
||||
* Allocates space for new Titem
|
||||
* @param size size of Titem
|
||||
@@ -292,6 +339,13 @@ struct Pool : PoolBase {
|
||||
* @note it's called only when CleaningPool()
|
||||
*/
|
||||
static inline void PreCleanPool() { }
|
||||
|
||||
/**
|
||||
* Returns an iterable ensemble of all valid Titem
|
||||
* @param from index of the first Titem to consider
|
||||
* @return an iterable ensemble of all valid Titem
|
||||
*/
|
||||
static Pool::IterateWrapper<Titem> Iterate(size_t from = 0) { return Pool::IterateWrapper<Titem>(from); }
|
||||
};
|
||||
|
||||
private:
|
||||
@@ -319,10 +373,4 @@ private:
|
||||
void FreeItem(size_t index);
|
||||
};
|
||||
|
||||
#define FOR_ALL_ITEMS_FROM(type, iter, var, start) \
|
||||
for (size_t iter = start; var = nullptr, iter < type::GetPoolSize(); iter++) \
|
||||
if ((var = type::Get(iter)) != nullptr)
|
||||
|
||||
#define FOR_ALL_ITEMS(type, iter, var) FOR_ALL_ITEMS_FROM(type, iter, var, 0)
|
||||
|
||||
#endif /* POOL_TYPE_HPP */
|
||||
|
||||
Reference in New Issue
Block a user