From 712e7e5db00f7746fa822fbaea4ea02e3154c9e6 Mon Sep 17 00:00:00 2001 From: stickz Date: Sun, 12 Jan 2025 14:16:20 -0500 Subject: [PATCH] cleanup: Remove rak/functional.h This commit replaces the `rak/functional.h` features with lambdas and std functions. There was one instance with `std::sort` where the comparison operator was always evaluating to false and it wasn't sorting anything. This is removed in favour of the default comparison operator. `std::sort(transferChunks.begin(), transferChunks.end());` --- rak/functional.h | 501 --------------------- src/command_ui.cc | 1 - src/core/curl_stack.cc | 1 - src/core/download.cc | 1 - src/core/download_list.cc | 5 +- src/core/http_queue.cc | 1 - src/core/view.cc | 3 +- src/core/view_manager.cc | 5 +- src/display/manager.cc | 1 - src/display/window_download_chunks_seen.cc | 3 +- src/display/window_http_queue.cc | 3 +- src/input/path_input.cc | 11 +- src/rpc/command_scheduler.cc | 3 +- src/rpc/object_storage.cc | 9 +- src/rpc/parse_commands.cc | 2 +- src/ui/element_peer_list.cc | 7 +- 16 files changed, 21 insertions(+), 536 deletions(-) delete mode 100644 rak/functional.h diff --git a/rak/functional.h b/rak/functional.h deleted file mode 100644 index a8fa8b4fa..000000000 --- a/rak/functional.h +++ /dev/null @@ -1,501 +0,0 @@ -// rak - Rakshasa's toolbox -// Copyright (C) 2005-2007, Jari Sundell -// -// 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -// -// In addition, as a special exception, the copyright holders give -// permission to link the code of portions of this program with the -// OpenSSL library under certain conditions as described in each -// individual source file, and distribute linked combinations -// including the two. -// -// You must obey the GNU General Public License in all respects for -// all of the code used other than OpenSSL. If you modify file(s) -// with this exception, you may extend this exception to your version -// of the file(s), but you are not obligated to do so. If you do not -// wish to do so, delete this exception statement from your version. -// If you delete this exception statement from all source files in the -// program, then also delete it here. -// -// Contact: Jari Sundell -// -// Skomakerveien 33 -// 3185 Skoppum, NORWAY - -#ifndef RAK_FUNCTIONAL_H -#define RAK_FUNCTIONAL_H - -#include -#include - -namespace rak { - -template -struct reference_fix { - typedef Type type; -}; - -template -struct reference_fix { - typedef Type type; -}; - -template -struct value_t { - value_t(Type v) : m_v(v) {} - - Type operator () () const { return m_v; } - - Type m_v; -}; - -template -inline value_t -value(Type v) { - return value_t(v); -} - -template -struct accumulate_t { - accumulate_t(Type t, Ftor f) : result(t), m_f(f) {} - - template - void operator () (const Arg& a) { result += m_f(a); } - - Type result; - Ftor m_f; -}; - -template -inline accumulate_t -accumulate(Type t, Ftor f) { - return accumulate_t(t, f); -} - -// Operators: - -template -struct equal_t { - typedef bool result_type; - - equal_t(Type t, Ftor f) : m_t(t), m_f(f) {} - - template - bool operator () (Arg& a) { - return m_t == m_f(a); - } - - Type m_t; - Ftor m_f; -}; - -template -inline equal_t -equal(Type t, Ftor f) { - return equal_t(t, f); -} - -template -struct equal_ptr_t { - typedef bool result_type; - - equal_ptr_t(Type* t, Ftor f) : m_t(t), m_f(f) {} - - template - bool operator () (const Arg& a) { - return *m_t == *m_f(a); - } - - Type* m_t; - Ftor m_f; -}; - -template -inline equal_ptr_t -equal_ptr(Type* t, Ftor f) { - return equal_ptr_t(t, f); -} - -template -struct not_equal_t { - typedef bool result_type; - - not_equal_t(Type t, Ftor f) : m_t(t), m_f(f) {} - - template - bool operator () (Arg& a) { - return m_t != m_f(a); - } - - Type m_t; - Ftor m_f; -}; - -template -inline not_equal_t -not_equal(Type t, Ftor f) { - return not_equal_t(t, f); -} - -template -struct less_t { - typedef bool result_type; - - less_t(Type t, Ftor f) : m_t(t), m_f(f) {} - - template - bool operator () (Arg& a) { - return m_t < m_f(a); - } - - Type m_t; - Ftor m_f; -}; - -template -inline less_t -less(Type t, Ftor f) { - return less_t(t, f); -} - -template -struct less2_t : public std::binary_function { - less2_t(FtorA f_a, FtorB f_b) : m_f_a(f_a), m_f_b(f_b) {} - - bool operator () (typename FtorA::argument_type a, typename FtorB::argument_type b) { - return m_f_a(a) < m_f_b(b); - } - - FtorA m_f_a; - FtorB m_f_b; -}; - -template -inline less2_t -less2(FtorA f_a, FtorB f_b) { - return less2_t(f_a,f_b); -} - -template -struct _greater { - typedef bool result_type; - - _greater(Type t, Ftor f) : m_t(t), m_f(f) {} - - template - bool operator () (Arg& a) { - return m_t > m_f(a); - } - - Type m_t; - Ftor m_f; -}; - -template -inline _greater -greater(Type t, Ftor f) { - return _greater(t, f); -} - -template -struct greater2_t : public std::binary_function { - greater2_t(FtorA f_a, FtorB f_b) : m_f_a(f_a), m_f_b(f_b) {} - - bool operator () (typename FtorA::argument_type a, typename FtorB::argument_type b) { - return m_f_a(a) > m_f_b(b); - } - - FtorA m_f_a; - FtorB m_f_b; -}; - -template -inline greater2_t -greater2(FtorA f_a, FtorB f_b) { - return greater2_t(f_a,f_b); -} - -template -struct less_equal_t { - typedef bool result_type; - - less_equal_t(Type t, Ftor f) : m_t(t), m_f(f) {} - - template - bool operator () (Arg& a) { - return m_t <= m_f(a); - } - - Type m_t; - Ftor m_f; -}; - -template -inline less_equal_t -less_equal(Type t, Ftor f) { - return less_equal_t(t, f); -} - -template -struct greater_equal_t { - typedef bool result_type; - - greater_equal_t(Type t, Ftor f) : m_t(t), m_f(f) {} - - template - bool operator () (Arg& a) { - return m_t >= m_f(a); - } - - Type m_t; - Ftor m_f; -}; - -template -inline greater_equal_t -greater_equal(Type t, Ftor f) { - return greater_equal_t(t, f); -} - -template -struct invert : public std::unary_function { - Tp - operator () (const Tp& x) const { return ~x; } -}; - -template -struct on_t : public std::unary_function { - typedef typename Dest::result_type result_type; - - on_t(Src s, Dest d) : m_dest(d), m_src(s) {} - - result_type operator () (typename reference_fix::type arg) { - return m_dest(m_src(arg)); - } - - Dest m_dest; - Src m_src; -}; - -template -inline on_t -on(Src s, Dest d) { - return on_t(s, d); -} - -template -struct on2_t : public std::binary_function { - typedef typename Dest::result_type result_type; - - on2_t(Src s, Dest d) : m_dest(d), m_src(s) {} - - result_type operator () (typename reference_fix::type first, typename reference_fix::type second) { - return m_dest(m_src(first), second); - } - - Dest m_dest; - Src m_src; -}; - -template -inline on2_t -on2(Src s, Dest d) { - return on2_t(s, d); -} - -// Creates a functor for accessing a member. -template -struct mem_ptr_t : public std::unary_function { - mem_ptr_t(Member Class::*m) : m_member(m) {} - - Member& operator () (Class* c) { - return c->*m_member; - } - - const Member& operator () (const Class* c) { - return c->*m_member; - } - - Member Class::*m_member; -}; - -template -inline mem_ptr_t -mem_ptr(Member Class::*m) { - return mem_ptr_t(m); -} - -template -struct mem_ref_t : public std::unary_function { - mem_ref_t(Member Class::*m) : m_member(m) {} - - Member& operator () (Class& c) { - return c.*m_member; - } - - Member Class::*m_member; -}; - -template -struct const_mem_ref_t : public std::unary_function { - const_mem_ref_t(const Member Class::*m) : m_member(m) {} - - const Member& operator () (const Class& c) { - return c.*m_member; - } - - const Member Class::*m_member; -}; - -template -inline mem_ref_t -mem_ref(Member Class::*m) { - return mem_ref_t(m); -} - -template -inline const_mem_ref_t -const_mem_ref(const Member Class::*m) { - return const_mem_ref_t(m); -} - -template -struct if_then_t { - if_then_t(Cond c, Then t) : m_cond(c), m_then(t) {} - - template - void operator () (Arg& a) { - if (m_cond(a)) - m_then(a); - } - - Cond m_cond; - Then m_then; -}; - -template -inline if_then_t -if_then(Cond c, Then t) { - return if_then_t(c, t); -} - -template -class bind1st_t : public std::unary_function { -public: - typedef typename reference_fix::type value_type; - typedef typename reference_fix::type argument_type; - - bind1st_t(const Operation& op, const value_type v) : - m_op(op), m_value(v) {} - - typename Operation::result_type - operator () (const argument_type arg) { - return m_op(m_value, arg); - } - -protected: - Operation m_op; - value_type m_value; -}; - -template -inline bind1st_t -bind1st(const Operation& op, const Type& val) { - return bind1st_t(op, val); -} - -template -class bind2nd_t : public std::unary_function { -public: - typedef typename reference_fix::type argument_type; - typedef typename reference_fix::type value_type; - - bind2nd_t(const Operation& op, const value_type v) : - m_op(op), m_value(v) {} - - typename Operation::result_type - operator () (argument_type arg) { - return m_op(arg, m_value); - } - -protected: - Operation m_op; - value_type m_value; -}; - -template -inline bind2nd_t -bind2nd(const Operation& op, const Type& val) { - return bind2nd_t(op, val); -} - -// Lightweight callback function including pointer to object. Should -// be replaced by TR1 stuff later. Requires an object to bind, instead -// of using a seperate functor for that. - -template -inline void -slot_list_call(const Container& slot_list) { - if (slot_list.empty()) - return; - - typename Container::const_iterator first = slot_list.begin(); - typename Container::const_iterator next = slot_list.begin(); - - while (++next != slot_list.end()) { - (*first)(); - first = next; - } - - (*first)(); -} - -template -inline void -slot_list_call(const Container& slot_list, Arg1 arg1) { - if (slot_list.empty()) - return; - - typename Container::const_iterator first = slot_list.begin(); - typename Container::const_iterator next = slot_list.begin(); - - while (++next != slot_list.end()) { - (*first)(arg1); - first = next; - } - - (*first)(arg1); -} - -template -inline void -slot_list_call(const Container& slot_list, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4) { - if (slot_list.empty()) - return; - - typename Container::const_iterator first = slot_list.begin(); - typename Container::const_iterator next = slot_list.begin(); - - while (++next != slot_list.end()) { - (*first)(arg1, arg2, arg3, arg4); - first = next; - } - - (*first)(arg1, arg2, arg3, arg4); -} - -} - -#endif diff --git a/src/command_ui.cc b/src/command_ui.cc index c93a33667..1eebc23c7 100644 --- a/src/command_ui.cc +++ b/src/command_ui.cc @@ -6,7 +6,6 @@ #include #include -#include #include #include "core/manager.h" diff --git a/src/core/curl_stack.cc b/src/core/curl_stack.cc index 0bf1a5439..74b8591a8 100644 --- a/src/core/curl_stack.cc +++ b/src/core/curl_stack.cc @@ -4,7 +4,6 @@ #include #include -#include "rak/functional.h" #include "curl_get.h" #include "curl_socket.h" #include "curl_stack.h" diff --git a/src/core/download.cc b/src/core/download.cc index fb58efbf4..8d1169555 100644 --- a/src/core/download.cc +++ b/src/core/download.cc @@ -38,7 +38,6 @@ #include #include -#include #include #include #include diff --git a/src/core/download_list.cc b/src/core/download_list.cc index 5860dc921..cebe6b65c 100644 --- a/src/core/download_list.cc +++ b/src/core/download_list.cc @@ -39,7 +39,6 @@ #include #include #include -#include #include #include #include @@ -99,7 +98,7 @@ DownloadList::session_save() { DownloadList::iterator DownloadList::find(const torrent::HashString& hash) { - return std::find_if(begin(), end(), rak::equal(hash, rak::on(std::mem_fun(&Download::info), std::mem_fun(&torrent::DownloadInfo::hash)))); + return std::find_if(begin(), end(), [hash](Download* d) { return hash == d->info()->hash(); }); } DownloadList::iterator @@ -109,7 +108,7 @@ DownloadList::find_hex(const char* hash) { for (torrent::HashString::iterator itr = key.begin(), last = key.end(); itr != last; itr++, hash += 2) *itr = (rak::hexchar_to_value(*hash) << 4) + rak::hexchar_to_value(*(hash + 1)); - return std::find_if(begin(), end(), rak::equal(key, rak::on(std::mem_fun(&Download::info), std::mem_fun(&torrent::DownloadInfo::hash)))); + return std::find_if(begin(), end(), [key](Download* d) { return key == d->info()->hash(); }); } Download* diff --git a/src/core/http_queue.cc b/src/core/http_queue.cc index d3ae2efd3..d83cddb5d 100644 --- a/src/core/http_queue.cc +++ b/src/core/http_queue.cc @@ -40,7 +40,6 @@ #include #include -#include "rak/functional.h" #include "http_queue.h" #include "curl_get.h" diff --git a/src/core/view.cc b/src/core/view.cc index 7d28af65e..9733d5b70 100644 --- a/src/core/view.cc +++ b/src/core/view.cc @@ -2,7 +2,6 @@ #include #include -#include #include #include @@ -144,7 +143,7 @@ View::initialize(const std::string& name) { m_name = name; // Urgh, wrong. No filtering being done. - std::for_each(dlist->begin(), dlist->end(), rak::bind1st(std::mem_fun(&View::push_back), this)); + std::for_each(dlist->begin(), dlist->end(), [&](Download* d) { push_back(d); }); m_size = base_type::size(); m_focus = 0; diff --git a/src/core/view_manager.cc b/src/core/view_manager.cc index 66db5ca2c..062f267a0 100644 --- a/src/core/view_manager.cc +++ b/src/core/view_manager.cc @@ -37,7 +37,6 @@ #include "config.h" #include -#include #include #include @@ -76,12 +75,12 @@ ViewManager::insert(const std::string& name) { ViewManager::iterator ViewManager::find(const std::string& name) { - return std::find_if(begin(), end(), rak::equal(name, std::mem_fun(&View::name))); + return std::find_if(begin(), end(), [name](View* v){ return name == v->name(); }); } ViewManager::iterator ViewManager::find_throw(const std::string& name) { - iterator itr = std::find_if(begin(), end(), rak::equal(name, std::mem_fun(&View::name))); + iterator itr = std::find_if(begin(), end(), [name](View* v){ return name == v->name(); }); if (itr == end()) throw torrent::input_error("Could not find view: " + name); diff --git a/src/display/manager.cc b/src/display/manager.cc index 6bdfd1547..e914e5e21 100644 --- a/src/display/manager.cc +++ b/src/display/manager.cc @@ -38,7 +38,6 @@ #include #include -#include #include "canvas.h" #include "globals.h" diff --git a/src/display/window_download_chunks_seen.cc b/src/display/window_download_chunks_seen.cc index 27fb0f794..e445320af 100644 --- a/src/display/window_download_chunks_seen.cc +++ b/src/display/window_download_chunks_seen.cc @@ -38,7 +38,6 @@ #include #include -#include #include #include #include @@ -95,7 +94,7 @@ WindowDownloadChunksSeen::redraw() { std::vector transferChunks(transfers->size(), 0); std::copy(transfers->begin(), transfers->end(), transferChunks.begin()); - std::sort(transferChunks.begin(), transferChunks.end(), rak::less2(std::mem_fun(&torrent::BlockList::index), std::mem_fun(&torrent::BlockList::index))); + std::sort(transferChunks.begin(), transferChunks.end()); std::vector::const_iterator itrTransfer = transferChunks.begin(); diff --git a/src/display/window_http_queue.cc b/src/display/window_http_queue.cc index 7b44d9b2e..936e7c0c4 100644 --- a/src/display/window_http_queue.cc +++ b/src/display/window_http_queue.cc @@ -42,7 +42,6 @@ #include "core/http_queue.h" #include "canvas.h" -#include "rak/functional.h" #include "window_http_queue.h" namespace display { @@ -145,7 +144,7 @@ WindowHttpQueue::receive_insert(core::CurlGet* h) { void WindowHttpQueue::receive_erase(core::CurlGet* h) { - Container::iterator itr = std::find_if(m_container.begin(), m_container.end(), rak::equal(h, std::mem_fun_ref(&Node::get_http))); + Container::iterator itr = std::find_if(m_container.begin(), m_container.end(), [h](Node n) { return h == n.get_http(); }); if (itr == m_container.end()) throw std::logic_error("WindowHttpQueue::receive_erase(...) tried to remove an object we don't have"); diff --git a/src/input/path_input.cc b/src/input/path_input.cc index 898807756..91ba37d47 100644 --- a/src/input/path_input.cc +++ b/src/input/path_input.cc @@ -38,7 +38,6 @@ #include #include -#include #include #include @@ -105,7 +104,7 @@ PathInput::receive_do_complete() { if (r.first == r.second) return; // Show some nice colors here. - std::string base = rak::make_base(r.first, r.second, rak::const_mem_ref(&utils::directory_entry::s_name)); + std::string base = rak::make_base(r.first, r.second, std::mem_fn(&utils::directory_entry::s_name)); // Clear the path after the cursor to make this code cleaner. It's // not really nessesary to add the complexity just because someone @@ -154,8 +153,12 @@ PathInput::range_type PathInput::find_incomplete(utils::Directory& d, const std::string& f) { range_type r; - r.first = std::find_if(d.begin(), d.end(), rak::bind2nd(std::ptr_fun(&find_complete_not_compare), f)); - r.second = std::find_if(r.first, d.end(), rak::bind2nd(std::ptr_fun(&find_complete_compare), f)); + r.first = std::find_if(d.begin(), d.end(), [f](const utils::directory_entry& de) { + return find_complete_not_compare(de, f); + }); + r.second = std::find_if(r.first, d.end(), [f](const utils::directory_entry& de) { + return find_complete_compare(de, f); + }); return r; } diff --git a/src/rpc/command_scheduler.cc b/src/rpc/command_scheduler.cc index c0861c5c2..c0a39b3ad 100644 --- a/src/rpc/command_scheduler.cc +++ b/src/rpc/command_scheduler.cc @@ -39,7 +39,6 @@ #include #include #include -#include #include #include @@ -55,7 +54,7 @@ CommandScheduler::~CommandScheduler() { CommandScheduler::iterator CommandScheduler::find(const std::string& key) { - return std::find_if(begin(), end(), rak::equal(key, std::mem_fun(&CommandSchedulerItem::key))); + return std::find_if(begin(), end(), [key](CommandSchedulerItem* item) { return key == item->key(); }); } CommandScheduler::iterator diff --git a/src/rpc/object_storage.cc b/src/rpc/object_storage.cc index 19fbd6ca2..38d4d1dfb 100644 --- a/src/rpc/object_storage.cc +++ b/src/rpc/object_storage.cc @@ -38,7 +38,6 @@ #include "object_storage.h" -#include "rak/functional.h" #include "parse.h" #include "parse_commands.h" @@ -223,8 +222,7 @@ object_storage::erase_multi_key(const torrent::raw_string& key, const std::strin if (r_itr == m_rlookup.end()) return; - rlookup_mapped_iterator rm_itr = std::find_if(r_itr->second.begin(), r_itr->second.end(), - rak::equal(key, rak::mem_ptr(&value_type::first))); + rlookup_mapped_iterator rm_itr = std::find_if(r_itr->second.begin(), r_itr->second.end(), [key](value_type* type) { return key == type->first; }); if (rm_itr != r_itr->second.end()) r_itr->second.erase(rm_itr); @@ -243,8 +241,7 @@ object_storage::set_multi_key_obj(const torrent::raw_string& key, const std::str if (r_itr == m_rlookup.end()) r_itr = m_rlookup.insert(std::make_pair(cmd_key, rlookup_type::mapped_type())).first; - if (std::find_if(r_itr->second.begin(), r_itr->second.end(), - rak::equal(key, rak::mem_ptr(&value_type::first))) == r_itr->second.end()) + if (std::find_if(r_itr->second.begin(), r_itr->second.end(), [key](value_type* type) { return key == type->first; }) == r_itr->second.end()) r_itr->second.push_back(&*itr); } @@ -259,7 +256,7 @@ object_storage::rlookup_list(const std::string& cmd_key) { if (r_itr != m_rlookup.end()) std::transform(r_itr->second.begin(), r_itr->second.end(), std::back_inserter(result), - std::bind(&key_type::c_str, std::bind(rak::mem_ptr(&value_type::first), std::placeholders::_1))); + std::bind(&key_type::c_str, std::bind(std::mem_fn(&value_type::first), std::placeholders::_1))); return result; } diff --git a/src/rpc/parse_commands.cc b/src/rpc/parse_commands.cc index b3015868d..7ba53c876 100644 --- a/src/rpc/parse_commands.cc +++ b/src/rpc/parse_commands.cc @@ -39,7 +39,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/ui/element_peer_list.cc b/src/ui/element_peer_list.cc index 2564e6dbd..216f9868a 100644 --- a/src/ui/element_peer_list.cc +++ b/src/ui/element_peer_list.cc @@ -36,8 +36,6 @@ #include "config.h" -#include - #include #include #include @@ -63,11 +61,10 @@ ElementPeerList::ElementPeerList(core::Download* d) : m_listItr = m_list.end(); - std::for_each(m_download->download()->connection_list()->begin(), m_download->download()->connection_list()->end(), - rak::bind1st(std::mem_fun(&PList::push_back), &m_list)); - torrent::ConnectionList* connection_list = m_download->download()->connection_list(); + std::for_each(connection_list->begin(), connection_list->end(), [&](torrent::Peer* peer) { m_list.push_back(peer); }); + m_peer_connected = connection_list->signal_connected().insert(connection_list->signal_connected().end(), std::bind(&ElementPeerList::receive_peer_connected, this, std::placeholders::_1)); m_peer_disconnected = connection_list->signal_disconnected().insert(connection_list->signal_disconnected().end(),