1 /***************************************************************************
2 * Copyright (C) 2008-2014 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #ifndef NCMPCPP_PROXY_SONG_LIST_H
22 #define NCMPCPP_PROXY_SONG_LIST_H
27 /// This fancy class provides a way to use NC::Menu<T> template instantiations
28 /// with different Ts in a uniform manner. Note that since it provides methods
29 /// such as getSong or currentSong, it's biased towards menus that somehow
32 /// Dependent types are T and F. T is type of items stored inside Menu, whereas
33 /// F is a function that takes Menu<T>::Item and converts it into MPD::Song pointer.
34 /// For Menu<MPD::Song> this is just taking address of given MPD::Song, but e.g.
35 /// Menu<MPD::Item> returns not null pointer only if requested position actually
36 /// contains a song and not directory or playlist.
41 virtual ~Interface() { }
43 virtual bool empty() = 0;
44 virtual size_t size() = 0;
45 virtual size_t choice() = 0;
46 virtual void highlight(size_t pos
) = 0;
48 virtual bool isSelected(size_t pos
) = 0;
49 virtual void setSelected(size_t pos
, bool selected
) = 0;
51 virtual bool isBold(size_t pos
) = 0;
52 virtual void setBold(size_t pos
, bool bold
) = 0;
54 virtual MPD::Song
*getSong(size_t pos
) = 0;
55 virtual MPD::Song
*currentSong() = 0;
58 template <typename T
, typename F
> struct Impl
: Interface
60 typedef typename
NC::Menu
<T
> Menu
;
62 Impl(Menu
&menu
, F f
) : m_menu(menu
), m_song_getter(f
) { }
65 virtual bool empty() { return m_menu
.empty(); }
66 virtual size_t size() { return m_menu
.size(); }
67 virtual size_t choice() { return m_menu
.choice(); }
68 virtual void highlight(size_t pos
) { m_menu
.highlight(pos
); }
70 virtual bool isSelected(size_t pos
) {
71 assert(pos
< m_menu
.size());
72 return m_menu
[pos
].isSelected();
74 virtual void setSelected(size_t pos
, bool selected
) {
75 assert(pos
< m_menu
.size());
76 m_menu
[pos
].setSelected(selected
);
79 virtual bool isBold(size_t pos
) {
80 assert(pos
< m_menu
.size());
81 return m_menu
[pos
].isBold();
83 virtual void setBold(size_t pos
, bool bold
) {
84 assert(pos
< m_menu
.size());
85 m_menu
[pos
].setBold(bold
);
88 virtual MPD::Song
*getSong(size_t pos
) {
89 assert(pos
< m_menu
.size());
90 return m_song_getter(m_menu
[pos
]);
92 virtual MPD::Song
*currentSong() {
94 return getSong(m_menu
.choice());
104 std::shared_ptr
<Interface
> m_impl
;
109 template <typename T
, typename F
>
110 ProxySongList(typename
NC::Menu
<T
> &menu
, F f
) : m_impl(new Impl
<T
, F
>(menu
, f
)) { }
112 bool empty() const { return m_impl
->empty(); }
113 size_t size() const { return m_impl
->size(); }
114 size_t choice() const { return m_impl
->choice(); }
115 void highlight(size_t pos
) const { m_impl
->highlight(pos
); }
117 bool isSelected(size_t pos
) const { return m_impl
->isSelected(pos
); }
118 void setSelected(size_t pos
, bool selected
) const { m_impl
->setSelected(pos
, selected
); }
120 bool isBold(size_t pos
) const { return m_impl
->isBold(pos
); }
121 void setBold(size_t pos
, bool bold
) const{ m_impl
->setBold(pos
, bold
); }
123 MPD::Song
*getSong(size_t pos
) const { return m_impl
->getSong(pos
); }
124 MPD::Song
*currentSong() const { return m_impl
->currentSong(); }
126 /// @return true if there is no underlying menu object, false otherwise
127 operator bool() const { return m_impl
.get() != 0; }
130 #endif // NCMPCPP_PROXY_SONG_LIST_H