From 77362a24295d8b0094b97e86e587799c9de9fa29 Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Thu, 25 Jun 2020 20:46:01 +0200 Subject: [PATCH] Consolidate common plugin search & sort methods Also add a namespace, to prevent any symbol conflicts for the structs. --- gtk2_ardour/mixer_ui.cc | 116 ++---------------------- gtk2_ardour/plugin_selector.cc | 46 +--------- gtk2_ardour/plugin_utils.h | 157 +++++++++++++++++++++++++++++++++ 3 files changed, 167 insertions(+), 152 deletions(-) create mode 100644 gtk2_ardour/plugin_utils.h diff --git a/gtk2_ardour/mixer_ui.cc b/gtk2_ardour/mixer_ui.cc index bd073e252e..8d03ec8238 100644 --- a/gtk2_ardour/mixer_ui.cc +++ b/gtk2_ardour/mixer_ui.cc @@ -34,7 +34,6 @@ #include #include -#include #include @@ -82,6 +81,7 @@ #include "actions.h" #include "gui_thread.h" #include "mixer_group_tabs.h" +#include "plugin_utils.h" #include "route_sorter.h" #include "timers.h" #include "ui_config.h" @@ -91,6 +91,7 @@ using namespace ARDOUR; using namespace ARDOUR_UI_UTILS; +using namespace ARDOUR_PLUGIN_UTILS; using namespace PBD; using namespace Gtk; using namespace Glib; @@ -2444,91 +2445,6 @@ Mixer_UI::set_strip_width (Width w, bool save) } } - -struct FavoritePluginSorter { -public: - bool operator() (PluginInfoPtr a, PluginInfoPtr b) const { - std::list::const_iterator aiter = std::find(_user.begin(), _user.end(), (*a).unique_id); - std::list::const_iterator biter = std::find(_user.begin(), _user.end(), (*b).unique_id); - if (aiter != _user.end() && biter != _user.end()) { - return std::distance (_user.begin(), aiter) < std::distance (_user.begin(), biter); - } - if (aiter != _user.end()) { - return true; - } - if (biter != _user.end()) { - return false; - } - return ARDOUR::cmp_nocase((*a).name, (*b).name) == -1; - } - - FavoritePluginSorter (std::list user) : _user (user) { } -private: - std::list _user; -}; - -struct RecentABCSorter { - bool operator() (PluginInfoPtr a, PluginInfoPtr b) const { - return ARDOUR::cmp_nocase((*a).name, (*b).name) == -1; - } -}; - -struct RecentPluginSorter { - bool operator() (PluginInfoPtr a, PluginInfoPtr b) const { - PluginManager& manager (PluginManager::instance()); - int64_t lru_a, lru_b; - uint64_t use_a, use_b; - bool stats_a, stats_b; - - stats_a = manager.stats (a, lru_a, use_a); - stats_b = manager.stats (b, lru_b, use_b); - - if (stats_a && stats_b) { - return lru_a > lru_b; - } - if (stats_a) { - return true; - } - if (stats_b) { - return false; - } - return ARDOUR::cmp_nocase((*a).name, (*b).name) == -1; - } - RecentPluginSorter () - : manager (PluginManager::instance()) - {} -private: - PluginManager& manager; -}; - -struct TopHitPluginSorter { - bool operator() (PluginInfoPtr a, PluginInfoPtr b) const { - PluginManager& manager (PluginManager::instance()); - int64_t lru_a, lru_b; - uint64_t use_a, use_b; - bool stats_a, stats_b; - - stats_a = manager.stats (a, lru_a, use_a); - stats_b = manager.stats (b, lru_b, use_b); - - if (stats_a && stats_b) { - return use_a > use_b; - } - if (stats_a) { - return true; - } - if (stats_b) { - return false; - } - return ARDOUR::cmp_nocase((*a).name, (*b).name) == -1; - } - TopHitPluginSorter () - : manager (PluginManager::instance()) - {} -private: - PluginManager& manager; -}; - int Mixer_UI::set_state (const XMLNode& node, int version) { @@ -3169,26 +3085,6 @@ Mixer_UI::plugin_search_clear_button_clicked () plugin_search_entry.set_text (""); } -static void -setup_search_string (string& searchstr) -{ - transform (searchstr.begin(), searchstr.end(), searchstr.begin(), ::toupper); -} - -static bool -match_search_strings (string const& haystack, string const& needle) -{ - boost::char_separator sep (" "); - typedef boost::tokenizer > tokenizer; - tokenizer t (needle, sep); - for (tokenizer::iterator ti = t.begin(); ti != t.end(); ++ti) { - if (haystack.find (*ti) == string::npos) { - return false; - } - } - return true; -} - void Mixer_UI::refiller (PluginInfoList& result, const PluginInfoList& plugs) { @@ -3269,14 +3165,14 @@ Mixer_UI::refill_favorite_plugins () break; case PLM_TopHits: { - TopHitPluginSorter cmp; + PluginChartsSorter cmp; plugs.sort (cmp); plugs.resize (std::min (plugs.size(), size_t(10))); } break; case PLM_Recent: { - RecentPluginSorter cmp; + PluginRecentSorter cmp; plugs.sort (cmp); plugs.resize (std::min (plugs.size(), size_t(10))); } @@ -3329,11 +3225,11 @@ Mixer_UI::sync_treeview_from_favorite_order () { PBD::Unwinder uw (ignore_plugin_reorder, true); if (plugin_list_mode () == PLM_Favorite) { - FavoritePluginSorter cmp (favorite_ui_order); + PluginUIOrderSorter cmp (favorite_ui_order); plugin_list.sort (cmp); } else { #if 0 - RecentABCSorter cmp; + PluginABCSorter cmp; plugin_list.sort (cmp); #endif } diff --git a/gtk2_ardour/plugin_selector.cc b/gtk2_ardour/plugin_selector.cc index 181d703a43..f7b681ae66 100644 --- a/gtk2_ardour/plugin_selector.cc +++ b/gtk2_ardour/plugin_selector.cc @@ -31,7 +31,6 @@ #include #include -#include #include #include @@ -52,6 +51,7 @@ #include "ardour/utils.h" #include "plugin_selector.h" +#include "plugin_utils.h" #include "gui_thread.h" #include "ui_config.h" @@ -62,6 +62,7 @@ using namespace PBD; using namespace Gtk; using namespace std; using namespace ArdourWidgets; +using namespace ARDOUR_PLUGIN_UTILS; static const uint32_t MAX_CREATOR_LEN = 24; @@ -358,27 +359,6 @@ PluginSelector::added_row_clicked(GdkEventButton* event) btn_remove_clicked(); } - -static void -setup_search_string (string& searchstr) -{ - transform (searchstr.begin(), searchstr.end(), searchstr.begin(), ::toupper); -} - -static bool -match_search_strings (string const& haystack, string const& needle) -{ - boost::char_separator sep (" "); - typedef boost::tokenizer > tokenizer; - tokenizer t (needle, sep); - for (tokenizer::iterator ti = t.begin(); ti != t.end(); ++ti) { - if (haystack.find (*ti) == string::npos) { - return false; - } - } - return true; -} - bool PluginSelector::show_this_plugin (const PluginInfoPtr& info, const std::string& searchstr) { @@ -938,24 +918,6 @@ struct PluginMenuCompareByCreator { } }; -struct PluginMenuCompareByName { - bool operator() (PluginInfoPtr a, PluginInfoPtr b) const { - int cmp; - - cmp = cmp_nocase_utf8 (a->name, b->name); - - if (cmp < 0) { - return true; - } else if (cmp == 0) { - /* same name ... compare type */ - if (a->type < b->type) { - return true; - } - } - return false; - } -}; - /** @return Plugin menu. The caller should not delete it */ Gtk::Menu* PluginSelector::plugin_menu() @@ -1050,7 +1012,7 @@ PluginSelector::create_favs_menu (PluginInfoList& all_plugs) Menu* favs = new Menu(); favs->set_name("ArdourContextMenu"); - PluginMenuCompareByName cmp_by_name; + PluginABCSorter cmp_by_name; all_plugs.sort (cmp_by_name); for (PluginInfoList::const_iterator i = all_plugs.begin(); i != all_plugs.end(); ++i) { @@ -1142,7 +1104,7 @@ PluginSelector::create_by_tags_menu (ARDOUR::PluginInfoList& all_plugs) submenu->set_name("ArdourContextMenu"); } - PluginMenuCompareByName cmp_by_name; + PluginABCSorter cmp_by_name; all_plugs.sort (cmp_by_name); for (PluginInfoList::const_iterator i = all_plugs.begin(); i != all_plugs.end(); ++i) { diff --git a/gtk2_ardour/plugin_utils.h b/gtk2_ardour/plugin_utils.h new file mode 100644 index 0000000000..cc026122a6 --- /dev/null +++ b/gtk2_ardour/plugin_utils.h @@ -0,0 +1,157 @@ +/* + * Copyright (C) 2020 Robin Gareus + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef __gtkardour_plugin_utils_h__ +#define __gtkardour_plugin_utils_h__ + +#include +#include + +#include + +#include "ardour/plugin.h" +#include "ardour/plugin_manager.h" +#include "ardour/utils.h" + +namespace ARDOUR_PLUGIN_UTILS +{ + +inline static void +setup_search_string (std::string& searchstr) +{ + transform (searchstr.begin (), searchstr.end (), searchstr.begin (), ::toupper); +} + +inline static bool +match_search_strings (std::string const& haystack, std::string const& needle) +{ + boost::char_separator sep (" "); + typedef boost::tokenizer > tokenizer; + tokenizer t (needle, sep); + + for (tokenizer::iterator ti = t.begin (); ti != t.end (); ++ti) { + if (haystack.find (*ti) == std::string::npos) { + return false; + } + } + return true; +} + +struct PluginUIOrderSorter { +public: + bool operator() (ARDOUR::PluginInfoPtr a, ARDOUR::PluginInfoPtr b) const + { + std::list::const_iterator aiter = std::find (_user.begin (), _user.end (), (*a).unique_id); + std::list::const_iterator biter = std::find (_user.begin (), _user.end (), (*b).unique_id); + if (aiter != _user.end () && biter != _user.end ()) { + return std::distance (_user.begin (), aiter) < std::distance (_user.begin (), biter); + } + if (aiter != _user.end ()) { + return true; + } + if (biter != _user.end ()) { + return false; + } + return ARDOUR::cmp_nocase ((*a).name, (*b).name) == -1; + } + + PluginUIOrderSorter (std::list user) + : _user (user) + { } + +private: + std::list _user; +}; + +struct PluginABCSorter { + bool operator() (ARDOUR::PluginInfoPtr a, ARDOUR::PluginInfoPtr b) const + { + int cmp = ARDOUR::cmp_nocase_utf8 (a->name, b->name); + if (cmp == 0) { + /* identical name, compare type */ + return a->type < b->type; + } else { + return cmp < 0; + } + } +}; + +struct PluginRecentSorter { + bool operator() (ARDOUR::PluginInfoPtr a, ARDOUR::PluginInfoPtr b) const + { + ARDOUR::PluginManager& manager (ARDOUR::PluginManager::instance ()); + + int64_t lru_a, lru_b; + uint64_t use_a, use_b; + bool stats_a, stats_b; + + stats_a = manager.stats (a, lru_a, use_a); + stats_b = manager.stats (b, lru_b, use_b); + + if (stats_a && stats_b) { + return lru_a > lru_b; + } + if (stats_a) { + return true; + } + if (stats_b) { + return false; + } + return ARDOUR::cmp_nocase ((*a).name, (*b).name) == -1; + } + PluginRecentSorter () + : manager (ARDOUR::PluginManager::instance ()) + { } + +private: + ARDOUR::PluginManager& manager; +}; + +struct PluginChartsSorter { + bool operator() (ARDOUR::PluginInfoPtr a, ARDOUR::PluginInfoPtr b) const + { + ARDOUR::PluginManager& manager (ARDOUR::PluginManager::instance ()); + + int64_t lru_a, lru_b; + uint64_t use_a, use_b; + bool stats_a, stats_b; + + stats_a = manager.stats (a, lru_a, use_a); + stats_b = manager.stats (b, lru_b, use_b); + + if (stats_a && stats_b) { + return use_a > use_b; + } + if (stats_a) { + return true; + } + if (stats_b) { + return false; + } + return ARDOUR::cmp_nocase ((*a).name, (*b).name) == -1; + } + PluginChartsSorter () + : manager (ARDOUR::PluginManager::instance ()) + { } + +private: + ARDOUR::PluginManager& manager; +}; + +} // namespace ARDOUR_PLUGIN_UTILS +#endif