1 /* Genre.cpp */
2 
3 /* Copyright (C) 2011-2020 Michael Lugmair (Lucio Carreras)
4  *
5  * This file is part of sayonara player
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11 
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16 
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 
22 
23 #include "Genre.h"
24 #include "Utils/Utils.h"
25 #include <QHash>
26 
27 struct Genre::Private
28 {
29 	QString name;
30 	GenreID id;
31 
calcIdGenre::Private32 	static GenreID calcId(const QString& name)
33 	{
34 		if(name.trimmed().isEmpty()){
35 			return 0;
36 		}
37 
38 		QByteArray nameData = name.trimmed().toLocal8Bit();
39 		return GenreID(qHash(nameData));
40 	}
41 
42 	Private()=default;
43 
44 	Private(const Private& other)=default;
45 	Private(Private&& other) noexcept=default;
46 	Private& operator=(const Private& other)=default;
47 	Private& operator=(Private&& other) noexcept=default;
48 };
49 
Genre()50 Genre::Genre()
51 {
52 	m = Pimpl::make<Private>();
53 	m->id = 0;
54 }
55 
Genre(const QString & name)56 Genre::Genre(const QString& name)
57 {
58 	m = Pimpl::make<Private>();
59 	m->name = name;
60 	m->id = m->calcId(name);
61 }
62 
63 Genre::~Genre() = default;
64 
Genre(const Genre & other)65 Genre::Genre(const Genre& other)
66 {
67 	m = Pimpl::make<Private>(*(other.m));
68 }
69 
Genre(Genre && other)70 Genre::Genre(Genre&& other) noexcept
71 {
72 	m = Pimpl::make<Private>(std::move(*(other.m)));
73 }
74 
operator =(const Genre & other)75 Genre& Genre::operator=(const Genre& other)
76 {
77 	*m = *(other.m);
78 	return *this;
79 }
80 
operator =(Genre && other)81 Genre& Genre::operator=(Genre&& other) noexcept
82 {
83 	*m = std::move(*(other.m));
84 	return *this;
85 }
86 
calculateId(const QString & name)87 GenreID Genre::calculateId(const QString& name)
88 {
89 	return Genre::Private::calcId(name);
90 }
91 
id() const92 GenreID Genre::id() const
93 {
94 	return m->id;
95 }
96 
name() const97 QString Genre::name() const
98 {
99 	return m->name;
100 }
101 
setName(const QString & name)102 void Genre::setName(const QString& name)
103 {
104 	m->name = name.trimmed();
105 	m->id = Genre::Private::calcId(m->name);
106 }
107 
isEqual(const Genre & other) const108 bool Genre::isEqual(const Genre& other) const
109 {
110 	return (m->id == other.id());
111 }
112 
operator ==(const Genre & other) const113 bool Genre::operator ==(const Genre& other) const
114 {
115 	return isEqual(other);
116 }
117 
operator <(const Genre & other) const118 bool Genre::operator <(const Genre& other) const
119 {
120 	return (m->id < other.id());
121 }
122 
operator >(const Genre & other) const123 bool Genre::operator >(const Genre& other) const
124 {
125 	return (m->id > other.id());
126 }
127 
128