From 99f4d0dd8408e46a163e39b8fe5dd5d9a32af8eb Mon Sep 17 00:00:00 2001 From: Jarod42 Date: Mon, 30 Sep 2024 11:06:56 +0200 Subject: [PATCH] [cleanup] use for-range and algorithm instead of manual usage of iterators --- src/focushandler.cpp | 29 ++--- src/gui.cpp | 113 ++++++------------- src/widget.cpp | 216 ++++++++++++------------------------ src/widgets/container.cpp | 14 +-- src/widgets/dropdown.cpp | 7 +- src/widgets/listbox.cpp | 7 +- src/widgets/radiobutton.cpp | 14 +-- src/widgets/tabbedarea.cpp | 6 +- 8 files changed, 130 insertions(+), 276 deletions(-) diff --git a/src/focushandler.cpp b/src/focushandler.cpp index 8d0270a..d26da65 100644 --- a/src/focushandler.cpp +++ b/src/focushandler.cpp @@ -65,6 +65,8 @@ #include "guisan/keyinput.hpp" #include "guisan/widget.hpp" +#include + namespace gcn { @@ -296,15 +298,10 @@ namespace gcn mFocusedWidget = nullptr; } - WidgetIterator iter; - - for (iter = mWidgets.begin(); iter != mWidgets.end(); ++iter) + const auto iter = std::find(mWidgets.begin(), mWidgets.end(), widget); + if (iter != mWidgets.end()) { - if ((*iter) == widget) - { - mWidgets.erase(iter); - break; - } + mWidgets.erase(iter); } if (mDraggedWidget == widget) @@ -508,14 +505,10 @@ namespace gcn { Widget* sourceWidget = focusEvent.getSource(); - std::list focusListeners = sourceWidget->_getFocusListeners(); - // Send the event to all focus listeners of the widget. - for (std::list::iterator it = focusListeners.begin(); - it != focusListeners.end(); - ++it) + for (auto* focusListener : sourceWidget->_getFocusListeners()) { - (*it)->focusLost(focusEvent); + focusListener->focusLost(focusEvent); } } @@ -523,14 +516,10 @@ namespace gcn { Widget* sourceWidget = focusEvent.getSource(); - std::list focusListeners = sourceWidget->_getFocusListeners(); - // Send the event to all focus listeners of the widget. - for (std::list::iterator it = focusListeners.begin(); - it != focusListeners.end(); - ++it) + for (auto* focusListener : sourceWidget->_getFocusListeners()) { - (*it)->focusGained(focusEvent); + focusListener->focusGained(focusEvent); } } diff --git a/src/gui.cpp b/src/gui.cpp index 4c2700c..a2e8f91 100644 --- a/src/gui.cpp +++ b/src/gui.cpp @@ -313,10 +313,9 @@ namespace gcn if (mouseInput.getX() < 0 || mouseInput.getY() < 0 || !mTop->getDimension().isContaining(mouseInput.getX(), mouseInput.getY())) { - std::set::const_iterator iter; - for (iter = mLastWidgetsWithMouse.begin(); iter != mLastWidgetsWithMouse.end(); iter++) + for (Widget* w : mLastWidgetsWithMouse) { - distributeMouseEvent((*iter), + distributeMouseEvent(w, MouseEvent::Exited, mouseInput.getButton(), mouseInput.getX(), @@ -348,11 +347,9 @@ namespace gcn mLastWidgetsWithMouse.end(), std::inserter(mWidgetsWithMouseEntered, mWidgetsWithMouseEntered.begin())); - std::set::const_iterator iter; - for (iter = mWidgetsWithMouseExited.begin(); iter != mWidgetsWithMouseExited.end(); - iter++) + for (Widget* w : mWidgetsWithMouseExited) { - distributeMouseEvent((*iter), + distributeMouseEvent(w, MouseEvent::Exited, mouseInput.getButton(), mouseInput.getX(), @@ -366,10 +363,8 @@ namespace gcn mLastMousePressTimeStamp = 0; } - for (iter = mWidgetsWithMouseEntered.begin(); iter != mWidgetsWithMouseEntered.end(); - iter++) + for (Widget* widget : mWidgetsWithMouseEntered) { - Widget* widget = (*iter); // If a widget has modal mouse input focus we // only want to send entered events to that widget // and the widget's parents. @@ -650,44 +645,26 @@ namespace gcn mouseEvent.mX = x - widgetX; mouseEvent.mY = y - widgetY; mouseEvent.mDistributor = widget; - std::list mouseListeners = widget->_getMouseListeners(); // Send the event to all mouse listeners of the widget. - for (std::list::iterator it = mouseListeners.begin(); - it != mouseListeners.end(); - ++it) + for (MouseListener* mouseListener : widget->_getMouseListeners()) { switch (mouseEvent.getType()) { - case MouseEvent::Entered: - (*it)->mouseEntered(mouseEvent); - break; - case MouseEvent::Exited: - (*it)->mouseExited(mouseEvent); - break; - case MouseEvent::Moved: - (*it)->mouseMoved(mouseEvent); - break; - case MouseEvent::Pressed: - (*it)->mousePressed(mouseEvent); - break; - case MouseEvent::Released: - (*it)->mouseReleased(mouseEvent); - break; - case MouseEvent::WheelMovedUp: - (*it)->mouseWheelMovedUp(mouseEvent); - break; - case MouseEvent::WheelMovedDown: - (*it)->mouseWheelMovedDown(mouseEvent); - break; - case MouseEvent::Dragged: - (*it)->mouseDragged(mouseEvent); - break; - case MouseEvent::Clicked: - (*it)->mouseClicked(mouseEvent); - break; - default: - throw GCN_EXCEPTION("Unknown mouse event type."); + case MouseEvent::Entered: mouseListener->mouseEntered(mouseEvent); break; + case MouseEvent::Exited: mouseListener->mouseExited(mouseEvent); break; + case MouseEvent::Moved: mouseListener->mouseMoved(mouseEvent); break; + case MouseEvent::Pressed: mouseListener->mousePressed(mouseEvent); break; + case MouseEvent::Released: mouseListener->mouseReleased(mouseEvent); break; + case MouseEvent::WheelMovedUp: + mouseListener->mouseWheelMovedUp(mouseEvent); + break; + case MouseEvent::WheelMovedDown: + mouseListener->mouseWheelMovedDown(mouseEvent); + break; + case MouseEvent::Dragged: mouseListener->mouseDragged(mouseEvent); break; + case MouseEvent::Clicked: mouseListener->mouseClicked(mouseEvent); break; + default: throw GCN_EXCEPTION("Unknown mouse event type."); } } @@ -752,23 +729,15 @@ namespace gcn if (widget->isEnabled()) { keyEvent.mDistributor = widget; - std::list keyListeners = widget->_getKeyListeners(); // Send the event to all key listeners of the source widget. - for (std::list::iterator it = keyListeners.begin(); - it != keyListeners.end(); - ++it) + for (KeyListener* keyListener : widget->_getKeyListeners()) { switch (keyEvent.getType()) { - case KeyEvent::Pressed: - (*it)->keyPressed(keyEvent); - break; - case KeyEvent::Released: - (*it)->keyReleased(keyEvent); - break; - default: - throw GCN_EXCEPTION("Unknown key event type."); + case KeyEvent::Pressed: keyListener->keyPressed(keyEvent); break; + case KeyEvent::Released: keyListener->keyReleased(keyEvent); break; + default: throw GCN_EXCEPTION("Unknown key event type."); } } } @@ -789,20 +758,13 @@ namespace gcn void Gui::distributeKeyEventToGlobalKeyListeners(KeyEvent& keyEvent) { - KeyListenerListIterator it; - - for (it = mKeyListeners.begin(); it != mKeyListeners.end(); it++) + for (KeyListener* keyListener : mKeyListeners) { switch (keyEvent.getType()) { - case KeyEvent::Pressed: - (*it)->keyPressed(keyEvent); - break; - case KeyEvent::Released: - (*it)->keyReleased(keyEvent); - break; - default: - throw GCN_EXCEPTION("Unknown key event type."); + case KeyEvent::Pressed: keyListener->keyPressed(keyEvent); break; + case KeyEvent::Released: keyListener->keyReleased(keyEvent); break; + default: throw GCN_EXCEPTION("Unknown key event type."); } if (keyEvent.isConsumed()) @@ -858,17 +820,10 @@ namespace gcn // and send them a mouse exited event. std::set mWidgetsWithMouse = getWidgetsAt(mLastMouseX, mLastMouseY); - for (std::set::const_iterator iter = mWidgetsWithMouse.begin(); - iter != mWidgetsWithMouse.end(); - iter++) + for (Widget* w : mWidgetsWithMouse) { - distributeMouseEvent((*iter), - MouseEvent::Exited, - mLastMousePressButton, - mLastMouseX, - mLastMouseY, - true, - true); + distributeMouseEvent( + w, MouseEvent::Exited, mLastMousePressButton, mLastMouseX, mLastMouseY, true, true); } mFocusHandler->setLastWidgetWithModalMouseInputFocus(mFocusHandler->getModalMouseInputFocused()); @@ -880,11 +835,9 @@ namespace gcn // and send them a mouse entered event. std::set mWidgetsWithMouse = getWidgetsAt(mLastMouseX, mLastMouseY); - for (std::set::const_iterator iter = mWidgetsWithMouse.begin(); - iter != mWidgetsWithMouse.end(); - ++iter) + for (Widget* w : mWidgetsWithMouse) { - distributeMouseEvent((*iter), + distributeMouseEvent(w, MouseEvent::Entered, mLastMousePressButton, mLastMouseX, diff --git a/src/widget.cpp b/src/widget.cpp index e322428..359fbc7 100644 --- a/src/widget.cpp +++ b/src/widget.cpp @@ -94,17 +94,15 @@ namespace gcn mParent->remove(this); } - std::list::const_iterator childrenIter; - for (childrenIter = mChildren.begin(); childrenIter != mChildren.end(); childrenIter++) + for (Widget* w : mChildren) { - (*childrenIter)->_setParent(nullptr); + w->_setParent(nullptr); } - std::list::const_iterator deathIter; - for (deathIter = mDeathListeners.begin(); deathIter != mDeathListeners.end(); ++deathIter) + for (DeathListener* deathListener : mDeathListeners) { - Event event(this); - (*deathIter)->death(event); + const Event event(this); + deathListener->death(event); } _setFocusHandler(nullptr); @@ -395,12 +393,11 @@ namespace gcn return; } - std::list::const_iterator iter; - for (iter = mChildren.begin(); iter != mChildren.end(); iter++) + for (Widget* w : mChildren) { - if (widgetExists(*iter)) + if (widgetExists(w)) { - (*iter)->_setFocusHandler(focusHandler); + w->_setFocusHandler(focusHandler); } } } @@ -507,12 +504,11 @@ namespace gcn { mGlobalFont = font; - std::list::iterator iter; - for (iter = mWidgetInstances.begin(); iter != mWidgetInstances.end(); ++iter) + for (Widget* w : mWidgetInstances) { - if ((*iter)->mCurrentFont == nullptr) + if (w->mCurrentFont == nullptr) { - (*iter)->fontChanged(); + w->fontChanged(); } } } @@ -525,16 +521,7 @@ namespace gcn bool Widget::widgetExists(const Widget* widget) { - std::list::const_iterator iter; - for (iter = mWidgetInstances.begin(); iter != mWidgetInstances.end(); ++iter) - { - if (*iter == widget) - { - return true; - } - } - - return false; + return std::find(mWidgetInstances.begin(), mWidgetInstances.end(), widget) != mWidgetInstances.end(); } bool Widget::isTabInEnabled() const @@ -698,16 +685,15 @@ namespace gcn { mInternalFocusHandler = focusHandler; - std::list::const_iterator iter; - for (iter = mChildren.begin(); iter != mChildren.end(); iter++) + for (Widget* w : mChildren) { if (mInternalFocusHandler == nullptr) { - (*iter)->_setFocusHandler(_getFocusHandler()); + w->_setFocusHandler(_getFocusHandler()); } else { - (*iter)->_setFocusHandler(mInternalFocusHandler); + w->_setFocusHandler(mInternalFocusHandler); } } } @@ -724,55 +710,51 @@ namespace gcn void Widget::distributeResizedEvent() { - std::list::const_iterator iter; + const Event event(this); - for (iter = mWidgetListeners.begin(); iter != mWidgetListeners.end(); ++iter) + for (WidgetListener* widgetListener : mWidgetListeners) { - Event event(this); - (*iter)->widgetResized(event); + widgetListener->widgetResized(event); } } void Widget::distributeMovedEvent() { - std::list::const_iterator iter; + const Event event(this); - for (iter = mWidgetListeners.begin(); iter != mWidgetListeners.end(); ++iter) + for (WidgetListener* widgetListener : mWidgetListeners) { - Event event(this); - (*iter)->widgetMoved(event); + widgetListener->widgetMoved(event); } } void Widget::distributeHiddenEvent() { - std::list::const_iterator iter; + const Event event(this); - for (iter = mWidgetListeners.begin(); iter != mWidgetListeners.end(); ++iter) + for (WidgetListener* widgetListener : mWidgetListeners) { - Event event(this); - (*iter)->widgetHidden(event); + widgetListener->widgetHidden(event); } } void Widget::distributeActionEvent() { - std::list::const_iterator iter; - for (iter = mActionListeners.begin(); iter != mActionListeners.end(); ++iter) + const ActionEvent actionEvent(this, mActionEventId); + + for (ActionListener* actionListener : mActionListeners) { - ActionEvent actionEvent(this, mActionEventId); - (*iter)->action(actionEvent); + actionListener->action(actionEvent); } } void Widget::distributeShownEvent() { - std::list::const_iterator iter; + const Event event(this); - for (iter = mWidgetListeners.begin(); iter != mWidgetListeners.end(); ++iter) + for (WidgetListener* widgetListener : mWidgetListeners) { - Event event(this); - (*iter)->widgetShown(event); + widgetListener->widgetShown(event); } } @@ -804,10 +786,8 @@ namespace gcn { std::list result; - std::list::const_iterator iter; - for (iter = mChildren.begin(); iter != mChildren.end(); iter++) + for (Widget* widget : mChildren) { - Widget* widget = (*iter); if (ignore != widget && widget->getDimension().isIntersecting(area)) { result.push_back(widget); @@ -819,20 +799,12 @@ namespace gcn void Widget::resizeToChildren() { - int w = 0, h = 0; - std::list::const_iterator iter; - for (iter = mChildren.begin(); iter != mChildren.end(); iter++) + int w = 0; + int h = 0; + for (Widget* widget : mChildren) { - Widget* widget = (*iter); - if (widget->getX() + widget->getWidth() > w) - { - w = widget->getX() + widget->getWidth(); - } - - if (widget->getY() + widget->getHeight() > h) - { - h = widget->getY() + widget->getHeight(); - } + w = std::max(w, widget->getX() + widget->getWidth()); + h = std::max(h, widget->getY() + widget->getHeight()); } setSize(w, h); @@ -840,24 +812,17 @@ namespace gcn Widget* Widget::findWidgetById(const std::string& id) { - std::list::const_iterator iter; - for (iter = mChildren.begin(); iter != mChildren.end(); iter++) + for (Widget* widget : mChildren) { - Widget* widget = (*iter); - if (widget->getId() == id) { return widget; } - - Widget* child = widget->findWidgetById(id); - - if (child != nullptr) + if (Widget* child = widget->findWidgetById(id)) { return child; } } - return nullptr; } @@ -891,32 +856,27 @@ namespace gcn void Widget::clear() { - std::list::const_iterator iter; - for (iter = mChildren.begin(); iter != mChildren.end(); iter++) + for (Widget* widget : mChildren) { - Widget* widget = (*iter); widget->_setFocusHandler(nullptr); widget->_setParent(nullptr); } - mChildren.clear(); } void Widget::remove(Widget* widget) { - std::list::iterator iter; - for (iter = mChildren.begin(); iter != mChildren.end(); iter++) + auto it = std::find(mChildren.begin(), mChildren.end(), widget); + if (it == mChildren.end()) { - if (*iter == widget) - { - mChildren.erase(iter); - widget->_setFocusHandler(nullptr); - widget->_setParent(nullptr); - return; - } + throw GCN_EXCEPTION("There is no such widget in this container."); + } + else + { + mChildren.erase(it); + widget->_setFocusHandler(nullptr); + widget->_setParent(nullptr); } - - throw GCN_EXCEPTION("There is no such widget in this container."); } void Widget::add(Widget* widget) @@ -937,8 +897,7 @@ namespace gcn void Widget::moveToTop(Widget* widget) { - std::list::iterator iter; - iter = std::find(mChildren.begin(), mChildren.end(), widget); + const auto iter = std::find(mChildren.begin(), mChildren.end(), widget); if (iter == mChildren.end()) { @@ -951,8 +910,7 @@ namespace gcn void Widget::moveToBottom(Widget* widget) { - std::list::iterator iter; - iter = find(mChildren.begin(), mChildren.end(), widget); + const auto iter = find(mChildren.begin(), mChildren.end(), widget); if (iter == mChildren.end()) { @@ -965,70 +923,37 @@ namespace gcn void Widget::focusNext() { - std::list::const_iterator iter; - - for (iter = mChildren.begin(); iter != mChildren.end(); iter++) - { - if ((*iter)->isFocused()) - { - break; - } - } + const auto focusedIt = std::find_if( + mChildren.begin(), mChildren.end(), [](auto* w) { return w->isFocused(); }); - std::list::const_iterator end = iter; + const auto next = focusedIt == mChildren.end() ? mChildren.begin() : std::next(focusedIt); + auto it = std::find_if(next, mChildren.end(), [](auto* w) { return w->isFocusable(); }); - if (iter == mChildren.end()) + if (it == mChildren.end()) { - iter = mChildren.begin(); + it = std::find_if(mChildren.begin(), next, [](auto* w) { return w->isFocusable(); }); } - - iter++; - - for (; iter != end; iter++) + if (it != next) { - if (iter == mChildren.end()) - { - iter = mChildren.begin(); - } - - if ((*iter)->isFocusable()) - { - (*iter)->requestFocus(); - return; - } + (*it)->requestFocus(); } } void Widget::focusPrevious() { - auto iter = mChildren.rbegin(); - for (; iter != mChildren.rend(); ++iter) - { - if ((*iter)->isFocused()) - { - break; - } - } - const auto end = iter; - iter++; + const auto focusedRit = std::find_if( + mChildren.rbegin(), mChildren.rend(), [](auto* w) { return w->isFocused(); }); + + const auto next = focusedRit == mChildren.rend() ? mChildren.rbegin() : std::next(focusedRit); + auto rit = std::find_if(next, mChildren.rend(), [](auto* w) { return w->isFocusable(); }); - if (iter == mChildren.rend()) + if (rit == mChildren.rend()) { - iter = mChildren.rbegin(); + rit = std::find_if(mChildren.rbegin(), next, [](auto* w) { return w->isFocusable(); }); } - - for (; iter != end; iter++) + if (rit != next) { - if (iter == mChildren.rend()) - { - iter = mChildren.rbegin(); - } - - if ((*iter)->isFocusable()) - { - (*iter)->requestFocus(); - return; - } + (*rit)->requestFocus(); } } @@ -1072,10 +997,9 @@ namespace gcn { logic(); - std::list::const_iterator iter; - for (iter = mChildren.begin(); iter != mChildren.end(); iter++) + for (Widget* w : mChildren) { - (*iter)->_logic(); + w->_logic(); } } diff --git a/src/widgets/container.cpp b/src/widgets/container.cpp index 641578f..0a3ed3a 100644 --- a/src/widgets/container.cpp +++ b/src/widgets/container.cpp @@ -130,23 +130,21 @@ namespace gcn void Container::distributeWidgetAddedEvent(Widget* source) { - ContainerListenerIterator iter; + const ContainerEvent event(source, this); - for (iter = mContainerListeners.begin(); iter != mContainerListeners.end(); ++iter) + for (ContainerListener* containerListener : mContainerListeners) { - ContainerEvent event(source, this); - (*iter)->widgetAdded(event); + containerListener->widgetAdded(event); } } void Container::distributeWidgetRemovedEvent(Widget* source) { - ContainerListenerIterator iter; + ContainerEvent event(source, this); - for (iter = mContainerListeners.begin(); iter != mContainerListeners.end(); ++iter) + for (ContainerListener* containerListener : mContainerListeners) { - ContainerEvent event(source, this); - (*iter)->widgetRemoved(event); + containerListener->widgetRemoved(event); } } diff --git a/src/widgets/dropdown.cpp b/src/widgets/dropdown.cpp index 166b3b9..8d84fd3 100644 --- a/src/widgets/dropdown.cpp +++ b/src/widgets/dropdown.cpp @@ -599,12 +599,11 @@ namespace gcn void DropDown::distributeValueChangedEvent() { - SelectionListenerIterator iter; + const SelectionEvent event(this); - for (iter = mSelectionListeners.begin(); iter != mSelectionListeners.end(); ++iter) + for (SelectionListener* selectionListener : mSelectionListeners) { - SelectionEvent event(this); - (*iter)->valueChanged(event); + selectionListener->valueChanged(event); } } } diff --git a/src/widgets/listbox.cpp b/src/widgets/listbox.cpp index 4606b91..c256642 100644 --- a/src/widgets/listbox.cpp +++ b/src/widgets/listbox.cpp @@ -350,12 +350,11 @@ namespace gcn void ListBox::distributeValueChangedEvent() { - SelectionListenerIterator iter; + const SelectionEvent event(this); - for (iter = mSelectionListeners.begin(); iter != mSelectionListeners.end(); ++iter) + for (SelectionListener* selectionListener : mSelectionListeners) { - SelectionEvent event(this); - (*iter)->valueChanged(event); + selectionListener->valueChanged(event); } } diff --git a/src/widgets/radiobutton.cpp b/src/widgets/radiobutton.cpp index 10a7f2b..8522726 100644 --- a/src/widgets/radiobutton.cpp +++ b/src/widgets/radiobutton.cpp @@ -195,12 +195,9 @@ namespace gcn { if (selected && !mGroup.empty()) { - GroupIterator iter, iterEnd; - iterEnd = mGroupMap.upper_bound(mGroup); + auto range = mGroupMap.equal_range(mGroup); - for (iter = mGroupMap.lower_bound(mGroup); - iter != iterEnd; - iter++) + for (auto iter = range.first; iter != range.second; iter++) { if (iter->second->isSelected()) { @@ -253,12 +250,9 @@ namespace gcn { if (!mGroup.empty()) { - GroupIterator iter, iterEnd; - iterEnd = mGroupMap.upper_bound(mGroup); + auto range = mGroupMap.equal_range(mGroup); - for (iter = mGroupMap.lower_bound(mGroup); - iter != iterEnd; - iter++) + for (auto iter = range.first; iter != range.second; iter++) { if (iter->second == this) { diff --git a/src/widgets/tabbedarea.cpp b/src/widgets/tabbedarea.cpp index c397672..e49c1f3 100644 --- a/src/widgets/tabbedarea.cpp +++ b/src/widgets/tabbedarea.cpp @@ -160,8 +160,7 @@ namespace gcn } } - std::vector >::iterator iter; - for (iter = mTabs.begin(); iter != mTabs.end(); iter++) + for (auto iter = mTabs.begin(); iter != mTabs.end(); ++iter) { if (iter->first == tab) { @@ -171,8 +170,7 @@ namespace gcn } } - std::vector::iterator iter2; - for (iter2 = mTabsToDelete.begin(); iter2 != mTabsToDelete.end(); iter2++) + for (auto iter2 = mTabsToDelete.begin(); iter2 != mTabsToDelete.end(); ++iter2) { if (*iter2 == tab) {