Skip to content

Commit

Permalink
Clean up rak::functional's last methods
Browse files Browse the repository at this point in the history
Also simplify slot_list_call, and perform only one pass for HashQueue::remove
  • Loading branch information
kannibalox committed Dec 13, 2024
1 parent 6705dd2 commit f63dc0a
Show file tree
Hide file tree
Showing 25 changed files with 77 additions and 245 deletions.
177 changes: 6 additions & 171 deletions rak/functional.h
Original file line number Diff line number Diff line change
@@ -1,183 +1,18 @@
// 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 <[email protected]>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY

#ifndef RAK_FUNCTIONAL_H
#define RAK_FUNCTIONAL_H

#include <cstddef>
#include <functional>
#include <utility>

namespace rak {

template <typename Type>
struct reference_fix {
typedef Type type;
};

template <typename Type>
struct reference_fix<Type&> {
typedef Type type;
};

// Operators:

template <typename Type, typename Ftor>
struct equal_t {
typedef bool result_type;

equal_t(Type t, Ftor f) : m_t(t), m_f(f) {}

template <typename Arg>
bool operator () (Arg& a) {
return m_t == m_f(a);
}

Type m_t;
Ftor m_f;
};

template <typename Type, typename Ftor>
inline equal_t<Type, Ftor>
equal(Type t, Ftor f) {
return equal_t<Type, Ftor>(t, f);
}

template <typename Type, typename Ftor>
struct less_t {
typedef bool result_type;

less_t(Type t, Ftor f) : m_t(t), m_f(f) {}

template <typename Arg>
bool operator () (Arg& a) {
return m_t < m_f(a);
}

Type m_t;
Ftor m_f;
};

template <typename Type, typename Ftor>
inline less_t<Type, Ftor>
less(Type t, Ftor f) {
return less_t<Type, Ftor>(t, f);
}

template <typename Type, typename Ftor>
struct less_equal_t {
typedef bool result_type;

less_equal_t(Type t, Ftor f) : m_t(t), m_f(f) {}

template <typename Arg>
bool operator () (Arg& a) {
return m_t <= m_f(a);
}

Type m_t;
Ftor m_f;
};

template <typename Type, typename Ftor>
inline less_equal_t<Type, Ftor>
less_equal(Type t, Ftor f) {
return less_equal_t<Type, Ftor>(t, f);
}

template <typename Src, typename Dest>
struct on_t : public std::unary_function<typename Src::argument_type, typename Dest::result_type> {
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<typename Src::argument_type>::type arg) {
return m_dest(m_src(arg));
}

Dest m_dest;
Src m_src;
};

template <typename Src, typename Dest>
inline on_t<Src, Dest>
on(Src s, Dest d) {
return on_t<Src, Dest>(s, d);
}

template <typename T>
struct call_delete : public std::unary_function<T*, void> {
void operator () (T* t) {
delete t;
}
};

template <typename Container>
template <typename Container, typename... Args>
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 <typename Container, typename Arg1>
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;
slot_list_call(const Container& slot_list, Args&&... args) {
for (const auto& slot : slot_list) {
slot(std::forward<Args>(args)...);
}

(*first)(arg1);
}

}
} // namespace rak

#endif
1 change: 0 additions & 1 deletion src/data/chunk_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#include "config.h"

#include <rak/error_number.h>
#include <rak/functional.h>

#include "torrent/exceptions.h"
#include "torrent/chunk_manager.h"
Expand Down
19 changes: 10 additions & 9 deletions src/data/hash_queue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#include "config.h"

#include <functional>
#include <rak/functional.h>
#include <unistd.h>

