Skip to content

Commit

Permalink
extend use of the templatized handler wrapper into the torrent class …
Browse files Browse the repository at this point in the history
…as well
  • Loading branch information
arvidn committed Oct 28, 2019
1 parent c5a8bc5 commit 2d5a028
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 11 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ elseif(CMAKE_CXX_COMPILER_ID MATCHES GNU)
-Wvla
-Wc++11-compat
-Wno-format-zero-length
-Wno-noexcept-type
-ftemplate-depth=512)
elseif(MSVC)
add_compile_options(
Expand Down
1 change: 1 addition & 0 deletions Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ rule warnings ( properties * )
result += <cflags>-Wvla ;
result += <cxxflags>-Wc++11-compat ;
result += <cflags>-Wno-format-zero-length ;
result += <cflags>-Wno-noexcept-type ;
}

if <toolset>msvc in $(properties)
Expand Down
9 changes: 6 additions & 3 deletions include/libtorrent/aux_/allocating_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ namespace libtorrent { namespace aux {
{}

template <class... A>
void operator()(A&&... a) const
void operator()(A&&... a)
{
#ifdef BOOST_NO_EXCEPTIONS
handler(std::forward<A>(a)...);
Expand Down Expand Up @@ -285,7 +285,10 @@ namespace libtorrent { namespace aux {
std::forward<Handler>(handler), &storage, &err_handler);
}

template <typename T, void (T::*Handler)(error_code const&, std::size_t)
// TODO: in C++17, Handler and Storage could just use "auto"
template <typename T
, typename HandlerType
, HandlerType Handler
, void (T::*ErrorHandler)(error_code const&)
, void (T::*ExceptHandler)(std::exception const&)
, typename StorageType
Expand Down Expand Up @@ -321,8 +324,8 @@ namespace libtorrent { namespace aux {
std::runtime_error e("unknown exception");
(ptr_.get()->*ExceptHandler)(e);
}
}
#endif
}

using allocator_type = handler_allocator<handler, StorageType::size, StorageType::name>;

Expand Down
5 changes: 2 additions & 3 deletions include/libtorrent/torrent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,6 @@ namespace libtorrent {
, private torrent_hot_members
, public request_callback
, public peer_class_set
, public aux::error_handler_interface
, public std::enable_shared_from_this<torrent>
{
public:
Expand Down Expand Up @@ -1186,8 +1185,8 @@ namespace libtorrent {

private:

void on_exception(std::exception const& e) override;
void on_error(error_code const& ec) override;
void on_exception(std::exception const& e);
void on_error(error_code const& ec);

// trigger deferred disconnection of peers
void on_remove_peers() noexcept;
Expand Down
2 changes: 2 additions & 0 deletions src/peer_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5802,6 +5802,7 @@ namespace libtorrent {

using write_handler_type = aux::handler<
peer_connection
, decltype(&peer_connection::on_send_data)
, &peer_connection::on_send_data
, &peer_connection::on_error
, &peer_connection::on_exception
Expand Down Expand Up @@ -5895,6 +5896,7 @@ namespace libtorrent {

using read_handler_type = aux::handler<
peer_connection
, decltype(&peer_connection::on_receive_data)
, &peer_connection::on_receive_data
, &peer_connection::on_error
, &peer_connection::on_exception
Expand Down
18 changes: 13 additions & 5 deletions src/torrent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5606,11 +5606,19 @@ bool is_downloading_state(int const st)
TORRENT_ASSERT_VAL(m_peers_to_disconnect.capacity() > m_peers_to_disconnect.size()
, m_peers_to_disconnect.capacity());
m_peers_to_disconnect.push_back(p);
m_deferred_disconnect.post_deferred(m_ses.get_context(), aux::make_handler([=]()
{
std::shared_ptr<torrent> t = weak_t.lock();
if (t) t->on_remove_peers();
}, m_deferred_handler_storage, *this));

using deferred_handler_type = aux::handler<
torrent
, decltype(&torrent::on_remove_peers)
, &torrent::on_remove_peers
, &torrent::on_error
, &torrent::on_exception
, decltype(m_deferred_handler_storage)
, &torrent::m_deferred_handler_storage
>;
static_assert(sizeof(deferred_handler_type) == sizeof(std::shared_ptr<peer_connection>)
, "deferred handler does not have the expected size");
m_deferred_disconnect.post_deferred(m_ses.get_context(), deferred_handler_type(shared_from_this()));
}
else
{
Expand Down

0 comments on commit 2d5a028

Please sign in to comment.