From 5222d92e9f1de0844bb63676a890bec012d32652 Mon Sep 17 00:00:00 2001 From: arvidn Date: Wed, 26 Jul 2017 16:16:29 -0700 Subject: [PATCH] make resume_data_flags type safe --- bindings/python/src/converters.cpp | 2 + bindings/python/src/torrent_handle.cpp | 12 +++--- include/libtorrent/torrent.hpp | 4 +- include/libtorrent/torrent_handle.hpp | 51 ++++++++++++-------------- src/torrent.cpp | 6 +-- src/torrent_handle.cpp | 6 ++- 6 files changed, 43 insertions(+), 38 deletions(-) diff --git a/bindings/python/src/converters.cpp b/bindings/python/src/converters.cpp index 5e6646e67..5cea412c1 100644 --- a/bindings/python/src/converters.cpp +++ b/bindings/python/src/converters.cpp @@ -299,6 +299,7 @@ void bind_converters() to_python_converter>(); to_python_converter>(); to_python_converter>(); + to_python_converter>(); // work-around types to_python_converter, address_to_tuple< @@ -348,4 +349,5 @@ void bind_converters() to_bitfield_flag(); to_bitfield_flag(); to_bitfield_flag(); + to_bitfield_flag(); } diff --git a/bindings/python/src/torrent_handle.cpp b/bindings/python/src/torrent_handle.cpp index dc1e9ebb4..d1407ead5 100644 --- a/bindings/python/src/torrent_handle.cpp +++ b/bindings/python/src/torrent_handle.cpp @@ -428,6 +428,7 @@ void add_piece(torrent_handle& th, piece_index_t piece, char const *data, int fl } class dummy {}; +class dummy4 {}; using by_value = return_value_policy; void bind_torrent_handle() @@ -603,11 +604,12 @@ void bind_torrent_handle() .value("graceful_pause", torrent_handle::graceful_pause) ; - enum_("save_resume_flags_t") - .value("flush_disk_cache", torrent_handle::flush_disk_cache) - .value("save_info_dict", torrent_handle::save_info_dict) - .value("only_if_modified", torrent_handle::only_if_modified) - ; + { + scope s = class_("save_resume_flags_t"); + s.attr("flush_disk_cache") = torrent_handle::flush_disk_cache; + s.attr("save_info_dict") = torrent_handle::save_info_dict; + s.attr("only_if_modified") = torrent_handle::only_if_modified; + } enum_("deadline_flags") .value("alert_when_available", torrent_handle::alert_when_available) diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index cb15e9aed..22e193e11 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -529,7 +529,7 @@ namespace libtorrent { bool is_paused() const; bool is_torrent_paused() const { return m_paused; } void force_recheck(); - void save_resume_data(int flags); + void save_resume_data(resume_data_flags_t flags); bool need_save_resume_data() const { return m_need_save_resume_data; } @@ -1532,7 +1532,7 @@ namespace libtorrent { // these are the flags sent in on a call to save_resume_data // we need to save them to check them in write_resume_data - std::uint32_t m_save_resume_flags:8; + resume_data_flags_t m_save_resume_flags; // ---- diff --git a/include/libtorrent/torrent_handle.hpp b/include/libtorrent/torrent_handle.hpp index a59eecb5f..09481cf04 100644 --- a/include/libtorrent/torrent_handle.hpp +++ b/include/libtorrent/torrent_handle.hpp @@ -57,10 +57,12 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/torrent_flags.hpp" #include "libtorrent/peer_info.hpp" // for peer_source_flags_t -namespace libtorrent { namespace aux { +namespace libtorrent { +namespace aux { - struct session_impl; - } + struct session_impl; + +} class entry; struct open_file_state; @@ -80,6 +82,9 @@ namespace libtorrent { namespace aux { void TORRENT_NO_RETURN throw_invalid_handle(); #endif + struct resume_data_flags_tag; + using resume_data_flags_t = flags::bitfield_flag; + // holds the state of a block in a piece. Who we requested // it from and how far along we are at downloading it. struct TORRENT_EXPORT block_info @@ -611,35 +616,27 @@ namespace libtorrent { namespace aux { // will start connecting to peers again, as normal. void force_recheck() const; - // flags used in the save_resume_data call to control additional - // actions or fields to save. - enum save_resume_flags_t - { - // the disk cache will be flushed before creating the resume data. - // This avoids a problem with file timestamps in the resume data in - // case the cache hasn't been flushed yet. - flush_disk_cache = 1, + // the disk cache will be flushed before creating the resume data. + // This avoids a problem with file timestamps in the resume data in + // case the cache hasn't been flushed yet. + static constexpr resume_data_flags_t flush_disk_cache{1}; - // the resume data will contain the metadata from the torrent file as - // well. This is default for any torrent that's added without a - // torrent file (such as a magnet link or a URL). - save_info_dict = 2, + // the resume data will contain the metadata from the torrent file as + // well. This is default for any torrent that's added without a + // torrent file (such as a magnet link or a URL). + static constexpr resume_data_flags_t save_info_dict{2}; - // if nothing significant has changed in the torrent since the last - // time resume data was saved, fail this attempt. Significant changes - // primarily include more data having been downloaded, file or piece - // priorities having changed etc. If the resume data doesn't need - // saving, a save_resume_data_failed_alert is posted with the error - // resume_data_not_modified. - only_if_modified = 4 - }; + // if nothing significant has changed in the torrent since the last + // time resume data was saved, fail this attempt. Significant changes + // primarily include more data having been downloaded, file or piece + // priorities having changed etc. If the resume data doesn't need + // saving, a save_resume_data_failed_alert is posted with the error + // resume_data_not_modified. + static constexpr resume_data_flags_t only_if_modified{4}; // ``save_resume_data()`` asks libtorrent to generate fast-resume data for // this torrent. // - // The ``flags`` argument is a bitmask of flags ORed together. see - // save_resume_flags_t - // // This operation is asynchronous, ``save_resume_data`` will return // immediately. The resume data is delivered when it's done through an // save_resume_data_alert. @@ -760,7 +757,7 @@ namespace libtorrent { namespace aux { // the alert, but it has not been received yet. Those torrents would // report that they don't need to save resume data again, and skipped by // the initial loop, and thwart the counter otherwise. - void save_resume_data(int flags = 0) const; + void save_resume_data(resume_data_flags_t flags = {}) const; // This function returns true if any whole chunk has been downloaded // since the torrent was first loaded or since the last time the resume diff --git a/src/torrent.cpp b/src/torrent.cpp index d819c12c4..008b7d42e 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -194,7 +194,7 @@ namespace libtorrent { , m_stop_when_ready(p.flags & torrent_flags::stop_when_ready) , m_need_save_resume_data(p.flags & torrent_flags::need_save_resume) , m_max_uploads((1 << 24) - 1) - , m_save_resume_flags(0) + , m_save_resume_flags() , m_num_uploads(0) , m_need_connect_boost(true) , m_lsd_seq(0) @@ -8326,7 +8326,7 @@ namespace libtorrent { // TODO: add a flag to ignore stats, and only care about resume data for // content. For unchanged files, don't trigger a load of the metadata // just to save an empty resume data file - void torrent::save_resume_data(int const flags) + void torrent::save_resume_data(resume_data_flags_t const flags) { TORRENT_ASSERT(is_single_thread()); INVARIANT_CHECK; @@ -8346,7 +8346,7 @@ namespace libtorrent { } m_need_save_resume_data = false; - m_save_resume_flags = aux::numeric_cast(flags); + m_save_resume_flags = flags; state_updated(); if ((flags & torrent_handle::flush_disk_cache) && m_storage) diff --git a/src/torrent_handle.cpp b/src/torrent_handle.cpp index 1fbc9778c..19198e2c8 100644 --- a/src/torrent_handle.cpp +++ b/src/torrent_handle.cpp @@ -58,6 +58,10 @@ using libtorrent::aux::session_impl; namespace libtorrent { + constexpr resume_data_flags_t torrent_handle::flush_disk_cache; + constexpr resume_data_flags_t torrent_handle::save_info_dict; + constexpr resume_data_flags_t torrent_handle::only_if_modified; + #ifndef BOOST_NO_EXCEPTIONS void TORRENT_NO_RETURN throw_invalid_handle() { @@ -344,7 +348,7 @@ namespace libtorrent { #endif } - void torrent_handle::save_resume_data(int f) const + void torrent_handle::save_resume_data(resume_data_flags_t f) const { async_call(&torrent::save_resume_data, f); }