From 660cdaf2d1a6aa2215a3097f5cf3a03549a604fa Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sat, 27 May 2017 20:33:31 -0400 Subject: [PATCH] deprecate direct access to array in session_stats_alert (#2033) --- bindings/python/src/alert.cpp | 6 +++--- examples/client_test.cpp | 2 +- examples/session_view.cpp | 8 +++++--- examples/session_view.hpp | 4 ++-- include/libtorrent/alert_types.hpp | 20 ++++++++++++++++++++ src/alert.cpp | 7 ++++++- test/setup_transfer.cpp | 2 +- 7 files changed, 38 insertions(+), 11 deletions(-) diff --git a/bindings/python/src/alert.cpp b/bindings/python/src/alert.cpp index bf2df6414..18f002b3b 100644 --- a/bindings/python/src/alert.cpp +++ b/bindings/python/src/alert.cpp @@ -123,11 +123,11 @@ dict session_stats_values(session_stats_alert const& alert) { std::vector map = session_stats_metrics(); dict d; + auto counters = alert.counters(); - for (std::vector::const_iterator i = map.begin(); - i != map.end(); ++i) + for (stats_metric const& m : map) { - d[i->name] = alert.values[i->value_index]; + d[m.name] = counters[m.value_index]; } return d; } diff --git a/examples/client_test.cpp b/examples/client_test.cpp index 3334c006b..d51931af2 100644 --- a/examples/client_test.cpp +++ b/examples/client_test.cpp @@ -764,7 +764,7 @@ bool handle_alert(torrent_view& view, session_view& ses_view if (session_stats_alert* s = alert_cast(a)) { - ses_view.update_counters(s->values.data(), int(s->values.size()) + ses_view.update_counters(s->counters() , duration_cast(s->timestamp().time_since_epoch()).count()); return true; } diff --git a/examples/session_view.cpp b/examples/session_view.cpp index b8a3c576d..6f06a4d5d 100644 --- a/examples/session_view.cpp +++ b/examples/session_view.cpp @@ -37,6 +37,8 @@ POSSIBILITY OF SUCH DAMAGE. #include // for std::max +using libtorrent::span; + session_view::session_view() : m_position(0) , m_print_utp_stats(false) @@ -190,8 +192,8 @@ void session_view::render() } } -void session_view::update_counters(std::int64_t const* stats_counters - , int num_cnt, std::uint64_t t) +void session_view::update_counters(span stats_counters + , std::uint64_t const t) { // only update the previous counters if there's been enough // time since it was last updated @@ -201,7 +203,7 @@ void session_view::update_counters(std::int64_t const* stats_counters m_timestamp[1] = m_timestamp[0]; } - m_cnt[0].assign(stats_counters, stats_counters + num_cnt); + m_cnt[0].assign(stats_counters.begin(), stats_counters.end()); m_timestamp[0] = t; render(); } diff --git a/examples/session_view.hpp b/examples/session_view.hpp index 34e5dae69..4c725564e 100644 --- a/examples/session_view.hpp +++ b/examples/session_view.hpp @@ -37,6 +37,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include "libtorrent/session_stats.hpp" +#include "libtorrent/span.hpp" struct session_view { @@ -53,8 +54,7 @@ struct session_view void print_utp_stats(bool p) { m_print_utp_stats = p; } bool print_utp_stats() const { return m_print_utp_stats; } - void update_counters(std::int64_t const* stats_counters, int num_cnt - , std::uint64_t t); + void update_counters(lt::span stats_counters, std::uint64_t t); private: diff --git a/include/libtorrent/alert_types.hpp b/include/libtorrent/alert_types.hpp index 00eb1fcd9..2f707b82d 100644 --- a/include/libtorrent/alert_types.hpp +++ b/include/libtorrent/alert_types.hpp @@ -1888,7 +1888,19 @@ namespace libtorrent { struct TORRENT_EXPORT session_stats_alert final : alert { session_stats_alert(aux::stack_allocator& alloc, counters const& cnt); + +#ifndef TORRENT_NO_DEPRECATE +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif +#endif TORRENT_DEFINE_ALERT_PRIO(session_stats_alert, 70) +#ifndef TORRENT_NO_DEPRECATE +#ifdef __GNUC__ +#pragma GCC diagnostic pop +#endif +#endif static const int static_category = alert::stats_notification; virtual std::string message() const override; @@ -1902,7 +1914,15 @@ namespace libtorrent { // interpret these values throughout the process' runtime. // // For more information, see the session-statistics_ section. + span counters() const; + +#ifdef TORRENT_NO_DEPRECATE + private: + // TODO: allocate this on the alert_stack in the future std::array const values; +#else + std::array const TORRENT_DEPRECATED_MEMBER values; +#endif }; #ifndef TORRENT_NO_DEPRECATE diff --git a/src/alert.cpp b/src/alert.cpp index fc55bb53b..81e41454f 100644 --- a/src/alert.cpp +++ b/src/alert.cpp @@ -1740,7 +1740,7 @@ namespace { } } - session_stats_alert::session_stats_alert(aux::stack_allocator&, counters const& cnt) + session_stats_alert::session_stats_alert(aux::stack_allocator&, struct counters const& cnt) : values(counters_to_array(cnt)) {} @@ -1759,6 +1759,11 @@ namespace { return ret; } + span session_stats_alert::counters() const + { + return values; + } + dht_stats_alert::dht_stats_alert(aux::stack_allocator& , std::vector table , std::vector requests) diff --git a/test/setup_transfer.cpp b/test/setup_transfer.cpp index 504ca7d1e..e89c30b53 100644 --- a/test/setup_transfer.cpp +++ b/test/setup_transfer.cpp @@ -150,7 +150,7 @@ std::map get_counters(lt::session& s) static std::vector metrics = session_stats_metrics(); for (auto const& m : metrics) - ret[m.name] = sa->values[static_cast(m.value_index)]; + ret[m.name] = sa->counters()[static_cast(m.value_index)]; return ret; } namespace {