summaryrefslogtreecommitdiff
path: root/src/proxy_song_list.h
blob: 68c7db268f284eeef6400357206ee77c3f09f043 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/***************************************************************************
 *   Copyright (C) 2008-2014 by Andrzej Rybczak                            *
 *   electricityispower@gmail.com                                          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.              *
 ***************************************************************************/

#ifndef NCMPCPP_PROXY_SONG_LIST_H
#define NCMPCPP_PROXY_SONG_LIST_H

#include "menu.h"
#include "song.h"

/// This fancy class provides a way to use NC::Menu<T> template instantiations
/// with different Ts in a uniform manner. Note that since it provides methods
/// such as getSong or currentSong, it's biased towards menus that somehow
/// contain songs.
///
/// Dependent types are T and F. T is type of items stored inside Menu, whereas
/// F is a function that takes Menu<T>::Item and converts it into MPD::Song pointer.
/// For Menu<MPD::Song> this is just taking address of given MPD::Song, but e.g.
/// Menu<MPD::Item> returns not null pointer only if requested position actually
/// contains a song and not directory or playlist.
class ProxySongList
{
	struct Interface
	{
		virtual ~Interface() { }
		
		virtual bool empty() = 0;
		virtual size_t size() = 0;
		virtual size_t choice() = 0;
		virtual void highlight(size_t pos) = 0;
		
		virtual bool isSelected(size_t pos) = 0;
		virtual void setSelected(size_t pos, bool selected) = 0;
		
		virtual bool isBold(size_t pos) = 0;
		virtual void setBold(size_t pos, bool bold) = 0;
		
		virtual MPD::Song *getSong(size_t pos) = 0;
		virtual MPD::Song *currentSong() = 0;
	};
	
	template <typename T, typename F> struct Impl : Interface
	{
		typedef typename NC::Menu<T> Menu;
		
		Impl(Menu &menu, F f) : m_menu(menu), m_song_getter(f) { }
		virtual ~Impl() { }
		
		virtual bool empty() { return m_menu.empty(); }
		virtual size_t size() { return m_menu.size(); }
		virtual size_t choice() { return m_menu.choice(); }
		virtual void highlight(size_t pos) { m_menu.highlight(pos); }
		
		virtual bool isSelected(size_t pos) {
			assert(pos < m_menu.size());
			return m_menu[pos].isSelected();
		}
		virtual void setSelected(size_t pos, bool selected) {
			assert(pos < m_menu.size());
			m_menu[pos].setSelected(selected);
		}
		
		virtual bool isBold(size_t pos) {
			assert(pos < m_menu.size());
			return m_menu[pos].isBold();
		}
		virtual void setBold(size_t pos, bool bold) {
			assert(pos < m_menu.size());
			m_menu[pos].setBold(bold);
		}
		
		virtual MPD::Song *getSong(size_t pos) {
			assert(pos < m_menu.size());
			return m_song_getter(m_menu[pos]);
		}
		virtual MPD::Song *currentSong() {
			if (!m_menu.empty())
				return getSong(m_menu.choice());
			else
				return 0;
		}
		
	private:
		Menu &m_menu;
		F m_song_getter;
	};
	
	std::shared_ptr<Interface> m_impl;
	
public:
	ProxySongList() { }
	
	template <typename T, typename F>
	ProxySongList(typename NC::Menu<T> &menu, F f) : m_impl(new Impl<T, F>(menu, f)) { }
	
	bool empty() const { return m_impl->empty(); }
	size_t size() const { return m_impl->size(); }
	size_t choice() const { return m_impl->choice(); }
	void highlight(size_t pos) const { m_impl->highlight(pos); }
	
	bool isSelected(size_t pos) const { return m_impl->isSelected(pos); }
	void setSelected(size_t pos, bool selected) const { m_impl->setSelected(pos, selected); }
	
	bool isBold(size_t pos) const { return m_impl->isBold(pos); }
	void setBold(size_t pos, bool bold) const{ m_impl->setBold(pos, bold); }
	
	MPD::Song *getSong(size_t pos) const { return m_impl->getSong(pos); }
	MPD::Song *currentSong() const { return m_impl->currentSong(); }
	
	/// @return true if there is no underlying menu object, false otherwise
	operator bool() const { return m_impl.get() != 0; }
};

#endif // NCMPCPP_PROXY_SONG_LIST_H