comparators: generalize objects a bit

This commit is contained in:
Andrzej Rybczak
2012-09-08 14:34:46 +02:00
parent 691e0322e7
commit 8fb88b7181
12 changed files with 54 additions and 34 deletions

View File

@@ -20,9 +20,10 @@
#include <locale>
#include "comparators.h"
#include "settings.h"
bool LocaleStringComparison::hasTheWord(const std::string &s) const
namespace {//
bool hasTheWord(const std::string &s)
{
return s.length() >= 4
&& (s[0] == 't' || s[0] == 'T')
@@ -31,6 +32,8 @@ bool LocaleStringComparison::hasTheWord(const std::string &s) const
&& (s[3] == ' ');
}
}
int LocaleStringComparison::operator()(const std::string &a, const std::string &b) const
{
const char *ac = a.c_str();
@@ -48,9 +51,7 @@ int LocaleStringComparison::operator()(const std::string &a, const std::string &
);
}
CaseInsensitiveSorting::CaseInsensitiveSorting(): cmp(std::locale(""), Config.ignore_leading_the) { }
bool CaseInsensitiveSorting::operator()(const MPD::Item &a, const MPD::Item &b) const
bool LocaleBasedItemSorting::operator()(const MPD::Item &a, const MPD::Item &b) const
{
bool result = false;
if (a.type == b.type)
@@ -58,22 +59,23 @@ bool CaseInsensitiveSorting::operator()(const MPD::Item &a, const MPD::Item &b)
switch (a.type)
{
case MPD::itDirectory:
result = cmp(getBasename(a.name), getBasename(b.name)) < 0;
result = m_cmp(getBasename(a.name), getBasename(b.name)) < 0;
break;
case MPD::itPlaylist:
result = cmp(a.name, b.name) < 0;
result = m_cmp(a.name, b.name) < 0;
break;
case MPD::itSong:
switch (Config.browser_sort_mode)
switch (m_sort_mode)
{
case smName:
result = operator()(*a.song, *b.song);
result = m_cmp(*a.song, *b.song);
break;
case smMTime:
result = a.song->getMTime() > b.song->getMTime();
break;
case smCustomFormat:
result = cmp(a.song->toString(Config.browser_sort_format), b.song->toString(Config.browser_sort_format)) < 0;
result = m_cmp(a.song->toString(Config.browser_sort_format),
b.song->toString(Config.browser_sort_format)) < 0;
break;
}
break;

View File

@@ -23,14 +23,14 @@
#include <string>
#include "mpdpp.h"
#include "settings.h"
#include "menu.h"
class LocaleStringComparison
{
std::locale m_locale;
bool m_ignore_the;
bool hasTheWord(const std::string &s) const;
public:
LocaleStringComparison(const std::locale &loc, bool ignore_the)
: m_locale(loc), m_ignore_the(ignore_the) { }
@@ -38,27 +38,41 @@ public:
int operator()(const std::string &a, const std::string &b) const;
};
class CaseInsensitiveSorting
class LocaleBasedSorting
{
LocaleStringComparison cmp;
LocaleStringComparison m_cmp;
public:
CaseInsensitiveSorting();
LocaleBasedSorting(const std::locale loc, bool ignore_the) : m_cmp(loc, ignore_the) { }
bool operator()(const std::string &a, const std::string &b) const {
return cmp(a, b) < 0;
return m_cmp(a, b) < 0;
}
bool operator()(const MPD::Song &a, const MPD::Song &b) const {
return cmp(a.getName(), b.getName()) < 0;
return m_cmp(a.getName(), b.getName()) < 0;
}
template <typename A, typename B>
bool operator()(const std::pair<A, B> &a, const std::pair<A, B> &b) const {
return cmp(a.first, b.first) < 0;
return m_cmp(a.first, b.first) < 0;
}
};
class LocaleBasedItemSorting
{
LocaleBasedSorting m_cmp;
SortMode m_sort_mode;
public:
LocaleBasedItemSorting(const std::locale loc, bool ignore_the, SortMode mode)
: m_cmp(loc, ignore_the), m_sort_mode(mode) { }
bool operator()(const MPD::Item &a, const MPD::Item &b) const;
bool operator()(const NC::Menu<MPD::Item>::Item &a, const NC::Menu<MPD::Item>::Item &b) const {
return (*this)(a.value(), b.value());
}
};
#endif // _UTILITY_COMPARATORS