]> git.tdb.fi Git - libs/gui.git/commitdiff
Streamline iterators and loop constructs
authorMikko Rasa <tdb@tdb.fi>
Sun, 27 Nov 2022 09:21:01 +0000 (11:21 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 27 Nov 2022 14:20:17 +0000 (16:20 +0200)
Use range-based for loops where appropriate and the auto type instead of
spelling out the entire iterator type.

source/graphics/cocoa/display.cpp
source/graphics/display.cpp
source/graphics/imageloader.cpp
source/graphics/windows/display.cpp
source/graphics/x11/display.cpp
source/input/bindings.cpp
source/input/controlscheme.cpp
source/input/hub.cpp
source/input/keys.cpp
source/input/linux/gamecontroller.cpp

index b9ce492771a4b265d162dc7f61a9e234b723ad79..31aa9a2a7fdc231462d8085bc2351b9e3d8e6443 100644 (file)
@@ -35,7 +35,7 @@ bool Display::process_events()
        if(!get_event(priv->display, &event.cevent))
                return false;
 
-       map<WindowHandle, Window *>::iterator i = priv->windows.find(event.cevent.any.window);
+       auto i = priv->windows.find(event.cevent.any.window);
        if(i!=priv->windows.end())
                i->second->event(event);
 
index 7f0b25af22f77d177ef244daa3997bc809b7951f..f40e02e572f6f7965d17e2a2fcadaf8173a00fa7 100644 (file)
@@ -32,9 +32,9 @@ const VideoMode &Display::get_desktop_mode() const
 
 void Display::restore_mode()
 {
-       for(list<Monitor>::const_iterator i=monitors.begin(); i!=monitors.end(); ++i)
-               if(i->desktop_settings.mode)
-                       set_mode(*i->desktop_settings.mode, false);
+       for(const Monitor &m: monitors)
+               if(m.desktop_settings.mode)
+                       set_mode(*m.desktop_settings.mode, false);
 }
 
 const VideoMode *Display::find_mode(const VideoMode &mode, float rate_tolerance) const
@@ -47,25 +47,25 @@ const VideoMode *Display::find_mode(const VideoMode &mode, float rate_tolerance)
                if(const VideoMode *current_mode = mode.monitor->current_settings.mode)
                        target_rate = current_mode->rate;
 
-       for(list<VideoMode>::const_iterator i=modes.begin(); i!=modes.end(); ++i)
+       for(const VideoMode &m: modes)
        {
-               if(mode.monitor && i->monitor!=mode.monitor)
+               if(mode.monitor && m.monitor!=mode.monitor)
                        continue;
                // Only check rate tolerance against an explicitly requested rate
-               if(mode.rate && (i->rate<mode.rate-rate_tolerance || i->rate>mode.rate+rate_tolerance))
+               if(mode.rate && (m.rate<mode.rate-rate_tolerance || m.rate>mode.rate+rate_tolerance))
                        continue;
-               if(i->width!=mode.width || i->height!=mode.height)
+               if(m.width!=mode.width || m.height!=mode.height)
                        continue;
 
                float ref_rate = target_rate;
                if(!ref_rate)
-                       if(const VideoMode *current_mode = i->monitor->current_settings.mode)
+                       if(const VideoMode *current_mode = m.monitor->current_settings.mode)
                                ref_rate = current_mode->rate;
 
-               float d = abs(i->rate-ref_rate);
+               float d = abs(m.rate-ref_rate);
                if(!closest || d<rate_diff)
                {
-                       closest = &*i;
+                       closest = &m;
                        rate_diff = d;
                }
        }
index 859a34c12894a1a0460b439e230a0fe9154d1385..9b8a42def04a6928162000a967bbefe2e71cb226 100644 (file)
@@ -35,8 +35,8 @@ ImageLoader::~ImageLoader()
 bool ImageLoader::detect_signature(const std::string &sig)
 {
        Registry &registry = get_registry();
-       for(list<RegisterBase *>::const_iterator i=registry.loaders.begin(); i!=registry.loaders.end(); ++i)
-               if((*i)->detect(sig))
+       for(const RegisterBase *r: registry.loaders)
+               if(r->detect(sig))
                        return true;
        return false;
 }
@@ -72,7 +72,7 @@ ImageLoader *ImageLoader::open_io(IO::Seekable &io)
        unsigned sig_len = io.read(&signature[0], signature.size());
 
        ImageLoader *loader = 0;
-       for(list<RegisterBase *>::const_iterator i=registry.loaders.begin(); (!loader && i!=registry.loaders.end()); ++i)
+       for(auto i=registry.loaders.begin(); (!loader && i!=registry.loaders.end()); ++i)
                if((*i)->detect(signature))
                        loader = (*i)->create(io);
 
@@ -147,8 +147,8 @@ ImageLoader::Registry::Registry():
 
 ImageLoader::Registry::~Registry()
 {
-       for(list<RegisterBase *>::iterator i=loaders.begin(); i!=loaders.end(); ++i)
-               delete *i;
+       for(auto l: loaders)
+               delete l;
 }
 
 } // namespace Graphics
