diff options
author | glx22 <glx@openttd.org> | 2021-04-28 21:24:24 +0200 |
---|---|---|
committer | Loïc Guilloux <glx22@users.noreply.github.com> | 2021-04-29 21:08:24 +0200 |
commit | 14e92bd8e241998ced263ee542965a71bbdd77a5 (patch) | |
tree | 08490feac7c2c15c97daee67dc3c1329fc703a95 /src/window_gui.h | |
parent | a61696d6c565ff92c6604b12eefe36198d094056 (diff) | |
download | openttd-14e92bd8e241998ced263ee542965a71bbdd77a5.tar.xz |
Codechange: Replace window related FOR_ALL with range-based for loops
Diffstat (limited to 'src/window_gui.h')
-rw-r--r-- | src/window_gui.h | 68 |
1 files changed, 62 insertions, 6 deletions
diff --git a/src/window_gui.h b/src/window_gui.h index 67a799c3d..66c867a54 100644 --- a/src/window_gui.h +++ b/src/window_gui.h @@ -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; |