From: Mikko Rasa Date: Sun, 27 Nov 2022 09:21:01 +0000 (+0200) Subject: Streamline iterators and loop constructs X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=24da3e62cf22663ef16b61b9510fbac49308f756;p=libs%2Fgui.git Streamline iterators and loop constructs Use range-based for loops where appropriate and the auto type instead of spelling out the entire iterator type. --- diff --git a/source/graphics/cocoa/display.cpp b/source/graphics/cocoa/display.cpp index b9ce492..31aa9a2 100644 --- a/source/graphics/cocoa/display.cpp +++ b/source/graphics/cocoa/display.cpp @@ -35,7 +35,7 @@ bool Display::process_events() if(!get_event(priv->display, &event.cevent)) return false; - map::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); diff --git a/source/graphics/display.cpp b/source/graphics/display.cpp index 7f0b25a..f40e02e 100644 --- a/source/graphics/display.cpp +++ b/source/graphics/display.cpp @@ -32,9 +32,9 @@ const VideoMode &Display::get_desktop_mode() const void Display::restore_mode() { - for(list::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::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->raterate>mode.rate+rate_tolerance)) + if(mode.rate && (m.ratemode.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::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::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::iterator i=loaders.begin(); i!=loaders.end(); ++i) - delete *i; + for(auto l: loaders) + delete l; } } // namespace Graphics diff --git a/source/graphics/windows/display.cpp b/source/graphics/windows/display.cpp index 61fe983..b5f97ad 100644 --- a/source/graphics/windows/display.cpp +++ b/source/graphics/windows/display.cpp @@ -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::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() diff --git a/source/graphics/x11/display.cpp b/source/graphics/x11/display.cpp index 972cc82..949509f 100644 --- a/source/graphics/x11/display.cpp +++ b/source/graphics/x11/display.cpp @@ -165,7 +165,7 @@ Display::Display(const string &disp_name): for(int j=0; jnmode; ++j) { - map::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::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::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::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::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 diff --git a/source/input/bindings.cpp b/source/input/bindings.cpp index 1d33292..d6317d8 100644 --- a/source/input/bindings.cpp +++ b/source/input/bindings.cpp @@ -1,3 +1,4 @@ +#include #include #include "bindings.h" #include "controlscheme.h" @@ -11,12 +12,12 @@ vector Bindings::resolve_devices(Device &dev) const { vector resolved_devs; resolved_devs.reserve(devices.size()); - for(vector::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 Bindings::resolve_devices(Device &dev) const bool Bindings::is_compatible(Device &dev) const { vector resolved_devs = resolve_devices(dev); - for(vector::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 resolved_devs = resolve_devices(dev); bool applied = false; - for(vector::const_iterator i=bindings.begin(); i!=bindings.end(); ++i) + for(const Binding &b: bindings) { - Control *ctrl = control_scheme.find(i->control); - Device *bdev = (i->devicedevice] : 0); + Control *ctrl = control_scheme.find(b.control); + Device *bdev = (b.deviceset_source(*bdev, i->type, i->index); + ctrl->set_source(*bdev, b.type, b.index); applied = true; } } diff --git a/source/input/controlscheme.cpp b/source/input/controlscheme.cpp index fe1c0eb..4e5e8de 100644 --- a/source/input/controlscheme.cpp +++ b/source/input/controlscheme.cpp @@ -19,14 +19,14 @@ Control &ControlScheme::get(const string &name) const Control *ControlScheme::find(const string &name) const { - map::const_iterator i = controls.find(name); + auto i = controls.find(name); return (i!=controls.end() ? i->second : 0); } void ControlScheme::reset_edges() { - for(map::const_iterator i=controls.begin(); i!=controls.end(); ++i) - i->second->reset_edges(); + for(const auto &kvp: controls) + kvp.second->reset_edges(); } } // namespace Input diff --git a/source/input/hub.cpp b/source/input/hub.cpp index e45cd65..79b42aa 100644 --- a/source/input/hub.cpp +++ b/source/input/hub.cpp @@ -27,8 +27,8 @@ void Hub::attach(Device &dev) Device *Hub::find_subdevice(DeviceType t, unsigned n) { - for(vector::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::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; } diff --git a/source/input/keys.cpp b/source/input/keys.cpp index 6be00ba..f1324a0 100644 --- a/source/input/keys.cpp +++ b/source/input/keys.cpp @@ -24,7 +24,7 @@ unsigned key_from_sys(unsigned code) init_done = true; } - map::const_iterator i = reverse_map.find(code); + auto i = reverse_map.find(code); if(i!=reverse_map.end()) return i->second; diff --git a/source/input/linux/gamecontroller.cpp b/source/input/linux/gamecontroller.cpp index d0029e8..58e4ef1 100644 --- a/source/input/linux/gamecontroller.cpp +++ b/source/input/linux/gamecontroller.cpp @@ -46,9 +46,9 @@ unsigned GameController::detect() FS::Path dev_input = "/dev/input"; vector devices = FS::list_filtered(dev_input, "^js[0-9]+"); sort(devices); - for(vector::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();