From 12e1ef78265a621dc51f7b9af245d81431835dcc Mon Sep 17 00:00:00 2001
From: Mathias Gumz <akira at fluxbox dot org>
Date: Wed, 15 Sep 2010 02:07:09 +0200
Subject: [PATCH] code deduplication by using <algorithm> and FbTk/STLUtil.hh

---
 src/FbTk/Container.cc |  70 +++++++---------------
 src/FbTk/STLUtil.hh   |  27 +++++++++
 src/FbTk/Theme.cc     |   9 +--
 src/FbWinFrame.cc     |  20 +++----
 src/IconbarTool.cc    |  10 ++--
 src/fluxbox.cc        | 136 ++++++++++++------------------------------
 6 files changed, 104 insertions(+), 168 deletions(-)

diff --git a/src/FbTk/Container.cc b/src/FbTk/Container.cc
index ce143ef9..e1d2e07c 100644
--- a/src/FbTk/Container.cc
+++ b/src/FbTk/Container.cc
@@ -26,6 +26,7 @@
 #include "TextUtils.hh"
 #include "EventManager.hh"
 #include "CompareEqual.hh"
+#include "STLUtil.hh"
 
 #include <algorithm>
 
@@ -78,7 +79,7 @@ void Container::insertItem(Item item, int pos) {
     } else if (pos == 0) {
         m_item_list.push_front(item);
     } else {
-        ItemList::iterator it = m_item_list.begin();
+        ItemList::iterator it = begin();
         for (; pos != 0; ++it, --pos)
             continue;
 
@@ -104,12 +105,10 @@ void Container::moveItem(Item item, int movement) {
     if (newindex < 0) // neg wrap
         newindex += size;
 
-    ItemList::iterator it = std::find(m_item_list.begin(),
-                                      m_item_list.end(),
-                                      item);
+    ItemList::iterator it = std::find(begin(), end(), item);
     m_item_list.erase(it);
 
-    for (it = m_item_list.begin(); newindex >= 0; ++it, --newindex) {
+    for (it = begin(); newindex >= 0; ++it, --newindex) {
         if (newindex == 0) {
             break;
         }
@@ -143,12 +142,11 @@ bool Container::moveItemTo(Item item, int x, int y) {
                                &itemwin))
         return false;
 
-    ItemList::iterator it = find_if(m_item_list.begin(),
-                                    m_item_list.end(),
+    ItemList::iterator it = find_if(begin(), end(),
                                     CompareWindow(&FbWindow::window,
                                                   itemwin));
     // not found :(
-    if (it == m_item_list.end())
+    if (it == end())
         return false;
 
     Window child_return = 0;
@@ -162,8 +160,8 @@ bool Container::moveItemTo(Item item, int x, int y) {
 }
 
 bool Container::removeItem(Item item) {
-    ItemList::iterator it = m_item_list.begin();
-    ItemList::iterator it_end = m_item_list.end();
+    ItemList::iterator it = begin();
+    ItemList::iterator it_end = end();
     for (; it != it_end && (*it) != item; ++it);
 
     if (it == it_end)
@@ -178,7 +176,7 @@ bool Container::removeItem(int index) {
     if (index < 0 || index > size())
         return false;
 
-    ItemList::iterator it = m_item_list.begin();
+    ItemList::iterator it = begin();
     for (; index != 0; ++it, --index)
         continue;
 
@@ -247,12 +245,11 @@ bool Container::tryExposeEvent(XExposeEvent &event) {
         return true;
     }
 
-    ItemList::iterator it = find_if(m_item_list.begin(),
-                                    m_item_list.end(),
+    ItemList::iterator it = find_if(begin(), end(),
                                     CompareWindow(&FbWindow::window,
                                                   event.window));
     // not found :(
-    if (it == m_item_list.end())
+    if (it == end())
         return false;
 
     (*it)->exposeEvent(event);
@@ -265,12 +262,11 @@ bool Container::tryButtonPressEvent(XButtonEvent &event) {
         return true;
     }
 
-    ItemList::iterator it = find_if(m_item_list.begin(),
-                                    m_item_list.end(),
+    ItemList::iterator it = find_if(begin(), end(),
                                     CompareWindow(&FbWindow::window,
                                                   event.window));
     // not found :(
-    if (it == m_item_list.end())
+    if (it == end())
         return false;
 
     (*it)->buttonPressEvent(event);
@@ -283,12 +279,11 @@ bool Container::tryButtonReleaseEvent(XButtonEvent &event) {
         return true;
     }
 
-    ItemList::iterator it = find_if(m_item_list.begin(),
-                                    m_item_list.end(),
+    ItemList::iterator it = find_if(begin(), end(),
                                     CompareWindow(&FbWindow::window,
                                                   event.window));
     // not found :(
-    if (it == m_item_list.end())
+    if (it == end())
         return false;
 
     (*it)->buttonReleaseEvent(event);
@@ -360,8 +355,8 @@ void Container::repositionItems() {
     }
 
 
-    ItemList::iterator it = m_item_list.begin();
-    const ItemList::iterator it_end = m_item_list.end();
+    ItemList::iterator it = begin();
+    const ItemList::iterator it_end = end();
 
     int rounding_error = 0;
 
@@ -442,41 +437,26 @@ unsigned int Container::maxWidthPerClient() const {
 }
 
 void Container::for_each(std::mem_fun_t<void, FbWindow> function) {
-    std::for_each(m_item_list.begin(),
-                  m_item_list.end(),
-                  function);
+    std::for_each(begin(), end(), function);
 }
 
 void Container::setAlpha(unsigned char alpha) {
     FbWindow::setAlpha(alpha);
-    ItemList::iterator it = m_item_list.begin();
-    ItemList::iterator it_end = m_item_list.end();
-    for (; it != it_end; ++it)
-        (*it)->setAlpha(alpha);
+    STLUtil::forAll(m_item_list, std::bind2nd(std::mem_fun(&Button::setAlpha), alpha));
 }
 
 void Container::parentMoved() {
     FbWindow::parentMoved();
-    ItemList::iterator it = m_item_list.begin();
-    ItemList::iterator it_end = m_item_list.end();
-    for (; it != it_end; ++it)
-        (*it)->parentMoved();
+    STLUtil::forAll(m_item_list, std::mem_fun(&Button::parentMoved));
 }
 
 void Container::invalidateBackground() {
     FbWindow::invalidateBackground();
-    ItemList::iterator it = m_item_list.begin();
-    ItemList::iterator it_end = m_item_list.end();
-    for (; it != it_end; ++it)
-        (*it)->invalidateBackground();
+    STLUtil::forAll(m_item_list, std::mem_fun(&Button::invalidateBackground));
 }
 
 void Container::clear() {
-    ItemList::iterator it = m_item_list.begin();
-    ItemList::iterator it_end = m_item_list.end();
-    for (; it != it_end; ++it)
-        (*it)->clear();
-
+    STLUtil::forAll(m_item_list, std::mem_fun(&Button::clear));
 }
 
 void Container::setOrientation(Orientation orient) {
@@ -484,11 +464,7 @@ void Container::setOrientation(Orientation orient) {
         return;
 
     FbWindow::invalidateBackground();
-
-    ItemList::iterator it = m_item_list.begin();
-    ItemList::iterator it_end = m_item_list.end();
-    for (; it != it_end; ++it)
-        (*it)->setOrientation(orient);
+    STLUtil::forAll(m_item_list, std::bind2nd(std::mem_fun(&Button::setOrientation), orient));
 
     if (((m_orientation == ROT0 || m_orientation == ROT180) &&
         (orient == ROT90 || orient == ROT270)) ||
diff --git a/src/FbTk/STLUtil.hh b/src/FbTk/STLUtil.hh
index e08036ad..b1007f7e 100644
--- a/src/FbTk/STLUtil.hh
+++ b/src/FbTk/STLUtil.hh
@@ -68,6 +68,33 @@ void destroyAndClearSecond(A &a) {
     a.clear();
 }
 
+
+template <typename C, typename F>
+F forAll(C& c, F f) {
+    typedef typename C::iterator iterator;
+    iterator i = c.begin();
+    iterator e = c.end();
+    for (; i != e; i++) {
+        f(*i);
+    }
+    return f;
+}
+
+template <typename C, typename I, typename F>
+F forAllIf(C& c, I i, F f) {
+    typedef typename C::iterator iterator;
+    iterator it = c.begin();
+    iterator end = c.end();
+    for (; it != end; it++) {
+        if (i(*it))
+            f(*it);
+    }
+    return f;
+}
+
+
+
+
 } // end namespace STLUtil
 } // end namespace FbTk
 
diff --git a/src/FbTk/Theme.cc b/src/FbTk/Theme.cc
index cd478fc2..c92e5636 100644
--- a/src/FbTk/Theme.cc
+++ b/src/FbTk/Theme.cc
@@ -27,6 +27,7 @@
 #include "FileUtil.hh"
 #include "I18n.hh"
 #include "Image.hh"
+#include "STLUtil.hh"
 
 #ifdef HAVE_CSTDIO
   #include <cstdio>
@@ -50,13 +51,11 @@ struct LoadThemeHelper {
     }
     void operator ()(ThemeManager::ThemeList &tmlist) {
 
-        for_each(tmlist.begin(), tmlist.end(),
-                 *this);
+        STLUtil::forAll(tmlist, *this);
         // send reconfiguration signal to theme and listeners
         ThemeManager::ThemeList::iterator it = tmlist.begin();
         ThemeManager::ThemeList::iterator it_end = tmlist.end();
         for (; it != it_end; ++it) {
-            (*it)->reconfigTheme();
             (*it)->reconfigSig().notify();
         }
     }
@@ -174,9 +173,7 @@ bool ThemeManager::load(const string &filename,
     // get list and go throu all the resources and load them
     // and then reconfigure them
     if (screen_num < 0 || screen_num > m_max_screens) {
-        for_each(m_themes.begin(),
-                 m_themes.end(),
-                 load_theme_helper);
+        STLUtil::forAll(m_themes, load_theme_helper);
     } else {
         load_theme_helper(m_themes[screen_num]);
     }
diff --git a/src/FbWinFrame.cc b/src/FbWinFrame.cc
index 4c56d497..9c359251 100644
--- a/src/FbWinFrame.cc
+++ b/src/FbWinFrame.cc
@@ -29,6 +29,7 @@
 #include "FbTk/Transparent.hh"
 #include "FbTk/CompareEqual.hh"
 #include "FbTk/TextUtils.hh"
+#include "FbTk/STLUtil.hh"
 
 #include "FbWinFrameTheme.hh"
 #include "Screen.hh"
@@ -42,6 +43,8 @@ using std::max;
 using std::mem_fun;
 using std::string;
 
+using FbTk::STLUtil::forAll;
+
 FbWinFrame::FbWinFrame(BScreen &screen, WindowState &state,
                        FocusableTheme<FbWinFrameTheme> &theme):
     m_screen(screen),
@@ -439,12 +442,8 @@ void FbWinFrame::notifyMoved(bool clear) {
 
         m_titlebar.parentMoved();
 
-        for_each(m_buttons_left.begin(),
-                 m_buttons_left.end(),
-                 mem_fun(&FbTk::Button::parentMoved));
-        for_each(m_buttons_right.begin(),
-                 m_buttons_right.end(),
-                 mem_fun(&FbTk::Button::parentMoved));
+        forAll(m_buttons_left, mem_fun(&FbTk::Button::parentMoved));
+        forAll(m_buttons_right, mem_fun(&FbTk::Button::parentMoved));
     }
 
     if (m_use_handle) {
@@ -463,13 +462,8 @@ void FbWinFrame::clearAll() {
 
     if  (m_use_titlebar) {
         redrawTitlebar();
-
-        for_each(m_buttons_left.begin(),
-                 m_buttons_left.end(),
-                 mem_fun(&FbTk::Button::clear));
-        for_each(m_buttons_right.begin(),
-                 m_buttons_right.end(),
-                 mem_fun(&FbTk::Button::clear));
+        forAll(m_buttons_left, mem_fun(&FbTk::Button::clear));
+        forAll(m_buttons_right, mem_fun(&FbTk::Button::clear));
     } else if (m_tabmode == EXTERNAL && m_use_tabs)
         m_tab_container.clear();
 
diff --git a/src/IconbarTool.cc b/src/IconbarTool.cc
index 14835cb5..cd47d45f 100644
--- a/src/IconbarTool.cc
+++ b/src/IconbarTool.cc
@@ -48,6 +48,9 @@
 #include "FbTk/MacroCommand.hh"
 #include "FbTk/MenuSeparator.hh"
 #include "FbTk/Util.hh"
+#include "FbTk/STLUtil.hh"
+#include "FbTk/Select2nd.hh"
+#include "FbTk/Compose.hh"
 
 #include <typeinfo>
 #include <iterator>
@@ -465,10 +468,9 @@ void IconbarTool::updateSizing() {
     m_icon_container.setBorderWidth(m_theme.border().width());
     m_icon_container.setBorderColor(m_theme.border().color());
 
-    IconMap::iterator icon_it = m_icons.begin();
-    const IconMap::iterator icon_it_end = m_icons.end();
-    for (; icon_it != icon_it_end; ++icon_it)
-        icon_it->second->reconfigTheme();
+    FbTk::STLUtil::forAll(m_icons, 
+            FbTk::Compose(std::mem_fun(&IconButton::reconfigTheme), 
+                FbTk::Select2nd<IconMap::value_type>()));
 
 }
 
diff --git a/src/fluxbox.cc b/src/fluxbox.cc
index 4d85bc17..61b740ac 100644
--- a/src/fluxbox.cc
+++ b/src/fluxbox.cc
@@ -387,10 +387,7 @@ Fluxbox::Fluxbox(int argc, char **argv, const char *dpy_name,
 #endif // REMEMBER
 
     // init all "screens"
-    ScreenList::iterator it = m_screen_list.begin();
-    ScreenList::iterator it_end = m_screen_list.end();
-    for(; it != it_end; ++it)
-        initScreen(*it);
+    STLUtil::forAll(m_screen_list, bind1st(mem_fun(&Fluxbox::initScreen), this));
 
     XAllowEvents(disp, ReplayPointer, CurrentTime);
 
@@ -435,11 +432,9 @@ void Fluxbox::initScreen(BScreen *screen) {
 
     // now we can create menus (which needs this screen to be in screen_list)
     screen->initMenus();
-
     screen->initWindows();
 
     // attach screen signals to this
-
     join(screen->workspaceAreaSig(),
          FbTk::MemFun(*this, &Fluxbox::workspaceAreaChanged));
 
@@ -458,11 +453,7 @@ void Fluxbox::initScreen(BScreen *screen) {
          FbTk::MemFun(*this, &Fluxbox::workspaceCountChanged));
 
     // initiate atomhandler for screen specific stuff
-    for (AtomHandlerContainerIt it= m_atomhandler.begin();
-         it != m_atomhandler.end();
-         it++) {
-        (*it)->initForScreen(*screen);
-    }
+    STLUtil::forAll(m_atomhandler, bind2nd(mem_fun(&AtomHandler::initForScreen), *screen));
 
     FocusControl::revertFocus(*screen); // make sure focus style is correct
 
@@ -984,11 +975,9 @@ void Fluxbox::update(FbTk::Subject *changedsub) {
     }
 
     if (fbwin && &fbwin->stateSig() == changedsub) { // state signal
-        for (AtomHandlerContainerIt it= m_atomhandler.begin();
-             it != m_atomhandler.end(); ++it) {
-            if ((*it)->update())
-                (*it)->updateState(*fbwin);
-        }
+        STLUtil::forAllIf(m_atomhandler, mem_fun(&AtomHandler::update),
+            bind2nd(mem_fun(&AtomHandler::updateState), *fbwin));
+
         // if window changed to iconic state
         // add to icon list
         if (fbwin->isIconic()) {
@@ -1009,17 +998,11 @@ void Fluxbox::update(FbTk::Subject *changedsub) {
             }
         }
     } else if (fbwin && &fbwin->layerSig() == changedsub) { // layer signal
-        AtomHandlerContainerIt it= m_atomhandler.begin();
-        for (; it != m_atomhandler.end(); ++it) {
-            if ((*it)->update())
-                (*it)->updateLayer(*fbwin);
-        }
+        STLUtil::forAllIf(m_atomhandler, mem_fun(&AtomHandler::update),
+            bind2nd(mem_fun(&AtomHandler::updateLayer), *fbwin));
     } else if (fbwin && &fbwin->dieSig() == changedsub) { // window death signal
-        AtomHandlerContainerIt it= m_atomhandler.begin();
-        for (; it != m_atomhandler.end(); ++it) {
-            if ((*it)->update())
-                (*it)->updateFrameClose(*fbwin);
-        }
+        STLUtil::forAllIf(m_atomhandler, mem_fun(&AtomHandler::update),
+            bind2nd(mem_fun(&AtomHandler::updateFrameClose), *fbwin));
 
         // make sure each workspace get this
         BScreen &scr = fbwin->screen();
@@ -1027,17 +1010,11 @@ void Fluxbox::update(FbTk::Subject *changedsub) {
         if (FocusControl::focusedFbWindow() == fbwin)
             FocusControl::setFocusedFbWindow(0);
     } else if (fbwin && &fbwin->workspaceSig() == changedsub) {  // workspace signal
-        for (AtomHandlerContainerIt it= m_atomhandler.begin();
-             it != m_atomhandler.end(); ++it) {
-            if ((*it)->update())
-                (*it)->updateWorkspace(*fbwin);
-        }
+        STLUtil::forAllIf(m_atomhandler, mem_fun(&AtomHandler::update),
+            bind2nd(mem_fun(&AtomHandler::updateClientClose), *fbwin));
     } else if (client && &client->dieSig() == changedsub) { // client death
-        for (AtomHandlerContainerIt it= m_atomhandler.begin();
-             it != m_atomhandler.end(); ++it) {
-            if ((*it)->update())
-                (*it)->updateClientClose(*client);
-        }
+        STLUtil::forAllIf(m_atomhandler, mem_fun(&AtomHandler::update),
+                bind2nd(mem_fun(&AtomHandler::updateClientClose), *client));
 
         BScreen &screen = client->screen();
 
@@ -1066,19 +1043,12 @@ void Fluxbox::attachSignals(FluxboxWindow &win) {
     win.workspaceSig().attach(this);
     win.layerSig().attach(this);
     win.dieSig().attach(this);
-    for (AtomHandlerContainerIt it= m_atomhandler.begin();
-         it != m_atomhandler.end(); ++it) {
-        (*it)->setupFrame(win);
-    }
+    STLUtil::forAll(m_atomhandler, bind2nd(mem_fun(&AtomHandler::setupFrame), win));
 }
 
 void Fluxbox::attachSignals(WinClient &winclient) {
     winclient.dieSig().attach(this);
-
-    for (AtomHandlerContainerIt it= m_atomhandler.begin();
-         it != m_atomhandler.end(); ++it) {
-        (*it)->setupClient(winclient);
-    }
+    STLUtil::forAll(m_atomhandler, bind2nd(mem_fun(&AtomHandler::setupClient), winclient));
 }
 
 BScreen *Fluxbox::searchScreen(Window window) {
@@ -1178,9 +1148,7 @@ void Fluxbox::shutdown() {
 
     XSetInputFocus(FbTk::App::instance()->display(), PointerRoot, None, CurrentTime);
 
-    //send shutdown to all screens
-    for_each(m_screen_list.begin(),
-             m_screen_list.end(), mem_fun(&BScreen::shutdown));
+    STLUtil::forAll(m_screen_list, mem_fun(&BScreen::shutdown));
 
     sync(false);
 }
@@ -1353,32 +1321,21 @@ void Fluxbox::real_reconfigure() {
     for (; screen_it != screen_it_end; ++screen_it)
         load_rc(*(*screen_it));
 
-    // reconfigure all screens
-    for_each(m_screen_list.begin(), m_screen_list.end(), mem_fun(&BScreen::reconfigure));
-
-    //reconfigure keys
+    STLUtil::forAll(m_screen_list, mem_fun(&BScreen::reconfigure));
     m_key->reconfigure();
-
-    // and atomhandlers
-    for (AtomHandlerContainerIt it= m_atomhandler.begin();
-         it != m_atomhandler.end();
-         it++) {
-        (*it)->reconfigure();
-    }
+    STLUtil::forAll(m_atomhandler, mem_fun(&AtomHandler::reconfigure));
 }
 
 BScreen *Fluxbox::findScreen(int id) {
-    ScreenList::iterator it = m_screen_list.begin();
-    ScreenList::iterator it_end = m_screen_list.end();
-    for (; it != it_end; ++it) {
-        if ((*it)->screenNumber() == id)
-            break;
-    }
 
-    if (it == m_screen_list.end())
-        return 0;
+    BScreen* result = 0;
+    ScreenList::iterator it = find_if(m_screen_list.begin(), m_screen_list.end(),
+            FbTk::CompareEqual<BScreen>(&BScreen::screenNumber, id));
 
-    return *it;
+    if (it != m_screen_list.end())
+        result = *it;
+
+    return result;
 }
 
 void Fluxbox::timed_reconfigure() {
@@ -1426,48 +1383,33 @@ bool Fluxbox::validateClient(const WinClient *client) const {
 }
 
 void Fluxbox::updateFrameExtents(FluxboxWindow &win) {
-    AtomHandlerContainerIt it = m_atomhandler.begin();
-    AtomHandlerContainerIt it_end = m_atomhandler.end();
-    for (; it != it_end; ++it ) {
-        (*it)->updateFrameExtents(win);
-    }
+    STLUtil::forAll(m_atomhandler, bind2nd(mem_fun(&AtomHandler::updateFrameExtents), win));
 }
 
 void Fluxbox::workspaceCountChanged( BScreen& screen ) {
-    for (AtomHandlerContainerIt it= m_atomhandler.begin();
-         it != m_atomhandler.end(); ++it) {
-        if ((*it)->update())
-            (*it)->updateWorkspaceCount(screen);
-    }
+    STLUtil::forAllIf(m_atomhandler, mem_fun(&AtomHandler::update),
+            bind2nd(mem_fun(&AtomHandler::updateWorkspaceCount), screen));
 }
 
 void Fluxbox::workspaceChanged( BScreen& screen ) {
-    for (AtomHandlerContainerIt it= m_atomhandler.begin();
-         it != m_atomhandler.end(); ++it) {
-        if ((*it)->update())
-            (*it)->updateCurrentWorkspace(screen);
-    }
+    STLUtil::forAllIf(m_atomhandler, mem_fun(&AtomHandler::update),
+            bind2nd(mem_fun(&AtomHandler::updateCurrentWorkspace), screen));
 }
 
 void Fluxbox::workspaceNamesChanged(BScreen &screen) {
-    for (AtomHandlerContainerIt it= m_atomhandler.begin();
-         it != m_atomhandler.end(); ++it) {
-        if ((*it)->update())
-            (*it)->updateWorkspaceNames(screen);
-    }
+    STLUtil::forAllIf(m_atomhandler, mem_fun(&AtomHandler::update),
+            bind2nd(mem_fun(&AtomHandler::updateWorkspaceNames), screen));
 }
 
 void Fluxbox::clientListChanged(BScreen &screen) {
-    for (AtomHandlerContainerIt it= m_atomhandler.begin();
-         it != m_atomhandler.end(); ++it) {
-        if ((*it)->update())
-            (*it)->updateClientList(screen);
-    }
+    STLUtil::forAllIf(m_atomhandler, mem_fun(&AtomHandler::update),
+            bind2nd(mem_fun(&AtomHandler::updateClientList), screen));
 }
 
 void Fluxbox::focusedWindowChanged(BScreen &screen, 
                                    FluxboxWindow* win, 
                                    WinClient* client) {
+
     for (AtomHandlerContainerIt it= m_atomhandler.begin();
          it != m_atomhandler.end(); it++) {
         (*it)->updateFocusedWindow(screen, client ? client->window() : 0 );
@@ -1475,9 +1417,7 @@ void Fluxbox::focusedWindowChanged(BScreen &screen,
 }
 
 void Fluxbox::workspaceAreaChanged(BScreen &screen) {
-    for (AtomHandlerContainerIt it= m_atomhandler.begin();
-         it != m_atomhandler.end(); ++it) {
-        if ((*it)->update())
-            (*it)->updateWorkarea(screen);
-    }
+    STLUtil::forAllIf(m_atomhandler, mem_fun(&AtomHandler::update),
+            bind2nd(mem_fun(&AtomHandler::updateWorkarea), screen));
 }
+