Fix enum class key in hashed containers

This commit is contained in:
Flössie 2021-11-24 15:32:04 +01:00
parent 23214ae5cd
commit ac19ea4507
4 changed files with 24 additions and 9 deletions

View File

@ -20,6 +20,7 @@
#include <functional> #include <functional>
#include <map> #include <map>
#include <type_traits>
#include <gtkmm.h> #include <gtkmm.h>
@ -74,6 +75,17 @@ private:
MyMutex mutex; MyMutex mutex;
}; };
struct ScopedEnumHash {
template<typename T, typename std::enable_if<std::is_enum<T>::value && !std::is_convertible<T, int>::value, int>::type = 0>
size_t operator ()(T val) const noexcept
{
using type = typename std::underlying_type<T>::type;
return std::hash<type>{}(static_cast<type>(val));
}
};
// TODO: The documentation says gdk_threads_enter and gdk_threads_leave should be replaced // TODO: The documentation says gdk_threads_enter and gdk_threads_leave should be replaced
// by g_main_context_invoke(), g_idle_add() and related functions, but this will require more extensive changes. // by g_main_context_invoke(), g_idle_add() and related functions, but this will require more extensive changes.
// We silence those warnings until then so that we notice the others. // We silence those warnings until then so that we notice the others.

View File

@ -24,6 +24,7 @@
#include "toolpanelcoord.h" #include "toolpanelcoord.h"
using Tool = ToolPanelCoordinator::Tool; using Tool = ToolPanelCoordinator::Tool;
using Favorites = std::unordered_set<Tool, ScopedEnumHash>;
std::string getToolName(Tool tool) std::string getToolName(Tool tool)
{ {
@ -200,7 +201,7 @@ struct ToolLocationPreference::Impl {
void addToolListRowGroup( void addToolListRowGroup(
const std::vector<ToolPanelCoordinator::ToolTree> &tools, const std::vector<ToolPanelCoordinator::ToolTree> &tools,
const Gtk::TreeIter &parentRowIter, const Gtk::TreeIter &parentRowIter,
const std::unordered_set<Tool> &favorites); const Favorites &favorites);
void favoriteToggled(const Glib::ustring &row_path); void favoriteToggled(const Glib::ustring &row_path);
Tool getToolFromName(const std::string &name) const; Tool getToolFromName(const std::string &name) const;
void initFavoritesRows(const std::vector<Tool> &favorites); void initFavoritesRows(const std::vector<Tool> &favorites);
@ -477,7 +478,7 @@ void ToolLocationPreference::Impl::initFavoritesRows(
void ToolLocationPreference::Impl::addToolListRowGroup( void ToolLocationPreference::Impl::addToolListRowGroup(
const std::vector<ToolPanelCoordinator::ToolTree> &tools, const std::vector<ToolPanelCoordinator::ToolTree> &tools,
const Gtk::TreeIter &parentRowIter, const Gtk::TreeIter &parentRowIter,
const std::unordered_set<Tool> &favorites) const Favorites &favorites)
{ {
for (const ToolPanelCoordinator::ToolTree &tool : tools) { for (const ToolPanelCoordinator::ToolTree &tool : tools) {
auto tool_row_iter = toolListModelPtr->append(parentRowIter->children()); auto tool_row_iter = toolListModelPtr->append(parentRowIter->children());
@ -498,7 +499,7 @@ void ToolLocationPreference::Impl::addToolListRowGroup(
void ToolLocationPreference::Impl::initToolListRows(const std::vector<Tool> &favorites) void ToolLocationPreference::Impl::initToolListRows(const std::vector<Tool> &favorites)
{ {
const auto panel_tools = ToolPanelCoordinator::getDefaultToolLayout(); const auto panel_tools = ToolPanelCoordinator::getDefaultToolLayout();
std::unordered_set<Tool> favorites_set; Favorites favorites_set;
for (const auto &tool : favorites) { for (const auto &tool : favorites) {
favorites_set.insert(tool); favorites_set.insert(tool);

View File

@ -236,7 +236,7 @@ const std::vector<ToolTree> RAW_PANEL_TOOLS = {
}, },
}; };
const std::unordered_map<ToolPanelCoordinator::Panel, const std::vector<ToolTree>> PANEL_TOOLS = { const ToolPanelCoordinator::ToolLayout PANEL_TOOLS = {
{ {
ToolPanelCoordinator::Panel::EXPOSURE, ToolPanelCoordinator::Panel::EXPOSURE,
EXPOSURE_PANEL_TOOLS EXPOSURE_PANEL_TOOLS
@ -523,7 +523,7 @@ ToolPanelCoordinator::ToolPanelCoordinator (bool batch) : ipc (nullptr), favorit
prevPage = toolPanelNotebook->get_nth_page(0); prevPage = toolPanelNotebook->get_nth_page(0);
} }
const std::unordered_map<ToolPanelCoordinator::Panel, const std::vector<ToolTree>> ToolPanelCoordinator::getDefaultToolLayout() const ToolPanelCoordinator::ToolLayout& ToolPanelCoordinator::getDefaultToolLayout()
{ {
return PANEL_TOOLS; return PANEL_TOOLS;
} }

View File

@ -298,13 +298,15 @@ public:
std::vector<ToolTree> children; std::vector<ToolTree> children;
}; };
using ToolLayout = std::unordered_map<Panel, const std::vector<ToolTree>, ScopedEnumHash>;
CoarsePanel* coarse; CoarsePanel* coarse;
Gtk::Notebook* toolPanelNotebook; Gtk::Notebook* toolPanelNotebook;
ToolPanelCoordinator(bool batch = false); ToolPanelCoordinator(bool batch = false);
~ToolPanelCoordinator () override; ~ToolPanelCoordinator () override;
static const std::unordered_map<Panel, const std::vector<ToolTree>> getDefaultToolLayout(); static const ToolLayout& getDefaultToolLayout();
static bool isFavoritable(Tool tool); static bool isFavoritable(Tool tool);
bool getChangedState() bool getChangedState()