introduced alert_cast

This commit is contained in:
Arvid Norberg 2009-11-02 03:34:16 +00:00
parent 67c8eace2c
commit fade413665
3 changed files with 31 additions and 11 deletions

View File

@ -2663,14 +2663,14 @@ Example code to pause and save resume data for all torrents and wait for the ale
std::auto_ptr<alert> holder = ses.pop_alert(); std::auto_ptr<alert> holder = ses.pop_alert();
if (dynamic_cast<save_resume_data_failed_alert const*>(a)) if (alert_cast<save_resume_data_failed_alert>(a))
{ {
process_alert(a); process_alert(a);
--num_resume_data; --num_resume_data;
continue; continue;
} }
save_resume_data_alert const* rd = dynamic_cast<save_resume_data_alert const*>(a); save_resume_data_alert const* rd = alert_cast<save_resume_data_alert>(a);
if (rd == 0) if (rd == 0)
{ {
process_alert(a); 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 alerts that belong to an enabled category are posted. Setting the alert bitmask to 0 will disable
all alerts all alerts
When you get an alert, you can use ``typeid()`` or ``dynamic_cast<>`` to get more detailed When you get an alert, you can use ``alert_cast<>`` to attempt to cast the pointer to a
information on exactly which type it is. i.e. what kind of error it is. You can also use a more specific alert type, to be queried for more information about the alert. ``alert_cast``
dispatcher_ mechanism that's available in libtorrent. has the followinf signature::
template <T> T* alert_cast(alert* a);
template <T> 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 ``<libtorrent/alert_types.hpp>`` header file. All alert types are defined in the ``<libtorrent/alert_types.hpp>`` header file.

View File

@ -571,10 +571,12 @@ void scan_dir(std::string const& dir_path
using namespace libtorrent; using namespace libtorrent;
printf("scanning dir: %s\n", dir_path.c_str());
error_code ec; error_code ec;
for (directory i(dir_path, ec); !i.done(); i.next(ec)) for (directory i(dir_path, ec); !i.done(); i.next(ec))
{ {
std::string file = i.file(); std::string file = i.file();
printf(" found: %s\n", file.c_str());
if (extension(file) != ".torrent") continue; if (extension(file) != ".torrent") continue;
handles_t::iterator k = handles.find(file); handles_t::iterator k = handles.find(file);
@ -673,7 +675,7 @@ void handle_alert(libtorrent::session& ses, libtorrent::alert* a
{ {
using namespace libtorrent; using namespace libtorrent;
if (torrent_finished_alert* p = dynamic_cast<torrent_finished_alert*>(a)) if (torrent_finished_alert* p = alert_cast<torrent_finished_alert>(a))
{ {
p->handle.set_max_connections(30); p->handle.set_max_connections(30);
@ -683,7 +685,7 @@ void handle_alert(libtorrent::session& ses, libtorrent::alert* a
torrent_handle h = p->handle; torrent_handle h = p->handle;
h.save_resume_data(); h.save_resume_data();
} }
else if (save_resume_data_alert* p = dynamic_cast<save_resume_data_alert*>(a)) else if (save_resume_data_alert* p = alert_cast<save_resume_data_alert>(a))
{ {
torrent_handle h = p->handle; torrent_handle h = p->handle;
TORRENT_ASSERT(p->resume_data); TORRENT_ASSERT(p->resume_data);
@ -697,7 +699,7 @@ void handle_alert(libtorrent::session& ses, libtorrent::alert* a
ses.remove_torrent(h); ses.remove_torrent(h);
} }
} }
else if (save_resume_data_failed_alert* p = dynamic_cast<save_resume_data_failed_alert*>(a)) else if (save_resume_data_failed_alert* p = alert_cast<save_resume_data_failed_alert>(a))
{ {
torrent_handle h = p->handle; torrent_handle h = p->handle;
if (std::find_if(handles.begin(), handles.end() 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 // be able to remove torrents that were added via the directory
// monitor when they're not in the directory anymore. // monitor when they're not in the directory anymore.
handles_t handles; 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 , session::add_default_plugins
, alert::all_categories , alert::all_categories
& ~(alert::dht_notification & ~(alert::dht_notification
@ -1071,13 +1073,13 @@ int main(int argc, char* argv[])
::print_alert(holder.get(), log); ::print_alert(holder.get(), log);
printf("%s\n", log.c_str()); printf("%s\n", log.c_str());
if (dynamic_cast<save_resume_data_failed_alert const*>(a)) if (alert_cast<save_resume_data_failed_alert>(a))
{ {
--num_resume_data; --num_resume_data;
continue; continue;
} }
save_resume_data_alert const* rd = dynamic_cast<save_resume_data_alert const*>(a); save_resume_data_alert const* rd = alert_cast<save_resume_data_alert>(a);
if (!rd) continue; if (!rd) continue;
--num_resume_data; --num_resume_data;

View File

@ -213,6 +213,19 @@ namespace libtorrent {
#endif // BOOST_NO_TYPEID #endif // BOOST_NO_TYPEID
template <class T>
T* alert_cast(alert* a)
{
if (a->type() == T::alert_type) return static_cast<T*>(a);
return 0;
}
template <class T>
T const* alert_cast(alert const* a)
{
if (a->type() == T::alert_type) return static_cast<T const*>(a);
return 0;
}
} // namespace libtorrent } // namespace libtorrent