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:
parent
e98dd14760
commit
393e705d5c
|
@ -83,9 +83,9 @@ namespace libtorrent
|
||||||
#endif
|
#endif
|
||||||
struct bdecode_node;
|
struct bdecode_node;
|
||||||
|
|
||||||
#if __cplusplus > 201103
|
|
||||||
namespace aux
|
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
|
// this enables us to compare a string_view against the std::string that's
|
||||||
// held by the std::map
|
// held by the std::map
|
||||||
struct strview_less
|
struct strview_less
|
||||||
|
@ -95,8 +95,26 @@ namespace libtorrent
|
||||||
bool operator()(T1 const& rhs, T2 const& lhs) const
|
bool operator()(T1 const& rhs, T2 const& lhs) const
|
||||||
{ return rhs < lhs; }
|
{ 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
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
// The ``entry`` class represents one node in a bencoded hierarchy. It works as a
|
// 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
|
// 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
|
// 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
|
// 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.
|
// to a list). The definition doesn't mention such a limit though.
|
||||||
#if __cplusplus <= 201103
|
using dictionary_type = aux::map_string<entry>;
|
||||||
using dictionary_type = std::map<std::string, entry>;
|
|
||||||
#else
|
|
||||||
using dictionary_type = std::map<std::string, entry, aux::strview_less>;
|
|
||||||
#endif
|
|
||||||
using string_type = std::string;
|
using string_type = std::string;
|
||||||
using list_type = std::vector<entry>;
|
using list_type = std::vector<entry>;
|
||||||
using integer_type = std::int64_t;
|
using integer_type = std::int64_t;
|
||||||
|
|
|
@ -90,46 +90,32 @@ namespace libtorrent
|
||||||
|
|
||||||
entry& entry::operator[](string_view key)
|
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);
|
dictionary_type::iterator i = dict().find(key);
|
||||||
#endif
|
|
||||||
if (i != dict().end()) return i->second;
|
if (i != dict().end()) return i->second;
|
||||||
dictionary_type::iterator ret = dict().insert(
|
dictionary_type::iterator ret = dict().emplace(
|
||||||
std::make_pair(key.to_string(), entry())).first;
|
std::piecewise_construct,
|
||||||
|
std::forward_as_tuple(key),
|
||||||
|
std::forward_as_tuple()).first;
|
||||||
return ret->second;
|
return ret->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
const entry& entry::operator[](string_view key) const
|
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);
|
dictionary_type::const_iterator i = dict().find(key);
|
||||||
#endif
|
|
||||||
if (i == dict().end()) throw_error();
|
if (i == dict().end()) throw_error();
|
||||||
return i->second;
|
return i->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
entry* entry::find_key(string_view key)
|
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);
|
dictionary_type::iterator i = dict().find(key);
|
||||||
#endif
|
|
||||||
if (i == dict().end()) return nullptr;
|
if (i == dict().end()) return nullptr;
|
||||||
return &i->second;
|
return &i->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
entry const* entry::find_key(string_view key) const
|
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);
|
dictionary_type::const_iterator i = dict().find(key);
|
||||||
#endif
|
|
||||||
if (i == dict().end()) return nullptr;
|
if (i == dict().end()) return nullptr;
|
||||||
return &i->second;
|
return &i->second;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue