From fdc7329ba5036ef5cde7bb5e61ed52a69850f5d8 Mon Sep 17 00:00:00 2001
From: Rafael Siejakowski <rs@rs-math.net>
Date: Sun, 29 May 2022 21:00:45 -0300
Subject: [PATCH] Remove almost all uses of std::unary_function

The templates std::unary_function and std::binary_function are
deprecated in C++17 and generate compile warnings on gcc 12.1.0.
This commit removes almost all uses of those templates and gets
rid of an inefficient map storing precomputed bit masks.
---
 src/device-manager.cpp                 | 28 +++++++++-----------------
 src/ui/dialog/undo-history.h           |  3 ++-
 src/ui/tool/multi-path-manipulator.cpp |  1 -
 src/util/units.h                       |  6 ++++--
 4 files changed, 15 insertions(+), 23 deletions(-)

diff --git a/src/device-manager.cpp b/src/device-manager.cpp
index e9841a34bb1..12c65cc6637 100644
--- a/src/device-manager.cpp
+++ b/src/device-manager.cpp
@@ -63,19 +63,7 @@ static bool isValidDevice(Glib::RefPtr<Gdk::Device> device)
 
 namespace Inkscape {
 
-using std::pair;
-
-static pair<gint, gint> vals[] = {
-    pair<gint, gint>(0, 1), pair<gint, gint>(1, 1 << 1), pair<gint, gint>(2, 1 << 2), pair<gint, gint>(3, 1 << 3),
-    pair<gint, gint>(4, 1 << 4), pair<gint, gint>(5, 1 << 5), pair<gint, gint>(6, 1 << 6), pair<gint, gint>(7, 1 << 7),
-    pair<gint, gint>(8, 1 << 8), pair<gint, gint>(9, 1 << 9), pair<gint, gint>(10, 1 << 10), pair<gint, gint>(11, 1 << 11),
-    pair<gint, gint>(12, 1 << 12), pair<gint, gint>(13, 1 << 13), pair<gint, gint>(14, 1 << 14), pair<gint, gint>(15, 1 << 15),
-    pair<gint, gint>(16, 1 << 16), pair<gint, gint>(17, 1 << 17), pair<gint, gint>(18, 1 << 18), pair<gint, gint>(19, 1 << 19),
-    pair<gint, gint>(20, 1 << 20), pair<gint, gint>(21, 1 << 21), pair<gint, gint>(22, 1 << 22), pair<gint, gint>(23, 1 << 23)
-};
-static std::map<gint, gint> bitVals(vals, &vals[G_N_ELEMENTS(vals)]);
-
-
+static int const NUM_AXES = 24;
 static const int RUNAWAY_MAX = 1000;
 
 static Glib::ustring getBaseDeviceName(Gdk::InputSource source)
@@ -201,7 +189,8 @@ private:
     guint liveButtons;
 };
 
-class IdMatcher : public std::unary_function<Glib::RefPtr<InputDeviceImpl>&, bool> {
+class IdMatcher
+{
 public:
     IdMatcher(Glib::ustring const& target):target(target) {}
     bool operator ()(Glib::RefPtr<InputDeviceImpl>& dev) {return dev && (target == dev->getId());}
@@ -210,7 +199,8 @@ private:
     Glib::ustring const& target;
 };
 
-class LinkMatcher : public std::unary_function<Glib::RefPtr<InputDeviceImpl>&, bool> {
+class LinkMatcher
+{
 public:
     LinkMatcher(Glib::ustring const& target):target(target) {}
     bool operator ()(Glib::RefPtr<InputDeviceImpl>& dev) {return dev && (target == dev->getLink());}
@@ -511,10 +501,10 @@ sigc::signal<void, Glib::RefPtr<InputDevice const> > DeviceManagerImpl::signalLi
 
 void DeviceManagerImpl::addAxis(Glib::ustring const & id, gint axis)
 {
-    if ( axis >= 0 && axis < static_cast<gint>(bitVals.size()) ) {
+    if ( axis >= 0 && axis < NUM_AXES ) {
         std::list<Glib::RefPtr<InputDeviceImpl> >::iterator it = std::find_if(devices.begin(), devices.end(), IdMatcher(id));
         if ( it != devices.end() ) {
-            gint mask = bitVals[axis];
+            gint mask = 1u << axis;
             if ( (mask & (*it)->getLiveAxes()) == 0 ) {
                 (*it)->setLiveAxes((*it)->getLiveAxes() | mask);
 
@@ -528,10 +518,10 @@ void DeviceManagerImpl::addAxis(Glib::ustring const & id, gint axis)
 
 void DeviceManagerImpl::addButton(Glib::ustring const & id, gint button)
 {
-    if ( button >= 0 && button < static_cast<gint>(bitVals.size()) ) {
+    if ( button >= 0 && button < NUM_AXES ) {
         std::list<Glib::RefPtr<InputDeviceImpl> >::iterator it = std::find_if(devices.begin(), devices.end(), IdMatcher(id));
         if ( it != devices.end() ) {
-            gint mask = bitVals[button];
+            gint mask = 1u << button;
             if ( (mask & (*it)->getLiveButtons()) == 0 ) {
                 (*it)->setLiveButtons((*it)->getLiveButtons() | mask);
 
diff --git a/src/ui/dialog/undo-history.h b/src/ui/dialog/undo-history.h
index 0b69693d930..7466daada88 100644
--- a/src/ui/dialog/undo-history.h
+++ b/src/ui/dialog/undo-history.h
@@ -63,7 +63,8 @@ private:
 class CellRendererInt : public Gtk::CellRendererText {
 public:
 
-    struct Filter : std::unary_function<int, bool> {
+    struct Filter
+    {
         virtual ~Filter() = default;
         virtual bool operator() (const int&) const =0;
     };
diff --git a/src/ui/tool/multi-path-manipulator.cpp b/src/ui/tool/multi-path-manipulator.cpp
index c3bf969f4f6..b414fe172d2 100644
--- a/src/ui/tool/multi-path-manipulator.cpp
+++ b/src/ui/tool/multi-path-manipulator.cpp
@@ -38,7 +38,6 @@ namespace UI {
 namespace {
 
 struct hash_nodelist_iterator
-    : public std::unary_function<NodeList::iterator, std::size_t>
 {
     std::size_t operator()(NodeList::iterator i) const {
         return std::hash<NodeList::iterator::pointer>()(&*i);
diff --git a/src/util/units.h b/src/util/units.h
index 39b6f7fd951..d7c0b3d7e49 100644
--- a/src/util/units.h
+++ b/src/util/units.h
@@ -26,8 +26,10 @@
 
 namespace std {
 template <>
-struct hash<Glib::ustring> : public std::unary_function<Glib::ustring, std::size_t> {
-    std::size_t operator()(Glib::ustring const &s) const {
+struct hash<Glib::ustring>
+{
+    std::size_t operator()(Glib::ustring const &s) const
+    {
         return hash<std::string>()(s.raw());
     }
 };
-- 
GitLab

