From bc8c49dc15e6f26baf9ba23381aa44d787dfaad0 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sun, 14 Aug 2016 13:11:59 -0400 Subject: [PATCH] make file_status return the vector instead of taking an out-parameter (#1000) make file_status return the vector instead of taking an out-parameter --- examples/client_test.cpp | 3 +-- include/libtorrent/file_pool.hpp | 10 +++++----- include/libtorrent/torrent_handle.hpp | 13 ++++++++----- src/file_pool.cpp | 26 +++++++++++++------------- src/torrent_handle.cpp | 18 +++++++++++++----- 5 files changed, 40 insertions(+), 30 deletions(-) diff --git a/examples/client_test.cpp b/examples/client_test.cpp index be44fb4b2..2e32afac2 100644 --- a/examples/client_test.cpp +++ b/examples/client_test.cpp @@ -2021,8 +2021,7 @@ int main(int argc, char* argv[]) { std::vector file_progress; h.file_progress(file_progress); - std::vector file_status; - h.file_status(file_status); + std::vector file_status = h.file_status(); std::vector file_prio = h.file_priorities(); std::vector::iterator f = file_status.begin(); boost::shared_ptr ti = h.torrent_file(); diff --git a/include/libtorrent/file_pool.hpp b/include/libtorrent/file_pool.hpp index 153862184..85c2cd723 100644 --- a/include/libtorrent/file_pool.hpp +++ b/include/libtorrent/file_pool.hpp @@ -48,9 +48,6 @@ namespace libtorrent // file list of this torrent. This starts indexing at 0. int file_index; - // a (high precision) timestamp of when the file was last used. - time_point last_use; - // ``open_mode`` is a bitmask of the file flags this file is currently opened with. These // are the flags used in the ``file::open()`` function. This enum is defined as a member // of the ``file`` class. @@ -73,6 +70,9 @@ namespace libtorrent // Note that the read/write mode is not a bitmask. The two least significant bits are used // to represent the read/write mode. Those bits can be masked out using the ``rw_mask`` constant. int open_mode; + + // a (high precision) timestamp of when the file was last used. + time_point last_use; }; // this is an internal cache of open file handles. It's primarily used by @@ -107,7 +107,7 @@ namespace libtorrent // internal void set_low_prio_io(bool b) { m_low_prio_io = b; } - void get_status(std::vector* files, void* st) const; + std::vector get_status(void* st) const; #if TORRENT_USE_ASSERTS bool assert_idle_files(void* st) const; @@ -136,7 +136,7 @@ namespace libtorrent // maps storage pointer, file index pairs to the // lru entry for the file - typedef std::map, lru_file_entry> file_set; + using file_set = std::map, lru_file_entry>; file_set m_files; #if TORRENT_USE_ASSERTS diff --git a/include/libtorrent/torrent_handle.hpp b/include/libtorrent/torrent_handle.hpp index 4a93c56f1..96e7eb9f8 100644 --- a/include/libtorrent/torrent_handle.hpp +++ b/include/libtorrent/torrent_handle.hpp @@ -404,7 +404,7 @@ namespace libtorrent // every interested peer on a high priority torrent will be unchoked // before any other, lower priority, torrents have any peers unchoked. void set_priority(int prio) const; - + #ifndef TORRENT_NO_DEPRECATE #if !TORRENT_NO_FPU // fills the specified vector with the download progress [0, 1] @@ -413,6 +413,9 @@ namespace libtorrent TORRENT_DEPRECATED void file_progress(std::vector& progress) const; #endif + + TORRENT_DEPRECATED + void file_status(std::vector& status) const; #endif // flags to be passed in file_progress(). @@ -442,14 +445,14 @@ namespace libtorrent // already keeps track of this internally and no calculation is required. void file_progress(std::vector& progress, int flags = 0) const; - // This function fills in the passed in vector with status about files - // that are open for this torrent. Any file that is not open in this - // torrent, will not be reported in the vector, i.e. it's possible that + // This function returns a vector with status about files + // that are open for this torrent. Any file that is not open + // will not be reported in the vector, i.e. it's possible that // the vector is empty when returning, if none of the files in the // torrent are currently open. // // see pool_file_status. - void file_status(std::vector& status) const; + std::vector file_status() const; // If the torrent is in an error state (i.e. ``torrent_status::error`` is // non-empty), this will clear the error and start the torrent again. diff --git a/src/file_pool.cpp b/src/file_pool.cpp index b07572799..918ffedca 100644 --- a/src/file_pool.cpp +++ b/src/file_pool.cpp @@ -221,22 +221,22 @@ namespace libtorrent return file_ptr; } - void file_pool::get_status(std::vector* files, void* st) const + std::vector file_pool::get_status(void* st) const { - std::unique_lock l(m_mutex); - - file_set::const_iterator start = m_files.lower_bound(std::make_pair(st, 0)); - file_set::const_iterator end = m_files.upper_bound(std::make_pair(st - , std::numeric_limits::max())); - - for (file_set::const_iterator i = start; i != end; ++i) + std::vector ret; { - pool_file_status s; - s.file_index = i->first.second; - s.open_mode = i->second.mode; - s.last_use = i->second.last_use; - files->push_back(s); + std::unique_lock l(m_mutex); + + auto start = m_files.lower_bound(std::make_pair(st, 0)); + auto end = m_files.upper_bound(std::make_pair(st + , std::numeric_limits::max())); + + for (file_set::const_iterator i = start; i != end; ++i) + { + ret.push_back({i->first.second, i->second.mode, i->second.last_use}); + } } + return ret; } void file_pool::remove_oldest(std::unique_lock& l) diff --git a/src/torrent_handle.cpp b/src/torrent_handle.cpp index d7ab77960..90e1c57b7 100644 --- a/src/torrent_handle.cpp +++ b/src/torrent_handle.cpp @@ -648,6 +648,16 @@ namespace libtorrent async_call(&torrent::force_tracker_request, aux::time_now() + seconds(duration.total_seconds()), -1); } + + void torrent_handle::file_status(std::vector& status) const + { + status.clear(); + + boost::shared_ptr t = m_torrent.lock(); + if (!t || !t->has_storage()) return; + session_impl& ses = static_cast(t->session()); + status = ses.disk_thread().files().get_status(&t->storage()); + } #endif void torrent_handle::force_dht_announce() const @@ -662,14 +672,12 @@ namespace libtorrent async_call(&torrent::force_tracker_request, aux::time_now() + seconds(s), idx); } - void torrent_handle::file_status(std::vector& status) const + std::vector torrent_handle::file_status() const { - status.clear(); - boost::shared_ptr t = m_torrent.lock(); - if (!t || !t->has_storage()) return; + if (!t || !t->has_storage()) return {}; session_impl& ses = static_cast(t->session()); - ses.disk_thread().files().get_status(&status, &t->storage()); + return ses.disk_thread().files().get_status(&t->storage()); } void torrent_handle::scrape_tracker(int idx) const