Skip to content

Commit

Permalink
rTorrent: Cleanup code for c++17 (#74)
Browse files Browse the repository at this point in the history
This pull request cleanups up the rTorrent code for the c++17 standard.
  • Loading branch information
stickz authored Jan 18, 2025
1 parent f5b6c78 commit 77e5aaa
Show file tree
Hide file tree
Showing 18 changed files with 38 additions and 73 deletions.
32 changes: 1 addition & 31 deletions rtorrent/rak/priority_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class priority_queue : public std::vector<Value, Alloc> {

template <typename Key>
iterator find(const Key& key) {
return std::find_if(begin(), end(), std::bind2nd(m_equal, key));
return std::find_if(begin(), end(), [&](auto& value) { return m_equal(value, key); });
}

template <typename Key>
Expand Down Expand Up @@ -114,36 +114,6 @@ class priority_queue : public std::vector<Value, Alloc> {
Equal m_equal;
};

// Iterate while the top node has higher priority, as 'Compare'
// returns false.
template <typename Queue, typename Compare>
class queue_pop_iterator
: public std::iterator<std::forward_iterator_tag, void, void, void, void> {
public:
typedef Queue container_type;

queue_pop_iterator() : m_queue(NULL) {}
queue_pop_iterator(Queue* q, Compare c) : m_queue(q), m_compare(c) {}

queue_pop_iterator& operator ++ () { m_queue->pop(); return *this; }
queue_pop_iterator& operator ++ (int) { m_queue->pop(); return *this; }

typename container_type::const_reference operator * () { return m_queue->top(); }

bool operator != (const queue_pop_iterator& itr) { return !m_queue->empty() && !m_compare(m_queue->top()); }
bool operator == (const queue_pop_iterator& itr) { return m_queue->empty() || m_compare(m_queue->top()); }

private:
Queue* m_queue;
Compare m_compare;
};

template <typename Queue, typename Compare>
inline queue_pop_iterator<Queue, Compare>
queue_popper(Queue& queue, Compare comp) {
return queue_pop_iterator<Queue, Compare>(&queue, comp);
}

}

#endif
2 changes: 1 addition & 1 deletion rtorrent/rak/regex.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

namespace rak {

class regex : public std::unary_function<std::string, bool> {
class regex : public std::function<bool (std::string)> {
public:
regex() {}
regex(const std::string& p) : m_pattern(p) {}
Expand Down
2 changes: 1 addition & 1 deletion rtorrent/src/command_events.cc
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ apply_close_low_diskspace(int64_t arg) {
bool closed = false;
core::Manager::DListItr itr = downloadList->begin();

while ((itr = std::find_if(itr, downloadList->end(), std::mem_fun(&core::Download::is_downloading)))
while ((itr = std::find_if(itr, downloadList->end(), std::mem_fn(&core::Download::is_downloading)))
!= downloadList->end()) {
if ((*itr)->file_list()->free_diskspace() < (uint64_t)arg) {
downloadList->close(*itr);
Expand Down
2 changes: 1 addition & 1 deletion rtorrent/src/command_local.cc
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ file_print_list(torrent::Object::list_const_iterator first, torrent::Object::lis
fprintf(output, (const char*)" %s" + !(flags & file_print_use_space), first->as_string().c_str());
break;
case torrent::Object::TYPE_VALUE:
fprintf(output, (const char*)" %lli" + !(flags & file_print_use_space), first->as_value());
fprintf(output, (const char*)" %li" + !(flags & file_print_use_space), first->as_value());
break;
case torrent::Object::TYPE_LIST:
file_print_list(first->as_list().begin(), first->as_list().end(), output, 0);
Expand Down
3 changes: 1 addition & 2 deletions rtorrent/src/command_tracker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ apply_dht_add_node(const std::string& arg) {
torrent::Object
apply_enable_trackers(int64_t arg) {
for (core::Manager::DListItr itr = control->core()->download_list()->begin(), last = control->core()->download_list()->end(); itr != last; ++itr) {
std::for_each((*itr)->tracker_list()->begin(), (*itr)->tracker_list()->end(),
arg ? std::mem_fun(&torrent::Tracker::enable) : std::mem_fun(&torrent::Tracker::disable));
std::for_each((*itr)->tracker_list()->begin(), (*itr)->tracker_list()->end(), std::mem_fn(arg ? &torrent::Tracker::enable : &torrent::Tracker::disable));

if (arg && !rpc::call_command_value("trackers.use_udp"))
(*itr)->enable_udp_trackers(false);
Expand Down
2 changes: 1 addition & 1 deletion rtorrent/src/core/curl_stack.cc
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ CurlStack::remove_get(CurlGet* get) {
throw torrent::internal_error("Error calling curl_multi_remove_handle.");

if (m_active == m_maxActive &&
(itr = std::find_if(begin(), end(), std::not1(std::mem_fun(&CurlGet::is_active)))) != end()) {
(itr = std::find_if(begin(), end(), [](CurlGet* get) { return !get->is_active(); })) != end()) {
(*itr)->set_active(true);

if (curl_multi_add_handle((CURLM*)m_handle, (*itr)->handle()) > 0)
Expand Down
14 changes: 7 additions & 7 deletions rtorrent/src/core/download_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ DownloadList::check_contains(Download* d) {

void
DownloadList::clear() {
std::for_each(begin(), end(), std::bind1st(std::mem_fun(&DownloadList::close), this));
std::for_each(begin(), end(), [&](Download* d) { close(d); });
std::for_each(begin(), end(), [](Download* d) { delete d; });

base_type::clear();
}

void
DownloadList::session_save() {
unsigned int c = std::count_if(begin(), end(), std::bind1st(std::mem_fun(&DownloadStore::save_resume), control->core()->download_store()));
unsigned int c = std::count_if(begin(), end(), [&](Download* d) { return control->core()->download_store()->save_resume(d); });

if (c != size())
lt_log_print(torrent::LOG_ERROR, "Failed to save session torrents.");
Expand Down Expand Up @@ -181,13 +181,13 @@ DownloadList::insert(Download* download) {
lt_log_print_info(torrent::LOG_TORRENT_INFO, download->info(), "download_list", "Inserting download.");

try {
(*itr)->data()->slot_initial_hash() = std::bind(&DownloadList::hash_done, this, download);
(*itr)->data()->slot_download_done() = std::bind(&DownloadList::received_finished, this, download);
(*itr)->data()->slot_initial_hash() = [this, download]() { hash_done(download); };
(*itr)->data()->slot_download_done() = [this, download]() { received_finished(download); };

// This needs to be separated into two different calls to ensure
// the download remains in the view.
std::for_each(control->view_manager()->begin(), control->view_manager()->end(), std::bind2nd(std::mem_fun(&View::insert), download));
std::for_each(control->view_manager()->begin(), control->view_manager()->end(), std::bind2nd(std::mem_fun(&View::filter_download), download));
std::for_each(control->view_manager()->begin(), control->view_manager()->end(), [&download](View* v) { v->insert(download); });
std::for_each(control->view_manager()->begin(), control->view_manager()->end(), [&download](View* v) { v->filter_download(download); });

DL_TRIGGER_EVENT(*itr, "event.download.inserted");

Expand Down Expand Up @@ -220,7 +220,7 @@ DownloadList::erase(iterator itr) {
control->core()->download_store()->remove(*itr);

DL_TRIGGER_EVENT(*itr, "event.download.erased");
std::for_each(control->view_manager()->begin(), control->view_manager()->end(), std::bind2nd(std::mem_fun(&View::erase), *itr));
std::for_each(control->view_manager()->begin(), control->view_manager()->end(), [itr](View* v) { v->erase(*itr); });

torrent::download_remove(*(*itr)->download());
delete *itr;
Expand Down
2 changes: 1 addition & 1 deletion rtorrent/src/core/download_store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ DownloadStore::get_formated_entries() {
if (!d.update(utils::Directory::update_hide_dot))
throw torrent::storage_error("core::DownloadStore::update() could not open directory \"" + m_path + "\"");

d.erase(std::remove_if(d.begin(), d.end(), std::ptr_fun(&not_correct_format)), d.end());
d.erase(std::remove_if(d.begin(), d.end(), [&](const utils::directory_entry& entry) { return not_correct_format(entry); }), d.end());

return d;
}
Expand Down
2 changes: 1 addition & 1 deletion rtorrent/src/core/http_queue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace core {

HttpQueue::iterator
HttpQueue::insert(const std::string& url, std::iostream* s) {
std::auto_ptr<CurlGet> h(m_slot_factory());
std::unique_ptr<CurlGet> h(m_slot_factory());

h->set_url(url);
h->set_stream(s);
Expand Down
8 changes: 4 additions & 4 deletions rtorrent/src/core/manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ Manager::cleanup() {
void
Manager::shutdown(bool force) {
if (!force)
std::for_each(m_downloadList->begin(), m_downloadList->end(), std::bind1st(std::mem_fun(&DownloadList::pause_default), m_downloadList));
std::for_each(m_downloadList->begin(), m_downloadList->end(), [this](Download* d) { m_downloadList->pause_default(d); });
else
std::for_each(m_downloadList->begin(), m_downloadList->end(), std::bind1st(std::mem_fun(&DownloadList::close_quick), m_downloadList));
std::for_each(m_downloadList->begin(), m_downloadList->end(), [this](Download* d) { m_downloadList->close_quick(d); });
}

void
Expand Down Expand Up @@ -431,7 +431,7 @@ path_expand(std::vector<std::string>* paths, const std::string& pattern) {
currentCache.swap(nextCache);
}

std::transform(currentCache.begin(), currentCache.end(), std::back_inserter(*paths), std::mem_fun_ref(&utils::Directory::path));
std::transform(currentCache.begin(), currentCache.end(), std::back_inserter(*paths), std::mem_fn(&utils::Directory::path));
}

bool
Expand Down Expand Up @@ -465,7 +465,7 @@ Manager::try_create_download_expand(const std::string& uri, int flags, command_l
void
Manager::receive_hashing_changed() {
bool foundHashing = std::find_if(m_hashingView->begin_visible(), m_hashingView->end_visible(),
std::mem_fun(&Download::is_hash_checking)) != m_hashingView->end_visible();
std::mem_fn(&Download::is_hash_checking)) != m_hashingView->end_visible();

// Try quick hashing all those with hashing == initial, set them to
// something else when failed.
Expand Down
4 changes: 2 additions & 2 deletions rtorrent/src/core/view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
namespace core {

// Also add focus thingie here?
struct view_downloads_compare : std::binary_function<Download*, Download*, bool> {
struct view_downloads_compare : std::function<bool (Download*, Download*)> {
view_downloads_compare(const torrent::Object& cmd) : m_command(cmd) {}

bool operator () (Download* d1, Download* d2) const {
Expand Down Expand Up @@ -86,7 +86,7 @@ struct view_downloads_compare : std::binary_function<Download*, Download*, bool>
const torrent::Object& m_command;
};

struct view_downloads_filter : std::unary_function<Download*, bool> {
struct view_downloads_filter : std::function<bool (Download*)> {
view_downloads_filter(const torrent::Object& cmd, const torrent::Object& cmd2) : m_command(cmd), m_command2(cmd2) {}

bool operator () (Download* d1) const {
Expand Down
2 changes: 1 addition & 1 deletion rtorrent/src/display/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ print_download_status(char* first, char* last, core::Download* d) {
} else if (d->tracker_list()->has_active_not_scrape()) {
torrent::TrackerList::iterator itr =
std::find_if(d->tracker_list()->begin(), d->tracker_list()->end(),
std::mem_fun(&torrent::Tracker::is_busy_not_scrape));
std::mem_fn(&torrent::Tracker::is_busy_not_scrape));
char status[128];

(*itr)->get_status(status, sizeof(status));
Expand Down
2 changes: 1 addition & 1 deletion rtorrent/src/display/window_download_chunks_seen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ WindowDownloadChunksSeen::redraw() {
if (bitfield->get(chunk - seen)) {
attr = A_NORMAL;
} else if (itrTransfer != transferChunks.end() && (uint32_t)(chunk - seen) == (*itrTransfer)->index()) {
if (std::find_if((*itrTransfer)->begin(), (*itrTransfer)->end(), std::mem_fun_ref(&torrent::Block::is_transfering)) != (*itrTransfer)->end())
if (std::any_of((*itrTransfer)->begin(), (*itrTransfer)->end(), std::mem_fn(&torrent::Block::is_transfering)))
attr = A_REVERSE;
else
attr = A_BOLD | A_UNDERLINE;
Expand Down
2 changes: 1 addition & 1 deletion rtorrent/src/input/manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Manager::pressed(int key) {
if (m_textInput != NULL)
m_textInput->pressed(key);
else
std::find_if(rbegin(), rend(), std::bind2nd(std::mem_fun(&Bindings::pressed), key));
std::find_if(rbegin(), rend(), [&key](Bindings* bind) { return bind->pressed(key); });
}

}
2 changes: 1 addition & 1 deletion rtorrent/src/option_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ bool
OptionParser::has_flag(char flag, int argc, char** argv) {
char options[3] = { '-', flag, '\0' };

return std::find_if(argv, argv + argc, std::not1(std::bind1st(std::ptr_fun(&std::strcmp), options))) != argv + argc;
return std::find_if(argv, argv + argc, [&options](char* c) { return std::strcmp(c, options) == 0; }) != argv + argc;
}

std::string
Expand Down
3 changes: 2 additions & 1 deletion rtorrent/src/rpc/command_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#ifndef RTORRENT_RPC_COMMAND_MAP_H
#define RTORRENT_RPC_COMMAND_MAP_H

#include <functional>
#include <map>
#include <string>
#include <cstring>
Expand All @@ -46,7 +47,7 @@

namespace rpc {

struct command_map_comp : public std::binary_function<const char*, const char*, bool> {
struct command_map_comp : public std::function<bool (const char*, const char*)> {
bool operator () (const char* arg1, const char* arg2) const { return std::strcmp(arg1, arg2) < 0; }
};

Expand Down
25 changes: 10 additions & 15 deletions rtorrent/src/rpc/parse_commands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
#include <algorithm>
#include <fstream>
#include <string>
#include <functional>
#include <rak/path.h>
#include <torrent/exceptions.h>

Expand All @@ -52,17 +51,13 @@ CommandMap commands;
XmlRpc xmlrpc;
ExecFile execFile;

struct command_map_is_space : std::unary_function<char, bool> {
bool operator () (char c) const {
return c == ' ' || c == '\t';
}
};
inline bool command_map_is_space(char c) {
return c == ' ' || c == '\t';
}

struct command_map_is_newline : std::unary_function<char, bool> {
bool operator () (char c) const {
return c == '\n' || c == '\0' || c == ';';
}
};
inline bool command_map_is_newline(char c) {
return c == '\n' || c == '\0' || c == ';';
}

// Only escape eol on odd number of escape characters. We know that
// there can't be any characters in between, so this should work for
Expand Down Expand Up @@ -131,15 +126,15 @@ parse_command_name(const char* first, const char* last, char* dest_first, char*
// the code below for both cases.
parse_command_type
parse_command(target_type target, const char* first, const char* last) {
first = std::find_if(first, last, std::not1(command_map_is_space()));
first = std::find_if(first, last, [&](char c) { return !command_map_is_space(c); });

if (first == last || *first == '#')
return std::make_pair(torrent::Object(), first);

char key[128];

first = parse_command_name(first, last, key, key + 128);
first = std::find_if(first, last, std::not1(command_map_is_space()));
first = std::find_if(first, last, [&](char c) { return !command_map_is_space(c); });

if (first == last || *first != '=')
throw torrent::input_error("Could not find '=' in command '" + std::string(key) + "'.");
Expand All @@ -150,10 +145,10 @@ parse_command(target_type target, const char* first, const char* last) {
// Find the last character that is part of this command, skipping
// the whitespace at the end. This ensures us that the caller
// doesn't need to do this nor check for junk at the end.
first = std::find_if(first, last, std::not1(command_map_is_space()));
first = std::find_if(first, last, [&](char c) { return !command_map_is_space(c); });

if (first != last) {
if (*first != '\n' && *first != ';' && *first != '\0')
if (!command_map_is_newline(*first))
throw torrent::input_error("Junk at end of input.");

first++;
Expand Down
2 changes: 1 addition & 1 deletion rtorrent/src/rpc/scgi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ SCgi::event_read() {
utils::SocketFd fd;

while ((fd = get_fd().accept(&sa)).is_valid()) {
SCgiTask* task = std::find_if(m_task, m_task + max_tasks, std::mem_fun_ref(&SCgiTask::is_available));
SCgiTask* task = std::find_if(m_task, m_task + max_tasks, std::mem_fn(&SCgiTask::is_available));

if (task == m_task + max_tasks) {
// Ergh... just closing for now.
Expand Down

0 comments on commit 77e5aaa

Please sign in to comment.