From 393e705d5c6fb2bf86b35c86aa79412b39b61b91 Mon Sep 17 00:00:00 2001 From: Andrei Kurushin Date: Fri, 20 Jan 2017 02:08:39 +0300 Subject: [PATCH] refactor entry::dictionary_type (#1568) consolidate conditional std::map string code enable C++14 string_view std::map for VS2015 --- include/libtorrent/entry.hpp | 28 +++++++++++++++++++++------- src/entry.cpp | 22 ++++------------------ 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/include/libtorrent/entry.hpp b/include/libtorrent/entry.hpp index 32ffd73dd..fadf57f31 100644 --- a/include/libtorrent/entry.hpp +++ b/include/libtorrent/entry.hpp @@ -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 using map_string = std::map; +#else + template + struct map_string : std::map + { + using base = std::map; + + 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; -#else - using dictionary_type = std::map; -#endif + using dictionary_type = aux::map_string; using string_type = std::string; using list_type = std::vector; using integer_type = std::int64_t; diff --git a/src/entry.cpp b/src/entry.cpp index a4ca6fae4..f18e3903f 100644 --- a/src/entry.cpp +++ b/src/entry.cpp @@ -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; }