index 61fe9837d6d1db0e4cc9f7da8edcbd1f02e9c3bd..b5f97ad56cee2a4beda4ea0c1f776d55bd23d263 100644 (file)
@@ -90,9 +90,9 @@ void Display::set_mode(const VideoMode &requested_mode, bool)
        if(ret!=DISP_CHANGE_SUCCESSFUL)
                throw unsupported_video_mode(requested_mode);
 
-       for(list<Monitor>::iterator i=monitors.begin(); i!=monitors.end(); ++i)
-               if(&*i==mode->monitor)
-                       i->current_settings.mode = mode;
+       for(Monitor &m: monitors)
+               if(&m==mode->monitor)
+                       m.current_settings.mode = mode;
 }
 
 bool Display::process_events()
index 972cc8236f25024eec7fee0167811a4aeb76814b..949509ff52ac62b5a3e3c3a5df20b4c2e19fc1fb 100644 (file)
@@ -165,7 +165,7 @@ Display::Display(const string &disp_name):
 
                                for(int j=0; j<output->nmode; ++j)
                                {
-                                       map<RRMode, XRRModeInfo *>::iterator k = modes_by_id.find(output->modes[j]);
+                                       auto k = modes_by_id.find(output->modes[j]);
                                        if(k==modes_by_id.end())
                                                continue;
 
@@ -197,27 +197,27 @@ Display::Display(const string &disp_name):
 
                        monitors.sort(monitor_x_compare);
                        Monitor *prev_enabled = 0;
-                       for(list<Monitor>::iterator i=monitors.begin(); i!=monitors.end(); ++i)
-                               if(i->desktop_settings.mode)
+                       for(Monitor &m: monitors)
+                               if(m.desktop_settings.mode)
                                {
-                                       i->next_left = prev_enabled;
+                                       m.next_left = prev_enabled;
                                        if(prev_enabled)
-                                               prev_enabled->next_right = &*i;
-                                       prev_enabled = &*i;
+                                               prev_enabled->next_right = &m;
+                                       prev_enabled = &m;
                                }
 
                        if(!primary_monitor || !primary_monitor->desktop_settings.mode)
                        {
                                // XRandR didn't give a sensible primary monitor.  Try to guess one.
                                unsigned largest = 0;
-                               for(list<Monitor>::iterator i=monitors.begin(); i!=monitors.end(); ++i)
-                                       if(const VideoMode *desktop_mode = i->desktop_settings.mode)
+                               for(Monitor &m: monitors)
+                                       if(const VideoMode *desktop_mode = m.desktop_settings.mode)
                                        {
                                                unsigned size = desktop_mode->width*desktop_mode->height;
                                                if(size>largest)
                                                {
                                                        largest = size;
-                                                       primary_monitor = &*i;
+                                                       primary_monitor = &m;
                                                }
                                        }
                        }
@@ -285,18 +285,18 @@ void Display::set_mode(const VideoMode &requested_mode, bool exclusive)
                monitor->current_settings.y = 0;
 
                // Disable other outputs for exclusive mode
-               for(list<Monitor>::iterator i=monitors.begin(); i!=monitors.end(); ++i)
-                       if(&*i!=mode->monitor)
+               for(Monitor &m: monitors)
+                       if(&m!=mode->monitor)
                        {
-                               XRROutputInfo *o = XRRGetOutputInfo(priv->display, res, priv->monitors[i->index]);
+                               XRROutputInfo *o = XRRGetOutputInfo(priv->display, res, priv->monitors[m.index]);
                                if(o->crtc)
                                        XRRSetCrtcConfig(priv->display, res, o->crtc, CurrentTime, 0, 0, 0, RR_Rotate_0, 0, 0);
                                XRRFreeOutputInfo(o);
 
-                               i->current_settings.mode = 0;
-                               i->current_settings.rotation = ROTATE_NORMAL;
-                               i->current_settings.x = 0;
-                               i->current_settings.y = 0;
+                               m.current_settings.mode = 0;
+                               m.current_settings.rotation = ROTATE_NORMAL;
+                               m.current_settings.x = 0;
+                               m.current_settings.y = 0;
                        }
        }
        else
@@ -334,7 +334,7 @@ bool Display::process_events()
 
                check_error();
 