#include "torrent/exceptions.h"
Expand Down Expand Up @@ -106,7 +105,7 @@ HashQueue::push_back(ChunkHandle handle, HashQueueNode::id_type id, slot_done_ty

bool
HashQueue::has(HashQueueNode::id_type id) {
return std::find_if(begin(), end(), rak::equal(id, std::mem_fun_ref(&HashQueueNode::id))) != end();
return std::any_of(begin(), end(), [id](const auto& n) { return id == n.id(); });
}

bool
Expand All @@ -116,10 +115,11 @@ HashQueue::has(HashQueueNode::id_type id, uint32_t index) {

void
HashQueue::remove(HashQueueNode::id_type id) {
iterator itr = begin();
base_type::erase(std::remove_if(begin(), end(), [id, this](auto& itr) {
if (itr.id() != id)
return false;

while ((itr = std::find_if(itr, end(), rak::equal(id, std::mem_fun_ref(&HashQueueNode::id)))) != end()) {
HashChunk *hash_chunk = itr->get_chunk();
HashChunk *hash_chunk = itr.get_chunk();

LT_LOG_DATA(id, DEBUG, "Removing index:%" PRIu32 " from queue.", hash_chunk->handle().index());

Expand All @@ -143,11 +143,12 @@ HashQueue::remove(HashQueueNode::id_type id) {
pthread_mutex_unlock(&m_done_chunks_lock);
}

itr->slot_done()(*hash_chunk->chunk(), NULL);
itr->clear();
itr.slot_done()(*hash_chunk->chunk(), NULL);
itr.clear();

return true;
}), end());

itr = erase(itr);
}
}

void
Expand Down
1 change: 0 additions & 1 deletion src/dht/dht_router.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
#include "globals.h"

#include <sstream>
#include <rak/functional.h>

#include "torrent/dht_manager.h"
#include "torrent/download_info.h"
Expand Down
9 changes: 6 additions & 3 deletions src/dht/dht_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@

#include <algorithm>
#include <cstdio>
#include <rak/functional.h>

#include "torrent/exceptions.h"
#include "torrent/connection_manager.h"
Expand Down Expand Up @@ -132,8 +131,12 @@ DhtServer::DhtServer(DhtRouter* router) :
DhtServer::~DhtServer() {
stop();

std::for_each(m_highQueue.begin(), m_highQueue.end(), rak::call_delete<DhtTransactionPacket>());
std::for_each(m_lowQueue.begin(), m_lowQueue.end(), rak::call_delete<DhtTransactionPacket>());
for (const auto& packet : m_highQueue) {
delete packet;
}
for (const auto& packet : m_lowQueue) {
delete packet;
}

manager->connection_manager()->dec_socket_count();
}
Expand Down
1 change: 0 additions & 1 deletion src/download/chunk_selector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@

#include <algorithm>
#include <stdlib.h>
#include <rak/functional.h>

#include "protocol/peer_chunks.h"
#include "torrent/exceptions.h"
Expand Down
5 changes: 3 additions & 2 deletions src/download/download_wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,9 @@ DownloadWrapper::receive_update_priorities() {

m_main->chunk_selector()->update_priorities();

std::for_each(m_main->connection_list()->begin(), m_main->connection_list()->end(),
rak::on(std::mem_fun(&Peer::m_ptr), std::mem_fun(&PeerConnectionBase::update_interested)));
for (const auto& peer : *m_main->connection_list()) {
peer->m_ptr()->update_interested();
}

// The 'partially_done/restarted' signal only gets triggered when a
// download is active and not completed.
Expand Down
5 changes: 3 additions & 2 deletions src/net/throttle_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@

#include "config.h"

#include <rak/functional.h>
#include <rak/timer.h>
#include <rak/priority_queue_default.h>

Expand Down Expand Up @@ -69,7 +68,9 @@ ThrottleInternal::~ThrottleInternal() {
if (is_root())
priority_queue_erase(&taskScheduler, &m_taskTick);

std::for_each(m_slaveList.begin(), m_slaveList.end(), rak::call_delete<ThrottleInternal>());
for (const auto& t : m_slaveList) {
delete t;
}
}

void
Expand Down
2 changes: 1 addition & 1 deletion src/protocol/handshake_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ handshake_manager_delete_handshake(Handshake* h) {

HandshakeManager::size_type
HandshakeManager::size_info(DownloadMain* info) const {
return std::count_if(base_type::begin(), base_type::end(), rak::equal(info, std::mem_fun(&Handshake::download)));
return std::count_if(base_type::begin(), base_type::end(), [info](Handshake* h) { return info == h->download(); });
}

void
Expand Down
1 change: 0 additions & 1 deletion src/protocol/peer_connection_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
#include <cstdio>
#include <fcntl.h>
#include <rak/error_number.h>
#include <rak/functional.h>
#include <rak/string_manip.h>

#include "data/chunk_iterator.h"
Expand Down
1 change: 0 additions & 1 deletion src/protocol/peer_connection_leech.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@

#include <cstring>
#include <sstream>
#include <rak/functional.h>
#include <rak/string_manip.h>

#include "data/chunk_list_node.h"
Expand Down
1 change: 0 additions & 1 deletion src/protocol/peer_connection_metadata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
#include "torrent/download/choke_queue.h"
#include "torrent/peer/connection_list.h"
#include "torrent/peer/peer_info.h"
#include "rak/functional.h"
#include "torrent/utils/log.h"

#include "extensions.h"
Expand Down
1 change: 0 additions & 1 deletion src/protocol/request_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <algorithm>
#include <functional>
#include <cinttypes>
#include <rak/functional.h>

#include "torrent/data/block.h"
#include "torrent/data/block_list.h"
Expand Down
Loading

0 comments on commit f63dc0a

Please sign in to comment.