From 8a47e85af1984e8271ddbff1cda88f6c0ecb3815 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Dom=C3=ADnguez?= Date: Sat, 7 Sep 2024 06:30:12 +0200 Subject: [PATCH] Replace loop patterns with std algorithms under libs/ --- libs/ardour/export_graph_builder.cc | 10 +--- libs/ardour/location.cc | 22 ++++---- libs/ardour/panner_manager.cc | 12 ++-- libs/ardour/playlist.cc | 33 ++--------- libs/ardour/plugin.cc | 24 ++++---- libs/ardour/port_manager.cc | 56 ++++++++++--------- libs/ardour/region_factory.cc | 15 +++-- libs/ardour/session.cc | 20 +++---- libs/ardour/session_directory.cc | 11 ++-- libs/ardour/session_playlists.cc | 16 +++--- libs/ardour/session_state.cc | 52 ++++++----------- libs/backends/alsa/alsa_audiobackend.cc | 54 ++++++++---------- libs/backends/coreaudio/coreaudio_backend.cc | 15 ++--- libs/backends/portaudio/portaudio_backend.cc | 14 ++--- libs/backends/portaudio/portaudio_io.cc | 26 +++++---- libs/canvas/item.cc | 14 ++--- libs/pbd/debug.cc | 7 +-- libs/pbd/pbd/abstract_ui.inc.cc | 21 +++---- libs/pbd/pbd/rcu.h | 10 +--- libs/pbd/search_path.cc | 22 +------- libs/ptformat/ptformat.cc | 25 +++------ libs/ptformat/ptformat/ptformat.h | 18 +----- libs/surfaces/mackie/gui.cc | 22 +++++--- libs/surfaces/mackie/strip.cc | 24 ++++---- libs/surfaces/us2400/gui.cc | 13 +++-- libs/surfaces/us2400/strip.cc | 12 ++-- .../us2400/us2400_control_protocol.cc | 12 ++-- 27 files changed, 249 insertions(+), 331 deletions(-) diff --git a/libs/ardour/export_graph_builder.cc b/libs/ardour/export_graph_builder.cc index 82706337b02..298010b268d 100644 --- a/libs/ardour/export_graph_builder.cc +++ b/libs/ardour/export_graph_builder.cc @@ -206,13 +206,9 @@ ExportGraphBuilder::process (samplecnt_t samples, bool last_cycle) bool ExportGraphBuilder::post_process () { - for (std::list::iterator it = intermediates.begin(); it != intermediates.end(); /* ++ in loop */) { - if ((*it)->process()) { - it = intermediates.erase (it); - } else { - ++it; - } - } + static_cast(intermediates.remove_if([] (Intermediate* const& it) { + return it->process(); + })); return intermediates.empty(); } diff --git a/libs/ardour/location.cc b/libs/ardour/location.cc index 95e1311268a..f7fbe4cc0ce 100644 --- a/libs/ardour/location.cc +++ b/libs/ardour/location.cc @@ -1611,27 +1611,29 @@ Locations::marks_either_side (timepos_t const & pos, timepos_t& before, timepos_ positions.sort (); - std::list::iterator i = positions.begin (); - - while (i != positions.end () && *i < pos) { - ++i; - } + auto time_mark_it = std::find_if( + positions.begin (), + positions.end (), + [&] (const auto& p) { + return p >= pos; + } + ); - if (i == positions.end ()) { + if (time_mark_it == positions.end ()) { /* run out of marks */ before = positions.back (); return; } - after = *i; + after = *time_mark_it; - if (i == positions.begin ()) { + if (time_mark_it == positions.begin ()) { /* none before */ return; } - --i; - before = *i; + --time_mark_it; + before = *time_mark_it; } void diff --git a/libs/ardour/panner_manager.cc b/libs/ardour/panner_manager.cc index 17b4c50f850..86cd45e8a1e 100644 --- a/libs/ardour/panner_manager.cc +++ b/libs/ardour/panner_manager.cc @@ -113,15 +113,11 @@ PannerManager::panner_discover (string path) if ((pinfo = get_descriptor (path)) != 0) { - list::iterator i; + auto pi_it = std::find_if(panner_info.begin(), panner_info.end(), [&] (const auto& pi) { + return pi->descriptor.name == pinfo->descriptor.name; + }); - for (i = panner_info.begin(); i != panner_info.end(); ++i) { - if (pinfo->descriptor.name == (*i)->descriptor.name) { - break; - } - } - - if (i == panner_info.end()) { + if (pi_it == panner_info.end()) { panner_info.push_back (pinfo); DEBUG_TRACE (DEBUG::Panning, string_compose(_("Panner discovered: \"%1\" in %2\n"), pinfo->descriptor.name, path)); } else { diff --git a/libs/ardour/playlist.cc b/libs/ardour/playlist.cc index 517a0e9296c..e1df5041418 100644 --- a/libs/ardour/playlist.cc +++ b/libs/ardour/playlist.cc @@ -858,17 +858,9 @@ Playlist::remove_region_internal (std::shared_ptr region, ThawList& thaw } #if 0 - for (set >::iterator x = all_regions.begin(); x != all_regions.end(); ++x) { - if ((*x) == region) { - all_regions.erase (x); - break; - } - } + all_regions.erase (region); #else /* sync_all_regions_with_regions */ - all_regions.clear (); - for (auto const& r: regions) { - all_regions.insert (r); - } + all_regions = std::set (regions.begin (), regions.end ()); #endif return -1; @@ -3461,29 +3453,14 @@ Playlist::share_with (const PBD::ID& id) void Playlist::unshare_with (const PBD::ID& id) { - list::iterator it = _shared_with_ids.begin (); - while (it != _shared_with_ids.end ()) { - if (*it == id) { - _shared_with_ids.erase (it); - break; - } - ++it; - } + list::iterator it = std::find (_shared_with_ids.begin (), _shared_with_ids.end (), id); + if (it != _shared_with_ids.end ()) _shared_with_ids.erase (it); } bool Playlist::shared_with (const PBD::ID& id) const { - bool shared = false; - list::const_iterator it = _shared_with_ids.begin (); - while (it != _shared_with_ids.end () && !shared) { - if (*it == id) { - shared = true; - } - ++it; - } - - return shared; + return std::find(_shared_with_ids.cbegin(), _shared_with_ids.cend(), id) != _shared_with_ids.cend(); } void diff --git a/libs/ardour/plugin.cc b/libs/ardour/plugin.cc index 20c4d9065e1..0f83cd11fa5 100644 --- a/libs/ardour/plugin.cc +++ b/libs/ardour/plugin.cc @@ -376,13 +376,11 @@ Plugin::preset_by_label (const string& label) } // FIXME: O(n) - for (map::const_iterator i = _presets.begin(); i != _presets.end(); ++i) { - if (i->second.label == label) { - return &i->second; - } - } + auto i = std::find_if (_presets.cbegin (), _presets.cend (), [&] (const auto& sp) { + return sp.second.label == label; + }); - return 0; + return i != _presets.cend () ? &i->second : 0; } const Plugin::PresetRecord * @@ -485,17 +483,21 @@ Plugin::resolve_midi () vector Plugin::get_presets () { - vector p; - if (!_have_presets) { _presets.clear (); find_presets (); _have_presets = true; } - for (map::const_iterator i = _presets.begin(); i != _presets.end(); ++i) { - p.push_back (i->second); - } + vector p; + static_cast (std::transform ( + _presets.cbegin (), + _presets.cend (), + p.begin (), + [] (const std::pair& i) { + return i.second; + } + )); std::sort (p.begin(), p.end()); diff --git a/libs/ardour/port_manager.cc b/libs/ardour/port_manager.cc index 41e9078bd19..75a2afc5434 100644 --- a/libs/ardour/port_manager.cc +++ b/libs/ardour/port_manager.cc @@ -1063,14 +1063,16 @@ PortManager::update_input_ports (bool clear) } else { std::shared_ptr aip = _audio_input_ports.reader (); /* find new audio ports */ - for (std::vector::iterator p = audio_ports.begin (); p != audio_ports.end (); ++p) { - if (port_is_mine (*p) || !_backend->get_port_by_name (*p)) { - continue; + static_cast (std::copy_if ( + audio_ports.cbegin (), + audio_ports.cend (), + new_audio.begin (), + [&] (const std::string& p) { + return !port_is_mine (p) + && _backend->get_port_by_name (p) + && aip->find (p) == aip->end (); } - if (aip->find (*p) == aip->end ()) { - new_audio.push_back (*p); - } - } + )); /* find stale audio ports */ for (auto const& p : *aip) { @@ -1081,19 +1083,20 @@ PortManager::update_input_ports (bool clear) std::shared_ptr mip = _midi_input_ports.reader (); /* find new MIDI ports */ - for (std::vector::iterator p = midi_ports.begin (); p != midi_ports.end (); ++p) { - if (port_is_mine (*p) || !_backend->get_port_by_name (*p)) { - continue; - } + static_cast (std::copy_if ( + midi_ports.cbegin (), + midi_ports.cend (), + new_audio.begin (), + [&, this] (const std::string& p) { + return !port_is_mine (p) + && _backend->get_port_by_name (p) #ifdef HAVE_ALSA - if ((*p).find (X_("Midi Through")) != string::npos || (*p).find (X_("Midi-Through")) != string::npos) { - continue; - } + && p.find (X_("Midi Through")) == string::npos + && p.find (X_("Midi-Through")) == string::npos #endif - if (mip->find (*p) == mip->end ()) { - new_midi.push_back (*p); + && mip->find (p) == mip->end (); } - } + )); /* find stale audio ports */ for (auto const& p : *mip) { @@ -1585,15 +1588,16 @@ PortManager::get_configurable_midi_ports (vector& copy, bool for_input) std::vector ports; AudioEngine::instance ()->get_ports (string (), DataType::MIDI, flags, ports); - for (vector::iterator p = ports.begin (); p != ports.end (); ++p) { - if (port_is_mine (*p) && !port_is_virtual_piano (*p)) { - continue; - } - if ((*p).find (X_("Midi Through")) != string::npos || (*p).find (X_("Midi-Through")) != string::npos) { - continue; - } - copy.push_back (*p); - } + static_cast (std::copy_if ( + ports.cbegin (), + ports.cend (), + copy.end (), + [this] (const string& p) { + return !(port_is_mine (p) && !port_is_virtual_piano (p)) + && p.find (X_("Midi Through")) == string::npos + && p.find (X_("Midi-Through")) == string::npos; + } + )); } void diff --git a/libs/ardour/region_factory.cc b/libs/ardour/region_factory.cc index 07495f1be83..657bdc32f5f 100644 --- a/libs/ardour/region_factory.cc +++ b/libs/ardour/region_factory.cc @@ -429,14 +429,17 @@ RegionFactory::rename_in_region_name_maps (std::shared_ptr region) Glib::Threads::Mutex::Lock lm (region_name_maps_mutex); - map::iterator i = region_name_map.begin (); - while (i != region_name_map.end () && i->second != region->id ()) { - ++i; - } + auto old_region_it = std::find_if ( + region_name_map.begin (), + region_name_map.end (), + [&] (auto& region_name) { + return region_name.second == region->id (); + } + ); /* Erase the entry for the old name and put in a new one */ - if (i != region_name_map.end ()) { - region_name_map.erase (i); + if (old_region_it != region_name_map.end ()) { + region_name_map.erase (old_region_it); region_name_map[region->name ()] = region->id (); } } diff --git a/libs/ardour/session.cc b/libs/ardour/session.cc index 31f282b108c..da7e9dbdff4 100644 --- a/libs/ardour/session.cc +++ b/libs/ardour/session.cc @@ -7004,16 +7004,16 @@ Session::ensure_search_path_includes (const string& path, DataType type) break; } - for (vector::iterator i = sp.begin(); i != sp.end(); ++i) { - /* No need to add this new directory if it has the same inode as - an existing one; checking inode rather than name prevents duplicated - directories when we are using symlinks. - - On Windows, I think we could just do if (*i == path) here. - */ - if (PBD::equivalent_paths (*i, path)) { - return; - } + /* No need to add this new directory if it has the same inode as + * an existing one; checking inode rather than name prevents duplicated + * directories when we are using symlinks. + * + * On Windows, I think we could just do if (*i == path) here. + */ + if (std::any_of (sp.cbegin (), sp.cend (), [&] (const std::string& i) { + return PBD::equivalent_paths (i, path); + })) { + return; } sp += path; diff --git a/libs/ardour/session_directory.cc b/libs/ardour/session_directory.cc index 7c5d8db0c1d..3e852d27ed8 100644 --- a/libs/ardour/session_directory.cc +++ b/libs/ardour/session_directory.cc @@ -82,13 +82,14 @@ SessionDirectory::is_valid () const vector sub_dirs = sub_directories (); - for (vector::iterator i = sub_dirs.begin(); i != sub_dirs.end(); ++i) { - if (!Glib::file_test (*i, Glib::FILE_TEST_IS_DIR)) { - PBD::warning << string_compose(_("Session subdirectory does not exist at path %1"), *i) << endmsg; + return std::all_of (sub_dirs.cbegin (), sub_dirs.cend (), [&] (const std::string& i) { + if (!Glib::file_test (i, Glib::FILE_TEST_IS_DIR)) { + PBD::warning << string_compose(_("Session subdirectory does not exist at path %1"), i) << endmsg; return false; + } else { + return true; } - } - return true; + }); } const std::string diff --git a/libs/ardour/session_playlists.cc b/libs/ardour/session_playlists.cc index 32e05ab228b..7831195f233 100644 --- a/libs/ardour/session_playlists.cc +++ b/libs/ardour/session_playlists.cc @@ -657,14 +657,16 @@ SessionPlaylists::playlists_for_track (std::shared_ptr tr) const vector > pl_tr; - for (vector >::iterator i = pl.begin(); i != pl.end(); ++i) { - if ( ((*i)->get_orig_track_id() == tr->id()) || - (tr->playlist()->id() == (*i)->id()) || - ((*i)->shared_with (tr->id())) ) - { - pl_tr.push_back (*i); + static_cast (std::copy_if ( + pl.cbegin (), + pl.cend (), + pl_tr.begin (), + [&] (const std::shared_ptr& i) { + return i->get_orig_track_id() == tr->id() + || tr->playlist()->id() == i->id() + || i->shared_with (tr->id()); } - } + )); return pl_tr; } diff --git a/libs/ardour/session_state.cc b/libs/ardour/session_state.cc index 37e8b1ad0a5..55b9450e1d8 100644 --- a/libs/ardour/session_state.cc +++ b/libs/ardour/session_state.cc @@ -2995,7 +2995,6 @@ Session::refresh_disk_space () DWORD nSectorsPerCluster, nBytesPerSector, nFreeClusters, nTotalClusters; char disk_drive[4]; - bool volume_found; _total_free_4k_blocks = 0; @@ -3004,19 +3003,15 @@ Session::refresh_disk_space () disk_drive[3] = 0; strupr(disk_drive); - volume_found = false; if (0 != (GetDiskFreeSpace(disk_drive, &nSectorsPerCluster, &nBytesPerSector, &nFreeClusters, &nTotalClusters))) { int64_t nBytesPerCluster = nBytesPerSector * nSectorsPerCluster; int64_t nFreeBytes = nBytesPerCluster * (int64_t)nFreeClusters; i->blocks = (uint32_t)(nFreeBytes / 4096); - for (j = scanned_volumes.begin(); j != scanned_volumes.end(); j++) { - if (0 == j->compare(disk_drive)) { - volume_found = true; - break; - } - } + bool volume_found = std::any_of(scanned_volumes.cbegin(), scanned_volumes.cend(), [&] (const string& j) { + return j.compare(disk_drive) == 0; + }); if (!volume_found) { scanned_volumes.push_back(disk_drive); @@ -3455,16 +3450,10 @@ Session::find_all_sources_across_snapshots (set& result, bool exclude_th this_snapshot_path = Glib::build_filename (_path, legalize_for_path (_current_snapshot_name)); this_snapshot_path += statefile_suffix; - for (vector::iterator i = state_files.begin(); i != state_files.end(); ++i) { - - if (exclude_this_snapshot && *i == this_snapshot_path) { - continue; - - } - - if (find_all_sources (*i, result) < 0) { - return -1; - } + if (std::any_of (state_files.cbegin (), state_files.cend (), [&] (const string& i) { + return !(exclude_this_snapshot && i == this_snapshot_path) && find_all_sources (i, result) < 0; + })) { + return -1; } return 0; @@ -5238,22 +5227,17 @@ Session::save_as (SaveAs& saveas) /* normal non-media file. Don't copy state, history, etc. */ - bool do_copy = true; - - for (vector::iterator v = do_not_copy_extensions.begin(); v != do_not_copy_extensions.end(); ++v) { - if ((from.length() > (*v).length()) && (from.find (*v) == from.length() - (*v).length())) { - /* end of filename matches extension, do not copy file */ - do_copy = false; - break; - } - } - - if (!saveas.copy_media && from.find (peakfile_suffix) != string::npos) { - /* don't copy peakfiles if - * we're not copying media - */ - do_copy = false; - } + /* don't copy peakfiles if we're not copying media */ + bool do_copy = (saveas.copy_media || from.find (peakfile_suffix) == string::npos) + && std::none_of ( + do_not_copy_extensions.cbegin (), + do_not_copy_extensions.cend (), + [&] (const string& v) { + /* end of filename matches extension, do not copy file */ + return from.length() > v.length() + && from.find (v) == (from.length() - v.length()); + } + ); if (do_copy) { string to = Glib::build_filename (to_dir, from.substr (prefix_len)); diff --git a/libs/backends/alsa/alsa_audiobackend.cc b/libs/backends/alsa/alsa_audiobackend.cc index 27ee52c6de1..a4680d67e5d 100644 --- a/libs/backends/alsa/alsa_audiobackend.cc +++ b/libs/backends/alsa/alsa_audiobackend.cc @@ -305,20 +305,16 @@ AlsaAudioBackend::set_input_device_name (const std::string& d) _input_audio_device_info.valid = false; return 0; } - std::string alsa_device; - std::map devices; + std::map devices; get_alsa_audio_device_names (devices, HalfDuplexIn); - for (std::map::const_iterator i = devices.begin (); i != devices.end (); ++i) { - if (i->first == d) { - alsa_device = i->second; - break; - } - } - if (alsa_device == "") { + auto alsa_device_it = devices.find(d); + + if (alsa_device_it == devices.cend ()) { _input_audio_device_info.valid = false; return 1; } + const std::string& alsa_device = alsa_device_it->second; AlsaDeviceReservation adr (alsa_device.c_str ()); /* device will be busy once used, hence cache the parameters */ /* return */ get_alsa_device_parameters (alsa_device.c_str (), false, &_input_audio_device_info); @@ -338,20 +334,16 @@ AlsaAudioBackend::set_output_device_name (const std::string& d) _output_audio_device_info.valid = false; return 0; } - std::string alsa_device; - std::map devices; + std::map devices; get_alsa_audio_device_names (devices, HalfDuplexOut); - for (std::map::const_iterator i = devices.begin (); i != devices.end (); ++i) { - if (i->first == d) { - alsa_device = i->second; - break; - } - } - if (alsa_device == "") { + auto alsa_device_it = devices.find(d); + + if (alsa_device_it == devices.cend ()) { _output_audio_device_info.valid = false; return 1; } + const std::string& alsa_device = alsa_device_it->second; AlsaDeviceReservation adr (alsa_device.c_str ()); /* return */ get_alsa_device_parameters (alsa_device.c_str (), true, &_output_audio_device_info); return 0; @@ -605,10 +597,9 @@ AlsaAudioBackend::systemic_midi_output_latency (std::string const device) const struct AlsaAudioBackend::AlsaMidiDeviceInfo* AlsaAudioBackend::midi_device_info (std::string const name) const { - for (std::map::const_iterator i = _midi_devices.begin (); i != _midi_devices.end (); ++i) { - if (i->first == name) { - return (i->second); - } + auto md = _midi_devices.find (name); + if (md != _midi_devices.cend ()) { + return md->second; } assert (_midi_driver_option != get_standard_device_name (DeviceNone)); @@ -620,11 +611,9 @@ AlsaAudioBackend::midi_device_info (std::string const name) const get_alsa_sequencer_names (devices); } - for (std::map::const_iterator i = devices.begin (); i != devices.end (); ++i) { - if (i->first == name) { - _midi_devices[name] = new AlsaMidiDeviceInfo (); - return _midi_devices[name]; - } + if (devices.find (name) != devices.cend ()) { + _midi_devices[name] = new AlsaMidiDeviceInfo (); + return _midi_devices[name]; } return 0; } @@ -652,9 +641,14 @@ AlsaAudioBackend::enumerate_midi_devices () const get_alsa_sequencer_names (devices); } - for (std::map::const_iterator i = devices.begin (); i != devices.end (); ++i) { - _midi_device_status.push_back (DeviceStatus (i->first, true)); - } + static_cast (std::transform ( + devices.cbegin (), + devices.cend (), + _midi_device_status.begin (), + [] (const std::pair& i) { + return DeviceStatus (i.first, true); + } + )); return _midi_device_status; } diff --git a/libs/backends/coreaudio/coreaudio_backend.cc b/libs/backends/coreaudio/coreaudio_backend.cc index 05c59220d09..8da527ffe1d 100644 --- a/libs/backends/coreaudio/coreaudio_backend.cc +++ b/libs/backends/coreaudio/coreaudio_backend.cc @@ -743,7 +743,6 @@ CoreAudioBackend::samples_since_cycle_start () uint32_t CoreAudioBackend::name_to_id(std::string device_name, DeviceFilter filter) const { - uint32_t device_id = UINT32_MAX; std::map devices; switch (filter) { case Input: @@ -761,13 +760,15 @@ CoreAudioBackend::name_to_id(std::string device_name, DeviceFilter filter) const break; } - for (std::map::const_iterator i = devices.begin (); i != devices.end(); ++i) { - if (i->second == device_name) { - device_id = i->first; - break; + auto device_it = std::find_if ( + devices.cbegin (), + devices.cend (), + [&] (const auto& dev) { + return dev.second == device_name; } - } - return device_id; + ); + + return device_it != devices.cend () ? device_it->first : UINT32_MAX; } void * diff --git a/libs/backends/portaudio/portaudio_backend.cc b/libs/backends/portaudio/portaudio_backend.cc index b1feb1271d4..dd1c1aac102 100644 --- a/libs/backends/portaudio/portaudio_backend.cc +++ b/libs/backends/portaudio/portaudio_backend.cc @@ -1022,18 +1022,18 @@ PortAudioBackend::samples_since_cycle_start () int PortAudioBackend::name_to_id(std::string device_name) const { - uint32_t device_id = UINT32_MAX; std::map devices; _pcmio->input_device_list(devices); _pcmio->output_device_list(devices); - for (std::map::const_iterator i = devices.begin (); i != devices.end(); ++i) { - if (i->second == device_name) { - device_id = i->first; - break; + auto device_it = std::find_if ( + devices.cbegin (), + devices.cend (), + [&] (const auto& i) { + return i.second == device_name; } - } - return device_id; + ); + return device_it != devices.cend () ? device_it->first : UINT32_MAX; } bool diff --git a/libs/backends/portaudio/portaudio_io.cc b/libs/backends/portaudio/portaudio_io.cc index d2d6ac4c587..870dba7e74b 100644 --- a/libs/backends/portaudio/portaudio_io.cc +++ b/libs/backends/portaudio/portaudio_io.cc @@ -320,21 +320,27 @@ PortAudioIO::available_buffer_sizes(int device_id, std::vector& buffer void PortAudioIO::input_device_list(std::map &devices) const { - for (std::map::const_iterator i = _input_devices.begin (); - i != _input_devices.end (); - ++i) { - devices.insert (std::pair(i->first, Glib::locale_to_utf8(i->second->name))); - } + static_cast (std::transform ( + _input_devices.cbegin (), + _input_devices.cend (), + devices.end (), + [] (const std::pair& i) { + return std::pair(i.first, Glib::locale_to_utf8(i.second->name); + } + )); } void PortAudioIO::output_device_list(std::map &devices) const { - for (std::map::const_iterator i = _output_devices.begin (); - i != _output_devices.end (); - ++i) { - devices.insert (std::pair(i->first, Glib::locale_to_utf8(i->second->name))); - } + static_cast (std::transform ( + _output_devices.cbegin (), + _output_devices.cend (), + devices.end (), + [] (const std::pair& i) { + return std::pair(i.first, Glib::locale_to_utf8(i.second->name); + } + )); } bool& diff --git a/libs/canvas/item.cc b/libs/canvas/item.cc index c301305b841..f24cf0b63ad 100644 --- a/libs/canvas/item.cc +++ b/libs/canvas/item.cc @@ -675,18 +675,14 @@ Item::set_size_request_to_display_given_text (const std::vector& st int width_max = 0; int height_max = 0; + auto string_match = std::find_if(strings.cbegin(), strings.cend(), [&] (const string& s) { + return s.find_first_of ("gy") != string::npos; + }); + vector copy; const vector* to_use; - vector::const_iterator i; - - for (i = strings.begin(); i != strings.end(); ++i) { - if ((*i).find_first_of ("gy") != string::npos) { - /* contains a descender */ - break; - } - } - if (i == strings.end()) { + if (string_match == strings.end()) { /* make a copy of the strings then add one that has a descender */ copy = strings; copy.push_back ("g"); diff --git a/libs/pbd/debug.cc b/libs/pbd/debug.cc index 2fcbc1f5106..ddd0630b2bf 100644 --- a/libs/pbd/debug.cc +++ b/libs/pbd/debug.cc @@ -165,10 +165,9 @@ PBD::list_debug_options () cout << '\t' << X_("all") << endl; vector options; - - for (map::iterator i = _debug_bit_map().begin(); i != _debug_bit_map().end(); ++i) { - options.push_back (i->first); - } + static_cast(std::transform(_debug_bit_map().cbegin(), _debug_bit_map().cend(), options.begin(), [] (const std::pair& i) { + return i.first; + })); sort (options.begin(), options.end()); diff --git a/libs/pbd/pbd/abstract_ui.inc.cc b/libs/pbd/pbd/abstract_ui.inc.cc index f3f735c973d..35c6d6bdc46 100644 --- a/libs/pbd/pbd/abstract_ui.inc.cc +++ b/libs/pbd/pbd/abstract_ui.inc.cc @@ -228,19 +228,16 @@ AbstractUI::handle_ui_requests () /* clean up any dead invalidation records (object was deleted) */ trash.sort(); trash.unique(); - for (std::list::iterator r = trash.begin(); r != trash.end();) { - if (!(*r)->in_use ()) { - assert (!(*r)->valid ()); - DEBUG_TRACE (PBD::DEBUG::AbstractUI, string_compose ("%1 drop invalidation trash %2\n", event_loop_name(), *r)); - std::list::iterator tmp = r; - ++tmp; - delete *r; - trash.erase (r); - r = tmp; - } else { - ++r; + static_cast(trash.remove_if([this] (InvalidationRecord* const& r) { + bool cond = !r->in_use (); + if (cond) { + assert (!r->valid ()); + DEBUG_TRACE (PBD::DEBUG::AbstractUI, string_compose ("%1 drop invalidation trash %2\n", event_loop_name(), r)); + delete r; } - } + + return cond; + })); #ifndef NDEBUG if (trash.size() > 0) { DEBUG_TRACE (PBD::DEBUG::AbstractUI, string_compose ("%1 items in trash: %2\n", event_loop_name(), trash.size())); diff --git a/libs/pbd/pbd/rcu.h b/libs/pbd/pbd/rcu.h index 8f86ba63575..003c15224c6 100644 --- a/libs/pbd/pbd/rcu.h +++ b/libs/pbd/pbd/rcu.h @@ -158,15 +158,7 @@ class /*LIBPBD_API*/ SerializedRCUManager : public RCUManager // clean out any dead wood - typename std::list >::iterator i; - - for (i = _dead_wood.begin (); i != _dead_wood.end ();) { - if ((*i).unique ()) { - i = _dead_wood.erase (i); - } else { - ++i; - } - } + static_cast (_dead_wood.remove_if ([] (const std::shared_ptr& d) { return d.unique (); })); /* store the current so that we can do compare and exchange * when someone calls update(). Notice that we hold diff --git a/libs/pbd/search_path.cc b/libs/pbd/search_path.cc index 178e3ff81f9..6800f179a27 100644 --- a/libs/pbd/search_path.cc +++ b/libs/pbd/search_path.cc @@ -54,19 +54,8 @@ Searchpath::Searchpath (const vector& paths) } void -Searchpath::remove_directory (const std::string& directory_path) -{ - if (directory_path.empty()) { - return; - } - - for (vector::iterator i = begin(); i != end();) { - if (*i == directory_path) { - i = erase (i); - } else { - ++i; - } - } +Searchpath::remove_directory (const std::string& directory_path) { + static_cast(std::remove(begin(), end(), directory_path)); } void @@ -172,12 +161,7 @@ Searchpath::add_subdirectory_to_paths (const string& subdir) bool Searchpath::contains (const string& path) const { - std::vector::const_iterator i = find(begin(), end(), path); - - if (i == end()) { - return false; - } - return true; + return find(begin(), end(), path) != end(); } /* This is not part of the Searchpath object, but is closely related to the diff --git a/libs/ptformat/ptformat.cc b/libs/ptformat/ptformat.cc index 17aaa387b84..0f15106092a 100644 --- a/libs/ptformat/ptformat.cc +++ b/libs/ptformat/ptformat.cc @@ -1058,14 +1058,10 @@ PTFFormat::parserest(void) { } } } - for (std::vector::iterator tr = _tracks.begin(); - tr != _tracks.end(); /* noop */) { - if ((*tr).reg.index == 65535) { - tr = _tracks.erase(tr); - } else { - tr++; - } - } + + static_cast(std::remove_if(_tracks.begin(), _tracks.end(), [] (track_t& tr) { + return tr.reg.index == 65535; + })); if (_tracks.begin() == _tracks.end()) return found; @@ -1319,13 +1315,10 @@ PTFFormat::parsemidi(void) { } } } - for (std::vector::iterator tr = _miditracks.begin(); - tr != _miditracks.end(); /* noop */) { - if ((*tr).reg.index == 65535) { - tr = _miditracks.erase(tr); - } else { - tr++; - } - } + + static_cast(std::remove_if(_miditracks.begin(), _miditracks.end(), [] (track_t& tr) { + return tr.reg.index == 65535; + })); + return true; } diff --git a/libs/ptformat/ptformat/ptformat.h b/libs/ptformat/ptformat/ptformat.h index 3426d7e05be..443ff0fd4b3 100644 --- a/libs/ptformat/ptformat/ptformat.h +++ b/libs/ptformat/ptformat/ptformat.h @@ -183,27 +183,13 @@ class LIBPTFORMAT_API PTFFormat { } static bool regionexistsin(std::vector const& reg, uint16_t index) { - std::vector::const_iterator begin = reg.begin(); - std::vector::const_iterator finish = reg.end(); - region_t r (index); - - if (std::find(begin, finish, r) != finish) { - return true; - } - return false; + return std::find(reg.begin(), reg.end(), r) != reg.end(); } static bool wavexistsin (std::vector const& wv, uint16_t index) { - std::vector::const_iterator begin = wv.begin(); - std::vector::const_iterator finish = wv.end(); - wav_t w (index); - - if (std::find(begin, finish, w) != finish) { - return true; - } - return false; + return std::find(wv.begin(), wv.end(), w) != wv.end(); } uint8_t version () const { return _version; } diff --git a/libs/surfaces/mackie/gui.cc b/libs/surfaces/mackie/gui.cc index ca8dd60c1a3..a3ca42d9f42 100644 --- a/libs/surfaces/mackie/gui.cc +++ b/libs/surfaces/mackie/gui.cc @@ -134,10 +134,13 @@ MackieControlProtocolGUI::MackieControlProtocolGUI (MackieControlProtocol& p) row++; vector surfaces; + static_cast(std::transform( + DeviceInfo::device_info.cbegin(), + DeviceInfo::device_info.cend(), + surfaces.begin(), + [] (const std::pair& i) { return i.first; } + )); - for (std::map::iterator i = DeviceInfo::device_info.begin(); i != DeviceInfo::device_info.end(); ++i) { - surfaces.push_back (i->first); - } Gtkmm2ext::set_popdown_strings (_surface_combo, surfaces); _surface_combo.signal_changed().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::surface_combo_changed)); @@ -222,11 +225,16 @@ MackieControlProtocolGUI::MackieControlProtocolGUI (MackieControlProtocol& p) row++; vector profiles; + static_cast(std::transform( + DeviceProfile::device_profiles.cbegin(), + DeviceProfile::device_profiles.cend(), + profiles.begin(), + [] (const std::pair& i) { + cerr << "add discovered profile " << i.first << endl; + return i.first; + } + )); - for (std::map::iterator i = DeviceProfile::device_profiles.begin(); i != DeviceProfile::device_profiles.end(); ++i) { - cerr << "add discovered profile " << i->first << endl; - profiles.push_back (i->first); - } Gtkmm2ext::set_popdown_strings (_profile_combo, profiles); cerr << "set active profile from " << p.device_profile().name() << endl; _profile_combo.set_active_text (p.device_profile().name()); diff --git a/libs/surfaces/mackie/strip.cc b/libs/surfaces/mackie/strip.cc index a019bf86070..e6d8d245579 100644 --- a/libs/surfaces/mackie/strip.cc +++ b/libs/surfaces/mackie/strip.cc @@ -1196,8 +1196,6 @@ Strip::return_to_vpot_mode_display () void Strip::next_pot_mode () { - vector::iterator i; - if (_surface->mcp().flip_mode() != MackieControlProtocol::Normal) { /* do not change vpot mode while in flipped mode */ DEBUG_TRACE (DEBUG::MackieControl, "not stepping pot mode - in flip mode\n"); @@ -1222,25 +1220,23 @@ Strip::next_pot_mode () return; } - for (i = possible_pot_parameters.begin(); i != possible_pot_parameters.end(); ++i) { - if ((*i) == ac->parameter().type()) { - break; - } - } + auto pot_param = std::find_if( + possible_pot_parameters.begin(), + possible_pot_parameters.end(), + [&] (auto& i) { return i == ac->parameter().type(); } + ); /* move to the next mode in the list, or back to the start (which will also happen if the current mode is not in the current pot mode list) */ - if (i != possible_pot_parameters.end()) { - ++i; - } - - if (i == possible_pot_parameters.end()) { - i = possible_pot_parameters.begin(); + if (pot_param != possible_pot_parameters.end()) { + ++pot_param; + } else { + pot_param = possible_pot_parameters.begin(); } - set_vpot_parameter (*i); + set_vpot_parameter (*pot_param); } void diff --git a/libs/surfaces/us2400/gui.cc b/libs/surfaces/us2400/gui.cc index 54847d74e37..625881d9b72 100644 --- a/libs/surfaces/us2400/gui.cc +++ b/libs/surfaces/us2400/gui.cc @@ -126,11 +126,16 @@ US2400ProtocolGUI::US2400ProtocolGUI (US2400Protocol& p) /* back to the boilerplate */ vector profiles; + static_cast(std::transform( + DeviceProfile::device_profiles.cbegin(), + DeviceProfile::device_profiles.cend(), + profiles.begin(), + [] (const std::pair& i) { + cerr << "add discovered profile " << i.first << endl; + return i.first; + } + )); - for (std::map::iterator i = DeviceProfile::device_profiles.begin(); i != DeviceProfile::device_profiles.end(); ++i) { - cerr << "add discovered profile " << i->first << endl; - profiles.push_back (i->first); - } Gtkmm2ext::set_popdown_strings (_profile_combo, profiles); cerr << "set active profile from " << p.device_profile().name() << endl; _profile_combo.set_active_text (p.device_profile().name()); diff --git a/libs/surfaces/us2400/strip.cc b/libs/surfaces/us2400/strip.cc index 7eda0c8c82f..b29a851d578 100644 --- a/libs/surfaces/us2400/strip.cc +++ b/libs/surfaces/us2400/strip.cc @@ -698,8 +698,6 @@ Strip::vpot_mode_string () void Strip::next_pot_mode () { - vector::iterator i; - std::shared_ptr ac = _vpot->control(); if (!ac) { @@ -715,11 +713,11 @@ Strip::next_pot_mode () return; } - for (i = possible_pot_parameters.begin(); i != possible_pot_parameters.end(); ++i) { - if ((*i) == ac->parameter().type()) { - break; - } - } + vector::iterator i = std::find_if( + possible_pot_parameters.begin(), + possible_pot_parameters.end(), + [&] (auto& i) { return i == ac->parameter().type(); } + ); /* move to the next mode in the list, or back to the start (which will also happen if the current mode is not in the current pot mode list) diff --git a/libs/surfaces/us2400/us2400_control_protocol.cc b/libs/surfaces/us2400/us2400_control_protocol.cc index d4c1ea56317..9000fb616b4 100644 --- a/libs/surfaces/us2400/us2400_control_protocol.cc +++ b/libs/surfaces/us2400/us2400_control_protocol.cc @@ -649,14 +649,10 @@ US2400Protocol::connect_session_signals() void US2400Protocol::set_profile (const string& profile_name) { - map::iterator d = DeviceProfile::device_profiles.find (profile_name); - - if (d == DeviceProfile::device_profiles.end()) { - _device_profile = DeviceProfile (profile_name); - return; - } - - _device_profile = d->second; + auto device_profile = DeviceProfile::device_profiles.find (profile_name); + _device_profile = device_profile == DeviceProfile::device_profiles.end() + ? DeviceProfile (profile_name) + : device_profile->second; } int