From 84a35f5265a6306d5e535fa4aa58936903647149 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Fri, 29 Apr 2016 12:01:33 -0400 Subject: [PATCH] clean up settings_pack (#665) --- include/libtorrent/aux_/session_settings.hpp | 58 +++++++++++++------- src/settings_pack.cpp | 15 +++-- test/test_dht_storage.cpp | 15 ++--- test/test_settings_pack.cpp | 6 ++ 4 files changed, 59 insertions(+), 35 deletions(-) diff --git a/include/libtorrent/aux_/session_settings.hpp b/include/libtorrent/aux_/session_settings.hpp index ad981fbe5..0a1152521 100644 --- a/include/libtorrent/aux_/session_settings.hpp +++ b/include/libtorrent/aux_/session_settings.hpp @@ -39,6 +39,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/assert.hpp" #include +#include namespace libtorrent { @@ -48,35 +49,54 @@ namespace libtorrent namespace libtorrent { namespace aux { -#define SET(type) \ - TORRENT_ASSERT((name & settings_pack::type_mask) == settings_pack:: type ## _type_base); \ - if ((name & settings_pack::type_mask) != settings_pack:: type ## _type_base) return; \ - m_ ## type ## s[name - settings_pack:: type ## _type_base] = value - -#define GET(type, default_val) \ - TORRENT_ASSERT((name & settings_pack::type_mask) == settings_pack:: type ## _type_base); \ - if ((name & settings_pack::type_mask) != settings_pack:: type ## _type_base) return default_val; \ - return m_ ## type ## s[name - settings_pack:: type ## _type_base] - struct TORRENT_EXTRA_EXPORT session_settings { friend void libtorrent::save_settings_to_dict( aux::session_settings const& s, entry::dictionary_type& sett); - void set_str(int name, std::string const& value) { SET(string); } - std::string const& get_str(int name) const { GET(string, m_strings[0]); } - void set_int(int name, int value) { SET(int); } - int get_int(int name) const { GET(int, 0); } - void set_bool(int name, bool value) { SET(bool); } - bool get_bool(int name) const { GET(bool, false); } + void set_str(int name, std::string const& value) + { set(m_strings, name, value, settings_pack::string_type_base); } + void set_int(int name, int value) + { set(m_ints, name, value, settings_pack::int_type_base); } + void set_bool(int name, bool value) + { set(m_bools, name, value, settings_pack::bool_type_base); } + + std::string const& get_str(int name) const + { return get(m_strings, name, settings_pack::string_type_base); } + int get_int(int name) const + { return get(m_ints, name, settings_pack::int_type_base); } + bool get_bool(int name) const + { return get(m_bools, name, settings_pack::bool_type_base); } session_settings(); private: - std::string m_strings[settings_pack::num_string_settings]; - int m_ints[settings_pack::num_int_settings]; + + template + void set(std::array& arr, int const name, T const& val, int const type) const + { + TORRENT_ASSERT((name & settings_pack::type_mask) == type); + if ((name & settings_pack::type_mask) != type) return; + int const index = name & settings_pack::index_mask; + TORRENT_ASSERT(index >= 0 && index < N); + arr[index] = val; + } + + template + T const& get(std::array const& arr, int const name, int const type) const + { + static T empty; + TORRENT_ASSERT((name & settings_pack::type_mask) == type); + if ((name & settings_pack::type_mask) != type) return empty; + int const index = name & settings_pack::index_mask; + TORRENT_ASSERT(index >= 0 && index < N); + return arr[index]; + } + + std::array m_strings; + std::array m_ints; // TODO: make this a bitfield - bool m_bools[settings_pack::num_bool_settings]; + std::array m_bools; }; #undef GET diff --git a/src/settings_pack.cpp b/src/settings_pack.cpp index 8e0ce5764..55df1e901 100644 --- a/src/settings_pack.cpp +++ b/src/settings_pack.cpp @@ -585,12 +585,13 @@ namespace libtorrent continue; // ignore settings that are out of bounds - int index = i->first & settings_pack::index_mask; + int const index = i->first & settings_pack::index_mask; + TORRENT_ASSERT_PRECOND(index >= 0 && index < settings_pack::num_string_settings); if (index < 0 || index >= settings_pack::num_string_settings) continue; sett.set_str(i->first, i->second); - str_setting_entry_t const& sa = str_settings[i->first & settings_pack::index_mask]; + str_setting_entry_t const& sa = str_settings[index]; if (sa.fun && ses && std::find(callbacks.begin(), callbacks.end(), sa.fun) == callbacks.end()) callbacks.push_back(sa.fun); @@ -604,12 +605,13 @@ namespace libtorrent continue; // ignore settings that are out of bounds - int index = i->first & settings_pack::index_mask; + int const index = i->first & settings_pack::index_mask; + TORRENT_ASSERT_PRECOND(index >= 0 && index < settings_pack::num_int_settings); if (index < 0 || index >= settings_pack::num_int_settings) continue; sett.set_int(i->first, i->second); - int_setting_entry_t const& sa = int_settings[i->first & settings_pack::index_mask]; + int_setting_entry_t const& sa = int_settings[index]; if (sa.fun && ses && std::find(callbacks.begin(), callbacks.end(), sa.fun) == callbacks.end()) callbacks.push_back(sa.fun); @@ -623,12 +625,13 @@ namespace libtorrent continue; // ignore settings that are out of bounds - int index = i->first & settings_pack::index_mask; + int const index = i->first & settings_pack::index_mask; + TORRENT_ASSERT_PRECOND(index >= 0 && index < settings_pack::num_bool_settings); if (index < 0 || index >= settings_pack::num_bool_settings) continue; sett.set_bool(i->first, i->second); - bool_setting_entry_t const& sa = bool_settings[i->first & settings_pack::index_mask]; + bool_setting_entry_t const& sa = bool_settings[index]; if (sa.fun && ses && std::find(callbacks.begin(), callbacks.end(), sa.fun) == callbacks.end()) callbacks.push_back(sa.fun); diff --git a/test/test_dht_storage.cpp b/test/test_dht_storage.cpp index 4246e76a4..428870322 100644 --- a/test/test_dht_storage.cpp +++ b/test/test_dht_storage.cpp @@ -202,15 +202,13 @@ TORRENT_TEST(set_custom) lt::session ses(p); TEST_EQUAL(g_storage_constructor_invoked, false); - bool r = ses.is_dht_running(); - TEST_CHECK(!r); + TEST_CHECK(ses.is_dht_running() == false); ses.set_dht_storage(dht_custom_storage_constructor); p.set_bool(settings_pack::enable_dht, true); ses.apply_settings(p); // async with dispatch - r = ses.is_dht_running(); - TEST_CHECK(r); + TEST_CHECK(ses.is_dht_running()); TEST_EQUAL(g_storage_constructor_invoked, true); } @@ -221,23 +219,20 @@ TORRENT_TEST(default_set_custom) p.set_bool(settings_pack::enable_dht, true); lt::session ses(p); - bool r = ses.is_dht_running(); - TEST_CHECK(r); + TEST_CHECK(ses.is_dht_running()); ses.set_dht_storage(dht_custom_storage_constructor); p.set_bool(settings_pack::enable_dht, false); ses.apply_settings(p); // async with dispatch - r = ses.is_dht_running(); - TEST_CHECK(!r); + TEST_CHECK(ses.is_dht_running() == false); TEST_EQUAL(g_storage_constructor_invoked, false); ses.set_dht_storage(dht_custom_storage_constructor); p.set_bool(settings_pack::enable_dht, true); ses.apply_settings(p); // async with dispatch - r = ses.is_dht_running(); - TEST_CHECK(r); + TEST_CHECK(ses.is_dht_running()); TEST_EQUAL(g_storage_constructor_invoked, true); } diff --git a/test/test_settings_pack.cpp b/test/test_settings_pack.cpp index bf05d52f8..8f4a18f97 100644 --- a/test/test_settings_pack.cpp +++ b/test/test_settings_pack.cpp @@ -105,6 +105,12 @@ TORRENT_TEST(test_name) #endif TEST_NAME(peer_turnover_interval); TEST_NAME(mmap_cache); + TEST_NAME(peer_fingerprint); + TEST_NAME(proxy_tracker_connections); + TEST_NAME(cache_size_volatile); + TEST_NAME(predictive_piece_announce); + TEST_NAME(max_metadata_size); + TEST_NAME(num_optimistic_unchoke_slots); } TORRENT_TEST(clear)