Skip to content

Commit

Permalink
fix compilation with C++17
Browse files Browse the repository at this point in the history
Various function wrappers were removed and need to be converted to
lambdas.

Signed-off-by: Rosen Penev <[email protected]>
  • Loading branch information
neheb committed Dec 25, 2024
1 parent d1c7c40 commit 71871ad
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 34 deletions.
2 changes: 1 addition & 1 deletion rak/algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ advance_backward(_InputIter __first, _InputIter __last, _Distance __distance) {
}

template <typename _Value>
struct compare_base : public std::binary_function<_Value, _Value, bool> {
struct compare_base {
bool operator () (const _Value& complete, const _Value& base) const {
return !complete.compare(0, base.size(), base);
}
Expand Down
2 changes: 1 addition & 1 deletion src/data/chunk_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ ChunkList::partition_optimize(Queue::iterator first, Queue::iterator last, int w
for (Queue::iterator itr = first; itr != last;) {
auto range = seek_range(itr, last);

bool required = std::find_if(itr, range, std::bind1st(std::mem_fn(&ChunkList::check_node), this)) != range;
bool required = std::any_of(itr, range, [this](auto wrapper) { return check_node(wrapper); });
dontSkip = dontSkip || required;

if (!required && std::distance(itr, range) < maxDistance) {
Expand Down
8 changes: 5 additions & 3 deletions src/dht/dht_bucket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ DhtBucket::add_node(DhtNode* n) {

void
DhtBucket::remove_node(DhtNode* n) {
iterator itr = std::find_if(begin(), end(), std::bind2nd(std::equal_to<DhtNode*>(), n));
auto itr = std::find(begin(), end(), n);
if (itr == end())
throw internal_error("DhtBucket::remove_node called for node not in bucket.");

Expand Down Expand Up @@ -165,9 +165,11 @@ DhtBucket::split(const HashString& id) {

// Move nodes over to other bucket if they fall in its range, then
// delete them from this one.
iterator split = std::partition(begin(), end(), std::bind2nd(std::mem_fn(&DhtNode::is_in_range), this));
auto split = std::partition(begin(), end(), [this](auto dht) { return dht->is_in_range(this); });
other->insert(other->end(), split, end());
std::for_each(other->begin(), other->end(), std::bind2nd(std::mem_fn(&DhtNode::set_bucket), other));
for (auto& dht : *other) {
dht->set_bucket(other);
}
erase(split, end());

other->set_time(m_lastChanged);
Expand Down
6 changes: 3 additions & 3 deletions src/dht/dht_hash_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace torrent {
// aligned 64-bit access.
static const unsigned int hashstring_hash_ofs = 8;

struct hashstring_ptr_hash : public std::unary_function<const HashString*, size_t> {
struct hashstring_ptr_hash {
size_t operator () (const HashString* n) const {
#if USE_ALIGNED
size_t result = 0;
Expand All @@ -75,7 +75,7 @@ struct hashstring_ptr_hash : public std::unary_function<const HashString*, size_
}
};

struct hashstring_hash : public std::unary_function<HashString, size_t> {
struct hashstring_hash {
size_t operator () (const HashString& n) const {
#if USE_ALIGNED
size_t result = 0;
Expand All @@ -93,7 +93,7 @@ struct hashstring_hash : public std::unary_function<HashString, size_t> {
};

// Compare HashString pointers by dereferencing them.
struct hashstring_ptr_equal : public std::binary_function<const HashString*, const HashString*, bool> {
struct hashstring_ptr_equal {
size_t operator () (const HashString* one, const HashString* two) const
{ return *one == *two; }
};
Expand Down
2 changes: 1 addition & 1 deletion src/dht/dht_tracker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ DhtTracker::prune(uint32_t maxAge) {
if (m_lastSeen[i] < minSeen) m_peers[i].peer.port = 0;

m_peers.erase(std::remove_if(m_peers.begin(), m_peers.end(), std::mem_fn(&BencodeAddress::empty)), m_peers.end());
m_lastSeen.erase(std::remove_if(m_lastSeen.begin(), m_lastSeen.end(), std::bind2nd(std::less<uint32_t>(), minSeen)), m_lastSeen.end());
m_lastSeen.erase(std::remove_if(m_lastSeen.begin(), m_lastSeen.end(), [minSeen](auto seen) { return seen < minSeen; }), m_lastSeen.end());

if (m_peers.size() != m_lastSeen.size())
throw internal_error("DhtTracker::prune did inconsistent peer pruning.");
Expand Down
2 changes: 1 addition & 1 deletion src/dht/dht_transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class DhtTransactionAnnouncePeer;


// Compare predicate for ID closeness.
struct dht_compare_closer : public std::binary_function<const DhtNode*, const DhtNode*, bool> {
struct dht_compare_closer {
dht_compare_closer(const HashString& target) : m_target(target) { }

bool operator () (const DhtNode* one, const DhtNode* two) const;
Expand Down
7 changes: 4 additions & 3 deletions src/manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,11 @@ Manager::receive_tick() {
// various limited resources, like sockets for handshakes, cycle the
// group in reverse order.
if (!m_downloadManager->empty()) {
DownloadManager::iterator split = m_downloadManager->end() - m_ticks % m_downloadManager->size() - 1;
auto split = m_downloadManager->end() - m_ticks % m_downloadManager->size() - 1;
auto f = [this](auto wrapper) { return wrapper->receive_tick(m_ticks); };

std::for_each(split, m_downloadManager->end(), std::bind2nd(std::mem_fn(&DownloadWrapper::receive_tick), m_ticks));
std::for_each(m_downloadManager->begin(), split, std::bind2nd(std::mem_fn(&DownloadWrapper::receive_tick), m_ticks));
std::for_each(split, m_downloadManager->end(), f);
std::for_each(m_downloadManager->begin(), split, f);
}

// If you change the interval, make sure the keepalives gets
Expand Down
4 changes: 3 additions & 1 deletion src/net/socket_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ SocketSet::_replace_with_last(size_type idx) {

void
SocketSet::prepare() {
std::for_each(m_erased.begin(), m_erased.end(), std::bind1st(std::mem_fn(&SocketSet::_replace_with_last), this));
for (auto& socket : m_erased) {
_replace_with_last(socket);
}

m_erased.clear();
}
Expand Down
5 changes: 2 additions & 3 deletions src/protocol/initial_seed.cc
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,8 @@ InitialSeeding::chunk_offer(PeerConnectionBase* pcb, uint32_t chunkDone) {

// Re-connection of a peer we already sent a chunk.
// Offer the same chunk again.
PeerInfo** peerChunksEnd = m_peerChunks + m_download->file_list()->size_chunks();
PeerInfo** itr = std::find_if(m_peerChunks, peerChunksEnd,
std::bind2nd(std::equal_to<PeerInfo*>(), peer));
auto peerChunksEnd = m_peerChunks + m_download->file_list()->size_chunks();
auto itr = std::find(m_peerChunks, peerChunksEnd, peer);
if (itr != peerChunksEnd)
return itr - m_peerChunks;

Expand Down
26 changes: 16 additions & 10 deletions src/torrent/data/block.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,14 @@ Block::~Block() {
m_leader = NULL;
m_state = STATE_INVALID;

std::for_each(m_queued.begin(), m_queued.end(), std::bind1st(std::mem_fn(&Block::invalidate_transfer), this));
for (auto& block : m_queued) {
invalidate_transfer(block);
}
m_queued.clear();

std::for_each(m_transfers.begin(), m_transfers.end(), std::bind1st(std::mem_fn(&Block::invalidate_transfer), this));
for (auto& block : m_transfers) {
invalidate_transfer(block);
}
m_transfers.clear();

if (m_notStalled != 0)
Expand Down Expand Up @@ -137,10 +141,10 @@ Block::erase(BlockTransfer* transfer) {
// Create a range containing transfers with
// is_not_leader(). Erased transfer will end up in the back.

transfer_list_type::iterator first = std::find_if(m_transfers.begin(), m_transfers.end(), std::not1(std::mem_fn(&BlockTransfer::is_leader)));
transfer_list_type::iterator last = std::stable_partition(first, m_transfers.end(), std::mem_fn(&BlockTransfer::is_not_leader));
auto first = std::find_if_not(m_transfers.begin(), m_transfers.end(), std::mem_fn(&BlockTransfer::is_leader));
auto last = std::stable_partition(first, m_transfers.end(), std::mem_fn(&BlockTransfer::is_not_leader));

transfer_list_type::iterator new_leader = std::max_element(first, last, [](BlockTransfer* t1, BlockTransfer* t2) {
auto new_leader = std::max_element(first, last, [](BlockTransfer* t1, BlockTransfer* t2) {
return t1->position() < t2->position();
});

Expand Down Expand Up @@ -233,7 +237,9 @@ Block::completed(BlockTransfer* transfer) {
// Block::transfering(...). But that would propably not be correct
// as we want to trigger cancel messages from here, as hash fail is
// a rare occurrence.
std::for_each(m_queued.begin(), m_queued.end(), std::bind1st(std::mem_fn(&Block::invalidate_transfer), this));
for (const auto& block : m_queued) {
invalidate_transfer(block);
}
m_queued.clear();

// We need to invalidate those unfinished and keep the one that
Expand Down Expand Up @@ -366,17 +372,17 @@ Block::invalidate_transfer(BlockTransfer* transfer) {

void
Block::remove_erased_transfers() {
transfer_list_type::iterator split = std::stable_partition(m_transfers.begin(), m_transfers.end(), std::not1(std::mem_fn(&BlockTransfer::is_erased)));
auto split = std::stable_partition(m_transfers.begin(), m_transfers.end(), [](auto block) { return !block->is_erased(); });

std::for_each(split, m_transfers.end(), std::bind1st(std::mem_fn(&Block::invalidate_transfer), this));
std::for_each(split, m_transfers.end(), [this](auto block) { invalidate_transfer(block); });
m_transfers.erase(split, m_transfers.end());
}

void
Block::remove_non_leader_transfers() {
transfer_list_type::iterator split = std::stable_partition(m_transfers.begin(), m_transfers.end(), std::mem_fn(&BlockTransfer::is_leader));
auto split = std::stable_partition(m_transfers.begin(), m_transfers.end(), std::mem_fn(&BlockTransfer::is_leader));

std::for_each(split, m_transfers.end(), std::bind1st(std::mem_fn(&Block::invalidate_transfer), this));
std::for_each(split, m_transfers.end(), [this](auto block) { invalidate_transfer(block); });
m_transfers.erase(split, m_transfers.end());
}

Expand Down
5 changes: 2 additions & 3 deletions src/torrent/torrent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ is_initialized() {

bool
is_inactive() {
return manager == NULL ||
std::find_if(manager->download_manager()->begin(), manager->download_manager()->end(), std::not1(std::mem_fn(&DownloadWrapper::is_stopped))) == manager->download_manager()->end();
return !manager || std::all_of(manager->download_manager()->begin(), manager->download_manager()->end(), std::mem_fn(&DownloadWrapper::is_stopped));
}

thread_base*
Expand Down Expand Up @@ -133,7 +132,7 @@ encoding_list() {

Download
download_add(Object* object) {
std::auto_ptr<DownloadWrapper> download(new DownloadWrapper);
auto download = std::make_unique<DownloadWrapper>();

DownloadConstructor ctor;
ctor.set_download(download.get());
Expand Down
2 changes: 1 addition & 1 deletion src/torrent/tracker_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ TrackerList::find_usable(const_iterator itr) const {

TrackerList::iterator
TrackerList::find_next_to_request(iterator itr) {
TrackerList::iterator preferred = itr = std::find_if(itr, end(), std::mem_fn(&Tracker::can_request_state));
auto preferred = itr = std::find_if(itr, end(), std::mem_fn(&Tracker::can_request_state));

if (preferred == end() || (*preferred)->failed_counter() == 0)
return preferred;
Expand Down
5 changes: 2 additions & 3 deletions src/torrent/utils/log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ const char log_level_char[] = { 'C', 'E', 'W', 'N', 'I', 'D' };

void
log_update_child_cache(int index) {
log_child_list::const_iterator first =
std::find_if(log_children.begin(), log_children.end(),
std::bind2nd(std::greater_equal<std::pair<int, int> >(), std::make_pair(index, 0)));
auto first =
std::find_if(log_children.begin(), log_children.end(), [index](const auto& pair) { return pair >= std::make_pair(index, 0); });

if (first == log_children.end())
return;
Expand Down

0 comments on commit 71871ad

Please sign in to comment.