clean up settings_pack (#665)

This commit is contained in:
Arvid Norberg 2016-04-29 12:01:33 -04:00
parent 46b33f920b
commit 84a35f5265
4 changed files with 59 additions and 35 deletions

View File

@ -39,6 +39,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/assert.hpp"
#include <string>
#include <array>
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 <typename T, size_t N>
void set(std::array<T, N>& 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 <typename T, size_t N>
T const& get(std::array<T, N> 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<std::string, settings_pack::num_string_settings> m_strings;
std::array<int, settings_pack::num_int_settings> m_ints;
// TODO: make this a bitfield
bool m_bools[settings_pack::num_bool_settings];
std::array<bool, settings_pack::num_bool_settings> m_bools;
};
#undef GET

View File

@ -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);

View File

@ -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);
}

View File

@ -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)