Skip to content

Commit

Permalink
cleanup: Changes for c++17
Browse files Browse the repository at this point in the history
This cleanup adjusts the rTorrent code to comply with c++17 standard. It also simplifies various parts of the code.
  • Loading branch information
stickz committed Jan 18, 2025
1 parent b5a6ee3 commit 7584164
Show file tree
Hide file tree
Showing 14 changed files with 28 additions and 33 deletions.
2 changes: 1 addition & 1 deletion 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 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
3 changes: 1 addition & 2 deletions 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
10 changes: 5 additions & 5 deletions 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 @@ -186,8 +186,8 @@ DownloadList::insert(Download* 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 src/core/download_store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,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 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
4 changes: 2 additions & 2 deletions src/core/manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,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 @@ -453,7 +453,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 src/core/view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,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) {}

Expand Down Expand Up @@ -50,7 +50,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) {}

Expand Down
2 changes: 1 addition & 1 deletion src/display/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,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 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 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 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
22 changes: 9 additions & 13 deletions src/rpc/parse_commands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,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 +127,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,7 +146,7 @@ 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_newline(c); });

if (first != last) {
if (*first != '\n' && *first != ';' && *first != '\0')
Expand Down
2 changes: 1 addition & 1 deletion 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 7584164

Please sign in to comment.