refactor entry::dictionary_type (#1568)

consolidate conditional std::map string code
enable C++14 string_view std::map for VS2015
This commit is contained in:
Andrei Kurushin 2017-01-20 02:08:39 +03:00 committed by Arvid Norberg
parent e98dd14760
commit 393e705d5c
2 changed files with 25 additions and 25 deletions

View File

@ -83,9 +83,9 @@ namespace libtorrent
#endif
struct bdecode_node;
#if __cplusplus > 201103
namespace aux
{
#if (__cplusplus > 201103) || (defined _MSC_VER && _MSC_VER >= 1900)
// this enables us to compare a string_view against the std::string that's
// held by the std::map
struct strview_less
@ -95,8 +95,26 @@ namespace libtorrent
bool operator()(T1 const& rhs, T2 const& lhs) const
{ return rhs < lhs; }
};
};
template<class T> using map_string = std::map<std::string, T, aux::strview_less>;
#else
template<class T>
struct map_string : std::map<std::string, T>
{
using base = std::map<std::string, T>;
typename base::iterator find(const string_view& key)
{
return this->base::find(key.to_string());
}
typename base::const_iterator find(const string_view& key) const
{
return this->base::find(key.to_string());
}
};
#endif
}
// The ``entry`` class represents one node in a bencoded hierarchy. It works as a
// variant type, it can be either a list, a dictionary (``std::map``), an integer
@ -108,11 +126,7 @@ namespace libtorrent
// the key is always a string. If a generic entry would be allowed
// as a key, sorting would become a problem (e.g. to compare a string
// to a list). The definition doesn't mention such a limit though.
#if __cplusplus <= 201103
using dictionary_type = std::map<std::string, entry>;
#else
using dictionary_type = std::map<std::string, entry, aux::strview_less>;
#endif
using dictionary_type = aux::map_string<entry>;
using string_type = std::string;
using list_type = std::vector<entry>;
using integer_type = std::int64_t;

View File

@ -90,46 +90,32 @@ namespace libtorrent
entry& entry::operator[](string_view key)
{
#if __cplusplus <= 201103
dictionary_type::iterator i = dict().find(key.to_string());
#else
dictionary_type::iterator i = dict().find(key);
#endif
if (i != dict().end()) return i->second;
dictionary_type::iterator ret = dict().insert(
std::make_pair(key.to_string(), entry())).first;
dictionary_type::iterator ret = dict().emplace(
std::piecewise_construct,
std::forward_as_tuple(key),
std::forward_as_tuple()).first;
return ret->second;
}
const entry& entry::operator[](string_view key) const
{
#if __cplusplus <= 201103
dictionary_type::const_iterator i = dict().find(key.to_string());
#else
dictionary_type::const_iterator i = dict().find(key);
#endif
if (i == dict().end()) throw_error();
return i->second;
}
entry* entry::find_key(string_view key)
{
#if __cplusplus <= 201103
dictionary_type::iterator i = dict().find(key.to_string());
#else
dictionary_type::iterator i = dict().find(key);
#endif
if (i == dict().end()) return nullptr;
return &i->second;
}
entry const* entry::find_key(string_view key) const
{
#if __cplusplus <= 201103
dictionary_type::const_iterator i = dict().find(key.to_string());
#else
dictionary_type::const_iterator i = dict().find(key);
#endif
if (i == dict().end()) return nullptr;
return &i->second;
}