Codechange: Replace window related FOR_ALL with range-based for loops

This commit is contained in:
glx22
2021-04-28 21:24:24 +02:00
committed by Loïc Guilloux
parent a61696d6c5
commit 14e92bd8e2
6 changed files with 113 additions and 106 deletions

View File

@@ -810,6 +810,68 @@ public:
* @pre this->IsNewGRFInspectable()
*/
virtual void ShowNewGRFInspectWindow() const { NOT_REACHED(); }
/**
* Iterator to iterate all valid Windows
* @tparam T Type of the class/struct that is going to be iterated
* @tparam Tfront Wether we iterate from front
*/
template <class T, bool Tfront>
struct WindowIterator {
typedef T value_type;
typedef T *pointer;
typedef T &reference;
typedef size_t difference_type;
typedef std::forward_iterator_tag iterator_category;
explicit WindowIterator(T *start) : w(start)
{
this->Validate();
}
bool operator==(const WindowIterator &other) const { return this->w == other.w; }
bool operator!=(const WindowIterator &other) const { return !(*this == other); }
T * operator*() const { return this->w; }
WindowIterator & operator++() { this->Next(); this->Validate(); return *this; }
private:
T *w;
void Validate() { while (this->w != nullptr && this->w->window_class == WC_INVALID) this->Next(); }
void Next() { if (this->w != nullptr) this->w = Tfront ? this->w->z_back : this->w->z_front; }
};
/**
* Iterable ensemble of all valid Windows
* @tparam T Type of the class/struct that is going to be iterated
* @tparam Tfront Wether we iterate from front
*/
template <class T, bool Tfront>
struct Iterate {
Iterate(T *from) : from(from) {}
WindowIterator<T, Tfront> begin() { return WindowIterator<T, Tfront>(this->from); }
WindowIterator<T, Tfront> end() { return WindowIterator<T, Tfront>(nullptr); }
bool empty() { return this->begin() == this->end(); }
private:
T *from;
};
/**
* Returns an iterable ensemble of all valid Window from back to front
* @tparam T Type of the class/struct that is going to be iterated
* @param from index of the first Window to consider
* @return an iterable ensemble of all valid Window
*/
template <class T = Window>
static Iterate<T, false> IterateFromBack(T *from = _z_back_window) { return Iterate<T, false>(from); }
/**
* Returns an iterable ensemble of all valid Window from front to back
* @tparam T Type of the class/struct that is going to be iterated
* @param from index of the first Window to consider
* @return an iterable ensemble of all valid Window
*/
template <class T = Window>
static Iterate<T, true> IterateFromFront(T *from = _z_front_window) { return Iterate<T, true>(from); }
};
/**
@@ -888,12 +950,6 @@ void GuiShowTooltips(Window *parent, StringID str, uint paramcount = 0, const ui
/* widget.cpp */
int GetWidgetFromPos(const Window *w, int x, int y);
/** Iterate over all windows */
#define FOR_ALL_WINDOWS_FROM_BACK_FROM(w, start) for (w = start; w != nullptr; w = w->z_front) if (w->window_class != WC_INVALID)
#define FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, start) for (w = start; w != nullptr; w = w->z_back) if (w->window_class != WC_INVALID)
#define FOR_ALL_WINDOWS_FROM_BACK(w) FOR_ALL_WINDOWS_FROM_BACK_FROM(w, _z_back_window)
#define FOR_ALL_WINDOWS_FROM_FRONT(w) FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, _z_front_window)
extern Point _cursorpos_drag_start;
extern int _scrollbar_start_pos;