Changeset - r25873:d10108931407
[Not reviewed]
master
0 1 0
Loïc Guilloux - 3 years ago 2021-08-10 17:03:15
glx22@users.noreply.github.com
Fix 68f2213: Don't use GetPoolSize() for end of pool iterator (#9461)
1 file changed with 12 insertions and 4 deletions:
0 comments (0 inline, 0 general)
src/core/pool_type.hpp
Show inline comments
 
@@ -157,25 +157,29 @@ struct Pool : PoolBase {
 
		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;
 
		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->index++;
 
			if (this->index >= T::GetPoolSize()) this->index = T::Pool::MAX_SIZE;
 
		}
 
	};
 

	
 
	/*
 
	 * 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;
 
		IterateWrapper(size_t from = 0) : from(from) {}
 
		PoolIterator<T> begin() { return PoolIterator<T>(this->from); }
 
		PoolIterator<T> end() { return PoolIterator<T>(T::GetPoolSize()); }
 
		PoolIterator<T> end() { return PoolIterator<T>(T::Pool::MAX_SIZE); }
 
		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
 
@@ -198,26 +202,30 @@ struct Pool : PoolBase {
 
		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++; }
 
		void ValidateIndex()
 
		{
 
			while (this->index < T::GetPoolSize() && !(T::IsValidID(this->index) && this->filter(this->index))) this->index++;
 
			if (this->index >= T::GetPoolSize()) this->index = T::Pool::MAX_SIZE;
 
		}
 
	};
 

	
 
	/*
 
	 * 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); }
 
		PoolIteratorFiltered<T, F> end() { return PoolIteratorFiltered<T, F>(T::Pool::MAX_SIZE, this->filter); }
 
		bool empty() { return this->begin() == this->end(); }
 
	};
 

	
 
	/**
 
	 * Base class for all PoolItems
 
	 * @tparam Tpool The pool this item is going to be part of
0 comments (0 inline, 0 general)