From fade413665ab8438dba7639314aed4829dfc08c0 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Mon, 2 Nov 2009 03:34:16 +0000 Subject: [PATCH] introduced alert_cast --- docs/manual.rst | 15 ++++++++++----- examples/client_test.cpp | 14 ++++++++------ include/libtorrent/alert.hpp | 13 +++++++++++++ 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/docs/manual.rst b/docs/manual.rst index 1d9994fed..a8b4ba02a 100644 --- a/docs/manual.rst +++ b/docs/manual.rst @@ -2663,14 +2663,14 @@ Example code to pause and save resume data for all torrents and wait for the ale std::auto_ptr holder = ses.pop_alert(); - if (dynamic_cast(a)) + if (alert_cast(a)) { process_alert(a); --num_resume_data; continue; } - save_resume_data_alert const* rd = dynamic_cast(a); + save_resume_data_alert const* rd = alert_cast(a); if (rd == 0) { process_alert(a); @@ -4687,9 +4687,14 @@ Every alert belongs to one or more category. There is a small cost involved in p alerts that belong to an enabled category are posted. Setting the alert bitmask to 0 will disable all alerts -When you get an alert, you can use ``typeid()`` or ``dynamic_cast<>`` to get more detailed -information on exactly which type it is. i.e. what kind of error it is. You can also use a -dispatcher_ mechanism that's available in libtorrent. +When you get an alert, you can use ``alert_cast<>`` to attempt to cast the pointer to a +more specific alert type, to be queried for more information about the alert. ``alert_cast`` +has the followinf signature:: + + template T* alert_cast(alert* a); + template T const* alert_cast(alert const* a); + +You can also use a dispatcher_ mechanism that's available in libtorrent. All alert types are defined in the ```` header file. diff --git a/examples/client_test.cpp b/examples/client_test.cpp index f8649f614..d5a2beb92 100644 --- a/examples/client_test.cpp +++ b/examples/client_test.cpp @@ -571,10 +571,12 @@ void scan_dir(std::string const& dir_path using namespace libtorrent; + printf("scanning dir: %s\n", dir_path.c_str()); error_code ec; for (directory i(dir_path, ec); !i.done(); i.next(ec)) { std::string file = i.file(); + printf(" found: %s\n", file.c_str()); if (extension(file) != ".torrent") continue; handles_t::iterator k = handles.find(file); @@ -673,7 +675,7 @@ void handle_alert(libtorrent::session& ses, libtorrent::alert* a { using namespace libtorrent; - if (torrent_finished_alert* p = dynamic_cast(a)) + if (torrent_finished_alert* p = alert_cast(a)) { p->handle.set_max_connections(30); @@ -683,7 +685,7 @@ void handle_alert(libtorrent::session& ses, libtorrent::alert* a torrent_handle h = p->handle; h.save_resume_data(); } - else if (save_resume_data_alert* p = dynamic_cast(a)) + else if (save_resume_data_alert* p = alert_cast(a)) { torrent_handle h = p->handle; TORRENT_ASSERT(p->resume_data); @@ -697,7 +699,7 @@ void handle_alert(libtorrent::session& ses, libtorrent::alert* a ses.remove_torrent(h); } } - else if (save_resume_data_failed_alert* p = dynamic_cast(a)) + else if (save_resume_data_failed_alert* p = alert_cast(a)) { torrent_handle h = p->handle; if (std::find_if(handles.begin(), handles.end() @@ -770,7 +772,7 @@ int main(int argc, char* argv[]) // be able to remove torrents that were added via the directory // monitor when they're not in the directory anymore. handles_t handles; - session ses(fingerprint("LT", LIBTORRENT_VERSION_MAJOR, LIBTORRENT_VERSION_MINOR, 0, 0) + session ses(fingerprint("LT", LIBTORRENT_VERSION_MAJOR, LIBTORRENT_VERSION_MINOR-1, 0, 0) , session::add_default_plugins , alert::all_categories & ~(alert::dht_notification @@ -1071,13 +1073,13 @@ int main(int argc, char* argv[]) ::print_alert(holder.get(), log); printf("%s\n", log.c_str()); - if (dynamic_cast(a)) + if (alert_cast(a)) { --num_resume_data; continue; } - save_resume_data_alert const* rd = dynamic_cast(a); + save_resume_data_alert const* rd = alert_cast(a); if (!rd) continue; --num_resume_data; diff --git a/include/libtorrent/alert.hpp b/include/libtorrent/alert.hpp index 9bc259c06..845f31bfe 100644 --- a/include/libtorrent/alert.hpp +++ b/include/libtorrent/alert.hpp @@ -213,6 +213,19 @@ namespace libtorrent { #endif // BOOST_NO_TYPEID +template +T* alert_cast(alert* a) +{ + if (a->type() == T::alert_type) return static_cast(a); + return 0; +} + +template +T const* alert_cast(alert const* a) +{ + if (a->type() == T::alert_type) return static_cast(a); + return 0; +} } // namespace libtorrent