-               map<WindowHandle, Window *>::iterator j = priv->windows.find(event.xevent.xany.window);
+               auto j = priv->windows.find(event.xevent.xany.window);
                if(j!=priv->windows.end())
                {
                        /* Filter keyboard autorepeat.  If this packet is a KeyRelease and
index 1d33292c57ca91f4d305d0ad73bce57e65c724bc..d6317d8627bdbffe0e2f31447a8dd1668697b908 100644 (file)
@@ -1,3 +1,4 @@
+#include <algorithm>
 #include <msp/strings/format.h>
 #include "bindings.h"
 #include "controlscheme.h"
@@ -11,12 +12,12 @@ vector<Device *> Bindings::resolve_devices(Device &dev) const
 {
        vector<Device *> resolved_devs;
        resolved_devs.reserve(devices.size());
-       for(vector<DeviceRef>::const_iterator i=devices.begin(); i!=devices.end(); ++i)
+       for(const DeviceRef &d: devices)
        {
-               if(i->type!=UNSPECIFIED)
-                       resolved_devs.push_back(dev.find_subdevice(i->type));
-               else if(!i->name.empty())
-                       resolved_devs.push_back(dev.find_subdevice(i->name));
+               if(d.type!=UNSPECIFIED)
+                       resolved_devs.push_back(dev.find_subdevice(d.type));
+               else if(!d.name.empty())
+                       resolved_devs.push_back(dev.find_subdevice(d.name));
                else
                        resolved_devs.push_back(0);
        }
@@ -26,23 +27,20 @@ vector<Device *> Bindings::resolve_devices(Device &dev) const
 bool Bindings::is_compatible(Device &dev) const
 {
        vector<Device *> resolved_devs = resolve_devices(dev);
-       for(vector<Device *>::const_iterator i=resolved_devs.begin(); i!=resolved_devs.end(); ++i)
-               if(!*i)
-                       return false;
-       return true;
+       return all_of(resolved_devs.begin(), resolved_devs.end(), [](Device *d) -> bool { return d; });
 }
 
 bool Bindings::apply_to(ControlScheme &control_scheme, Device &dev)
 {
        vector<Device *> resolved_devs = resolve_devices(dev);
        bool applied = false;
-       for(vector<Binding>::const_iterator i=bindings.begin(); i!=bindings.end(); ++i)
+       for(const Binding &b: bindings)
        {
-               Control *ctrl = control_scheme.find(i->control);
-               Device *bdev = (i->device<resolved_devs.size() ? resolved_devs[i->device] : 0);
+               Control *ctrl = control_scheme.find(b.control);
+               Device *bdev = (b.device<resolved_devs.size() ? resolved_devs[b.device] : 0);
                if(ctrl && bdev)
                {
-                       ctrl->set_source(*bdev, i->type, i->index);
+                       ctrl->set_source(*bdev, b.type, b.index);
                        applied = true;
                }
        }
index fe1c0ebfa25803de3ce8ff372a81fe03c356a700..4e5e8deba376c109ea693aebd464d3924d9defe2 100644 (file)
@@ -19,14 +19,14 @@ Control &ControlScheme::get(const string &name) const
 
 Control *ControlScheme::find(const string &name) const
 {
-       map<string, Control *>::const_iterator i = controls.find(name);
+       auto i = controls.find(name);
        return (i!=controls.end() ? i->second : 0);
 }
 
 void ControlScheme::reset_edges()
 {
-       for(map<string, Control *>::const_iterator i=controls.begin(); i!=controls.end(); ++i)
-               i->second->reset_edges();
+       for(const auto &kvp: controls)
+               kvp.second->reset_edges();
 }
 
 } // namespace Input
index e45cd6576b3bd02b890f2ab96606fc125fee63dc..79b42aaaf65c87294401b397889ed63f581d1a52 100644 (file)
@@ -27,8 +27,8 @@ void Hub::attach(Device &dev)
 
 Device *Hub::find_subdevice(DeviceType t, unsigned n)
 {
-       for(vector<Device *>::const_iterator i=devices.begin(); i!=devices.end(); ++i)
-               if(Device *dev = (*i)->find_subdevice(t, 0))
+       for(Device *d: devices)
+               if(Device *dev = d->find_subdevice(t, 0))
                {
                        if(!n)
                                return dev;
@@ -41,8 +41,8 @@ Device *Hub::find_subdevice(const string &n)
 {
        if(n==name)
                return this;
-       for(vector<Device *>::const_iterator i=devices.begin(); i!=devices.end(); ++i)
-               if(Device *dev = (*i)->find_subdevice(n))
+       for(Device *d: devices)
+               if(Device *dev = d->find_subdevice(n))
                        return dev;
        return 0;
 }
index 6be00ba591cbb0618733e0ba1533f867c16fe146..f1324a0a82ed338cf9548beb2f227bb4ef2dd4d1 100644 (file)
@@ -24,7 +24,7 @@ unsigned key_from_sys(unsigned code)
                init_done = true;
        }
 
-       map<unsigned, unsigned>::const_iterator i = reverse_map.find(code);
+       auto i = reverse_map.find(code);
        if(i!=reverse_map.end())
                return i->second;
 
index d0029e8fe3c4bbad1249bb4fdcea6b1baca1d7b6..58e4ef17325d2aa8d3b0a22447ccae4b30ac86da 100644 (file)
@@ -46,9 +46,9 @@ unsigned GameController::detect()
        FS::Path dev_input = "/dev/input";
        vector<string> devices = FS::list_filtered(dev_input, "^js[0-9]+");
        sort(devices);
-       for(vector<string>::const_iterator i=devices.begin(); i!=devices.end(); ++i)
+       for(const string &f: devices)
                // TODO check permissions
-               Private::detected_controllers.push_back((dev_input / *i).str());
+               Private::detected_controllers.push_back((dev_input/f).str());
 
        detect_done = true;
        n_detected_controllers = Private::detected_controllers.size();