db/PlaylistInfo: pass std::string_view to CompareName

This commit is contained in:
Max Kellermann 2020-04-02 19:46:19 +02:00
parent 02294a8236
commit 6197b29aa0
3 changed files with 10 additions and 8 deletions

View File

@ -23,6 +23,7 @@
#include "util/Compiler.h" #include "util/Compiler.h"
#include <string> #include <string>
#include <string_view>
#include <chrono> #include <chrono>
/** /**
@ -42,14 +43,15 @@ struct PlaylistInfo {
std::chrono::system_clock::time_point::min(); std::chrono::system_clock::time_point::min();
class CompareName { class CompareName {
const char *const name; const std::string_view name;
public: public:
constexpr CompareName(const char *_name):name(_name) {} constexpr CompareName(std::string_view _name) noexcept
:name(_name) {}
gcc_pure gcc_pure
bool operator()(const PlaylistInfo &pi) const noexcept { bool operator()(const PlaylistInfo &pi) const noexcept {
return pi.name.compare(name) == 0; return pi.name == name;
} }
}; };

View File

@ -24,10 +24,9 @@
#include <cassert> #include <cassert>
PlaylistVector::iterator PlaylistVector::iterator
PlaylistVector::find(const char *name) noexcept PlaylistVector::find(std::string_view name) noexcept
{ {
assert(holding_db_lock()); assert(holding_db_lock());
assert(name != nullptr);
return std::find_if(begin(), end(), return std::find_if(begin(), end(),
PlaylistInfo::CompareName(name)); PlaylistInfo::CompareName(name));
@ -51,7 +50,7 @@ PlaylistVector::UpdateOrInsert(PlaylistInfo &&pi) noexcept
} }
bool bool
PlaylistVector::erase(const char *name) noexcept PlaylistVector::erase(std::string_view name) noexcept
{ {
assert(holding_db_lock()); assert(holding_db_lock());

View File

@ -24,6 +24,7 @@
#include "util/Compiler.h" #include "util/Compiler.h"
#include <list> #include <list>
#include <string_view>
class PlaylistVector : protected std::list<PlaylistInfo> { class PlaylistVector : protected std::list<PlaylistInfo> {
protected: protected:
@ -31,7 +32,7 @@ protected:
* Caller must lock the #db_mutex. * Caller must lock the #db_mutex.
*/ */
gcc_pure gcc_pure
iterator find(const char *name) noexcept; iterator find(std::string_view name) noexcept;
public: public:
using std::list<PlaylistInfo>::empty; using std::list<PlaylistInfo>::empty;
@ -50,7 +51,7 @@ public:
/** /**
* Caller must lock the #db_mutex. * Caller must lock the #db_mutex.
*/ */
bool erase(const char *name) noexcept; bool erase(std::string_view name) noexcept;
}; };
#endif /* SONGVEC_H */ #endif /* SONGVEC_H */